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 bind, err := net.ResolveTCPAddr("tcp", *bindAddr)
34 log.Fatalln("Can not resolve bind address:", err)
36 listener, err := net.ListenTCP("tcp", bind)
38 log.Fatalln("Can not listen on TCP:", err)
40 log.Println("Listening on TCP", *bindAddr)
43 conn, err := listener.AcceptTCP()
45 log.Println("Error accepting TCP:", err)
53 func handleTCP(conn net.Conn) {
54 addr := conn.RemoteAddr().String()
55 buf := make([]byte, govpn.MTU)
59 var hs *govpn.Handshake
63 var conf *govpn.PeerConf
65 if prev == govpn.MTU {
68 conn.SetReadDeadline(time.Now().Add(time.Duration(govpn.TimeoutDefault) * time.Second))
69 n, err = conn.Read(buf[prev:])
71 // Either EOFed or timeouted
75 peerId := govpn.IDsCache.Find(buf[:prev])
82 log.Println("Can not get peer configuration:", peerId.String())
85 hs = govpn.NewHandshake(addr, conn, conf)
87 peer = hs.Server(buf[:prev])
94 addrPrev, exists := peersById[*peer.Id]
95 peersByIdLock.RUnlock()
98 tap = peers[addrPrev].tap
102 terminator: peers[addrPrev].terminator,
105 ps.terminator <- struct{}{}
109 delete(peers, addrPrev)
110 delete(knownPeers, addrPrev)
111 delete(peersById, *peer.Id)
113 knownPeers[addr] = &peer
114 peersById[*peer.Id] = addr
116 peersByIdLock.Unlock()
119 log.Println("Rehandshake finished:", peer.Id.String())
121 ifaceName, err := callUp(peer.Id)
125 tap, err = govpn.TAPListen(ifaceName)
127 log.Println("Unable to create TAP:", err)
133 terminator: make(chan struct{}, 1),
140 peersById[*peer.Id] = addr
141 knownPeers[addr] = &peer
143 peersByIdLock.Unlock()
145 log.Println("New peer:", peer.Id.String())
156 nonceExpectation := make([]byte, govpn.NonceSize)
157 binary.BigEndian.PutUint64(nonceExpectation, peer.NonceExpect)
158 peer.NonceCipher.Encrypt(nonceExpectation, nonceExpectation)
162 if prev == govpn.MTU {
165 conn.SetReadDeadline(time.Now().Add(conf.Timeout))
166 n, err = conn.Read(buf[prev:])
168 // Either EOFed or timeouted
173 i = bytes.Index(buf[:prev], nonceExpectation)
177 if !peer.PktProcess(buf[:i+govpn.NonceSize], tap, false) {
180 binary.BigEndian.PutUint64(nonceExpectation, peer.NonceExpect)
181 peer.NonceCipher.Encrypt(nonceExpectation, nonceExpectation)
182 copy(buf, buf[i+govpn.NonceSize:prev])
183 prev = prev - i - govpn.NonceSize