Usenet comp.unix.tru64 (2000-2013)
 help / color / mirror / Atom feed
* Re: [Q]How to get ethernet hardware(MAC) address?
  2000-11-24  5:00 [Q]How to get ethernet hardware(MAC) address? Kim YuSung
@ 2000-11-23  5:00 ` Floyd Davidson
  2000-11-26 14:00 ` Paul Szabo
  1 sibling, 0 replies; 5+ messages in thread
From: Floyd Davidson @ 2000-11-23  5:00 UTC (permalink / raw)


"Kim YuSung" <yskim@inzen.com> wrote:
>Hi, I'm want to get ethernet hardware address of my own network card in
>program.
>
>In the book "UNIX Network Programming", I founded an approach to get
>ethernet address from arp table.
>
>That approach do well at solaris 2.6, 7, HP-UX 11.00.
>
>But on HP-UX 10.20, and digital-unix 4.0f, it doesn't work.
>
>Maybe, there is no entry for it's own ethernet address in arp table.
>
>Is there any other approach to get ethernet address??
>
>Thanks in advance.

Here is a short program which demonstrates how to get a variety of
information about a network interface, including the hardware address
of an ethernet card among other things.

  Floyd

/*
 * display info about network interfaces
 */
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/ioctl.h>
#include <net/if_arp.h>
#include <arpa/inet.h>

#define inaddrr(x) (*(struct in_addr *) &ifr->x[sizeof sa.sin_port])
#define IFRSIZE   ((int)(size * sizeof (struct ifreq)))

int main(void)
{
  unsigned char      *u;
  int                sockfd, size  = 1;
  struct ifreq       *ifr;
  struct ifconf      ifc;
  struct sockaddr_in sa;

  if (0 > (sockfd = socket(AF_INET, SOCK_DGRAM, IPPROTO_IP))) {
    fprintf(stderr, "Cannot open socket.\n");
    exit(EXIT_FAILURE);
  }

  ifc.ifc_len = IFRSIZE;
  ifc.ifc_req = NULL;

  do {
    ++size;
    /* realloc buffer size until no overflow occurs  */
    if (NULL == (ifc.ifc_req = realloc(ifc.ifc_req, IFRSIZE))) {
      fprintf(stderr, "Out of memory.\n");
      exit(EXIT_FAILURE);
    }
    ifc.ifc_len = IFRSIZE;
    if (ioctl(sockfd, SIOCGIFCONF, &ifc)) {
      perror("ioctl SIOCFIFCONF");
      exit(EXIT_FAILURE);
    }
  } while  (IFRSIZE <= ifc.ifc_len);

  ifr = ifc.ifc_req;
  for (;(char *) ifr < (char *) ifc.ifc_req + ifc.ifc_len; ++ifr) {

    if (ifr->ifr_addr.sa_data == (ifr+1)->ifr_addr.sa_data) {
      continue;  /* duplicate, skip it */
    }

    if (ioctl(sockfd, SIOCGIFFLAGS, ifr)) {
      continue;  /* failed to get flags, skip it */
    }

    printf("Interface:  %s\n", ifr->ifr_name);
    printf("IP Address: %s\n", inet_ntoa(inaddrr(ifr_addr.sa_data)));
    
    if (0 == ioctl(sockfd, SIOCGIFHWADDR, ifr)) {

      /* Select which  hardware types to process.
       *
       *    See list in system include file included from
       *    /usr/include/net/if_arp.h  (For example, on
       *    Linux see file /usr/include/linux/if_arp.h to
       *    get the list.)
       */
      switch (ifr->ifr_hwaddr.sa_family) {
      default:
	printf("\n");
	continue;
      case  ARPHRD_NETROM:  case  ARPHRD_ETHER:  case  ARPHRD_PPP:
      case  ARPHRD_EETHER:  case  ARPHRD_IEEE802: break;
      }

      u = (unsigned char *) &ifr->ifr_addr.sa_data;

      if (u[0] + u[1] + u[2] + u[3] + u[4] + u[5]) {
        printf("HW Address: %2.2x.%2.2x.%2.2x.%2.2x.%2.2x.%2.2x\n",
	     u[0], u[1], u[2], u[3], u[4], u[5]);
      }
    }

    if (0 == ioctl(sockfd, SIOCGIFNETMASK, ifr) &&
	strcmp("255.255.255.255", inet_ntoa(inaddrr(ifr_addr.sa_data)))) {
      printf("Netmask:    %s\n", inet_ntoa(inaddrr(ifr_addr.sa_data)));
    }

    if (ifr->ifr_flags & IFF_BROADCAST) {
      if (0 == ioctl(sockfd, SIOCGIFBRDADDR, ifr) &&
	  strcmp("0.0.0.0", inet_ntoa(inaddrr(ifr_addr.sa_data)))) {
        printf("Broadcast:  %s\n", inet_ntoa(inaddrr(ifr_addr.sa_data)));
      }
    }

    if (0 == ioctl(sockfd, SIOCGIFMTU, ifr)) {
      printf("MTU:        %u\n",  ifr->ifr_mtu);
    }

    if (0 == ioctl(sockfd, SIOCGIFMETRIC, ifr)) {
      printf("Metric:     %u\n",  ifr->ifr_metric);
    }
    printf("\n");
  }

  close(sockfd);
  return EXIT_SUCCESS;
}


