[farsight2/master] Move fs-interfaces into core library

Olivier Crête olivier.crete at collabora.co.uk
Tue Dec 23 15:22:30 PST 2008


---
 gst-libs/gst/farsight/Makefile.am                  |    6 +-
 gst-libs/gst/farsight/fs-interfaces.c              |  580 ++++++++++++++++++++
 gst-libs/gst/farsight/fs-interfaces.h              |   31 +
 transmitters/rawudp/Makefile.am                    |    2 -
 transmitters/rawudp/fs-interfaces.c                |  580 --------------------
 transmitters/rawudp/fs-interfaces.h                |   31 -
 transmitters/rawudp/fs-rawudp-component.c          |    2 +-
 transmitters/rawudp/fs-rawudp-stream-transmitter.c |    2 -
 8 files changed, 616 insertions(+), 618 deletions(-)
 create mode 100644 gst-libs/gst/farsight/fs-interfaces.c
 create mode 100644 gst-libs/gst/farsight/fs-interfaces.h
 delete mode 100644 transmitters/rawudp/fs-interfaces.c
 delete mode 100644 transmitters/rawudp/fs-interfaces.h

diff --git a/gst-libs/gst/farsight/Makefile.am b/gst-libs/gst/farsight/Makefile.am
index d1a685f..8bd371c 100644
--- a/gst-libs/gst/farsight/Makefile.am
+++ b/gst-libs/gst/farsight/Makefile.am
@@ -13,7 +13,8 @@ libgstfarsightinclude_HEADERS = \
 		fs-plugin.h \
 		fs-marshal.h \
 		fs-enum-types.h \
-		fs-element-added-notifier.h
+		fs-element-added-notifier.h \
+		fs-interfaces.h
 
 lib_LTLIBRARIES = libgstfarsight- at GST_MAJORMINOR@.la
 
@@ -38,7 +39,8 @@ libgstfarsight_ at GST_MAJORMINOR@_la_SOURCES = \
 		fs-plugin.c \
 		fs-marshal.c \
 		fs-enum-types.c \
-		fs-element-added-notifier.c
+		fs-element-added-notifier.c \
+		fs-interfaces.c
 
 noinst_HEADERS = \
 	fs-marshal.h \
