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