Implement getInterfaces in util

getInterfaceAddrs returns the map of interface
and associated address,if there is no address
associated with the interface,then we don't get
that interface.
This API gets all the available interfaces on the
system.

Change-Id: I86f07d6e9950a15547cb74356939ca40368bddcc
Signed-off-by: Ratan Gupta <ratagupt@in.ibm.com>
diff --git a/network_manager.cpp b/network_manager.cpp
index 261885a..ae0cbaf 100644
--- a/network_manager.cpp
+++ b/network_manager.cpp
@@ -58,12 +58,12 @@
     //clear all the interfaces first
     interfaces.clear();
 
-    auto interfaceInfoList = getInterfaceAddrs();
+    auto interfaceStrList = getInterfaces();
 
-    for (const auto& intfInfo : interfaceInfoList)
+    for (auto& interface : interfaceStrList)
     {
         fs::path objPath = objectPath;
-        auto index = intfInfo.first.find(".");
+        auto index = interface.find(".");
 
         // interface can be of vlan type or normal ethernet interface.
         // vlan interface looks like "interface.vlanid",so here by looking
@@ -72,17 +72,17 @@
         if (index != std::string::npos)
         {
             //it is vlan interface
-            auto interface = intfInfo.first.substr(0, index);
-            auto vlanid = intfInfo.first.substr(index + 1);
+            auto interfaceName = interface.substr(0, index);
+            auto vlanid = interface.substr(index + 1);
             uint32_t vlanInt = std::stoul(vlanid);
 
-            interfaces[interface]->loadVLAN(vlanInt);
+            interfaces[interfaceName]->loadVLAN(vlanInt);
             continue;
         }
-        // normal ethernet inetrface
-        objPath /= intfInfo.first;
+        // normal ethernet interface
+        objPath /= interface;
 
-        auto dhcp = getDHCPValue(confDir, intfInfo.first);
+        auto dhcp = getDHCPValue(confDir, interface);
 
         auto intf =  std::make_shared<phosphor::network::EthernetInterface>(
                          bus,
@@ -94,7 +94,7 @@
         intf->createIPAddressObjects();
 
         this->interfaces.emplace(std::make_pair(
-                                     intfInfo.first, std::move(intf)));
+                                     std::move(interface), std::move(intf)));
 
     }
 
diff --git a/types.hpp b/types.hpp
index e792a3e..4f07522 100644
--- a/types.hpp
+++ b/types.hpp
@@ -7,7 +7,7 @@
 #include <vector>
 #include <map>
 #include <memory>
-
+#include <set>
 #include <systemd/sd-event.h>
 
 namespace phosphor
@@ -61,7 +61,7 @@
 
 using AddrList = std::list<AddrInfo>;
 using IntfAddrMap = std::map<IntfName, AddrList>;
-
+using InterfaceList = std::set<IntfName>;
 
 }//namespace network
 }//namespace phosphor
diff --git a/util.cpp b/util.cpp
index 8f3aabb..48b5fcb 100644
--- a/util.cpp
+++ b/util.cpp
@@ -299,6 +299,38 @@
     return intfMap;
 }
 
+InterfaceList getInterfaces()
+{
+    InterfaceList interfaces {};
+    struct ifaddrs* ifaddr = nullptr;
+
+    // attempt to fill struct with ifaddrs
+    if (getifaddrs(&ifaddr) == -1)
+    {
+        auto error = errno;
+        log<level::ERR>("Error occurred during the getifaddrs call",
+                        entry("ERRNO=%d", error));
+        elog<InternalFailure>();
+    }
+
+    AddrPtr ifaddrPtr(ifaddr);
+    ifaddr = nullptr;
+
+    for (ifaddrs* ifa = ifaddrPtr.get(); ifa != nullptr; ifa = ifa->ifa_next)
+    {
+        // walk interfaces
+        // if loopback, or not running ignore
+        if ((ifa->ifa_flags & IFF_LOOPBACK) ||
+             !(ifa->ifa_flags & IFF_RUNNING))
+        {
+            continue;
+        }
+        interfaces.emplace(ifa->ifa_name);
+    }
+    return interfaces;
+}
+
+
 void deleteInterface(const std::string& intf)
 {
     pid_t pid = fork();
diff --git a/util.hpp b/util.hpp
index 4a9d5f7..429b213 100644
--- a/util.hpp
+++ b/util.hpp
@@ -89,8 +89,9 @@
 std::string getNetworkID(int addressFamily, const std::string& ipaddress,
                          uint8_t prefix);
 
-/** @brief Get all the interfaces from the system.
- *  @returns list of interface names.
+/** @brief Gets the map of interface and the associated
+ *         address.
+ *  @returns map of interface and the address.
  */
 IntfAddrMap getInterfaceAddrs();
 
@@ -112,6 +113,11 @@
 
 }
 
+/** @brief Get all the interfaces from the system.
+ *  @returns list of interface names.
+ */
+InterfaceList getInterfaces();
+
 /** @brief Delete the given interface.
  *  @param[in] intf - interface name.
  */