/* pcap-util.c
* Utility routines for packet capture
*
- * $Id: pcap-util.c,v 1.24 2004/02/24 01:11:27 guy Exp $
+ * $Id$
*
* Ethereal - Network traffic analyzer
* By Gerald Combs <gerald@ethereal.com>
#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-
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)
{
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);
}
#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_)
{
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