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{})
53 go handleTCP(c, sink, ready)
54 go func() { ready <- struct{}{} }()
55 return conn, sink, ready
58 func handleTCP(conn *net.TCPConn, sink chan []byte, ready chan struct{}) {
62 sizeBuf := make([]byte, 2)
65 buf := make([]byte, govpn.MTU)
69 n, err = conn.Read(sizeBuf[sizeNbuf:2])
78 sizeNeed = binary.BigEndian.Uint16(sizeBuf)
79 if int(sizeNeed) > govpn.MTU-2 {
80 log.Println("Invalid TCP size, skipping")
89 n, err = conn.Read(buf[bufN:sizeNeed])
97 sink <- buf[:sizeNeed]