]> Cypherpunks.ru repositories - govpn.git/blob - src/cypherpunks.ru/govpn/cmd/govpn-server/common.go
Various stylistic and grammar fixes
[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 // PeerState holds server side state of a single connecting/connected peer
29 type PeerState struct {
30         peer       *govpn.Peer
31         terminator chan struct{}
32         tap        *govpn.TAP
33 }
34
35 var (
36         handshakes map[string]*govpn.Handshake = make(map[string]*govpn.Handshake)
37         hsLock     sync.RWMutex
38
39         peers     = make(map[string]*PeerState)
40         peersLock sync.RWMutex
41
42         peersByID     = make(map[govpn.PeerID]string)
43         peersByIDLock sync.RWMutex
44
45         knownPeers govpn.KnownPeers
46         kpLock     sync.RWMutex
47 )
48
49 func callUp(peerID *govpn.PeerID, remoteAddr string) (string, error) {
50         ifaceName := confs[*peerID].Iface
51         if confs[*peerID].Up != "" {
52                 result, err := govpn.ScriptCall(confs[*peerID].Up, ifaceName, remoteAddr)
53                 if err != nil {
54                         govpn.Printf(
55                                 `[script-failed bind="%s" path="%s" err="%s"]`,
56                                 *bindAddr,
57                                 confs[*peerID].Up,
58                                 err,
59                         )
60                         return "", err
61                 }
62                 if ifaceName == "" {
63                         sepIndex := bytes.Index(result, []byte{'\n'})
64                         if sepIndex < 0 {
65                                 sepIndex = len(result)
66                         }
67                         ifaceName = string(result[:sepIndex])
68                 }
69         }
70         if ifaceName == "" {
71                 govpn.Printf(`[tap-failed bind="%s" peer="%s"]`, *bindAddr, *peerID)
72         }
73         return ifaceName, nil
74 }