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

  1. /* $Id: enetaddr.c,v 2.0 89/10/20 19:01:58 dupuy Exp $ */
  2.  
  3. #include <sys/types.h>            /* FD_ISSET */
  4. #include <sys/ioctl.h>            /* ioctl */
  5.  
  6. #include <net/enet.h>            /* endevp */
  7.  
  8. #include "libether.h"
  9.  
  10. static fd_set addrcachevalid;
  11.  
  12. static ether_addr ifaddrs[FD_SETSIZE];
  13.  
  14. #define ifaddr (ifaddrs[fd])
  15.  
  16. /*
  17.  * Returns the local ethernet address for the ethernet interface on the file
  18.  * descriptor fd.  The result is stored in the structure given by address; if
  19.  * none is given, malloc is used to allocate space.  We cache results here
  20.  * because this function is called every time we send a packet.
  21.  */
  22.  
  23. ether_addr *
  24. ether_address (fd, address)
  25. int fd;
  26. ether_addr *address;
  27. {
  28.     if (!FD_ISSET (fd, &addrcachevalid))
  29.     {
  30.     struct endevp edev;
  31.  
  32.     if (ioctl (fd, EIOCDEVP, (char *) &edev) < 0)
  33.     {
  34. #ifdef DEBUG
  35.         perror ("ether_address: ioctl EIOCDEVP");
  36. #endif
  37.         return (0);
  38.     }
  39.  
  40.     bcopy ((char *) edev.end_addr, (char *) &ifaddr, sizeof (ether_addr));
  41.  
  42.     FD_SET (fd, &addrcachevalid);
  43.     }
  44.  
  45.  
  46.     if (address == 0)
  47.     address = (ether_addr *) malloc (sizeof (ether_addr));
  48.  
  49.     if (address != 0)
  50.     bcopy ((char *) &ifaddr, (char *) address, sizeof (ether_addr));
  51.  
  52.     return (address);
  53. }
  54.  
  55. /*
  56.  * This function is called by ether_open to invalidate the local address
  57.  * cache, since the file descriptor may be attached to a different interface.
  58.  */
  59.  
  60. void
  61. _ether_newaddr (fd)
  62. int fd;
  63. {
  64.     FD_CLR (fd, &addrcachevalid);
  65. }
  66.