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

  1. /* $Id: nit4intaddr.c,v 2.1 89/10/23 15:42:56 dupuy Exp $ */
  2.  
  3. #include <strings.h>            /* strncpy */
  4. #include <sys/file.h>            /* O_RDWR */
  5. #include <sys/types.h>            /* NIOCBIND (u_long) */
  6. #include <sys/time.h>            /* NIOCBIND (timeval) */
  7. #include <sys/ioccom.h>            /* ioctl */
  8. #include <sys/socket.h>            /* ifreq (sockaddr) */
  9.  
  10. #include <net/if.h>            /* ifreq */
  11. #include <net/nit_if.h>            /* NIOCBIND */
  12.  
  13. #include "libether.h"
  14.  
  15. extern int errno;
  16.  
  17. #ifndef NIT_DEV
  18. #define NIT_DEV "/dev/nit"
  19. #endif
  20.  
  21. ether_addr _ether_localaddr;        /* shared with ether_address() */
  22. int _ether_haveaddress;            /* shared with ether_address() */
  23.  
  24. /*
  25.  * Returns the local ethernet address for an ethernet interface.  The result
  26.  * is stored in the structure given by address; if none is given, malloc is
  27.  * used to allocate space.  Sun systems have the "feature" that all ethernet
  28.  * interfaces are set to the same address (taken from the ID PROM).  So we can
  29.  * get the answer once, and use it forever.
  30.  */
  31.  
  32. ether_addr *
  33. ether_intfaddr (intf, address)
  34. char *intf;
  35. ether_addr *address;
  36. {
  37.     if (!_ether_haveaddress)
  38.     {
  39.     int fd;
  40.     struct ifreq ifr;
  41.     int saved_errno;
  42.  
  43.     if ((fd = open (NIT_DEV, O_RDWR)) < 0)
  44.     {
  45. #ifdef DEBUG
  46.         perror (NIT_DEV);
  47. #endif
  48.         return (0);
  49.     }
  50.  
  51.     (void) strncpy (ifr.ifr_name, intf, sizeof (ifr.ifr_name));
  52.  
  53.     if (ioctl (fd, NIOCBIND, (char *) &ifr) < 0)
  54.     {
  55.         saved_errno = errno;
  56. #ifdef DEBUG
  57.         perror ("ether_intfaddr: ioctl NIOCBIND");
  58. #endif
  59.         (void) close (fd);
  60.         errno = saved_errno;
  61.         return (0);
  62.     }
  63.  
  64.     address = ether_address (fd, address);
  65.  
  66.     saved_errno = errno;
  67.     (void) close (fd);
  68.     errno = saved_errno;
  69.  
  70.     return (address);
  71.     }
  72.  
  73.     if (address == 0)
  74.     address = (ether_addr *) malloc (sizeof (ether_addr));
  75.  
  76.     if (address != 0)
  77.     *address = _ether_localaddr;
  78.  
  79.     return (address);
  80. }
  81.