hostnics.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. /*====================================================================*
  2. *
  3. * Copyright (c) 2013 Qualcomm Atheros, Inc.
  4. *
  5. * All rights reserved.
  6. *
  7. * Redistribution and use in source and binary forms, with or
  8. * without modification, are permitted (subject to the limitations
  9. * in the disclaimer below) provided that the following conditions
  10. * are met:
  11. *
  12. * * Redistributions of source code must retain the above copyright
  13. * notice, this list of conditions and the following disclaimer.
  14. *
  15. * * Redistributions in binary form must reproduce the above
  16. * copyright notice, this list of conditions and the following
  17. * disclaimer in the documentation and/or other materials
  18. * provided with the distribution.
  19. *
  20. * * Neither the name of Qualcomm Atheros nor the names of
  21. * its contributors may be used to endorse or promote products
  22. * derived from this software without specific prior written
  23. * permission.
  24. *
  25. * NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE
  26. * GRANTED BY THIS LICENSE. THIS SOFTWARE IS PROVIDED BY THE
  27. * COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR
  28. * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  29. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  30. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
  31. * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  32. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  33. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  34. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  35. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  36. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
  37. * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  38. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  39. *
  40. *--------------------------------------------------------------------*/
  41. /*====================================================================*
  42. *
  43. * unsigned hostnics (struct nic list [], unsigned size);
  44. *
  45. * ether.h
  46. *
  47. * encode an external memory region with a packed list of available
  48. * nost network interfaces; return the number of interfaces found;
  49. * each interface has an index, ethernet address, internet address,
  50. * name and description;
  51. *
  52. * this function is implemented for Linux and MacOSX;
  53. *
  54. *
  55. * Contributor(s):
  56. * Charles Maier
  57. *
  58. *--------------------------------------------------------------------*/
  59. #ifndef HOSTNICS_SOURCE
  60. #define HOSTNICS_SOURCE
  61. #if defined (__linux__)
  62. # include <net/if.h>
  63. # include <net/ethernet.h>
  64. # include <sys/ioctl.h>
  65. #elif defined (__linux__)
  66. # include <net/if.h>
  67. # include <netpacket/packet.h>
  68. # include <ifaddrs.h>
  69. #elif defined (__APPLE__) || defined (__OpenBSD__) || defined (__NetBSD__) || defined (__FreeBSD__)
  70. # include <sys/types.h>
  71. # include <sys/socket.h>
  72. # include <net/if.h>
  73. # include <net/if_dl.h>
  74. # include <net/if_types.h>
  75. # include <ifaddrs.h>
  76. #elif defined (WIN32)
  77. #error "Not implemented for Windows"
  78. #else
  79. #error "Unknown environment"
  80. #endif
  81. #include <unistd.h>
  82. #include <memory.h>
  83. #include <errno.h>
  84. #include "../ether/ether.h"
  85. #include "../tools/error.h"
  86. unsigned hostnics (struct nic nics [], unsigned size)
  87. {
  88. #if defined (__linux__)
  89. /*
  90. * native method on Linux systems;
  91. */
  92. char buffer [1024];
  93. struct ifconf ifconf;
  94. struct ifreq * ifreq;
  95. unsigned next;
  96. signed fd;
  97. memset (nics, 0, size * sizeof (struct nic));
  98. ifconf.ifc_len = sizeof (buffer);
  99. ifconf.ifc_buf = buffer;
  100. if ((fd = socket (AF_INET, SOCK_DGRAM, 0)) < 0)
  101. {
  102. error (1, errno, "Can't open socket");
  103. }
  104. if (ioctl (fd, SIOCGIFCONF, &ifconf) < 0)
  105. {
  106. error (1, errno, "Can't read socket configuration");
  107. }
  108. ifreq = ifconf.ifc_req;
  109. next = ifconf.ifc_len / sizeof (struct ifreq);
  110. if (next > size)
  111. {
  112. next = size;
  113. }
  114. if (next < size)
  115. {
  116. size = next;
  117. }
  118. for (next = 0; next < size; next++)
  119. {
  120. struct nic * nic = &nics [next];
  121. struct sockaddr_in * sockaddr_in = (struct sockaddr_in *)(&ifreq->ifr_addr);
  122. memcpy (nic->ifname, ifreq->ifr_name, sizeof (nic->ifname));
  123. memcpy (nic->ifdesc, ifreq->ifr_name, sizeof (nic->ifdesc));
  124. memcpy (nic->internet, &sockaddr_in->sin_addr, sizeof (nic->internet));
  125. if (ioctl (fd, SIOCGIFHWADDR, ifreq) == -1)
  126. {
  127. error (0, errno, "Can't read hardware address: %s", ifreq->ifr_name);
  128. }
  129. memcpy (nic->ethernet, ifreq->ifr_hwaddr.sa_data, sizeof (nic->ethernet));
  130. if (ioctl (fd, SIOCGIFINDEX, ifreq) == -1)
  131. {
  132. error (0, errno, "Can't read interface index: %s", ifreq->ifr_name);
  133. }
  134. nic->ifindex = ifreq->ifr_ifindex;
  135. ifreq++;
  136. }
  137. close (fd);
  138. #elif defined (__linux__) || defined (__APPLE__) || defined (__OpenBSD__) || defined (__NetBSD__) || defined (__FreeBSD__)
  139. /*
  140. * generic (POSIX) method for unix-like systems;
  141. */
  142. struct ifaddrs * ifaddrs;
  143. memset (nics, 0, size * sizeof (struct nic));
  144. unsigned next = 0;
  145. if (getifaddrs (&ifaddrs) != -1)
  146. {
  147. struct ifaddrs * ifaddr;
  148. for (ifaddr = ifaddrs; ifaddr && size; ifaddr = ifaddr->ifa_next)
  149. {
  150. struct nic * nic = &nics [next];
  151. struct nic * tmp = nics;
  152. if (!ifaddr->ifa_addr)
  153. {
  154. continue;
  155. }
  156. nic->ifindex = if_nametoindex (ifaddr->ifa_name);
  157. for (tmp = nics; tmp->ifindex != nic->ifindex; tmp++);
  158. if (tmp == nic)
  159. {
  160. next++;
  161. size--;
  162. }
  163. else
  164. {
  165. nic = tmp;
  166. }
  167. memcpy (nic->ifname, ifaddr->ifa_name, sizeof (nic->ifname));
  168. memcpy (nic->ifdesc, ifaddr->ifa_name, sizeof (nic->ifdesc));
  169. if (ifaddr->ifa_addr->sa_family == AF_INET)
  170. {
  171. struct sockaddr_in * sockaddr_in = (struct sockaddr_in *) (ifaddr->ifa_addr);
  172. struct in_addr * in_addr = (struct in_addr *)(&sockaddr_in->sin_addr);
  173. memcpy (nic->internet, &in_addr->s_addr, sizeof (nic->internet));
  174. }
  175. #if defined (__linux__)
  176. #define LLADDR(socket) &(socket)->sll_addr
  177. if (ifaddr->ifa_addr->sa_family == AF_PACKET)
  178. {
  179. struct sockaddr_ll * sockaddr_ll = (struct sockaddr_ll *) (ifaddr->ifa_addr);
  180. memcpy (nic->ethernet, LLADDR (sockaddr_ll), sizeof (nic->ethernet));
  181. }
  182. #elif defined (__APPLE__) || defined (__OpenBSD__) || defined (__NetBSD__) || defined (__FreeBSD__)
  183. if (ifaddr->ifa_addr->sa_family == AF_LINK)
  184. {
  185. struct sockaddr_dl * sockaddr_dl = (struct sockaddr_dl *) (ifaddr->ifa_addr);
  186. if (sockaddr_dl->sdl_type == IFT_ETHER)
  187. {
  188. #if 1
  189. memcpy (nic->ethernet, LLADDR (sockaddr_dl), sizeof (nic->ethernet));
  190. #else
  191. memcpy (nic->ethernet, LLADDR (sockaddr_dl), sockaddr_dl->sdl_alen);
  192. #endif
  193. }
  194. }
  195. #else
  196. #error "Abandon all hope!"
  197. #endif
  198. }
  199. freeifaddrs (ifaddrs);
  200. }
  201. #else
  202. #error "Unknown environment"
  203. #endif
  204. return (next);
  205. }
  206. #endif