net-if.c 7.4 KB

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