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 2
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 needed;
/* prepare params for sysctl */
int mib[6] = { CTL_NET, PF_ROUTE, 0, 0, NET_RT_IFLIST, 0 };
char *buf, *lim, *next;
struct if_msghdr *ifm, *nextifm;
struct ifa_msghdr *ifam;
struct sockaddr_dl *sdl;
int flags, addrcount=0, etheraddrcount=0;
char name[16]; /* cannot be more */
char *cp;
int n;
if (sysctl(mib, 6, NULL, &needed, NULL, 0) < 0)
perror("iflist-sysctl-estimate");
if ((buf = malloc(needed)) == NULL) perror("malloc");
if (sysctl(mib, 6, buf, &needed, NULL, 0) < 0)
perror("actual retrieval of interface table");
lim = buf + needed;
next = buf;
while (next < lim)
{
ifm = (struct if_msghdr *)next;
if (ifm->ifm_type == RTM_IFINFO)
{
sdl = (struct sockaddr_dl *)(ifm + 1);
flags = ifm->ifm_flags;
}
else
{
exit (1);
}
next += ifm->ifm_msglen;
ifam = NULL;
addrcount = 0;
while (next < lim)
{
nextifm = (struct if_msghdr *)next;
if (nextifm->ifm_type != RTM_NEWADDR) break;
if (ifam == NULL) ifam = (struct ifa_msghdr *)nextifm;
addrcount++;
next += nextifm->ifm_msglen;
}
/*
* Ok, the interface is found
* Save it if it is an ethernet.
*/
if(sdl->sdl_type == IFT_ETHER)
{
memcpy((void *)&sdlr[etheraddrcount],sdl, sizeof(struct sockaddr_dl));
etheraddrcount++;
strncpy(name, sdl->sdl_data, sdl->sdl_nlen);
name[sdl->sdl_nlen] = '\0';
cp = (char *)LLADDR(sdl);
if ((n = sdl->sdl_alen) > 0)
{
/*
if (sdl->sdl_type == 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 == 0 ) printf("\tNone");
free(buf);
return etheraddrcount;
}
†4
On Wednesday 01 August 2001 11:47, Andrzej Bialecki wrote:
> Michael VanLoon wrote:
> > > From: Chris Faulhaber [mailto:[EMAIL PROTECTED]]
> > > 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 poorly
> > 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 hints.
> > :-) Much appreciated.
>
> Take a look at /usr/src/release/picobsd/tinyware/ns. I went through the
> same hoops some time ago... :-)
--
*********************************************************************
("`-''-/").___..--''"`-._ (\ Dimmy the Wild UA1ACZ
`6_ 6 ) `-. ( ).`-.__.`) Enterprise Information Sys
(_Y_.)' ._ ) `._ `. ``-..-' Nevsky prospekt, 20 / 44
_..`--'_..-_/ /--'_.' ,' Saint Petersburg, Russia
(il),-'' (li),' ((!.-' +7 (812) 3148860, 5585314
*********************************************************************
To Unsubscribe: send mail to [EMAIL PROTECTED]
with "unsubscribe freebsd-hackers" in the body of the message