2 GoVPN -- simple secure free software virtual private network daemon
3 Copyright (C) 2014-2016 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 startTCP(timeouted, rehandshaking, termination chan struct{}) {
32 remote, err := net.ResolveTCPAddr("tcp", *remoteAddr)
34 log.Fatalln("Can not resolve remote address:", err)
36 conn, err := net.DialTCP("tcp", nil, remote)
38 log.Fatalln("Can not connect to address:", err)
40 log.Println("Connected to TCP:" + *remoteAddr)
41 handleTCP(conn, timeouted, rehandshaking, termination)
44 func handleTCP(conn *net.TCPConn, timeouted, rehandshaking, termination chan struct{}) {
45 hs := govpn.HandshakeStart(*remoteAddr, conn, conf)
46 buf := make([]byte, 2*(govpn.EnclessEnlargeSize+*mtu)+*mtu)
51 var terminator chan struct{}
60 log.Println("Timeouted waiting for the packet")
61 timeouted <- struct{}{}
65 conn.SetReadDeadline(time.Now().Add(time.Duration(timeout) * time.Second))
66 n, err = conn.Read(buf[prev:])
68 log.Println("Connection timeouted")
69 timeouted <- struct{}{}
74 peerId := idsCache.Find(buf[:prev])
78 peer = hs.Client(buf[:prev])
83 log.Println("Handshake completed")
84 knownPeers = govpn.KnownPeers(map[string]**govpn.Peer{*remoteAddr: &peer})
86 go govpn.ScriptCall(*upPath, *ifaceName)
90 terminator = make(chan struct{})
92 heartbeat := time.NewTicker(peer.Timeout)
101 case data = <-tap.Sink:
102 peer.EthProcess(data)
117 nonceExpectation := make([]byte, govpn.NonceSize)
118 peer.NonceExpectation(nonceExpectation)
128 if prev == len(buf) {
129 log.Println("Timeouted waiting for the packet")
130 timeouted <- struct{}{}
133 conn.SetReadDeadline(time.Now().Add(time.Duration(timeout) * time.Second))
134 n, err = conn.Read(buf[prev:])
136 log.Println("Connection timeouted")
137 timeouted <- struct{}{}
142 if prev < govpn.MinPktLength {
145 i = bytes.Index(buf[:prev], nonceExpectation)
149 if !peer.PktProcess(buf[:i+govpn.NonceSize], tap, false) {
150 log.Println("Unauthenticated packet, dropping connection")
151 timeouted <- struct{}{}
154 if atomic.LoadInt64(&peer.BytesIn)+atomic.LoadInt64(&peer.BytesOut) > govpn.MaxBytesPerKey {
155 log.Println("Need rehandshake")
156 rehandshaking <- struct{}{}
159 peer.NonceExpectation(nonceExpectation)
160 copy(buf, buf[i+govpn.NonceSize:prev])
161 prev = prev - i - govpn.NonceSize
164 if terminator != nil {
165 terminator <- struct{}{}