From Pascal Quantin:
[obnox/wireshark/wip.git] / capture-pcap-util-unix.c
1 /* capture-pcap-util-unix.c
2  * UN*X-specific utility routines for packet capture
3  *
4  * $Id$
5  *
6  * Wireshark - Network traffic analyzer
7  * By Gerald Combs <gerald@wireshark.org>
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 "capture-pcap-util.h"
64 #include "capture-pcap-util-int.h"
65
66 #ifndef HAVE_PCAP_FINDALLDEVS
67 struct search_user_data {
68         char    *name;
69         if_info_t *if_info;
70 };
71
72 static void
73 search_for_if_cb(gpointer data, gpointer user_data);
74 #endif
75
76 #ifdef HAVE_PCAP_REMOTE
77 GList *
78 get_remote_interface_list(const char *hostname, const char *port,
79                           int auth_type, const char *username,
80                           const char *passwd, int *err, char **err_str)
81 {
82     struct pcap_rmtauth auth;
83     char source[PCAP_BUF_SIZE];
84     char errbuf[PCAP_ERRBUF_SIZE];
85
86     auth.type = auth_type;
87     auth.username = username;
88     auth.password = passwd;
89
90     if (pcap_createsrcstr(source, PCAP_SRC_IFREMOTE, hostname, port,
91                           NULL, errbuf) == -1) {
92         *err = CANT_GET_INTERFACE_LIST;
93         if (err_str != NULL)
94             *err_str = cant_get_if_list_error_message(errbuf);
95         return NULL;
96     }
97     return get_interface_list_findalldevs_ex(source, &auth, err, err_str);
98 }
99 #endif
100
101 GList *
102 get_interface_list(int *err, char **err_str)
103 {
104 #ifdef HAVE_PCAP_FINDALLDEVS
105 #ifdef HAVE_PCAP_REMOTE
106     char source[PCAP_BUF_SIZE];
107     char errbuf[PCAP_ERRBUF_SIZE];
108
109     if (pcap_createsrcstr(source, PCAP_SRC_IFLOCAL,
110                           NULL, NULL, NULL, errbuf) == -1) {
111         *err = CANT_GET_INTERFACE_LIST;
112         if (err_str != NULL)
113             *err_str = cant_get_if_list_error_message(errbuf);
114         return NULL;
115     }
116     return get_interface_list_findalldevs_ex(source, NULL, err, err_str);
117 #else
118         return get_interface_list_findalldevs(err, err_str);
119 #endif
120 #else
121         GList  *il = NULL;
122         gint    nonloopback_pos = 0;
123         struct  ifreq *ifr, *last;
124         struct  ifconf ifc;
125         struct  ifreq ifrflags;
126         int     sock = socket(AF_INET, SOCK_DGRAM, 0);
127         struct search_user_data user_data;
128         pcap_t *pch;
129         int len, lastlen;
130         char *buf;
131         if_info_t *if_info;
132         char errbuf[PCAP_ERRBUF_SIZE];
133
134         if (sock < 0) {
135                 *err = CANT_GET_INTERFACE_LIST;
136                 if (err_str != NULL) {
137                         *err_str = g_strdup_printf(
138                             "Can't get list of interfaces: error opening socket: %s",
139                             strerror(errno));
140                 }
141                 return NULL;
142         }
143
144         /*
145          * This code came from: W. Richard Stevens: "UNIX Network Programming",
146          * Networking APIs: Sockets and XTI, Vol 1, page 434.
147          */
148         lastlen = 0;
149         len = 100 * sizeof(struct ifreq);
150         for ( ; ; ) {
151                 buf = g_malloc(len);
152                 ifc.ifc_len = len;
153                 ifc.ifc_buf = buf;
154                 memset (buf, 0, len);
155                 if (ioctl(sock, SIOCGIFCONF, &ifc) < 0) {
156                         if (errno != EINVAL || lastlen != 0) {
157                                 if (err_str != NULL) {
158                                         *err_str = g_strdup_printf(
159                                             "Can't get list of interfaces: SIOCGIFCONF ioctl error: %s",
160                                             strerror(errno));
161                                 }
162                                 goto fail;
163                         }
164                 } else {
165                         if ((unsigned) ifc.ifc_len < sizeof(struct ifreq)) {
166                                 if (err_str != NULL) {
167                                         *err_str = g_strdup(
168                                             "Can't get list of interfaces: SIOCGIFCONF ioctl gave too small return buffer");
169                                 }
170                                 goto fail;
171                         }
172                         if (ifc.ifc_len == lastlen)
173                                 break;                  /* success, len has not changed */
174                         lastlen = ifc.ifc_len;
175                 }
176                 len += 10 * sizeof(struct ifreq);       /* increment */
177                 g_free(buf);
178         }
179         ifr = (struct ifreq *) ifc.ifc_req;
180         last = (struct ifreq *) ((char *) ifr + ifc.ifc_len);
181         while (ifr < last) {
182                 /*
183                  * Skip entries that begin with "dummy", or that include
184                  * a ":" (the latter are Solaris virtuals).
185                  */
186                 if (strncmp(ifr->ifr_name, "dummy", 5) == 0 ||
187                     strchr(ifr->ifr_name, ':') != NULL)
188                         goto next;
189
190                 /*
191                  * If we already have this interface name on the list,
192                  * don't add it, but, if we don't already have an IP
193                  * address for it, add that address (SIOCGIFCONF returns,
194                  * at least on BSD-flavored systems, one entry per
195                  * interface *address*; if an interface has multiple
196                  * addresses, we get multiple entries for it).
197                  */
198                 user_data.name = ifr->ifr_name;
199                 user_data.if_info = NULL;
200                 g_list_foreach(il, search_for_if_cb, &user_data);
201                 if (user_data.if_info != NULL) {
202                         if_info_add_address(user_data.if_info, &ifr->ifr_addr);
203                         goto next;
204                 }
205
206                 /*
207                  * Get the interface flags.
208                  */
209                 memset(&ifrflags, 0, sizeof ifrflags);
210                 g_strlcpy(ifrflags.ifr_name, ifr->ifr_name,
211                     sizeof ifrflags.ifr_name);
212                 if (ioctl(sock, SIOCGIFFLAGS, (char *)&ifrflags) < 0) {
213                         if (errno == ENXIO)
214                                 goto next;
215                         if (err_str != NULL) {
216                                 *err_str = g_strdup_printf(
217                                     "Can't get list of interfaces: SIOCGIFFLAGS error getting flags for interface %s: %s",
218                                     ifr->ifr_name, strerror(errno));
219                         }
220                         goto fail;
221                 }
222
223                 /*
224                  * Skip interfaces that aren't up.
225                  */
226                 if (!(ifrflags.ifr_flags & IFF_UP))
227                         goto next;
228
229                 /*
230                  * Skip interfaces that we can't open with "libpcap".
231                  * Open with the minimum packet size - it appears that the
232                  * IRIX SIOCSNOOPLEN "ioctl" may fail if the capture length
233                  * supplied is too large, rather than just truncating it.
234                  */
235                 pch = pcap_open_live(ifr->ifr_name, MIN_PACKET_SIZE, 0, 0,
236                     errbuf);
237                 if (pch == NULL)
238                         goto next;
239                 pcap_close(pch);
240
241                 /*
242                  * If it's a loopback interface, add it at the end of the
243                  * list, otherwise add it after the last non-loopback
244                  * interface, so all loopback interfaces go at the end - we
245                  * don't want a loopback interface to be the default capture
246                  * device unless there are no non-loopback devices.
247                  */
248                 if_info = if_info_new(ifr->ifr_name, NULL);
249                 if_info_add_address(if_info, &ifr->ifr_addr);
250                 if ((ifrflags.ifr_flags & IFF_LOOPBACK) ||
251                     strncmp(ifr->ifr_name, "lo", 2) == 0) {
252                         if_info->loopback = TRUE;
253                         il = g_list_append(il, if_info);
254                 } else {
255                         if_info->loopback = FALSE;
256                         il = g_list_insert(il, if_info, nonloopback_pos);
257                         /*
258                          * Insert the next non-loopback interface after this
259                          * one.
260                          */
261                         nonloopback_pos++;
262                 }
263
264         next:
265 #ifdef HAVE_SA_LEN
266                 ifr = (struct ifreq *) ((char *) ifr +
267                     (ifr->ifr_addr.sa_len > sizeof(ifr->ifr_addr) ?
268                         ifr->ifr_addr.sa_len : sizeof(ifr->ifr_addr)) +
269                     IFNAMSIZ);
270 #else
271                 ifr = (struct ifreq *) ((char *) ifr + sizeof(struct ifreq));
272 #endif
273         }
274
275 #ifdef linux
276         /*
277          * OK, maybe we have support for the "any" device, to do a cooked
278          * capture on all interfaces at once.
279          * Try opening it and, if that succeeds, add it to the end of
280          * the list of interfaces.
281          */
282         pch = pcap_open_live("any", MIN_PACKET_SIZE, 0, 0, errbuf);
283         if (pch != NULL) {
284                 /*
285                  * It worked; we can use the "any" device.
286                  */
287                 if_info = if_info_new("any",
288                     "Pseudo-device that captures on all interfaces");
289                 il = g_list_insert(il, if_info, -1);
290                 pcap_close(pch);
291         }
292 #endif
293
294         g_free(ifc.ifc_buf);
295         close(sock);
296
297         if (il == NULL) {
298                 /*
299                  * No interfaces found.
300                  */
301                 *err = NO_INTERFACES_FOUND;
302                 if (err_str != NULL)
303                         *err_str = NULL;
304         }
305         return il;
306
307 fail:
308         if (il != NULL)
309                 free_interface_list(il);
310         g_free(ifc.ifc_buf);
311         close(sock);
312         *err = CANT_GET_INTERFACE_LIST;
313         return NULL;
314 #endif /* HAVE_PCAP_FINDALLDEVS */
315 }
316
317 #ifndef HAVE_PCAP_FINDALLDEVS
318 static void
319 search_for_if_cb(gpointer data, gpointer user_data)
320 {
321         struct search_user_data *search_user_data = user_data;
322         if_info_t *if_info = data;
323
324         if (strcmp(if_info->name, search_user_data->name) == 0)
325                 search_user_data->if_info = if_info;
326 }
327 #endif /* HAVE_PCAP_FINDALLDEVS */
328
329 /*
330  * Get an error message string for a CANT_GET_INTERFACE_LIST error from
331  * "get_interface_list()".
332  */
333 gchar *
334 cant_get_if_list_error_message(const char *err_str)
335 {
336         return g_strdup_printf("Can't get list of interfaces: %s", err_str);
337 }
338
339 /*
340  * Append the version of libpcap with which we were compiled to a GString.
341  */
342 void
343 get_compiled_pcap_version(GString *str)
344 {
345 #ifdef HAVE_PCAP_VERSION
346         extern char pcap_version[];
347
348         g_string_append_printf(str, "with libpcap %s", pcap_version);
349 #else
350         g_string_append(str, "with libpcap (version unknown)");
351 #endif
352 }
353
354 /*
355  * Append the version of libpcap with which we we're running to a GString.
356  */
357 void
358 get_runtime_pcap_version(GString *str)
359 {
360         g_string_append_printf(str, "with ");
361 #ifdef HAVE_PCAP_LIB_VERSION
362         g_string_append_printf(str, pcap_lib_version());
363 #else
364         g_string_append(str, "libpcap (version unknown)");
365 #endif
366 }
367
368 #else /* HAVE_LIBPCAP */
369
370 /*
371  * Append an indication that we were not compiled with libpcap
372  * to a GString.
373  */
374 void
375 get_compiled_pcap_version(GString *str)
376 {
377         g_string_append(str, "without libpcap");
378 }
379
380 /*
381  * Don't append anything, as we weren't even compiled to use WinPcap.
382  */
383 void
384 get_runtime_pcap_version(GString *str _U_)
385 {
386 }
387
388 #endif /* HAVE_LIBPCAP */