2 GoVPN -- simple secure free software virtual private network daemon
3 Copyright (C) 2014-2019 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, version 3 of the License.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program. If not, see <http://www.gnu.org/licenses/>.
26 "cypherpunks.ru/govpn"
29 func (c *Client) startUDP() {
30 remote, err := net.ResolveUDPAddr("udp", c.config.RemoteAddress)
32 c.Error <- fmt.Errorf("Can not resolve remote address: %s", err)
35 conn, err := net.DialUDP("udp", nil, remote)
37 c.Error <- fmt.Errorf("Can not connect remote address: %s", err)
40 govpn.Printf(`[connected remote="%s"]`, c.config.RemoteAddress)
42 hs := govpn.HandshakeStart(c.config.RemoteAddress, conn, c.config.Peer)
43 buf := make([]byte, c.config.MTU*2)
47 var terminator chan struct{}
48 timeout := int(c.config.Peer.Timeout.Seconds())
57 if err = conn.SetReadDeadline(time.Now().Add(time.Second)); err != nil {
61 n, err = conn.Read(buf)
62 if timeouts == timeout {
63 govpn.Printf(`[connection-timeouted remote="%s"]`, c.config.RemoteAddress)
64 c.timeouted <- struct{}{}
72 if peer.PktProcess(buf[:n], c.tap, true) {
75 govpn.Printf(`[packet-unauthenticated remote="%s"]`, c.config.RemoteAddress)
78 if atomic.LoadUint64(&peer.BytesIn)+atomic.LoadUint64(&peer.BytesOut) > govpn.MaxBytesPerKey {
79 govpn.Printf(`[rehandshake-required remote="%s"]`, c.config.RemoteAddress)
80 c.rehandshaking <- struct{}{}
85 if c.idsCache.Find(buf[:n]) == nil {
86 govpn.Printf(`[identity-invalid remote="%s"]`, c.config.RemoteAddress)
90 peer = hs.Client(buf[:n])
94 govpn.Printf(`[handshake-completed remote="%s"]`, c.config.RemoteAddress)
95 c.knownPeers.Store(c.config.RemoteAddress, &peer)
97 go govpn.ScriptCall(c.config.UpPath, c.config.InterfaceName, c.config.RemoteAddress)
101 terminator = make(chan struct{})
102 go govpn.PeerTapProcessor(peer, c.tap, terminator)
104 if terminator != nil {
105 terminator <- struct{}{}
110 if err = conn.Close(); err != nil {