]> Cypherpunks.ru repositories - govpn.git/blob - src/cypherpunks.ru/govpn/cmd/govpn-client/tcp.go
Merge branch 'develop'
[govpn.git] / src / cypherpunks.ru / govpn / cmd / govpn-client / tcp.go
1 /*
2 GoVPN -- simple secure free software virtual private network daemon
3 Copyright (C) 2014-2016 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         "bytes"
23         "log"
24         "net"
25         "sync/atomic"
26         "time"
27
28         "cypherpunks.ru/govpn"
29 )
30
31 func startTCP(timeouted, rehandshaking, termination chan struct{}) {
32         remote, err := net.ResolveTCPAddr("tcp", *remoteAddr)
33         if err != nil {
34                 log.Fatalln("Can not resolve remote address:", err)
35         }
36         conn, err := net.DialTCP("tcp", nil, remote)
37         if err != nil {
38                 log.Fatalln("Can not connect to address:", err)
39         }
40         log.Println("Connected to TCP:" + *remoteAddr)
41         handleTCP(conn, timeouted, rehandshaking, termination)
42 }
43
44 func handleTCP(conn *net.TCPConn, timeouted, rehandshaking, termination chan struct{}) {
45         hs := govpn.HandshakeStart(*remoteAddr, conn, conf)
46         buf := make([]byte, 2*(govpn.EnclessEnlargeSize+*mtu)+*mtu)
47         var n int
48         var err error
49         var prev int
50         var peer *govpn.Peer
51         var terminator chan struct{}
52 HandshakeCycle:
53         for {
54                 select {
55                 case <-termination:
56                         break HandshakeCycle
57                 default:
58                 }
59                 if prev == len(buf) {
60                         log.Println("Timeouted waiting for the packet")
61                         timeouted <- struct{}{}
62                         break HandshakeCycle
63                 }
64
65                 conn.SetReadDeadline(time.Now().Add(time.Duration(timeout) * time.Second))
66                 n, err = conn.Read(buf[prev:])
67                 if err != nil {
68                         log.Println("Connection timeouted")
69                         timeouted <- struct{}{}
70                         break HandshakeCycle
71                 }
72
73                 prev += n
74                 peerId := idsCache.Find(buf[:prev])
75                 if peerId == nil {
76                         continue
77                 }
78                 peer = hs.Client(buf[:prev])
79                 prev = 0
80                 if peer == nil {
81                         continue
82                 }
83                 log.Println("Handshake completed")
84                 knownPeers = govpn.KnownPeers(map[string]**govpn.Peer{*remoteAddr: &peer})
85                 if firstUpCall {
86                         go govpn.ScriptCall(*upPath, *ifaceName, *remoteAddr)
87                         firstUpCall = false
88                 }
89                 hs.Zero()
90                 terminator = make(chan struct{})
91                 go func() {
92                         heartbeat := time.NewTicker(peer.Timeout)
93                         var data []byte
94                 Processor:
95                         for {
96                                 select {
97                                 case <-heartbeat.C:
98                                         peer.EthProcess(nil)
99                                 case <-terminator:
100                                         break Processor
101                                 case data = <-tap.Sink:
102                                         peer.EthProcess(data)
103                                 }
104                         }
105                         heartbeat.Stop()
106                         peer.Zero()
107                 }()
108                 break HandshakeCycle
109         }
110         if hs != nil {
111                 hs.Zero()
112         }
113         if peer == nil {
114                 return
115         }
116
117         nonceExpectation := make([]byte, govpn.NonceSize)
118         peer.NonceExpectation(nonceExpectation)
119         prev = 0
120         var i int
121 TransportCycle:
122         for {
123                 select {
124                 case <-termination:
125                         break TransportCycle
126                 default:
127                 }
128                 if prev == len(buf) {
129                         log.Println("Timeouted waiting for the packet")
130                         timeouted <- struct{}{}
131                         break TransportCycle
132                 }
133                 conn.SetReadDeadline(time.Now().Add(time.Duration(timeout) * time.Second))
134                 n, err = conn.Read(buf[prev:])
135                 if err != nil {
136                         log.Println("Connection timeouted")
137                         timeouted <- struct{}{}
138                         break TransportCycle
139                 }
140                 prev += n
141         CheckMore:
142                 if prev < govpn.MinPktLength {
143                         continue
144                 }
145                 i = bytes.Index(buf[:prev], nonceExpectation)
146                 if i == -1 {
147                         continue
148                 }
149                 if !peer.PktProcess(buf[:i+govpn.NonceSize], tap, false) {
150                         log.Println("Unauthenticated packet, dropping connection")
151                         timeouted <- struct{}{}
152                         break TransportCycle
153                 }
154                 if atomic.LoadUint64(&peer.BytesIn)+atomic.LoadUint64(&peer.BytesOut) > govpn.MaxBytesPerKey {
155                         log.Println("Need rehandshake")
156                         rehandshaking <- struct{}{}
157                         break TransportCycle
158                 }
159                 peer.NonceExpectation(nonceExpectation)
160                 copy(buf, buf[i+govpn.NonceSize:prev])
161                 prev = prev - i - govpn.NonceSize
162                 goto CheckMore
163         }
164         if terminator != nil {
165                 terminator <- struct{}{}
166         }
167         peer.Zero()
168         conn.Close()
169 }