2 * Utility routines for packet capture
6 * Wireshark - Network traffic analyzer
7 * By Gerald Combs <gerald@wireshark.org>
8 * Copyright 1998 Gerald Combs
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.
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.
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.
36 #ifdef HAVE_SYS_TYPES_H
37 # include <sys/types.h>
40 #ifdef HAVE_SYS_SOCKET_H
41 #include <sys/socket.h>
47 #include "capture_ifinfo.h"
48 #include "capture-pcap-util.h"
49 #include "capture-pcap-util-int.h"
52 #include <netinet/in.h>
56 #include "capture_win_ifnames.h" /* windows friendly interface names */
58 #include "capture_unix_ifnames.h"
62 if_info_new(const char *name, const char *description, gboolean loopback)
66 const char *guid_text;
70 if_info = (if_info_t *)g_malloc(sizeof (if_info_t));
71 if_info->name = g_strdup(name);
74 * On Windows, the "description" is a vendor description,
75 * and the friendly name isn't returned by WinPcap.
80 * Skip over the "\Device\NPF_" prefix in the device name,
83 if (strncmp("\\Device\\NPF_", name, 12) == 0)
84 guid_text = name + 12;
88 /* Now try to parse what remains as a GUID. */
89 if (parse_as_guid(guid_text, &guid)) {
91 * Success. Try to get a friendly name using the GUID.
92 * As this is a regular interface, the description is a
95 if_info->friendly_name = get_interface_friendly_name_from_device_guid(&guid);
96 if_info->vendor_description = g_strdup(description);
99 * This is probably not a regular interface; we only
100 * support NT 5 (W2K) and later, so all regular interfaces
101 * should have GUIDs at the end of the name. Therefore,
102 * the description, if supplied, is a friendly name
103 * provided by WinPcap, and there is no vendor
106 if_info->friendly_name = g_strdup(description);
107 if_info->vendor_description = NULL;
111 * On UN*X, if there is a description, it's a friendly
112 * name, and there is no vendor description.
113 * If there's no description, fetch a friendly name
114 * if we can; if that fails, then, for a loopback
115 * interface, give it the friendly name "Loopback".
117 if_info->friendly_name = g_strdup(description);
118 if (if_info->friendly_name == NULL) {
119 if_info->friendly_name = get_unix_interface_friendly_name(name);
120 if (if_info->friendly_name == NULL) {
122 * If this is a loopback interface, give it a
123 * "friendly name" of "Loopback".
126 if_info->friendly_name = g_strdup("Loopback");
129 if_info->vendor_description = NULL;
131 if_info->loopback = loopback;
132 if_info->addrs = NULL;
137 if_info_add_address(if_info_t *if_info, struct sockaddr *addr)
140 struct sockaddr_in *ai;
142 struct sockaddr_in6 *ai6;
145 switch (addr->sa_family) {
148 ai = (struct sockaddr_in *)(void *)addr;
149 if_addr = (if_addr_t *)g_malloc(sizeof(*if_addr));
150 if_addr->ifat_type = IF_AT_IPv4;
151 if_addr->addr.ip4_addr =
152 *((guint32 *)&(ai->sin_addr.s_addr));
153 if_info->addrs = g_slist_append(if_info->addrs, if_addr);
158 ai6 = (struct sockaddr_in6 *)(void *)addr;
159 if_addr = (if_addr_t *)g_malloc(sizeof(*if_addr));
160 if_addr->ifat_type = IF_AT_IPv6;
161 memcpy((void *)&if_addr->addr.ip6_addr,
162 (void *)&ai6->sin6_addr.s6_addr,
163 sizeof if_addr->addr.ip6_addr);
164 if_info->addrs = g_slist_append(if_info->addrs, if_addr);
170 #ifdef HAVE_PCAP_FINDALLDEVS
172 * Get all IP address information for the given interface.
175 if_info_ip(if_info_t *if_info, pcap_if_t *d)
180 for (a = d->addresses; a != NULL; a = a->next) {
182 if_info_add_address(if_info, a->addr);
186 #ifdef HAVE_PCAP_REMOTE
188 get_interface_list_findalldevs_ex(const char *source,
189 struct pcap_rmtauth *auth,
190 int *err, char **err_str)
193 pcap_if_t *alldevs, *dev;
195 char errbuf[PCAP_ERRBUF_SIZE];
197 if (pcap_findalldevs_ex((char *)source, auth, &alldevs, errbuf) == -1) {
198 *err = CANT_GET_INTERFACE_LIST;
200 *err_str = cant_get_if_list_error_message(errbuf);
204 if (alldevs == NULL) {
206 * No interfaces found.
208 *err = NO_INTERFACES_FOUND;
214 for (dev = alldevs; dev != NULL; dev = dev->next) {
215 if_info = if_info_new(dev->name, dev->description,
216 (dev->flags & PCAP_IF_LOOPBACK) ? TRUE : FALSE);
217 il = g_list_append(il, if_info);
218 if_info_ip(if_info, dev);
220 pcap_freealldevs(alldevs);
227 get_interface_list_findalldevs(int *err, char **err_str)
230 pcap_if_t *alldevs, *dev;
232 char errbuf[PCAP_ERRBUF_SIZE];
234 if (pcap_findalldevs(&alldevs, errbuf) == -1) {
235 *err = CANT_GET_INTERFACE_LIST;
237 *err_str = cant_get_if_list_error_message(errbuf);
241 if (alldevs == NULL) {
243 * No interfaces found.
245 *err = NO_INTERFACES_FOUND;
251 for (dev = alldevs; dev != NULL; dev = dev->next) {
252 if_info = if_info_new(dev->name, dev->description,
253 (dev->flags & PCAP_IF_LOOPBACK) ? TRUE : FALSE);
254 il = g_list_append(il, if_info);
255 if_info_ip(if_info, dev);
257 pcap_freealldevs(alldevs);
261 #endif /* HAVE_PCAP_FINDALLDEVS */
264 free_if_info_addr_cb(gpointer addr, gpointer user_data _U_)
270 free_if_cb(gpointer data, gpointer user_data _U_)
272 if_info_t *if_info = (if_info_t *)data;
274 g_free(if_info->name);
275 g_free(if_info->friendly_name);
276 g_free(if_info->vendor_description);
278 g_slist_foreach(if_info->addrs, free_if_info_addr_cb, NULL);
279 g_slist_free(if_info->addrs);
284 free_interface_list(GList *if_list)
286 g_list_foreach(if_list, free_if_cb, NULL);
287 g_list_free(if_list);
290 #if !defined(HAVE_PCAP_DATALINK_NAME_TO_VAL) || !defined(HAVE_PCAP_DATALINK_VAL_TO_NAME) || !defined(HAVE_PCAP_DATALINK_VAL_TO_DESCRIPTION)
293 const char *description;
297 #define DLT_CHOICE(code, description) { #code, description, code }
298 #define DLT_CHOICE_SENTINEL { NULL, NULL, 0 }
300 static struct dlt_choice dlt_choices[] = {
301 DLT_CHOICE(DLT_NULL, "BSD loopback"),
302 DLT_CHOICE(DLT_EN10MB, "Ethernet"),
303 DLT_CHOICE(DLT_IEEE802, "Token ring"),
304 DLT_CHOICE(DLT_ARCNET, "ARCNET"),
305 DLT_CHOICE(DLT_SLIP, "SLIP"),
306 DLT_CHOICE(DLT_PPP, "PPP"),
307 DLT_CHOICE(DLT_FDDI, "FDDI"),
308 DLT_CHOICE(DLT_ATM_RFC1483, "RFC 1483 IP-over-ATM"),
309 DLT_CHOICE(DLT_RAW, "Raw IP"),
310 DLT_CHOICE(DLT_SLIP_BSDOS, "BSD/OS SLIP"),
311 DLT_CHOICE(DLT_PPP_BSDOS, "BSD/OS PPP"),
312 DLT_CHOICE(DLT_ATM_CLIP, "Linux Classical IP-over-ATM"),
313 DLT_CHOICE(DLT_PPP_SERIAL, "PPP over serial"),
314 DLT_CHOICE(DLT_PPP_ETHER, "PPPoE"),
315 DLT_CHOICE(DLT_C_HDLC, "Cisco HDLC"),
316 DLT_CHOICE(DLT_IEEE802_11, "802.11"),
317 DLT_CHOICE(DLT_FRELAY, "Frame Relay"),
318 DLT_CHOICE(DLT_LOOP, "OpenBSD loopback"),
319 DLT_CHOICE(DLT_ENC, "OpenBSD encapsulated IP"),
320 DLT_CHOICE(DLT_LINUX_SLL, "Linux cooked"),
321 DLT_CHOICE(DLT_LTALK, "Localtalk"),
322 DLT_CHOICE(DLT_PFLOG, "OpenBSD pflog file"),
323 DLT_CHOICE(DLT_PRISM_HEADER, "802.11 plus Prism header"),
324 DLT_CHOICE(DLT_IP_OVER_FC, "RFC 2625 IP-over-Fibre Channel"),
325 DLT_CHOICE(DLT_SUNATM, "Sun raw ATM"),
326 DLT_CHOICE(DLT_IEEE802_11_RADIO, "802.11 plus BSD radio information header"),
327 DLT_CHOICE(DLT_APPLE_IP_OVER_IEEE1394, "Apple IP-over-IEEE 1394"),
328 DLT_CHOICE(DLT_ARCNET_LINUX, "Linux ARCNET"),
329 DLT_CHOICE(DLT_LINUX_IRDA, "Linux IrDA"),
330 DLT_CHOICE(DLT_IEEE802_11_RADIO_AVS, "802.11 plus AVS radio information header"),
334 #if !defined(HAVE_PCAP_DATALINK_NAME_TO_VAL)
336 pcap_datalink_name_to_val(const char *name)
340 for (i = 0; dlt_choices[i].name != NULL; i++) {
341 if (g_ascii_strcasecmp(dlt_choices[i].name + sizeof("DLT_") - 1,
343 return (dlt_choices[i].dlt);
347 #endif /* defined(HAVE_PCAP_DATALINK_NAME_TO_VAL) */
349 #if !defined(HAVE_PCAP_DATALINK_VAL_TO_NAME)
351 pcap_datalink_val_to_name(int dlt)
355 for (i = 0; dlt_choices[i].name != NULL; i++) {
356 if (dlt_choices[i].dlt == dlt)
357 return (dlt_choices[i].name + sizeof("DLT_") - 1);
361 #endif /* defined(HAVE_PCAP_DATALINK_VAL_TO_NAME) */
363 #if !defined(HAVE_PCAP_DATALINK_VAL_TO_DESCRIPTION)
365 pcap_datalink_val_to_description(int dlt)
369 for (i = 0; dlt_choices[i].name != NULL; i++) {
370 if (dlt_choices[i].dlt == dlt)
371 return (dlt_choices[i].description);
375 #endif /* defined(HAVE_PCAP_DATALINK_VAL_TO_DESCRIPTION) */
377 #endif /* !defined(HAVE_PCAP_DATALINK_VAL_TO_NAME) || !defined(HAVE_PCAP_DATALINK_VAL_TO_DESCRIPTION) */
380 free_linktype_cb(gpointer data, gpointer user_data _U_)
382 data_link_info_t *linktype_info = (data_link_info_t *)data;
384 g_free(linktype_info->name);
385 g_free(linktype_info->description);
389 free_if_capabilities(if_capabilities_t *caps)
391 g_list_foreach(caps->data_link_types, free_linktype_cb, NULL);
392 g_list_free(caps->data_link_types);
397 linktype_val_to_name(int dlt)
399 return pcap_datalink_val_to_name(dlt);
402 int linktype_name_to_val(const char *linktype)
404 return pcap_datalink_name_to_val(linktype);
407 #endif /* HAVE_LIBPCAP */