]> Cypherpunks.ru repositories - govpn.git/blob - src/cypherpunks.ru/govpn/tap_linux.go
Fix copyright years and authorship
[govpn.git] / src / cypherpunks.ru / govpn / tap_linux.go
1 /*
2 GoVPN -- simple secure free software virtual private network daemon
3 Copyright (C) 2014-2017 Sergey Matveev <stargrave@stargrave.org>
4               2016-2017 Bruno Clermont <bruno@robotinfra.com>
5
6 This program is free software: you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation, either version 3 of the License, or
9 (at your option) any later version.
10
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with this program.  If not, see <http://www.gnu.org/licenses/>.
18 */
19
20 package govpn
21
22 import (
23         "io"
24         "strings"
25
26         "github.com/pkg/errors"
27         "github.com/songgao/water"
28 )
29
30 func newTAPer(ifaceName *string) (io.ReadWriteCloser, error) {
31         config := water.Config{}
32
33         if len(*ifaceName) == 0 {
34                 return nil, errors.New("Can't figure interface type, empty name")
35         }
36
37         if strings.HasPrefix(*ifaceName, interfaceTap) {
38                 config.DeviceType = water.TAP
39                 if len(*ifaceName) > len(interfaceTap) {
40                         config.Name = *ifaceName
41                 }
42         } else if strings.HasPrefix(*ifaceName, interfaceTun) {
43                 config.DeviceType = water.TUN
44                 if len(*ifaceName) > len(interfaceTun) {
45                         config.Name = *ifaceName
46                 }
47         } else {
48                 return nil, errors.Errorf("Unrecognized interface name %q", *ifaceName)
49         }
50
51         output, err := water.New(config)
52         if err != nil {
53                 return nil, errors.Wrap(err, "water.New")
54         }
55         *ifaceName = output.Name()
56         return output, nil
57 }