]> Cypherpunks.ru repositories - govpn.git/blob - src/govpn/cmd/govpn-client/udp.go
0df7ab13a4b2c23f2b223932f24dc4a74a49af41
[govpn.git] / src / govpn / cmd / govpn-client / udp.go
1 /*
2 GoVPN -- simple secure free software virtual private network daemon
3 Copyright (C) 2014-2015 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         "log"
23         "net"
24         "sync/atomic"
25         "time"
26
27         "govpn"
28 )
29
30 func startUDP(timeouted, rehandshaking, termination chan struct{}) {
31         remote, err := net.ResolveUDPAddr("udp", *remoteAddr)
32         if err != nil {
33                 log.Fatalln("Can not resolve remote address:", err)
34         }
35         conn, err := net.DialUDP("udp", nil, remote)
36         if err != nil {
37                 log.Fatalln("Can not listen on UDP:", err)
38         }
39
40         hs := govpn.HandshakeStart(*remoteAddr, conn, conf)
41         buf := make([]byte, govpn.MTU)
42         var n int
43         var timeouts int
44         var peer *govpn.Peer
45         var terminator chan struct{}
46 MainCycle:
47         for {
48                 select {
49                 case <-termination:
50                         break MainCycle
51                 default:
52                 }
53
54                 conn.SetReadDeadline(time.Now().Add(time.Second))
55                 n, err = conn.Read(buf)
56                 if timeouts == timeout {
57                         log.Println("Timeouted")
58                         timeouted <- struct{}{}
59                         break
60                 }
61                 if err != nil {
62                         timeouts++
63                         continue
64                 }
65                 if peer != nil {
66                         if peer.PktProcess(buf[:n], tap, true) {
67                                 timeouts = 0
68                         } else {
69                                 timeouts++
70                         }
71                         if atomic.LoadInt64(&peer.BytesIn)+atomic.LoadInt64(&peer.BytesOut) > govpn.MaxBytesPerKey {
72                                 log.Println("Need rehandshake")
73                                 terminator <- struct{}{}
74                                 terminator = nil
75                                 rehandshaking <- struct{}{}
76                                 break MainCycle
77                         }
78                         continue
79                 }
80                 if govpn.IDsCache.Find(buf[:n]) == nil {
81                         log.Println("Invalid identity in handshake packet")
82                         continue
83                 }
84                 timeouts = 0
85                 peer = hs.Client(buf[:n])
86                 if peer == nil {
87                         continue
88                 }
89                 log.Println("Handshake completed")
90                 knownPeers = govpn.KnownPeers(map[string]**govpn.Peer{*remoteAddr: &peer})
91                 if firstUpCall {
92                         go govpn.ScriptCall(*upPath, *ifaceName)
93                         firstUpCall = false
94                 }
95                 hs.Zero()
96                 terminator = make(chan struct{})
97                 go func() {
98                         heartbeat := time.NewTicker(peer.Timeout)
99                         var data []byte
100                 Processor:
101                         for {
102                                 select {
103                                 case <-heartbeat.C:
104                                         peer.EthProcess(nil)
105                                 case <-terminator:
106                                         break Processor
107                                 case data = <-tap.Sink:
108                                         peer.EthProcess(data)
109                                 }
110                         }
111                         heartbeat.Stop()
112                         peer.Zero()
113                 }()
114         }
115         if terminator != nil {
116                 terminator <- struct{}{}
117         }
118         if hs != nil {
119                 hs.Zero()
120         }
121 }