]> Cypherpunks.ru repositories - govpn.git/blob - client/tcp.go
Raise copyright years
[govpn.git] / client / tcp.go
1 /*
2 GoVPN -- simple secure free software virtual private network daemon
3 Copyright (C) 2014-2020 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, version 3 of the License.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 GNU General Public License for more details.
13
14 You should have received a copy of the GNU General Public License
15 along with this program.  If not, see <http://www.gnu.org/licenses/>.
16 */
17
18 package client
19
20 import (
21         "bytes"
22         "fmt"
23         "net"
24         "sync/atomic"
25         "time"
26
27         "go.cypherpunks.ru/govpn/v7"
28 )
29
30 func (c *Client) startTCP() {
31         remote, err := net.ResolveTCPAddr("tcp", c.config.RemoteAddress)
32         if err != nil {
33                 c.Error <- fmt.Errorf("Can not resolve remote address: %s", err)
34                 return
35         }
36         conn, err := net.DialTCP("tcp", nil, remote)
37         if err != nil {
38                 c.Error <- fmt.Errorf("Can not connect to address: %s", err)
39                 return
40         }
41         govpn.Printf(`[connected remote="%s"]`, c.config.RemoteAddress)
42         c.handleTCP(conn)
43 }
44
45 func (c *Client) handleTCP(conn *net.TCPConn) {
46         hs := govpn.HandshakeStart(c.config.RemoteAddress, conn, c.config.Peer)
47         buf := make([]byte, 2*(govpn.EnclessEnlargeSize+c.config.MTU)+c.config.MTU)
48         var n int
49         var err error
50         var prev int
51         var peer *govpn.Peer
52         var terminator chan struct{}
53 HandshakeCycle:
54         for {
55                 select {
56                 case <-c.termination:
57                         break HandshakeCycle
58                 default:
59                 }
60                 if prev == len(buf) {
61                         govpn.Printf(`[packet-timeouted remote="%s"]`, c.config.RemoteAddress)
62                         c.timeouted <- struct{}{}
63                         break HandshakeCycle
64                 }
65
66                 if err = conn.SetReadDeadline(time.Now().Add(c.config.Peer.Timeout)); err != nil {
67                         c.Error <- err
68                         break HandshakeCycle
69                 }
70                 n, err = conn.Read(buf[prev:])
71                 if err != nil {
72                         govpn.Printf(`[connection-timeouted remote="%s"]`, c.config.RemoteAddress)
73                         c.timeouted <- struct{}{}
74                         break HandshakeCycle
75                 }
76
77                 prev += n
78                 peerID := c.idsCache.Find(buf[:prev])
79                 if peerID == nil {
80                         continue
81                 }
82                 peer = hs.Client(buf[:prev])
83                 prev = 0
84                 if peer == nil {
85                         continue
86                 }
87                 govpn.Printf(`[handshake-completed remote="%s"]`, c.config.RemoteAddress)
88                 c.knownPeers.Store(c.config.RemoteAddress, &peer)
89                 if c.firstUpCall {
90                         go govpn.ScriptCall(c.config.UpPath, c.config.InterfaceName, c.config.RemoteAddress)
91                         c.firstUpCall = false
92                 }
93                 hs.Zero()
94                 terminator = make(chan struct{})
95                 go govpn.PeerTapProcessor(peer, c.tap, terminator)
96                 break HandshakeCycle
97         }
98         if hs != nil {
99                 hs.Zero()
100         }
101         if peer == nil {
102                 return
103         }
104
105         prev = 0
106         var i int
107 TransportCycle:
108         for {
109                 select {
110                 case <-c.termination:
111                         break TransportCycle
112                 default:
113                 }
114                 if prev == len(buf) {
115                         govpn.Printf(`[packet-timeouted remote="%s"]`, c.config.RemoteAddress)
116                         c.timeouted <- struct{}{}
117                         break TransportCycle
118                 }
119                 if err = conn.SetReadDeadline(time.Now().Add(c.config.Peer.Timeout)); err != nil {
120                         c.Error <- err
121                         break TransportCycle
122                 }
123                 n, err = conn.Read(buf[prev:])
124                 if err != nil {
125                         govpn.Printf(`[connection-timeouted remote="%s"]`, c.config.RemoteAddress)
126                         c.timeouted <- struct{}{}
127                         break TransportCycle
128                 }
129                 prev += n
130         CheckMore:
131                 if prev < govpn.MinPktLength {
132                         continue
133                 }
134                 i = bytes.Index(buf[:prev], peer.NonceExpect)
135                 if i == -1 {
136                         continue
137                 }
138                 if !peer.PktProcess(buf[:i+govpn.NonceSize], c.tap, false) {
139                         govpn.Printf(`[packet-unauthenticated remote="%s"]`, c.config.RemoteAddress)
140                         c.timeouted <- struct{}{}
141                         break TransportCycle
142                 }
143                 if atomic.LoadUint64(&peer.BytesIn)+atomic.LoadUint64(&peer.BytesOut) > govpn.MaxBytesPerKey {
144                         govpn.Printf(`[rehandshake-required remote="%s"]`, c.config.RemoteAddress)
145                         c.rehandshaking <- struct{}{}
146                         break TransportCycle
147                 }
148                 copy(buf, buf[i+govpn.NonceSize:prev])
149                 prev = prev - i - govpn.NonceSize
150                 goto CheckMore
151         }
152         if terminator != nil {
153                 terminator <- struct{}{}
154         }
155         peer.Zero()
156         if err = conn.Close(); err != nil {
157                 c.Error <- err
158         }
159 }