]> Cypherpunks.ru repositories - govpn.git/blob - src/cypherpunks.ru/govpn/cmd/govpn-server/common.go
f5c7e2692e6f61dcc06195d67b114279376a9d4a
[govpn.git] / src / cypherpunks.ru / govpn / cmd / govpn-server / common.go
1 /*
2 GoVPN -- simple secure free software virtual private network daemon
3 Copyright (C) 2014-2017 Sergey Matveev <stargrave@stargrave.org>
4
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.
9
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.
14
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/>.
17 */
18
19 package main
20
21 import (
22         "bytes"
23         "sync"
24
25         "cypherpunks.ru/govpn"
26 )
27
28 type PeerState struct {
29         peer       *govpn.Peer
30         terminator chan struct{}
31         tap        *govpn.TAP
32 }
33
34 var (
35         handshakes map[string]*govpn.Handshake = make(map[string]*govpn.Handshake)
36         hsLock     sync.RWMutex
37
38         peers     map[string]*PeerState = make(map[string]*PeerState)
39         peersLock sync.RWMutex
40
41         peersById     map[govpn.PeerId]string = make(map[govpn.PeerId]string)
42         peersByIdLock sync.RWMutex
43
44         knownPeers govpn.KnownPeers
45         kpLock     sync.RWMutex
46 )
47
48 func callUp(peerId *govpn.PeerId, remoteAddr string) (string, error) {
49         ifaceName := confs[*peerId].Iface
50         if confs[*peerId].Up != "" {
51                 result, err := govpn.ScriptCall(confs[*peerId].Up, ifaceName, remoteAddr)
52                 if err != nil {
53                         govpn.Printf(`[script-failed bind="%s" path="%s" err="%s"]`, *bindAddr, confs[*peerId].Up, err)
54                         return "", err
55                 }
56                 if ifaceName == "" {
57                         sepIndex := bytes.Index(result, []byte{'\n'})
58                         if sepIndex < 0 {
59                                 sepIndex = len(result)
60                         }
61                         ifaceName = string(result[:sepIndex])
62                 }
63         }
64         if ifaceName == "" {
65                 govpn.Printf(`[tap-failed bind="%s" peer="%s"]`, *bindAddr, *peerId)
66         }
67         return ifaceName, nil
68 }