Add the network utility file

Moving utility functions from network manager to
util.cpp.

Added few more utility functions.

Change-Id: I2e73c873e9a3bea543d6979463b2181e8374e12e
Signed-off-by: Ratan Gupta <ratagupt@in.ibm.com>
diff --git a/network_manager.cpp b/network_manager.cpp
index 8ea82d5..a4a9b54 100644
--- a/network_manager.cpp
+++ b/network_manager.cpp
@@ -105,9 +105,9 @@
 
 IntfAddrMap Manager::getInterfaceAddrs() const
 {
-    IntfAddrMap intfMap;
-    AddrList addrList;
-    struct ifaddrs* ifaddr;
+    IntfAddrMap intfMap{};
+    AddrList addrList{};
+    struct ifaddrs* ifaddr = nullptr;
 
     using namespace sdbusplus::xyz::openbmc_project::Common::Error;
     // attempt to fill struct with ifaddrs
@@ -122,7 +122,7 @@
     details::AddrPtr ifaddrPtr(ifaddr);
     ifaddr = nullptr;
 
-    std::string intfName;
+    std::string intfName{};
 
     for (ifaddrs* ifa = ifaddrPtr.get(); ifa != nullptr; ifa = ifa->ifa_next)
     {
@@ -151,7 +151,7 @@
                 addrList.clear();
             }
             intfName = ifa->ifa_name;
-            AddrInfo info;
+            AddrInfo info{};
             char ip[INET6_ADDRSTRLEN] = { 0 };
             char subnetMask[INET6_ADDRSTRLEN] = { 0 };
 
@@ -185,9 +185,7 @@
 
             info.addrType = ifa->ifa_addr->sa_family;
             info.ipaddress = ip;
-
-            info.prefix = toCidr(info.addrType, subnetMask);
-
+            info.prefix = toCidr(info.addrType, std::string(subnetMask));
             addrList.emplace_back(info);
         }
     }
@@ -195,96 +193,5 @@
     return intfMap;
 }
 
-uint8_t Manager::toCidr(int addressFamily, const std::string& subnetMask) const
-{
-    uint32_t buff = 0;
-
-    if (addressFamily == AF_INET6)
-    {
-        return toV6Cidr(std::string(subnetMask));
-    }
-
-    auto rc = inet_pton(addressFamily, subnetMask.c_str(), &buff);
-    if (rc <= 0)
-    {
-        log<level::ERR>("inet_pton failed:",
-                         entry("SUBNETMASK=%s", subnetMask.c_str()));
-    }
-
-    buff = be32toh(buff);
-    // total no of bits - total no of leading zero == total no of ones
-    if (((sizeof(buff) * 8) - (__builtin_ctz(buff))) == __builtin_popcount(buff))
-    {
-        return __builtin_popcount(buff);
-    }
-    else
-    {
-        log<level::ERR>("Invalid Mask",
-                         entry("SUBNETMASK=%s", subnetMask.c_str()));
-        return 0;
-    }
-}
-
-uint8_t Manager::toV6Cidr(const std::string& subnetMask) const
-{
-    uint8_t pos {};
-    uint8_t prevPos {};
-    uint8_t cidr {};
-    uint16_t buff {};
-
-    log<level::INFO>("toV6Cidr called with",
-                     entry("SUBNETMASK=%s", subnetMask));
-    do
-    {
-        //subnet mask look like ffff:ffff::
-        // or ffff:c000::
-        pos =  subnetMask.find(":", prevPos);
-        if (pos == std::string::npos)
-        {
-            return cidr;
-        }
-
-        auto str = subnetMask.substr(prevPos, (pos - prevPos));
-        prevPos = pos + 1;
-
-        // String length is 0
-        if (!str.length())
-        {
-            return cidr;
-        }
-        //converts it into number.
-        if (sscanf(str.c_str(), "%hx", &buff) <= 0)
-        {
-            log<level::ERR>("Invalid SubnetMask",
-                             entry("SUBNETMASK=%s", subnetMask));
-
-            return 0;
-        }
-
-        // convert the number into bitset
-        // and check for how many ones are there.
-        // if we don't have all the ones then make
-        // sure that all the ones should be left justify.
-
-        if (__builtin_popcount(buff) != 16)
-        {
-            if (((sizeof(buff) * 8) - (__builtin_ctz(buff))) != __builtin_popcount(buff))
-            {
-                log<level::ERR>("Invalid SubnetMask",
-                                 entry("SUBNETMASK=%s", subnetMask));
-
-                return 0;
-            }
-            cidr += __builtin_popcount(buff);
-            return cidr;
-        }
-        cidr += 16;
-
-    }
-    while (1);
-
-    return cidr;
-}
-
 }//namespace network
 }//namespace phosphor