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 type TCPSender struct {
33 func (c TCPSender) Write(data []byte) (int, error) {
34 size := make([]byte, 2)
35 binary.BigEndian.PutUint16(size, uint16(len(data)))
36 return c.conn.Write(append(size, data...))
39 func startTCP(sink *chan Pkt) {
40 bind, err := net.ResolveTCPAddr("tcp", *bindAddr)
42 log.Fatalln("Can not resolve bind address:", err)
44 listener, err := net.ListenTCP("tcp", bind)
46 log.Fatalln("Can not listen on TCP:", err)
48 log.Println("Listening on TCP", *bindAddr)
51 conn, _ := listener.AcceptTCP()
52 ready := make(chan struct{}, 1)
53 go func(conn *net.TCPConn, ready chan struct{}) {
54 addr := conn.RemoteAddr().String()
58 sizeBuf := make([]byte, 2)
61 buf := make([]byte, govpn.MTU)
65 n, err = conn.Read(sizeBuf[sizeNbuf:2])
71 sizeNeed = binary.BigEndian.Uint16(sizeBuf)
72 if sizeNeed > uint16(govpn.MTU)-2 {
73 log.Println("Invalid TCP size, skipping")
75 *sink <- Pkt{ready: ready}
83 n, err = conn.Read(buf[bufN:sizeNeed])