hostnics.c.html 7.9 KB

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