Move function from network manager to utility function

Function which fetches the interface address from the system.
Add some types to types.hpp

Change-Id: Ie2c02980a9fd7a0aaeafa22d93bc758b5c79fd04
Signed-off-by: Ratan Gupta <ratagupt@in.ibm.com>
diff --git a/network_manager.cpp b/network_manager.cpp
index ee88c3f..e1bfa41 100644
--- a/network_manager.cpp
+++ b/network_manager.cpp
@@ -1,5 +1,6 @@
 #include "config.h"
 #include "config_parser.hpp"
+#include "util.hpp"
 #include "network_manager.hpp"
 #include "network_config.hpp"
 #include "xyz/openbmc_project/Common/error.hpp"
@@ -133,95 +134,6 @@
     return;
 }
 
-IntfAddrMap Manager::getInterfaceAddrs() const
-{
-    IntfAddrMap intfMap{};
-    AddrList addrList{};
-    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=%s", strerror(error)));
-        elog<InternalFailure>();
-    }
-
-    details::AddrPtr ifaddrPtr(ifaddr);
-    ifaddr = nullptr;
-
-    std::string intfName{};
-
-    for (ifaddrs* ifa = ifaddrPtr.get(); ifa != nullptr; ifa = ifa->ifa_next)
-    {
-        // walk interfaces
-        if (ifa->ifa_addr == nullptr)
-        {
-            continue;
-        }
-
-        // get only INET interfaces not ipv6
-        if (ifa->ifa_addr->sa_family == AF_INET ||
-            ifa->ifa_addr->sa_family == AF_INET6)
-        {
-            // if loopback, or not running ignore
-            if ((ifa->ifa_flags & IFF_LOOPBACK) ||
-                !(ifa->ifa_flags & IFF_RUNNING))
-            {
-                continue;
-            }
-            // if the interface name is  not same as the  previous
-            // iteration then add the addr list into
-            // the map.
-            if (intfName != "" && intfName != std::string(ifa->ifa_name))
-            {
-                intfMap.emplace(intfName, addrList);
-                addrList.clear();
-            }
-            intfName = ifa->ifa_name;
-            AddrInfo info{};
-            char ip[INET6_ADDRSTRLEN] = { 0 };
-            char subnetMask[INET6_ADDRSTRLEN] = { 0 };
-
-            if (ifa->ifa_addr->sa_family == AF_INET)
-            {
-
-                inet_ntop(ifa->ifa_addr->sa_family,
-                          &(((struct sockaddr_in*)(ifa->ifa_addr))->sin_addr),
-                          ip,
-                          sizeof(ip));
-
-                inet_ntop(ifa->ifa_addr->sa_family,
-                          &(((struct sockaddr_in*)(ifa->ifa_netmask))->sin_addr),
-                          subnetMask,
-                          sizeof(subnetMask));
-
-            }
-            else
-            {
-                inet_ntop(ifa->ifa_addr->sa_family,
-                          &(((struct sockaddr_in6*)(ifa->ifa_addr))->sin6_addr),
-                          ip,
-                          sizeof(ip));
-
-                inet_ntop(ifa->ifa_addr->sa_family,
-                          &(((struct sockaddr_in6*)(ifa->ifa_netmask))->sin6_addr),
-                          subnetMask,
-                          sizeof(subnetMask));
-
-            }
-
-            info.addrType = ifa->ifa_addr->sa_family;
-            info.ipaddress = ip;
-            info.prefix = toCidr(info.addrType, std::string(subnetMask));
-            addrList.emplace_back(info);
-        }
-    }
-    intfMap.emplace(intfName, addrList);
-    return intfMap;
-}
-
 // Need to merge the below function with the code which writes the
 // config file during factory reset.
 //TODO openbmc/openbmc#1751
diff --git a/network_manager.hpp b/network_manager.hpp
index 5c6358b..ce61737 100644
--- a/network_manager.hpp
+++ b/network_manager.hpp
@@ -2,12 +2,10 @@
 
 #include "ethernet_interface.hpp"
 #include "system_configuration.hpp"
-#include "types.hpp"
 #include "xyz/openbmc_project/Network/VLAN/Create/server.hpp"
 #include <xyz/openbmc_project/Common/FactoryReset/server.hpp>
 
 #include <sdbusplus/bus.hpp>
-#include <ifaddrs.h>
 
 #include <list>
 #include <memory>
@@ -31,29 +29,6 @@
     sdbusplus::xyz::openbmc_project::Network::VLAN::server::Create,
     sdbusplus::xyz::openbmc_project::Common::server::FactoryReset>;
 
-using IntfName = std::string;
-
-struct AddrInfo
-{
-    short addrType;
-    std::string ipaddress;
-};
-
-using Addr_t = ifaddrs*;
-
-struct AddrDeleter
-{
-    void operator()(Addr_t ptr) const
-    {
-        freeifaddrs(ptr);
-    }
-};
-
-using AddrPtr = std::unique_ptr<ifaddrs, AddrDeleter>;
-
-using AddrList = std::list<AddrInfo>;
-using IntfAddrMap = std::map<IntfName, AddrList>;
-
 } // namespace details
 
 class TestNetworkManager; //forward declaration
