Support nopi mode

This commit is contained in:
Jason A. Donenfeld 2018-02-28 12:40:56 +01:00
parent 09de7d1342
commit fd248c6cb1

View file

@ -27,6 +27,7 @@ type NativeTun struct {
name string // name of interface
errors chan error // async error handling
events chan TUNEvent // device related events
nopi bool // the device was pased IFF_NO_PI
}
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) {
if tun.nopi {
buff = buff[offset:]
} else {
// reserve space for header
buff = buff[offset-4:]
@ -258,6 +262,7 @@ func (tun *NativeTun) Write(buff []byte, offset int) (int, error) {
buff[2] = 0x08
buff[3] = 0x00
}
}
// write
@ -269,6 +274,9 @@ func (tun *NativeTun) Read(buff []byte, offset int) (int, error) {
case err := <-tun.errors:
return 0, err
default:
if tun.nopi {
return tun.fd.Read(buff[offset:])
} else {
buff := buff[offset-4:]
n, err := tun.fd.Read(buff[:])
if n < 4 {
@ -277,6 +285,7 @@ func (tun *NativeTun) Read(buff []byte, offset int) (int, error) {
return n - 4, err
}
}
}
func (tun *NativeTun) Events() chan TUNEvent {
return tun.events
@ -292,6 +301,7 @@ func CreateTUNFromFile(name string, fd *os.File) (TUNDevice, error) {
name: name,
events: make(chan TUNEvent, 5),
errors: make(chan error, 5),
nopi: false,
}
// start event listener
@ -349,6 +359,7 @@ func CreateTUN(name string) (TUNDevice, error) {
name: newName,
events: make(chan TUNEvent, 5),
errors: make(chan error, 5),
nopi: false,
}
// start event listener