home *** CD-ROM | disk | FTP | other *** search
/ Source Code 1992 March / Source_Code_CD-ROM_Walnut_Creek_March_1992.iso / usenet / altsrcs / 1 / 1812 < prev    next >
Encoding:
Text File  |  1990-12-28  |  3.0 KB  |  94 lines

  1. Newsgroups: alt.sources
  2. From: thomas@mipsbx.nac.dec.com (Matt Thomas)
  3. Subject: [ultrix] Re: Reading the HARDWARE ethernet address from a program?
  4. Message-ID: <1990Sep12.001506.22805@math.lsa.umich.edu>
  5. Date: Wed, 12 Sep 90 00:15:06 GMT
  6.  
  7. Archive-name: ultrix-geteaddr/04-Sep-90
  8. Original-posting-by: thomas@mipsbx.nac.dec.com (Matt Thomas)
  9. Original-subject: Re: Reading the HARDWARE ethernet address from a program?
  10. Reposted-by: emv@math.lsa.umich.edu (Edward Vielmetti)
  11.  
  12. [Reposted from comp.unix.ultrix.
  13. Comments on this service to emv@math.lsa.umich.edu (Edward Vielmetti).]
  14.  
  15. In article <19290@dime.cs.umass.edu> williams@cs.umass.edu writes:
  16. >We have a need to obtain the hardware ethernet address within a program.
  17. >We can obtain this address on a Sun using an ioctl call on /dev/nit
  18. >([sun-spots program] getaddr.c ) Does anyone know how one could obtain the
  19. >physical ethernet address in ultrix, in particular for the dec5000, MIPS
  20. >based workstation?
  21. >
  22. >If this is not of general interest, you can email to ralf@aai.com.  I will
  23. >also check for followups.
  24. >
  25. >Thanks for any help!
  26. >
  27. >Leo    leo@aai.com   leo%aai@uunet.uu.net   ...uunet!aai!leo
  28.  
  29. #include <stdio.h>            /* standard I/O */
  30. #include <errno.h>            /* error numbers */
  31. #include <time.h>            /* time definitions */
  32. #include <sys/types.h>            /* system types */
  33. #include <sys/socket.h>            /* socket stuff */
  34. #include <sys/ioctl.h>            /* ioctls */
  35. #include <net/if.h>            /* generic interface structs */
  36.  
  37. extern    char    *optarg;
  38. extern    int    optind;
  39. extern    char    *sys_errlist[];
  40.  
  41. main( argc, argv )
  42.     int argc;
  43.     char *argv[];
  44. {
  45.     struct ifdevea devea;
  46.     struct ifreq *ifr, ifreqs[32];
  47.     struct ifconf ifc;
  48.     int s, i;
  49.  
  50.     bzero(&devea, sizeof(devea));
  51.  
  52.     /*  we need a socket -- any old socket will do.  */
  53.     s = socket(AF_UNIX, SOCK_DGRAM, 0);
  54.     if (s < 0) {
  55.     perror("socket");
  56.     exit(1);
  57.     }
  58.  
  59.     ifc.ifc_req = ifreqs;
  60.     ifc.ifc_len = sizeof(ifreqs);
  61.     if (ioctl(s, SIOCGIFCONF, &ifc) < 0) {
  62.     perror("siocgifconf");
  63.     exit(1);
  64.     }
  65.  
  66.     for (ifr = ifreqs; ifc.ifc_len > 0; ifr++, ifc.ifc_len -= sizeof(*ifr)) {
  67.     if (strcmp(devea.ifr_name, ifr->ifr_name)) {
  68.         (void) strcpy(devea.ifr_name, ifr->ifr_name);
  69.         /* read the address of the interface */
  70.         if (ioctl(s, SIOCRPHYSADDR, &devea) < 0)  {
  71.         /* error? */
  72.         if  (errno != EOPNOTSUPP && errno |= EINVAL) {
  73.             /* unexpected error */
  74.             perror(devea.ifr_name);
  75.         }
  76.         continue;
  77.         }
  78.         printf("%s: ", devea.ifr_name);
  79.         printf("current = %02x-%02x-%02x-%02x-%02x-%02x, ",
  80.         devea.current_pa[0], devea.current_pa[1], devea.current_pa[2], 
  81.         devea.current_pa[3], devea.current_pa[4], devea.current_pa[5]
  82.         );
  83.         printf("default = %02x-%02x-%02x-%02x-%02x-%02x\n",
  84.         devea.default_pa[0], devea.default_pa[1], devea.default_pa[2], 
  85.         devea.default_pa[3], devea.default_pa[4], devea.default_pa[5]
  86.         );
  87.     }
  88.     }
  89. }
  90. Matt Thomas                     Internet:   thomas@wrl.dec.com
  91. DECnet-ULTRIX Development       UUCP:       ...!decwrl!thomas
  92. Digital Equipment Corporation   Disclaimer: This message reflects my own
  93. Littleton, MA                               warped views, etc.
  94.