2 GoVPN -- simple secure free software virtual private network daemon
3 Copyright (C) 2014-2017 Sergey Matveev <stargrave@stargrave.org>
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.
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.
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/>.
27 "cypherpunks.ru/govpn"
30 func (c *Client) startUDP() {
31 remote, err := net.ResolveUDPAddr("udp", c.config.RemoteAddress)
33 c.Error <- fmt.Errorf("Can not resolve remote address: %s", err)
36 conn, err := net.DialUDP("udp", nil, remote)
38 c.Error <- fmt.Errorf("Can not connect remote address: %s", err)
41 govpn.Printf(`[connected remote="%s"]`, c.config.RemoteAddress)
43 hs := govpn.HandshakeStart(c.config.RemoteAddress, conn, c.config.Peer)
44 buf := make([]byte, c.config.MTU*2)
48 var terminator chan struct{}
49 timeout := int(c.config.Peer.Timeout.Seconds())
58 if err = conn.SetReadDeadline(time.Now().Add(time.Second)); err != nil {
62 n, err = conn.Read(buf)
63 if timeouts == timeout {
64 govpn.Printf(`[connection-timeouted remote="%s"]`, c.config.RemoteAddress)
65 c.timeouted <- struct{}{}
73 if peer.PktProcess(buf[:n], c.tap, true) {
76 govpn.Printf(`[packet-unauthenticated remote="%s"]`, c.config.RemoteAddress)
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{}{}
86 if c.idsCache.Find(buf[:n]) == nil {
87 govpn.Printf(`[identity-invalid remote="%s"]`, c.config.RemoteAddress)
91 peer = hs.Client(buf[:n])
95 govpn.Printf(`[handshake-completed remote="%s"]`, c.config.RemoteAddress)
96 c.knownPeers = govpn.KnownPeers(map[string]**govpn.Peer{c.config.RemoteAddress: &peer})
98 go govpn.ScriptCall(c.config.UpPath, c.config.InterfaceName, c.config.RemoteAddress)
102 terminator = make(chan struct{})
103 go govpn.PeerTapProcessor(peer, c.tap, terminator)
105 if terminator != nil {
106 terminator <- struct{}{}
111 if err = conn.Close(); err != nil {