gethwaddr.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  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. * int gethwaddr (void volatile * memory, char const * device);
  44. *
  45. * ether.h
  46. *
  47. * encode memory with the hardware address of a named host Ethernet
  48. * interface;
  49. *
  50. * there are two ways to obtain the hardware address on Linux; we
  51. * use the first because some systems do not support getifaddrs()
  52. * and some implementations are inconsistent;
  53. *
  54. *
  55. * Contributor(s):
  56. * Nathaniel Houghton
  57. * Charles Maier
  58. *
  59. *--------------------------------------------------------------------*/
  60. #ifndef GETHWADDR_SOURCE
  61. #define GETHWADDR_SOURCE
  62. #include <stdint.h>
  63. #include <unistd.h>
  64. #include <memory.h>
  65. #include "../tools/error.h"
  66. #include "../ether/ether.h"
  67. #ifndef OID_802_3_CURRENT_ADDRESS
  68. #define OID_802_3_CURRENT_ADDRESS 0x01010102
  69. #endif
  70. int gethwaddr (void * memory, char const * device)
  71. {
  72. #if defined (__linux__)
  73. # include <ifaddrs.h>
  74. # include <netpacket/packet.h>
  75. # include <sys/ioctl.h>
  76. struct ifreq ifreq;
  77. int fd;
  78. if ((fd = socket (AF_INET, SOCK_DGRAM, 0)) == -1)
  79. {
  80. error (1, errno, "%s: %s", __func__, device);
  81. }
  82. memcpy (ifreq.ifr_name, device, sizeof (ifreq.ifr_name));
  83. if (ioctl (fd, SIOCGIFHWADDR, &ifreq) == -1)
  84. {
  85. close (fd);
  86. return (-1);
  87. }
  88. memcpy (memory, ifreq.ifr_ifru.ifru_hwaddr.sa_data, ETHER_ADDR_LEN);
  89. close (fd);
  90. #elif defined (__linux__)
  91. #include <ifaddrs.h>
  92. #include <net/if_types.h>
  93. struct ifaddrs *ifaddrs;
  94. struct ifaddrs *ifaddr;
  95. if (getifaddrs (&ifaddrs) == -1)
  96. {
  97. error (1, errno, "No interfaces available");
  98. }
  99. for (ifaddr = ifaddrs; ifaddr; ifaddr = ifaddr->ifa_next)
  100. {
  101. if (strcmp (device, ifaddr->ifa_name))
  102. {
  103. continue;
  104. }
  105. if (!ifaddr->ifa_addr)
  106. {
  107. continue;
  108. }
  109. if (ifaddr->ifa_addr->sa_family == AF_PACKET)
  110. {
  111. struct sockaddr_ll * sockaddr = (struct sockaddr_ll *) (ifaddr->ifa_addr);
  112. memcpy (memory, sockaddr->sll_addr, ETHER_ADDR_LEN);
  113. break;
  114. }
  115. }
  116. freeifaddrs (ifaddrs);
  117. #elif defined (__APPLE__)
  118. #include <ifaddrs.h>
  119. #include <net/if_dl.h>
  120. struct ifaddrs *ifaddrs;
  121. struct ifaddrs *ifaddr;
  122. if (getifaddrs (&ifaddrs) == -1)
  123. {
  124. error (1, errno, "No interfaces available");
  125. }
  126. for (ifaddr = ifaddrs; ifaddr; ifaddr = ifaddr->ifa_next)
  127. {
  128. if (strcmp (device, ifaddr->ifa_name))
  129. {
  130. continue;
  131. }
  132. if (!ifaddr->ifa_addr)
  133. {
  134. continue;
  135. }
  136. if (ifaddr->ifa_addr->sa_family == AF_LINK)
  137. {
  138. struct sockaddr_dl * sockaddr = (struct sockaddr_dl *) (ifaddr->ifa_addr);
  139. memcpy (memory, LLADDR (sockaddr), ETHER_ADDR_LEN);
  140. break;
  141. }
  142. }
  143. freeifaddrs (ifaddrs);
  144. #elif defined (__OpenBSD__) || defined(__NetBSD__) || defined (__FreeBSD__)
  145. #include <ifaddrs.h>
  146. #include <net/if_dl.h>
  147. struct ifaddrs *ifaddrs;
  148. struct ifaddrs *ifaddr;
  149. if (getifaddrs (&ifaddrs) == -1)
  150. {
  151. error (1, errno, "No interfaces available");
  152. }
  153. for (ifaddr = ifaddrs; ifaddr; ifaddr = ifaddr->ifa_next)
  154. {
  155. if (strcmp (device, ifaddr->ifa_name))
  156. {
  157. continue;
  158. }
  159. if (!ifaddr->ifa_addr)
  160. {
  161. continue;
  162. }
  163. if (ifaddr->ifa_addr->sa_family == AF_LINK)
  164. {
  165. struct sockaddr_dl * sockaddr = (struct sockaddr_dl *) (ifaddr->ifa_addr);
  166. memcpy (memory, LLADDR (sockaddr), ETHER_ADDR_LEN);
  167. break;
  168. }
  169. }
  170. freeifaddrs (ifaddrs);
  171. #elif defined (WINPCAP) || defined (LIBPCAP)
  172. LPADAPTER adapter = PacketOpenAdapter ((PCHAR)(device));
  173. PPACKET_OID_DATA data = (PPACKET_OID_DATA)(malloc (ETHER_ADDR_LEN + sizeof (PACKET_OID_DATA)));
  174. if (!data)
  175. {
  176. error (1, errno, "Can't allocate packet: %s", device);
  177. }
  178. data->Oid = OID_802_3_CURRENT_ADDRESS;
  179. data->Length = ETHER_ADDR_LEN;
  180. if ((adapter == 0) || (adapter->hFile == INVALID_HANDLE_VALUE))
  181. {
  182. error (1, errno, "Can't access interface: %s", device);
  183. }
  184. if (!PacketRequest (adapter, FALSE, data))
  185. {
  186. memset (memory, 0, ETHER_ADDR_LEN);
  187. PacketCloseAdapter (adapter);
  188. free (data);
  189. return (-1);
  190. }
  191. memcpy (memory, data->Data, data->Length);
  192. PacketCloseAdapter (adapter);
  193. free (data);
  194. #else
  195. #error "Unknown environment"
  196. #endif
  197. return (0);
  198. }
  199. #endif