nexmon – Blame information for rev 1

Subversion Repositories:
Rev:
Rev Author Line No. Line
1 office 1 /* interface.c
2 * Utility functions to get infos from interfaces
3 *
4 * Copyright 2016, Dario Lombardo
5 *
6 * Wireshark - Network traffic analyzer
7 * By Gerald Combs <gerald@wireshark.org>
8 * Copyright 1998 Gerald Combs
9 *
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.
14 *
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.
19 *
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.
23 */
24  
25 #include "config.h"
26  
27 #include "interface.h"
28  
29 #include <string.h>
30 #include <wsutil/inet_addr.h>
31  
32 #ifdef HAVE_SYS_TYPES_H
33 #include <sys/types.h>
34 #endif
35  
36 #ifdef HAVE_SYS_SOCKET_H
37 #include <sys/socket.h>
38 #endif
39  
40 #ifdef HAVE_NETINET_IN_H
41 #include <netinet/in.h>
42 #endif
43  
44 #ifdef HAVE_ARPA_INET_H
45 #include <arpa/inet.h>
46 #endif
47  
48 #ifdef HAVE_IFADDRS_H
49 #include <ifaddrs.h>
50 #endif
51  
52 GSList *local_interfaces_to_list(void)
53 {
54 GSList *interfaces = NULL;
55 #ifdef HAVE_GETIFADDRS
56 struct ifaddrs *ifap;
57 struct ifaddrs *ifa;
58 int family;
59 char ip[INET6_ADDRSTRLEN];
60  
61 if (getifaddrs(&ifap)) {
62 goto end;
63 }
64  
65 for (ifa = ifap; ifa != NULL; ifa = ifa->ifa_next) {
66 if (ifa->ifa_addr == NULL)
67 continue;
68  
69 family = ifa->ifa_addr->sa_family;
70  
71 memset(ip, 0x0, INET6_ADDRSTRLEN);
72  
73 switch (family) {
74 case AF_INET:
75 {
76 struct sockaddr_in *addr4 = (struct sockaddr_in *)ifa->ifa_addr;
77 ws_inet_ntop4(&addr4->sin_addr, ip, sizeof(ip));
78 break;
79 }
80  
81 case AF_INET6:
82 {
83 struct sockaddr_in6 *addr6 = (struct sockaddr_in6 *)ifa->ifa_addr;
84 ws_inet_ntop6(&addr6->sin6_addr, ip, sizeof(ip));
85 break;
86 }
87  
88 default:
89 break;
90 }
91  
92 /* skip loopback addresses */
93 if (!g_strcmp0(ip, "127.0.0.1") || !g_strcmp0(ip, "::1"))
94 continue;
95  
96 if (*ip) {
97 interfaces = g_slist_prepend(interfaces, g_strdup(ip));
98 }
99 }
100 freeifaddrs(ifap);
101 end:
102 #endif /* HAVE_GETIFADDRS */
103 return interfaces;
104 }
105  
106 /*
107 * Editor modelines - https://www.wireshark.org/tools/modelines.html
108 *
109 * Local variables:
110 * c-basic-offset: 4
111 * tab-width: 8
112 * indent-tabs-mode: t
113 * End:
114 *
115 * vi: set shiftwidth=4 tabstop=8 noexpandtab:
116 * :indentSize=4:tabSize=8:noTabs=false:
117 */