diff --git a/gst-libs/gst/farsight/fs-interfaces.c b/gst-libs/gst/farsight/fs-interfaces.c
new file mode 100644
index 0000000..cc198ca
--- /dev/null
+++ b/gst-libs/gst/farsight/fs-interfaces.c
@@ -0,0 +1,580 @@
+/*
+ * farsight-interfaces.c - Source for interface discovery code
+ *
+ * Farsight Helper functions
+ * Copyright (C) 2006 Youness Alaoui <kakaroto at kakaroto.homelinux.net>
+ * Copyright (C) 2007 Collabora, Nokia
+ * Copyright (C) 2008 Haakon Sporsheim <haakon.sporsheim at tandberg.com>
+ * @author: Youness Alaoui <kakaroto at kakaroto.homelinux.net>
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301 USA
+ */
+
+#ifdef HAVE_CONFIG_H
+# include "config.h"
+#endif
+
+#include "fs-interfaces.h"
+
+#include <gst/gst.h>
+
+GST_DEBUG_CATEGORY_EXTERN (fs_base_conference_debug);
+#define GST_CAT_DEFAULT fs_base_conference_debug
+
+#ifdef G_OS_UNIX
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <unistd.h>
+#include <string.h>
+#include <errno.h>
+#include <sys/ioctl.h>
+#include <sys/types.h>
+#ifdef HAVE_GETIFADDRS
+ #include <sys/socket.h>
+ #include <ifaddrs.h>
+#endif
+#include <net/if.h>
+#include <net/if_arp.h>
+#include <arpa/inet.h>
+
+/**
+ * farsight_get_local_interfaces:
+ *
+ * Get the list of local interfaces
+ *
+ * Returns: a #GList of strings.
+ */
+#ifdef HAVE_GETIFADDRS
+GList *
+farsight_get_local_interfaces (void)
+{
+  GList *interfaces = NULL;
+  struct ifaddrs *ifa, *results;
+
+  if (getifaddrs (&results) < 0) {
+    return NULL;
+  }
+
+  /* Loop and get each interface the system has, one by one... */
+  for (ifa = results; ifa; ifa = ifa->ifa_next) {
+    /* no ip address from interface that is down */
+    if ((ifa->ifa_flags & IFF_UP) == 0)
+      continue;
+
+    if (ifa->ifa_addr == NULL || ifa->ifa_addr->sa_family != AF_INET)
+      continue;
+
+    GST_DEBUG ("Found interface : %s", ifa->ifa_name);
+    interfaces = g_list_prepend (interfaces, g_strdup (ifa->ifa_name));
+  }
+
+  freeifaddrs (results);
+
+  return interfaces;
+}
+
+#else /* ! HAVE_GETIFADDRS */
+
+GList *
+farsight_get_local_interfaces (void)
+{
+  GList *interfaces = NULL;
+  gint sockfd;
+  gint size = 0;
+  struct ifreq *ifr;
+  struct ifconf ifc;
+
+  if ((sockfd = socket (AF_INET, SOCK_DGRAM, IPPROTO_IP)) < 0) {
+    GST_ERROR ("Cannot open socket to retreive interface list");
+    return NULL;
+  }
+
+  ifc.ifc_len = 0;
+  ifc.ifc_req = NULL;
+
+  /* Loop and get each interface the system has, one by one... */
+  do {
+    size += sizeof (struct ifreq);
+    /* realloc buffer size until no overflow occurs  */
+    if (NULL == (ifc.ifc_req = realloc (ifc.ifc_req, size))) {
+      GST_ERROR ("Out of memory while allocation interface configuration structure");
+      close (sockfd);
+      return NULL;
+    }
+    ifc.ifc_len = size;
+
+    if (ioctl (sockfd, SIOCGIFCONF, &ifc)) {
+      perror ("ioctl SIOCFIFCONF");
+      close (sockfd);
+      free (ifc.ifc_req);
+      return NULL;
+    }
+  } while (size <= ifc.ifc_len);
+
+
+  /* Loop throught the interface list and get the IP address of each IF */
+  for (ifr = ifc.ifc_req;
+       (gchar *) ifr < (gchar *) ifc.ifc_req + ifc.ifc_len;
+       ++ifr) {
+    GST_DEBUG ("Found interface : %s", ifr->ifr_name);
+    interfaces = g_list_prepend (interfaces, g_strdup (ifr->ifr_name));
+  }
+
+  free (ifc.ifc_req);
+  close (sockfd);
+
+  return interfaces;
+}
+#endif /* HAVE_GETIFADDRS */
+
+
+static gboolean
+farsight_is_private_ip (const struct in_addr in)
+{
+  /* 10.x.x.x/8 */
+  if (in.s_addr >> 24 == 0x0A)
+    return TRUE;
+
+  /* 172.16.0.0 - 172.31.255.255 = 172.16.0.0/10 */
+  if (in.s_addr >> 20 == 0xAC1)
+    return TRUE;
+
+  /* 192.168.x.x/16 */
+  if (in.s_addr >> 16 == 0xC0A8)
+    return TRUE;
+
+  /* 169.254.x.x/16  (for APIPA) */
+  if (in.s_addr >> 16 == 0xA9FE)
+    return TRUE;
+
+  return FALSE;
+}
+
+/**
+ * farsight_get_local_ips:
+ * @include_loopback: Include any loopback devices
+ *
+ * Get a list of local ip4 interface addresses
+ *
+ * Returns: A #GList of strings
+ */
+
+#ifdef HAVE_GETIFADDRS
+
+GList *
+farsight_get_local_ips (gboolean include_loopback)
+{
+  GList *ips = NULL;
+  struct sockaddr_in *sa;
+  struct ifaddrs *ifa, *results;
+  gchar *loopback = NULL;
+
+
+  if (getifaddrs (&results) < 0)
+      return NULL;
+
+  /* Loop through the interface list and get the IP address of each IF */
+  for (ifa = results; ifa; ifa = ifa->ifa_next)
+  {
+    /* no ip address from interface that is down */
+    if ((ifa->ifa_flags & IFF_UP) == 0)
+      continue;
+
+    if (ifa->ifa_addr == NULL || ifa->ifa_addr->sa_family != AF_INET)
+      continue;
+
+    sa = (struct sockaddr_in *) ifa->ifa_addr;
+
+    GST_DEBUG ("Interface:  %s", ifa->ifa_name);
+    GST_DEBUG ("IP Address: %s", inet_ntoa (sa->sin_addr));
+    if ((ifa->ifa_flags & IFF_LOOPBACK) == IFF_LOOPBACK)
+    {
+      if (include_loopback)
+        loopback = g_strdup (inet_ntoa (sa->sin_addr));
+      else
+        GST_DEBUG ("Ignoring loopback interface");
+    }
+    else
+    {
+      if (farsight_is_private_ip (sa->sin_addr))
+        ips = g_list_append (ips, g_strdup (inet_ntoa (sa->sin_addr)));
+      else
+        ips = g_list_prepend (ips, g_strdup (inet_ntoa (sa->sin_addr)));
+    }
+  }
+
+  freeifaddrs (results);
+
+  if (loopback)
+    ips = g_list_append (ips, loopback);
+
+  return ips;
+}
+
+#else /* ! HAVE_GETIFADDRS */
+
+GList *
+farsight_get_local_ips (gboolean include_loopback)
+{
+  GList *ips = NULL;
+  gint sockfd;
+  gint size = 0;
+  struct ifreq *ifr;
+  struct ifconf ifc;
+  struct sockaddr_in *sa;
+  gchar *loopback = NULL;
+
+  if ((sockfd = socket (AF_INET, SOCK_DGRAM, IPPROTO_IP)) < 0) {
+    GST_ERROR ("Cannot open socket to retreive interface list");
+    return NULL;
+  }
+
+  ifc.ifc_len = 0;
+  ifc.ifc_req = NULL;
+
+  /* Loop and get each interface the system has, one by one... */
+  do {
+    size += sizeof (struct ifreq);
+    /* realloc buffer size until no overflow occurs  */
+    if (NULL == (ifc.ifc_req = realloc (ifc.ifc_req, size))) {
+      GST_ERROR ("Out of memory while allocation interface configuration"
+        " structure");
+      close (sockfd);
+      return NULL;
+    }
+    ifc.ifc_len = size;
+
+    if (ioctl (sockfd, SIOCGIFCONF, &ifc)) {
+      perror ("ioctl SIOCFIFCONF");
+      close (sockfd);
+      free (ifc.ifc_req);
+      return NULL;
+    }
+  } while  (size <= ifc.ifc_len);
+
+
+  /* Loop throught the interface list and get the IP address of each IF */
+  for (ifr = ifc.ifc_req;
+       (gchar *) ifr < (gchar *) ifc.ifc_req + ifc.ifc_len;
+       ++ifr) {
+
+    if (ioctl (sockfd, SIOCGIFFLAGS, ifr)) {
+      GST_ERROR ("Unable to get IP information for interface %s. Skipping...",
+        ifr->ifr_name);
+      continue;  /* failed to get flags, skip it */
+    }
+    sa = (struct sockaddr_in *) &ifr->ifr_addr;
+    GST_DEBUG ("Interface:  %s", ifr->ifr_name);
+    GST_DEBUG ("IP Address: %s", inet_ntoa (sa->sin_addr));
+    if ((ifr->ifr_flags & IFF_LOOPBACK) == IFF_LOOPBACK){
+      if (include_loopback)
+        loopback = g_strdup (inet_ntoa (sa->sin_addr));
+      else
+        GST_DEBUG ("Ignoring loopback interface");
+    } else {
+      if (farsight_is_private_ip (sa->sin_addr)) {
+        ips = g_list_append (ips, g_strdup (inet_ntoa (sa->sin_addr)));
+      } else {
+        ips = g_list_prepend (ips, g_strdup (inet_ntoa (sa->sin_addr)));
+      }
+    }
+  }
+
+  close (sockfd);
+  free (ifc.ifc_req);
+
+  if (loopback)
+    ips = g_list_append (ips, loopback);
+
+  return ips;
+}
+
+#endif /* HAVE_GETIFADDRS */
+
+
+/**
+ * farsight_get_ip_for_interface:
+ * @interface_name: name of local interface
+ *
+ * Retreives the IP Address of an interface by its name
+ *
+ * Returns: a newly-allocated string with the IP address
+ **/
+gchar *
+farsight_get_ip_for_interface (gchar *interface_name)
+{
+  struct ifreq ifr;
+  struct sockaddr_in *sa;
+  gint sockfd;
+
+
+  ifr.ifr_addr.sa_family = AF_INET;
+  memset (ifr.ifr_name, 0, sizeof (ifr.ifr_name));
+  strncpy (ifr.ifr_name, interface_name, sizeof (ifr.ifr_name)-1);
+
+  if ((sockfd = socket (AF_INET, SOCK_DGRAM, IPPROTO_IP)) < 0) {
+    GST_ERROR ("Cannot open socket to retreive interface list");
+    return NULL;
+  }
+
+  if (ioctl (sockfd, SIOCGIFADDR, &ifr) < 0) {
+    GST_ERROR ("Unable to get IP information for interface %s",
+      interface_name);
+    close (sockfd);
+    return NULL;
+  }
+
+  close (sockfd);
+  sa = (struct sockaddr_in *) &ifr.ifr_addr;
+  GST_DEBUG ("Address for %s: %s", interface_name, inet_ntoa (sa->sin_addr));
+  return g_strdup (inet_ntoa (sa->sin_addr));
+}
+
+#else /* G_OS_UNIX */
+#ifdef G_OS_WIN32
+
+#include <winsock2.h>
+#include <Iphlpapi.h>
+
+static gboolean started_wsa_engine = FALSE;
+
+/**
+ * private function that initializes the WinSock engine and
+ *  returns a prebuilt socket
+ **/
+SOCKET farsight_get_WSA_socket ()
+{
+  WORD wVersionRequested;
+  WSADATA wsaData;
+  int err;
+  SOCKET sock;
+
+  if (started_wsa_engine == FALSE) {
+    wVersionRequested = MAKEWORD ( 2, 0 );
+
+    err = WSAStartup ( wVersionRequested, &wsaData );
+    if ( err != 0 ) {
+      GST_ERROR ("Could not start the winsocket engine");
+      return INVALID_SOCKET;
+    }
+    started_wsa_engine = TRUE;
+  }
+
+
+  if ((sock = socket (AF_INET, SOCK_DGRAM, 0)) == INVALID_SOCKET) {
+    GST_ERROR ("Could not open socket to retreive interface list, error no : %d", WSAGetLastError ());
+    return INVALID_SOCKET;
+  }
+
+  return sock;
+}
+
+/**
+ * farsight_get_local_interfaces:
+ *
+ * Get the list of local interfaces
+ *
+ * Returns: a #GList of strings.
+ */
+GList * farsight_get_local_interfaces ()
+{
+  ULONG size = 0;
+  PMIB_IFTABLE if_table;
+  GList * ret = NULL;
+
+  GetIfTable(NULL, &size, TRUE);
+
+  if (!size)
+    return NULL;
+
+  if_table = (PMIB_IFTABLE)g_malloc0(size);
+
+  if (GetIfTable(if_table, &size, TRUE) == ERROR_SUCCESS)
+  {
+    DWORD i;
+    for (i = 0; i < if_table->dwNumEntries; i++)
+    {
+      ret = g_list_prepend (ret, g_strdup ((gchar*)if_table->table[i].bDescr));
+    }
+  }
+
+  g_free(if_table);
+
+  return ret;
+}
+
+/**
+ * farsight_get_local_ips:
+ * @include_loopback: Include any loopback devices
+ *
+ * Get a list of local ip4 interface addresses
+ *
+ * Returns: A #GList of strings
+ */
+GList * farsight_get_local_ips (gboolean include_loopback)
+{
+  ULONG size = 0;
+  DWORD pref = 0;
+  PMIB_IPADDRTABLE ip_table;
+  GList * ret = NULL;
+
+  GetIpAddrTable (NULL, &size, TRUE);
+
+  if (!size)
+    return NULL;
+
+  /*
+   * Get the best interface for transport to 0.0.0.0.
+   * This interface should be first in list!
+   */
+  if (GetBestInterface (0, &pref) != NO_ERROR)
+    pref = 0;
+
+  ip_table = (PMIB_IPADDRTABLE)g_malloc0 (size);
+
+  if (GetIpAddrTable (ip_table, &size, TRUE) == ERROR_SUCCESS)
+  {
+    DWORD i;
+    for (i = 0; i < ip_table->dwNumEntries; i++)
+    {
+      gchar * ipstr;
+      PMIB_IPADDRROW ipaddr = &ip_table->table[i];
+      if (!(ipaddr->wType & (MIB_IPADDR_DISCONNECTED | MIB_IPADDR_DELETED)) &&
+          ipaddr->dwAddr)
+      {
+        if (!include_loopback)
+        {
+		  DWORD type = 0;
+          PMIB_IFROW ifr = (PMIB_IFROW)g_malloc0 (sizeof (MIB_IFROW));
+          ifr->dwIndex = ipaddr->dwIndex;
+		  if (GetIfEntry (ifr) == NO_ERROR)
+		    type = ifr->dwType;
+		  g_free (ifr);
+
+		  if (type == IF_TYPE_SOFTWARE_LOOPBACK)
+		    continue;
+        }
+
+        ipstr = g_strdup_printf ("%d.%d.%d.%d", 
+            (ipaddr->dwAddr      ) & 0xFF,
+            (ipaddr->dwAddr >>  8) & 0xFF,
+            (ipaddr->dwAddr >> 16) & 0xFF,
+            (ipaddr->dwAddr >> 24) & 0xFF);
+        if (ipaddr->dwIndex == pref)
+          ret = g_list_prepend (ret, ipstr);
+        else
+          ret = g_list_append (ret, ipstr);
+      }
+    }
+  }
+
+  g_free(ip_table);
+
+  return ret;
+}
+
+/**
+ * returns ip address as an utf8 string
+ */
+static gchar *
+win32_get_ip_for_interface (IF_INDEX idx)
+{
+  ULONG size = 0;
+  PMIB_IPADDRTABLE ip_table;
+  gchar * ret = NULL;
+
+  GetIpAddrTable (NULL, &size, TRUE);
+
+  if (!size)
+    return NULL;
+
+  ip_table = (PMIB_IPADDRTABLE)g_malloc0 (size);
+
+  if (GetIpAddrTable (ip_table, &size, TRUE) == ERROR_SUCCESS)
+  {
+    DWORD i;
+    for (i = 0; i < ip_table->dwNumEntries; i++)
+    {
+      PMIB_IPADDRROW ipaddr = &ip_table->table[i];
+      if (ipaddr->dwIndex == idx && 
+          !(ipaddr->wType & (MIB_IPADDR_DISCONNECTED | MIB_IPADDR_DELETED)))
+      {
+        ret = g_strdup_printf ("%d.%d.%d.%d", 
+            (ipaddr->dwAddr      ) & 0xFF,
+            (ipaddr->dwAddr >>  8) & 0xFF,
+            (ipaddr->dwAddr >> 16) & 0xFF,
+            (ipaddr->dwAddr >> 24) & 0xFF);
+        break;
+      }
+    }
+  }
+
+  g_free (ip_table);
+  return ret;
+}
+
+/**
+ * farsight_get_ip_for_interface:
+ * @interface_name: name of local interface
+ *
+ * Retreives the IP Address of an interface by its name
+ *
+ * Returns: a newly-allocated string with the IP address
+ **/
+gchar * farsight_get_ip_for_interface (gchar *interface_name)
+{
+  ULONG size = 0;
+  PMIB_IFTABLE if_table;
+  gchar * ret = NULL;
+
+  GetIfTable (NULL, &size, TRUE);
+
+  if (!size)
+    return NULL;
+
+  if_table = (PMIB_IFTABLE)g_malloc0 (size);
+
+  if (GetIfTable (if_table, &size, TRUE) == ERROR_SUCCESS)
+  {
+    DWORD i;
+    gchar * tmp_str;
+    for (i = 0; i < if_table->dwNumEntries; i++)
+    {
+      tmp_str = g_utf16_to_utf8 (
+          if_table->table[i].wszName, MAX_INTERFACE_NAME_LEN,
+          NULL, NULL, NULL);
+
+      if (g_strcasecmp (interface_name, tmp_str) == 0)
+      {
+        ret = win32_get_ip_for_interface (if_table->table[i].dwIndex);
+        g_free (tmp_str);
+        break;
+      }
+
+      g_free (tmp_str);
+    }
+  }
+
+  g_free (if_table);
+
+  return ret;
+}
+
+
+#else /* G_OS_WIN32 */
+#error Can not use this method for retreiving ip list from OS other than unix or windows
+#endif /* G_OS_WIN32 */
+#endif /* G_OS_UNIX */
diff --git a/gst-libs/gst/farsight/fs-interfaces.h b/gst-libs/gst/farsight/fs-interfaces.h
new file mode 100644
index 0000000..c260f9b
--- /dev/null
+++ b/gst-libs/gst/farsight/fs-interfaces.h
@@ -0,0 +1,31 @@
+/*
+ * farsight-interfaces.h - Source for interface discovery code
+ *
+ * Farsight Helper functions
+ * Copyright (C) 2006 Youness Alaoui <kakaroto at kakaroto.homelinux.net>
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301 USA
+ */
+
+#ifndef __FARSIGHT_INTERFACES_H__
+#define __FARSIGHT_INTERFACES_H__
+
+#include <glib.h>
+
+gchar * farsight_get_ip_for_interface (gchar *interface_name);
+GList * farsight_get_local_ips (gboolean include_loopback);
+GList * farsight_get_local_interfaces (void);
+
+#endif
diff --git a/transmitters/rawudp/Makefile.am b/transmitters/rawudp/Makefile.am
index bf8e052..2f908f0 100644
--- a/transmitters/rawudp/Makefile.am
+++ b/transmitters/rawudp/Makefile.am
@@ -8,7 +8,6 @@ librawudp_transmitter_la_SOURCES = \
 	fs-rawudp-transmitter.c \
 	fs-rawudp-stream-transmitter.c \
 	fs-rawudp-component.c \
