finding hardware address from ip address
Web Server forum
Back To The Forum Home!Search!Private Messaging System

Web Server Talk Web Server Talk > Unix and Linux reviews > Free Unix support > Unix Programming > finding hardware address from ip address




  Last Thread   Next Thread Next
  Show Printable Version Email this Page Subscribe to this Thread      Post New Thread    Post A Reply      

    finding hardware address from ip address  
manoj


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
08-22-04 11: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





[ Post a follow-up to this message ]



    Re: finding hardware address from ip address  
Michael J. Malone


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
08-22-04 11:08 PM

guptajimanoj@hotmail.com (manoj) wrote in message news:<b9a461e.0408210245.26cec76@posting.g
oogle.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.





[ Post a follow-up to this message ]



    Re: finding hardware address from ip address  
Rich Gibbs


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
08-22-04 11: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






[ Post a follow-up to this message ]



    Re: finding hardware address from ip address  
manoj


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
08-22-04 11:08 PM

Rich Gibbs <rgibbs@REMOVEalumni.CAPSprinceton.edu> wrote in message news:<41279de6@news101.h
is.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 ca
che
then also ethernet address is returned





[ Post a follow-up to this message ]



    Re: finding hardware address from ip address  
manoj


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
08-22-04 11:08 PM

mjmalone@gmail.com (Michael J. Malone) wrote in message news:<a9d9121c.0408210955.484db137@p
osting.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





[ Post a follow-up to this message ]



    Re: finding hardware address from ip address  
Barry Margolin


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
08-22-04 11: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 cach
e
> 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 ***





[ Post a follow-up to this message ]



    Re: finding hardware address from ip address  
Boltar


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
08-23-04 10:55 PM

mjmalone@gmail.com (Michael J. Malone) wrote in message news:<a9d9121c.0408210955.484db137@p
osting.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





[ Post a follow-up to this message ]



    Re: finding hardware address from ip address  
Boltar


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
08-23-04 10:55 PM

mjmalone@gmail.com (Michael J. Malone) wrote in message news:<a9d9121c.0408210955.484db137@p
osting.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





[ Post a follow-up to this message ]



    Sponsored Links  




 





   All times are GMT. The time now is 09:28 AM.      Post New Thread    Post A Reply      
  Last Thread   Next Thread Next


Most Popular forums 

Forum Jump:
Rate This Thread:

Forum Rules:
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is OFF
vB code is ON
Smilies are ON
[IMG] code is OFF
 
Medical and Health forum | Computer Games Reviews | Graphics design forum

Back To The Top
Home | Usercp | Faq | Register