gethwaddr.c.html 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  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. gethwaddr.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='getargv.c.html' title=' getargv.c '>PREV</a>]
  17. [<a href='toolkit.html' title=' Index '>HOME</a>]
  18. [<a href='getifname.c.html' title=' getifname.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. * int gethwaddr (void volatile * memory, char const * device);
  64. *
  65. * ether.h
  66. *
  67. * encode memory with the hardware address of a named host Ethernet
  68. * interface;
  69. *
  70. * there are two ways to obtain the hardware address on Linux; we
  71. * use the first because some systems do not support getifaddrs()
  72. * and some implementations are inconsistent;
  73. *
  74. *
  75. * Contributor(s):
  76. * Nathaniel Houghton &lt;nhoughto@qca.qualcomm.com&gt;
  77. * Charles Maier &lt;cmaier@qca.qualcomm.com&gt;
  78. *
  79. *--------------------------------------------------------------------*/
  80. #ifndef GETHWADDR_SOURCE
  81. #define GETHWADDR_SOURCE
  82. #include &lt;stdint.h&gt;
  83. #include &lt;unistd.h&gt;
  84. #include &lt;memory.h&gt;
  85. #include &quot;../tools/error.h&quot;
  86. #include &quot;../ether/ether.h&quot;
  87. #ifndef OID_802_3_CURRENT_ADDRESS
  88. #define OID_802_3_CURRENT_ADDRESS 0x01010102
  89. #endif
  90. int gethwaddr (void * memory, char const * device)
  91. {
  92. #if defined (__linux__)
  93. # include &lt;ifaddrs.h&gt;
  94. # include &lt;netpacket/packet.h&gt;
  95. # include &lt;sys/ioctl.h&gt;
  96. struct ifreq ifreq;
  97. int fd;
  98. if ((fd = socket (AF_INET, SOCK_DGRAM, 0)) == -1)
  99. {
  100. error (1, errno, &quot;%s: %s&quot;, __func__, device);
  101. }
  102. memcpy (ifreq.ifr_name, device, sizeof (ifreq.ifr_name));
  103. if (ioctl (fd, SIOCGIFHWADDR, &amp;ifreq) == -1)
  104. {
  105. close (fd);
  106. return (-1);
  107. }
  108. memcpy (memory, ifreq.ifr_ifru.ifru_hwaddr.sa_data, ETHER_ADDR_LEN);
  109. close (fd);
  110. #elif defined (__linux__)
  111. #include &lt;ifaddrs.h&gt;
  112. #include &lt;net/if_types.h&gt;
  113. struct ifaddrs *ifaddrs;
  114. struct ifaddrs *ifaddr;
  115. if (getifaddrs (&amp;ifaddrs) == -1)
  116. {
  117. error (1, errno, &quot;No interfaces available&quot;);
  118. }
  119. for (ifaddr = ifaddrs; ifaddr; ifaddr = ifaddr-&gt;ifa_next)
  120. {
  121. if (strcmp (device, ifaddr-&gt;ifa_name))
  122. {
  123. continue;
  124. }
  125. if (!ifaddr-&gt;ifa_addr)
  126. {
  127. continue;
  128. }
  129. if (ifaddr-&gt;ifa_addr-&gt;sa_family == AF_PACKET)
  130. {
  131. struct sockaddr_ll * sockaddr = (struct sockaddr_ll *) (ifaddr-&gt;ifa_addr);
  132. memcpy (memory, sockaddr-&gt;sll_addr, ETHER_ADDR_LEN);
  133. break;
  134. }
  135. }
  136. freeifaddrs (ifaddrs);
  137. #elif defined (__APPLE__)
  138. #include &lt;ifaddrs.h&gt;
  139. #include &lt;net/if_dl.h&gt;
  140. struct ifaddrs *ifaddrs;
  141. struct ifaddrs *ifaddr;
  142. if (getifaddrs (&amp;ifaddrs) == -1)
  143. {
  144. error (1, errno, &quot;No interfaces available&quot;);
  145. }
  146. for (ifaddr = ifaddrs; ifaddr; ifaddr = ifaddr-&gt;ifa_next)
  147. {
  148. if (strcmp (device, ifaddr-&gt;ifa_name))
  149. {
  150. continue;
  151. }
  152. if (!ifaddr-&gt;ifa_addr)
  153. {
  154. continue;
  155. }
  156. if (ifaddr-&gt;ifa_addr-&gt;sa_family == AF_LINK)
  157. {
  158. struct sockaddr_dl * sockaddr = (struct sockaddr_dl *) (ifaddr-&gt;ifa_addr);
  159. memcpy (memory, LLADDR (sockaddr), ETHER_ADDR_LEN);
  160. break;
  161. }
  162. }
  163. freeifaddrs (ifaddrs);
  164. #elif defined (__OpenBSD__)
  165. #include &lt;ifaddrs.h&gt;
  166. #include &lt;net/if_dl.h&gt;
  167. struct ifaddrs *ifaddrs;
  168. struct ifaddrs *ifaddr;
  169. if (getifaddrs (&amp;ifaddrs) == -1)
  170. {
  171. error (1, errno, &quot;No interfaces available&quot;);
  172. }
  173. for (ifaddr = ifaddrs; ifaddr; ifaddr = ifaddr-&gt;ifa_next)
  174. {
  175. if (strcmp (device, ifaddr-&gt;ifa_name))
  176. {
  177. continue;
  178. }
  179. if (!ifaddr-&gt;ifa_addr)
  180. {
  181. continue;
  182. }
  183. if (ifaddr-&gt;ifa_addr-&gt;sa_family == AF_LINK)
  184. {
  185. struct sockaddr_dl * sockaddr = (struct sockaddr_dl *) (ifaddr-&gt;ifa_addr);
  186. memcpy (memory, LLADDR (sockaddr), ETHER_ADDR_LEN);
  187. break;
  188. }
  189. }
  190. freeifaddrs (ifaddrs);
  191. #elif defined (WINPCAP) || defined (LIBPCAP)
  192. LPADAPTER adapter = PacketOpenAdapter ((PCHAR)(device));
  193. PPACKET_OID_DATA data = (PPACKET_OID_DATA)(malloc (ETHER_ADDR_LEN + sizeof (PACKET_OID_DATA)));
  194. if (!data)
  195. {
  196. error (1, errno, &quot;Can't allocate packet: %s&quot;, device);
  197. }
  198. data-&gt;Oid = OID_802_3_CURRENT_ADDRESS;
  199. data-&gt;Length = ETHER_ADDR_LEN;
  200. if ((adapter == 0) || (adapter-&gt;hFile == INVALID_HANDLE_VALUE))
  201. {
  202. error (1, errno, &quot;Can't access interface: %s&quot;, device);
  203. }
  204. if (!PacketRequest (adapter, FALSE, data))
  205. {
  206. memset (memory, 0, ETHER_ADDR_LEN);
  207. PacketCloseAdapter (adapter);
  208. free (data);
  209. return (-1);
  210. }
  211. memcpy (memory, data-&gt;Data, data-&gt;Length);
  212. PacketCloseAdapter (adapter);
  213. free (data);
  214. #else
  215. #error &quot;Unknown environment&quot;
  216. #endif
  217. return (0);
  218. }
  219. #endif
  220. </pre>
  221. <div class='footerlink'>
  222. [<a href='getargv.c.html' title=' getargv.c '>PREV</a>]
  223. [<a href='toolkit.html' title=' Index '>HOME</a>]
  224. [<a href='getifname.c.html' title=' getifname.c '>NEXT</a>]
  225. </div>
  226. </body>
  227. </html>