Christian Reinecke
|
Getting MAC for a given IP
|
Christian Reinecke
03/11/2010 4:29 AM
post49249
|
Getting MAC for a given IP
Hello,
I am trying to get the MAC address for a given IP.
I can do an "arp -a | grep xxx | sed yyy", but I need it within my sourcecode.
I have been trying to set up the ioctl function that should return this value, but it always fails with errno 6 "No such
device or address".
Here is what I am trying to do:
struct arpreq req;
struct sockaddr_in *sin = (struct sockaddr_in *)&req.arp_pa;
memset(&req, 0, sizeof(req));
sin->sin_family = AF_INET;
memcpy(&sin->sin_addr.s_addr, &servAddr.sin_addr.s_addr, sizeof(sin->sin_addr.s_addr));
if(ioctl(sd, SIOCGARP, (caddr_t)&req) != EOK)
printf("errno %d %s", errno, strerror(errno));
Before this snippet, I have created a socket with
sd = socket(AF_INET, SOCK_STREAM, 0);
and connected to the server with
connect(sd, (struct sockaddr *) &servAddr, sizeof(servAddr));
The connection is built up without Problems, so I assume the servAddr-struct is valid.
Regards
Christian
|
|
|