2 * Utility routines for packet capture
4 * $Id: pcap-util.c,v 1.18 2003/10/10 03:00:10 guy Exp $
6 * Ethereal - Network traffic analyzer
7 * By Gerald Combs <gerald@ethereal.com>
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.
36 #ifdef HAVE_SYS_TYPES_H
37 # include <sys/types.h>
40 #ifdef HAVE_SYS_SOCKET_H
41 #include <sys/socket.h>
46 #include "pcap-util.h"
47 #include "pcap-util-int.h"
50 * Get the data-link type for a libpcap device.
51 * This works around AIX 5.x's non-standard and incompatible-with-the-
52 * rest-of-the-universe libpcap.
55 get_pcap_linktype(pcap_t *pch, char *devname
66 linktype = pcap_datalink(pch);
70 * The libpcap that comes with AIX 5.x uses RFC 1573 ifType values
71 * rather than DLT_ values for link-layer types; the ifType values
72 * for LAN devices are:
79 * and the ifType value for a loopback device is 24.
81 * The AIX names for LAN devices begin with:
88 * and the AIX names for loopback devices begin with "lo".
90 * (The difference between "Ethernet" and "802.3" is presumably
91 * whether packets have an Ethernet header, with a packet type,
92 * or an 802.3 header, with a packet length, followed by an 802.2
93 * header and possibly a SNAP header.)
95 * If the device name matches "linktype" interpreted as an ifType
96 * value, rather than as a DLT_ value, we will assume this is AIX's
97 * non-standard, incompatible libpcap, rather than a standard libpcap,
98 * and will map the link-layer type to the standard DLT_ value for
99 * that link-layer type, as that's what the rest of Ethereal expects.
101 * (This means the capture files won't be readable by a tcpdump
102 * linked with AIX's non-standard libpcap, but so it goes. They
103 * *will* be readable by standard versions of tcpdump, Ethereal,
106 * XXX - if we conclude we're using AIX libpcap, should we also
107 * set a flag to cause us to assume the time stamps are in
108 * seconds-and-nanoseconds form, and to convert them to
109 * seconds-and-microseconds form before processing them and
114 * Find the last component of the device name, which is the
117 ifacename = strchr(devname, '/');
118 if (ifacename == NULL)
119 ifacename = devnames;
121 /* See if it matches any of the LAN device names. */
122 if (strncmp(ifacename, "en", 2) == 0) {
125 * That's the RFC 1573 value for Ethernet; map it
130 } else if (strncmp(ifacename, "et", 2) == 0) {
133 * That's the RFC 1573 value for 802.3; map it to
135 * (libpcap, tcpdump, Ethereal, etc. don't care if
136 * it's Ethernet or 802.3.)
140 } else if (strncmp(ifacename, "tr") == 0) {
143 * That's the RFC 1573 value for 802.5 (Token Ring);
144 * map it to DLT_IEEE802, which is what's used for
149 } else if (strncmp(ifacename, "fi") == 0) {
150 if (linktype == 15) {
152 * That's the RFC 1573 value for FDDI; map it to
157 } else if (strncmp(ifacename, "lo") == 0) {
158 if (linktype == 24) {
160 * That's the RFC 1573 value for "software loopback"
161 * devices; map it to DLT_NULL, which is what's used
162 * for loopback devices on BSD.
173 if_info_new(char *name, char *description)
177 if_info = g_malloc(sizeof (if_info_t));
178 if_info->name = g_strdup(name);
179 if (description == NULL)
180 if_info->description = NULL;
182 if_info->description = g_strdup(description);
186 #ifdef HAVE_PCAP_FINDALLDEVS
188 get_interface_list_findalldevs(int *err, char *err_str)
191 pcap_if_t *alldevs, *dev;
194 if (pcap_findalldevs(&alldevs, err_str) == -1) {
195 *err = CANT_GET_INTERFACE_LIST;
199 if (alldevs == NULL) {
201 * No interfaces found.
203 *err = NO_INTERFACES_FOUND;
207 for (dev = alldevs; dev != NULL; dev = dev->next) {
208 if_info = if_info_new(dev->name, dev->description);
209 il = g_list_append(il, if_info);
211 pcap_freealldevs(alldevs);
215 #endif /* HAVE_PCAP_FINDALLDEVS */
218 free_if_cb(gpointer data, gpointer user_data _U_)
220 if_info_t *if_info = data;
222 g_free(if_info->name);
223 if (if_info->description != NULL)
224 g_free(if_info->description);
228 free_interface_list(GList *if_list)
230 g_list_foreach(if_list, free_if_cb, NULL);
231 g_list_free(if_list);
234 #endif /* HAVE_LIBPCAP */