]> Cypherpunks.ru repositories - govpn.git/blob - src/cypherpunks.ru/govpn/cmd/govpn-server/common.go
go vet/lint
[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     = make(map[string]*PeerState)
39         peersLock sync.RWMutex
40
41         peersByID     = 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(
54                                 `[script-failed bind="%s" path="%s" err="%s"]`,
55                                 *bindAddr,
56                                 confs[*peerID].Up,
57                                 err,
58                         )
59                         return "", err
60                 }
61                 if ifaceName == "" {
62                         sepIndex := bytes.Index(result, []byte{'\n'})
63                         if sepIndex < 0 {
64                                 sepIndex = len(result)
65                         }
66                         ifaceName = string(result[:sepIndex])
67                 }
68         }
69         if ifaceName == "" {
70                 govpn.Printf(`[tap-failed bind="%s" peer="%s"]`, *bindAddr, *peerID)
71         }
72         return ifaceName, nil
73 }