Support nopi mode
This commit is contained in:
parent
09de7d1342
commit
fd248c6cb1
11
tun_linux.go
11
tun_linux.go
|
@ -27,6 +27,7 @@ type NativeTun struct {
|
||||||
name string // name of interface
|
name string // name of interface
|
||||||
errors chan error // async error handling
|
errors chan error // async error handling
|
||||||
events chan TUNEvent // device related events
|
events chan TUNEvent // device related events
|
||||||
|
nopi bool // the device was pased IFF_NO_PI
|
||||||
}
|
}
|
||||||
|
|
||||||
func toRTMGRP(sc uint) uint {
|
func toRTMGRP(sc uint) uint {
|
||||||
|
@ -242,6 +243,9 @@ func (tun *NativeTun) MTU() (int, error) {
|
||||||
|
|
||||||
func (tun *NativeTun) Write(buff []byte, offset int) (int, error) {
|
func (tun *NativeTun) Write(buff []byte, offset int) (int, error) {
|
||||||
|
|
||||||
|
if tun.nopi {
|
||||||
|
buff = buff[offset:]
|
||||||
|
} else {
|
||||||
// reserve space for header
|
// reserve space for header
|
||||||
|
|
||||||
buff = buff[offset-4:]
|
buff = buff[offset-4:]
|
||||||
|
@ -258,6 +262,7 @@ func (tun *NativeTun) Write(buff []byte, offset int) (int, error) {
|
||||||
buff[2] = 0x08
|
buff[2] = 0x08
|
||||||
buff[3] = 0x00
|
buff[3] = 0x00
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// write
|
// write
|
||||||
|
|
||||||
|
@ -269,6 +274,9 @@ func (tun *NativeTun) Read(buff []byte, offset int) (int, error) {
|
||||||
case err := <-tun.errors:
|
case err := <-tun.errors:
|
||||||
return 0, err
|
return 0, err
|
||||||
default:
|
default:
|
||||||
|
if tun.nopi {
|
||||||
|
return tun.fd.Read(buff[offset:])
|
||||||
|
} else {
|
||||||
buff := buff[offset-4:]
|
buff := buff[offset-4:]
|
||||||
n, err := tun.fd.Read(buff[:])
|
n, err := tun.fd.Read(buff[:])
|
||||||
if n < 4 {
|
if n < 4 {
|
||||||
|
@ -276,6 +284,7 @@ func (tun *NativeTun) Read(buff []byte, offset int) (int, error) {
|
||||||
}
|
}
|
||||||
return n - 4, err
|
return n - 4, err
|
||||||
}
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (tun *NativeTun) Events() chan TUNEvent {
|
func (tun *NativeTun) Events() chan TUNEvent {
|
||||||
|
@ -292,6 +301,7 @@ func CreateTUNFromFile(name string, fd *os.File) (TUNDevice, error) {
|
||||||
name: name,
|
name: name,
|
||||||
events: make(chan TUNEvent, 5),
|
events: make(chan TUNEvent, 5),
|
||||||
errors: make(chan error, 5),
|
errors: make(chan error, 5),
|
||||||
|
nopi: false,
|
||||||
}
|
}
|
||||||
|
|
||||||
// start event listener
|
// start event listener
|
||||||
|
@ -349,6 +359,7 @@ func CreateTUN(name string) (TUNDevice, error) {
|
||||||
name: newName,
|
name: newName,
|
||||||
events: make(chan TUNEvent, 5),
|
events: make(chan TUNEvent, 5),
|
||||||
errors: make(chan error, 5),
|
errors: make(chan error, 5),
|
||||||
|
nopi: false,
|
||||||
}
|
}
|
||||||
|
|
||||||
// start event listener
|
// start event listener
|
||||||
|
|
Loading…
Reference in a new issue