Bring aboard a class that encapsulate the list of network interfaces.
authorGuilherme Maciel Ferreira <guilherme.maciel.ferreira@gmail.com>
Sat, 12 Nov 2011 00:42:21 +0000 (22:42 -0200)
committerGuilherme Maciel Ferreira <guilherme.maciel.ferreira@gmail.com>
Sat, 12 Nov 2011 00:42:21 +0000 (22:42 -0200)
src/host/networkinterfacelist.cpp [new file with mode: 0644]
src/host/networkinterfacelist.h [new file with mode: 0644]

diff --git a/src/host/networkinterfacelist.cpp b/src/host/networkinterfacelist.cpp
new file mode 100644 (file)
index 0000000..4052e0c
--- /dev/null
@@ -0,0 +1,139 @@
+/*
+ The software in this package is distributed under the GNU General
+ Public License version 2 (with a special exception described below).
+
+ A copy of GNU General Public License (GPL) is included in this distribution,
+ in the file COPYING.GPL.
+
+ As a special exception, if other files instantiate templates or use macros
+ or inline functions from this file, or you compile this file and link it
+ with other works to produce a work based on this file, this file
+ does not by itself cause the resulting work to be covered
+ by the GNU General Public License.
+
+ However the source code for this file must still be made available
+ in accordance with section (3) of the GNU General Public License.
+
+ This exception does not invalidate any other reasons why a work based
+ on this file might be covered by the GNU General Public License.
+ */
+
+#include "host/networkinterfacelist.h"
+
+#include <stddef.h>
+
+#include <boost/assert.hpp>
+
+using namespace std;
+
+//-----------------------------------------------------------------------------
+// NetworkInterfaceList
+//-----------------------------------------------------------------------------
+
+/**
+ * @brief Default constructor.
+ */
+NetworkInterfaceList::NetworkInterfaceList() :
+    IfaFirst( NULL )
+{
+    BOOST_ASSERT( IfaFirst == NULL );
+}
+
+/**
+ * @brief Destructor.
+ */
+NetworkInterfaceList::~NetworkInterfaceList()
+{
+    if ( initialized() )
+    {
+        destroy_list();
+    }
+}
+
+/**
+ * @brief Find the @c ifaddrs structure that represents the network interface
+ * with the given @a nic_name and @a family.
+ *
+ * @param nic_name Requested network interface name.
+ * @param family Requested family type, PF_INET for IPv4 or PF_INET6 for IPv6.
+ *
+ * @return A pointer to the @c ifaddrs that represents the network interface
+ * entry.
+ */
+const struct ifaddrs * NetworkInterfaceList::find_interface(
+        const string nic_name,
+        const sa_family_t family
+)
+{
+    BOOST_ASSERT( !nic_name.empty() );
+    BOOST_ASSERT( (PF_INET == family) || (PF_INET6 == family) );
+
+    if( !initialized() && !create_list() )
+    {
+        return NULL;
+    }
+
+    BOOST_ASSERT( IfaFirst != NULL );
+
+    const ifaddrs_t *ifa_current = IfaFirst;
+    while ( ifa_current != NULL )
+    {
+        if ( ifa_current->ifa_addr->sa_data != NULL )
+        {
+            const char *nic_name_current = ifa_current->ifa_name;
+            bool name_match = ( nic_name == nic_name_current );
+
+            const sa_family_t family_current = ifa_current->ifa_addr->sa_family;
+            bool family_match = ( family == family_current );
+
+            if ( name_match && family_match )
+            {
+                return ifa_current;
+            }
+        }
+
+        ifa_current = ifa_current->ifa_next;
+    }
+
+    return NULL;
+}
+
+/**
+ * @brief Verify it the list is initialized or not.
+ *
+ * @return @c true if the list is initialized, or @false if it is not.
+ */
+bool NetworkInterfaceList::initialized()
+{
+    return ( IfaFirst != NULL );
+}
+
+/**
+ * @brief Create the interface list, allocating the required resources.
+ *
+ * @return @c true if the list could be created, or @c false if not.
+ */
+bool NetworkInterfaceList::create_list()
+{
+    BOOST_ASSERT( IfaFirst == NULL );
+
+    // The getifaddrs() returns a linked list of ifaddrs
+    int ret = getifaddrs( &IfaFirst );
+    bool success = ( ret == 0 );
+
+    return success;
+}
+
+/**
+ * @brief Release the resources allocated by the interface list.
+ */
+void NetworkInterfaceList::destroy_list()
+{
+    BOOST_ASSERT( IfaFirst != NULL );
+
+    // The freeifaddrs() must release the linked list allocated by getifaddrs()
+    freeifaddrs( IfaFirst );
+    IfaFirst = NULL;
+}
+
+
diff --git a/src/host/networkinterfacelist.h b/src/host/networkinterfacelist.h
new file mode 100644 (file)
index 0000000..8577c84
--- /dev/null
@@ -0,0 +1,61 @@
+/*
+ The software in this package is distributed under the GNU General
+ Public License version 2 (with a special exception described below).
+
+ A copy of GNU General Public License (GPL) is included in this distribution,
+ in the file COPYING.GPL.
+
+ As a special exception, if other files instantiate templates or use macros
+ or inline functions from this file, or you compile this file and link it
+ with other works to produce a work based on this file, this file
+ does not by itself cause the resulting work to be covered
+ by the GNU General Public License.
+
+ However the source code for this file must still be made available
+ in accordance with section (3) of the GNU General Public License.
+
+ This exception does not invalidate any other reasons why a work based
+ on this file might be covered by the GNU General Public License.
+ */
+
+#ifndef NETWORK_INTERFACE_LIST_H
+#define NETWORK_INTERFACE_LIST_H
+
+#include <ifaddrs.h>
+
+#include <string>
+
+typedef struct ifaddrs ifaddrs_t;
+
+//-----------------------------------------------------------------------------
+// NetworkInterfaceList
+//-----------------------------------------------------------------------------
+
+/**
+ * @brief Represents the list of local network interfaces.
+ */
+class NetworkInterfaceList
+{
+public:
+    NetworkInterfaceList();
+    ~NetworkInterfaceList();
+
+    const struct ifaddrs * find_interface(
+            const std::string nic_name,
+            const sa_family_t family
+    );
+
+private:
+    // Do not allow copies of this object, cannot deep copy.
+    NetworkInterfaceList( const NetworkInterfaceList &other );
+    NetworkInterfaceList& operator=( const NetworkInterfaceList &other );
+
+    bool initialized();
+    bool create_list();
+    void destroy_list();
+
+private:
+    ifaddrs_t *IfaFirst;
+};
+
+#endif // NETWORK_INTERFACE_LIST_H