diff --git a/arping.go b/arping.go index b305731..9e3d0fd 100644 --- a/arping.go +++ b/arping.go @@ -121,7 +121,6 @@ func PingOverIface(dstIP net.IP, iface net.Interface) (net.HardwareAddr, time.Du if err != nil { return nil, 0, err } - defer sock.deinitialize() type PingResult struct { mac net.HardwareAddr @@ -131,6 +130,7 @@ func PingOverIface(dstIP net.IP, iface net.Interface) (net.HardwareAddr, time.Du pingResultChan := make(chan PingResult, 1) go func() { + defer sock.deinitialize() // send arp request verboseLog.Printf("arping '%s' over interface: '%s' with address: '%s'\n", dstIP, iface.Name, srcIP) if sendTime, err := sock.send(request); err != nil { @@ -162,9 +162,6 @@ func PingOverIface(dstIP net.IP, iface net.Interface) (net.HardwareAddr, time.Du select { case pingResult := <-pingResultChan: return pingResult.mac, pingResult.duration, pingResult.err - case <-time.After(timeout): - sock.deinitialize() - return nil, 0, ErrTimeout } } diff --git a/arping_linux.go b/arping_linux.go index fc225da..75485fb 100644 --- a/arping_linux.go +++ b/arping_linux.go @@ -23,6 +23,9 @@ func initialize(iface net.Interface) (s *LinuxSocket, err error) { } func (s *LinuxSocket) send(request arpDatagram) (time.Time, error) { + socketTimeout := timeout.Nanoseconds * 2 + t := syscall.NsecToTimeval(socketTimeout) + syscall.SetsockoptTimeval(s.sock, syscall.SOL_SOCKET, syscall.SO_SNDTIMEO, &t) return time.Now(), syscall.Sendto(s.sock, request.MarshalWithEthernetHeader(), 0, &s.toSockaddr) }