Glynn Clements <[EMAIL PROTECTED]> wrote:
>
> Mark K. Gardner wrote:
>
> > 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;
Oops. That's what I meant. ;-)
> > /* 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;
Double oops. This was actually in there at one point, but it succumbed
to bit-rot I guess.
> strcpy(areq.arp_dev, "eth0"); // should be a command-line option
I suspected that something like this was necessary. I added the lines
as you suggested and I still get the "device not configured" error.
After rereading arp(7), it seems that I need to also set the type of
the hardware device. I therefore added the following lines right after
the two you suggested above. I still get "device not configured".
sin = (struct sockaddr_in *) &areq.arp_ha;
sin->sin_family = ARPHRD_ETHER;
Any other suggestions?
PS- In case it helps, I am using RH 6.1 with kernel 2.2.12-20 compiled
with SMP.
PPS- the code with all the above changes is included below.
Mark
--
Mark K. Gardner
RADIANT Team
Network Engineering, CIC-5
Los Alamos National Laboratory
P.O. Box 1663, M.S. D451
Los Alamos, NM 87545
Email: [EMAIL PROTECTED]
Phone: 1-505-665-4953
--
=== Start ===
#include <errno.h>
#include <stdio.h>
#include <sys/socket.h>
#include <net/if_arp.h>
#include <netinet/in.h>
#include <linux/sockios.h>
static char *ethernet_mactoa(struct sockaddr *addr) {
static char buff[256];
unsigned char *ptr = (unsigned char *) addr->sa_data;
sprintf(buff, "%02X:%02X:%02X:%02X:%02X:%02X",
(ptr[0] & 0377), (ptr[1] & 0377), (ptr[2] & 0377),
(ptr[3] & 0377), (ptr[4] & 0377), (ptr[5] & 0377));
return (buff);
}
int main(int argc, char *argv[]) {
int s;
struct arpreq areq;
struct sockaddr_in *sin;
struct in_addr ipaddr;
if (argc < 2 || argc > 2) {
fprintf(stderr, "-- Usage: %s ipaddress\n", argv[0]);
exit(1);
}
/* Get an internet domain socket. */
if ((s = socket(AF_INET, SOCK_DGRAM, 0)) == -1) {
perror("socket");
exit(1);
}
/* 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);
}
sin->sin_addr = ipaddr;
sin = (struct sockaddr_in *) &areq.arp_ha;
sin->sin_family = ARPHRD_ETHER;
strncpy(areq.arp_dev, "eth0", 15);
if (ioctl(s, SIOCGARP, (caddr_t) &areq) == -1) {
perror("-- Error: unable to make ARP request, error");
exit(1);
}
printf("%s (%s) -> %s\n", argv[1],
inet_ntoa(&((struct sockaddr_in *) &areq.arp_pa)->sin_addr),
ethernet_mactoa(&areq.arp_ha));
return 0;
}
==== End ====
-
To unsubscribe from this list: send the line "unsubscribe linux-net" in
the body of a message to [EMAIL PROTECTED]