-	fs-interfaces.c \
 	fs-rawudp-marshal.c \
 	stun.c
 
@@ -26,7 +25,6 @@ noinst_HEADERS = \
 	fs-rawudp-transmitter.h \
 	fs-rawudp-stream-transmitter.h \
 	fs-rawudp-component.h \
-	fs-interfaces.h \
 	fs-rawudp-marshal.h \
 	stun.h
 
diff --git a/transmitters/rawudp/fs-interfaces.c b/transmitters/rawudp/fs-interfaces.c
deleted file mode 100644
index 2ffd2dd..0000000
--- a/transmitters/rawudp/fs-interfaces.c
+++ /dev/null
@@ -1,580 +0,0 @@
-/*
- * farsight-interfaces.c - Source for interface discovery code
- *
- * Farsight Helper functions
- * Copyright (C) 2006 Youness Alaoui <kakaroto at kakaroto.homelinux.net>
- * Copyright (C) 2007 Collabora, Nokia
- * Copyright (C) 2008 Haakon Sporsheim <haakon.sporsheim at tandberg.com>
- * @author: Youness Alaoui <kakaroto at kakaroto.homelinux.net>
- *
- * This library is free software; you can redistribute it and/or
- * modify it under the terms of the GNU Lesser General Public
- * License as published by the Free Software Foundation; either
- * version 2.1 of the License, or (at your option) any later version.
- *
- * This library is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
- * Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public
- * License along with this library; if not, write to the Free Software
- * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301 USA
- */
-
-#ifdef HAVE_CONFIG_H
-# include "config.h"
-#endif
-
-#include "fs-interfaces.h"
-
-#include <gst/gst.h>
-
-GST_DEBUG_CATEGORY_EXTERN (fs_rawudp_transmitter_debug);
-#define GST_CAT_DEFAULT fs_rawudp_transmitter_debug
-
-#ifdef G_OS_UNIX
-
-#include <stdio.h>
-#include <stdlib.h>
-#include <unistd.h>
-#include <string.h>
-#include <errno.h>
-#include <sys/ioctl.h>
-#include <sys/types.h>
-#ifdef HAVE_GETIFADDRS
- #include <sys/socket.h>
- #include <ifaddrs.h>
-#endif
-#include <net/if.h>
-#include <net/if_arp.h>
-#include <arpa/inet.h>
-
-/**
- * farsight_get_local_interfaces:
- *
- * Get the list of local interfaces
- *
- * Returns: a #GList of strings.
- */
-#ifdef HAVE_GETIFADDRS
-GList *
-farsight_get_local_interfaces (void)
-{
-  GList *interfaces = NULL;
-  struct ifaddrs *ifa, *results;
-
-  if (getifaddrs (&results) < 0) {
-    return NULL;
-  }
-
-  /* Loop and get each interface the system has, one by one... */
-  for (ifa = results; ifa; ifa = ifa->ifa_next) {
-    /* no ip address from interface that is down */
-    if ((ifa->ifa_flags & IFF_UP) == 0)
-      continue;
-
-    if (ifa->ifa_addr == NULL || ifa->ifa_addr->sa_family != AF_INET)
-      continue;
-
-    GST_DEBUG ("Found interface : %s", ifa->ifa_name);
-    interfaces = g_list_prepend (interfaces, g_strdup (ifa->ifa_name));
-  }
-
-  freeifaddrs (results);
-
-  return interfaces;
-}
-
-#else /* ! HAVE_GETIFADDRS */
-
-GList *
-farsight_get_local_interfaces (void)
-{
-  GList *interfaces = NULL;
-  gint sockfd;
-  gint size = 0;
-  struct ifreq *ifr;
-  struct ifconf ifc;
-
-  if ((sockfd = socket (AF_INET, SOCK_DGRAM, IPPROTO_IP)) < 0) {
-    GST_ERROR ("Cannot open socket to retreive interface list");
-    return NULL;
-  }
-
-  ifc.ifc_len = 0;
-  ifc.ifc_req = NULL;
-
-  /* Loop and get each interface the system has, one by one... */
-  do {
-    size += sizeof (struct ifreq);
-    /* realloc buffer size until no overflow occurs  */
-    if (NULL == (ifc.ifc_req = realloc (ifc.ifc_req, size))) {
-      GST_ERROR ("Out of memory while allocation interface configuration structure");
-      close (sockfd);
-      return NULL;
-    }
-    ifc.ifc_len = size;
-
-    if (ioctl (sockfd, SIOCGIFCONF, &ifc)) {
-      perror ("ioctl SIOCFIFCONF");
-      close (sockfd);
-      free (ifc.ifc_req);
-      return NULL;
-    }
-  } while (size <= ifc.ifc_len);
-
-
-  /* Loop throught the interface list and get the IP address of each IF */
-  for (ifr = ifc.ifc_req;
-       (gchar *) ifr < (gchar *) ifc.ifc_req + ifc.ifc_len;
-       ++ifr) {
-    GST_DEBUG ("Found interface : %s", ifr->ifr_name);
-    interfaces = g_list_prepend (interfaces, g_strdup (ifr->ifr_name));
-  }
-
-  free (ifc.ifc_req);
-  close (sockfd);
-
-  return interfaces;
-}
-#endif /* HAVE_GETIFADDRS */
-
-
-static gboolean
-farsight_is_private_ip (const struct in_addr in)
-{
-  /* 10.x.x.x/8 */
-  if (in.s_addr >> 24 == 0x0A)
-    return TRUE;
-
-  /* 172.16.0.0 - 172.31.255.255 = 172.16.0.0/10 */
-  if (in.s_addr >> 20 == 0xAC1)
-    return TRUE;
-
-  /* 192.168.x.x/16 */
-  if (in.s_addr >> 16 == 0xC0A8)
-    return TRUE;
-
-  /* 169.254.x.x/16  (for APIPA) */
-  if (in.s_addr >> 16 == 0xA9FE)
-    return TRUE;
-
-  return FALSE;
-}
-
-/**
- * farsight_get_local_ips:
- * @include_loopback: Include any loopback devices
- *
- * Get a list of local ip4 interface addresses
- *
- * Returns: A #GList of strings
- */
-
-#ifdef HAVE_GETIFADDRS
-
-GList *
-farsight_get_local_ips (gboolean include_loopback)
-{
-  GList *ips = NULL;
-  struct sockaddr_in *sa;
-  struct ifaddrs *ifa, *results;
-  gchar *loopback = NULL;
-
-
-  if (getifaddrs (&results) < 0)
-      return NULL;
-
-  /* Loop through the interface list and get the IP address of each IF */
-  for (ifa = results; ifa; ifa = ifa->ifa_next)
-  {
-    /* no ip address from interface that is down */
-    if ((ifa->ifa_flags & IFF_UP) == 0)
-      continue;
-
-    if (ifa->ifa_addr == NULL || ifa->ifa_addr->sa_family != AF_INET)
-      continue;
-
-    sa = (struct sockaddr_in *) ifa->ifa_addr;
-
-    GST_DEBUG ("Interface:  %s", ifa->ifa_name);
-    GST_DEBUG ("IP Address: %s", inet_ntoa (sa->sin_addr));
-    if ((ifa->ifa_flags & IFF_LOOPBACK) == IFF_LOOPBACK)
-    {
-      if (include_loopback)
-        loopback = g_strdup (inet_ntoa (sa->sin_addr));
-      else
-        GST_DEBUG ("Ignoring loopback interface");
-    }
-    else
-    {
-      if (farsight_is_private_ip (sa->sin_addr))
-        ips = g_list_append (ips, g_strdup (inet_ntoa (sa->sin_addr)));
-      else
-        ips = g_list_prepend (ips, g_strdup (inet_ntoa (sa->sin_addr)));
-    }
-  }
-
-  freeifaddrs (results);
-
-  if (loopback)
-    ips = g_list_append (ips, loopback);
-
-  return ips;
-}
-
-#else /* ! HAVE_GETIFADDRS */
-
-GList *
-farsight_get_local_ips (gboolean include_loopback)
-{
-  GList *ips = NULL;
-  gint sockfd;
-  gint size = 0;
-  struct ifreq *ifr;
-  struct ifconf ifc;
-  struct sockaddr_in *sa;
-  gchar *loopback = NULL;
-
-  if ((sockfd = socket (AF_INET, SOCK_DGRAM, IPPROTO_IP)) < 0) {
-    GST_ERROR ("Cannot open socket to retreive interface list");
-    return NULL;
-  }
-
-  ifc.ifc_len = 0;
-  ifc.ifc_req = NULL;
-
-  /* Loop and get each interface the system has, one by one... */
-  do {
-    size += sizeof (struct ifreq);
-    /* realloc buffer size until no overflow occurs  */
-    if (NULL == (ifc.ifc_req = realloc (ifc.ifc_req, size))) {
-      GST_ERROR ("Out of memory while allocation interface configuration"
-        " structure");
-      close (sockfd);
-      return NULL;
-    }
-    ifc.ifc_len = size;
-
-    if (ioctl (sockfd, SIOCGIFCONF, &ifc)) {
-      perror ("ioctl SIOCFIFCONF");
-      close (sockfd);
-      free (ifc.ifc_req);
-      return NULL;
-    }
-  } while  (size <= ifc.ifc_len);
-
-
-  /* Loop throught the interface list and get the IP address of each IF */
-  for (ifr = ifc.ifc_req;
-       (gchar *) ifr < (gchar *) ifc.ifc_req + ifc.ifc_len;
-       ++ifr) {
-
-    if (ioctl (sockfd, SIOCGIFFLAGS, ifr)) {
-      GST_ERROR ("Unable to get IP information for interface %s. Skipping...",
-        ifr->ifr_name);
-      continue;  /* failed to get flags, skip it */
-    }
-    sa = (struct sockaddr_in *) &ifr->ifr_addr;
-    GST_DEBUG ("Interface:  %s", ifr->ifr_name);
-    GST_DEBUG ("IP Address: %s", inet_ntoa (sa->sin_addr));
-    if ((ifr->ifr_flags & IFF_LOOPBACK) == IFF_LOOPBACK){
-      if (include_loopback)
-        loopback = g_strdup (inet_ntoa (sa->sin_addr));
-      else
-        GST_DEBUG ("Ignoring loopback interface");
-    } else {
-      if (farsight_is_private_ip (sa->sin_addr)) {
-        ips = g_list_append (ips, g_strdup (inet_ntoa (sa->sin_addr)));
-      } else {
-        ips = g_list_prepend (ips, g_strdup (inet_ntoa (sa->sin_addr)));
-      }
-    }
-  }
-
-  close (sockfd);
-  free (ifc.ifc_req);
-
-  if (loopback)
-    ips = g_list_append (ips, loopback);
-
-  return ips;
-}
-
-#endif /* HAVE_GETIFADDRS */
-
-
-/**
- * farsight_get_ip_for_interface:
- * @interface_name: name of local interface
- *
- * Retreives the IP Address of an interface by its name
- *
- * Returns: a newly-allocated string with the IP address
- **/
-gchar *
-farsight_get_ip_for_interface (gchar *interface_name)
-{
-  struct ifreq ifr;
-  struct sockaddr_in *sa;
-  gint sockfd;
-
-
-  ifr.ifr_addr.sa_family = AF_INET;
-  memset (ifr.ifr_name, 0, sizeof (ifr.ifr_name));
-  strncpy (ifr.ifr_name, interface_name, sizeof (ifr.ifr_name)-1);
-
-  if ((sockfd = socket (AF_INET, SOCK_DGRAM, IPPROTO_IP)) < 0) {
-    GST_ERROR ("Cannot open socket to retreive interface list");
-    return NULL;
-  }
-
-  if (ioctl (sockfd, SIOCGIFADDR, &ifr) < 0) {
-    GST_ERROR ("Unable to get IP information for interface %s",
-      interface_name);
-    close (sockfd);
-    return NULL;
-  }
-
-  close (sockfd);
-  sa = (struct sockaddr_in *) &ifr.ifr_addr;
-  GST_DEBUG ("Address for %s: %s", interface_name, inet_ntoa (sa->sin_addr));
-  return g_strdup (inet_ntoa (sa->sin_addr));
-}
-
-#else /* G_OS_UNIX */
-#ifdef G_OS_WIN32
-
-#include <winsock2.h>
-#include <Iphlpapi.h>
-
-static gboolean started_wsa_engine = FALSE;
-
-/**
- * private function that initializes the WinSock engine and
- *  returns a prebuilt socket
- **/
-SOCKET farsight_get_WSA_socket ()
-{
-  WORD wVersionRequested;
-  WSADATA wsaData;
-  int err;
-  SOCKET sock;
-
-  if (started_wsa_engine == FALSE) {
-    wVersionRequested = MAKEWORD ( 2, 0 );
-
-    err = WSAStartup ( wVersionRequested, &wsaData );
-    if ( err != 0 ) {
-      GST_ERROR ("Could not start the winsocket engine");
-      return INVALID_SOCKET;
-    }
-    started_wsa_engine = TRUE;
-  }
-
-
-  if ((sock = socket (AF_INET, SOCK_DGRAM, 0)) == INVALID_SOCKET) {
-    GST_ERROR ("Could not open socket to retreive interface list, error no : %d", WSAGetLastError ());
-    return INVALID_SOCKET;
-  }
-
-  return sock;
-}
-
-/**
- * farsight_get_local_interfaces:
- *
- * Get the list of local interfaces
- *
- * Returns: a #GList of strings.
- */
-GList * farsight_get_local_interfaces ()
-{
-  ULONG size = 0;
-  PMIB_IFTABLE if_table;
-  GList * ret = NULL;
-
-  GetIfTable(NULL, &size, TRUE);
-
-  if (!size)
-    return NULL;
-
-  if_table = (PMIB_IFTABLE)g_malloc0(size);
-
-  if (GetIfTable(if_table, &size, TRUE) == ERROR_SUCCESS)
-  {
-    DWORD i;
-    for (i = 0; i < if_table->dwNumEntries; i++)
-    {
-      ret = g_list_prepend (ret, g_strdup ((gchar*)if_table->table[i].bDescr));
-    }
-  }
-
-  g_free(if_table);
-
-  return ret;
-}
-
-/**
- * farsight_get_local_ips:
- * @include_loopback: Include any loopback devices
- *
- * Get a list of local ip4 interface addresses
- *
- * Returns: A #GList of strings
- */
-GList * farsight_get_local_ips (gboolean include_loopback)
-{
-  ULONG size = 0;
-  DWORD pref = 0;
-  PMIB_IPADDRTABLE ip_table;
-  GList * ret = NULL;
-
-  GetIpAddrTable (NULL, &size, TRUE);
-
-  if (!size)
-    return NULL;
-
-  /*
-   * Get the best interface for transport to 0.0.0.0.
-   * This interface should be first in list!
-   */
-  if (GetBestInterface (0, &pref) != NO_ERROR)
-    pref = 0;
-
-  ip_table = (PMIB_IPADDRTABLE)g_malloc0 (size);
-
-  if (GetIpAddrTable (ip_table, &size, TRUE) == ERROR_SUCCESS)
-  {
-    DWORD i;
-    for (i = 0; i < ip_table->dwNumEntries; i++)
-    {
-      gchar * ipstr;
-      PMIB_IPADDRROW ipaddr = &ip_table->table[i];
-      if (!(ipaddr->wType & (MIB_IPADDR_DISCONNECTED | MIB_IPADDR_DELETED)) &&
-          ipaddr->dwAddr)
-      {
-        if (!include_loopback)
-        {
-		  DWORD type = 0;
-          PMIB_IFROW ifr = (PMIB_IFROW)g_malloc0 (sizeof (MIB_IFROW));
-          ifr->dwIndex = ipaddr->dwIndex;
-		  if (GetIfEntry (ifr) == NO_ERROR)
-		    type = ifr->dwType;
-		  g_free (ifr);
-
-		  if (type == IF_TYPE_SOFTWARE_LOOPBACK)
-		    continue;
-        }
-
-        ipstr = g_strdup_printf ("%d.%d.%d.%d", 
-            (ipaddr->dwAddr      ) & 0xFF,
-            (ipaddr->dwAddr >>  8) & 0xFF,
-            (ipaddr->dwAddr >> 16) & 0xFF,
-            (ipaddr->dwAddr >> 24) & 0xFF);
-        if (ipaddr->dwIndex == pref)
-          ret = g_list_prepend (ret, ipstr);
-        else
-          ret = g_list_append (ret, ipstr);
-      }
-    }
-  }
-
-  g_free(ip_table);
-
-  return ret;
-}
-
-/**
- * returns ip address as an utf8 string
- */
-static gchar *
-win32_get_ip_for_interface (IF_INDEX idx)
-{
-  ULONG size = 0;
-  PMIB_IPADDRTABLE ip_table;
-  gchar * ret = NULL;
-
-  GetIpAddrTable (NULL, &size, TRUE);
-
-  if (!size)
-    return NULL;
-
-  ip_table = (PMIB_IPADDRTABLE)g_malloc0 (size);
-
-  if (GetIpAddrTable (ip_table, &size, TRUE) == ERROR_SUCCESS)
-  {
-    DWORD i;
-    for (i = 0; i < ip_table->dwNumEntries; i++)
-    {
-      PMIB_IPADDRROW ipaddr = &ip_table->table[i];
-      if (ipaddr->dwIndex == idx && 
-          !(ipaddr->wType & (MIB_IPADDR_DISCONNECTED | MIB_IPADDR_DELETED)))
-      {
-        ret = g_strdup_printf ("%d.%d.%d.%d", 
-            (ipaddr->dwAddr      ) & 0xFF,
-            (ipaddr->dwAddr >>  8) & 0xFF,
-            (ipaddr->dwAddr >> 16) & 0xFF,
-            (ipaddr->dwAddr >> 24) & 0xFF);
-        break;
-      }
-    }
-  }
-
-  g_free (ip_table);
-  return ret;
-}
-
-/**
- * farsight_get_ip_for_interface:
- * @interface_name: name of local interface
- *
- * Retreives the IP Address of an interface by its name
- *
- * Returns: a newly-allocated string with the IP address
- **/
-gchar * farsight_get_ip_for_interface (gchar *interface_name)
-{
-  ULONG size = 0;
-  PMIB_IFTABLE if_table;
-  gchar * ret = NULL;
-
-  GetIfTable (NULL, &size, TRUE);
-
-  if (!size)
-    return NULL;
-
-  if_table = (PMIB_IFTABLE)g_malloc0 (size);
-
-  if (GetIfTable (if_table, &size, TRUE) == ERROR_SUCCESS)
-  {
-    DWORD i;
-    gchar * tmp_str;
-    for (i = 0; i < if_table->dwNumEntries; i++)
-    {
-      tmp_str = g_utf16_to_utf8 (
-          if_table->table[i].wszName, MAX_INTERFACE_NAME_LEN,
-          NULL, NULL, NULL);
-
-      if (g_strcasecmp (interface_name, tmp_str) == 0)
-      {
-        ret = win32_get_ip_for_interface (if_table->table[i].dwIndex);
-        g_free (tmp_str);
-        break;
-      }
-
-      g_free (tmp_str);
-    }
-  }
-
-  g_free (if_table);
-
-  return ret;
-}
-
-
-#else /* G_OS_WIN32 */
-#error Can not use this method for retreiving ip list from OS other than unix or windows
-#endif /* G_OS_WIN32 */
-#endif /* G_OS_UNIX */
diff --git a/transmitters/rawudp/fs-interfaces.h b/transmitters/rawudp/fs-interfaces.h
deleted file mode 100644
index c260f9b..0000000
--- a/transmitters/rawudp/fs-interfaces.h
+++ /dev/null
@@ -1,31 +0,0 @@
-/*
- * farsight-interfaces.h - Source for interface discovery code
- *
- * Farsight Helper functions
- * Copyright (C) 2006 Youness Alaoui <kakaroto at kakaroto.homelinux.net>
- *
- * This library is free software; you can redistribute it and/or
- * modify it under the terms of the GNU Lesser General Public
- * License as published by the Free Software Foundation; either
- * version 2.1 of the License, or (at your option) any later version.
- *
- * This library is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
- * Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public
- * License along with this library; if not, write to the Free Software
- * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301 USA
- */
-
-#ifndef __FARSIGHT_INTERFACES_H__
-#define __FARSIGHT_INTERFACES_H__
-
-#include <glib.h>
-
-gchar * farsight_get_ip_for_interface (gchar *interface_name);
-GList * farsight_get_local_ips (gboolean include_loopback);
-GList * farsight_get_local_interfaces (void);
-
-#endif
diff --git a/transmitters/rawudp/fs-rawudp-component.c b/transmitters/rawudp/fs-rawudp-component.c
index 4776c1c..94527e2 100644
--- a/transmitters/rawudp/fs-rawudp-component.c
+++ b/transmitters/rawudp/fs-rawudp-component.c
@@ -32,9 +32,9 @@
 #include "fs-rawudp-marshal.h"
 
 #include "stun.h"
-#include "fs-interfaces.h"
 
 #include <gst/farsight/fs-conference-iface.h>
+#include <gst/farsight/fs-interfaces.h>
 
 #include <string.h>
 #include <sys/types.h>
diff --git a/transmitters/rawudp/fs-rawudp-stream-transmitter.c b/transmitters/rawudp/fs-rawudp-stream-transmitter.c
index f241074..3c2d60a 100644
--- a/transmitters/rawudp/fs-rawudp-stream-transmitter.c
+++ b/transmitters/rawudp/fs-rawudp-stream-transmitter.c
@@ -74,8 +74,6 @@
 
 #include "fs-rawudp-component.h"
 
-#include "fs-interfaces.h"
-
 #include <gst/farsight/fs-candidate.h>
 #include <gst/farsight/fs-conference-iface.h>
 
-- 
1.5.6.5




More information about the farsight-commits mailing list