]> Cypherpunks.ru repositories - govpn.git/blob - src/cypherpunks.ru/govpn/common.go
Upgrade Client
[govpn.git] / src / cypherpunks.ru / govpn / 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 govpn
20
21 import (
22         "encoding/hex"
23         "encoding/json"
24         "runtime"
25         "strings"
26         "time"
27
28         "github.com/pkg/errors"
29 )
30
31 const (
32         // ProtocolUDP is UDP transport protocol
33         ProtocolUDP Protocol = iota
34         // ProtocolTCP is TCP transport protocol
35         ProtocolTCP
36         // ProtocolALL is TCP+UDP transport protocol
37         ProtocolALL
38
39         EtherSize = 14
40         // MTUMax is maximum MTU size of Ethernet packet
41         MTUMax = 9000 + EtherSize + 1
42         // MTUDefault is default MTU size of Ethernet packet
43         MTUDefault = 1500 + EtherSize + 1
44
45         ENV_IFACE  = "GOVPN_IFACE"
46         ENV_REMOTE = "GOVPN_REMOTE"
47
48         wrapNewProtocolFromString = "NewProtocolFromString"
49 )
50
51 var (
52         // Version holds release string set at build time
53         Version      string
54         protocolText = map[Protocol]string{
55                 ProtocolUDP: "udp",
56                 ProtocolTCP: "tcp",
57                 ProtocolALL: "all",
58         }
59         // TimeoutDefault is default timeout value for various network operations
60         TimeoutDefault = 60 * time.Second
61 )
62
63 // Protocol is a GoVPN supported protocol: either UDP, TCP or both
64 type Protocol int
65
66 // String converts a Protocol into a string
67 func (p Protocol) String() string {
68         return protocolText[p]
69 }
70
71 // MarshalJSON returns a JSON string from a protocol
72 func (p Protocol) MarshalJSON() ([]byte, error) {
73         str := p.String()
74         output, err := json.Marshal(&str)
75         return output, errors.Wrap(err, "json.Marshal")
76 }
77
78 // UnmarshalJSON converts a JSON string into a Protocol
79 func (p *Protocol) UnmarshalJSON(encoded []byte) error {
80         var str string
81         if err := json.Unmarshal(encoded, &str); err != nil {
82                 return errors.Wrapf(err, "Can't unmarshall to string %q", hex.EncodeToString(encoded))
83         }
84         proto, err := NewProtocolFromString(str)
85         if err != nil {
86                 return errors.Wrap(err, wrapNewProtocolFromString)
87         }
88         *p = proto
89         return nil
90 }
91
92 // UnmarshalYAML converts a YAML string into a Protocol
93 func (p *Protocol) UnmarshalYAML(unmarshal func(interface{}) error) error {
94         var str string
95         err := unmarshal(&str)
96         if err != nil {
97                 return errors.Wrap(err, "unmarshall")
98         }
99
100         proto, err := NewProtocolFromString(str)
101         if err != nil {
102                 return errors.Wrap(err, wrapNewProtocolFromString)
103         }
104         *p = proto
105         return nil
106 }
107
108 // NewProtocolFromString converts a string into a govpn.Protocol
109 func NewProtocolFromString(p string) (Protocol, error) {
110         lowP := strings.ToLower(p)
111         for k, v := range protocolText {
112                 if strings.ToLower(v) == lowP {
113                         return k, nil
114                 }
115         }
116
117         choices := make([]string, len(protocolText))
118         var index = 0
119         for k, v := range protocolText {
120                 if v == p {
121                         z := k
122                         p = &z
123                         return nil
124                 }
125                 choices[index] = v
126                 index++
127         }
128
129         return Protocol(-1), errors.Errorf("Invalid protocol %q: %s", p, strings.Join(choices, ","))
130 }
131
132 // SliceZero zeros each byte.
133 func SliceZero(data []byte) {
134         for i := 0; i < len(data); i++ {
135                 data[i] = 0
136         }
137 }
138
139 func VersionGet() string {
140         return "GoVPN version " + Version + " built with " + runtime.Version()
141 }