From owner-freebsd-hackers Wed Aug 1 0:59:43 2001 Delivered-To: freebsd-hackers@freebsd.org Received: from cyclone.eis.ru (for.spb.ru [195.201.69.16]) by hub.freebsd.org (Postfix) with SMTP id DA22637B406 for ; Wed, 1 Aug 2001 00:59:34 -0700 (PDT) (envelope-from diwil@eis.ru) Received: (qmail 31066 invoked from network); 1 Aug 2001 08:07:12 -0000 Received: from good.for.spb.ru (HELO wild.eis.ru) (195.201.69.80) by for.spb.ru with SMTP; 1 Aug 2001 08:07:12 -0000 Content-Type: text/plain; charset="koi8-r" From: Dmitry Diky Reply-To: diwil@eis.ru To: hackers@freebsd.org Subject: Re: Finding MAC address of interface - programming question Date: Wed, 1 Aug 2001 11:51:52 +0400 X-Mailer: KMail [version 1.2] References: <36F7B20351634E4FBFFE6C6A216B30D54C80@ecx1.edifecs.com> <3B67B425.40D6FE0E@webgiro.com> In-Reply-To: <3B67B425.40D6FE0E@webgiro.com> MIME-Version: 1.0 Message-Id: <01080111515200.13229@wild.eis.ru> Content-Transfer-Encoding: quoted-printable Sender: owner-freebsd-hackers@FreeBSD.ORG Precedence: bulk List-ID: List-Archive: (Web Archive) List-Help: (List Instructions) List-Subscribe: List-Unsubscribe: X-Loop: FreeBSD.ORG the code as follows (at least it works in my case) : #include #include #include #include #include #include #include #include #include #include /* IP */ #include #include #include #include /* OSI */ #include #include #include #include #include #include #include #include #include #include #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