Mark K. Gardner wrote:
> I need to translate IP addresses to ethernet MAC addresses. I
> initially tried something like the code from Steven's Unix Network
> Programming book, volume 1 (see code #1). Looking up an IP address on
> the same subnet gives ENXIO "no such device". No joy.
>
> Next I looked for other examples on the WWW. They were almost
> identical to code #1. Reading arp(7) caused me to think that I needed
> to fill in some of the other fields in struct arpreq.
Yep. Providing the IP address and the interface would help.
> But I haven't
> been able to find information on how to fill them in correctly. All
> combinations I have tried resulted in EINVAL "invalid argument".
> static char *ethernet_mactoa(struct sockaddr *addr) {
> static char buff[256];
> unsigned char *ptr = (unsigned char *) addr;
Change to:
unsigned char *ptr = (unsigned char *) addr->sa_data;
> /* Make the ARP request. */
> memset(&areq, 0, sizeof(areq));
> sin = (struct sockaddr_in *) &areq.arp_pa;
> sin->sin_family = AF_INET;
> if (inet_aton(argv[1], &ipaddr) == 0) {
> fprintf(stderr, "-- Error: invalid numbers-and-dots IP address %s.\n",
> argv[1]);
> exit(1);
> }
Note that "ipaddr" isn't actually used after this. Add the lines:
sin->sin_addr = ipaddr;
strcpy(areq.arp_dev, "eth0"); // should be a command-line option
--
Glynn Clements <[EMAIL PROTECTED]>
-
To unsubscribe from this list: send the line "unsubscribe linux-net" in
the body of a message to [EMAIL PROTECTED]