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 (c TCPSender) Reorderable() bool {
43 func startTCP(sink chan Pkt) {
44 bind, err := net.ResolveTCPAddr("tcp", *bindAddr)
46 log.Fatalln("Can not resolve bind address:", err)
48 listener, err := net.ListenTCP("tcp", bind)
50 log.Fatalln("Can not listen on TCP:", err)
52 log.Println("Listening on TCP", *bindAddr)
55 conn, _ := listener.AcceptTCP()
56 ready := make(chan struct{}, 1)
57 go handleTCP(conn, sink, ready)
63 func handleTCP(conn net.Conn, sink chan Pkt, ready chan struct{}) {
64 addr := conn.RemoteAddr().String()
68 sizeBuf := make([]byte, 2)
71 buf := make([]byte, govpn.MTU)
75 n, err = conn.Read(sizeBuf[sizeNbuf:2])
81 sink <- Pkt{ready: ready}
84 sizeNeed = binary.BigEndian.Uint16(sizeBuf)
85 if int(sizeNeed) > govpn.MTU-2 {
86 log.Println("Invalid TCP size, skipping")
88 sink <- Pkt{ready: ready}
95 n, err = conn.Read(buf[bufN:sizeNeed])