2 * $Id: get_mac.c,v 1.1.1.1 2000/05/25 00:28:49 route Exp $
5 * get_mac.c - get the MAC address of a remote host
7 * Copyright (c) 1998 - 2001 Mike D. Schiffman <mike@infonexus.com>
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 #include "../../include/config.h"
39 #define IP_UCHAR_COMP(x, y) \
40 (x[0] == y[0] && x[1] == y[1] && x[2] == y[2] && x[3] == y[3])
43 u_char enet_dst[6] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
45 int libnet_do_arp(struct libnet_link_int *, u_char *, struct ether_addr *,
49 main(int argc, char *argv[])
54 struct libnet_link_int *l;
58 while ((c = getopt(argc, argv, "i:")) != EOF)
72 fprintf(stderr, "Specify a device\n");
77 if ((ip = libnet_name_resolve(argv[optind], 1)) == -1)
79 fprintf(stderr, "Cannot resolve IP address\n");
85 fprintf(stderr, "IP address to ARP for?\n");
89 l = libnet_open_link_interface(device, errbuf);
92 fprintf(stderr, "libnet_open_link_interface: %s\n", errbuf);
96 c = libnet_do_arp(l, device, &e, ip);
99 for (i = 0; i < 6; i++)
101 printf("%x", e.ether_addr_octet[i]);
109 return (c == -1 ? EXIT_FAILURE : EXIT_SUCCESS);
114 libnet_do_arp(struct libnet_link_int *l, u_char *device, struct ether_addr *e,
118 u_char *buf, errbuf[256], *packet, *ip_p;
119 struct libnet_ethernet_hdr *p;
120 struct libnet_arp_hdr *a;
123 struct pcap_pkthdr pc_hdr;
126 * Initialize a packet.
128 if (libnet_init_packet(LIBNET_ARP_H + LIBNET_ETH_H, &buf) == -1)
130 perror("libnet_init_packet memory:");
135 * Get the ethernet address of the device.
137 e = libnet_get_hwaddr(l, device, errbuf);
140 fprintf(stderr, "libnet_get_hwaddr: %s\n", errbuf);
145 * Get the IP address of the device.
147 local_ip = htonl(libnet_get_ipaddr(l, device, errbuf));
150 fprintf(stderr, "libnet_get_ipaddr: %s\n", errbuf);
155 * Open the pcap device.
157 pd = pcap_open_live(device, LIBNET_ARP_H + LIBNET_ETH_H, 1, 500, errbuf);
160 fprintf(stderr, "pcap_open_live: %s\n", errbuf);
167 libnet_build_ethernet(
168 enet_dst, /* broadcast ethernet address */
169 e->ether_addr_octet, /* source ethernet address */
170 ETHERTYPE_ARP, /* this frame holds an ARP packet */
172 0, /* payload size */
173 buf); /* packet header memory */
179 ARPHRD_ETHER, /* hardware address type */
180 ETHERTYPE_IP, /* protocol address type */
181 ETHER_ADDR_LEN, /* hardware address length */
182 4, /* protocol address length */
183 ARPOP_REQUEST, /* packet type - ARP request */
184 e->ether_addr_octet, /* source (local) ethernet address */
185 (u_char *)&local_ip, /* source (local) IP address */
186 enet_dst, /* target's ethernet address (broadcast) */
187 (u_char *)&ip, /* target's IP address */
189 0, /* payload size */
190 buf + LIBNET_ETH_H); /* packet header memory */
192 n = libnet_write_link_layer(l, device, buf, LIBNET_ARP_H + LIBNET_ETH_H);
195 fprintf(stderr, "libnet_write_link_layer choked\n");
198 printf("Sent a %d byte ARP request looking for the MAC of %s.\n",
199 n, libnet_host_lookup(ip, 0));
200 printf("Waiting for a response...\n");
202 for (;(packet = ((u_char *)pcap_next(pd, &pc_hdr)));)
204 p = (struct libnet_ethernet_hdr *)(packet);
205 if (ntohs(p->ether_type) == ETHERTYPE_ARP)
207 a = (struct libnet_arp_hdr *)(packet + LIBNET_ETH_H);
208 if (ntohs(a->ar_op) == ARPOP_REPLY)
210 ip_p = (u_char *)&ip;
211 if (IP_UCHAR_COMP(a->ar_spa, ip_p))
213 printf("Target hardware address: ");
214 for (i = 0; i < 6; i++)
216 printf("%x", a->ar_sha[i]);
228 libnet_destroy_packet(&buf);