addrs_ioctl.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. /*
  2. * addrs_ioctl.c:
  3. *
  4. * Provides the get_addrs_ioctl() function for use on systems that
  5. * support a simple socket ioctl for acquiring low-level ethernet
  6. * information about interfaces.
  7. *
  8. */
  9. #include <stdio.h>
  10. #include <stdlib.h>
  11. #include <unistd.h>
  12. #include <string.h>
  13. #include <sys/types.h>
  14. #include <sys/ioctl.h>
  15. #include <sys/socket.h>
  16. #include <net/if.h>
  17. #include <netinet/in.h>
  18. #if defined __FreeBSD__ || defined __OpenBSD__ || defined __APPLE__ \
  19. || ( defined __GLIBC__ && ! defined __linux__ )
  20. #include <sys/param.h>
  21. #include <sys/sysctl.h>
  22. #include <net/if_dl.h>
  23. #endif
  24. #ifdef USE_GETIFADDRS
  25. #include <ifaddrs.h>
  26. #endif
  27. #include "iftop.h"
  28. /*
  29. * This function identifies the IP address and ethernet address for the requested
  30. * interface
  31. *
  32. * This function returns -1 on catastrophic failure, or a bitwise OR of the
  33. * following values:
  34. *
  35. * 1 - Was able to get the ethernet address
  36. * 2 - Was able to get the IP address
  37. *
  38. * This function should return 3 if all information was found
  39. */
  40. int
  41. get_addrs_ioctl(char *interface, char if_hw_addr[], struct in_addr *if_ip_addr, struct in6_addr *if_ip6_addr)
  42. {
  43. int s;
  44. struct ifreq ifr = {};
  45. int got_hw_addr = 0;
  46. int got_ip_addr = 0;
  47. int got_ip6_addr = 0;
  48. #ifdef USE_GETIFADDRS
  49. struct ifaddrs *ifa, *ifas;
  50. #endif
  51. /* -- */
  52. s = socket(AF_INET, SOCK_DGRAM, 0); /* any sort of IP socket will do */
  53. if (s == -1) {
  54. perror("socket");
  55. return -1;
  56. }
  57. fprintf(stderr,"interface: %s\n", interface);
  58. memset(if_hw_addr, 0, 6);
  59. strncpy(ifr.ifr_name, interface, IFNAMSIZ);
  60. #ifdef SIOCGIFHWADDR
  61. if (ioctl(s, SIOCGIFHWADDR, &ifr) < 0) {
  62. fprintf(stderr, "Error getting hardware address for interface: %s\n", interface);
  63. perror("ioctl(SIOCGIFHWADDR)");
  64. }
  65. else {
  66. memcpy(if_hw_addr, ifr.ifr_hwaddr.sa_data, 6);
  67. got_hw_addr = 1;
  68. }
  69. #else
  70. #if defined __FreeBSD__ || defined __OpenBSD__ || defined __APPLE__ \
  71. || ( defined __GLIBC__ && ! defined __linux__ )
  72. {
  73. int sysctlparam[6] = {CTL_NET, PF_ROUTE, 0, 0, NET_RT_IFLIST, 0};
  74. size_t needed = 0;
  75. char *buf = NULL;
  76. struct if_msghdr *msghdr = NULL;
  77. sysctlparam[5] = if_nametoindex(interface);
  78. if (sysctlparam[5] == 0) {
  79. fprintf(stderr, "Error getting hardware address for interface: %s\n", interface);
  80. }
  81. else if (sysctl(sysctlparam, 6, NULL, &needed, NULL, 0) < 0) {
  82. fprintf(stderr, "Error getting hardware address for interface: %s\n", interface);
  83. }
  84. else if ((buf = malloc(needed)) == NULL) {
  85. fprintf(stderr, "Error getting hardware address for interface: %s\n", interface);
  86. }
  87. else if (sysctl(sysctlparam, 6, buf, &needed, NULL, 0) < 0) {
  88. fprintf(stderr, "Error getting hardware address for interface: %s\n", interface);
  89. free(buf);
  90. }
  91. else {
  92. msghdr = (struct if_msghdr *) buf;
  93. memcpy(if_hw_addr, LLADDR((struct sockaddr_dl *)(buf + sizeof(struct if_msghdr) - sizeof(struct if_data) + sizeof(struct if_data))), 6);
  94. free(buf);
  95. got_hw_addr = 1;
  96. }
  97. }
  98. #else
  99. fprintf(stderr, "Cannot obtain hardware address on this platform\n");
  100. #endif
  101. #endif
  102. /* Get the IP address of the interface */
  103. #ifdef USE_GETIFADDRS
  104. if (getifaddrs(&ifas) == -1) {
  105. fprintf(stderr, "Unable to get IP address for interface: %s\n", interface);
  106. perror("getifaddrs()");
  107. }
  108. else {
  109. for (ifa = ifas; ifa != NULL; ifa = ifa->ifa_next) {
  110. if (got_ip_addr && got_ip6_addr)
  111. break; /* Search is already complete. */
  112. if (strcmp(ifa->ifa_name, interface))
  113. continue; /* Not our interface. */
  114. if (ifa->ifa_addr == NULL)
  115. continue; /* Skip NULL interface address. */
  116. if ( (ifa->ifa_addr->sa_family != AF_INET)
  117. && (ifa->ifa_addr->sa_family != AF_INET6) )
  118. continue; /* AF_PACKET is beyond our scope. */
  119. if ( (ifa->ifa_addr->sa_family == AF_INET)
  120. && !got_ip_addr ) {
  121. got_ip_addr = 2;
  122. memcpy(if_ip_addr,
  123. &(((struct sockaddr_in *) ifa->ifa_addr)->sin_addr),
  124. sizeof(*if_ip_addr));
  125. continue;
  126. }
  127. /* Must be a IPv6 address at this point. */
  128. struct sockaddr_in6 *sa6 = (struct sockaddr_in6 *) ifa->ifa_addr;
  129. if ( IN6_IS_ADDR_LINKLOCAL(&(sa6->sin6_addr))
  130. || IN6_IS_ADDR_SITELOCAL(&(sa6->sin6_addr)) )
  131. continue;
  132. /* A useful IPv6 address. */
  133. memcpy(if_ip6_addr, &(sa6->sin6_addr), sizeof(*if_ip6_addr));
  134. got_ip6_addr = 4;
  135. }
  136. freeifaddrs(ifas);
  137. } /* getifaddrs() */
  138. #elif defined(SIOCGIFADDR)
  139. (*(struct sockaddr_in *) &ifr.ifr_addr).sin_family = AF_INET;
  140. if (ioctl(s, SIOCGIFADDR, &ifr) < 0) {
  141. fprintf(stderr, "Unable to get IP address for interface: %s\n", interface);
  142. perror("ioctl(SIOCGIFADDR)");
  143. }
  144. else {
  145. memcpy(if_ip_addr, &((*(struct sockaddr_in *) &ifr.ifr_addr).sin_addr), sizeof(struct in_addr));
  146. got_ip_addr = 2;
  147. }
  148. #else
  149. fprintf(stderr, "Cannot obtain IP address on this platform\n");
  150. #endif
  151. close(s);
  152. return got_hw_addr + got_ip_addr + got_ip6_addr;
  153. }