-- 
Floyd L. Davidson                          floyd@barrow.com
Ukpeagvik (Barrow, Alaska)



^ permalink raw reply	[flat|nested] 5+ messages in thread

* [Q]How to get ethernet hardware(MAC) address?
@ 2000-11-24  5:00 Kim YuSung
  2000-11-23  5:00 ` Floyd Davidson
  2000-11-26 14:00 ` Paul Szabo
  0 siblings, 2 replies; 5+ messages in thread
From: Kim YuSung @ 2000-11-24  5:00 UTC (permalink / raw)


Hi, I'm want to get ethernet hardware address of my own network card in
program.

In the book "UNIX Network Programming", I founded an approach to get
ethernet address from arp table.

That approach do well at solaris 2.6, 7, HP-UX 11.00.

But on HP-UX 10.20, and digital-unix 4.0f, it doesn't work.

Maybe, there is no entry for it's own ethernet address in arp table.

Is there any other approach to get ethernet address??

Thanks in advance.





^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [Q]How to get ethernet hardware(MAC) address?
  2000-11-24  5:00 [Q]How to get ethernet hardware(MAC) address? Kim YuSung
  2000-11-23  5:00 ` Floyd Davidson
@ 2000-11-26 14:00 ` Paul Szabo
  2000-11-27  5:00   ` Larry Gensch
  1 sibling, 1 reply; 5+ messages in thread
From: Paul Szabo @ 2000-11-26 14:00 UTC (permalink / raw)
  To: Kim YuSung

Kim YuSung wrote:
> I want to get ethernet hardware address of my own network card ...

I often use

  /sbin/grep 'hardware address' /usr/adm/messages | \
    /usr/bin/tail -5 | \
    /usr/bin/sed -e 's/^.* vmunix: //' -e 's/^\(tu.:\) /\1      /' | \
    /sbin/sort -u

Hope this helps.

-- 
Paul Szabo - psz@maths.usyd.edu.au  http://www.maths.usyd.edu.au:8000/u/psz/
School of Mathematics and Statistics  University of Sydney   2006  Australia


^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [Q]How to get ethernet hardware(MAC) address?
  2000-11-26 14:00 ` Paul Szabo
@ 2000-11-27  5:00   ` Larry Gensch
  2000-11-27  5:00     ` Floyd Davidson
  0 siblings, 1 reply; 5+ messages in thread
From: Larry Gensch @ 2000-11-27  5:00 UTC (permalink / raw)


In article <3A211779.41C6@maths.usyd.edu.au>,
	Paul Szabo <psz@maths.usyd.edu.au> writes:
> Kim YuSung wrote:
>> I want to get ethernet hardware address of my own network card ...
> 
> I often use
> 
>   /sbin/grep 'hardware address' /usr/adm/messages | \
>     /usr/bin/tail -5 | \
>     /usr/bin/sed -e 's/^.* vmunix: //' -e 's/^\(tu.:\) /\1      /' | \
>     /sbin/sort -u
> 
> Hope this helps.

Nah.  Too much typing:

	$ netstat -i

-- 
Larry Gensch					   Larry.Gensch@compaq.com
Compaq Computer Corporation                        As usual, the standard
110 Spit Brook Road ZKO3-2/W17                     disclaimers apply, etc.
Nashua, NH 03062                                   Yada yada yada



^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [Q]How to get ethernet hardware(MAC) address?
  2000-11-27  5:00   ` Larry Gensch
@ 2000-11-27  5:00     ` Floyd Davidson
  0 siblings, 0 replies; 5+ messages in thread
From: Floyd Davidson @ 2000-11-27  5:00 UTC (permalink / raw)


larry.gensch@compaq.com (Larry Gensch) wrote:
>	Paul Szabo <psz@maths.usyd.edu.au> writes:
>> Kim YuSung wrote:
>>> I want to get ethernet hardware address of my own network card ...
>> 
>> I often use
>> 
>>   /sbin/grep 'hardware address' /usr/adm/messages | \
>>     /usr/bin/tail -5 | \
>>     /usr/bin/sed -e 's/^.* vmunix: //' -e 's/^\(tu.:\) /\1      /' | \
>>     /sbin/sort -u
>> 
>> Hope this helps.
>
>Nah.  Too much typing:
>
>	$ netstat -i

This is probably system specifc.  On Linux netstat does not display
the hardware address.  But ifconfig of course does...

-- 
Floyd L. Davidson                          floyd@barrow.com
Ukpeagvik (Barrow, Alaska)



^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2000-11-27  5:00 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2000-11-24  5:00 [Q]How to get ethernet hardware(MAC) address? Kim YuSung
2000-11-23  5:00 ` Floyd Davidson
2000-11-26 14:00 ` Paul Szabo
2000-11-27  5:00   ` Larry Gensch
2000-11-27  5:00     ` Floyd Davidson

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox