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/>.
29 // TCPSender prepends size prefix to each outgoing packet.
30 type TCPSender struct {
34 func (c TCPSender) Write(data []byte) (int, error) {
35 size := make([]byte, 2)
36 binary.BigEndian.PutUint16(size, uint16(len(data)))
37 return c.conn.Write(append(size, data...))
40 func (c TCPSender) Reorderable() bool {
44 func startTCP() (govpn.RemoteConn, chan []byte, chan struct{}) {
45 remote, err := net.ResolveTCPAddr("tcp", *remoteAddr)
47 log.Fatalln("Can not resolve remote address:", err)
49 c, err := net.DialTCP("tcp", nil, remote)
52 log.Fatalln("Can not connect TCP:", err)
54 sink := make(chan []byte)
55 ready := make(chan struct{})
56 go handleTCP(c, sink, ready)
57 go func() { ready <- struct{}{} }()
58 return conn, sink, ready
61 func handleTCP(conn *net.TCPConn, sink chan []byte, ready chan struct{}) {
65 sizeBuf := make([]byte, 2)
68 buf := make([]byte, govpn.MTU)
72 n, err = conn.Read(sizeBuf[sizeNbuf:2])
81 sizeNeed = binary.BigEndian.Uint16(sizeBuf)
82 if int(sizeNeed) > govpn.MTU-2 {
83 log.Println("Invalid TCP size, skipping")
92 n, err = conn.Read(buf[bufN:sizeNeed])
100 sink <- buf[:sizeNeed]