]> Cypherpunks.ru repositories - govpn.git/blob - src/govpn/cmd/govpn-client/udp.go
b4398f327a0b69931fa87350e34be004b97a7719
[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         log.Println("Connected to UDP:" + *remoteAddr)
40
41         hs := govpn.HandshakeStart(*remoteAddr, conn, conf)
42         buf := make([]byte, govpn.MTU)
43         var n int
44         var timeouts int
45         var peer *govpn.Peer
46         var terminator chan struct{}
47 MainCycle:
48         for {
49                 select {
50                 case <-termination:
51                         break MainCycle
52                 default:
53                 }
54
55                 conn.SetReadDeadline(time.Now().Add(time.Second))
56                 n, err = conn.Read(buf)
57                 if timeouts == timeout {
58                         log.Println("Timeouted")
59                         timeouted <- struct{}{}
60                         break
61                 }
62                 if err != nil {
63                         timeouts++
64                         continue
65                 }
66                 if peer != nil {
67                         if peer.PktProcess(buf[:n], tap, true) {
68                                 timeouts = 0
69                         } else {
70                                 log.Println("Unauthenticated packet")
71                                 timeouts++
72                         }
73                         if atomic.LoadInt64(&peer.BytesIn)+atomic.LoadInt64(&peer.BytesOut) > govpn.MaxBytesPerKey {
74                                 log.Println("Need rehandshake")
75                                 terminator <- struct{}{}
76                                 terminator = nil
77                                 rehandshaking <- struct{}{}
78                                 break MainCycle
79                         }
80                         continue
81                 }
82                 if govpn.IDsCache.Find(buf[:n]) == nil {
83                         log.Println("Invalid identity in handshake packet")
84                         continue
85                 }
86                 timeouts = 0
87                 peer = hs.Client(buf[:n])
88                 if peer == nil {
89                         continue
90                 }
91                 log.Println("Handshake completed")
92                 knownPeers = govpn.KnownPeers(map[string]**govpn.Peer{*remoteAddr: &peer})
93                 if firstUpCall {
94                         go govpn.ScriptCall(*upPath, *ifaceName)
95                         firstUpCall = false
96                 }
97                 hs.Zero()
98                 terminator = make(chan struct{})
99                 go func() {
100                         heartbeat := time.NewTicker(peer.Timeout)
101                         var data []byte
102                 Processor:
103                         for {
104                                 select {
105                                 case <-heartbeat.C:
106                                         peer.EthProcess(nil)
107                                 case <-terminator:
108                                         break Processor
109                                 case data = <-tap.Sink:
110                                         peer.EthProcess(data)
111                                 }
112                         }
113                         heartbeat.Stop()
114                         peer.Zero()
115                 }()
116         }
117         if terminator != nil {
118                 terminator <- struct{}{}
119         }
120         if hs != nil {
121                 hs.Zero()
122         }
123 }