2 * Routines for getting interface information from dumpcap
4 * Wireshark - Network traffic analyzer
5 * By Gerald Combs <gerald@wireshark.org>
6 * Copyright 1998 Gerald Combs
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License
10 * as published by the Free Software Foundation; either version 2
11 * of the License, or (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
31 #ifdef HAVE_ARPA_INET_H
32 #include <arpa/inet.h>
35 #ifdef HAVE_SYS_SOCKET_H
36 #include <sys/socket.h> /* needed to define AF_ values on UNIX */
39 #ifdef HAVE_WINSOCK2_H
40 #include <winsock2.h> /* needed to define AF_ values on Windows */
43 #ifdef NEED_INET_V6DEFS_H
44 # include "wsutil/inet_v6defs.h"
49 #include "capture_opts.h"
50 #include "capture_session.h"
51 #include "capture_sync.h"
54 #include "capture_ifinfo.h"
56 #ifdef HAVE_PCAP_REMOTE
57 static GList *remote_interface_list = NULL;
59 static void append_remote_list(GList *iflist)
63 if_addr_t *if_addr, *temp_addr;
64 if_info_t *if_info, *temp;
66 for (rlist = g_list_nth(remote_interface_list, 0); rlist != NULL; rlist = g_list_next(rlist)) {
67 if_info = (if_info_t *)rlist->data;
68 temp = g_malloc0(sizeof(if_info_t));
69 temp->name = g_strdup(if_info->name);
70 temp->friendly_name = g_strdup(if_info->friendly_name);
71 temp->vendor_description = g_strdup(if_info->vendor_description);
72 for (list = g_slist_nth(if_info->addrs, 0); list != NULL; list = g_slist_next(list)) {
73 temp_addr = g_malloc0(sizeof(if_addr_t));
74 if_addr = (if_addr_t *)list->data;
76 temp_addr->ifat_type = if_addr->ifat_type;
77 if (temp_addr->ifat_type == IF_AT_IPv4) {
78 temp_addr->addr.ip4_addr = if_addr->addr.ip4_addr;
80 memcpy(temp_addr->addr.ip6_addr, if_addr->addr.ip6_addr, sizeof(if_addr->addr));
87 temp->addrs = g_slist_append(temp->addrs, temp_addr);
90 temp->loopback = if_info->loopback;
91 iflist = g_list_append(iflist, temp);
97 * Fetch the interface list from a child process (dumpcap).
99 * @return A GList containing if_info_t structs if successful, NULL (with err and possibly err_str set) otherwise.
103 /* XXX - We parse simple text output to get our interface list. Should
104 * we use "real" data serialization instead, e.g. via XML? */
106 capture_interface_list(int *err, char **err_str, void (*update_cb)(void))
109 GList *if_list = NULL;
111 gchar *data, *primary_msg, *secondary_msg;
112 gchar **raw_list, **if_parts, **addr_parts;
117 g_log(LOG_DOMAIN_CAPTURE, G_LOG_LEVEL_MESSAGE, "Capture Interface List ...");
119 /* Try to get our interface list */
120 ret = sync_interface_list_open(&data, &primary_msg, &secondary_msg, update_cb);
122 g_log(LOG_DOMAIN_CAPTURE, G_LOG_LEVEL_MESSAGE, "Capture Interface List failed!");
124 *err_str = primary_msg;
128 g_free(secondary_msg);
129 *err = CANT_GET_INTERFACE_LIST;
133 /* Split our lines */
135 raw_list = g_strsplit(data, "\r\n", 0);
137 raw_list = g_strsplit(data, "\n", 0);
141 for (i = 0; raw_list[i] != NULL; i++) {
142 if_parts = g_strsplit(raw_list[i], "\t", 6);
143 if (if_parts[0] == NULL || if_parts[1] == NULL || if_parts[2] == NULL ||
144 if_parts[3] == NULL || if_parts[4] == NULL || if_parts[5] == NULL) {
145 g_strfreev(if_parts);
149 /* Number followed by the name, e.g "1. eth0" */
150 name = strchr(if_parts[0], ' ');
154 g_strfreev(if_parts);
158 if_info = g_new0(if_info_t,1);
159 if_info->name = g_strdup(name);
160 if (strlen(if_parts[1]) > 0)
161 if_info->vendor_description = g_strdup(if_parts[1]);
162 if (strlen(if_parts[2]) > 0)
163 if_info->friendly_name = g_strdup(if_parts[2]);
164 if_info->type = (interface_type)(int)strtol(if_parts[3], NULL, 10);
165 addr_parts = g_strsplit(if_parts[4], ",", 0);
166 for (j = 0; addr_parts[j] != NULL; j++) {
167 if_addr = g_new0(if_addr_t,1);
168 if (inet_pton(AF_INET, addr_parts[j], &if_addr->addr.ip4_addr) > 0) {
169 if_addr->ifat_type = IF_AT_IPv4;
170 } else if (inet_pton(AF_INET6, addr_parts[j],
171 &if_addr->addr.ip6_addr) > 0) {
172 if_addr->ifat_type = IF_AT_IPv6;
178 if_info->addrs = g_slist_append(if_info->addrs, if_addr);
181 if (strcmp(if_parts[5], "loopback") == 0)
182 if_info->loopback = TRUE;
183 g_strfreev(if_parts);
184 g_strfreev(addr_parts);
185 if_list = g_list_append(if_list, if_info);
187 g_strfreev(raw_list);
189 /* Check to see if we built a list */
190 if (if_list == NULL) {
191 *err = NO_INTERFACES_FOUND;
193 *err_str = g_strdup("No interfaces found");
195 #ifdef HAVE_PCAP_REMOTE
196 if (remote_interface_list && g_list_length(remote_interface_list) > 0) {
197 append_remote_list(if_list);
203 /* XXX - We parse simple text output to get our interface list. Should
204 * we use "real" data serialization instead, e.g. via XML? */
206 capture_get_if_capabilities(const gchar *ifname, gboolean monitor_mode,
207 char **err_str, void (*update_cb)(void))
209 if_capabilities_t *caps;
210 GList *linktype_list = NULL;
212 gchar *data, *primary_msg, *secondary_msg;
213 gchar **raw_list, **lt_parts;
214 data_link_info_t *data_link_info;
216 g_log(LOG_DOMAIN_CAPTURE, G_LOG_LEVEL_MESSAGE, "Capture Interface Capabilities ...");
218 /* Try to get our interface list */
219 err = sync_if_capabilities_open(ifname, monitor_mode, &data,
220 &primary_msg, &secondary_msg, update_cb);
222 g_log(LOG_DOMAIN_CAPTURE, G_LOG_LEVEL_MESSAGE, "Capture Interface Capabilities failed!");
224 *err_str = primary_msg;
228 g_free(secondary_msg);
232 /* Split our lines */
234 raw_list = g_strsplit(data, "\r\n", 0);
236 raw_list = g_strsplit(data, "\n", 0);
241 * First line is 0 if monitor mode isn't supported, 1 if it is.
243 if (raw_list[0] == NULL || *raw_list[0] == '\0') {
244 g_log(LOG_DOMAIN_CAPTURE, G_LOG_LEVEL_MESSAGE, "Capture Interface Capabilities returned no information!");
246 *err_str = g_strdup("Dumpcap returned no interface capability information");
252 * Allocate the interface capabilities structure.
254 caps = (if_capabilities_t *)g_malloc(sizeof *caps);
255 switch (*raw_list[0]) {
258 caps->can_set_rfmon = FALSE;
262 caps->can_set_rfmon = TRUE;
266 g_log(LOG_DOMAIN_CAPTURE, G_LOG_LEVEL_MESSAGE, "Capture Interface Capabilities returned bad information!");
268 *err_str = g_strdup_printf("Dumpcap returned \"%s\" for monitor-mode capability",
276 * The rest are link-layer types.
278 for (i = 1; raw_list[i] != NULL; i++) {
279 /* ...and what if the interface name has a tab in it, Mr. Clever Programmer? */
280 lt_parts = g_strsplit(raw_list[i], "\t", 3);
281 if (lt_parts[0] == NULL || lt_parts[1] == NULL || lt_parts[2] == NULL) {
282 g_strfreev(lt_parts);
286 data_link_info = g_new(data_link_info_t,1);
287 data_link_info->dlt = (int) strtol(lt_parts[0], NULL, 10);
288 data_link_info->name = g_strdup(lt_parts[1]);
289 if (strcmp(lt_parts[2], "(not supported)") != 0)
290 data_link_info->description = g_strdup(lt_parts[2]);
292 data_link_info->description = NULL;
294 linktype_list = g_list_append(linktype_list, data_link_info);
296 g_strfreev(raw_list);
298 /* Check to see if we built a list */
299 if (linktype_list == NULL) {
302 *err_str = g_strdup("Dumpcap returned no link-layer types");
306 caps->data_link_types = linktype_list;
310 #ifdef HAVE_PCAP_REMOTE
311 void add_interface_to_remote_list(if_info_t *if_info)
314 if_addr_t *if_addr, *temp_addr;
316 if_info_t *temp = g_malloc0(sizeof(if_info_t));
317 temp->name = g_strdup(if_info->name);
318 temp->friendly_name = g_strdup(if_info->friendly_name);
319 temp->vendor_description = g_strdup(if_info->vendor_description);
320 for (list = g_slist_nth(if_info->addrs, 0); list != NULL; list = g_slist_next(list)) {
321 temp_addr = g_malloc0(sizeof(if_addr_t));
322 if_addr = (if_addr_t *)list->data;
324 temp_addr->ifat_type = if_addr->ifat_type;
325 if (temp_addr->ifat_type == IF_AT_IPv4) {
326 temp_addr->addr.ip4_addr = if_addr->addr.ip4_addr;
328 memcpy(temp_addr->addr.ip6_addr, if_addr->addr.ip6_addr, sizeof(if_addr->addr));
335 temp->addrs = g_slist_append(temp->addrs, temp_addr);
338 temp->loopback = if_info->loopback;
339 remote_interface_list = g_list_append(remote_interface_list, temp);
342 #endif /* HAVE_LIBPCAP */