Unix Programming - finding hardware address from ip address

This is Interesting: Free IT Magazines  
Home > Archive > Unix Programming > August 2004 > finding hardware address from ip address





You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

Author finding hardware address from ip address
manoj

2004-08-22, 6:08 pm

how can i find harware address on ethernet given the ip address using
a C , PERL or Java program on a unix platform
Michael J. Malone

2004-08-22, 6:08 pm

guptajimanoj@hotmail.com (manoj) wrote in message news:<b9a461e.0408210245.26cec76@posting.google.com>...
> how can i find harware address on ethernet given the ip address using
> a C , PERL or Java program on a unix platform


The ioctl() system call, with a socket file descripter as the first
argument, and a SIOCGARP request, will return the hardware address in
the buffer provided by the third argument.

For example:

struct arpreq {
struct sockaddr arp_pa;
struct sockaddr arp_ha;
int arp_flags;
} arpreq;
unsigned char *ptr;

sockfd = socket(AF_INET, SOCK_DGRAM, 0);
....
ioctl(sockfd, SIOCGARP, &arpreq);
ptr = &arpreq.arp_ha.sa_data[0];
printf("%x:%x:%x:%x:%x:%x\n", *ptr, *(ptr + 1), *(ptr + 2), *(ptr +
3), *(ptr + 4), *(ptr + 5));

Check out Stevens, UNPv1 for more info--particularly chapter 16.6
where he develops a primitive version of the unix 'ifconfig' program.
Rich Gibbs

2004-08-22, 6:08 pm

manoj said the following, on 08/21/04 06:45:
> how can i find harware address on ethernet given the ip address using
> a C , PERL or Java program on a unix platform


man 8 arp

--
Rich Gibbs
rgibbs@alumni.princeton.edu

manoj

2004-08-22, 6:08 pm

Rich Gibbs <rgibbs@REMOVEalumni.CAPSprinceton.edu> wrote in message news:<41279de6@news101.his.com>...
> manoj said the following, on 08/21/04 06:45:
>
> man 8 arp


thanx for ur answers but i have already tried those
the program that micheal has given only reads the arp cache
but if the ip - ethernet pair is not in the cache
the return value of ioctl = -1
how to get around this problem so that even is the pair is not in the arp cache
then also ethernet address is returned
manoj

2004-08-22, 6:08 pm

mjmalone@gmail.com (Michael J. Malone) wrote in message news:<a9d9121c.0408210955.484db137@posting.google.com>...
> guptajimanoj@hotmail.com (manoj) wrote in message news:<b9a461e.0408210245.26cec76@posting.google.com>...
>
> The ioctl() system call, with a socket file descripter as the first
> argument, and a SIOCGARP request, will return the hardware address in
> the buffer provided by the third argument.
>
> For example:
>
> struct arpreq {
> struct sockaddr arp_pa;
> struct sockaddr arp_ha;
> int arp_flags;
> } arpreq;
> unsigned char *ptr;
>
> sockfd = socket(AF_INET, SOCK_DGRAM, 0);
> ...
> ioctl(sockfd, SIOCGARP, &arpreq);
> ptr = &arpreq.arp_ha.sa_data[0];
> printf("%x:%x:%x:%x:%x:%x\n", *ptr, *(ptr + 1), *(ptr + 2), *(ptr +
> 3), *(ptr + 4), *(ptr + 5));
>
> Check out Stevens, UNPv1 for more info--particularly chapter 16.6
> where he develops a primitive version of the unix 'ifconfig' program.



thanx for ur ans
but the above program just reads the arp cache
if the ipaddress-ethernet pair is not in the cache
the return value of the ioctl function = -1
how to get around this problem so that even if the pair is not in the cache
i should get the ethernet address
Barry Margolin

2004-08-22, 6:08 pm

In article <b9a461e.0408212023.1f24997@posting.google.com>,
guptajimanoj@hotmail.com (manoj) wrote:

> but the above program just reads the arp cache
> if the ipaddress-ethernet pair is not in the cache
> the return value of the ioctl function = -1
> how to get around this problem so that even if the pair is not in the cache
> i should get the ethernet address


Ping the address first, which should force it to be in the cache. Note,
however, that this only works for devices attached to the local network.
Hardware addresses are not used for communications outside of the local
LAN, so there's not any completely general and portable way to get this
information across LANs.

If the devices all support SNMP, you might be able to perform SNMP
queries to get the information from them. I think it's in the
NetToMedia table.

--
Barry Margolin, barmar@alum.mit.edu
Arlington, MA
*** PLEASE post questions in newsgroups, not directly to me ***
Boltar

2004-08-23, 5:55 pm

mjmalone@gmail.com (Michael J. Malone) wrote in message news:<a9d9121c.0408210955.484db137@posting.google.com>...
> guptajimanoj@hotmail.com (manoj) wrote in message news:<b9a461e.0408210245.26cec76@posting.google.com>...
>
> The ioctl() system call, with a socket file descripter as the first
> argument, and a SIOCGARP request, will return the hardware address in
> the buffer provided by the third argument.
>
> For example:
>
> struct arpreq {
> struct sockaddr arp_pa;
> struct sockaddr arp_ha;
> int arp_flags;
> } arpreq;
> unsigned char *ptr;
>
> sockfd = socket(AF_INET, SOCK_DGRAM, 0);
> ...
> ioctl(sockfd, SIOCGARP, &arpreq);
> ptr = &arpreq.arp_ha.sa_data[0];
> printf("%x:%x:%x:%x:%x:%x\n", *ptr, *(ptr + 1), *(ptr + 2), *(ptr +
> 3), *(ptr + 4), *(ptr + 5));
>


With the datagram socket you're using above presumably it returns the
hardware address of the machine that sent the most recent packet to the
socket? Or will it only work with connected datagram sockets?

B2003
Boltar

2004-08-23, 5:55 pm

mjmalone@gmail.com (Michael J. Malone) wrote in message news:<a9d9121c.0408210955.484db137@posting.google.com>...
> guptajimanoj@hotmail.com (manoj) wrote in message news:<b9a461e.0408210245.26cec76@posting.google.com>...
>
> The ioctl() system call, with a socket file descripter as the first
> argument, and a SIOCGARP request, will return the hardware address in
> the buffer provided by the third argument.
>
> For example:
>
> struct arpreq {
> struct sockaddr arp_pa;
> struct sockaddr arp_ha;
> int arp_flags;
> } arpreq;
> unsigned char *ptr;
>
> sockfd = socket(AF_INET, SOCK_DGRAM, 0);
> ...
> ioctl(sockfd, SIOCGARP, &arpreq);
> ptr = &arpreq.arp_ha.sa_data[0];
> printf("%x:%x:%x:%x:%x:%x\n", *ptr, *(ptr + 1), *(ptr + 2), *(ptr +
> 3), *(ptr + 4), *(ptr + 5));
>


With the datagram socket you're using above presumably it returns the
hardware address of the machine that sent the most recent packet to the
socket? Or will it only work with connected datagram sockets?

B2003
Sponsored Links






Free braindumps | Software forum | Database administration forum

Copyright 2003 - 2008 webservertalk.com