From 7473a7604ac351f105da1a740d0a08fb9ea4bd7d Mon Sep 17 00:00:00 2001 From: mangoyhuang Date: Thu, 27 Apr 2023 10:55:50 +0800 Subject: [PATCH 1/3] set PingOverIface forcedly, add option to ignore ipnet check --- arping.go | 4 ++-- netutils.go | 19 ++++++++++++++++--- 2 files changed, 18 insertions(+), 5 deletions(-) diff --git a/arping.go b/arping.go index b305731..b689be2 100644 --- a/arping.go +++ b/arping.go @@ -102,14 +102,14 @@ func PingOverIfaceByName(dstIP net.IP, ifaceName string) (net.HardwareAddr, time return PingOverIface(dstIP, *iface) } -// PingOverIface sends an arp ping over interface 'iface' to 'dstIP' +// PingOverIface forcedly sends an arp ping over interface 'iface' to 'dstIP' func PingOverIface(dstIP net.IP, iface net.Interface) (net.HardwareAddr, time.Duration, error) { if err := validateIP(dstIP); err != nil { return nil, 0, err } srcMac := iface.HardwareAddr - srcIP, err := findIPInNetworkFromIface(dstIP, iface) + srcIP, err := findIPInNetworkFromIface(dstIP, iface, true) if err != nil { return nil, 0, err } diff --git a/netutils.go b/netutils.go index 2b707d3..6885f71 100644 --- a/netutils.go +++ b/netutils.go @@ -6,20 +6,33 @@ import ( "net" ) -func findIPInNetworkFromIface(dstIP net.IP, iface net.Interface) (net.IP, error) { +// findIPInNetworkFromIface find an ip from iface as src +func findIPInNetworkFromIface(dstIP net.IP, iface net.Interface, ignoreNet bool) (net.IP, error) { addrs, err := iface.Addrs() if err != nil { return nil, err } - for _, a := range addrs { + if len(addrs) == 0 { + return nil, fmt.Errorf("iface: '%s' do not contains any ip", iface.Name) + } + + var firstIP net.IP + for i, a := range addrs { if ipnet, ok := a.(*net.IPNet); ok { if ipnet.Contains(dstIP) { return ipnet.IP, nil } + if i == 0 { + firstIP = ipnet.IP + } } } + + if ignoreNet { + return firstIP, nil + } return nil, fmt.Errorf("iface: '%s' can't reach ip: '%s'", iface.Name, dstIP) } @@ -35,7 +48,7 @@ func findUsableInterfaceForNetwork(dstIP net.IP) (*net.Interface, error) { } hasAddressInNetwork := func(iface net.Interface) bool { - if _, err := findIPInNetworkFromIface(dstIP, iface); err != nil { + if _, err := findIPInNetworkFromIface(dstIP, iface, false); err != nil { return false } return true From b3881a7909d9394e3af64caf2bef43301c56d588 Mon Sep 17 00:00:00 2001 From: mangoyhuang Date: Thu, 27 Apr 2023 11:01:58 +0800 Subject: [PATCH 2/3] update go mod --- go.mod | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/go.mod b/go.mod index 04c772b..17ec164 100644 --- a/go.mod +++ b/go.mod @@ -1,3 +1,3 @@ -module github.com/j-keck/arping +module github.com/fzu-huang/arping -go 1.12 +go 1.17 From 929de1889816fdf3e58f3b1051dc1e702e06560a Mon Sep 17 00:00:00 2001 From: mangoyhuang Date: Thu, 27 Apr 2023 17:39:17 +0800 Subject: [PATCH 3/3] fix import --- cmd/arping/main.go | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/cmd/arping/main.go b/cmd/arping/main.go index 8c1fb77..fc2cdcd 100644 --- a/cmd/arping/main.go +++ b/cmd/arping/main.go @@ -24,10 +24,11 @@ package main import ( "flag" "fmt" - "github.com/j-keck/arping" "net" "os" "time" + + "github.com/fzu-huang/arping" ) var (