]> Cypherpunks.ru repositories - govpn.git/blob - src/cypherpunks.ru/govpn/tap.go
efb53a3e591c95dacf1f90057a9d065a63292ac4
[govpn.git] / src / cypherpunks.ru / govpn / tap.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
24         "github.com/Sirupsen/logrus"
25         "github.com/pkg/errors"
26 )
27
28 const (
29         interfaceTap = "tap"
30         interfaceTun = "tun"
31 )
32
33 // TAP is a TUN or a TAP interface.
34 // TODO: rename to something more... generic?
35 type TAP struct {
36         Name string
37         Sink chan []byte
38         dev  io.ReadWriteCloser
39 }
40
41 var (
42         taps                    = make(map[string]*TAP)
43         errUnsupportedInterface = errors.New("Unsupported interface")
44 )
45
46 // NewTAP creates a new TUN/TAP virtual interface
47 func NewTAP(ifaceName string, mtu int) (*TAP, error) {
48         tapRaw, err := newTAPer(&ifaceName)
49         if err != nil {
50                 return nil, errors.Wrap(err, "newTAPer")
51         }
52         tap := TAP{
53                 Name: ifaceName,
54                 dev:  tapRaw,
55                 Sink: make(chan []byte),
56         }
57         go func() {
58                 var n int
59                 var err error
60                 var buf []byte
61                 buf0 := make([]byte, mtu)
62                 buf1 := make([]byte, mtu)
63                 bufZ := false
64                 for {
65                         if bufZ {
66                                 buf = buf0
67                         } else {
68                                 buf = buf1
69                         }
70                         bufZ = !bufZ
71                         n, err = tap.dev.Read(buf)
72                         if err != nil {
73                                 logger.WithError(err).WithFields(logrus.Fields{
74                                         "func": logFuncPrefix + "TAP read sink loop",
75                                         "name": tap.Name,
76                                         "mtu":  mtu,
77                                 }).Error("Can not read interface")
78                                 return
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?
82                         }
83                         tap.Sink <- buf[:n]
84                 }
85         }()
86         return &tap, nil
87 }
88
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))
92 }
93
94 // Close close TAP/TUN virtual network interface
95 func (t *TAP) Close() error {
96         // TODO add chan to stop read loop
97         return t.dev.Close()
98 }
99
100 // TAPListen opens an existing TAP (creates if none exists)
101 func TAPListen(ifaceName string, mtu int) (*TAP, error) {
102         tap, exists := taps[ifaceName]
103         if exists {
104                 return tap, nil
105         }
106         tap, err := NewTAP(ifaceName, mtu)
107         if err != nil {
108                 return nil, errors.Wrap(err, "NewTAP")
109         }
110         taps[ifaceName] = tap
111         return tap, nil
112 }