2 * Utility functions to get infos from interfaces
4 * Copyright 2016, Dario Lombardo
6 * Wireshark - Network traffic analyzer
7 * By Gerald Combs <gerald@wireshark.org>
8 * Copyright 1998 Gerald Combs
10 * This program is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU General Public License
12 * as published by the Free Software Foundation; either version 2
13 * of the License, or (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
27 #include "interface.h"
30 #include <wsutil/inet_addr.h>
32 #ifdef HAVE_SYS_TYPES_H
33 #include <sys/types.h>
36 #ifdef HAVE_SYS_SOCKET_H
37 #include <sys/socket.h>
40 #ifdef HAVE_NETINET_IN_H
41 #include <netinet/in.h>
44 #ifdef HAVE_ARPA_INET_H
45 #include <arpa/inet.h>
58 #define WORKING_BUFFER_SIZE 15000
60 #ifdef HAVE_GETIFADDRS
61 static GSList* local_interfaces_to_list_nix(void)
63 GSList *interfaces = NULL;
67 char ip[INET6_ADDRSTRLEN];
69 if (getifaddrs(&ifap)) {
73 for (ifa = ifap; ifa != NULL; ifa = ifa->ifa_next) {
74 if (ifa->ifa_addr == NULL)
77 family = ifa->ifa_addr->sa_family;
79 memset(ip, 0x0, INET6_ADDRSTRLEN);
84 struct sockaddr_in *addr4 = (struct sockaddr_in *)ifa->ifa_addr;
85 ws_inet_ntop4(&addr4->sin_addr, ip, sizeof(ip));
91 struct sockaddr_in6 *addr6 = (struct sockaddr_in6 *)ifa->ifa_addr;
92 ws_inet_ntop6(&addr6->sin6_addr, ip, sizeof(ip));
100 /* skip loopback addresses */
101 if (!g_strcmp0(ip, "127.0.0.1") || !g_strcmp0(ip, "::1"))
105 interfaces = g_slist_prepend(interfaces, g_strdup(ip));
112 #endif /* HAVE_GETIFADDRS */
115 static GSList* local_interfaces_to_list_win(void)
117 GSList *interfaces = NULL;
118 PIP_ADAPTER_ADDRESSES pAddresses = NULL;
119 ULONG outBufLen = WORKING_BUFFER_SIZE;
120 ULONG family = AF_UNSPEC;
121 PIP_ADAPTER_ADDRESSES pCurrAddresses = NULL;
122 PIP_ADAPTER_UNICAST_ADDRESS pUnicast = NULL;
126 pAddresses = (IP_ADAPTER_ADDRESSES *)g_malloc0(outBufLen);
127 if (pAddresses == NULL)
130 if (GetAdaptersAddresses(family, 0, NULL, pAddresses, &outBufLen) != NO_ERROR)
133 pCurrAddresses = pAddresses;
134 while (pCurrAddresses) {
136 for (pUnicast = pCurrAddresses->FirstUnicastAddress; pUnicast != NULL; pUnicast = pUnicast->Next) {
137 if (pUnicast->Address.lpSockaddr->sa_family == AF_INET) {
138 SOCKADDR_IN* sa_in = (SOCKADDR_IN *)pUnicast->Address.lpSockaddr;
139 ws_inet_ntop4(&(sa_in->sin_addr), ip, iplen);
140 if (!g_strcmp0(ip, "127.0.0.1"))
143 interfaces = g_slist_prepend(interfaces, g_strdup(ip));
145 if (pUnicast->Address.lpSockaddr->sa_family == AF_INET6) {
146 SOCKADDR_IN6* sa_in6 = (SOCKADDR_IN6 *)pUnicast->Address.lpSockaddr;
147 ws_inet_ntop6(&(sa_in6->sin6_addr), ip, iplen);
148 if (!g_strcmp0(ip, "::1"))
151 interfaces = g_slist_prepend(interfaces, g_strdup(ip));
154 pCurrAddresses = pCurrAddresses->Next;
164 GSList* local_interfaces_to_list(void)
167 return local_interfaces_to_list_win();
168 #elif defined(HAVE_GETIFADDRS)
169 return local_interfaces_to_list_nix();
176 * Editor modelines - https://www.wireshark.org/tools/modelines.html
181 * indent-tabs-mode: t
184 * vi: set shiftwidth=4 tabstop=8 noexpandtab:
185 * :indentSize=4:tabSize=8:noTabs=false: