]> Cypherpunks.ru repositories - govpn.git/blob - src/govpn/cmd/govpn-server/tcp.go
d02565c6138c8ac08a94c60b8d7ed10a2ba92313
[govpn.git] / src / govpn / cmd / govpn-server / 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 type TCPSender struct {
30         conn net.Conn
31 }
32
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...))
37 }
38
39 func (c TCPSender) Reorderable() bool {
40         return false
41 }
42
43 func startTCP(sink chan Pkt) {
44         bind, err := net.ResolveTCPAddr("tcp", *bindAddr)
45         if err != nil {
46                 log.Fatalln("Can not resolve bind address:", err)
47         }
48         listener, err := net.ListenTCP("tcp", bind)
49         if err != nil {
50                 log.Fatalln("Can not listen on TCP:", err)
51         }
52         log.Println("Listening on TCP", *bindAddr)
53         go func() {
54                 for {
55                         conn, _ := listener.AcceptTCP()
56                         ready := make(chan struct{}, 1)
57                         go handleTCP(conn, sink, ready)
58                         ready <- struct{}{}
59                 }
60         }()
61 }
62
63 func handleTCP(conn net.Conn, sink chan Pkt, ready chan struct{}) {
64         addr := conn.RemoteAddr().String()
65         var err error
66         var n int
67         var sizeNbuf int
68         sizeBuf := make([]byte, 2)
69         var sizeNeed uint16
70         var bufN uint16
71         buf := make([]byte, govpn.MTU)
72         for {
73                 <-ready
74                 if sizeNbuf != 2 {
75                         n, err = conn.Read(sizeBuf[sizeNbuf:2])
76                         if err != nil {
77                                 break
78                         }
79                         sizeNbuf += n
80                         if sizeNbuf != 2 {
81                                 sink <- Pkt{ready: ready}
82                                 continue
83                         }
84                         sizeNeed = binary.BigEndian.Uint16(sizeBuf)
85                         if int(sizeNeed) > govpn.MTU-2 {
86                                 log.Println("Invalid TCP size, skipping")
87                                 sizeNbuf = 0
88                                 sink <- Pkt{ready: ready}
89                                 continue
90                         }
91                         bufN = 0
92                 }
93         ReadMore:
94                 if sizeNeed != bufN {
95                         n, err = conn.Read(buf[bufN:sizeNeed])
96                         if err != nil {
97                                 break
98                         }
99                         bufN += uint16(n)
100                         goto ReadMore
101                 }
102                 sizeNbuf = 0
103                 sink <- Pkt{
104                         addr,
105                         TCPSender{conn},
106                         buf[:sizeNeed],
107                         ready,
108                 }
109         }
110 }