]> Cypherpunks.ru repositories - govpn.git/blob - src/govpn/cmd/govpn-client/tcp.go
dcf7b14b88f503add1378aadcb1c3005df76ff9f
[govpn.git] / src / govpn / cmd / govpn-client / tcp.go
1 /*
2 GoVPN -- simple secure free software virtual private network daemon
3 Copyright (C) 2014-2015 Sergey Matveev <stargrave@stargrave.org>
4
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.
9
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.
14
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/>.
17 */
18
19 package main
20
21 import (
22         "encoding/binary"
23         "log"
24         "net"
25
26         "govpn"
27 )
28
29 // TCPSender prepends size prefix to each outgoing packet.
30 type TCPSender struct {
31         conn *net.TCPConn
32 }
33
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...))
38 }
39
40 func (c TCPSender) Reorderable() bool {
41         return false
42 }
43
44 func startTCP() (govpn.RemoteConn, chan []byte, chan struct{}) {
45         remote, err := net.ResolveTCPAddr("tcp", *remoteAddr)
46         if err != nil {
47                 log.Fatalln("Can not resolve remote address:", err)
48         }
49         c, err := net.DialTCP("tcp", nil, remote)
50         conn := TCPSender{c}
51         if err != nil {
52                 log.Fatalln("Can not connect TCP:", err)
53         }
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
59 }
60
61 func handleTCP(conn *net.TCPConn, sink chan []byte, ready chan struct{}) {
62         var err error
63         var n int
64         var sizeNbuf int
65         sizeBuf := make([]byte, 2)
66         var sizeNeed uint16
67         var bufN uint16
68         buf := make([]byte, govpn.MTU)
69         for {
70                 <-ready
71                 if sizeNbuf != 2 {
72                         n, err = conn.Read(sizeBuf[sizeNbuf:2])
73                         if err != nil {
74                                 break
75                         }
76                         sizeNbuf += n
77                         if sizeNbuf != 2 {
78                                 sink <- nil
79                                 continue
80                         }
81                         sizeNeed = binary.BigEndian.Uint16(sizeBuf)
82                         if int(sizeNeed) > govpn.MTU-2 {
83                                 log.Println("Invalid TCP size, skipping")
84                                 sizeNbuf = 0
85                                 sink <- nil
86                                 continue
87                         }
88                         bufN = 0
89                 }
90         ReadMore:
91                 if sizeNeed != bufN {
92                         n, err = conn.Read(buf[bufN:sizeNeed])
93                         if err != nil {
94                                 break
95                         }
96                         bufN += uint16(n)
97                         goto ReadMore
98                 }
99                 sizeNbuf = 0
100                 sink <- buf[:sizeNeed]
101         }
102 }