]> Cypherpunks.ru repositories - govpn.git/blob - src/cypherpunks.ru/govpn/tap.go
fix close by interrupt read, do not close multiple times
[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         closed bool
40 }
41
42 var (
43         taps                    = make(map[string]*TAP)
44         errUnsupportedInterface = errors.New("Unsupported interface")
45 )
46
47 // NewTAP creates a new TUN/TAP virtual interface
48 func NewTAP(ifaceName string, mtu int) (*TAP, error) {
49         tapRaw, err := newTAPer(&ifaceName)
50         if err != nil {
51                 return nil, errors.Wrap(err, "newTAPer")
52         }
53         tap := TAP{
54                 Name: ifaceName,
55                 dev:  tapRaw,
56                 Sink: make(chan []byte),
57         }
58         go func() {
59                 var n int
60                 var err error
61                 var buf []byte
62                 buf0 := make([]byte, mtu)
63                 buf1 := make([]byte, mtu)
64                 bufZ := false
65                 for {
66                         if bufZ {
67                                 buf = buf0
68                         } else {
69                                 buf = buf1
70                         }
71                         bufZ = !bufZ
72                         n, err = tap.dev.Read(buf)
73                         if err != nil {
74                                 if tap.closed {
75                                         return
76                                 }
77                                 logger.WithError(err).WithFields(logrus.Fields{
78                                         "func": logFuncPrefix + "TAP read sink loop",
79                                         "name": tap.Name,
80                                         "mtu":  mtu,
81                                 }).Error("Can not read interface")
82                                 return
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?
86                         }
87                         tap.Sink <- buf[:n]
88                 }
89         }()
90         return &tap, nil
91 }
92
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))
96 }
97
98 // Close close TAP/TUN virtual network interface
99 func (t *TAP) Close() error {
100         if t.closed {
101                 return nil
102         }
103         err := t.dev.Close()
104         if err != nil {
105                 return errors.Wrap(err, "water.Interface.Close")
106         }
107         t.closed = true
108         return nil
109 }
110
111 // TAPListen opens an existing TAP (creates if none exists)
112 func TAPListen(ifaceName string, mtu int) (*TAP, error) {
113         tap, exists := taps[ifaceName]
114         if exists {
115                 return tap, nil
116         }
117         tap, err := NewTAP(ifaceName, mtu)
118         if err != nil {
119                 return nil, errors.Wrap(err, "NewTAP")
120         }
121         taps[ifaceName] = tap
122         return tap, nil
123 }