mirror of
https://github.com/golang/go.git
synced 2025-12-08 06:10:04 +00:00
net: avoid unnecessary interface lookup fetching all interface addresses
InterfaceAddrs returns a list of the system's unicast interface addresses. In order to do so, the function reuses the existing helpers and list first all addresses with the netlink call RTM_GETADDR, then all interfaces with RTM_GETLINK, and later it merge both lists (each address references an interface). However, the list of interfaces and addresses are obtained at different times and there can be inconsistencies and, if an address references an interface that is not present in the list of interfaces, the function fails with an error. Since the function InterfaceAddress is only about the system addresses, there is no need to list all the interfaces, and we can obtain the list of addresses directly from the netlink call RTM_GETADDR. There is no need to correlate this list with the list of interfaces, as the OS is the source of truth and should be the one providing the consistency between addresses and interfaces. Fixes #51934 Change-Id: I3b816e8146b1c07fdfe1bf6af338f001ef75734f Reviewed-on: https://go-review.googlesource.com/c/go/+/635196 Reviewed-by: Ian Lance Taylor <iant@google.com> Commit-Queue: Ian Lance Taylor <iant@google.com> LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com> Auto-Submit: Ian Lance Taylor <iant@google.com>
This commit is contained in:
parent
e424d78c3d
commit
979c1cfbe8
1 changed files with 3 additions and 18 deletions
|
|
@ -129,22 +129,14 @@ func interfaceAddrTable(ifi *Interface) ([]Addr, error) {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, os.NewSyscallError("parsenetlinkmessage", err)
|
return nil, os.NewSyscallError("parsenetlinkmessage", err)
|
||||||
}
|
}
|
||||||
var ift []Interface
|
ifat, err := addrTable(ifi, msgs)
|
||||||
if ifi == nil {
|
|
||||||
var err error
|
|
||||||
ift, err = interfaceTable(0)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
}
|
|
||||||
ifat, err := addrTable(ift, ifi, msgs)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
return ifat, nil
|
return ifat, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func addrTable(ift []Interface, ifi *Interface, msgs []syscall.NetlinkMessage) ([]Addr, error) {
|
func addrTable(ifi *Interface, msgs []syscall.NetlinkMessage) ([]Addr, error) {
|
||||||
var ifat []Addr
|
var ifat []Addr
|
||||||
loop:
|
loop:
|
||||||
for _, m := range msgs {
|
for _, m := range msgs {
|
||||||
|
|
@ -153,14 +145,7 @@ loop:
|
||||||
break loop
|
break loop
|
||||||
case syscall.RTM_NEWADDR:
|
case syscall.RTM_NEWADDR:
|
||||||
ifam := (*syscall.IfAddrmsg)(unsafe.Pointer(&m.Data[0]))
|
ifam := (*syscall.IfAddrmsg)(unsafe.Pointer(&m.Data[0]))
|
||||||
if len(ift) != 0 || ifi.Index == int(ifam.Index) {
|
if ifi == nil || ifi.Index == int(ifam.Index) {
|
||||||
if len(ift) != 0 {
|
|
||||||
var err error
|
|
||||||
ifi, err = interfaceByIndex(ift, int(ifam.Index))
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
}
|
|
||||||
attrs, err := syscall.ParseNetlinkRouteAttr(&m)
|
attrs, err := syscall.ParseNetlinkRouteAttr(&m)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, os.NewSyscallError("parsenetlinkrouteattr", err)
|
return nil, os.NewSyscallError("parsenetlinkrouteattr", err)
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue