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/>.
24 "github.com/Sirupsen/logrus"
25 "github.com/pkg/errors"
33 // TAP is a TUN or a TAP interface.
34 // TODO: rename to something more... generic?
38 dev io.ReadWriteCloser
43 taps = make(map[string]*TAP)
44 errUnsupportedInterface = errors.New("Unsupported interface")
47 // NewTAP creates a new TUN/TAP virtual interface
48 func NewTAP(ifaceName string, mtu int) (*TAP, error) {
49 tapRaw, err := newTAPer(&ifaceName)
51 return nil, errors.Wrap(err, "newTAPer")
56 Sink: make(chan []byte),
62 buf0 := make([]byte, mtu)
63 buf1 := make([]byte, mtu)
72 n, err = tap.dev.Read(buf)
77 logger.WithError(err).WithFields(logrus.Fields{
78 "func": logFuncPrefix + "TAP read sink loop",
81 }).Error("Can not read interface")
83 // TODO: need a way to warn consumer that something is wrong
84 // TODO: to force peer to just disconnect
85 // TODO: use the client/server error channel?
93 func (t *TAP) Write(data []byte) (int, error) {
94 n, err := t.dev.Write(data)
95 return n, errors.Wrapf(err, "t.dev.Write %d", len(data))
98 // Close close TAP/TUN virtual network interface
99 func (t *TAP) Close() error {
105 return errors.Wrap(err, "water.Interface.Close")
111 // TAPListen opens an existing TAP (creates if none exists)
112 func TAPListen(ifaceName string, mtu int) (*TAP, error) {
113 tap, exists := taps[ifaceName]
117 tap, err := NewTAP(ifaceName, mtu)
119 return nil, errors.Wrap(err, "NewTAP")
121 taps[ifaceName] = tap