@@ -99,10 +74,6 @@
         void setConfDir(const fs::path& dir);
 
     private:
-        /** @brief Get all the interfaces from the system.
-         *  @returns list of interface names.
-         */
-        IntfAddrMap getInterfaceAddrs() const;
 
         /** @brief Restart the systemd networkd
          */
diff --git a/types.hpp b/types.hpp
index 28b34d9..ca35421 100644
--- a/types.hpp
+++ b/types.hpp
@@ -1,9 +1,12 @@
 #pragma once
 
+#include <ifaddrs.h>
+
 #include <list>
 #include <string>
 #include <vector>
 #include <map>
+#include <memory>
 
 namespace phosphor
 {
@@ -18,6 +21,19 @@
     uint16_t prefix;
 };
 
+using Addr_t = ifaddrs*;
+
+struct AddrDeleter
+{
+    void operator()(Addr_t ptr) const
+    {
+        freeifaddrs(ptr);
+    }
+};
+
+using AddrPtr = std::unique_ptr<ifaddrs, AddrDeleter>;
+
+
 using AddrList = std::list<AddrInfo>;
 using IntfAddrMap = std::map<IntfName, AddrList>;
 
diff --git a/util.cpp b/util.cpp
index 43ebf43..3c411cb 100644
--- a/util.cpp
+++ b/util.cpp
@@ -1,11 +1,13 @@
-#include <arpa/inet.h>
-#include <dirent.h>
-#include <net/if.h>
+#include "util.hpp"
 #include "xyz/openbmc_project/Common/error.hpp"
 
 #include <phosphor-logging/log.hpp>
 #include <phosphor-logging/elog-errors.hpp>
 
+#include <arpa/inet.h>
+#include <dirent.h>
+#include <net/if.h>
+
 #include <iostream>
 #include <list>
 #include <string>
@@ -202,5 +204,94 @@
                             true : false;
 }
 
+IntfAddrMap getInterfaceAddrs()
+{
+    IntfAddrMap intfMap{};
+    AddrList addrList{};
+    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=%s", strerror(error)));
+        elog<InternalFailure>();
+    }
+
+    AddrPtr ifaddrPtr(ifaddr);
+    ifaddr = nullptr;
+
+    std::string intfName{};
+
+    for (ifaddrs* ifa = ifaddrPtr.get(); ifa != nullptr; ifa = ifa->ifa_next)
+    {
+        // walk interfaces
+        if (ifa->ifa_addr == nullptr)
+        {
+            continue;
+        }
+
+        // get only INET interfaces not ipv6
+        if (ifa->ifa_addr->sa_family == AF_INET ||
+            ifa->ifa_addr->sa_family == AF_INET6)
+        {
+            // if loopback, or not running ignore
+            if ((ifa->ifa_flags & IFF_LOOPBACK) ||
+                !(ifa->ifa_flags & IFF_RUNNING))
+            {
+                continue;
+            }
+            // if the interface name is  not same as the  previous
+            // iteration then add the addr list into
+            // the map.
+            if (intfName != "" && intfName != std::string(ifa->ifa_name))
+            {
+                intfMap.emplace(intfName, addrList);
+                addrList.clear();
+            }
+            intfName = ifa->ifa_name;
+            AddrInfo info{};
+            char ip[INET6_ADDRSTRLEN] = { 0 };
+            char subnetMask[INET6_ADDRSTRLEN] = { 0 };
+
+            if (ifa->ifa_addr->sa_family == AF_INET)
+            {
+
+                inet_ntop(ifa->ifa_addr->sa_family,
+                          &(((struct sockaddr_in*)(ifa->ifa_addr))->sin_addr),
+                          ip,
+                          sizeof(ip));
+
+                inet_ntop(ifa->ifa_addr->sa_family,
+                          &(((struct sockaddr_in*)(ifa->ifa_netmask))->sin_addr),
+                          subnetMask,
+                          sizeof(subnetMask));
+
+            }
+            else
+            {
+                inet_ntop(ifa->ifa_addr->sa_family,
+                          &(((struct sockaddr_in6*)(ifa->ifa_addr))->sin6_addr),
+                          ip,
+                          sizeof(ip));
+
+                inet_ntop(ifa->ifa_addr->sa_family,
+                          &(((struct sockaddr_in6*)(ifa->ifa_netmask))->sin6_addr),
+                          subnetMask,
+                          sizeof(subnetMask));
+
+            }
+
+            info.addrType = ifa->ifa_addr->sa_family;
+            info.ipaddress = ip;
+            info.prefix = toCidr(info.addrType, std::string(subnetMask));
+            addrList.emplace_back(info);
+        }
+    }
+    intfMap.emplace(intfName, addrList);
+    return intfMap;
+}
+
 }//namespace network
 }//namespace phosphor
diff --git a/util.hpp b/util.hpp
index fb511fe..6f27af9 100644
--- a/util.hpp
+++ b/util.hpp
@@ -1,6 +1,7 @@
 #pragma once
 
 #include <unistd.h>
+#include "types.hpp"
 
 namespace phosphor
 {
@@ -36,6 +37,11 @@
 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.
+ */
+IntfAddrMap getInterfaceAddrs();
+
 } //namespace network
 
 class Descriptor