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 * SPDX-License-Identifier: GPL-2.0-or-later
15 #include "interface.h"
18 #include <wsutil/inet_addr.h>
20 #include <sys/types.h>
22 #ifdef HAVE_SYS_SOCKET_H
23 #include <sys/socket.h>
26 #ifdef HAVE_NETINET_IN_H
27 #include <netinet/in.h>
30 #ifdef HAVE_ARPA_INET_H
31 #include <arpa/inet.h>
44 #define WORKING_BUFFER_SIZE 15000
46 #ifdef HAVE_GETIFADDRS
47 static GSList
* local_interfaces_to_list_nix(void)
49 GSList
*interfaces
= NULL
;
53 char ip
[WS_INET6_ADDRSTRLEN
];
55 if (getifaddrs(&ifap
)) {
59 for (ifa
= ifap
; ifa
!= NULL
; ifa
= ifa
->ifa_next
) {
60 if (ifa
->ifa_addr
== NULL
)
63 family
= ifa
->ifa_addr
->sa_family
;
65 memset(ip
, 0x0, WS_INET6_ADDRSTRLEN
);
70 struct sockaddr_in
*addr4
= (struct sockaddr_in
*)ifa
->ifa_addr
;
71 ws_inet_ntop4(&addr4
->sin_addr
, ip
, sizeof(ip
));
77 struct sockaddr_in6
*addr6
= (struct sockaddr_in6
*)ifa
->ifa_addr
;
78 ws_inet_ntop6(&addr6
->sin6_addr
, ip
, sizeof(ip
));
86 /* skip loopback addresses */
87 if (!g_strcmp0(ip
, "127.0.0.1") || !g_strcmp0(ip
, "::1"))
91 interfaces
= g_slist_prepend(interfaces
, g_strdup(ip
));
98 #endif /* HAVE_GETIFADDRS */
101 static GSList
* local_interfaces_to_list_win(void)
103 GSList
*interfaces
= NULL
;
104 PIP_ADAPTER_ADDRESSES pAddresses
= NULL
;
105 ULONG outBufLen
= WORKING_BUFFER_SIZE
;
106 ULONG family
= AF_UNSPEC
;
107 PIP_ADAPTER_ADDRESSES pCurrAddresses
= NULL
;
108 PIP_ADAPTER_UNICAST_ADDRESS pUnicast
= NULL
;
110 unsigned iplen
= 100;
112 pAddresses
= (IP_ADAPTER_ADDRESSES
*)g_malloc0(outBufLen
);
113 if (pAddresses
== NULL
)
116 if (GetAdaptersAddresses(family
, 0, NULL
, pAddresses
, &outBufLen
) != NO_ERROR
)
119 pCurrAddresses
= pAddresses
;
120 while (pCurrAddresses
) {
122 for (pUnicast
= pCurrAddresses
->FirstUnicastAddress
; pUnicast
!= NULL
; pUnicast
= pUnicast
->Next
) {
123 if (pUnicast
->Address
.lpSockaddr
->sa_family
== AF_INET
) {
124 SOCKADDR_IN
* sa_in
= (SOCKADDR_IN
*)pUnicast
->Address
.lpSockaddr
;
125 ws_inet_ntop4(&(sa_in
->sin_addr
), ip
, iplen
);
126 if (!g_strcmp0(ip
, "127.0.0.1"))
129 interfaces
= g_slist_prepend(interfaces
, g_strdup(ip
));
131 if (pUnicast
->Address
.lpSockaddr
->sa_family
== AF_INET6
) {
132 SOCKADDR_IN6
* sa_in6
= (SOCKADDR_IN6
*)pUnicast
->Address
.lpSockaddr
;
133 ws_inet_ntop6(&(sa_in6
->sin6_addr
), ip
, iplen
);
134 if (!g_strcmp0(ip
, "::1"))
137 interfaces
= g_slist_prepend(interfaces
, g_strdup(ip
));
140 pCurrAddresses
= pCurrAddresses
->Next
;
149 GSList
* local_interfaces_to_list(void)
152 return local_interfaces_to_list_win();
153 #elif defined(HAVE_GETIFADDRS)
154 return local_interfaces_to_list_nix();
161 * Editor modelines - https://www.wireshark.org/tools/modelines.html
166 * indent-tabs-mode: t
169 * vi: set shiftwidth=4 tabstop=8 noexpandtab:
170 * :indentSize=4:tabSize=8:noTabs=false: