Squelch an MSVC++ complaint.
[obnox/wireshark/wip.git] / pcap-util.c
1 /* pcap-util.c
2  * Utility routines for packet capture
3  *
4  * $Id: pcap-util.c,v 1.3 2001/11/09 08:16:24 guy Exp $
5  *
6  * Ethereal - Network traffic analyzer
7  * By Gerald Combs <gerald@ethereal.com>
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 #ifdef HAVE_LIBPCAP
30
31 #include <glib.h>
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_TYPES_H
43 #include <sys/types.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 #include <pcap.h>
55
56 #ifndef WIN32
57 #include <net/if.h>
58 #endif
59
60 #ifdef HAVE_SYS_SOCKIO_H
61 # include <sys/sockio.h>
62 #endif
63
64 #include "globals.h"
65
66 #ifdef WIN32
67 #include "capture-wpcap.h"
68 #endif
69
70 #include "pcap-util.h"
71
72 /*
73  * Get the data-link type for a libpcap device.
74  * This works around AIX 5.x's non-standard and incompatible-with-the-
75  * rest-of-the-universe libpcap.
76  */
77 int
78 get_pcap_linktype(pcap_t *pch, char *devname)
79 {
80         int linktype;
81 #ifdef AIX
82         char *ifacename;
83 #endif
84
85         linktype = pcap_datalink(pch);
86 #ifdef AIX
87
88         /*
89          * The libpcap that comes with AIX 5.x uses RFC 1573 ifType values
90          * rather than DLT_ values for link-layer types; the ifType values
91          * for LAN devices are:
92          *
93          *      Ethernet        6
94          *      802.3           7
95          *      Token Ring      9
96          *      FDDI            15
97          *
98          * The AIX names for LAN devices begin with:
99          *
100          *      Ethernet                en
101          *      802.3                   et
102          *      Token Ring              tr
103          *      FDDI                    fi
104          *
105          * (The difference between "Ethernet" and "802.3" is presumably
106          * whether packets have an Ethernet header, with a packet type,
107          * or an 802.3 header, with a packet length, followed by an 802.2
108          * header and possibly a SNAP header.)
109          *
110          * If the device name matches "linktype" interpreted as an ifType
111          * value, rather than as a DLT_ value, we will assume this is AIX's
112          * non-standard, incompatible libpcap, rather than a standard libpcap,
113          * and will map the link-layer type to the standard DLT_ value for
114          * that link-layer type, as that's what the rest of Ethereal expects.
115          *
116          * (This means the capture files won't be readable by a tcpdump
117          * linked with AIX's non-standard libpcap, but so it goes.  They
118          * *will* be readable by standard versions of tcpdump, Ethereal,
119          * and so on.)
120          *
121          * XXX - if we conclude we're using AIX libpcap, should we also
122          * set a flag to cause us to assume the time stamps are in
123          * seconds-and-nanoseconds form, and to convert them to
124          * seconds-and-microseconds form before processing them and
125          * writing them out?
126          */
127
128         /*
129          * Find the last component of the device name, which is the
130          * interface name.
131          */
132         ifacename = strchr(devname, '/');
133         if (ifacename == NULL)
134                 ifacename = devnames;
135
136         /* See if it matches any of the LAN device names. */
137         if (strncmp(ifacename, "en", 2) == 0) {
138                 if (linktype == 6) {
139                         /*
140                          * That's the RFC 1573 value for Ethernet; map it
141                          * to DLT_EN10MB.
142                          */
143                         linktype = 1;
144                 }
145         } else if (strncmp(ifacename, "et", 2) == 0) {
146                 if (linktype == 7) {
147                         /*
148                          * That's the RFC 1573 value for 802.3; map it to
149                          * DLT_EN10MB.
150                          * (libpcap, tcpdump, Ethereal, etc. don't care if
151                          * it's Ethernet or 802.3.)
152                          */
153                         linktype = 1;
154                 }
155         } else if (strncmp(ifacename, "tr") == 0) {
156                 if (linktype == 9) {
157                         /*
158                          * That's the RFC 1573 value for 802.5 (Token Ring);
159                          * map it to DLT_IEEE802, which is what's used for
160                          * Token Ring.
161                          */
162                         linktype = 6;
163                 }
164         } else if (strncmp(ifacename, "fi") == 0) {
165                 if (linktype == 15) {
166                         /*
167                          * That's the RFC 1573 value for FDDI; map it to
168                          * DLT_FDDI.
169                          */
170                         linktype = 10;
171                 }
172         }
173 #endif
174
175         return linktype;
176 }
177
178 /*
179  * If the ability to capture packets is added to Wiretap, these
180  * routines should be moved to the Wiretap source (with
181  * "get_interface_list()" and "free_interface_list()" renamed to
182  * "wtap_get_interface_list()" and "wtap_free_interface_list()",
183  * and modified to use Wiretap routines to attempt to open the
184  * interface.
185  */
186
187 struct search_user_data {
188         char    *name;
189         int     found;
190 };
191
192 static void
193 search_for_if_cb(gpointer data, gpointer user_data);
194
195 static void
196 free_if_cb(gpointer data, gpointer user_data);
197
198 #ifndef WIN32
199 GList *
200 get_interface_list(int *err, char *err_str)
201 {
202         GList  *il = NULL;
203         gint    nonloopback_pos = 0;
204         struct  ifreq *ifr, *last;
205         struct  ifconf ifc;
206         struct  ifreq ifrflags;
207         int     sock = socket(AF_INET, SOCK_DGRAM, 0);
208         struct search_user_data user_data;
209         pcap_t *pch;
210         int len, lastlen;
211         char *buf;
212
213         if (sock < 0) {
214                 sprintf(err_str, "Error opening socket: %s",
215                     strerror(errno));
216                 return NULL;
217         }
218
219         /*
220          * This code came from: W. Richard Stevens: "UNIX Network Programming",
221          * Networking APIs: Sockets and XTI, Vol 1, page 434.
222          */
223         lastlen = 0;
224         len = 100 * sizeof(struct ifreq);
225         for ( ; ; ) {
226                 buf = g_malloc(len);
227                 ifc.ifc_len = len;
228                 ifc.ifc_buf = buf;
229                 memset (buf, 0, len);
230                 if (ioctl(sock, SIOCGIFCONF, &ifc) < 0) {
231                         if (errno != EINVAL || lastlen != 0) {
232                                 sprintf(err_str,
233                                         "SIOCGIFCONF ioctl error getting list of interfaces: %s",
234                                         strerror(errno));
235                                 goto fail;
236                         }
237                 } else {
238                         if ((unsigned) ifc.ifc_len < sizeof(struct ifreq)) {
239                                 sprintf(err_str,
240                                         "SIOCGIFCONF ioctl gave too small return buffer");
241                                 goto fail;
242                         }
243                         if (ifc.ifc_len == lastlen)
244                                 break;                  /* success, len has not changed */
245                         lastlen = ifc.ifc_len;
246                 }
247                 len += 10 * sizeof(struct ifreq);       /* increment */
248                 g_free(buf);
249         }
250         ifr = (struct ifreq *) ifc.ifc_req;
251         last = (struct ifreq *) ((char *) ifr + ifc.ifc_len);
252         while (ifr < last) {
253                 /*
254                  * Skip addresses that begin with "dummy", or that include
255                  * a ":" (the latter are Solaris virtuals).
256                  */
257                 if (strncmp(ifr->ifr_name, "dummy", 5) == 0 ||
258                     strchr(ifr->ifr_name, ':') != NULL)
259                         goto next;
260
261                 /*
262                  * If we already have this interface name on the list,
263                  * don't add it (SIOCGIFCONF returns, at least on
264                  * BSD-flavored systems, one entry per interface *address*;
265                  * if an interface has multiple addresses, we get multiple
266                  * entries for it).
267                  */
268                 user_data.name = ifr->ifr_name;
269                 user_data.found = FALSE;
270                 g_list_foreach(il, search_for_if_cb, &user_data);
271                 if (user_data.found)
272                         goto next;
273
274                 /*
275                  * Get the interface flags.
276                  */
277                 memset(&ifrflags, 0, sizeof ifrflags);
278                 strncpy(ifrflags.ifr_name, ifr->ifr_name,
279                     sizeof ifrflags.ifr_name);
280                 if (ioctl(sock, SIOCGIFFLAGS, (char *)&ifrflags) < 0) {
281                         if (errno == ENXIO)
282                                 goto next;
283                         sprintf(err_str, "SIOCGIFFLAGS error getting flags for interface %s: %s",
284                             ifr->ifr_name, strerror(errno));
285                         goto fail;
286                 }
287
288                 /*
289                  * Skip interfaces that aren't up.
290                  */
291                 if (!(ifrflags.ifr_flags & IFF_UP))
292                         goto next;
293
294                 /*
295                  * Skip interfaces that we can't open with "libpcap".
296                  * Open with the minimum packet size - it appears that the
297                  * IRIX SIOCSNOOPLEN "ioctl" may fail if the capture length
298                  * supplied is too large, rather than just truncating it.
299                  */
300                 pch = pcap_open_live(ifr->ifr_name, MIN_PACKET_SIZE, 0, 0,
301                     err_str);
302                 if (pch == NULL)
303                         goto next;
304                 pcap_close(pch);
305
306                 /*
307                  * If it's a loopback interface, add it at the end of the
308                  * list, otherwise add it after the last non-loopback
309                  * interface, so all loopback interfaces go at the end - we
310                  * don't want a loopback interface to be the default capture
311                  * device unless there are no non-loopback devices.
312                  */
313                 if ((ifrflags.ifr_flags & IFF_LOOPBACK) ||
314                     strncmp(ifr->ifr_name, "lo", 2) == 0)
315                         il = g_list_insert(il, g_strdup(ifr->ifr_name), -1);
316                 else {
317                         il = g_list_insert(il, g_strdup(ifr->ifr_name),
318                             nonloopback_pos);
319                         /*
320                          * Insert the next non-loopback interface after this
321                          * one.
322                          */
323                         nonloopback_pos++;
324                 }
325
326         next:
327 #ifdef HAVE_SA_LEN
328                 ifr = (struct ifreq *) ((char *) ifr +
329                     (ifr->ifr_addr.sa_len > sizeof(ifr->ifr_addr) ?
330                         ifr->ifr_addr.sa_len : sizeof(ifr->ifr_addr)) +
331                     IFNAMSIZ);
332 #else
333                 ifr = (struct ifreq *) ((char *) ifr + sizeof(struct ifreq));
334 #endif
335         }
336
337 #ifdef linux
338         /*
339          * OK, maybe we have support for the "any" device, to do a cooked
340          * capture on all interfaces at once.
341          * Try opening it and, if that succeeds, add it to the end of
342          * the list of interfaces.
343          */
344         pch = pcap_open_live("any", MIN_PACKET_SIZE, 0, 0, err_str);
345         if (pch != NULL) {
346                 /*
347                  * It worked; we can use the "any" device.
348                  */
349                 il = g_list_insert(il, g_strdup("any"), -1);
350                 pcap_close(pch);
351         }
352 #endif
353
354         g_free(ifc.ifc_buf);
355         close(sock);
356
357         if (il == NULL) {
358                 /*
359                  * No interfaces found.
360                  */
361                 *err = NO_INTERFACES_FOUND;
362         }
363         return il;
364
365 fail:
366         if (il != NULL) {
367                 g_list_foreach(il, free_if_cb, NULL);
368                 g_list_free(il);
369         }
370         g_free(ifc.ifc_buf);
371         close(sock);
372         *err = CANT_GET_INTERFACE_LIST;
373         return NULL;
374 }
375
376 static void
377 search_for_if_cb(gpointer data, gpointer user_data)
378 {
379         struct search_user_data *search_user_data = user_data;
380
381         if (strcmp((char *)data, search_user_data->name) == 0)
382                 search_user_data->found = TRUE;
383 }
384 #else
385 GList *
386 get_interface_list(int *err, char *err_str) {
387   GList  *il = NULL;
388   wchar_t *names;
389   char *win95names; 
390   char newname[255];
391   int i, j, done;
392   
393   names = (wchar_t *)pcap_lookupdev(err_str);
394   i = done = 0;
395
396   if (names) {
397           if (names[0]<256) { 
398                   /* If names[0] is less than 256 it means the first byte is 0
399                      This implies that we are using unicode characters */
400                   do 
401                   { 
402                           j = 0; 
403                           while (names[i] != 0) 
404                                   newname[j++] = names[i++]; 
405                           i++; 
406                           if (names[i] == 0) 
407                                   done = 1; 
408                           newname[j++] = 0; 
409                           il = g_list_append(il, g_strdup(newname)); 
410                   } while (!done); 
411           } 
412           else { 
413                   /* Otherwise we are in Windows 95/98 and using ascii(8 bit)
414                      characters */
415                   do 
416                   { 
417                           win95names=(char *)names; 
418                           j = 0; 
419                           while (win95names[i] != 0) 
420                                   newname[j++] = win95names[i++]; 
421                           i++; 
422                           if (win95names[i] == 0) 
423                                   done = 1; 
424                           newname[j++] = 0; 
425                           il = g_list_append(il, g_strdup(newname)); 
426                   } while (!done); 
427           } 
428   }
429   return(il);
430 }
431 #endif
432
433 static void
434 free_if_cb(gpointer data, gpointer user_data)
435 {
436         g_free(data);
437 }
438
439 void
440 free_interface_list(GList *if_list)
441 {
442         while (if_list != NULL) {
443                 g_free(if_list->data);
444                 if_list = g_list_remove_link(if_list, if_list);
445         }
446 }
447
448 #endif /* HAVE_LIBPCAP */