]> Cypherpunks.ru repositories - govpn.git/blob - src/cypherpunks.ru/govpn/tap_android.go
fix close by interrupt read, do not close multiple times
[govpn.git] / src / cypherpunks.ru / govpn / tap_android.go
1 /*
2 GoVPN -- simple secure free software virtual private network daemon
3 Copyright (C) 2016-2017 Bruno Clermont <bruno@robotinfra.com>
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         "os"
23         "syscall"
24         "time"
25
26         "github.com/Sirupsen/logrus"
27 )
28
29 func TapListenFileDescriptor(fd uintptr, ifaceName string, mtu int) *TAP {
30         tap, exists := taps[ifaceName]
31         if exists {
32                 return tap
33         }
34
35         tap = &TAP{
36                 Name: ifaceName,
37                 dev:  os.NewFile(fd, ifaceName),
38                 Sink: make(chan []byte),
39         }
40         go func() {
41                 var n int
42                 var err error
43                 var buf []byte
44                 buf0 := make([]byte, mtu)
45                 buf1 := make([]byte, mtu)
46                 bufZ := false
47                 for {
48                         if bufZ {
49                                 buf = buf0
50                         } else {
51                                 buf = buf1
52                         }
53                         bufZ = !bufZ
54                         n, err = tap.dev.Read(buf)
55                         if err != nil {
56                                 if tap.closed {
57                                         return
58                                 }
59
60                                 e, ok := err.(*os.PathError)
61                                 if ok && e.Err == syscall.EAGAIN {
62                                         time.Sleep(time.Millisecond * 20)
63                                         continue
64                                 }
65
66                                 logger.WithError(err).WithFields(logrus.Fields{
67                                         "func", logFuncPrefix + "TUN read sink loop",
68                                         "name": tap.Name,
69                                         "mtu":  mtu,
70                                 }).Error("Can not read interface, stop")
71                                 return
72                                 // TODO: need a way to warn consumer that something is wrong
73                                 // TODO: to force peer to just disconnect
74                                 // TODO: use the client/server error channel?
75                         } else {
76                                 tap.Sink <- buf[:n]
77                         }
78                 }
79         }()
80         taps[ifaceName] = tap
81         return tap
82 }