net-if.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  1. /******************************************************************************
  2. Copyright (C) 2016 B. Lee <[email protected]>
  3. This program is free software: you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation, either version 2 of the License, or
  6. (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with this program. If not, see <http://www.gnu.org/licenses/>.
  13. ******************************************************************************/
  14. #include "net-if.h"
  15. #include <util/platform.h>
  16. #include <util/dstr.h>
  17. #define do_log(level, format, ...) \
  18. blog(level, "[net if] " format, ##__VA_ARGS__)
  19. #define warn(format, ...) do_log(LOG_WARNING, format, ##__VA_ARGS__)
  20. #define info(format, ...) do_log(LOG_INFO, format, ##__VA_ARGS__)
  21. #define debug(format, ...) do_log(LOG_DEBUG, format, ##__VA_ARGS__)
  22. static inline void netif_saddr_data_push_back(struct netif_saddr_data *sd,
  23. const char *ip, const char *adapter)
  24. {
  25. struct netif_saddr_item item;
  26. struct dstr full_name = {0};
  27. char *ip_dup = bstrdup(ip);
  28. if (adapter && *adapter)
  29. dstr_printf(&full_name, "[%s] %s", adapter, ip);
  30. else
  31. dstr_copy(&full_name, ip);
  32. item.name = full_name.array;
  33. item.addr = ip_dup;
  34. da_push_back(sd->addrs, &item);
  35. }
  36. static void netif_convert_to_string(char *dest,
  37. struct sockaddr_storage *byte_address)
  38. {
  39. int family = byte_address->ss_family;
  40. char temp_char[INET6_ADDRSTRLEN] = {0};
  41. #ifndef _WIN32
  42. if (family == AF_INET)
  43. inet_ntop(family, &(((struct sockaddr_in*)byte_address)->sin_addr),
  44. temp_char, INET6_ADDRSTRLEN);
  45. else if (family == AF_INET6)
  46. inet_ntop(family, &(((struct sockaddr_in*)byte_address)->sin_addr),
  47. temp_char, INET6_ADDRSTRLEN);
  48. #else
  49. if (family == AF_INET)
  50. InetNtopA(family, &(((SOCKADDR_IN *)byte_address)->sin_addr),
  51. temp_char, INET6_ADDRSTRLEN);
  52. else if (family == AF_INET6)
  53. InetNtopA(family, &(((SOCKADDR_IN6 *)byte_address)->sin6_addr),
  54. temp_char, INET6_ADDRSTRLEN);
  55. #endif
  56. strncpy(dest, temp_char, INET6_ADDRSTRLEN);
  57. }
  58. static void netif_push(struct sockaddr *copy_source,
  59. struct netif_saddr_data *saddr_d,
  60. const char *adapter)
  61. {
  62. char temp_char[INET6_ADDRSTRLEN] = {0};
  63. struct sockaddr_storage sa = {0};
  64. if (copy_source->sa_family == AF_INET)
  65. memcpy(&sa, copy_source, sizeof(struct sockaddr_in));
  66. else if (copy_source->sa_family == AF_INET6)
  67. memcpy(&sa, copy_source, sizeof(struct sockaddr_in6));
  68. netif_convert_to_string(temp_char, &sa);
  69. netif_saddr_data_push_back(saddr_d, temp_char, adapter);
  70. }
  71. void netif_log_saddrs(struct netif_saddr_data *sd)
  72. {
  73. for(size_t i = 0; i < sd->addrs.num; i++)
  74. info("\t\t%s", sd->addrs.array[i].name);
  75. }
  76. bool netif_str_to_addr(struct sockaddr_storage *out, int *addr_len,
  77. const char *addr)
  78. {
  79. bool ipv6;
  80. memset(out, 0, sizeof(*out));
  81. *addr_len = 0;
  82. if (!addr)
  83. return false;
  84. ipv6 = (strchr(addr, ':') != NULL);
  85. out->ss_family = ipv6 ? AF_INET6 : AF_INET;
  86. *addr_len = sizeof(*out);
  87. #ifdef _WIN32
  88. int ret = WSAStringToAddressA((LPSTR)addr, out->ss_family, NULL,
  89. (LPSOCKADDR)out, addr_len);
  90. if (ret == SOCKET_ERROR)
  91. warn("Could not parse address, error code: %d", GetLastError());
  92. return ret != SOCKET_ERROR;
  93. #else
  94. struct sockaddr_in *sin = (struct sockaddr_in *)out;
  95. if (inet_pton(out->ss_family, addr, &sin->sin_addr)) {
  96. *addr_len = ipv6 ?
  97. sizeof(struct sockaddr_in6) :
  98. sizeof(struct sockaddr_in);
  99. return true;
  100. }
  101. return false;
  102. #endif
  103. }
  104. #ifndef _WIN32
  105. static inline bool is_loopback(struct ifaddrs *ifa)
  106. {
  107. const char *n = ifa->ifa_name;
  108. return n && (strcmp(n, "lo") == 0 || strcmp(n, "lo0") == 0);
  109. }
  110. static inline void netif_get_addrs_nix(struct netif_saddr_data *ifaddrs)
  111. {
  112. struct ifaddrs *ifaddr, *ifa;
  113. unsigned int family, s;
  114. char host[NI_MAXHOST];
  115. if (getifaddrs(&ifaddr) == -1) {
  116. warn("getifaddrs() failed");
  117. return;
  118. }
  119. for (ifa = ifaddr; ifa != NULL; ifa = ifa->ifa_next) {
  120. if (ifa->ifa_addr == NULL || is_loopback(ifa))
  121. continue;
  122. family = ifa->ifa_addr->sa_family;
  123. if ((family == AF_INET) || (family == AF_INET6)) {
  124. s = getnameinfo(ifa->ifa_addr,
  125. (family == AF_INET) ?
  126. sizeof(struct sockaddr_in) :
  127. sizeof(struct sockaddr_in6),
  128. host, NI_MAXHOST, NULL, 0, NI_NUMERICHOST);
  129. if (s != 0) {
  130. warn("getnameinfo() failed: %s",
  131. gai_strerror(s));
  132. continue;
  133. }
  134. netif_push(ifa->ifa_addr, ifaddrs, ifa->ifa_name);
  135. }
  136. }
  137. freeifaddrs(ifaddr);
  138. }
  139. #else
  140. static inline PIP_ADAPTER_ADDRESSES get_adapters(void)
  141. {
  142. PIP_ADAPTER_ADDRESSES adapter = NULL;
  143. unsigned long ret = 0;
  144. unsigned long out_buf_len = 4096;
  145. unsigned long flags =
  146. GAA_FLAG_SKIP_ANYCAST |
  147. GAA_FLAG_SKIP_MULTICAST |
  148. GAA_FLAG_SKIP_DNS_SERVER;
  149. const int max_tries = 3;
  150. int i = 0;
  151. do {
  152. adapter = (IP_ADAPTER_ADDRESSES*)bmalloc(out_buf_len);
  153. if (!adapter)
  154. return NULL;
  155. ret = GetAdaptersAddresses(AF_UNSPEC, flags, NULL, adapter,
  156. &out_buf_len);
  157. if (ret == ERROR_BUFFER_OVERFLOW) {
  158. bfree(adapter);
  159. adapter = NULL;
  160. } else {
  161. break;
  162. }
  163. i++;
  164. } while ((ret == ERROR_BUFFER_OVERFLOW) && (i < max_tries));
  165. if (ret != NO_ERROR && ret != ERROR_NO_DATA) {
  166. LPSTR msg_buf = NULL;
  167. bfree(adapter);
  168. adapter = NULL;
  169. FormatMessageA(
  170. FORMAT_MESSAGE_ALLOCATE_BUFFER |
  171. FORMAT_MESSAGE_FROM_SYSTEM |
  172. FORMAT_MESSAGE_IGNORE_INSERTS,
  173. NULL, ret,
  174. MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
  175. msg_buf, 0, NULL);
  176. if (msg_buf) {
  177. warn("Call to GetAdaptersAddresses failed: %s (%d)",
  178. msg_buf, ret);
  179. LocalFree(msg_buf);
  180. }
  181. }
  182. return adapter;
  183. }
  184. static inline void netif_get_addrs_win32(struct netif_saddr_data *ifaddrs)
  185. {
  186. PIP_ADAPTER_ADDRESSES adapter = get_adapters();
  187. PIP_ADAPTER_UNICAST_ADDRESS unicast = NULL;
  188. PIP_ADAPTER_ADDRESSES cur_adap = NULL;
  189. SOCKET_ADDRESS socket_addr;
  190. int family;
  191. if (!adapter)
  192. return;
  193. for (cur_adap = adapter; !!cur_adap; cur_adap = cur_adap->Next) {
  194. char *adap_name = NULL;
  195. if (cur_adap->OperStatus != IfOperStatusUp ||
  196. cur_adap->IfType == IF_TYPE_SOFTWARE_LOOPBACK)
  197. continue;
  198. os_wcs_to_utf8_ptr(cur_adap->FriendlyName, 0, &adap_name);
  199. unicast = cur_adap->FirstUnicastAddress;
  200. for (; !!unicast; unicast = unicast->Next) {
  201. socket_addr = unicast->Address;
  202. family = socket_addr.lpSockaddr->sa_family;
  203. if (family == AF_INET || family == AF_INET6)
  204. netif_push(socket_addr.lpSockaddr, ifaddrs,
  205. adap_name);
  206. }
  207. bfree(adap_name);
  208. }
  209. bfree(adapter);
  210. }
  211. #endif
  212. void netif_get_addrs(struct netif_saddr_data *ifaddrs)
  213. {
  214. da_init(ifaddrs->addrs);
  215. #ifdef _WIN32
  216. netif_get_addrs_win32(ifaddrs);
  217. #else
  218. netif_get_addrs_nix(ifaddrs);
  219. #endif
  220. }