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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
40 #ifdef HAVE_SYS_TYPES_H
41 # include <sys/types.h>
44 #ifdef HAVE_SYS_SOCKET_H
45 #include <sys/socket.h>
51 #include "capture-pcap-util.h"
52 #include "capture-pcap-util-int.h"
55 #include <netinet/in.h>
60 * Get the data-link type for a libpcap device.
61 * This works around AIX 5.x's non-standard and incompatible-with-the-
62 * rest-of-the-universe libpcap.
65 get_pcap_linktype(pcap_t *pch, const char *devname
73 const char *ifacename;
76 linktype = pcap_datalink(pch);
80 * The libpcap that comes with AIX 5.x uses RFC 1573 ifType values
81 * rather than DLT_ values for link-layer types; the ifType values
82 * for LAN devices are:
89 * and the ifType value for a loopback device is 24.
91 * The AIX names for LAN devices begin with:
98 * and the AIX names for loopback devices begin with "lo".
100 * (The difference between "Ethernet" and "802.3" is presumably
101 * whether packets have an Ethernet header, with a packet type,
102 * or an 802.3 header, with a packet length, followed by an 802.2
103 * header and possibly a SNAP header.)
105 * If the device name matches "linktype" interpreted as an ifType
106 * value, rather than as a DLT_ value, we will assume this is AIX's
107 * non-standard, incompatible libpcap, rather than a standard libpcap,
108 * and will map the link-layer type to the standard DLT_ value for
109 * that link-layer type, as that's what the rest of Wireshark expects.
111 * (This means the capture files won't be readable by a tcpdump
112 * linked with AIX's non-standard libpcap, but so it goes. They
113 * *will* be readable by standard versions of tcpdump, Wireshark,
116 * XXX - if we conclude we're using AIX libpcap, should we also
117 * set a flag to cause us to assume the time stamps are in
118 * seconds-and-nanoseconds form, and to convert them to
119 * seconds-and-microseconds form before processing them and
124 * Find the last component of the device name, which is the
127 ifacename = strchr(devname, '/');
128 if (ifacename == NULL)
131 /* See if it matches any of the LAN device names. */
132 if (strncmp(ifacename, "en", 2) == 0) {
135 * That's the RFC 1573 value for Ethernet; map it
140 } else if (strncmp(ifacename, "et", 2) == 0) {
143 * That's the RFC 1573 value for 802.3; map it to
145 * (libpcap, tcpdump, Wireshark, etc. don't care if
146 * it's Ethernet or 802.3.)
150 } else if (strncmp(ifacename, "tr", 2) == 0) {
153 * That's the RFC 1573 value for 802.5 (Token Ring);
154 * map it to DLT_IEEE802, which is what's used for
159 } else if (strncmp(ifacename, "fi", 2) == 0) {
160 if (linktype == 15) {
162 * That's the RFC 1573 value for FDDI; map it to
167 } else if (strncmp(ifacename, "lo", 2) == 0) {
168 if (linktype == 24) {
170 * That's the RFC 1573 value for "software loopback"
171 * devices; map it to DLT_NULL, which is what's used
172 * for loopback devices on BSD.
183 if_info_new(char *name, char *description)
187 if_info = g_malloc(sizeof (if_info_t));
188 if_info->name = g_strdup(name);
189 if (description == NULL)
190 if_info->description = NULL;
192 if_info->description = g_strdup(description);
193 if_info->ip_addr = NULL;
194 if_info->loopback = FALSE;
199 if_info_add_address(if_info_t *if_info, struct sockaddr *addr)
202 struct sockaddr_in *ai;
204 struct sockaddr_in6 *ai6;
207 switch (addr->sa_family) {
210 ai = (struct sockaddr_in *)addr;
211 ip_addr = g_malloc(sizeof(*ip_addr));
212 ip_addr->type = AT_IPv4;
213 ip_addr->ip_addr.ip4_addr =
214 *((guint32 *)&(ai->sin_addr.s_addr));
215 if_info->ip_addr = g_slist_append(if_info->ip_addr, ip_addr);
220 ai6 = (struct sockaddr_in6 *)addr;
221 ip_addr = g_malloc(sizeof(*ip_addr));
222 ip_addr->type = AT_IPv6;
223 memcpy((void *)&ip_addr->ip_addr.ip6_addr,
224 (void *)&ai6->sin6_addr.s6_addr,
225 sizeof ip_addr->ip_addr.ip6_addr);
226 if_info->ip_addr = g_slist_append(if_info->ip_addr, ip_addr);
232 #ifdef HAVE_PCAP_FINDALLDEVS
234 * Get all IP address information, and the loopback flag, for the given
238 if_info_ip(if_info_t *if_info, pcap_if_t *d)
243 if_info->loopback = (d->flags & PCAP_IF_LOOPBACK) ? TRUE : FALSE;
246 for (a = d->addresses; a != NULL; a = a->next) {
248 if_info_add_address(if_info, a->addr);
252 #ifdef HAVE_PCAP_REMOTE
254 get_interface_list_findalldevs_ex(const char *source,
255 struct pcap_rmtauth *auth,
256 int *err, char **err_str)
259 pcap_if_t *alldevs, *dev;
261 char errbuf[PCAP_ERRBUF_SIZE];
263 if (pcap_findalldevs_ex((char *)source, auth, &alldevs, errbuf) == -1) {
264 *err = CANT_GET_INTERFACE_LIST;
266 *err_str = cant_get_if_list_error_message(errbuf);
270 if (alldevs == NULL) {
272 * No interfaces found.
274 *err = NO_INTERFACES_FOUND;
280 for (dev = alldevs; dev != NULL; dev = dev->next) {
281 if_info = if_info_new(dev->name, dev->description);
282 il = g_list_append(il, if_info);
283 if_info_ip(if_info, dev);
285 pcap_freealldevs(alldevs);
292 get_interface_list_findalldevs(int *err, char **err_str)
295 pcap_if_t *alldevs, *dev;
297 char errbuf[PCAP_ERRBUF_SIZE];
299 if (pcap_findalldevs(&alldevs, errbuf) == -1) {
300 *err = CANT_GET_INTERFACE_LIST;
302 *err_str = cant_get_if_list_error_message(errbuf);
306 if (alldevs == NULL) {
308 * No interfaces found.
310 *err = NO_INTERFACES_FOUND;
316 for (dev = alldevs; dev != NULL; dev = dev->next) {
317 if_info = if_info_new(dev->name, dev->description);
318 il = g_list_append(il, if_info);
319 if_info_ip(if_info, dev);
321 pcap_freealldevs(alldevs);
325 #endif /* HAVE_PCAP_FINDALLDEVS */
328 free_if_info_addr_cb(gpointer addr, gpointer user_data _U_)
334 free_if_cb(gpointer data, gpointer user_data _U_)
336 if_info_t *if_info = data;
338 g_free(if_info->name);
339 if (if_info->description != NULL)
340 g_free(if_info->description);
342 g_slist_foreach(if_info->ip_addr, free_if_info_addr_cb, NULL);
343 g_slist_free(if_info->ip_addr);
348 free_interface_list(GList *if_list)
350 g_list_foreach(if_list, free_if_cb, NULL);
351 g_list_free(if_list);
355 * Get the data-link types available for a libpcap device.
357 static data_link_info_t *
358 create_data_link_info(int dlt)
360 data_link_info_t *data_link_info;
361 #ifdef HAVE_PCAP_DATALINK_VAL_TO_NAME
362 const char *typename;
366 data_link_info = g_malloc(sizeof (data_link_info_t));
367 data_link_info->dlt = dlt;
368 #ifdef HAVE_PCAP_DATALINK_VAL_TO_NAME
369 typename = pcap_datalink_val_to_name(dlt);
370 if (typename != NULL)
371 data_link_info->name = g_strdup(typename);
374 data_link_info->name = g_strdup_printf("DLT %d", dlt);
375 wtap_encap = wtap_pcap_encap_to_wtap_encap(dlt);
376 if (wtap_encap == WTAP_ENCAP_UNKNOWN) {
378 * We don't support this in Wiretap.
379 * However, we should, so you can capture on it.
380 * Put in an entry for it, with no description.
382 data_link_info->description = NULL;
385 * If this is null, that's a bug in
386 * "wtap_pcap_encap_to_wtap_encap()" - it should always
387 * return a valid encapsulation type - so we assume it's
390 data_link_info->description =
391 g_strdup(wtap_encap_string(wtap_encap));
393 return data_link_info;
397 get_pcap_linktype_list(const char *devname, char **err_str)
399 GList *linktype_list = NULL;
402 char errbuf[PCAP_ERRBUF_SIZE];
403 #ifdef HAVE_PCAP_LIST_DATALINKS
407 data_link_info_t *data_link_info;
409 #ifdef HAVE_PCAP_OPEN
410 pch = pcap_open(devname, MIN_PACKET_SIZE, 0, 0, NULL, errbuf);
412 pch = pcap_open_live(devname, MIN_PACKET_SIZE, 0, 0, errbuf);
416 *err_str = g_strdup(errbuf);
419 deflt = get_pcap_linktype(pch, devname);
420 #ifdef HAVE_PCAP_LIST_DATALINKS
421 nlt = pcap_list_datalinks(pch, &linktypes);
422 if (nlt == 0 || linktypes == NULL) {
425 *err_str = NULL; /* an empty list doesn't mean an error */
428 for (i = 0; i < nlt; i++) {
429 data_link_info = create_data_link_info(linktypes[i]);
432 * XXX - for 802.11, make the most detailed 802.11
433 * version the default, rather than the one the
434 * device has as the default?
436 if (linktypes[i] == deflt)
437 linktype_list = g_list_prepend(linktype_list,
440 linktype_list = g_list_append(linktype_list,
443 #ifdef HAVE_PCAP_FREE_DATALINKS
444 pcap_free_datalinks(linktypes);
447 * In Windows, there's no guarantee that if you have a library
448 * built with one version of the MSVC++ run-time library, and
449 * it returns a pointer to allocated data, you can free that
450 * data from a program linked with another version of the
451 * MSVC++ run-time library.
453 * This is not an issue on UN*X.
455 * See the mail threads starting at
457 * http://www.winpcap.org/pipermail/winpcap-users/2006-September/001421.html
461 * http://www.winpcap.org/pipermail/winpcap-users/2008-May/002498.html
466 #endif /* HAVE_PCAP_FREE_DATALINKS */
467 #else /* HAVE_PCAP_LIST_DATALINKS */
468 data_link_info = create_data_link_info(deflt);
469 linktype_list = g_list_append(linktype_list, data_link_info);
470 #endif /* HAVE_PCAP_LIST_DATALINKS */
473 return linktype_list;
477 free_linktype_cb(gpointer data, gpointer user_data _U_)
479 data_link_info_t *linktype_info = data;
481 g_free(linktype_info->name);
482 if (linktype_info->description != NULL)
483 g_free(linktype_info->description);
487 free_pcap_linktype_list(GList *linktype_list)
489 g_list_foreach(linktype_list, free_linktype_cb, NULL);
490 g_list_free(linktype_list);
493 /* Set the data link type on a pcap. */
495 set_pcap_linktype(pcap_t *pch, char *devname
496 #ifdef HAVE_PCAP_SET_DATALINK
501 #ifdef HAVE_PCAP_SET_DATALINK
502 if (pcap_set_datalink(pch, dlt) == 0)
503 return NULL; /* no error */
504 return pcap_geterr(pch);
506 /* Let them set it to the type it is; reject any other request. */
507 if (get_pcap_linktype(pch, devname) == dlt)
508 return NULL; /* no error */
509 return "That DLT isn't one of the DLTs supported by this device";
514 #ifdef HAVE_PCAP_DATALINK_VAL_TO_NAME
516 linktype_val_to_name(int dlt)
518 return pcap_datalink_val_to_name(dlt);
522 #ifdef HAVE_PCAP_DATALINK_NAME_TO_VAL
523 int linktype_name_to_val(const char *linktype)
525 return pcap_datalink_name_to_val(linktype);
529 #endif /* HAVE_LIBPCAP */