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