mirror of
https://git.zx2c4.com/wireguard-go
synced 2024-11-15 01:05:15 +01:00
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
|
||||
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 {
|
||||
@ -276,6 +284,7 @@ func (tun *NativeTun) Read(buff []byte, offset int) (int, error) {
|
||||
}
|
||||
return n - 4, err
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (tun *NativeTun) Events() chan TUNEvent {
|
||||
@ -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
|
||||
|
Loading…
Reference in New Issue
Block a user