Problems getting MAC address of network card

Problems getting MAC address of network card

Post by Joha » Wed, 15 Nov 2000 04:00:00



I'm trying to get the MAC address from my ethernet card with the name
eth0. I do this with the ioctl call SIOCGIFHWADDR with an ifreq struct
(ifr) as an argument. The hardware address is then supposed to be put in
ifr.ifr_hwaddr.sa_data but that string is empty. Why?

This is the code I use:

int main(void)
{
  int i;
  int ioctl_sid;
  static char mac_addr[16];
  struct ifreq ifr;
  char interface[] = "eth0";

  if ((ioctl_sid = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
        printf("Unable to create a new socket.");
 return(-1);

  strcpy(ifr.ifr_name, interface);

  if (ioctl(ioctl_sid, SIOCGIFHWADDR, &ifr) < 0) {
    printf("Unable to get MAC addrress of the interface: %s.",
    interface);
    close(ioctl_sid)
    return(-1);
  }

  else  {
    memcpy(mac_addr, ifr.ifr_hwaddr.sa_data, 8);
  }
  printf("mac_addr = %s\n", mac_addr); /* This one returns an empty
string, why? */
  close(ioctl_sid);
  return(0);

Quote:}

 
 
 

Problems getting MAC address of network card

Post by Klaus Stysc » Wed, 15 Nov 2000 04:00:00


have you tried to acces each element of the MAC address directly, like

printf ("%X:", ifr.ifr_ifru.ifru_hwaddr.sa_data[0]);
printf ("%X:", ifr.ifr_ifru.ifru_hwaddr.sa_data[2]);
...
printf ("%X\n", ifr.ifr_ifru.ifru_hwaddr.sa_data[5]);

(perhaps some additional header-files have to be included)

bye,
   klaus


> I'm trying to get the MAC address from my ethernet card with the name
> eth0. I do this with the ioctl call SIOCGIFHWADDR with an ifreq struct
> (ifr) as an argument. The hardware address is then supposed to be put in
> ifr.ifr_hwaddr.sa_data but that string is empty. Why?

> This is the code I use:

> int main(void)
> {
>   int i;
>   int ioctl_sid;
>   static char mac_addr[16];
>   struct ifreq ifr;
>   char interface[] = "eth0";

>   if ((ioctl_sid = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
>         printf("Unable to create a new socket.");
>  return(-1);

>   strcpy(ifr.ifr_name, interface);

>   if (ioctl(ioctl_sid, SIOCGIFHWADDR, &ifr) < 0) {
>     printf("Unable to get MAC addrress of the interface: %s.",
>     interface);
>     close(ioctl_sid)
>     return(-1);
>   }

>   else  {
>     memcpy(mac_addr, ifr.ifr_hwaddr.sa_data, 8);
>   }
>   printf("mac_addr = %s\n", mac_addr); /* This one returns an empty
> string, why? */
>   close(ioctl_sid);
>   return(0);
> }


 
 
 

Problems getting MAC address of network card

Post by David Wrag » Wed, 15 Nov 2000 04:00:00



> I'm trying to get the MAC address from my ethernet card with the name
> eth0. I do this with the ioctl call SIOCGIFHWADDR with an ifreq struct
> (ifr) as an argument. The hardware address is then supposed to be put in
> ifr.ifr_hwaddr.sa_data but that string is empty. Why?

> [snip]
>   if (ioctl(ioctl_sid, SIOCGIFHWADDR, &ifr) < 0) {
>     printf("Unable to get MAC addrress of the interface: %s.",
>     interface);
>     close(ioctl_sid)
>     return(-1);
>   }

>   else  {
>     memcpy(mac_addr, ifr.ifr_hwaddr.sa_data, 8);
>   }
>   printf("mac_addr = %s\n", mac_addr); /* This one returns an empty
> string, why? */

ifr_hwaddr is a struct sockaddr.  The sa_data field contains the raw
address data, not a MAC address formatted as a string.  It is not nul
terminated, and it is not something you want printed on a tty in any
case.

You need to check that ifr_hwaddr.sa_family contains ARPHRD_ETHER;
then sa_data contains the six bytes of the Ethernet MAC.  If you want
it printed in the conventional form, you need to use printf (I don't
think there is a libc function to do this).

David Wragg

 
 
 

Problems getting MAC address of network card

Post by vaneg.. » Wed, 15 Nov 2000 04:00:00





> > I'm trying to get the MAC address from my ethernet card with the
name
> > eth0. I do this with the ioctl call SIOCGIFHWADDR with an ifreq
struct
> > (ifr) as an argument. The hardware address is then supposed to be
put in
> > ifr.ifr_hwaddr.sa_data but that string is empty. Why?

> > [snip]
> >   if (ioctl(ioctl_sid, SIOCGIFHWADDR, &ifr) < 0) {
> >     printf("Unable to get MAC addrress of the interface: %s.",
> >     interface);
> >     close(ioctl_sid)
> >     return(-1);
> >   }

> >   else  {
> >     memcpy(mac_addr, ifr.ifr_hwaddr.sa_data, 8);
> >   }
> >   printf("mac_addr = %s\n", mac_addr); /* This one returns an empty
> > string, why? */

> ifr_hwaddr is a struct sockaddr.  The sa_data field contains the raw
> address data, not a MAC address formatted as a string.  It is not nul
> terminated, and it is not something you want printed on a tty in any
> case.

> You need to check that ifr_hwaddr.sa_family contains ARPHRD_ETHER;
> then sa_data contains the six bytes of the Ethernet MAC.  If you want
> it printed in the conventional form, you need to use printf (I don't
> think there is a libc function to do this).

> David Wragg

the struct sockaddr is 16byte lenght , the MAC addr is 6 byte lenght,
and begins at the third byte of the structure .

Sent via Deja.com http://www.deja.com/
Before you buy.