From mboxrd@z Thu Jan 1 00:00:00 1970 X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 1164f8,2db621ecef37f432 X-Google-Attributes: gid1164f8,public X-Google-ArrivalTime: 1992-08-05 07:05:02 PST Newsgroups: comp.sys.dec Path: sparky!uunet!decwrl!deccrl!news.crl.dec.com!rdg.dec.com!uvo.dec.com!brsux0.bro.dec.com!delamper From: delamper@brsux0.bro.dec.com (Delamper Lukas) Subject: Re: reading E-net address on DecStation 3100 Message-ID: <1992Aug5.102810.18707@decuk.uvo.dec.com> Sender: usenet@decuk.uvo.dec.com (News Account) Reply-To: delamper@brsux0.bro.dec.com (Delamper Lukas) Organization: Digital Equipment Corporation References: <1992Jul26.015003.1@matrix.cs.wright.edu> <1992Jul26.211939.25698@prl.dec.com> <1397@sousa.ltn.dec.com> Date: Wed, 5 Aug 1992 10:28:10 GMT * * 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 demostration * 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. */ #include #include #include #include #include #include static char getadrSID[] = "@(#)getadr.c 1.3 8/17/89 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; { struct ifreq ifr; char *interface; if(argc < 2) interface = NULL; else interface = argv[1]; if(getadr(interface, &ifr) < 0) { fprintf(stderr, "getadr failed, exiting\n"); exit(1); } printf("ifr_name = '%s'\n", ifr.ifr_name); printf("family = %04x\n", 0xffff & ifr.ifr_addr.sa_family); printf( "Ethernet hardware address = (%02x-%02x-%02x-%02x-%02x-%02x)\n", 0xff & ifr.ifr_addr.sa_family, 0xff & (ifr.ifr_addr.sa_family >> 8), 0xff & ifr.ifr_addr.sa_data[0], 0xff & ifr.ifr_addr.sa_data[1], 0xff & ifr.ifr_addr.sa_data[2], 0xff & ifr.ifr_addr.sa_data[3]); printf( "Ethernet physical address = (%02x-%02x-%02x-%02x-%02x-%02x)\n", 0xff & ifr.ifr_addr.sa_data[4], 0xff & ifr.ifr_addr.sa_data[5], 0xff & ifr.ifr_addr.sa_data[6], 0xff & ifr.ifr_addr.sa_data[7], 0xff & ifr.ifr_addr.sa_data[8], 0xff & ifr.ifr_addr.sa_data[9]); } /* getadr -- get primary ethernet interface address * read /etc/rc.local to find primary interface and query that * device. This function does not require setuid(2) priviledges. * NOTE: Assumes standard Ultrix rc.local as set up by * netsetup(8). Will break if rc.local was hacked... */ /* offsets for tokens on rc.local line */ #define IFCONFIG 0 #define INTERFACE 1 #define HOSTNAME 2 #define BROADCAST 3 #define NUMTOKENS 4 int getadr(name, ifrp) char *name; struct ifreq *ifrp; { int stat, i, s; char line[256]; FILE *rcf; char *tokens[NUMTOKENS]; char *cp; if((s = socket(AF_INET, SOCK_DGRAM, 0)) == -1) { perror("getadr, socket failed"); return( -1); } if(name != NULL) strcpy(ifrp->ifr_name, name); else { if((rcf = fopen("/etc/rc.local", "r")) == NULL) { perror("getadr, open failed on rc.local"); return -1; } while((cp = fgets(line, 256, rcf)) != NULL) { for(i = 0; i < NUMTOKENS; i++) { tokens[i] = cp; while(*cp != NULL && *cp != ' ' && *cp != '\t') cp++; if(*cp == NULL) continue; *cp++ = NULL; while(*cp == ' ' || *cp == '\t') cp++; } if(strcmp(tokens[IFCONFIG], "/etc/ifconfig") == 0 && strcmp(tokens[HOSTNAME], "`/bin/hostname`") == 0 && strcmp(tokens[BROADCAST], "broadcast") == 0) break; } fclose(rcf); strcpy(ifrp->ifr_name, tokens[INTERFACE]); } if((stat = ioctl(s, SIOCRPHYSADDR, ifrp)) == -1) { perror("getadr, ioctl failed"); return -1; } return 0; }