File: main.go

package info (click to toggle)
golang-github-mdlayher-arp 0.0~git20220512.6706a29-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, experimental, forky, sid, trixie
  • size: 140 kB
  • sloc: makefile: 3
file content (76 lines) | stat: -rw-r--r-- 1,794 bytes parent folder | download
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
package main

import (
	"bytes"
	"flag"
	"io"
	"log"
	"net"
	"net/netip"

	"github.com/mdlayher/arp"
	"github.com/mdlayher/ethernet"
)

var (
	// ifaceFlag is used to set a network interface for ARP traffic
	ifaceFlag = flag.String("i", "eth0", "network interface to use for ARP traffic")

	// ipFlag is used to set an IPv4 address to proxy ARP on behalf of
	ipFlag = flag.String("ip", "", "IP address for device to proxy ARP on behalf of")
)

func main() {
	flag.Parse()

	// Ensure valid interface and IPv4 address
	ifi, err := net.InterfaceByName(*ifaceFlag)
	if err != nil {
		log.Fatal(err)
	}
	ip, err := netip.ParseAddr(*ipFlag)
	if err != nil || !ip.Is4() {
		log.Fatalf("invalid IPv4 address: %q", *ipFlag)
	}

	client, err := arp.Dial(ifi)
	if err != nil {
		log.Fatalf("couldn't create ARP client: %s", err)
	}

	// Handle ARP requests bound for designated IPv4 address, using proxy ARP
	// to indicate that the address belongs to this machine
	for {
		pkt, eth, err := client.Read()
		if err != nil {
			if err == io.EOF {
				log.Println("EOF")
				break
			}
			log.Fatalf("error processing ARP requests: %s", err)
		}

		// Ignore ARP replies
		if pkt.Operation != arp.OperationRequest {
			continue
		}

		// Ignore ARP requests which are not broadcast or bound directly for
		// this machine
		if !bytes.Equal(eth.Destination, ethernet.Broadcast) && !bytes.Equal(eth.Destination, ifi.HardwareAddr) {
			continue
		}

		log.Printf("request: who-has %s?  tell %s (%s)", pkt.TargetIP, pkt.SenderIP, pkt.SenderHardwareAddr)

		// Ignore ARP requests which do not indicate the target IP
		if pkt.TargetIP != ip {
			continue
		}

		log.Printf("  reply: %s is-at %s", ip, ifi.HardwareAddr)
		if err := client.Reply(pkt, ifi.HardwareAddr, ip); err != nil {
			log.Fatal(err)
		}
	}
}