2 GoVPN -- simple secure free software virtual private network daemon
3 Copyright (C) 2014-2015 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/>.
32 func startTCP(timeouted, rehandshaking, termination chan struct{}) {
33 remote, err := net.ResolveTCPAddr("tcp", *remoteAddr)
35 log.Fatalln("Can not resolve remote address:", err)
37 conn, err := net.DialTCP("tcp", nil, remote)
39 log.Fatalln("Can not connect to address:", err)
41 log.Println("Connected to TCP:" + *remoteAddr)
42 handleTCP(conn, timeouted, rehandshaking, termination)
45 func handleTCP(conn *net.TCPConn, timeouted, rehandshaking, termination chan struct{}) {
46 hs := govpn.HandshakeStart(*remoteAddr, conn, conf)
47 buf := make([]byte, govpn.MTU)
52 var terminator chan struct{}
60 if prev == govpn.MTU {
61 log.Println("Timeouted waiting for the packet")
62 timeouted <- struct{}{}
66 conn.SetReadDeadline(time.Now().Add(time.Duration(timeout) * time.Second))
67 n, err = conn.Read(buf[prev:])
69 log.Println("Connection timeouted")
70 timeouted <- struct{}{}
75 peerId := govpn.IDsCache.Find(buf[:prev])
79 peer = hs.Client(buf[:prev])
84 log.Println("Handshake completed")
85 knownPeers = govpn.KnownPeers(map[string]**govpn.Peer{*remoteAddr: &peer})
87 go govpn.ScriptCall(*upPath, *ifaceName)
91 terminator = make(chan struct{})
93 heartbeat := time.NewTicker(peer.Timeout)
102 case data = <-tap.Sink:
103 peer.EthProcess(data)
118 nonceExpectation := make([]byte, govpn.NonceSize)
119 binary.BigEndian.PutUint64(nonceExpectation, peer.NonceExpect)
120 peer.NonceCipher.Encrypt(nonceExpectation, nonceExpectation)
130 if prev == govpn.MTU {
131 log.Println("Timeouted waiting for the packet")
132 timeouted <- struct{}{}
135 conn.SetReadDeadline(time.Now().Add(time.Duration(timeout) * time.Second))
136 n, err = conn.Read(buf[prev:])
138 log.Println("Connection timeouted")
139 timeouted <- struct{}{}
144 if prev < govpn.MinPktLength {
147 i = bytes.Index(buf[:prev], nonceExpectation)
151 if !peer.PktProcess(buf[:i+govpn.NonceSize], tap, false) {
152 log.Println("Unauthenticated packet, dropping connection")
153 timeouted <- struct{}{}
156 if atomic.LoadInt64(&peer.BytesIn)+atomic.LoadInt64(&peer.BytesOut) > govpn.MaxBytesPerKey {
157 log.Println("Need rehandshake")
158 rehandshaking <- struct{}{}
161 binary.BigEndian.PutUint64(nonceExpectation, peer.NonceExpect)
162 peer.NonceCipher.Encrypt(nonceExpectation, nonceExpectation)
163 copy(buf, buf[i+govpn.NonceSize:prev])
164 prev = prev - i - govpn.NonceSize
167 if terminator != nil {
168 terminator <- struct{}{}