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 remote address:", err)
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, govpn.MTU)
51 var terminator chan struct{}
59 if prev == govpn.MTU {
63 conn.SetReadDeadline(time.Now().Add(time.Duration(timeout) * time.Second))
64 n, err = conn.Read(buf[prev:])
66 // Either EOFed or timeouted
71 peerId := govpn.IDsCache.Find(buf[:prev])
75 peer = hs.Client(buf[:prev])
80 log.Println("Handshake completed")
81 knownPeers = govpn.KnownPeers(map[string]**govpn.Peer{*remoteAddr: &peer})
83 go govpn.ScriptCall(*upPath, *ifaceName)
87 terminator = make(chan struct{})
89 heartbeat := time.NewTicker(peer.Timeout)
98 case data = <-tap.Sink:
114 nonceExpectation := make([]byte, govpn.NonceSize)
115 binary.BigEndian.PutUint64(nonceExpectation, peer.NonceExpect)
116 peer.NonceCipher.Encrypt(nonceExpectation, nonceExpectation)
126 if prev == govpn.MTU {
127 timeouted <- struct{}{}
130 conn.SetReadDeadline(time.Now().Add(time.Duration(timeout) * time.Second))
131 n, err = conn.Read(buf[prev:])
133 // Either EOFed or timeouted
134 timeouted <- struct{}{}
139 i = bytes.Index(buf[:prev], nonceExpectation)
143 if !peer.PktProcess(buf[:i+govpn.NonceSize], tap, false) {
144 timeouted <- struct{}{}
147 if atomic.LoadInt64(&peer.BytesIn)+atomic.LoadInt64(&peer.BytesOut) > govpn.MaxBytesPerKey {
148 log.Println("Need rehandshake")
149 rehandshaking <- struct{}{}
152 binary.BigEndian.PutUint64(nonceExpectation, peer.NonceExpect)
153 peer.NonceCipher.Encrypt(nonceExpectation, nonceExpectation)
154 copy(buf, buf[i+govpn.NonceSize:prev])
155 prev = prev - i - govpn.NonceSize
158 if terminator != nil {
159 terminator <- struct{}{}