IPv4 address get
@@ -1357,28 +1357,19 @@ func FindJumpMap(ap *tc.AttachPoint) (mapFD bpf.MapFD, err error) {
}
func getInterfaceIP(ifaceName string) (net.IP, error) {
- ifaces, err := net.Interfaces()
- var ip net.IP
+ intf,err := net.InterfaceByName(ifaceName)
if err != nil {
- return ip,err
+ return net.IPv4zero, err
}
- for _, i := range ifaces {
- addrs, err := i.Addrs()
+ addrs, err := intf.Addrs()
if err != nil{
- return ip, err
+ return net.IPv4zero, err
}
for _,addr := range addrs {
- switch v := addr.(type) {
- case *net.IPNet:
- ip = v.IP
- case *net.IPAddr:
- ip = v.IP
- if ip.To4() != nil {
- break
+ if ipv4Addr := addr.(*net.IPNet).IP.To4(); ipv4Addr != nil {
+ return ipv4Addr, nil
}
}
- }
- }
- return ip, nil
+ return net.IPv4zero, errors.New("IP address not present")
}
