diff options
author | Dario Lombardo <lomato@gmail.com> | 2016-08-24 16:48:39 +0200 |
---|---|---|
committer | Dario Lombardo <lomato@gmail.com> | 2016-08-26 21:04:40 +0000 |
commit | 139170c6c6b29cf9c1eba9bbcacfbffdf447e2ef (patch) | |
tree | 7a9be7b596f947e980e445caa3b321c6f7d5d6e3 /wsutil/interface.c | |
parent | d0565ad22b14eae64776fb7cf04377e902380ef9 (diff) |
wsutil: add enumeration of local ip addresses in Windows.
Routine used by ssh-based extcaps.
Change-Id: I06d8e1e1444cd03a0508dc0c7cb91d340a451c58
Reviewed-on: https://code.wireshark.org/review/17308
Petri-Dish: Dario Lombardo <lomato@gmail.com>
Tested-by: Petri Dish Buildbot <buildbot-no-reply@wireshark.org>
Reviewed-by: Roland Knall <rknall@gmail.com>
Reviewed-by: João Valverde <j@v6e.pt>
Reviewed-by: Dario Lombardo <lomato@gmail.com>
Diffstat (limited to 'wsutil/interface.c')
-rw-r--r-- | wsutil/interface.c | 73 |
1 files changed, 71 insertions, 2 deletions
diff --git a/wsutil/interface.c b/wsutil/interface.c index 4cd8b5954c..11104f1eba 100644 --- a/wsutil/interface.c +++ b/wsutil/interface.c @@ -49,10 +49,18 @@ #include <ifaddrs.h> #endif -GSList *local_interfaces_to_list(void) +#ifdef _WIN32 + #include <winsock2.h> + #include <iphlpapi.h> + #include <Ws2tcpip.h> +#endif + +#define WORKING_BUFFER_SIZE 15000 + +#ifdef HAVE_GETIFADDRS +static GSList* local_interfaces_to_list_nix(void) { GSList *interfaces = NULL; -#ifdef HAVE_GETIFADDRS struct ifaddrs *ifap; struct ifaddrs *ifa; int family; @@ -99,9 +107,70 @@ GSList *local_interfaces_to_list(void) } freeifaddrs(ifap); end: + return interfaces; +} #endif /* HAVE_GETIFADDRS */ + +#ifdef _WIN32 +static GSList* local_interfaces_to_list_win(void) +{ + GSList *interfaces = NULL; + PIP_ADAPTER_ADDRESSES pAddresses = NULL; + ULONG outBufLen = WORKING_BUFFER_SIZE; + ULONG family = AF_UNSPEC; + PIP_ADAPTER_ADDRESSES pCurrAddresses = NULL; + PIP_ADAPTER_UNICAST_ADDRESS pUnicast = NULL; + char ip[100]; + guint iplen = 100; + + pAddresses = (IP_ADAPTER_ADDRESSES *)g_malloc0(outBufLen); + if (pAddresses == NULL) + return NULL; + + if (GetAdaptersAddresses(family, 0, NULL, pAddresses, &outBufLen) != NO_ERROR) + goto end; + + pCurrAddresses = pAddresses; + while (pCurrAddresses) { + + for (pUnicast = pCurrAddresses->FirstUnicastAddress; pUnicast != NULL; pUnicast = pUnicast->Next) { + if (pUnicast->Address.lpSockaddr->sa_family == AF_INET) { + SOCKADDR_IN* sa_in = (SOCKADDR_IN *)pUnicast->Address.lpSockaddr; + ws_inet_ntop4(&(sa_in->sin_addr), ip, iplen); + if (!g_strcmp0(ip, "127.0.0.1")) + continue; + if (*ip) + interfaces = g_slist_prepend(interfaces, g_strdup(ip)); + } + if (pUnicast->Address.lpSockaddr->sa_family == AF_INET6) { + SOCKADDR_IN6* sa_in6 = (SOCKADDR_IN6 *)pUnicast->Address.lpSockaddr; + ws_inet_ntop6(&(sa_in6->sin6_addr), ip, iplen); + if (!g_strcmp0(ip, "::1")) + continue; + if (*ip) + interfaces = g_slist_prepend(interfaces, g_strdup(ip)); + } + } + pCurrAddresses = pCurrAddresses->Next; + } +end: + if (pAddresses) + g_free(pAddresses); + return interfaces; } +#endif /* _WIN32 */ + +GSList* local_interfaces_to_list(void) +{ +#if defined(_WIN32) + return local_interfaces_to_list_win(); +#elif defined(HAVE_GETIFADDRS) + return local_interfaces_to_list_nix(); +#else + return NULL; +#endif +} /* * Editor modelines - https://www.wireshark.org/tools/modelines.html |