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