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

remove the risk of file descriptor reuse from arping #17

Open
wants to merge 1 commit 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
3 changes: 1 addition & 2 deletions arping.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 {
Expand Down Expand Up @@ -163,7 +163,6 @@ func PingOverIface(dstIP net.IP, iface net.Interface) (net.HardwareAddr, time.Du
case pingResult := <-pingResultChan:
return pingResult.mac, pingResult.duration, pingResult.err
case <-time.After(timeout):
sock.deinitialize()
return nil, 0, ErrTimeout
}
}
Expand Down
5 changes: 4 additions & 1 deletion arping_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,15 @@ func initialize(iface net.Interface) (s *LinuxSocket, err error) {
}

func (s *LinuxSocket) send(request arpDatagram) (time.Time, error) {
socketTimeout := timeout.Nanoseconds()
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)
}

func (s *LinuxSocket) receive() (arpDatagram, time.Time, error) {
buffer := make([]byte, 128)
socketTimeout := timeout.Nanoseconds() * 2
socketTimeout := timeout.Nanoseconds()
t := syscall.NsecToTimeval(socketTimeout)
syscall.SetsockoptTimeval(s.sock, syscall.SOL_SOCKET, syscall.SO_RCVTIMEO, &t)
n, _, err := syscall.Recvfrom(s.sock, buffer, 0)
Expand Down