]> Cypherpunks.ru repositories - govpn.git/blob - src/cypherpunks.ru/govpn/tap_linux.go
72d52f2158ce1649571c9fb1909c35f643a0c79a
[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
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         "io"
23         "strings"
24
25         "github.com/pkg/errors"
26         "github.com/songgao/water"
27 )
28
29 func newTAPer(ifaceName *string) (io.ReadWriteCloser, error) {
30         config := water.Config{}
31
32         if len(*ifaceName) == 0 {
33                 return nil, errors.New("Can't figure interface type, empty name")
34         }
35
36         if strings.HasPrefix(*ifaceName, interfaceTap) {
37                 config.DeviceType = water.TAP
38                 if len(*ifaceName) > len(interfaceTap) {
39                         config.Name = *ifaceName
40                 }
41         } else if strings.HasPrefix(*ifaceName, interfaceTun) {
42                 config.DeviceType = water.TUN
43                 if len(*ifaceName) > len(interfaceTun) {
44                         config.Name = *ifaceName
45                 }
46         } else {
47                 return nil, errors.Errorf("Unrecognized interface name %q", *ifaceName)
48         }
49
50         output, err := water.New(config)
51         if err != nil {
52                 return nil, errors.Wrap(err, "water.New")
53         }
54         *ifaceName = output.Name()
55         return output, nil
56 }