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