]> Cypherpunks.ru repositories - govpn.git/blob - src/govpn/cmd/govpn-client/tcp.go
Ability to use TCP as a base transport
[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         "io"
24         "log"
25         "net"
26
27         "govpn"
28 )
29
30 // TCPSender prepends size prefix to each outgoing packet.
31 type TCPSender struct {
32         conn *net.TCPConn
33 }
34
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...))
39 }
40
41 func startTCP() (io.Writer, chan []byte, chan struct{}) {
42         remote, err := net.ResolveTCPAddr("tcp", *remoteAddr)
43         if err != nil {
44                 log.Fatalln("Can not resolve remote address:", err)
45         }
46         c, err := net.DialTCP("tcp", nil, remote)
47         conn := TCPSender{c}
48         if err != nil {
49                 log.Fatalln("Can not connect TCP:", err)
50         }
51         sink := make(chan []byte)
52         ready := make(chan struct{})
53         go func() {
54                 var err error
55                 var n int
56                 var sizeNbuf int
57                 sizeBuf := make([]byte, 2)
58                 var sizeNeed uint16
59                 var bufN uint16
60                 buf := make([]byte, govpn.MTU)
61                 for {
62                         <-ready
63                         if sizeNbuf != 2 {
64                                 n, err = c.Read(sizeBuf[sizeNbuf:2])
65                                 if err != nil {
66                                         break
67                                 }
68                                 sizeNbuf += n
69                                 if sizeNbuf == 2 {
70                                         sizeNeed = binary.BigEndian.Uint16(sizeBuf)
71                                         if sizeNeed > uint16(govpn.MTU)-2 {
72                                                 log.Println("Invalid TCP size, skipping")
73                                                 sizeNbuf = 0
74                                                 sink <- nil
75                                                 continue
76                                         }
77                                         bufN = 0
78                                 }
79                         }
80                 ReadMore:
81                         if sizeNeed != bufN {
82                                 n, err = c.Read(buf[bufN:sizeNeed])
83                                 if err != nil {
84                                         break
85                                 }
86                                 bufN += uint16(n)
87                                 goto ReadMore
88                         }
89                         sizeNbuf = 0
90                         sink <- buf[:sizeNeed]
91                 }
92         }()
93         go func() { ready <- struct{}{} }()
94         return conn, sink, ready
95 }