func main() { log.SetFlags(0) usage := `Usage: arp <addr> Resolve the given IP address using ARP.` args, err := docopt.Parse(usage, nil, true, "", false) if err != nil { log.Fatalf("Invalid arguments: %s", err) } addr := args["<addr>"].(string) addr_ip := net.ParseIP(addr) timeout := 5 * time.Second route, err := routing.RouteTo(addr_ip) if err != nil { log.Fatalf("Error: %s", err) } if route == nil { log.Println("No route found") } c, err := pcap.Open(route.Iface.Name) if err != nil { log.Fatalf("Error opening interface: %s", err) } defer c.Close() err = c.Activate() if err != nil { log.Fatalf("Error activating source: %s", err) } eth_pkt := eth.Make() eth_pkt.SrcAddr = route.Iface.HardwareAddr eth_pkt.DstAddr, _ = net.ParseMAC("ff:ff:ff:ff:ff:ff") arp_pkt := arp.Make() arp_pkt.HWSrcAddr = route.Iface.HardwareAddr arp_pkt.HWDstAddr, _ = net.ParseMAC("00:00:00:00:00:00") arp_pkt.ProtoSrcAddr, _ = route.GetIfaceIPv4Addr() arp_pkt.ProtoDstAddr = addr_ip pkt, err := network.SendRecv(c, timeout, eth_pkt, arp_pkt) if err != nil { log.Fatal(err) } log.Println(pkt.Payload().(*arp.Packet).HWSrcAddr) }
func ResolveARP(c capture.Handle, t time.Duration, r *routing.Route, addr net.IP) net.HardwareAddr { eth_pkt := eth.Make() eth_pkt.SrcAddr = r.Iface.HardwareAddr eth_pkt.DstAddr, _ = net.ParseMAC("ff:ff:ff:ff:ff:ff") arp_pkt := arp.Make() arp_pkt.HWSrcAddr = r.Iface.HardwareAddr arp_pkt.HWDstAddr, _ = net.ParseMAC("00:00:00:00:00:00") arp_pkt.ProtoSrcAddr, _ = r.GetIfaceIPv4Addr() arp_pkt.ProtoDstAddr = addr pkt, err := network.SendRecv(c, t, eth_pkt, arp_pkt) if err != nil { log.Fatal(err) } return pkt.Payload().(*arp.Packet).HWSrcAddr }
func TestPackEthArp(t *testing.T) { eth_pkt := eth.Make() eth_pkt.SrcAddr, _ = net.ParseMAC(hwsrc_str) eth_pkt.DstAddr, _ = net.ParseMAC("ff:ff:ff:ff:ff:ff") arp_pkt := arp.Make() arp_pkt.HWSrcAddr, _ = net.ParseMAC(hwsrc_str) arp_pkt.HWDstAddr, _ = net.ParseMAC("00:00:00:00:00:00") arp_pkt.ProtoSrcAddr = net.ParseIP(ipsrc_str) arp_pkt.ProtoDstAddr = net.ParseIP(ipdst_str) buf, err := layers.Pack(eth_pkt, arp_pkt) if err != nil { t.Fatalf("Error packing: %s", err) } if !bytes.Equal(test_eth_arp, buf) { t.Fatalf("Raw packet mismatch: %x", buf) } }
func ExamplePack() { // Create an Ethernet packet eth_pkt := eth.Make() eth_pkt.SrcAddr, _ = net.ParseMAC("4c:72:b9:54:e5:3d") eth_pkt.DstAddr, _ = net.ParseMAC("ff:ff:ff:ff:ff:ff") // Create an ARP packet arp_pkt := arp.Make() arp_pkt.HWSrcAddr, _ = net.ParseMAC("4c:72:b9:54:e5:3d") arp_pkt.HWDstAddr, _ = net.ParseMAC("00:00:00:00:00:00") arp_pkt.ProtoSrcAddr = net.ParseIP("192.168.1.135") arp_pkt.ProtoDstAddr = net.ParseIP("192.168.1.254") buf, err := layers.Pack(eth_pkt, arp_pkt) if err != nil { log.Fatal(err) } // do something with the packet log.Println(buf) }