Clean comments and indentation.
[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 "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     *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(&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 = msg;
87         } else {
88             g_free(msg);
89         }
90         *err = CANT_RUN_DUMPCAP;
91         return NULL;
92     }
93
94     /* Split our lines */
95 #ifdef _WIN32
96     raw_list = g_strsplit(msg, "\r\n", 0);
97 #else
98     raw_list = g_strsplit(msg, "\n", 0);
99 #endif
100     g_free(msg);
101
102     for (i = 0; raw_list[i] != NULL; i++) {
103         if_parts = g_strsplit(raw_list[i], "\t", 4);
104         if (if_parts[0] == NULL || if_parts[1] == NULL || if_parts[2] == NULL ||
105                 if_parts[3] == NULL) {
106             g_strfreev(if_parts);
107             continue;
108         }
109
110         /* Number followed by the name, e.g "1. eth0" */
111         name = strchr(if_parts[0], ' ');
112         if (name) {
113             name++;
114         } else {
115             g_strfreev(if_parts);
116             continue;
117         }
118
119         if_info = g_malloc0(sizeof(if_info_t));
120         if_info->name = g_strdup(name);
121         if (strlen(if_parts[1]) > 0)
122             if_info->description = g_strdup(if_parts[1]);
123         addr_parts = g_strsplit(if_parts[2], ",", 0);
124         for (j = 0; addr_parts[j] != NULL; j++) {
125             if_addr = g_malloc0(sizeof(if_addr_t));
126             if (inet_pton(AF_INET, addr_parts[j], &if_addr->addr.ip4_addr)) {
127                 if_addr->ifat_type = IF_AT_IPv4;
128             } else if (inet_pton(AF_INET6, addr_parts[j],
129                     &if_addr->addr.ip6_addr)) {
130                 if_addr->ifat_type = IF_AT_IPv6;
131             } else {
132                 g_free(if_addr);
133                 if_addr = NULL;
134             }
135             if (if_addr) {
136                 if_info->addrs = g_slist_append(if_info->addrs, if_addr);
137             }
138         }
139         if (strcmp(if_parts[3], "loopback") == 0)
140             if_info->loopback = TRUE;
141         g_strfreev(if_parts);
142         g_strfreev(addr_parts);
143         if_list = g_list_append(if_list, if_info);
144     }
145     g_strfreev(raw_list);
146
147     /* Check to see if we built a list */
148     if (if_list == NULL) {
149         *err = NO_INTERFACES_FOUND;
150         if (err_str)
151             *err_str = g_strdup("No interfaces found");
152     }
153     return if_list;
154 }
155
156 /* XXX - We parse simple text output to get our interface list.  Should
157  * we use "real" data serialization instead, e.g. via XML? */
158 GList *
159 capture_pcap_linktype_list(const gchar *ifname, char **err_str)
160 {
161     GList     *linktype_list = NULL;
162     int        err, i;
163     gchar     *msg;
164     gchar    **raw_list, **lt_parts;
165     data_link_info_t *data_link_info;
166
167     g_log(LOG_DOMAIN_CAPTURE, G_LOG_LEVEL_MESSAGE, "Capture Linktype List ...");
168
169     /* Try to get our interface list */
170     err = sync_linktype_list_open(ifname, &msg);
171     if (err != 0) {
172         g_log(LOG_DOMAIN_CAPTURE, G_LOG_LEVEL_MESSAGE, "Capture Linktype List failed!");
173         if (err_str) {
174             *err_str = msg;
175         } else {
176             g_free(msg);
177         }
178         return NULL;
179     }
180
181     /* Split our lines */
182 #ifdef _WIN32
183     raw_list = g_strsplit(msg, "\r\n", 0);
184 #else
185     raw_list = g_strsplit(msg, "\n", 0);
186 #endif
187     g_free(msg);
188
189     for (i = 0; raw_list[i] != NULL; i++) {
190         /* ...and what if the interface name has a tab in it, Mr. Clever Programmer? */
191         lt_parts = g_strsplit(raw_list[i], "\t", 3);
192         if (lt_parts[0] == NULL || lt_parts[1] == NULL || lt_parts[2] == NULL) {
193             g_strfreev(lt_parts);
194             continue;
195         }
196
197         data_link_info = g_malloc(sizeof (data_link_info_t));
198         data_link_info->dlt = (int) strtol(lt_parts[0], NULL, 10);
199         data_link_info->name = g_strdup(lt_parts[1]);
200         if (strcmp(lt_parts[2], "(not supported)") != 0)
201             data_link_info->description = g_strdup(lt_parts[2]);
202         else
203             data_link_info->description = NULL;
204
205         linktype_list = g_list_append(linktype_list, data_link_info);
206     }
207     g_strfreev(raw_list);
208
209     /* Check to see if we built a list */
210     if (linktype_list == NULL) {
211         if (err_str)
212             *err_str = NULL;
213     }
214     return linktype_list;
215 }
216
217 #endif /* HAVE_LIBPCAP */