2 GoVPN -- simple secure free software virtual private network daemon
3 Copyright (C) 2014-2017 Sergey Matveev <stargrave@stargrave.org>
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.
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.
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/>.
28 "github.com/pkg/errors"
32 // ProtocolUDP is UDP transport protocol
33 ProtocolUDP Protocol = iota
34 // ProtocolTCP is TCP transport protocol
36 // ProtocolALL is TCP+UDP transport protocol
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
45 ENV_IFACE = "GOVPN_IFACE"
46 ENV_REMOTE = "GOVPN_REMOTE"
48 wrapNewProtocolFromString = "NewProtocolFromString"
52 // Version holds release string set at build time
54 protocolText = map[Protocol]string{
59 // TimeoutDefault is default timeout value for various network operations
60 TimeoutDefault = 60 * time.Second
63 // Protocol is a GoVPN supported protocol: either UDP, TCP or both
66 // String converts a Protocol into a string
67 func (p Protocol) String() string {
68 return protocolText[p]
71 // MarshalJSON returns a JSON string from a protocol
72 func (p Protocol) MarshalJSON() ([]byte, error) {
74 output, err := json.Marshal(&str)
75 return output, errors.Wrap(err, "json.Marshal")
78 // UnmarshalJSON converts a JSON string into a Protocol
79 func (p *Protocol) UnmarshalJSON(encoded []byte) error {
81 if err := json.Unmarshal(encoded, &str); err != nil {
82 return errors.Wrapf(err, "Can't unmarshall to string %q", hex.EncodeToString(encoded))
84 proto, err := NewProtocolFromString(str)
86 return errors.Wrap(err, wrapNewProtocolFromString)
92 // UnmarshalYAML converts a YAML string into a Protocol
93 func (p *Protocol) UnmarshalYAML(unmarshal func(interface{}) error) error {
95 err := unmarshal(&str)
97 return errors.Wrap(err, "unmarshall")
100 proto, err := NewProtocolFromString(str)
102 return errors.Wrap(err, wrapNewProtocolFromString)
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 {
117 choices := make([]string, len(protocolText))
119 for k, v := range protocolText {
129 return Protocol(-1), errors.Errorf("Invalid protocol %q: %s", p, strings.Join(choices, ","))
132 // SliceZero zeros each byte.
133 func SliceZero(data []byte) {
134 for i := 0; i < len(data); i++ {
139 func VersionGet() string {
140 return "GoVPN version " + Version + " built with " + runtime.Version()