2 * Utility routines for packet capture
4 * $Id: pcap-util.c,v 1.3 2001/11/09 08:16:24 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.
42 #ifdef HAVE_SYS_TYPES_H
43 #include <sys/types.h>
46 #ifdef HAVE_SYS_SOCKET_H
47 #include <sys/socket.h>
50 #ifdef HAVE_SYS_IOCTL_H
51 #include <sys/ioctl.h>
60 #ifdef HAVE_SYS_SOCKIO_H
61 # include <sys/sockio.h>
67 #include "capture-wpcap.h"
70 #include "pcap-util.h"
73 * Get the data-link type for a libpcap device.
74 * This works around AIX 5.x's non-standard and incompatible-with-the-
75 * rest-of-the-universe libpcap.
78 get_pcap_linktype(pcap_t *pch, char *devname)
85 linktype = pcap_datalink(pch);
89 * The libpcap that comes with AIX 5.x uses RFC 1573 ifType values
90 * rather than DLT_ values for link-layer types; the ifType values
91 * for LAN devices are:
98 * The AIX names for LAN devices begin with:
105 * (The difference between "Ethernet" and "802.3" is presumably
106 * whether packets have an Ethernet header, with a packet type,
107 * or an 802.3 header, with a packet length, followed by an 802.2
108 * header and possibly a SNAP header.)
110 * If the device name matches "linktype" interpreted as an ifType
111 * value, rather than as a DLT_ value, we will assume this is AIX's
112 * non-standard, incompatible libpcap, rather than a standard libpcap,
113 * and will map the link-layer type to the standard DLT_ value for
114 * that link-layer type, as that's what the rest of Ethereal expects.
116 * (This means the capture files won't be readable by a tcpdump
117 * linked with AIX's non-standard libpcap, but so it goes. They
118 * *will* be readable by standard versions of tcpdump, Ethereal,
121 * XXX - if we conclude we're using AIX libpcap, should we also
122 * set a flag to cause us to assume the time stamps are in
123 * seconds-and-nanoseconds form, and to convert them to
124 * seconds-and-microseconds form before processing them and
129 * Find the last component of the device name, which is the
132 ifacename = strchr(devname, '/');
133 if (ifacename == NULL)
134 ifacename = devnames;
136 /* See if it matches any of the LAN device names. */
137 if (strncmp(ifacename, "en", 2) == 0) {
140 * That's the RFC 1573 value for Ethernet; map it
145 } else if (strncmp(ifacename, "et", 2) == 0) {
148 * That's the RFC 1573 value for 802.3; map it to
150 * (libpcap, tcpdump, Ethereal, etc. don't care if
151 * it's Ethernet or 802.3.)
155 } else if (strncmp(ifacename, "tr") == 0) {
158 * That's the RFC 1573 value for 802.5 (Token Ring);
159 * map it to DLT_IEEE802, which is what's used for
164 } else if (strncmp(ifacename, "fi") == 0) {
165 if (linktype == 15) {
167 * That's the RFC 1573 value for FDDI; map it to
179 * If the ability to capture packets is added to Wiretap, these
180 * routines should be moved to the Wiretap source (with
181 * "get_interface_list()" and "free_interface_list()" renamed to
182 * "wtap_get_interface_list()" and "wtap_free_interface_list()",
183 * and modified to use Wiretap routines to attempt to open the
187 struct search_user_data {
193 search_for_if_cb(gpointer data, gpointer user_data);
196 free_if_cb(gpointer data, gpointer user_data);
200 get_interface_list(int *err, char *err_str)
203 gint nonloopback_pos = 0;
204 struct ifreq *ifr, *last;
206 struct ifreq ifrflags;
207 int sock = socket(AF_INET, SOCK_DGRAM, 0);
208 struct search_user_data user_data;
214 sprintf(err_str, "Error opening socket: %s",
220 * This code came from: W. Richard Stevens: "UNIX Network Programming",
221 * Networking APIs: Sockets and XTI, Vol 1, page 434.
224 len = 100 * sizeof(struct ifreq);
229 memset (buf, 0, len);
230 if (ioctl(sock, SIOCGIFCONF, &ifc) < 0) {
231 if (errno != EINVAL || lastlen != 0) {
233 "SIOCGIFCONF ioctl error getting list of interfaces: %s",
238 if ((unsigned) ifc.ifc_len < sizeof(struct ifreq)) {
240 "SIOCGIFCONF ioctl gave too small return buffer");
243 if (ifc.ifc_len == lastlen)
244 break; /* success, len has not changed */
245 lastlen = ifc.ifc_len;
247 len += 10 * sizeof(struct ifreq); /* increment */
250 ifr = (struct ifreq *) ifc.ifc_req;
251 last = (struct ifreq *) ((char *) ifr + ifc.ifc_len);
254 * Skip addresses that begin with "dummy", or that include
255 * a ":" (the latter are Solaris virtuals).
257 if (strncmp(ifr->ifr_name, "dummy", 5) == 0 ||
258 strchr(ifr->ifr_name, ':') != NULL)
262 * If we already have this interface name on the list,
263 * don't add it (SIOCGIFCONF returns, at least on
264 * BSD-flavored systems, one entry per interface *address*;
265 * if an interface has multiple addresses, we get multiple
268 user_data.name = ifr->ifr_name;
269 user_data.found = FALSE;
270 g_list_foreach(il, search_for_if_cb, &user_data);
275 * Get the interface flags.
277 memset(&ifrflags, 0, sizeof ifrflags);
278 strncpy(ifrflags.ifr_name, ifr->ifr_name,
279 sizeof ifrflags.ifr_name);
280 if (ioctl(sock, SIOCGIFFLAGS, (char *)&ifrflags) < 0) {
283 sprintf(err_str, "SIOCGIFFLAGS error getting flags for interface %s: %s",
284 ifr->ifr_name, strerror(errno));
289 * Skip interfaces that aren't up.
291 if (!(ifrflags.ifr_flags & IFF_UP))
295 * Skip interfaces that we can't open with "libpcap".
296 * Open with the minimum packet size - it appears that the
297 * IRIX SIOCSNOOPLEN "ioctl" may fail if the capture length
298 * supplied is too large, rather than just truncating it.
300 pch = pcap_open_live(ifr->ifr_name, MIN_PACKET_SIZE, 0, 0,
307 * If it's a loopback interface, add it at the end of the
308 * list, otherwise add it after the last non-loopback
309 * interface, so all loopback interfaces go at the end - we
310 * don't want a loopback interface to be the default capture
311 * device unless there are no non-loopback devices.
313 if ((ifrflags.ifr_flags & IFF_LOOPBACK) ||
314 strncmp(ifr->ifr_name, "lo", 2) == 0)
315 il = g_list_insert(il, g_strdup(ifr->ifr_name), -1);
317 il = g_list_insert(il, g_strdup(ifr->ifr_name),
320 * Insert the next non-loopback interface after this
328 ifr = (struct ifreq *) ((char *) ifr +
329 (ifr->ifr_addr.sa_len > sizeof(ifr->ifr_addr) ?
330 ifr->ifr_addr.sa_len : sizeof(ifr->ifr_addr)) +
333 ifr = (struct ifreq *) ((char *) ifr + sizeof(struct ifreq));
339 * OK, maybe we have support for the "any" device, to do a cooked
340 * capture on all interfaces at once.
341 * Try opening it and, if that succeeds, add it to the end of
342 * the list of interfaces.
344 pch = pcap_open_live("any", MIN_PACKET_SIZE, 0, 0, err_str);
347 * It worked; we can use the "any" device.
349 il = g_list_insert(il, g_strdup("any"), -1);
359 * No interfaces found.
361 *err = NO_INTERFACES_FOUND;
367 g_list_foreach(il, free_if_cb, NULL);
372 *err = CANT_GET_INTERFACE_LIST;
377 search_for_if_cb(gpointer data, gpointer user_data)
379 struct search_user_data *search_user_data = user_data;
381 if (strcmp((char *)data, search_user_data->name) == 0)
382 search_user_data->found = TRUE;
386 get_interface_list(int *err, char *err_str) {
393 names = (wchar_t *)pcap_lookupdev(err_str);
398 /* If names[0] is less than 256 it means the first byte is 0
399 This implies that we are using unicode characters */
403 while (names[i] != 0)
404 newname[j++] = names[i++];
409 il = g_list_append(il, g_strdup(newname));
413 /* Otherwise we are in Windows 95/98 and using ascii(8 bit)
417 win95names=(char *)names;
419 while (win95names[i] != 0)
420 newname[j++] = win95names[i++];
422 if (win95names[i] == 0)
425 il = g_list_append(il, g_strdup(newname));
434 free_if_cb(gpointer data, gpointer user_data)
440 free_interface_list(GList *if_list)
442 while (if_list != NULL) {
443 g_free(if_list->data);
444 if_list = g_list_remove_link(if_list, if_list);
448 #endif /* HAVE_LIBPCAP */