Fetch an indication of whether the interface supports capturing in
[obnox/wireshark/wip.git] / capture-pcap-util.c
index fd4d0525a3ee758bacf7604fb88bc1eccb0f9ae3..1850b10acd203f3b93d4251e61b5aa2ad9ec39ce 100644 (file)
@@ -35,8 +35,6 @@
 #include <limits.h>
 #include <string.h>
 
-#include <pcap.h>
-
 #ifdef HAVE_SYS_TYPES_H
 # include <sys/types.h>
 #endif
@@ -48,6 +46,7 @@
 #include <wtap.h>
 #include <libpcap.h>
 
+#include "capture_ifinfo.h"
 #include "capture-pcap-util.h"
 #include "capture-pcap-util-int.h"
 
@@ -184,13 +183,13 @@ if_info_new(char *name, char *description)
 {
        if_info_t *if_info;
 
-       if_info = g_malloc(sizeof (if_info_t));
+       if_info = (if_info_t *)g_malloc(sizeof (if_info_t));
        if_info->name = g_strdup(name);
        if (description == NULL)
                if_info->description = NULL;
        else
                if_info->description = g_strdup(description);
-       if_info->ip_addr = NULL;
+       if_info->addrs = NULL;
        if_info->loopback = FALSE;
        return if_info;
 }
@@ -198,7 +197,7 @@ if_info_new(char *name, char *description)
 void
 if_info_add_address(if_info_t *if_info, struct sockaddr *addr)
 {
-       if_addr_t *ip_addr;
+       if_addr_t *if_addr;
        struct sockaddr_in *ai;
 #ifdef INET6
        struct sockaddr_in6 *ai6;
@@ -208,22 +207,22 @@ if_info_add_address(if_info_t *if_info, struct sockaddr *addr)
 
        case AF_INET:
                ai = (struct sockaddr_in *)addr;
-               ip_addr = g_malloc(sizeof(*ip_addr));
-               ip_addr->type = AT_IPv4;
-               ip_addr->ip_addr.ip4_addr =
+               if_addr = (if_addr_t *)g_malloc(sizeof(*if_addr));
+               if_addr->ifat_type = IF_AT_IPv4;
+               if_addr->addr.ip4_addr =
                    *((guint32 *)&(ai->sin_addr.s_addr));
-               if_info->ip_addr = g_slist_append(if_info->ip_addr, ip_addr);
+               if_info->addrs = g_slist_append(if_info->addrs, if_addr);
                break;
 
 #ifdef INET6
        case AF_INET6:
                ai6 = (struct sockaddr_in6 *)addr;
-               ip_addr = g_malloc(sizeof(*ip_addr));
-               ip_addr->type = AT_IPv6;
-               memcpy((void *)&ip_addr->ip_addr.ip6_addr,
+               if_addr = (if_addr_t *)g_malloc(sizeof(*if_addr));
+               if_addr->ifat_type = IF_AT_IPv6;
+               memcpy((void *)&if_addr->addr.ip6_addr,
                    (void *)&ai6->sin6_addr.s6_addr,
-                   sizeof ip_addr->ip_addr.ip6_addr);
-               if_info->ip_addr = g_slist_append(if_info->ip_addr, ip_addr);
+                   sizeof if_addr->addr.ip6_addr);
+               if_info->addrs = g_slist_append(if_info->addrs, if_addr);
                break;
 #endif
        }
@@ -333,13 +332,13 @@ free_if_info_addr_cb(gpointer addr, gpointer user_data _U_)
 static void
 free_if_cb(gpointer data, gpointer user_data _U_)
 {
-       if_info_t *if_info = data;
+       if_info_t *if_info = (if_info_t *)data;
 
        g_free(if_info->name);
        g_free(if_info->description);
 
-       g_slist_foreach(if_info->ip_addr, free_if_info_addr_cb, NULL);
-       g_slist_free(if_info->ip_addr);
+       g_slist_foreach(if_info->addrs, free_if_info_addr_cb, NULL);
+       g_slist_free(if_info->addrs);
        g_free(if_info);
 }
 
@@ -439,125 +438,21 @@ pcap_datalink_val_to_description(int dlt)
 
 #endif /* !defined(HAVE_PCAP_DATALINK_VAL_TO_NAME) || !defined(HAVE_PCAP_DATALINK_VAL_TO_DESCRIPTION) */
 
