]> Cypherpunks.ru repositories - govpn.git/blob - src/govpn/cmd/govpn-server/udp.go
Do not check nonce against buckets in TCP mode
[govpn.git] / src / govpn / cmd / govpn-server / 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         "time"
25
26         "govpn"
27 )
28
29 type UDPSender struct {
30         conn *net.UDPConn
31         addr *net.UDPAddr
32 }
33
34 func (c UDPSender) Write(data []byte) (int, error) {
35         return c.conn.WriteToUDP(data, c.addr)
36 }
37
38 func (c UDPSender) Reorderable() bool {
39         return true
40 }
41
42 func startUDP(sink chan Pkt) {
43         bind, err := net.ResolveUDPAddr("udp", *bindAddr)
44         ready := make(chan struct{})
45         if err != nil {
46                 log.Fatalln("Can not resolve bind address:", err)
47         }
48         lconn, err := net.ListenUDP("udp", bind)
49         if err != nil {
50                 log.Fatalln("Can not listen on UDP:", err)
51         }
52         log.Println("Listening on UDP", *bindAddr)
53         go func() {
54                 buf := make([]byte, govpn.MTU)
55                 var n int
56                 var raddr *net.UDPAddr
57                 var err error
58                 for {
59                         <-ready
60                         lconn.SetReadDeadline(time.Now().Add(time.Second))
61                         n, raddr, err = lconn.ReadFromUDP(buf)
62                         if err != nil {
63                                 sink <- Pkt{ready: ready}
64                                 continue
65                         }
66                         sink <- Pkt{
67                                 raddr.String(),
68                                 UDPSender{lconn, raddr},
69                                 buf[:n],
70                                 ready,
71                         }
72                 }
73         }()
74         ready <- struct{}{}
75 }