From owner-freebsd-hackers Tue Jul 31 20:49:15 2001 Delivered-To: freebsd-hackers@freebsd.org Received: from InterJet.elischer.org (c421509-a.pinol1.sfba.home.com [24.7.86.9]) by hub.freebsd.org (Postfix) with ESMTP id 9EB7C37B409 for ; Tue, 31 Jul 2001 20:49:04 -0700 (PDT) (envelope-from julian@elischer.org) Received: from elischer.org (InterJet.elischer.org [192.168.1.1]) by InterJet.elischer.org (8.9.1a/8.9.1) with ESMTP id WAA37546; Tue, 31 Jul 2001 22:44:41 -0700 (PDT) Message-ID: <3B67779B.3DAEE6B5@elischer.org> Date: Tue, 31 Jul 2001 20:29:31 -0700 From: Julian Elischer X-Mailer: Mozilla 4.7 [en] (X11; U; FreeBSD 5.0-CURRENT i386) X-Accept-Language: en, hu MIME-Version: 1.0 To: Michael VanLoon Cc: "'hackers@freebsd.org'" Subject: Re: Finding MAC address of interface - programming question References: <36F7B20351634E4FBFFE6C6A216B30D54C7F@ecx1.edifecs.com> Content-Type: multipart/mixed; boundary="------------885B4547E9E7435AD8E18A16" 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 This is a multi-part message in MIME format. --------------885B4547E9E7435AD8E18A16 Content-Type: text/plain; charset=iso-8859-2 Content-Transfer-Encoding: 7bit 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? > > Thanks much! here's a little routine that probably does what you want: > > To Unsubscribe: send mail to majordomo@FreeBSD.org > with "unsubscribe freebsd-hackers" in the body of the message -- +------------------------------------+ ______ _ __ | __--_|\ Julian Elischer | \ U \/ / hard at work in | / \ julian@elischer.org +------>x USA \ a very strange | ( OZ ) \___ ___ | country ! +- X_.---._/ presently in San Francisco \_/ \\ v --------------885B4547E9E7435AD8E18A16 Content-Type: text/plain; charset=iso-8859-2; name="dumpifs.c" Content-Transfer-Encoding: 7bit Content-Disposition: inline; filename="dumpifs.c" #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include void showbytes(unsigned char *cptr,int len) { int i,j; for (j = 0 ;len > 0 ;j++) { for ( i = 0; i < 16; i++) { printf("0x%x ",*cptr++); if (--len == 0) break; } printf("\n"); } } void print_sa (struct sockaddr *sap) { char buf[128]; printf("len=%d,",sap->sa_len); switch(sap->sa_family) { case AF_LINK: printf(" family=LINK\n"); { char *cptr; struct sockaddr_dl *sdl = (struct sockaddr_dl *)sap; cptr = sdl->sdl_data; printf("index=%u, type=%u ", sdl->sdl_index, sdl->sdl_type); printf("nlen=%u, alen=%u, slen=%u\n", sdl->sdl_nlen, sdl->sdl_alen, sdl->sdl_slen); if(sdl->sdl_nlen) { bcopy(cptr,buf,sdl->sdl_nlen); buf[sdl->sdl_nlen] = 0; printf(" name = %s\n",buf); cptr += sdl->sdl_nlen; } if(sdl->sdl_alen) { printf(" address = "); showbytes(cptr,sdl->sdl_alen); cptr += sdl->sdl_alen; } if(sdl->sdl_slen) { printf(" selector = "); showbytes(cptr,sdl->sdl_slen); cptr += sdl->sdl_slen; } } break; case AF_INET: printf(" family=INET\n"); printf("[%s]", inet_ntoa(((struct sockaddr_in *)sap)->sin_addr)); printf(".%hu\n",((struct sockaddr_in *)sap)->sin_port); break; default: printf(" family=%d\n",sap->sa_family); showbytes(sap->sa_data,sap->sa_len-2); } } /* * Get the configuration from the kernel. */ void getifconf( void ) { struct ifconf ifc; struct ifreq ifrs[ 64 ], *ifr, *nextifr; struct interface *iface, *niface; int s; struct sockaddr *sa_p; int ifrsize = 0; u_char proto; bzero(&ifc,sizeof(struct ifconf)); bzero(ifrs,sizeof(struct ifreq) * 64); if (( s = socket( AF_INET, SOCK_DGRAM, 0 )) < 0 ) { perror( "socket" ); exit( 1 ); } ifc.ifc_len = sizeof( ifrs ); ifc.ifc_buf = (caddr_t)ifrs; if ( ioctl( s, SIOCGIFCONF, &ifc ) < 0 ) { perror( "getifconf" ); exit( 1 ); } for ( ifr = ifc.ifc_req; ifc.ifc_len >= sizeof( struct ifreq ); ifr = nextifr, ifc.ifc_len -= ifrsize) { /* * in BSD4.4, this returns an entry for every address * Associated with the if. including physical.. they * include a sockaddr which is VARIABLE LENGTH! * * Calculate the length of this entry. */ sa_p = &(ifr->ifr_addr); print_sa(&(ifr->ifr_addr)); /*print_sa(&(ifr->ifr_dstaddr)); print_sa(&(ifr->ifr_broadaddr));*/ ifrsize = IFNAMSIZ + sa_p->sa_len; nextifr = (struct ifreq *)((caddr_t)ifr + ifrsize); proto = ifr->ifr_addr.sa_family; /* * Now get it's Netmask */ if ( ioctl( s, SIOCGIFNETMASK, ifr ) < 0 ) { fprintf( stderr, "netmask not available from SIOCGIFNETMASK\n" ); } else { if(proto == AF_INET) ifr->ifr_addr.sa_family = proto; printf("netmask: "); print_sa(&(ifr->ifr_addr)); } /* * Now get it's flags */ if ( ioctl( s, SIOCGIFFLAGS, ifr ) < 0 ) { perror( ifr->ifr_name ); } printf("FLAGS = 0x%x\n",(unsigned short)ifr->ifr_flags); printf("\n"); } if ( ifc.ifc_len != 0 ) { fprintf( stderr, "Funky gifconf return.\n" ); exit( 1 ); } (void)close( s ); return; } main() { getifconf(); exit(0); } --------------885B4547E9E7435AD8E18A16-- To Unsubscribe: send mail to majordomo@FreeBSD.org with "unsubscribe freebsd-hackers" in the body of the message