mirror of
https://github.com/amnezia-vpn/amneziawg-go.git
synced 2025-08-02 01:42:54 +02:00
tun: darwin: fetch flags and mtu from if_msghdr directly
Signed-off-by: ruokeqx <ruokeqx@gmail.com> Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
This commit is contained in:
parent
263c3feccb
commit
5e468bb888
1 changed files with 9 additions and 25 deletions
|
@ -6,14 +6,12 @@
|
||||||
package tun
|
package tun
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"errors"
|
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
"net"
|
"net"
|
||||||
"os"
|
"os"
|
||||||
"sync"
|
"sync"
|
||||||
"syscall"
|
"syscall"
|
||||||
"time"
|
|
||||||
"unsafe"
|
"unsafe"
|
||||||
|
|
||||||
"golang.org/x/sys/unix"
|
"golang.org/x/sys/unix"
|
||||||
|
@ -30,18 +28,6 @@ type NativeTun struct {
|
||||||
closeOnce sync.Once
|
closeOnce sync.Once
|
||||||
}
|
}
|
||||||
|
|
||||||
func retryInterfaceByIndex(index int) (iface *net.Interface, err error) {
|
|
||||||
for i := 0; i < 20; i++ {
|
|
||||||
iface, err = net.InterfaceByIndex(index)
|
|
||||||
if err != nil && errors.Is(err, unix.ENOMEM) {
|
|
||||||
time.Sleep(time.Duration(i) * time.Second / 3)
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
return iface, err
|
|
||||||
}
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
func (tun *NativeTun) routineRouteListener(tunIfindex int) {
|
func (tun *NativeTun) routineRouteListener(tunIfindex int) {
|
||||||
var (
|
var (
|
||||||
statusUp bool
|
statusUp bool
|
||||||
|
@ -62,26 +48,22 @@ func (tun *NativeTun) routineRouteListener(tunIfindex int) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
if n < 14 {
|
if n < 28 {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
if data[3 /* type */] != unix.RTM_IFINFO {
|
if data[3 /* ifm_type */] != unix.RTM_IFINFO {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
ifindex := int(*(*uint16)(unsafe.Pointer(&data[12 /* ifindex */])))
|
ifindex := int(*(*uint16)(unsafe.Pointer(&data[12 /* ifm_index */])))
|
||||||
if ifindex != tunIfindex {
|
if ifindex != tunIfindex {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
iface, err := retryInterfaceByIndex(ifindex)
|
flags := int(*(*uint32)(unsafe.Pointer(&data[8 /* ifm_flags */])))
|
||||||
if err != nil {
|
|
||||||
tun.errors <- err
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
// Up / Down event
|
// Up / Down event
|
||||||
up := (iface.Flags & net.FlagUp) != 0
|
up := (flags & syscall.IFF_UP) != 0
|
||||||
if up != statusUp && up {
|
if up != statusUp && up {
|
||||||
tun.events <- EventUp
|
tun.events <- EventUp
|
||||||
}
|
}
|
||||||
|
@ -90,11 +72,13 @@ func (tun *NativeTun) routineRouteListener(tunIfindex int) {
|
||||||
}
|
}
|
||||||
statusUp = up
|
statusUp = up
|
||||||
|
|
||||||
|
mtu := int(*(*uint32)(unsafe.Pointer(&data[24 /* ifm_data.ifi_mtu */])))
|
||||||
|
|
||||||
// MTU changes
|
// MTU changes
|
||||||
if iface.MTU != statusMTU {
|
if mtu != statusMTU {
|
||||||
tun.events <- EventMTUUpdate
|
tun.events <- EventMTUUpdate
|
||||||
}
|
}
|
||||||
statusMTU = iface.MTU
|
statusMTU = mtu
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue