File: arping_linux.go

package info (click to toggle)
golang-github-j-keck-arping 0.0~git20160618.2cf9dc6-1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 92 kB
  • sloc: makefile: 3
file content (38 lines) | stat: -rw-r--r-- 853 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
package arping

import (
	"net"
	"syscall"
	"time"
)

var sock int
var toSockaddr syscall.SockaddrLinklayer

func initialize(iface net.Interface) error {
	toSockaddr = syscall.SockaddrLinklayer{Ifindex: iface.Index}

	// 1544 = htons(ETH_P_ARP)
	const proto = 1544
	var err error
	sock, err = syscall.Socket(syscall.AF_PACKET, syscall.SOCK_RAW, proto)
	return err
}

func send(request arpDatagram) (time.Time, error) {
	return time.Now(), syscall.Sendto(sock, request.MarshalWithEthernetHeader(), 0, &toSockaddr)
}

func receive() (arpDatagram, time.Time, error) {
	buffer := make([]byte, 128)
	n, _, err := syscall.Recvfrom(sock, buffer, 0)
	if err != nil {
		return arpDatagram{}, time.Now(), err
	}
	// skip 14 bytes ethernet header
	return parseArpDatagram(buffer[14:n]), time.Now(), nil
}

func deinitialize() error {
	return syscall.Close(sock)
}