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

set PingOverIface forcedly, add option to ignore ipnet check #14

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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
4 changes: 2 additions & 2 deletions arping.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
3 changes: 2 additions & 1 deletion cmd/arping/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,11 @@ package main
import (
"flag"
"fmt"
"github.com/j-keck/arping"
"net"
"os"
"time"

"github.com/fzu-huang/arping"
)

var (
Expand Down
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module github.com/j-keck/arping
module github.com/fzu-huang/arping

go 1.12
go 1.17
19 changes: 16 additions & 3 deletions netutils.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand All @@ -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
Expand Down