Pull in versions from the 0.10.7 branch.
[obnox/wireshark/wip.git] / pcap-util-unix.c
1 /* pcap-util-unix.c
2  * UN*X-specific utility routines for packet capture
3  *
4  * $Id$
5  *
6  * Ethereal - Network traffic analyzer
7  * By Gerald Combs <gerald@ethereal.com>
8  * Copyright 1998 Gerald Combs
9  *
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.
14  *
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.
19  *
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.
23  */
24
25 #ifdef HAVE_CONFIG_H
26 # include "config.h"
27 #endif
28
29 #include <glib.h>
30
31 #ifdef HAVE_LIBPCAP
32
33 #include <stdlib.h>
34 #include <string.h>
35 #include <stdio.h>
36 #include <errno.h>
37
38 #ifdef HAVE_UNISTD_H
39 #include <unistd.h>
40 #endif
41
42 #ifdef HAVE_SYS_SOCKET_H
43 #include <sys/socket.h>
44 #endif
45
46 #ifdef HAVE_SYS_IOCTL_H
47 #include <sys/ioctl.h>
48 #endif
49
50 #include <pcap.h>
51
52 /*
53  * Keep Digital UNIX happy when including <net/if.h>.
54  */
55 struct mbuf;
56 struct rtentry;
57 #include <net/if.h>
58
59 #ifdef HAVE_SYS_SOCKIO_H
60 # include <sys/sockio.h>
61 #endif
62
63 #include "globals.h"
64
65 #include "pcap-util.h"
66 #include "pcap-util-int.h"
67
68 #ifndef HAVE_PCAP_FINDALLDEVS
69 struct search_user_data {
70         char    *name;
71         if_info_t *if_info;
72 };
73
74 static void
75 search_for_if_cb(gpointer data, gpointer user_data);
76 #endif
77
78 GList *
79 get_interface_list(int *err, char *err_str)
80 {
81 #ifdef HAVE_PCAP_FINDALLDEVS
82         return get_interface_list_findalldevs(err, err_str);
83 #else
84         GList  *il = NULL;
85         gint    nonloopback_pos = 0;
86         struct  ifreq *ifr, *last;
87         struct  ifconf ifc;
88         struct  ifreq ifrflags;
89         int     sock = socket(AF_INET, SOCK_DGRAM, 0);
90         struct search_user_data user_data;
91         pcap_t *pch;
92         int len, lastlen;
93         char *buf;
94         if_info_t *if_info;
95
96         if (sock < 0) {
97                 *err = CANT_GET_INTERFACE_LIST;
98                 sprintf(err_str, "Error opening socket: %s",
99                     strerror(errno));
100                 return NULL;
101         }
102
103         /*
104          * This code came from: W. Richard Stevens: "UNIX Network Programming",
105          * Networking APIs: Sockets and XTI, Vol 1, page 434.
106          */
107         lastlen = 0;
108         len = 100 * sizeof(struct ifreq);
109         for ( ; ; ) {
110                 buf = g_malloc(len);
111                 ifc.ifc_len = len;
112                 ifc.ifc_buf = buf;
113                 memset (buf, 0, len);
114                 if (ioctl(sock, SIOCGIFCONF, &ifc) < 0) {
115                         if (errno != EINVAL || lastlen != 0) {
116                                 sprintf(err_str,
117                                         "SIOCGIFCONF ioctl error getting list of interfaces: %s",
118                                         strerror(errno));
119                                 goto fail;
120                         }
121                 } else {
122                         if ((unsigned) ifc.ifc_len < sizeof(struct ifreq)) {
123                                 sprintf(err_str,
124                                         "SIOCGIFCONF ioctl gave too small return buffer");
125                                 goto fail;
126                         }
127                         if (ifc.ifc_len == lastlen)
128                                 break;                  /* success, len has not changed */
129                         lastlen = ifc.ifc_len;
130                 }
131                 len += 10 * sizeof(struct ifreq);       /* increment */
132                 g_free(buf);
133         }
134         ifr = (struct ifreq *) ifc.ifc_req;
135         last = (struct ifreq *) ((char *) ifr + ifc.ifc_len);
136         while (ifr < last) {
137                 /*
138                  * Skip entries that begin with "dummy", or that include
139                  * a ":" (the latter are Solaris virtuals).
140                  */
141                 if (strncmp(ifr->ifr_name, "dummy", 5) == 0 ||
142                     strchr(ifr->ifr_name, ':') != NULL)
143                         goto next;
144
145                 /*
146                  * If we already have this interface name on the list,
147                  * don't add it, but, if we don't already have an IP
148                  * address for it, add that address (SIOCGIFCONF returns,
149                  * at least on BSD-flavored systems, one entry per
150                  * interface *address*; if an interface has multiple
151                  * addresses, we get multiple entries for it).
152                  */
153                 user_data.name = ifr->ifr_name;
154                 user_data.if_info = NULL;
155                 g_list_foreach(il, search_for_if_cb, &user_data);
156                 if (user_data.if_info != NULL) {
157                         if_info_add_address(user_data.if_info, &ifr->ifr_addr);
158                         goto next;
159                 }
160
161                 /*
162                  * Get the interface flags.
163                  */
164                 memset(&ifrflags, 0, sizeof ifrflags);
165                 strncpy(ifrflags.ifr_name, ifr->ifr_name,
166                     sizeof ifrflags.ifr_name);
167                 if (ioctl(sock, SIOCGIFFLAGS, (char *)&ifrflags) < 0) {
168                         if (errno == ENXIO)
169                                 goto next;
170                         sprintf(err_str, "SIOCGIFFLAGS error getting flags for interface %s: %s",
171                             ifr->ifr_name, strerror(errno));
172                         goto fail;
173                 }
174
175                 /*
176                  * Skip interfaces that aren't up.
177                  */
178                 if (!(ifrflags.ifr_flags & IFF_UP))
179                         goto next;
180
181                 /*
182                  * Skip interfaces that we can't open with "libpcap".
183                  * Open with the minimum packet size - it appears that the
184                  * IRIX SIOCSNOOPLEN "ioctl" may fail if the capture length
185                  * supplied is too large, rather than just truncating it.
186                  */
187                 pch = pcap_open_live(ifr->ifr_name, MIN_PACKET_SIZE, 0, 0,
188                     err_str);
189                 if (pch == NULL)
190                         goto next;
191                 pcap_close(pch);
192
193                 /*
194                  * If it's a loopback interface, add it at the end of the
195                  * list, otherwise add it after the last non-loopback
196                  * interface, so all loopback interfaces go at the end - we
197                  * don't want a loopback interface to be the default capture
198                  * device unless there are no non-loopback devices.
199                  */
200                 if_info = if_info_new(ifr->ifr_name, NULL);
201                 if_info_add_address(if_info, &ifr->ifr_addr);
202                 if ((ifrflags.ifr_flags & IFF_LOOPBACK) ||
203                     strncmp(ifr->ifr_name, "lo", 2) == 0) {
204                         if_info->loopback = TRUE;
205                         il = g_list_append(il, if_info);
206                 } else {
207                         if_info->loopback = FALSE;
208                         il = g_list_insert(il, if_info, nonloopback_pos);
209                         /*
210                          * Insert the next non-loopback interface after this
211                          * one.
212                          */
213                         nonloopback_pos++;
214                 }
215
216         next:
217 #ifdef HAVE_SA_LEN
218                 ifr = (struct ifreq *) ((char *) ifr +
219                     (ifr->ifr_addr.sa_len > sizeof(ifr->ifr_addr) ?
220                         ifr->ifr_addr.sa_len : sizeof(ifr->ifr_addr)) +
221                     IFNAMSIZ);
222 #else
223                 ifr = (struct ifreq *) ((char *) ifr + sizeof(struct ifreq));
224 #endif
225         }
226
227 #ifdef linux
228         /*
229          * OK, maybe we have support for the "any" device, to do a cooked
230          * capture on all interfaces at once.
231          * Try opening it and, if that succeeds, add it to the end of
232          * the list of interfaces.
233          */
234         pch = pcap_open_live("any", MIN_PACKET_SIZE, 0, 0, err_str);
235         if (pch != NULL) {
236                 /*
237                  * It worked; we can use the "any" device.
238                  */
239                 if_info = if_info_new("any",
240                     "Pseudo-device that captures on all interfaces");
241                 il = g_list_insert(il, if_info, -1);
242                 pcap_close(pch);
243         }
244 #endif
245
246         g_free(ifc.ifc_buf);
247         close(sock);
248
249         if (il == NULL) {
250                 /*
251                  * No interfaces found.
252                  */
253                 *err = NO_INTERFACES_FOUND;
254         }
255         return il;
256
257 fail:
258         if (il != NULL)
259                 free_interface_list(il);
260         g_free(ifc.ifc_buf);
261         close(sock);
262         *err = CANT_GET_INTERFACE_LIST;
263         return NULL;
264 #endif /* HAVE_PCAP_FINDALLDEVS */
265 }
266
267 #ifndef HAVE_PCAP_FINDALLDEVS
268 static void
269 search_for_if_cb(gpointer data, gpointer user_data)
270 {
271         struct search_user_data *search_user_data = user_data;
272         if_info_t *if_info = data;
273
274         if (strcmp(if_info->name, search_user_data->name) == 0)
275                 search_user_data->if_info = if_info;
276 }
277 #endif /* HAVE_PCAP_FINDALLDEVS */
278
279 /*
280  * Get an error message string for a CANT_GET_INTERFACE_LIST error from
281  * "get_interface_list()".
282  */
283 gchar *
284 cant_get_if_list_error_message(const char *err_str)
285 {
286         return g_strdup_printf("Can't get list of interfaces: %s", err_str);
287 }
288
289 /*
290  * Append the version of libpcap with which we were compiled to a GString.
291  */
292 void
293 get_compiled_pcap_version(GString *str)
294 {
295 #ifdef HAVE_PCAP_VERSION
296         extern char pcap_version[];
297
298         g_string_sprintfa(str, "with libpcap %s", pcap_version);
299 #else
300         g_string_append(str, "with libpcap (version unknown)");
301 #endif
302 }
303
304 /*
305  * Append the version of libpcap with which we we're running to a GString.
306  */
307 void
308 get_runtime_pcap_version(GString *str)
309 {
310         g_string_sprintfa(str, "with ");
311 #ifdef HAVE_PCAP_LIB_VERSION
312         g_string_sprintfa(str, pcap_lib_version());
313 #else
314         g_string_append(str, "libpcap (version unknown)");
315 #endif
316         g_string_append(str, " ");
317 }
318
319 #else /* HAVE_LIBPCAP */
320
321 /*
322  * Append an indication that we were not compiled with libpcap
323  * to a GString.
324  */
325 void
326 get_compiled_pcap_version(GString *str)
327 {
328         g_string_append(str, "without libpcap");
329 }
330
331 /*
332  * Don't append anything, as we weren't even compiled to use WinPcap.
333  */
334 void
335 get_runtime_pcap_version(GString *str _U_)
336 {
337 }
338
339 #endif /* HAVE_LIBPCAP */