-/*
- * Get the data-link types available for a libpcap device.
- */
-static data_link_info_t *
-create_data_link_info(int dlt)
-{
-       data_link_info_t *data_link_info;
-       const char *text;
-
-       data_link_info = g_malloc(sizeof (data_link_info_t));
-       data_link_info->dlt = dlt;
-       text = pcap_datalink_val_to_name(dlt);
-       if (text != NULL)
-               data_link_info->name = g_strdup(text);
-       else
-               data_link_info->name = g_strdup_printf("DLT %d", dlt);
-       text = pcap_datalink_val_to_description(dlt);
-       if (text != NULL)
-               data_link_info->description = g_strdup(text);
-       else
-               data_link_info->description = NULL;
-       return data_link_info;
-}
-
-GList *
-get_pcap_linktype_list(const char *devname, char **err_str)
-{
-       GList *linktype_list = NULL;
-       pcap_t *pch;
-       int deflt;
-       char errbuf[PCAP_ERRBUF_SIZE];
-#ifdef HAVE_PCAP_LIST_DATALINKS
-       int *linktypes;
-       int i, nlt;
-#endif
-       data_link_info_t *data_link_info;
-
-#ifdef HAVE_PCAP_OPEN
-       pch = pcap_open(devname, MIN_PACKET_SIZE, 0, 0, NULL, errbuf);
-#else
-       pch = pcap_open_live(devname, MIN_PACKET_SIZE, 0, 0, errbuf);
-#endif
-       if (pch == NULL) {
-               if (err_str != NULL)
-                       *err_str = g_strdup(errbuf);
-               return NULL;
-       }
-       deflt = get_pcap_linktype(pch, devname);
-#ifdef HAVE_PCAP_LIST_DATALINKS
-       nlt = pcap_list_datalinks(pch, &linktypes);
-       if (nlt == 0 || linktypes == NULL) {
-               pcap_close(pch);
-               if (err_str != NULL)
-                       *err_str = NULL;        /* an empty list doesn't mean an error */
-               return NULL;
-       }
-       for (i = 0; i < nlt; i++) {
-               data_link_info = create_data_link_info(linktypes[i]);
-
-               /*
-                * XXX - for 802.11, make the most detailed 802.11
-                * version the default, rather than the one the
-                * device has as the default?
-                */
-               if (linktypes[i] == deflt)
-                       linktype_list = g_list_prepend(linktype_list,
-                           data_link_info);
-               else
-                       linktype_list = g_list_append(linktype_list,
-                           data_link_info);
-       }
-#ifdef HAVE_PCAP_FREE_DATALINKS
-       pcap_free_datalinks(linktypes);
-#else
-       /*
-        * In Windows, there's no guarantee that if you have a library
-        * built with one version of the MSVC++ run-time library, and
-        * it returns a pointer to allocated data, you can free that
-        * data from a program linked with another version of the
-        * MSVC++ run-time library.
-        *
-        * This is not an issue on UN*X.
-        *
-        * See the mail threads starting at
-        *
-        *      http://www.winpcap.org/pipermail/winpcap-users/2006-September/001421.html
-        *
-        * and
-        *
-        *      http://www.winpcap.org/pipermail/winpcap-users/2008-May/002498.html
-        */
-#ifndef _WIN32
-#define xx_free free  /* hack so checkAPIs doesn't complain */
-       xx_free(linktypes);
-#endif /* _WIN32 */
-#endif /* HAVE_PCAP_FREE_DATALINKS */
-#else /* HAVE_PCAP_LIST_DATALINKS */
-       data_link_info = create_data_link_info(deflt);
-       linktype_list = g_list_append(linktype_list, data_link_info);
-#endif /* HAVE_PCAP_LIST_DATALINKS */
-
-       pcap_close(pch);
-       return linktype_list;
-}
-
 static void
 free_linktype_cb(gpointer data, gpointer user_data _U_)
 {
-       data_link_info_t *linktype_info = data;
+       data_link_info_t *linktype_info = (data_link_info_t *)data;
 
        g_free(linktype_info->name);
        g_free(linktype_info->description);
 }
 
 void
-free_pcap_linktype_list(GList *linktype_list)
+free_if_capabilities(if_capabilities_t *caps)
 {
-       g_list_foreach(linktype_list, free_linktype_cb, NULL);
-       g_list_free(linktype_list);
+       g_list_foreach(caps->data_link_types, free_linktype_cb, NULL);
+       g_list_free(caps->data_link_types);
+       g_free(caps);
 }
 
 const char *