Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

net: improve GetIPAddresses() #6

Merged
merged 4 commits into from
Feb 2, 2023
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
net: refactor GetIPAddress to reuse the []net.Addr to []netip.Addr co…
…nversion

Signed-off-by: Alejandro Mery <[email protected]>
  • Loading branch information
amery committed Feb 2, 2023
commit 7f0338368c5e9ad7fdb032374eeb59c3d199747e
32 changes: 19 additions & 13 deletions net.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,23 +58,29 @@ func GetIPAddresses(ifaces ...string) ([]netip.Addr, error) {
return out, err
}

for _, addr := range addrs {
var s []byte

switch v := addr.(type) {
case *net.IPAddr:
s = v.IP
case *net.IPNet:
s = v.IP
}
out = appendNetIPAsIP(out, addrs...)
}

if ip, ok := netip.AddrFromSlice(s); ok {
out = append(out, ip.Unmap())
}
return out, nil
}

func appendNetIPAsIP(out []netip.Addr, addrs ...net.Addr) []netip.Addr {
for _, addr := range addrs {
var s []byte

switch v := addr.(type) {
case *net.IPAddr:
s = v.IP
case *net.IPNet:
s = v.IP
}

if ip, ok := netip.AddrFromSlice(s); ok {
out = append(out, ip.Unmap())
}
}

return out, nil
return out
}

// GetStringIPAddresses returns IP addresses as string
Expand Down