Skip site navigation (1)Skip section navigation (2)
Date:      Wed, 1 Aug 2001 11:51:52 +0400
From:      Dmitry Diky <diwil@eis.ru>
To:        hackers@freebsd.org
Subject:   Re: Finding MAC address of interface - programming question
Message-ID:  <01080111515200.13229@wild.eis.ru>
In-Reply-To: <3B67B425.40D6FE0E@webgiro.com>
References:  <36F7B20351634E4FBFFE6C6A216B30D54C80@ecx1.edifecs.com> <3B67B425.40D6FE0E@webgiro.com>

next in thread | previous in thread | raw e-mail | index | archive | help
the code as follows (at least it  works in my case) :

#include <sys/types.h>
#include <sys/sysctl.h>

#include <sys/param.h>
#include <sys/ioctl.h>
#include <sys/socket.h>

#include <net/if.h>
#include <net/if_var.h>
#include <net/if_dl.h>
#include <net/if_types.h>
#include <net/route.h>

/* IP */
#include <netinet/in.h>
#include <netinet/in_var.h>
#include <arpa/inet.h>
#include <netdb.h>

/* OSI */

#include <ctype.h>
#include <err.h>
#include <errno.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

#include <sys/time.h>
#include <net/bpf.h>

#define NO_OF_IFS=092
char mac[NO_OF_IFS][6];


/* sdlr must be provided */
/* MAC will be filled */
/* number of IFs returned */
int get_ifs(struct sockaddr_dl *sdlr)
{
  size_t=09=09needed;

 /* prepare params for sysctl */
  int=09=09=09mib[6] =3D { CTL_NET, PF_ROUTE, 0, 0, NET_RT_IFLIST, 0 };

  char=09 =09=09*buf, *lim, *next;
  struct if_msghdr=09*ifm, *nextifm;
  struct ifa_msghdr=09*ifam;
  struct sockaddr_dl=09*sdl;
  int =09=09=09flags, addrcount=3D0, etheraddrcount=3D0;
  char=09=09=09name[16];       /* cannot be more */
  char=09=09=09*cp;
  int=09=09=09n;

  if (sysctl(mib, 6, NULL, &needed, NULL, 0) < 0)
    perror("iflist-sysctl-estimate");

  if ((buf =3D malloc(needed)) =3D=3D NULL) perror("malloc");

  if (sysctl(mib, 6, buf, &needed, NULL, 0) < 0)
    perror("actual retrieval of interface table");

  lim =3D buf + needed;
  next =3D buf;

  while (next < lim)
  {
    ifm =3D (struct if_msghdr *)next;

    if (ifm->ifm_type =3D=3D RTM_IFINFO)
    {
      sdl =3D (struct sockaddr_dl *)(ifm + 1);
      flags =3D ifm->ifm_flags;
    }
    else
    {
      exit (1);
    }

    next +=3D ifm->ifm_msglen;
    ifam =3D NULL;
    addrcount =3D 0;
    while (next < lim)
    {
      nextifm =3D (struct if_msghdr *)next;

      if (nextifm->ifm_type !=3D RTM_NEWADDR) break;

      if (ifam =3D=3D NULL) ifam =3D (struct ifa_msghdr *)nextifm;

      addrcount++;
      next +=3D nextifm->ifm_msglen;
    }

   /*
    *  Ok,  the interface is found
    *  Save it if it is an ethernet.
    */
    if(sdl->sdl_type =3D=3D IFT_ETHER)
    {
      memcpy((void *)&sdlr[etheraddrcount],sdl, sizeof(struct sockaddr_dl=
));
      etheraddrcount++;

      strncpy(name, sdl->sdl_data, sdl->sdl_nlen);
      name[sdl->sdl_nlen] =3D '\0';

      cp =3D (char *)LLADDR(sdl);
      if ((n =3D sdl->sdl_alen) > 0)
      {
      /*
        if (sdl->sdl_type =3D=3D IFT_ETHER)
          errMsg("\tEthernet %s:\tMAC - %s\n", name, macPrt(cp));
        else
          errMsg("\tlladdr %s:\tMAC - %s\n", name, macPrt(cp));
       */
        memcpy(mac[etheraddrcount-1],cp,6);
      }
    }

  }
  if( etheraddrcount =3D=3D 0 ) printf("\tNone");
  free(buf);

  return etheraddrcount;
}
=08=A0=864=08



On Wednesday 01 August 2001 11:47, Andrzej Bialecki wrote:
> Michael VanLoon wrote:
> > > From: Chris Faulhaber [mailto:jedgar@fxp.org]
> > > Sent: Tuesday, July 31, 2001 3:56 PM
> > >
> > > On Tue, Jul 31, 2001 at 03:56:40PM -0700, Michael VanLoon wrote:
> > > > Please point me to a more appropriate forum if there is
> > >
> > > one.  I'm kinda out
> > >
> > > > of my depth on this question.  Pseudo code is fine. :-)
> > > >
> > > > What I'm looking for is how to enumerate the network
> > >
> > > interfaces and get the
> > >
> > > > Ethernet MAC address of one programmatically.  Can anyone
> > >
> > > point me in the
> > >
> > > > right direction?
> > >
> > > /usr/src/sbin/ifconfig
> >
> > Thanks, I've already been looking at that. :-)
> >
> > The problem is, ifconfig does a lot of other stuff, and it's very poo=
rly
> > documented.
> >
> > I was hoping someone could give me some hints to speed my research.  =
Such
> > as recommended places to look in the code, ioctl's used and/or sysctl=
's
> > called.
> >
> > I'm happy to do most of the work myself, I'm just asking for some hin=
ts.
> > :-) Much appreciated.
>
> Take a look at /usr/src/release/picobsd/tinyware/ns. I went through the
> same hoops some time ago... :-)

--=20
*********************************************************************
   ("`-''-/").___..--''"`-._     (\       Dimmy the Wild      UA1ACZ
    `6_ 6  )   `-.  (     ).`-.__.`)      Enterprise Information Sys=20
    (_Y_.)'  ._   )  `._ `. ``-..-'       Nevsky prospekt,   20 / 44
  _..`--'_..-_/  /--'_.' ,'               Saint Petersburg,   Russia
 (il),-''  (li),'  ((!.-'                 +7 (812) 3148860,  5585314
*********************************************************************

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?01080111515200.13229>