From ab437b8f75964d6d8c0a1a3539ce35fedf2c1412 Mon Sep 17 00:00:00 2001 From: wlynxg Date: Fri, 20 Oct 2023 17:17:31 +0800 Subject: [PATCH] feat: Android system version number setting --- interface.go | 4 +++- interface_android.go | 24 ++++++++++++++++++++++++ 2 files changed, 27 insertions(+), 1 deletion(-) diff --git a/interface.go b/interface.go index 4f540a6..384f29c 100644 --- a/interface.go +++ b/interface.go @@ -21,8 +21,10 @@ func InterfaceAddrs() ([]net.Addr, error) { return net.InterfaceAddrs() } -// InterfaceAddrsByInterface returns a list of the system's unicast +// InterfaceAddrsByInterface returns a list of the system's unicast // interface addresses by specific interface. func InterfaceAddrsByInterface(ifi *net.Interface) ([]net.Addr, error) { return ifi.Addrs() } + +func SetAndroidVersion(version uint) {} diff --git a/interface_android.go b/interface_android.go index 34875ea..ce1366d 100644 --- a/interface_android.go +++ b/interface_android.go @@ -9,7 +9,12 @@ import ( "unsafe" ) +const ( + android11 = 11 +) + var ( + androidVersion uint errNoSuchInterface = errors.New("no such network interface") errInvalidInterface = errors.New("invalid network interface") ) @@ -18,6 +23,10 @@ type ifReq [40]byte // Interfaces returns a list of the system's network interfaces. func Interfaces() ([]net.Interface, error) { + if androidVersion > android11 { + return net.Interfaces() + } + ift, err := interfaceTable(0) if err != nil { return nil, &net.OpError{Op: "route", Net: "ip+net", Source: nil, Addr: nil, Err: err} @@ -32,6 +41,10 @@ func Interfaces() ([]net.Interface, error) { // The returned list does not identify the associated interface; use // Interfaces and Interface.Addrs for more detail. func InterfaceAddrs() ([]net.Addr, error) { + if androidVersion > android11 { + return net.InterfaceAddrs() + } + ifat, err := interfaceAddrTable(nil) if err != nil { err = &net.OpError{Op: "route", Net: "ip+net", Source: nil, Addr: nil, Err: err} @@ -46,6 +59,10 @@ func InterfaceAddrsByInterface(ifi *net.Interface) ([]net.Addr, error) { return nil, &net.OpError{Op: "route", Net: "ip+net", Source: nil, Addr: nil, Err: errInvalidInterface} } + if androidVersion > android11 { + return ifi.Addrs() + } + ifat, err := interfaceAddrTable(ifi) if err != nil { err = &net.OpError{Op: "route", Net: "ip+net", Source: nil, Addr: nil, Err: err} @@ -53,6 +70,13 @@ func InterfaceAddrsByInterface(ifi *net.Interface) ([]net.Addr, error) { return ifat, err } +// SetAndroidVersion set the Android environment in which the program runs. +// The Android system version number can be obtained through +// `android.os.Build.VERSION.RELEASE` of the Android framework. +func SetAndroidVersion(version uint) { + androidVersion = version +} + // If the ifindex is zero, interfaceTable returns mappings of all // network interfaces. Otherwise it returns a mapping of a specific // interface.