2 GoVPN -- simple secure free software virtual private network daemon
3 Copyright (C) 2014-2016 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
42 taps = make(map[string]*TAP)
43 errUnsupportedInterface = errors.New("Unsupported interface")
46 // NewTAP creates a new TUN/TAP virtual interface
47 func NewTAP(ifaceName string, mtu int) (*TAP, error) {
48 tapRaw, err := newTAPer(&ifaceName)
50 return nil, errors.Wrap(err, "newTAPer")
55 Sink: make(chan []byte),
61 buf0 := make([]byte, mtu)
62 buf1 := make([]byte, mtu)
71 n, err = tap.dev.Read(buf)
73 logger.WithError(err).WithFields(logrus.Fields{
74 "func": logFuncPrefix + "TAP read sink loop",
77 }).Error("Can't read interface")
79 // TODO: need a way to warn consumer that something is wrong
80 // TODO: to force peer to just disconnect
81 // TODO: use the client/server error channel?
89 func (t *TAP) Write(data []byte) (int, error) {
90 n, err := t.dev.Write(data)
91 return n, errors.Wrapf(err, "t.dev.Write %d", len(data))
94 // Close close TAP/TUN virtual network interface
95 func (t *TAP) Close() error {
96 // TODO add chan to stop read loop
100 // TAPListen opens an existing TAP (creates if none exists)
101 func TAPListen(ifaceName string, mtu int) (*TAP, error) {
102 tap, exists := taps[ifaceName]
106 tap, err := NewTAP(ifaceName, mtu)
108 return nil, errors.Wrap(err, "NewTAP")
110 taps[ifaceName] = tap