]> Cypherpunks.ru repositories - govpn.git/blob - src/cypherpunks.ru/govpn/cmd/govpn-server/common.go
Use convenient simpler Go 1.9's sync.Map
[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 sync.Map
36         peers      sync.Map
37         peersByID  sync.Map
38         knownPeers sync.Map
39 )
40
41 func callUp(peerID *govpn.PeerID, remoteAddr string) (string, error) {
42         ifaceName := confs[*peerID].Iface
43         if confs[*peerID].Up != "" {
44                 result, err := govpn.ScriptCall(confs[*peerID].Up, ifaceName, remoteAddr)
45                 if err != nil {
46                         govpn.Printf(
47                                 `[script-failed bind="%s" path="%s" err="%s"]`,
48                                 *bindAddr,
49                                 confs[*peerID].Up,
50                                 err,
51                         )
52                         return "", err
53                 }
54                 if ifaceName == "" {
55                         sepIndex := bytes.Index(result, []byte{'\n'})
56                         if sepIndex < 0 {
57                                 sepIndex = len(result)
58                         }
59                         ifaceName = string(result[:sepIndex])
60                 }
61         }
62         if ifaceName == "" {
63                 govpn.Printf(`[tap-failed bind="%s" peer="%s"]`, *bindAddr, *peerID)
64         }
65         return ifaceName, nil
66 }