From mboxrd@z Thu Jan 1 00:00:00 1970 X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: fee84,a684a3049b399a41 X-Google-Attributes: gidfee84,public From: Joe Spanicek Subject: Re: Getting Ethernet (MAC) address on DU 4.0 Date: 1998/09/08 Message-ID: <35F488F9.486107CD@resptk.bhp.com.au> X-Deja-AN: 388880723 References: <35F3AE4E.413E4BF3@nospam.sebb.bel.alcatel.be> To: markwicn@sebb.bel.alcatel.be Content-Type: multipart/mixed; boundary="------------F5EA1B1C3F3A45A95BACF602" Organization: BHP Research Mime-Version: 1.0 Newsgroups: comp.unix.osf.osf1 This is a multi-part message in MIME format. --------------F5EA1B1C3F3A45A95BACF602 Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit Niel Markwick wrote: > > I know this is a question that has been asked before, but I am looking > for some different answers! > > I am looking for a generic way of getting the MAC address from the > ethernet card from within a C program... > > On the VMS and Solaris platforms, I am sucessfully using the SIOCGARP > ioctl to get ther ARP entry for the host, but on DEC Unix, this fails... > It seems that VMS and SOLARIS store an entry in the ARP tables for the > local host, but Dec Unix doesn't. > > The answer found in Deja News > (http://www.dejanews.com/getdoc.xp?AN=333909507) > was to use the SIOCRPHYSADDR ioctl, but this requires knowledge of the > name of the network interface that you are interrogating (ie tu#, ln#, > le#) > > So, is there another way to get the MAC address (I suspect not). > Alternatively, is there a way to get the names of the network interfaces > on a system, so that I can then use these with the SIOCRPHYSADDR ioctl > to get the addresses. > > I currently have a workaround which is to popen the command: > /usr/sbin/netstat -i \ > | awk ' {print $4} ' \ > | grep '[0-9a-f:]\{17\}' > and read the output back into my program, but this feels sooooo messy, > and is subject to the whims of Digital changing the output format of > netstat! > > All help gratefully recieved. > > Niel. > > -- > Niel Markwick > Consultant Software Engineer > Alcatel Bell Telecom, Kontich, Belgium. > Work Email: markwicn@sebb.bel.alcatel.be Hi, I found this bit of C code on the Digital Web Site. Hope it helps. Joe PS: The code is an attachment --------------F5EA1B1C3F3A45A95BACF602 Content-Type: text/plain; charset=us-ascii; name="ether_minv2.0.c" Content-Transfer-Encoding: 7bit Content-Disposition: inline; filename="ether_minv2.0.c" /* * getadr.c -- program to acquire ethernet controller addresses * (c) Digital Equipment Corporation, 1989 * Author: Jim Lieb, Digital Equipment Corp. suntan::lieb * This program is distributed free as a demonstration * of network access methods. No warrantee is expressed or * implied. * * Application: Use the getadr function to get the hardware address * for the system in applications where the license key is * verified by checking something unique like a system serial * number (not always present on DEC cpus). * * NOTE: There is a difference between the hardware address and the * physical address of Digital manufactured Ethernet controller. * The hardware address is a unique number, typically of the * form: 08-00-2B-xx-xx-xx, as assigned by IEEE authority. The * xx-xx-xx number is fixed in ROM and is unique for each * controller. This address is guaranteed fixed and unique. * The physical address is programmable and is MODIFIED by the * DECnet routing layer to encode the 16 bit Phase IV DECnet * address in the least significant 2 octets (byte swapped) * of the source address of packets. This address, typically * of the form aa-00-04-00-yy-yy where yy-yy is the DECnet * address, becomes the address used by the controller for * packet receive and transmit. * * March 3, 1990 - Jim Lieb, changed to use internal tables * and clean up struct usage. Please note! * May 21, 1990 - John Dustin OSG, use status bits rather than * string compares to get right interface. * also test for interface turned on and use * best guess of none on... * September 28, 1993 - Doug Olson, updated to match change in * /usr/include/net/if.h (IFF_MULTICAST). * 3 November 1994 Chuck Newman. getadr was changed to return the * addresses of all ethernet devices. The * interface was completely changed. */ #include #include #include #include #include #include /* error numbers */ #include static char getadrSID[] = "@(#)getadr.c 1.5 6/22/90 demo"; extern int errno; int getadr(); /* main -- Shell to demonstrate the call to the getadr function. This * also prints the address contents to stdout. */ main(argc, argv, envp) int argc; char **argv, **envp; { char *interface; #define INTERFACE_COUNT 32 unsigned char hardware[INTERFACE_COUNT][6]; int temp_int, count; if(argc < 2) interface = NULL; else interface = argv[1]; if ((count = getadr(hardware, INTERFACE_COUNT)) == 0) { fprintf(stderr, "couldn't get network interface\n"); exit(1); } for (temp_int = 0; temp_int < abs(count); temp_int++) { printf("Ethernet hardware address = (%02.2X-%02.2X-%02.2X-%02.2X-%02.2X-%02.2X)\n", hardware[temp_int][0], hardware[temp_int][1], hardware[temp_int][2], hardware[temp_int][3], hardware[temp_int][4], hardware[temp_int][5]); } if (count < 0) printf("There are more devices than those listed\nCall getadr with a larger buffer\n"); } /* getadr -- get primary ethernet interface address * read list of net devices via ioctl to ifreqs. Pass over * loopback device and any serial line interfaces. Return * all devices. * * This function does not require setuid(2) priviledges. */ #if 0 This routine accepts two parameters, and returns the count of addresses found Input 1: Area to receive ethernet addresses Input 2: Number of addresses that can fit into the area specified by Input 1. Each address takes 6 bytes. Return Value: Positive -- number of addresses returned Negative -- absolute value is number of addresses returned, but there are more addresses Zero -- no addresses, or failure #endif #define IFREQCNT 64 /* need enough room for total number of possible * interfaces, including aliases for LAT and DECnet */ int getadr(unsigned char hardware[], int count) { int s; struct ifreq ifreqs[IFREQCNT]; struct ifconf ifc; struct ifdevea ifrp; int index = 0; int ndx, ifr; /* JOE - added this for loop - insure picked up errors */ for(s=0; s= 0) { if (index==0) { if (index == count) return -index; hardware[0+index*6] = ifrp.default_pa[0]; hardware[1+index*6] = ifrp.default_pa[1]; hardware[2+index*6] = ifrp.default_pa[2]; hardware[3+index*6] = ifrp.default_pa[3]; hardware[4+index*6] = ifrp.default_pa[4]; hardware[5+index*6] = ifrp.default_pa[5]; index+=1; } else { for (ndx=0; ; ndx++) { if (ndx == count) return -index; if (ndx == index) { hardware[0+index*6] = ifrp.default_pa[0]; hardware[1+index*6] = ifrp.default_pa[1]; hardware[2+index*6] = ifrp.default_pa[2]; hardware[3+index*6] = ifrp.default_pa[3]; hardware[4+index*6] = ifrp.default_pa[4]; hardware[5+index*6] = ifrp.default_pa[5]; index+=1; break; } if ((ifrp.default_pa[5]==hardware[ndx*6+5])&& (ifrp.default_pa[4]==hardware[ndx*6+4])&& (ifrp.default_pa[3]==hardware[ndx*6+3])&& (ifrp.default_pa[2]==hardware[ndx*6+2])&& (ifrp.default_pa[1]==hardware[ndx*6+1])&& (ifrp.default_pa[0]==hardware[ndx*6+0])) { break; } } } } } return index; } --------------F5EA1B1C3F3A45A95BACF602--