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/>.
28 "cypherpunks.ru/govpn"
31 func (c *Client) startTCP() {
32 remote, err := net.ResolveTCPAddr("tcp", c.config.RemoteAddress)
34 c.Error <- fmt.Errorf("Can not resolve remote address: %s", err)
37 conn, err := net.DialTCP("tcp", nil, remote)
39 c.Error <- fmt.Errorf("Can not connect to address: %s", err)
42 govpn.Printf(`[connected remote="%s"]`, c.config.RemoteAddress)
46 func (c *Client) handleTCP(conn *net.TCPConn) {
47 hs := govpn.HandshakeStart(c.config.RemoteAddress, conn, c.config.Peer)
48 buf := make([]byte, 2*(govpn.EnclessEnlargeSize+c.config.MTU)+c.config.MTU)
53 var terminator chan struct{}
62 govpn.Printf(`[packet-timeouted remote="%s"]`, c.config.RemoteAddress)
63 c.timeouted <- struct{}{}
67 if err = conn.SetReadDeadline(time.Now().Add(c.config.Peer.Timeout)); err != nil {
71 n, err = conn.Read(buf[prev:])
73 govpn.Printf(`[connection-timeouted remote="%s"]`, c.config.RemoteAddress)
74 c.timeouted <- struct{}{}
79 peerId := c.idsCache.Find(buf[:prev])
83 peer = hs.Client(buf[:prev])
88 govpn.Printf(`[handshake-completed remote="%s"]`, c.config.RemoteAddress)
89 c.knownPeers = govpn.KnownPeers(map[string]**govpn.Peer{c.config.RemoteAddress: &peer})
91 go govpn.ScriptCall(c.config.UpPath, c.config.InterfaceName, c.config.RemoteAddress)
95 terminator = make(chan struct{})
96 go govpn.PeerTapProcessor(peer, c.tap, terminator)
111 case <-c.termination:
115 if prev == len(buf) {
116 govpn.Printf(`[packet-timeouted remote="%s"]`, c.config.RemoteAddress)
117 c.timeouted <- struct{}{}
120 if err = conn.SetReadDeadline(time.Now().Add(c.config.Peer.Timeout)); err != nil {
124 n, err = conn.Read(buf[prev:])
126 govpn.Printf(`[connection-timeouted remote="%s"]`, c.config.RemoteAddress)
127 c.timeouted <- struct{}{}
132 if prev < govpn.MinPktLength {
135 i = bytes.Index(buf[:prev], peer.NonceExpect)
139 if !peer.PktProcess(buf[:i+govpn.NonceSize], c.tap, false) {
140 govpn.Printf(`[packet-unauthenticated remote="%s"]`, c.config.RemoteAddress)
141 c.timeouted <- struct{}{}
144 if atomic.LoadUint64(&peer.BytesIn)+atomic.LoadUint64(&peer.BytesOut) > govpn.MaxBytesPerKey {
145 govpn.Printf(`[rehandshake-required remote="%s"]`, c.config.RemoteAddress)
146 c.rehandshaking <- struct{}{}
149 copy(buf, buf[i+govpn.NonceSize:prev])
150 prev = prev - i - govpn.NonceSize
153 if terminator != nil {
154 terminator <- struct{}{}
157 if err = conn.Close(); err != nil {