Don't call gtk_widget_unregister_window() in bytes_view_unrealize() after GTK+ 3.8.0
[metze/wireshark/wip.git] / capture-pcap-util.c
1 /* capture-pcap-util.c
2  * 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 #ifdef HAVE_LIBPCAP
28
29 #include <glib.h>
30
31 #include <stdlib.h>
32 #include <stdio.h>
33 #include <limits.h>
34 #include <string.h>
35
36 #ifdef HAVE_SYS_TYPES_H
37 # include <sys/types.h>
38 #endif
39
40 #ifdef HAVE_SYS_SOCKET_H
41 #include <sys/socket.h>
42 #endif
43
44 #include <wtap.h>
45 #include <libpcap.h>
46
47 #include "capture_ifinfo.h"
48 #include "capture-pcap-util.h"
49 #include "capture-pcap-util-int.h"
50
51 #include "wsutil/file_util.h"
52
53 #ifndef _WIN32
54 #include <netinet/in.h>
55 #endif
56
57 #ifdef _WIN32
58 #include "capture_win_ifnames.h" /* windows friendly interface names */
59 #endif
60
61 /*
62  * Given an interface name, find the "friendly name" and interface
63  * type for the interface.
64  */
65
66 #if defined(__APPLE__)
67
68 #include <CoreFoundation/CoreFoundation.h>
69 #include <SystemConfiguration/SystemConfiguration.h>
70
71 #include "cfutils.h"
72
73 /*
74  * On OS X, we get the "friendly name" and interface type for the interface
75  * from the System Configuration framework.
76  *
77  * To find the System Configuration framework information for the
78  * interface, we get all the interfaces that the System Configuration
79  * framework knows about and look for the one with a "BSD name" matching
80  * the interface name.
81  *
82  * If we find it, we use its "localized display name", if it has one, as
83  * the "friendly name".
84  *
85  * As for the interface type:
86  *
87  * Yes, fetching all the network addresses for an interface gets you an
88  * AF_LINK address, of type "struct sockaddr_dl", and, yes, that includes
89  * an SNMP MIB-II ifType value.
90  *
91  * However, it's IFT_ETHER, i.e. Ethernet, for AirPort interfaces,
92  * not IFT_IEEE80211 (which isn't defined in OS X in any case).
93  *
94  * Perhaps some other BSD-flavored OSes won't make this mistake;
95  * however, FreeBSD 7.0 and OpenBSD 4.2, at least, appear to have
96  * made the same mistake, at least for my Belkin ZyDAS stick.
97  *
98  * SCNetworkInterfaceGetInterfaceType() will get the interface
99  * type.  The interface type is a CFString, and:
100  *
101  *    kSCNetworkInterfaceTypeIEEE80211 means IF_WIRELESS;
102  *    kSCNetworkInterfaceTypeBluetooth means IF_BLUETOOTH;
103  *    kSCNetworkInterfaceTypeModem or
104  *    kSCNetworkInterfaceTypePPP or
105  *    maybe kSCNetworkInterfaceTypeWWAN means IF_DIALUP
106  */
107 static void
108 add_unix_interface_ifinfo(if_info_t *if_info, const char *name,
109     const char *description _U_)
110 {
111         CFStringRef name_CFString;
112         CFArrayRef interfaces;
113         CFIndex num_interfaces;
114         CFIndex i;
115         SCNetworkInterfaceRef interface;
116         CFStringRef bsdname_CFString;
117         CFStringRef friendly_name_CFString;
118         CFStringRef interface_type_CFString;
119
120         interfaces = SCNetworkInterfaceCopyAll();
121         if (interfaces == NULL) {
122                 /*
123                  * Couldn't get a list of interfaces.
124                  */
125                 return;
126         }
127
128         name_CFString = CFStringCreateWithCString(kCFAllocatorDefault,
129             name, kCFStringEncodingUTF8);
130         if (name_CFString == NULL) {
131                 /*
132                  * Couldn't convert the interface name to a CFString.
133                  */
134                 CFRelease(interfaces);
135                 return;
136         }
137
138         num_interfaces = CFArrayGetCount(interfaces);
139         for (i = 0; i < num_interfaces; i++) {
140                 interface = (SCNetworkInterfaceRef)CFArrayGetValueAtIndex(interfaces, i);
141                 bsdname_CFString = SCNetworkInterfaceGetBSDName(interface);
142                 if (bsdname_CFString == NULL) {
143                         /*
144                          * This interface has no BSD name, so it's not
145                          * a regular network interface.
146                          */
147                         continue;
148                 }
149                 if (CFStringCompare(name_CFString, bsdname_CFString, 0) == 0) {
150                         /*
151                          * This is the interface.
152                          * First, get the friendly name.
153                          */
154                         friendly_name_CFString = SCNetworkInterfaceGetLocalizedDisplayName(interface);
155                         if (friendly_name_CFString != NULL)
156                                 if_info->friendly_name = CFString_to_C_string(friendly_name_CFString);
157
158                         /*
159                          * Now get the interface type.
160                          */
161                         interface_type_CFString = SCNetworkInterfaceGetInterfaceType(interface);
162                         if (CFStringCompare(interface_type_CFString,
163                             kSCNetworkInterfaceTypeIEEE80211, 0) == kCFCompareEqualTo)
164                                 if_info->type = IF_WIRELESS;
165                         else if (CFStringCompare(interface_type_CFString,
166                             kSCNetworkInterfaceTypeBluetooth, 0) == kCFCompareEqualTo)
167                                 if_info->type = IF_BLUETOOTH;
168                         else if (CFStringCompare(interface_type_CFString,
169                             kSCNetworkInterfaceTypeModem, 0) == kCFCompareEqualTo)
170                                 if_info->type = IF_DIALUP;
171                         else if (CFStringCompare(interface_type_CFString,
172                             kSCNetworkInterfaceTypePPP, 0) == kCFCompareEqualTo)
173                                 if_info->type = IF_DIALUP;
174                         else if (CFStringCompare(interface_type_CFString,
175                             kSCNetworkInterfaceTypeWWAN, 0) == kCFCompareEqualTo)
176                                 if_info->type = IF_DIALUP;
177                         else
178                                 if_info->type = IF_WIRED;
179                         break;
180                 }
181         }
182
183         CFRelease(interfaces);
184 }
185 #elif defined(__linux__)
186 /*
187  * Linux doesn't offer any form of "friendly name", but you can
188  * determine an interface type to some degree.
189  */
190 static void
191 add_unix_interface_ifinfo(if_info_t *if_info, const char *name,
192     const char *description _U_)
193 {
194         char *wireless_path;
195         ws_statb64 statb;
196
197         /*
198          * Look for /sys/class/net/{device}/wireless.  If it exists,
199          * it's a wireless interface.
200          */
201         wireless_path = g_strdup_printf("/sys/class/net/%s/wireless", name);
202         if (wireless_path != NULL) {
203                 if (ws_stat64(wireless_path, &statb) == 0)
204                         if_info->type = IF_WIRELESS;
205                 g_free(wireless_path);
206         }
207         if (if_info->type == IF_WIRED) {
208                 /*
209                  * We still don't know what it is.  Check for
210                  * Bluetooth and USB devices.
211                  */
212                 if (strstr(name, "bluetooth") != NULL) {
213                         /*
214                          * XXX - this is for raw Bluetooth capture; what
215                          * about IP-over-Bluetooth devices?
216                          */
217                         if_info->type = IF_BLUETOOTH;
218                 } else if (strstr(name, "usbmon") != NULL)
219                         if_info->type = IF_USB;
220         }
221 }
222 #else
223 /*
224  * On other UN*Xes, if there is a description, it's a friendly
225  * name, and there is no vendor description.  ("Other UN*Xes"
226  * currently means "FreeBSD and OpenBSD".)
227  */
228 void
229 add_unix_interface_ifinfo(if_info_t *if_info, const char *name _U_,
230     const char *description)
231 {
232         if_info->friendly_name = g_strdup(description);
233 }
234 #endif
235
236 if_info_t *
237 if_info_new(const char *name, const char *description, gboolean loopback)
238 {
239         if_info_t *if_info;
240 #ifdef _WIN32
241         const char *guid_text;
242         GUID guid;
243 #endif
244
245         if_info = (if_info_t *)g_malloc(sizeof (if_info_t));
246         if_info->name = g_strdup(name);
247         if_info->friendly_name = NULL;  /* default - unknown */
248         if_info->vendor_description = NULL;
249         if_info->type = IF_WIRED;       /* default */
250 #ifdef _WIN32
251         /*
252          * Get the interface type.
253          *
254          * Much digging failed to reveal any obvious way to get something
255          * such as the SNMP MIB-II ifType value for an interface:
256          *
257          *    http://www.iana.org/assignments/ianaiftype-mib
258          *
259          * by making some NDIS request.  And even if there were such
260          * a way, there's no guarantee that the ifType reflects an
261          * interface type that a user would view as correct (for
262          * example, some systems report Wi-Fi interfaces as
263          * Ethernet interfaces).
264          *
265          * So we look for keywords in the vendor's interface
266          * description.
267          */
268         if (description && (strstr(description, "generic dialup") != NULL ||
269             strstr(description, "PPP/SLIP") != NULL)) {
270                 if_info->type = IF_DIALUP;
271         } else if (description && (strstr(description, "Wireless") != NULL ||
272             strstr(description,"802.11") != NULL)) {
273                 if_info->type = IF_WIRELESS;
274         } else if (description && strstr(description, "AirPcap") != NULL ||
275             strstr(name, "airpcap") != NULL) {
276                 if_info->type = IF_AIRPCAP;
277         } else if (description && strstr(description, "Bluetooth") != NULL ) {
278                 if_info->type = IF_BLUETOOTH;
279         } else if (description && strstr(description, "VMware") != NULL) {
280                 /*
281                  * Bridge, NAT, or host-only interface on a VMware host.
282                  *
283                  * XXX - what about guest interfaces?
284                  */
285                 if_info->type = IF_VIRTUAL;
286         }
287
288         /*
289          * On Windows, the "description" is a vendor description,
290          * and the friendly name isn't returned by WinPcap.
291          * Fetch it ourselves.
292          */
293
294         /*
295          * Skip over the "\Device\NPF_" prefix in the device name,
296          * if present.
297          */
298         if (strncmp("\\Device\\NPF_", name, 12) == 0)
299                 guid_text = name + 12;
300         else
301                 guid_text = name;
302
303         /* Now try to parse what remains as a GUID. */
304         if (parse_as_guid(guid_text, &guid)) {
305                 /*
306                  * Success. Try to get a friendly name using the GUID.
307                  * As this is a regular interface, the description is a
308                  * vendor description.
309                  */
310                 if_info->friendly_name = get_interface_friendly_name_from_device_guid(&guid);
311                 if_info->vendor_description = g_strdup(description);
312         } else {
313                 /*
314                  * This is probably not a regular interface; we only
315                  * support NT 5 (W2K) and later, so all regular interfaces
316                  * should have GUIDs at the end of the name.  Therefore,
317                  * the description, if supplied, is a friendly name
318                  * provided by WinPcap, and there is no vendor
319                  * description.
320                  */
321                 if_info->friendly_name = g_strdup(description);
322                 if_info->vendor_description = NULL;
323         }
324 #else
325         /*
326          * On UN*X, if there is a description, it's a friendly
327          * name, and there is no vendor description.
328          *
329          * Try the platform's way of getting a friendly name and
330          * interface type first.
331          *
332          * If that fails, then, for a loopback interface, give it the
333          * friendly name "Loopback" and, for VMware interfaces,
334          * give them the type IF_VIRTUAL.
335          */
336         add_unix_interface_ifinfo(if_info, name, description);
337         if (if_info->type == IF_WIRED) {
338                 /*
339                  * This is the default interface type.
340                  *
341                  * Bridge, NAT, or host-only interfaces on VMWare hosts
342                  * have the name vmnet[0-9]+. Guests might use a native
343                  * (LANCE or E1000) driver or the vmxnet driver.  Check
344                  * the name.
345                  */
346                 if (g_ascii_strncasecmp(name, "vmnet", 5) == 0)
347                         if_info->type = IF_VIRTUAL;
348                 else if (g_ascii_strncasecmp(name, "vmxnet", 6) == 0)
349                         if_info->type = IF_VIRTUAL;
350         }
351         if (if_info->friendly_name == NULL) {
352                 /*
353                  * We couldn't get interface information using platform-
354                  * dependent calls.
355                  *
356                  * If this is a loopback interface, give it a
357                  * "friendly name" of "Loopback".
358                  */
359                 if (loopback)
360                         if_info->friendly_name = g_strdup("Loopback");
361         }
362         if_info->vendor_description = NULL;
363 #endif
364         if_info->loopback = loopback;
365         if_info->addrs = NULL;
366         return if_info;
367 }
368
369 void
370 if_info_add_address(if_info_t *if_info, struct sockaddr *addr)
371 {
372         if_addr_t *if_addr;
373         struct sockaddr_in *ai;
374 #ifdef INET6
375         struct sockaddr_in6 *ai6;
376 #endif
377
378         switch (addr->sa_family) {
379
380         case AF_INET:
381                 ai = (struct sockaddr_in *)(void *)addr;
382                 if_addr = (if_addr_t *)g_malloc(sizeof(*if_addr));
383                 if_addr->ifat_type = IF_AT_IPv4;
384                 if_addr->addr.ip4_addr =
385                     *((guint32 *)&(ai->sin_addr.s_addr));
386                 if_info->addrs = g_slist_append(if_info->addrs, if_addr);
387                 break;
388
389 #ifdef INET6
390         case AF_INET6:
391                 ai6 = (struct sockaddr_in6 *)(void *)addr;
392                 if_addr = (if_addr_t *)g_malloc(sizeof(*if_addr));
393                 if_addr->ifat_type = IF_AT_IPv6;
394                 memcpy((void *)&if_addr->addr.ip6_addr,
395                     (void *)&ai6->sin6_addr.s6_addr,
396                     sizeof if_addr->addr.ip6_addr);
397                 if_info->addrs = g_slist_append(if_info->addrs, if_addr);
398                 break;
399 #endif
400         }
401 }
402
403 #ifdef HAVE_PCAP_FINDALLDEVS
404 /*
405  * Get all IP address information for the given interface.
406  */
407 static void
408 if_info_ip(if_info_t *if_info, pcap_if_t *d)
409 {
410         pcap_addr_t *a;
411
412         /* All addresses */
413         for (a = d->addresses; a != NULL; a = a->next) {
414                 if (a->addr != NULL)
415                         if_info_add_address(if_info, a->addr);
416         }
417 }
418
419 #ifdef HAVE_PCAP_REMOTE
420 GList *
421 get_interface_list_findalldevs_ex(const char *source,
422                                   struct pcap_rmtauth *auth,
423                                   int *err, char **err_str)
424 {
425         GList  *il = NULL;
426         pcap_if_t *alldevs, *dev;
427         if_info_t *if_info;
428         char errbuf[PCAP_ERRBUF_SIZE];
429
430         if (pcap_findalldevs_ex((char *)source, auth, &alldevs, errbuf) == -1) {
431                 *err = CANT_GET_INTERFACE_LIST;
432                 if (err_str != NULL)
433                         *err_str = cant_get_if_list_error_message(errbuf);
434                 return NULL;
435         }
436
437         if (alldevs == NULL) {
438                 /*
439                  * No interfaces found.
440                  */
441                 *err = NO_INTERFACES_FOUND;
442                 if (err_str != NULL)
443                         *err_str = NULL;
444                 return NULL;
445         }
446
447         for (dev = alldevs; dev != NULL; dev = dev->next) {
448                 if_info = if_info_new(dev->name, dev->description,
449                     (dev->flags & PCAP_IF_LOOPBACK) ? TRUE : FALSE);
450                 il = g_list_append(il, if_info);
451                 if_info_ip(if_info, dev);
452         }
453         pcap_freealldevs(alldevs);
454
455         return il;
456 }
457 #endif
458
459 GList *
460 get_interface_list_findalldevs(int *err, char **err_str)
461 {
462         GList  *il = NULL;
463         pcap_if_t *alldevs, *dev;
464         if_info_t *if_info;
465         char errbuf[PCAP_ERRBUF_SIZE];
466
467         if (pcap_findalldevs(&alldevs, errbuf) == -1) {
468                 *err = CANT_GET_INTERFACE_LIST;
469                 if (err_str != NULL)
470                         *err_str = cant_get_if_list_error_message(errbuf);
471                 return NULL;
472         }
473
474         if (alldevs == NULL) {
475                 /*
476                  * No interfaces found.
477                  */
478                 *err = NO_INTERFACES_FOUND;
479                 if (err_str != NULL)
480                         *err_str = NULL;
481                 return NULL;
482         }
483
484         for (dev = alldevs; dev != NULL; dev = dev->next) {
485                 if_info = if_info_new(dev->name, dev->description,
486                     (dev->flags & PCAP_IF_LOOPBACK) ? TRUE : FALSE);
487                 il = g_list_append(il, if_info);
488                 if_info_ip(if_info, dev);
489         }
490         pcap_freealldevs(alldevs);
491
492         return il;
493 }
494 #endif /* HAVE_PCAP_FINDALLDEVS */
495
496 static void
497 free_if_info_addr_cb(gpointer addr, gpointer user_data _U_)
498 {
499         g_free(addr);
500 }
501
502 static void
503 free_if_cb(gpointer data, gpointer user_data _U_)
504 {
505         if_info_t *if_info = (if_info_t *)data;
506
507         g_free(if_info->name);
508         g_free(if_info->friendly_name);
509         g_free(if_info->vendor_description);
510
511         g_slist_foreach(if_info->addrs, free_if_info_addr_cb, NULL);
512         g_slist_free(if_info->addrs);
513         g_free(if_info);
514 }
515
516 void
517 free_interface_list(GList *if_list)
518 {
519         g_list_foreach(if_list, free_if_cb, NULL);
520         g_list_free(if_list);
521 }
522
523 #if !defined(HAVE_PCAP_DATALINK_NAME_TO_VAL) || !defined(HAVE_PCAP_DATALINK_VAL_TO_NAME) || !defined(HAVE_PCAP_DATALINK_VAL_TO_DESCRIPTION)
524 struct dlt_choice {
525         const char *name;
526         const char *description;
527         int     dlt;
528 };
529
530 #define DLT_CHOICE(code, description) { #code, description, code }
531 #define DLT_CHOICE_SENTINEL { NULL, NULL, 0 }
532
533 static struct dlt_choice dlt_choices[] = {
534         DLT_CHOICE(DLT_NULL, "BSD loopback"),
535         DLT_CHOICE(DLT_EN10MB, "Ethernet"),
536         DLT_CHOICE(DLT_IEEE802, "Token ring"),
537         DLT_CHOICE(DLT_ARCNET, "ARCNET"),
538         DLT_CHOICE(DLT_SLIP, "SLIP"),
539         DLT_CHOICE(DLT_PPP, "PPP"),
540         DLT_CHOICE(DLT_FDDI, "FDDI"),
541         DLT_CHOICE(DLT_ATM_RFC1483, "RFC 1483 IP-over-ATM"),
542         DLT_CHOICE(DLT_RAW, "Raw IP"),
543         DLT_CHOICE(DLT_SLIP_BSDOS, "BSD/OS SLIP"),
544         DLT_CHOICE(DLT_PPP_BSDOS, "BSD/OS PPP"),
545         DLT_CHOICE(DLT_ATM_CLIP, "Linux Classical IP-over-ATM"),
546         DLT_CHOICE(DLT_PPP_SERIAL, "PPP over serial"),
547         DLT_CHOICE(DLT_PPP_ETHER, "PPPoE"),
548         DLT_CHOICE(DLT_C_HDLC, "Cisco HDLC"),
549         DLT_CHOICE(DLT_IEEE802_11, "802.11"),
550         DLT_CHOICE(DLT_FRELAY, "Frame Relay"),
551         DLT_CHOICE(DLT_LOOP, "OpenBSD loopback"),
552         DLT_CHOICE(DLT_ENC, "OpenBSD encapsulated IP"),
553         DLT_CHOICE(DLT_LINUX_SLL, "Linux cooked"),
554         DLT_CHOICE(DLT_LTALK, "Localtalk"),
555         DLT_CHOICE(DLT_PFLOG, "OpenBSD pflog file"),
556         DLT_CHOICE(DLT_PRISM_HEADER, "802.11 plus Prism header"),
557         DLT_CHOICE(DLT_IP_OVER_FC, "RFC 2625 IP-over-Fibre Channel"),
558         DLT_CHOICE(DLT_SUNATM, "Sun raw ATM"),
559         DLT_CHOICE(DLT_IEEE802_11_RADIO, "802.11 plus BSD radio information header"),
560         DLT_CHOICE(DLT_APPLE_IP_OVER_IEEE1394, "Apple IP-over-IEEE 1394"),
561         DLT_CHOICE(DLT_ARCNET_LINUX, "Linux ARCNET"),
562         DLT_CHOICE(DLT_LINUX_IRDA, "Linux IrDA"),
563         DLT_CHOICE(DLT_IEEE802_11_RADIO_AVS, "802.11 plus AVS radio information header"),
564         DLT_CHOICE_SENTINEL
565 };
566
567 #if !defined(HAVE_PCAP_DATALINK_NAME_TO_VAL)
568 static int
569 pcap_datalink_name_to_val(const char *name)
570 {
571         int i;
572
573         for (i = 0; dlt_choices[i].name != NULL; i++) {
574                 if (g_ascii_strcasecmp(dlt_choices[i].name + sizeof("DLT_") - 1,
575                     name) == 0)
576                         return (dlt_choices[i].dlt);
577         }
578         return (-1);
579 }
580 #endif /* defined(HAVE_PCAP_DATALINK_NAME_TO_VAL) */
581
582 #if !defined(HAVE_PCAP_DATALINK_VAL_TO_NAME)
583 static const char *
584 pcap_datalink_val_to_name(int dlt)
585 {
586         int i;
587
588         for (i = 0; dlt_choices[i].name != NULL; i++) {
589                 if (dlt_choices[i].dlt == dlt)
590                         return (dlt_choices[i].name + sizeof("DLT_") - 1);
591         }
592         return (NULL);
593 }
594 #endif /* defined(HAVE_PCAP_DATALINK_VAL_TO_NAME) */
595
596 #if !defined(HAVE_PCAP_DATALINK_VAL_TO_DESCRIPTION)
597 const char *
598 pcap_datalink_val_to_description(int dlt)
599 {
600         int i;
601
602         for (i = 0; dlt_choices[i].name != NULL; i++) {
603                 if (dlt_choices[i].dlt == dlt)
604                         return (dlt_choices[i].description);
605         }
606         return (NULL);
607 }
608 #endif /* defined(HAVE_PCAP_DATALINK_VAL_TO_DESCRIPTION) */
609
610 #endif /* !defined(HAVE_PCAP_DATALINK_VAL_TO_NAME) || !defined(HAVE_PCAP_DATALINK_VAL_TO_DESCRIPTION) */
611
612 static void
613 free_linktype_cb(gpointer data, gpointer user_data _U_)
614 {
615         data_link_info_t *linktype_info = (data_link_info_t *)data;
616
617         g_free(linktype_info->name);
618         g_free(linktype_info->description);
619 }
620
621 void
622 free_if_capabilities(if_capabilities_t *caps)
623 {
624         g_list_foreach(caps->data_link_types, free_linktype_cb, NULL);
625         g_list_free(caps->data_link_types);
626         g_free(caps);
627 }
628
629 const char *
630 linktype_val_to_name(int dlt)
631 {
632     return pcap_datalink_val_to_name(dlt);
633 }
634
635 int linktype_name_to_val(const char *linktype)
636 {
637     return pcap_datalink_name_to_val(linktype);
638 }
639
640 #endif /* HAVE_LIBPCAP */