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/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