@ -17,17 +17,31 @@ import (
func TestLikelyHomeRouterIPSyscallExec ( t * testing . T ) {
func TestLikelyHomeRouterIPSyscallExec ( t * testing . T ) {
syscallIP , syscallOK := likelyHomeRouterIPBSDFetchRIB ( )
syscallIP , syscallOK := likelyHomeRouterIPBSDFetchRIB ( )
netstatIP , netstatOK := likelyHomeRouterIPDarwinExec ( )
netstatIP , netstatIf , netstatOK := likelyHomeRouterIPDarwinExec ( )
if syscallOK != netstatOK || syscallIP != netstatIP {
if syscallOK != netstatOK || syscallIP != netstatIP {
t . Errorf ( "syscall() = %v, %v, netstat = %v, %v" ,
t . Errorf ( "syscall() = %v, %v, netstat = %v, %v" ,
syscallIP , syscallOK ,
syscallIP , syscallOK ,
netstatIP , netstatOK ,
netstatIP , netstatOK ,
)
)
}
}
if ! syscallOK {
return
}
def , err := defaultRoute ( )
if err != nil {
t . Errorf ( "defaultRoute() error: %v" , err )
}
if def . InterfaceName != netstatIf {
t . Errorf ( "syscall default route interface %s differs from netstat %s" , def . InterfaceName , netstatIf )
}
}
}
/ *
/ *
Parse out 10.0 .0 .1 from :
Parse out 10.0 .0 .1 and en0 from:
$ netstat - r - n - f inet
$ netstat - r - n - f inet
Routing tables
Routing tables
@ -40,12 +54,12 @@ default link#14 UCSI utun2
10.0 .0 .1 / 32 link # 4 UCS en0 !
10.0 .0 .1 / 32 link # 4 UCS en0 !
...
...
* /
* /
func likelyHomeRouterIPDarwinExec ( ) ( ret netip . Addr , ok bool ) {
func likelyHomeRouterIPDarwinExec ( ) ( ret netip . Addr , netif string , ok bool ) {
if version . IsMobile ( ) {
if version . IsMobile ( ) {
// Don't try to do subprocesses on iOS. Ends up with log spam like:
// Don't try to do subprocesses on iOS. Ends up with log spam like:
// kernel: "Sandbox: IPNExtension(86580) deny(1) process-fork"
// kernel: "Sandbox: IPNExtension(86580) deny(1) process-fork"
// This is why we have likelyHomeRouterIPDarwinSyscall.
// This is why we have likelyHomeRouterIPDarwinSyscall.
return ret , false
return ret , "" , false
}
}
cmd := exec . Command ( "/usr/sbin/netstat" , "-r" , "-n" , "-f" , "inet" )
cmd := exec . Command ( "/usr/sbin/netstat" , "-r" , "-n" , "-f" , "inet" )
stdout , err := cmd . StdoutPipe ( )
stdout , err := cmd . StdoutPipe ( )
@ -64,22 +78,26 @@ func likelyHomeRouterIPDarwinExec() (ret netip.Addr, ok bool) {
return nil
return nil
}
}
f = mem . AppendFields ( f [ : 0 ] , line )
f = mem . AppendFields ( f [ : 0 ] , line )
if len ( f ) < 3 || ! f [ 0 ] . EqualString ( "default" ) {
if len ( f ) < 4 || ! f [ 0 ] . EqualString ( "default" ) {
return nil
return nil
}
}
ipm , flagsm := f [ 1 ] , f [ 2 ]
ipm , flagsm , netifm := f [ 1 ] , f [ 2 ] , f [ 3 ]
if ! mem . Contains ( flagsm , mem . S ( "G" ) ) {
if ! mem . Contains ( flagsm , mem . S ( "G" ) ) {
return nil
return nil
}
}
if mem . Contains ( flagsm , mem . S ( "I" ) ) {
return nil
}
ip , err := netip . ParseAddr ( string ( mem . Append ( nil , ipm ) ) )
ip , err := netip . ParseAddr ( string ( mem . Append ( nil , ipm ) ) )
if err == nil && ip . IsPrivate ( ) {
if err == nil && ip . IsPrivate ( ) {
ret = ip
ret = ip
netif = netifm . StringCopy ( )
// We've found what we're looking for.
// We've found what we're looking for.
return errStopReadingNetstatTable
return errStopReadingNetstatTable
}
}
return nil
return nil
} )
} )
return ret , ret. IsValid ( )
return ret , netif, ret. IsValid ( )
}
}
func TestFetchRoutingTable ( t * testing . T ) {
func TestFetchRoutingTable ( t * testing . T ) {