Don't create a subtree that is not used.
[obnox/wireshark/wip.git] / capture_ifinfo.c
1 /* capture_ifinfo.c
2  * Routines for getting interface information from dumpcap
3  *
4  * $Id$
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., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
23  */
24
25 #ifdef HAVE_CONFIG_H
26 # include "config.h"
27 #endif
28
29 #ifdef HAVE_LIBPCAP
30
31 #include <stdlib.h>
32 #include <string.h>
33
34 #ifdef HAVE_ARPA_INET_H
35 #include <arpa/inet.h>
36 #endif
37
38 #ifdef HAVE_SYS_SOCKET_H
39 #include <sys/socket.h>         /* needed to define AF_ values on UNIX */
40 #endif
41
42 #ifdef HAVE_WINSOCK2_H
43 #include <winsock2.h>           /* needed to define AF_ values on Windows */
44 #endif
45
46 #ifdef NEED_INET_V6DEFS_H
47 # include "wsutil/inet_v6defs.h"
48 #endif
49
50 #include <glib.h>
51
52 #include "capture_opts.h"
53 #include "capture_sync.h"
54 #include "log.h"
55
56 #include "capture_ifinfo.h"
57
58 /**
59  * Fetch the interface list from a child process (dumpcap).
60  *
61  * @return A GList containing if_info_t structs if successful, NULL (with err and possibly err_str set) otherwise.
62  *
63  */
64
65 /* XXX - We parse simple text output to get our interface list.  Should
66  * we use "real" data serialization instead, e.g. via XML? */
67 GList *
68 capture_interface_list(int *err, char **err_str)
69 {
70     int        ret;
71     GList     *if_list = NULL;
72     int        i, j;
73     gchar     *data, *primary_msg, *secondary_msg;
74     gchar    **raw_list, **if_parts, **addr_parts;
75     gchar     *name;
76     if_info_t *if_info;
77     if_addr_t *if_addr;
78
79     g_log(LOG_DOMAIN_CAPTURE, G_LOG_LEVEL_MESSAGE, "Capture Interface List ...");
80
81     /* Try to get our interface list */
82     ret = sync_interface_list_open(&data, &primary_msg, &secondary_msg);
83     if (ret != 0) {
84         g_log(LOG_DOMAIN_CAPTURE, G_LOG_LEVEL_MESSAGE, "Capture Interface List failed!");
85         if (err_str) {
86             *err_str = primary_msg;
87         } else {
88             g_free(primary_msg);
89         }
90         g_free(secondary_msg);
91         *err = CANT_GET_INTERFACE_LIST;
92         return NULL;
93     }
94
95     /* Split our lines */
96 #ifdef _WIN32
97     raw_list = g_strsplit(data, "\r\n", 0);
98 #else
99     raw_list = g_strsplit(data, "\n", 0);
100 #endif
101     g_free(data);
102
103     for (i = 0; raw_list[i] != NULL; i++) {
104         if_parts = g_strsplit(raw_list[i], "\t", 4);
105         if (if_parts[0] == NULL || if_parts[1] == NULL || if_parts[2] == NULL ||
106                 if_parts[3] == NULL) {
107             g_strfreev(if_parts);
108             continue;
109         }
110
111         /* Number followed by the name, e.g "1. eth0" */
112         name = strchr(if_parts[0], ' ');
113         if (name) {
114             name++;
115         } else {
116             g_strfreev(if_parts);
117             continue;
118         }
119
120         if_info = g_malloc0(sizeof(if_info_t));
121         if_info->name = g_strdup(name);
122         if (strlen(if_parts[1]) > 0)
123             if_info->description = g_strdup(if_parts[1]);
124         addr_parts = g_strsplit(if_parts[2], ",", 0);
125         for (j = 0; addr_parts[j] != NULL; j++) {
126             if_addr = g_malloc0(sizeof(if_addr_t));
127             if (inet_pton(AF_INET, addr_parts[j], &if_addr->addr.ip4_addr)) {
128                 if_addr->ifat_type = IF_AT_IPv4;
129             } else if (inet_pton(AF_INET6, addr_parts[j],
130                     &if_addr->addr.ip6_addr)) {
131                 if_addr->ifat_type = IF_AT_IPv6;
132             } else {
133                 g_free(if_addr);
134                 if_addr = NULL;
135             }
136             if (if_addr) {
137                 if_info->addrs = g_slist_append(if_info->addrs, if_addr);
138             }
139         }
140         if (strcmp(if_parts[3], "loopback") == 0)
141             if_info->loopback = TRUE;
142         g_strfreev(if_parts);
143         g_strfreev(addr_parts);
144         if_list = g_list_append(if_list, if_info);
145     }
146     g_strfreev(raw_list);
147
148     /* Check to see if we built a list */
149     if (if_list == NULL) {
150         *err = NO_INTERFACES_FOUND;
151         if (err_str)
152             *err_str = g_strdup("No interfaces found");
153     }
154     return if_list;
155 }
156
157 /* XXX - We parse simple text output to get our interface list.  Should
158  * we use "real" data serialization instead, e.g. via XML? */
159 if_capabilities_t *
160 capture_get_if_capabilities(const gchar *ifname, gboolean monitor_mode,
161                             char **err_str)
162 {
163     if_capabilities_t *caps;
164     GList              *linktype_list = NULL;
165     int                 err, i;
166     gchar              *data, *primary_msg, *secondary_msg;
167     gchar             **raw_list, **lt_parts;
168     data_link_info_t   *data_link_info;
169
170     g_log(LOG_DOMAIN_CAPTURE, G_LOG_LEVEL_MESSAGE, "Capture Interface Capabilities ...");
171
172     /* Try to get our interface list */
173     err = sync_if_capabilities_open(ifname, monitor_mode, &data,
174                                     &primary_msg, &secondary_msg);
175     if (err != 0) {
176         g_log(LOG_DOMAIN_CAPTURE, G_LOG_LEVEL_MESSAGE, "Capture Interface Capabilities failed!");
177         if (err_str) {
178             *err_str = primary_msg;
179         } else {
180             g_free(primary_msg);
181         }
182         g_free(secondary_msg);
183         return NULL;
184     }
185
186     /* Split our lines */
187 #ifdef _WIN32
188     raw_list = g_strsplit(data, "\r\n", 0);
189 #else
190     raw_list = g_strsplit(data, "\n", 0);
191 #endif
192     g_free(data);
193
194     /*
195      * First line is 0 if monitor mode isn't supported, 1 if it is.
196      */
197     if (raw_list[0] == NULL || *raw_list[0] == '\0') {
198         g_log(LOG_DOMAIN_CAPTURE, G_LOG_LEVEL_MESSAGE, "Capture Interface Capabilities returned no information!");
199         if (err_str) {
200             *err_str = g_strdup("Dumpcap returned no interface capability information");
201         }
202         return NULL;
203     }
204
205     /*
206      * Allocate the interface capabilities structure.
207      */
208     caps = g_malloc(sizeof *caps);
209     switch (*raw_list[0]) {
210
211     case '0':
212         caps->can_set_rfmon = FALSE;
213         break;
214
215     case '1':
216         caps->can_set_rfmon = TRUE;
217         break;
218
219     default:
220         g_log(LOG_DOMAIN_CAPTURE, G_LOG_LEVEL_MESSAGE, "Capture Interface Capabilities returned bad information!");
221         if (err_str) {
222             *err_str = g_strdup_printf("Dumpcap returned \"%s\" for monitor-mode capability",
223                                        raw_list[0]);
224         }
225         g_free(caps);
226         return NULL;
227     }
228
229     /*
230      * The rest are link-layer types.
231      */
232     for (i = 1; raw_list[i] != NULL; i++) {
233         /* ...and what if the interface name has a tab in it, Mr. Clever Programmer? */
234         lt_parts = g_strsplit(raw_list[i], "\t", 3);
235         if (lt_parts[0] == NULL || lt_parts[1] == NULL || lt_parts[2] == NULL) {
236             g_strfreev(lt_parts);
237             continue;
238         }
239
240         data_link_info = g_malloc(sizeof (data_link_info_t));
241         data_link_info->dlt = (int) strtol(lt_parts[0], NULL, 10);
242         data_link_info->name = g_strdup(lt_parts[1]);
243         if (strcmp(lt_parts[2], "(not supported)") != 0)
244             data_link_info->description = g_strdup(lt_parts[2]);
245         else
246             data_link_info->description = NULL;
247
248         linktype_list = g_list_append(linktype_list, data_link_info);
249     }
250     g_strfreev(raw_list);
251
252     /* Check to see if we built a list */
253     if (linktype_list == NULL) {
254         /* No. */
255         if (err_str)
256             *err_str = g_strdup("Dumpcap returned no link-layer types");
257         g_free(caps);
258         return NULL;
259     }
260     caps->data_link_types = linktype_list;
261     return caps;
262 }
263
264 #endif /* HAVE_LIBPCAP */