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