fix doxygen generation
[obnox/wireshark/wip.git] / pcap-util.c
index 0fd6f061c5a693e74e73436b2867dfd4bd359dfc..f2cb31cac6694475938bf6c3cd65bf524fbc1ffd 100644 (file)
@@ -1,7 +1,7 @@
 /* pcap-util.c
  * Utility routines for packet capture
  *
- * $Id: pcap-util.c,v 1.22 2003/11/26 02:54:05 guy Exp $
+ * $Id$
  *
  * Ethereal - Network traffic analyzer
  * By Gerald Combs <gerald@ethereal.com>
@@ -28,6 +28,8 @@
 
 #ifdef HAVE_LIBPCAP
 
+#include <pcap.h>
+
 #include <glib.h>
 
 #include <stdlib.h>
 #include <sys/socket.h>
 #endif
 
-#include <pcap.h>
-
 #include <wtap.h>
 #include <wtap-capture.h>
 
 #include "pcap-util.h"
 #include "pcap-util-int.h"
 
+#ifndef _WIN32
+#include <netinet/in.h>
+#endif
+
+
 /*
  * Get the data-link type for a libpcap device.
  * This works around AIX 5.x's non-standard and incompatible-with-the-
@@ -184,10 +189,65 @@ if_info_new(char *name, char *description)
                if_info->description = NULL;
        else
                if_info->description = g_strdup(description);
+       if_info->ip_addr = NULL;
+       if_info->loopback = FALSE;
        return if_info;
 }
 
+void
+if_info_add_address(if_info_t *if_info, struct sockaddr *addr)
+{
+       if_addr_t *ip_addr;
+       struct sockaddr_in *ai;
+#ifdef INET6
+       struct sockaddr_in6 *ai6;
+#endif
+
+       switch (addr->sa_family) {
+
+       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 =
+                   *((guint32 *)&(ai->sin_addr.s_addr));
+               if_info->ip_addr = g_slist_append(if_info->ip_addr, ip_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,
+                   (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);
+               break;
+#endif
+       }
+}
+
 #ifdef HAVE_PCAP_FINDALLDEVS
+/*
+ * Get all IP address information, and the loopback flag, for the given
+ * interface.
+ */
+static void
+if_info_ip(if_info_t *if_info, pcap_if_t *d)
+{
+       pcap_addr_t *a;
+
+       /* Loopback flag */
+       if_info->loopback = (d->flags & PCAP_IF_LOOPBACK) ? TRUE : FALSE;
+
+       /* All addresses */
+       for (a = d->addresses; a != NULL; a = a->next) {
+               if (a->addr != NULL)
+                       if_info_add_address(if_info, a->addr);
+       }
+}
+
 GList *
 get_interface_list_findalldevs(int *err, char *err_str)
 {
@@ -211,6 +271,7 @@ get_interface_list_findalldevs(int *err, char *err_str)
        for (dev = alldevs; dev != NULL; dev = dev->next) {
                if_info = if_info_new(dev->name, dev->description);
                il = g_list_append(il, if_info);
+               if_info_ip(if_info, dev);
        }
        pcap_freealldevs(alldevs);
 
@@ -218,6 +279,12 @@ get_interface_list_findalldevs(int *err, char *err_str)
 }
 #endif /* HAVE_PCAP_FINDALLDEVS */
 
+static void
+free_if_info_addr_cb(gpointer addr, gpointer user_data _U_)
+{
+       g_free(addr);
+}
+
 static void
 free_if_cb(gpointer data, gpointer user_data _U_)
 {
@@ -226,6 +293,9 @@ free_if_cb(gpointer data, gpointer user_data _U_)
        g_free(if_info->name);
        if (if_info->description != NULL)
                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);
 }
 
 void
@@ -342,7 +412,11 @@ free_pcap_linktype_list(GList *linktype_list)
 
 /* Set the data link type on a pcap. */
 const char *
-set_pcap_linktype(pcap_t *pch, char *devname, int dlt)
+set_pcap_linktype(pcap_t *pch, char *devname
+#ifdef HAVE_PCAP_SET_DATALINK
+       _U_
+#endif
+       , int dlt)
 {
 #ifdef HAVE_PCAP_SET_DATALINK
        if (pcap_set_datalink(pch, dlt) == 0)