home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / unix / volume20 / etherlib / part01 / src / nitaddr.c < prev    next >
Encoding:
C/C++ Source or Header  |  1989-10-24  |  1.3 KB  |  53 lines

  1. /* $Id: nitaddr.c,v 2.1 89/10/23 15:43:02 dupuy Exp $ */
  2.  
  3. #include <sys/types.h>            /* ifreq (u_short) */
  4. #include <sys/socket.h>            /* ifreq (sockaddr) */
  5. #include <net/if.h>            /* ifreq */
  6. #include <sys/ioctl.h>            /* SIOCGIFADDR */
  7.  
  8. #include "libether.h"
  9.  
  10. ether_addr _ether_localaddr;        /* shared with ether_intfaddr() */
  11. int _ether_haveaddress;            /* shared with ether_intfaddr() */
  12.  
  13. /*
  14.  * Returns the local ethernet address for the ethernet interface on the file
  15.  * descriptor fd.  The result is stored in the structure given by address; if
  16.  * none is given, malloc is used to allocate space.  Sun systems have the
  17.  * "feature" that all ethernet interfaces are set to the same address (taken
  18.  * from the ID PROM).  So we can get the answer once, and use it forever.
  19.  */
  20.  
  21. ether_addr *
  22. ether_address (fd, address)
  23. int fd;
  24. ether_addr *address;
  25. {
  26.     if (!_ether_haveaddress)
  27.     {
  28.     struct ifreq ifr;
  29.  
  30.     ifr.ifr_addr.sa_family = AF_NIT;
  31.     if (ioctl (fd, SIOCGIFADDR, (char *) &ifr) < 0)
  32.     {
  33. #ifdef DEBUG
  34.         perror ("ether_address: ioctl SIOCGIFADDR");
  35. #endif
  36.         return (0);
  37.     }
  38.  
  39.     bcopy (ifr.ifr_addr.sa_data, (char *) &_ether_localaddr,
  40.            sizeof (ether_addr));
  41.  
  42.     _ether_haveaddress = 1;
  43.     }
  44.  
  45.     if (address == 0)
  46.     address = (ether_addr *) malloc (sizeof (ether_addr));
  47.  
  48.     if (address != 0)
  49.     *address = _ether_localaddr;
  50.  
  51.     return (address);
  52. }
  53.