]> Cypherpunks.ru repositories - govpn.git/blob - src/cypherpunks.ru/govpn/client/tcp.go
go vet/lint
[govpn.git] / src / cypherpunks.ru / govpn / client / tcp.go
1 /*
2 GoVPN -- simple secure free software virtual private network daemon
3 Copyright (C) 2014-2017 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 client
20
21 import (
22         "bytes"
23         "fmt"
24         "net"
25         "sync/atomic"
26         "time"
27
28         "cypherpunks.ru/govpn"
29 )
30
31 func (c *Client) startTCP() {
32         remote, err := net.ResolveTCPAddr("tcp", c.config.RemoteAddress)
33         if err != nil {
34                 c.Error <- fmt.Errorf("Can not resolve remote address: %s", err)
35                 return
36         }
37         conn, err := net.DialTCP("tcp", nil, remote)
38         if err != nil {
39                 c.Error <- fmt.Errorf("Can not connect to address: %s", err)
40                 return
41         }
42         govpn.Printf(`[connected remote="%s"]`, c.config.RemoteAddress)
43         c.handleTCP(conn)
44 }
45
46 func (c *Client) handleTCP(conn *net.TCPConn) {
47         hs := govpn.HandshakeStart(c.config.RemoteAddress, conn, c.config.Peer)
48         buf := make([]byte, 2*(govpn.EnclessEnlargeSize+c.config.MTU)+c.config.MTU)
49         var n int
50         var err error
51         var prev int
52         var peer *govpn.Peer
53         var terminator chan struct{}
54 HandshakeCycle:
55         for {
56                 select {
57                 case <-c.termination:
58                         break HandshakeCycle
59                 default:
60                 }
61                 if prev == len(buf) {
62                         govpn.Printf(`[packet-timeouted remote="%s"]`, c.config.RemoteAddress)
63                         c.timeouted <- struct{}{}
64                         break HandshakeCycle
65                 }
66
67                 if err = conn.SetReadDeadline(time.Now().Add(c.config.Peer.Timeout)); err != nil {
68                         c.Error <- err
69                         break HandshakeCycle
70                 }
71                 n, err = conn.Read(buf[prev:])
72                 if err != nil {
73                         govpn.Printf(`[connection-timeouted remote="%s"]`, c.config.RemoteAddress)
74                         c.timeouted <- struct{}{}
75                         break HandshakeCycle
76                 }
77
78                 prev += n
79                 peerID := c.idsCache.Find(buf[:prev])
80                 if peerID == nil {
81                         continue
82                 }
83                 peer = hs.Client(buf[:prev])
84                 prev = 0
85                 if peer == nil {
86                         continue
87                 }
88                 govpn.Printf(`[handshake-completed remote="%s"]`, c.config.RemoteAddress)
89                 c.knownPeers = govpn.KnownPeers(map[string]**govpn.Peer{c.config.RemoteAddress: &peer})
90                 if c.firstUpCall {
91                         go govpn.ScriptCall(c.config.UpPath, c.config.InterfaceName, c.config.RemoteAddress)
92                         c.firstUpCall = false
93                 }
94                 hs.Zero()
95                 terminator = make(chan struct{})
96                 go govpn.PeerTapProcessor(peer, c.tap, terminator)
97                 break HandshakeCycle
98         }
99         if hs != nil {
100                 hs.Zero()
101         }
102         if peer == nil {
103                 return
104         }
105
106         prev = 0
107         var i int
108 TransportCycle:
109         for {
110                 select {
111                 case <-c.termination:
112                         break TransportCycle
113                 default:
114                 }
115                 if prev == len(buf) {
116                         govpn.Printf(`[packet-timeouted remote="%s"]`, c.config.RemoteAddress)
117                         c.timeouted <- struct{}{}
118                         break TransportCycle
119                 }
120                 if err = conn.SetReadDeadline(time.Now().Add(c.config.Peer.Timeout)); err != nil {
121                         c.Error <- err
122                         break TransportCycle
123                 }
124                 n, err = conn.Read(buf[prev:])
125                 if err != nil {
126                         govpn.Printf(`[connection-timeouted remote="%s"]`, c.config.RemoteAddress)
127                         c.timeouted <- struct{}{}
128                         break TransportCycle
129                 }
130                 prev += n
131         CheckMore:
132                 if prev < govpn.MinPktLength {
133                         continue
134                 }
135                 i = bytes.Index(buf[:prev], peer.NonceExpect)
136                 if i == -1 {
137                         continue
138                 }
139                 if !peer.PktProcess(buf[:i+govpn.NonceSize], c.tap, false) {
140                         govpn.Printf(`[packet-unauthenticated remote="%s"]`, c.config.RemoteAddress)
141                         c.timeouted <- struct{}{}
142                         break TransportCycle
143                 }
144                 if atomic.LoadUint64(&peer.BytesIn)+atomic.LoadUint64(&peer.BytesOut) > govpn.MaxBytesPerKey {
145                         govpn.Printf(`[rehandshake-required remote="%s"]`, c.config.RemoteAddress)
146                         c.rehandshaking <- struct{}{}
147                         break TransportCycle
148                 }
149                 copy(buf, buf[i+govpn.NonceSize:prev])
150                 prev = prev - i - govpn.NonceSize
151                 goto CheckMore
152         }
153         if terminator != nil {
154                 terminator <- struct{}{}
155         }
156         peer.Zero()
157         if err = conn.Close(); err != nil {
158                 c.Error <- err
159         }
160 }