2 * UN*X-specific utility routines for packet capture
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>
63 #include "pcap-util.h"
64 #include "pcap-util-int.h"
66 #ifndef HAVE_PCAP_FINDALLDEVS
67 struct search_user_data {
73 search_for_if_cb(gpointer data, gpointer user_data);
77 get_interface_list(int *err, char *err_str)
79 #ifdef HAVE_PCAP_FINDALLDEVS
80 return get_interface_list_findalldevs(err, err_str);
83 gint nonloopback_pos = 0;
84 struct ifreq *ifr, *last;
86 struct ifreq ifrflags;
87 int sock = socket(AF_INET, SOCK_DGRAM, 0);
88 struct search_user_data user_data;
95 *err = CANT_GET_INTERFACE_LIST;
96 sprintf(err_str, "Error opening socket: %s",
102 * This code came from: W. Richard Stevens: "UNIX Network Programming",
103 * Networking APIs: Sockets and XTI, Vol 1, page 434.
106 len = 100 * sizeof(struct ifreq);
111 memset (buf, 0, len);
112 if (ioctl(sock, SIOCGIFCONF, &ifc) < 0) {
113 if (errno != EINVAL || lastlen != 0) {
115 "SIOCGIFCONF ioctl error getting list of interfaces: %s",
120 if ((unsigned) ifc.ifc_len < sizeof(struct ifreq)) {
122 "SIOCGIFCONF ioctl gave too small return buffer");
125 if (ifc.ifc_len == lastlen)
126 break; /* success, len has not changed */
127 lastlen = ifc.ifc_len;
129 len += 10 * sizeof(struct ifreq); /* increment */
132 ifr = (struct ifreq *) ifc.ifc_req;
133 last = (struct ifreq *) ((char *) ifr + ifc.ifc_len);
136 * Skip entries that begin with "dummy", or that include
137 * a ":" (the latter are Solaris virtuals).
139 if (strncmp(ifr->ifr_name, "dummy", 5) == 0 ||
140 strchr(ifr->ifr_name, ':') != NULL)
144 * If we already have this interface name on the list,
145 * don't add it, but, if we don't already have an IP
146 * address for it, add that address (SIOCGIFCONF returns,
147 * at least on BSD-flavored systems, one entry per
148 * interface *address*; if an interface has multiple
149 * addresses, we get multiple entries for it).
151 user_data.name = ifr->ifr_name;
152 user_data.if_info = NULL;
153 g_list_foreach(il, search_for_if_cb, &user_data);
154 if (user_data.if_info != NULL) {
155 if_info_add_address(user_data.if_info, &ifr->ifr_addr);
160 * Get the interface flags.
162 memset(&ifrflags, 0, sizeof ifrflags);
163 strncpy(ifrflags.ifr_name, ifr->ifr_name,
164 sizeof ifrflags.ifr_name);
165 if (ioctl(sock, SIOCGIFFLAGS, (char *)&ifrflags) < 0) {
168 sprintf(err_str, "SIOCGIFFLAGS error getting flags for interface %s: %s",
169 ifr->ifr_name, strerror(errno));
174 * Skip interfaces that aren't up.
176 if (!(ifrflags.ifr_flags & IFF_UP))
180 * Skip interfaces that we can't open with "libpcap".
181 * Open with the minimum packet size - it appears that the
182 * IRIX SIOCSNOOPLEN "ioctl" may fail if the capture length
183 * supplied is too large, rather than just truncating it.
185 pch = pcap_open_live(ifr->ifr_name, MIN_PACKET_SIZE, 0, 0,
192 * If it's a loopback interface, add it at the end of the
193 * list, otherwise add it after the last non-loopback
194 * interface, so all loopback interfaces go at the end - we
195 * don't want a loopback interface to be the default capture
196 * device unless there are no non-loopback devices.
198 if_info = if_info_new(ifr->ifr_name, NULL);
199 if_info_add_address(if_info, &ifr->ifr_addr);
200 if ((ifrflags.ifr_flags & IFF_LOOPBACK) ||
201 strncmp(ifr->ifr_name, "lo", 2) == 0) {
202 if_info->loopback = TRUE;
203 il = g_list_append(il, if_info);
205 if_info->loopback = FALSE;
206 il = g_list_insert(il, if_info, nonloopback_pos);
208 * Insert the next non-loopback interface after this
216 ifr = (struct ifreq *) ((char *) ifr +
217 (ifr->ifr_addr.sa_len > sizeof(ifr->ifr_addr) ?
218 ifr->ifr_addr.sa_len : sizeof(ifr->ifr_addr)) +
221 ifr = (struct ifreq *) ((char *) ifr + sizeof(struct ifreq));
227 * OK, maybe we have support for the "any" device, to do a cooked
228 * capture on all interfaces at once.
229 * Try opening it and, if that succeeds, add it to the end of
230 * the list of interfaces.
232 pch = pcap_open_live("any", MIN_PACKET_SIZE, 0, 0, err_str);
235 * It worked; we can use the "any" device.
237 if_info = if_info_new("any",
238 "Pseudo-device that captures on all interfaces");
239 il = g_list_insert(il, if_info, -1);
249 * No interfaces found.
251 *err = NO_INTERFACES_FOUND;
257 free_interface_list(il);
260 *err = CANT_GET_INTERFACE_LIST;
262 #endif /* HAVE_PCAP_FINDALLDEVS */
265 #ifndef HAVE_PCAP_FINDALLDEVS
267 search_for_if_cb(gpointer data, gpointer user_data)
269 struct search_user_data *search_user_data = user_data;
270 if_info_t *if_info = data;
272 if (strcmp(if_info->name, search_user_data->name) == 0)
273 search_user_data->if_info = if_info;
275 #endif /* HAVE_PCAP_FINDALLDEVS */
278 * Get an error message string for a CANT_GET_INTERFACE_LIST error from
279 * "get_interface_list()".
282 cant_get_if_list_error_message(const char *err_str)
284 return g_strdup_printf("Can't get list of interfaces: %s", err_str);
288 * Append the version of libpcap with which we were compiled to a GString.
291 get_compiled_pcap_version(GString *str)
293 #ifdef HAVE_PCAP_VERSION
294 extern char pcap_version[];
296 g_string_sprintfa(str, "with libpcap %s", pcap_version);
298 g_string_append(str, "with libpcap (version unknown)");
303 * Append the version of libpcap with which we we're running to a GString.
306 get_runtime_pcap_version(GString *str)
308 g_string_sprintfa(str, "with ");
309 #ifdef HAVE_PCAP_LIB_VERSION
310 g_string_sprintfa(str, pcap_lib_version());
312 g_string_append(str, "libpcap (version unknown)");
314 g_string_append(str, " ");
317 #else /* HAVE_LIBPCAP */
320 * Append an indication that we were not compiled with libpcap
324 get_compiled_pcap_version(GString *str)
326 g_string_append(str, "without libpcap");
330 * Don't append anything, as we weren't even compiled to use WinPcap.
333 get_runtime_pcap_version(GString *str _U_)
337 #endif /* HAVE_LIBPCAP */