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

  1. /* $Id: dliaddr.c,v 2.0 89/10/20 19:01:26 dupuy Exp $ */
  2.  
  3. #include <sys/param.h>            /* NOFILE */
  4. #include <sys/ioctl.h>            /* ioctl */
  5. #include <sys/socket.h>            /* sockaddr_dl (sockaddr) */
  6.  
  7. #include <net/if.h>            /* ifdevea */
  8. #include <netinet/in.h>            /* (ether_header) (in_addr) */
  9. #include <netinet/if_ether.h>        /* sockaddr_dl (ether_header)  */
  10. #include <netdnet/dli_var.h>        /* sockaddr_dl */
  11.  
  12. #include "libether.h"
  13.  
  14. extern struct sockaddr_dl _ether_sockaddrs[NOFILE];
  15.  
  16. #define sad (_ether_sockaddrs[fd])
  17.  
  18.  
  19. static void
  20. devid_to_name (name, devid)
  21. char *name;
  22. struct dli_devid *devid;
  23. {
  24.     register int i;
  25.  
  26.     for (i = 0; devid->dli_devname[i]; i++)
  27.     name[i] = devid->dli_devname[i];
  28.  
  29.     name[i++] = '0' + devid->dli_devnumber;
  30.  
  31.     name[i] = '\0';
  32. }
  33.  
  34.  
  35. /*
  36.  * Returns the local ethernet address for the ethernet interface on the file
  37.  * descriptor fd.  The result is stored in the structure given by address; if
  38.  * none is given, malloc is used to allocate space.
  39.  */
  40.  
  41. ether_addr *
  42. ether_address (fd, address)
  43. int fd;
  44. ether_addr *address;
  45. {
  46.     struct ifdevea ifd;
  47.  
  48.     devid_to_name (ifd.ifr_name, &sad.dli_device);
  49.  
  50.     if (ioctl (fd, SIOCRPHYSADDR, (char *) &ifd) < 0)
  51.     {
  52. #ifdef DEBUG
  53.     perror ("ether_address: ioctl SIOCRPHYSADDR");
  54. #endif
  55.     return (0);
  56.     }
  57.  
  58.     if (address == 0)
  59.     address = (ether_addr *) malloc (sizeof (ether_addr));
  60.  
  61.     if (address != 0)
  62.     bcopy ((char *) ifd.current_pa, (char *) address, sizeof (ether_addr));
  63.  
  64.     return (address);
  65. }
  66.