]> Cypherpunks.ru repositories - govpn.git/blob - src/cypherpunks.ru/govpn/tap_android.go
Fix copyright years and authorship
[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                                 e, ok := err.(*os.PathError)
57                                 if ok && e.Err == syscall.EAGAIN {
58                                         time.Sleep(time.Millisecond * 20)
59                                         continue
60                                 }
61
62                                 logger.WithError(err).WithFields(logrus.Fields{
63                                         "func", logFuncPrefix + "TUN read sink loop",
64                                         "name": tap.Name,
65                                         "mtu":  mtu,
66                                 }).Error("Can't read interface, stop")
67                                 return
68                                 // TODO: need a way to warn consumer that something is wrong
69                                 // TODO: to force peer to just disconnect
70                                 // TODO: use the client/server error channel?
71                         } else {
72                                 tap.Sink <- buf[:n]
73                         }
74                 }
75         }()
76         taps[ifaceName] = tap
77         return tap
78 }