Skip to content

Commit

Permalink
feat: Android system version number setting
Browse files Browse the repository at this point in the history
  • Loading branch information
wlynxg committed Oct 20, 2023
1 parent aa9594d commit ab437b8
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 1 deletion.
4 changes: 3 additions & 1 deletion interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {}
24 changes: 24 additions & 0 deletions interface_android.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
)
Expand All @@ -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}
Expand All @@ -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}
Expand All @@ -46,13 +59,24 @@ 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}
}
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.
Expand Down

0 comments on commit ab437b8

Please sign in to comment.