]> Cypherpunks.ru repositories - govpn.git/blob - src/cypherpunks.ru/govpn/client/udp.go
9527daeee457f89b5a6cae3297be296828cc1d32
[govpn.git] / src / cypherpunks.ru / govpn / client / udp.go
1 /*
2 GoVPN -- simple secure free software virtual private network daemon
3 Copyright (C) 2014-2019 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         "fmt"
22         "net"
23         "sync/atomic"
24         "time"
25
26         "cypherpunks.ru/govpn"
27 )
28
29 func (c *Client) startUDP() {
30         remote, err := net.ResolveUDPAddr("udp", c.config.RemoteAddress)
31         if err != nil {
32                 c.Error <- fmt.Errorf("Can not resolve remote address: %s", err)
33                 return
34         }
35         conn, err := net.DialUDP("udp", nil, remote)
36         if err != nil {
37                 c.Error <- fmt.Errorf("Can not connect remote address: %s", err)
38                 return
39         }
40         govpn.Printf(`[connected remote="%s"]`, c.config.RemoteAddress)
41
42         hs := govpn.HandshakeStart(c.config.RemoteAddress, conn, c.config.Peer)
43         buf := make([]byte, c.config.MTU*2)
44         var n int
45         var timeouts int
46         var peer *govpn.Peer
47         var terminator chan struct{}
48         timeout := int(c.config.Peer.Timeout.Seconds())
49 MainCycle:
50         for {
51                 select {
52                 case <-c.termination:
53                         break MainCycle
54                 default:
55                 }
56
57                 if err = conn.SetReadDeadline(time.Now().Add(time.Second)); err != nil {
58                         c.Error <- err
59                         break MainCycle
60                 }
61                 n, err = conn.Read(buf)
62                 if timeouts == timeout {
63                         govpn.Printf(`[connection-timeouted remote="%s"]`, c.config.RemoteAddress)
64                         c.timeouted <- struct{}{}
65                         break
66                 }
67                 if err != nil {
68                         timeouts++
69                         continue
70                 }
71                 if peer != nil {
72                         if peer.PktProcess(buf[:n], c.tap, true) {
73                                 timeouts = 0
74                         } else {
75                                 govpn.Printf(`[packet-unauthenticated remote="%s"]`, c.config.RemoteAddress)
76                                 timeouts++
77                         }
78                         if atomic.LoadUint64(&peer.BytesIn)+atomic.LoadUint64(&peer.BytesOut) > govpn.MaxBytesPerKey {
79                                 govpn.Printf(`[rehandshake-required remote="%s"]`, c.config.RemoteAddress)
80                                 c.rehandshaking <- struct{}{}
81                                 break MainCycle
82                         }
83                         continue
84                 }
85                 if c.idsCache.Find(buf[:n]) == nil {
86                         govpn.Printf(`[identity-invalid remote="%s"]`, c.config.RemoteAddress)
87                         continue
88                 }
89                 timeouts = 0
90                 peer = hs.Client(buf[:n])
91                 if peer == nil {
92                         continue
93                 }
94                 govpn.Printf(`[handshake-completed remote="%s"]`, c.config.RemoteAddress)
95                 c.knownPeers.Store(c.config.RemoteAddress, &peer)
96                 if c.firstUpCall {
97                         go govpn.ScriptCall(c.config.UpPath, c.config.InterfaceName, c.config.RemoteAddress)
98                         c.firstUpCall = false
99                 }
100                 hs.Zero()
101                 terminator = make(chan struct{})
102                 go govpn.PeerTapProcessor(peer, c.tap, terminator)
103         }
104         if terminator != nil {
105                 terminator <- struct{}{}
106         }
107         if hs != nil {
108                 hs.Zero()
109         }
110         if err = conn.Close(); err != nil {
111                 c.Error <- err
112         }
113 }