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