2 GoVPN -- simple secure free software virtual private network daemon
3 Copyright (C) 2014-2015 Sergey Matveev <stargrave@stargrave.org>
5 This program is free software: you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation, either version 3 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
30 // TCPSender prepends size prefix to each outgoing packet.
31 type TCPSender struct {
35 func (c TCPSender) Write(data []byte) (int, error) {
36 size := make([]byte, 2)
37 binary.BigEndian.PutUint16(size, uint16(len(data)))
38 return c.conn.Write(append(size, data...))
41 func startTCP() (io.Writer, chan []byte, chan struct{}) {
42 remote, err := net.ResolveTCPAddr("tcp", *remoteAddr)
44 log.Fatalln("Can not resolve remote address:", err)
46 c, err := net.DialTCP("tcp", nil, remote)
49 log.Fatalln("Can not connect TCP:", err)
51 sink := make(chan []byte)
52 ready := make(chan struct{})
57 sizeBuf := make([]byte, 2)
60 buf := make([]byte, govpn.MTU)
64 n, err = c.Read(sizeBuf[sizeNbuf:2])
70 sizeNeed = binary.BigEndian.Uint16(sizeBuf)
71 if sizeNeed > uint16(govpn.MTU)-2 {
72 log.Println("Invalid TCP size, skipping")
82 n, err = c.Read(buf[bufN:sizeNeed])
90 sink <- buf[:sizeNeed]
93 go func() { ready <- struct{}{} }()
94 return conn, sink, ready