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