2 * UN*X-specific utility routines for packet capture
4 * $Id: pcap-util-unix.c,v 1.2 2003/10/10 06:05:48 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_SOCKET_H
43 #include <sys/socket.h>
46 #ifdef HAVE_SYS_IOCTL_H
47 #include <sys/ioctl.h>
53 * Keep Digital UNIX happy when including <net/if.h>.
59 #ifdef HAVE_SYS_SOCKIO_H
60 # include <sys/sockio.h>
65 #include "pcap-util.h"
66 #include "pcap-util-int.h"
68 #ifndef HAVE_PCAP_FINDALLDEVS
69 struct search_user_data {
75 search_for_if_cb(gpointer data, gpointer user_data);
79 get_interface_list(int *err, char *err_str)
81 #ifdef HAVE_PCAP_FINDALLDEVS
82 return get_interface_list_findalldevs(err, err_str);
85 gint nonloopback_pos = 0;
86 struct ifreq *ifr, *last;
88 struct ifreq ifrflags;
89 int sock = socket(AF_INET, SOCK_DGRAM, 0);
90 struct search_user_data user_data;
97 *err = CANT_GET_INTERFACE_LIST;
98 sprintf(err_str, "Error opening socket: %s",
104 * This code came from: W. Richard Stevens: "UNIX Network Programming",
105 * Networking APIs: Sockets and XTI, Vol 1, page 434.
108 len = 100 * sizeof(struct ifreq);
113 memset (buf, 0, len);
114 if (ioctl(sock, SIOCGIFCONF, &ifc) < 0) {
115 if (errno != EINVAL || lastlen != 0) {
117 "SIOCGIFCONF ioctl error getting list of interfaces: %s",
122 if ((unsigned) ifc.ifc_len < sizeof(struct ifreq)) {
124 "SIOCGIFCONF ioctl gave too small return buffer");
127 if (ifc.ifc_len == lastlen)
128 break; /* success, len has not changed */
129 lastlen = ifc.ifc_len;
131 len += 10 * sizeof(struct ifreq); /* increment */
134 ifr = (struct ifreq *) ifc.ifc_req;
135 last = (struct ifreq *) ((char *) ifr + ifc.ifc_len);
138 * Skip addresses that begin with "dummy", or that include
139 * a ":" (the latter are Solaris virtuals).
141 if (strncmp(ifr->ifr_name, "dummy", 5) == 0 ||
142 strchr(ifr->ifr_name, ':') != NULL)
146 * If we already have this interface name on the list,
147 * don't add it (SIOCGIFCONF returns, at least on
148 * BSD-flavored systems, one entry per interface *address*;
149 * if an interface has multiple addresses, we get multiple
152 user_data.name = ifr->ifr_name;
153 user_data.found = FALSE;
154 g_list_foreach(il, search_for_if_cb, &user_data);
159 * Get the interface flags.
161 memset(&ifrflags, 0, sizeof ifrflags);
162 strncpy(ifrflags.ifr_name, ifr->ifr_name,
163 sizeof ifrflags.ifr_name);
164 if (ioctl(sock, SIOCGIFFLAGS, (char *)&ifrflags) < 0) {
167 sprintf(err_str, "SIOCGIFFLAGS error getting flags for interface %s: %s",
168 ifr->ifr_name, strerror(errno));
173 * Skip interfaces that aren't up.
175 if (!(ifrflags.ifr_flags & IFF_UP))
179 * Skip interfaces that we can't open with "libpcap".
180 * Open with the minimum packet size - it appears that the
181 * IRIX SIOCSNOOPLEN "ioctl" may fail if the capture length
182 * supplied is too large, rather than just truncating it.
184 pch = pcap_open_live(ifr->ifr_name, MIN_PACKET_SIZE, 0, 0,
191 * If it's a loopback interface, add it at the end of the
192 * list, otherwise add it after the last non-loopback
193 * interface, so all loopback interfaces go at the end - we
194 * don't want a loopback interface to be the default capture
195 * device unless there are no non-loopback devices.
197 if_info = if_info_new(ifr->ifr_name, NULL);
198 if ((ifrflags.ifr_flags & IFF_LOOPBACK) ||
199 strncmp(ifr->ifr_name, "lo", 2) == 0)
200 il = g_list_append(il, if_info);
202 il = g_list_insert(il, if_info, nonloopback_pos);
204 * Insert the next non-loopback interface after this
212 ifr = (struct ifreq *) ((char *) ifr +
213 (ifr->ifr_addr.sa_len > sizeof(ifr->ifr_addr) ?
214 ifr->ifr_addr.sa_len : sizeof(ifr->ifr_addr)) +
217 ifr = (struct ifreq *) ((char *) ifr + sizeof(struct ifreq));
223 * OK, maybe we have support for the "any" device, to do a cooked
224 * capture on all interfaces at once.
225 * Try opening it and, if that succeeds, add it to the end of
226 * the list of interfaces.
228 pch = pcap_open_live("any", MIN_PACKET_SIZE, 0, 0, err_str);
231 * It worked; we can use the "any" device.
233 if_info = if_info_new("any",
234 "Pseudo-device that captures on all interfaces");
235 il = g_list_insert(il, if_info, -1);
245 * No interfaces found.
247 *err = NO_INTERFACES_FOUND;
253 free_interface_list(il);
256 *err = CANT_GET_INTERFACE_LIST;
258 #endif /* HAVE_PCAP_FINDALLDEVS */
261 #ifndef HAVE_PCAP_FINDALLDEVS
263 search_for_if_cb(gpointer data, gpointer user_data)
265 struct search_user_data *search_user_data = user_data;
266 if_info_t *if_info = data;
268 if (strcmp(if_info->name, search_user_data->name) == 0)
269 search_user_data->found = TRUE;
271 #endif /* HAVE_PCAP_FINDALLDEVS */
274 * Append the version of libpcap with which we were compiled to a GString.
277 get_compiled_pcap_version(GString *str)
279 #ifdef HAVE_PCAP_VERSION
280 extern char pcap_version[];
282 g_string_sprintfa(str, "with libpcap %s", pcap_version);
284 g_string_append(str, "with libpcap (version unknown)");
289 * Append the version of libpcap with which we we're running to a GString.
292 get_runtime_pcap_version(GString *str)
294 g_string_sprintfa(str, "with ");
295 #ifdef HAVE_PCAP_LIB_VERSION
296 g_string_sprintfa(str, pcap_lib_version());
298 g_string_append(str, "libpcap (version unknown)");
300 g_string_append(str, " ");
303 #else /* HAVE_LIBPCAP */
306 * Append an indication that we were not compiled with libpcap
310 get_compiled_pcap_version(GString *str)
312 g_string_append(str, "without libpcap");
316 * Don't append anything, as we weren't even compiled to use WinPcap.
319 get_runtime_pcap_version(GString *str _U_)
323 #endif /* HAVE_LIBPCAP */