github.com/apernet/sing-tun@v0.2.6-0.20240323130332-b9f6511036ad/monitor_linux_default.go (about)

     1  //go:build linux && !android
     2  
     3  package tun
     4  
     5  import (
     6  	"github.com/sagernet/netlink"
     7  
     8  	"golang.org/x/sys/unix"
     9  )
    10  
    11  func (m *defaultInterfaceMonitor) checkUpdate() error {
    12  	routes, err := netlink.RouteListFiltered(netlink.FAMILY_ALL, &netlink.Route{Table: unix.RT_TABLE_MAIN}, netlink.RT_FILTER_TABLE)
    13  	if err != nil {
    14  		return err
    15  	}
    16  	for _, route := range routes {
    17  		if route.Dst != nil {
    18  			continue
    19  		}
    20  
    21  		var link netlink.Link
    22  		link, err = netlink.LinkByIndex(route.LinkIndex)
    23  		if err != nil {
    24  			return err
    25  		}
    26  
    27  		oldInterface := m.defaultInterfaceName
    28  		oldIndex := m.defaultInterfaceIndex
    29  
    30  		m.defaultInterfaceName = link.Attrs().Name
    31  		m.defaultInterfaceIndex = link.Attrs().Index
    32  
    33  		if oldInterface == m.defaultInterfaceName && oldIndex == m.defaultInterfaceIndex {
    34  			return nil
    35  		}
    36  		m.emit(EventInterfaceUpdate)
    37  		return nil
    38  	}
    39  	return ErrNoRoute
    40  }