]> Cypherpunks.ru repositories - govpn.git/blob - common.go
Raise copyright years
[govpn.git] / common.go
1 /*
2 GoVPN -- simple secure free software virtual private network daemon
3 Copyright (C) 2014-2020 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, version 3 of the License.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 GNU General Public License for more details.
13
14 You should have received a copy of the GNU General Public License
15 along with this program.  If not, see <http://www.gnu.org/licenses/>.
16 */
17
18 package govpn
19
20 import (
21         "log"
22         "os"
23         "os/exec"
24         "runtime"
25 )
26
27 const (
28         TimeoutDefault = 60
29         EtherSize      = 14
30         MTUMax         = 9000 + EtherSize + 1
31         MTUDefault     = 1500 + EtherSize + 1
32
33         ENV_IFACE  = "GOVPN_IFACE"
34         ENV_REMOTE = "GOVPN_REMOTE"
35 )
36
37 var (
38         Version string = "UNKNOWN"
39 )
40
41 // Call external program/script.
42 // You have to specify path to it and (inteface name as a rule) something
43 // that will be the first argument when calling it. Function will return
44 // it's output and possible error.
45 func ScriptCall(path, ifaceName, remoteAddr string) ([]byte, error) {
46         if path == "" {
47                 return nil, nil
48         }
49         if _, err := os.Stat(path); err != nil && os.IsNotExist(err) {
50                 return nil, err
51         }
52         cmd := exec.Command(path)
53         cmd.Env = append(cmd.Env, ENV_IFACE+"="+ifaceName)
54         cmd.Env = append(cmd.Env, ENV_REMOTE+"="+remoteAddr)
55         out, err := cmd.CombinedOutput()
56         if err != nil {
57                 log.Println("Script error", path, err, string(out))
58         }
59         return out, err
60 }
61
62 // Zero each byte.
63 func SliceZero(data []byte) {
64         for i := 0; i < len(data); i++ {
65                 data[i] = 0
66         }
67 }
68
69 func VersionGet() string {
70         return "GoVPN version " + Version + " built with " + runtime.Version()
71 }