net-if.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  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. #define _WINSOCK_DEPRECATED_NO_WARNINGS
  15. #include "net-if.h"
  16. #include <util/platform.h>
  17. #include <util/dstr.h>
  18. #define do_log(level, format, ...) 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, const char *ip, const char *adapter)
  23. {
  24. struct netif_saddr_item item;
  25. struct dstr full_name = {0};
  26. char *ip_dup = bstrdup(ip);
  27. if (adapter && *adapter)
  28. dstr_printf(&full_name, "[%s] %s", adapter, ip);
  29. else
  30. dstr_copy(&full_name, ip);
  31. item.name = full_name.array;
  32. item.addr = ip_dup;
  33. da_push_back(sd->addrs, &item);
  34. }
  35. static void netif_convert_to_string(char *dest, struct sockaddr_storage *byte_address)
  36. {
  37. int family = byte_address->ss_family;
  38. char temp_char[INET6_ADDRSTRLEN] = {0};
  39. #ifndef _WIN32
  40. if (family == AF_INET)
  41. inet_ntop(family, &(((struct sockaddr_in *)byte_address)->sin_addr), temp_char, INET6_ADDRSTRLEN);
  42. else if (family == AF_INET6)
  43. inet_ntop(family, &(((struct sockaddr_in6 *)byte_address)->sin6_addr), temp_char, INET6_ADDRSTRLEN);
  44. #else
  45. if (family == AF_INET)
  46. InetNtopA(family, &(((SOCKADDR_IN *)byte_address)->sin_addr), temp_char, INET6_ADDRSTRLEN);
  47. else if (family == AF_INET6)
  48. InetNtopA(family, &(((SOCKADDR_IN6 *)byte_address)->sin6_addr), temp_char, INET6_ADDRSTRLEN);
  49. #endif
  50. strncpy(dest, temp_char, INET6_ADDRSTRLEN);
  51. }
  52. static void netif_push(struct sockaddr *copy_source, struct netif_saddr_data *saddr_d, const char *adapter)
  53. {
  54. char temp_char[INET6_ADDRSTRLEN] = {0};
  55. struct sockaddr_storage sa = {0};
  56. if (copy_source->sa_family == AF_INET)
  57. memcpy(&sa, copy_source, sizeof(struct sockaddr_in));
  58. else if (copy_source->sa_family == AF_INET6)
  59. memcpy(&sa, copy_source, sizeof(struct sockaddr_in6));
  60. netif_convert_to_string(temp_char, &sa);
  61. netif_saddr_data_push_back(saddr_d, temp_char, adapter);
  62. }
  63. void netif_log_saddrs(struct netif_saddr_data *sd)
  64. {
  65. for (size_t i = 0; i < sd->addrs.num; i++)
  66. info("\t\t%s", sd->addrs.array[i].name);
  67. }
  68. bool netif_addr_to_str(struct sockaddr_storage *in, char *addr, int addr_len)
  69. {
  70. if (!in || !addr)
  71. return false;
  72. if (in->ss_family != AF_INET6 && in->ss_family != AF_INET)
  73. return false;
  74. if ((in->ss_family == AF_INET6 && addr_len < INET6_ADDRSTRLEN) ||
  75. (in->ss_family == AF_INET && addr_len < INET_ADDRSTRLEN))
  76. return false;
  77. memset(addr, 0, addr_len);
  78. netif_convert_to_string(addr, in);
  79. return true;
  80. }
  81. bool netif_str_to_addr(struct sockaddr_storage *out, int *addr_len, const char *addr)
  82. {
  83. bool ipv6;
  84. memset(out, 0, sizeof(*out));
  85. *addr_len = 0;
  86. if (!addr)
  87. return false;
  88. ipv6 = (strchr(addr, ':') != NULL);
  89. out->ss_family = ipv6 ? AF_INET6 : AF_INET;
  90. *addr_len = sizeof(*out);
  91. #ifdef _WIN32
  92. int ret = WSAStringToAddressA((LPSTR)addr, out->ss_family, NULL, (LPSOCKADDR)out, addr_len);
  93. if (ret == SOCKET_ERROR)
  94. warn("Could not parse address, error code: %d", GetLastError());
  95. return ret != SOCKET_ERROR;
  96. #else
  97. *addr_len = ipv6 ? sizeof(struct sockaddr_in6) : sizeof(struct sockaddr_in);
  98. void *dst = NULL;
  99. if (ipv6)
  100. dst = &((struct sockaddr_in6 *)out)->sin6_addr;
  101. else
  102. dst = &((struct sockaddr_in *)out)->sin_addr;
  103. if (inet_pton(out->ss_family, addr, dst))
  104. return true;
  105. return false;
  106. #endif
  107. }
  108. #ifndef _WIN32
  109. static inline bool is_loopback(struct ifaddrs *ifa)
  110. {
  111. const char *n = ifa->ifa_name;
  112. return n && (strcmp(n, "lo") == 0 || strcmp(n, "lo0") == 0);
  113. }
  114. static inline void netif_get_addrs_nix(struct netif_saddr_data *ifaddrs)
  115. {
  116. struct ifaddrs *ifaddr, *ifa;
  117. unsigned int family, s;
  118. char host[NI_MAXHOST];
  119. if (getifaddrs(&ifaddr) == -1) {
  120. warn("getifaddrs() failed");
  121. return;
  122. }
  123. for (ifa = ifaddr; ifa != NULL; ifa = ifa->ifa_next) {
  124. if (ifa->ifa_addr == NULL || is_loopback(ifa))
  125. continue;
  126. family = ifa->ifa_addr->sa_family;
  127. if ((family == AF_INET) || (family == AF_INET6)) {
  128. s = getnameinfo(ifa->ifa_addr,
  129. (family == AF_INET) ? sizeof(struct sockaddr_in) : sizeof(struct sockaddr_in6),
  130. host, NI_MAXHOST, NULL, 0, NI_NUMERICHOST);
  131. if (s != 0) {
  132. warn("getnameinfo() failed: %s", gai_strerror(s));
  133. continue;
  134. }
  135. netif_push(ifa->ifa_addr, ifaddrs, ifa->ifa_name);
  136. }
  137. }
  138. freeifaddrs(ifaddr);
  139. }
  140. #else
  141. static inline PIP_ADAPTER_ADDRESSES get_adapters(void)
  142. {
  143. PIP_ADAPTER_ADDRESSES adapter = NULL;
  144. unsigned long ret = 0;
  145. unsigned long out_buf_len = 16384;
  146. unsigned long flags = GAA_FLAG_SKIP_ANYCAST | GAA_FLAG_SKIP_MULTICAST | GAA_FLAG_SKIP_DNS_SERVER;
  147. const int max_tries = 3;
  148. int i = 0;
  149. do {
  150. adapter = (IP_ADAPTER_ADDRESSES *)bmalloc(out_buf_len);
  151. if (!adapter)
  152. return NULL;
  153. ret = GetAdaptersAddresses(AF_UNSPEC, flags, NULL, adapter, &out_buf_len);
  154. if (ret == ERROR_BUFFER_OVERFLOW) {
  155. bfree(adapter);
  156. adapter = NULL;
  157. } else {
  158. break;
  159. }
  160. i++;
  161. } while ((ret == ERROR_BUFFER_OVERFLOW) && (i < max_tries));
  162. if (ret != NO_ERROR && ret != ERROR_NO_DATA) {
  163. LPSTR msg_buf = NULL;
  164. bfree(adapter);
  165. adapter = NULL;
  166. FormatMessageA(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM |
  167. FORMAT_MESSAGE_IGNORE_INSERTS,
  168. NULL, ret, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (LPSTR)&msg_buf, 0, NULL);
  169. if (msg_buf) {
  170. warn("Call to GetAdaptersAddresses failed: %s (%d)", msg_buf, ret);
  171. LocalFree(msg_buf);
  172. }
  173. }
  174. return adapter;
  175. }
  176. static inline void netif_get_addrs_win32(struct netif_saddr_data *ifaddrs)
  177. {
  178. PIP_ADAPTER_ADDRESSES adapter = get_adapters();
  179. PIP_ADAPTER_UNICAST_ADDRESS unicast = NULL;
  180. PIP_ADAPTER_ADDRESSES cur_adap = NULL;
  181. SOCKET_ADDRESS socket_addr;
  182. int family;
  183. if (!adapter)
  184. return;
  185. for (cur_adap = adapter; !!cur_adap; cur_adap = cur_adap->Next) {
  186. char *adap_name = NULL;
  187. if (cur_adap->OperStatus != IfOperStatusUp || cur_adap->IfType == IF_TYPE_SOFTWARE_LOOPBACK)
  188. continue;
  189. os_wcs_to_utf8_ptr(cur_adap->FriendlyName, 0, &adap_name);
  190. unicast = cur_adap->FirstUnicastAddress;
  191. for (; !!unicast; unicast = unicast->Next) {
  192. socket_addr = unicast->Address;
  193. family = socket_addr.lpSockaddr->sa_family;
  194. if (family == AF_INET || family == AF_INET6)
  195. netif_push(socket_addr.lpSockaddr, ifaddrs, adap_name);
  196. }
  197. bfree(adap_name);
  198. }
  199. bfree(adapter);
  200. }
  201. #endif
  202. void netif_get_addrs(struct netif_saddr_data *ifaddrs)
  203. {
  204. da_init(ifaddrs->addrs);
  205. #ifdef _WIN32
  206. netif_get_addrs_win32(ifaddrs);
  207. #else
  208. netif_get_addrs_nix(ifaddrs);
  209. #endif
  210. }