{"id":13434758,"url":"https://github.com/wlynxg/anet","last_synced_at":"2025-04-30T07:24:23.824Z","repository":{"id":177630575,"uuid":"660574628","full_name":"wlynxg/anet","owner":"wlynxg","description":"fix go bug #68082 #40569","archived":false,"fork":false,"pushed_at":"2025-01-09T06:58:09.000Z","size":5370,"stargazers_count":28,"open_issues_count":1,"forks_count":17,"subscribers_count":4,"default_branch":"main","last_synced_at":"2025-03-30T13:51:16.040Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","language":"Go","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"bsd-3-clause","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/wlynxg.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2023-06-30T10:09:43.000Z","updated_at":"2025-01-21T02:02:10.000Z","dependencies_parsed_at":"2024-08-14T11:42:25.665Z","dependency_job_id":"3498ea2a-bb55-4edd-b3e0-b06cfff485bf","html_url":"https://github.com/wlynxg/anet","commit_stats":null,"previous_names":["wlynxg/anet"],"tags_count":5,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/wlynxg%2Fanet","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/wlynxg%2Fanet/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/wlynxg%2Fanet/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/wlynxg%2Fanet/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/wlynxg","download_url":"https://codeload.github.com/wlynxg/anet/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":251659252,"owners_count":21623004,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-07-31T03:00:22.252Z","updated_at":"2025-04-30T07:24:23.808Z","avatar_url":"https://github.com/wlynxg.png","language":"Go","funding_links":[],"categories":["HarmonyOS"],"sub_categories":["Windows Manager"],"readme":"## Introduction\nIn response to the modifications made to the permissions for accessing system MAC addresses in Android 11, ordinary applications encounter several main issues when using NETLINK sockets:\n\n- Not allowing bind operations on `NETLINK` sockets.\n- Not permitting the use of the `RTM_GETLINK` functionality.\n\nFor detailed information, please refer to: https://developer.android.com/training/articles/user-data-ids#mac-11-plus\n\nAs a result of the aforementioned reasons, using `net.Interfaces()` and `net.InterfaceAddrs()` from the Go net package in the Android environment leads to the `route ip+net: netlinkrib: permission denied` error. \n\nYou can find specific issue details here: https://github.com/golang/go/issues/40569\n\nTo address the issue of using the Go net package in the Android environment, we have made partial modifications to its source code to ensure proper functionality on Android. \n\nI have fully resolved the issues with `net.InterfaceAddrs()`. \n\nHowever, for `net.Interfaces()`, we have only addressed some problems, as the following issues still remain:\n- It can only return interfaces with IP addresses.\n- It cannot return hardware MAC addresses.\n\nNevertheless, the fixed `net.Interfaces()` function now aligns with the Android API's `NetworkInterface.getNetworkInterfaces()` and can be used normally in most scenarios.\n\nThe specific fix logic includes:\n\nRemoving the `Bind()` operation on `Netlink` sockets in the `NetlinkRIB()` function.\nUsing `ioctl` based on the Index number returned by `RTM_GETADDR` to retrieve the network card's name, MTU, and flags.\n\nThere are two implementations of the `net` package: one from the [Go standard library](https://pkg.go.dev/net) and another from the [golang.org/x/net](https://pkg.go.dev/golang.org/x/net) module. Both of these implementations have the same issues in the Android environment. The `anet` package should be compatible with both of them.\n\n## How to build with Go 1.23.0 or later\nSince https://github.com/golang/go/issues/68082 and https://github.com/golang/go/issues/40569 have the same problem, `anet` chose to solve https://github.com/golang/go/issues/68082 together.\n\nIn order to solve https://github.com/golang/go/issues/68082 `anet` introduced the `//golinkname` mechanism.\n\nBut unfortunately, since the usage of `//go:linkname` has been restricted since Go 1.23.0 ([Go 1.23 Release Notes](https://tip.golang.org/doc/go1.23#linker)), it is necessary to specify the `-ldflags \"-checklinkname=0\"` linker flag when building the `anet` package with Go 1.23.0 or later. \n\nWithout this flag, the following linker error will occur: \n``` \nlink: github.com/wlynxg/anet: invalid reference to net.zoneCache\n```\n\n## Test Code\n### net.Interface()\nuse `net.Interface()`:\n```go\nfunc RawInterface() {\n\tinterfaces, err := net.Interfaces()\n\tif err != nil {\n\t\tpanic(err)\n\t}\n\n\tfor _, i := range interfaces {\n\t\tlog.Println(i)\n\t}\n}\n```\nresult:\n```\npanic: route ip+net: netlinkrib: permission denied\n```\n\nuse `anet.Interface()`:\n```go\nfunc AnetInterface() {\n\tinterfaces, err := anet.Interfaces()\n\tif err != nil {\n\t\tpanic(err)\n\t}\n\n\tfor _, i := range interfaces {\n\t\tlog.Println(i)\n\t}\n}\n```\n\nresult:\n```\n{1 65536 lo  up|loopback|running}\n{15 1400 rmnet_data1  up|running}\n{24 1500 wlan0  up|broadcast|multicast|running}\n{3 1500 dummy0  up|broadcast|running}\n{4 1500 ifb0  up|broadcast|running}\n{5 1500 ifb1  up|broadcast|running}\n{12 1500 ifb2  up|broadcast|running}\n{14 1500 rmnet_data0  up|running}\n{16 1400 rmnet_data2  up|running}\n{17 1400 rmnet_data3  up|running}\n```\n\n### net.InterfaceAddrs()\nuse `net.InterfaceAddrs()`:\n```go\nfunc NetInterfaceAddrs() {\n\taddrs, err := net.InterfaceAddrs()\n\tif err != nil {\n\t\tpanic(err)\n\t}\n\n\tfor _, addr := range addrs {\n\t\tlog.Println(addr)\n\t}\n}\n```\nresult: \n```\npanic: route ip+net: netlinkrib: permission denied\n```\n\nuse `anet.InterfaceAddrs()`:\n```go\nfunc AnetInterfaceAddrs() {\n\taddrs, err := anet.InterfaceAddrs()\n\tif err != nil {\n\t\tpanic(err)\n\t}\n\n\tfor _, addr := range addrs {\n\t\tlog.Println(addr)\n\t}\n}\n```\nresult:\n```\n127.0.0.1/8\n::1/128\n...\n192.168.6.143/24\nfe80::7e4f:4446:eb3:1eb8/64\n```\n\n## Other issues due to #40569\n- https://github.com/golang/go/issues/68082\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fwlynxg%2Fanet","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fwlynxg%2Fanet","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fwlynxg%2Fanet/lists"}