Skip to content

Commit

Permalink
Fetch all ip addresses in a single syscall on Linux
Browse files Browse the repository at this point in the history
  • Loading branch information
swiatekm committed Oct 28, 2024
1 parent b4a498b commit 8590530
Showing 1 changed file with 12 additions and 8 deletions.
20 changes: 12 additions & 8 deletions providers/shared/network.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,19 @@ func Network() (ips, macs []string, err error) {

ips = make([]string, 0, len(ifcs))
macs = make([]string, 0, len(ifcs))
for _, ifc := range ifcs {
addrs, err := ifc.Addrs()
if err != nil {
return nil, nil, err
}
for _, addr := range addrs {
ips = append(ips, addr.String())
}

// This function fetches all the addresses in a single syscall. Fetching addresses individually for each interface
// can be expensive when the host has a lot of interfaces. This usually happens when the host is doing virtualized
// networking for guests, in Kubernetes for example.
addrs, err := net.InterfaceAddrs()
if err != nil {
return nil, nil, err
}
for _, addr := range addrs {
ips = append(ips, addr.String())
}

for _, ifc := range ifcs {
mac := ifc.HardwareAddr.String()
if mac != "" {
macs = append(macs, mac)
Expand Down

0 comments on commit 8590530

Please sign in to comment.