Skip site navigation (1)Skip section navigation (2)
Date:      01 Aug 2001 04:50:11 -0500
From:      Hal Snyder <hal@vailsys.com>
To:        <hackers@freebsd.org>
Subject:   Re: Finding MAC address of interface - programming question
Message-ID:  <873d7c9kj0.fsf@gamera.vail>
In-Reply-To: <36F7B20351634E4FBFFE6C6A216B30D54C81@ecx1.edifecs.com> (Michael VanLoon's message of "Tue, 31 Jul 2001 16:34:17 -0700")
References:  <36F7B20351634E4FBFFE6C6A216B30D54C81@ecx1.edifecs.com>

next in thread | previous in thread | raw e-mail | index | archive | help
Michael VanLoon <MichaelV@EDIFECS.COM> writes:

> Thanks that's just exactly the information I was looking for. :-)
> 
> I'm slow grunging through the code and man pages that take this apart.
> 
> As far as UNPv1 I assume you're referring to Stevens' "Unix Network
> Programming"? If I'm not mistaken he doesn't go much below the
> socket level. A quick glance through didn't reveal anything of this
> nature.

See UNIX Network Programming 2ed, Vol.1, pp434-435 for SIOCGIFCONF
explanation. But nowadays getifaddrs(3) is easier:

/* hacked from netbsd ifconfig.c */

#include <sys/types.h>
#include <sys/socket.h>
#include <ifaddrs.h>
#include <stdio.h>
#include <net/if_dl.h>

int main (void) {

  struct ifaddrs *ifap, *ifaphead;
  int rtnerr;
  const struct sockaddr_dl *sdl;
  caddr_t ap;
  int alen;

  rtnerr = getifaddrs(&ifaphead);
  if (rtnerr) {
    perror(NULL);
    return 1;
  }

  for (ifap = ifaphead; ifap; ifap = ifap->ifa_next) {

    if (ifap->ifa_addr->sa_family == AF_LINK) {
      sdl = (const struct sockaddr_dl *) ifap->ifa_addr;
      ap = ((caddr_t)((sdl)->sdl_data + (sdl)->sdl_nlen));
      alen = sdl->sdl_alen;
      if (ap && alen > 0) {
        int i;

        printf ("%s:", ifap->ifa_name);
        for (i = 0; i < alen; i++, ap++)
          printf("%c%02x", i > 0 ? ':' : ' ', 0xff&*ap);
        putchar('\n');
      }
    }
  }
  putchar('\n');

  freeifaddrs(ifaphead);
  return 0;
}


To Unsubscribe: send mail to majordomo@FreeBSD.org
with "unsubscribe freebsd-hackers" in the body of the message




Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?873d7c9kj0.fsf>