Implement NetworkManager
Change-Id: Ifa8169d903ecab1575662cfc0f9a1c950d56144f
Signed-off-by: Ratan Gupta <ratagupt@in.ibm.com>
diff --git a/Makefile.am b/Makefile.am
index abc5067..d5bc9b3 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -15,6 +15,7 @@
phosphor_network_manager_SOURCES = \
ethernet_interface.cpp \
+ network_manager.cpp \
network_manager_main.cpp \
xyz/openbmc_project/Network/VLAN/Create/server.cpp
diff --git a/network_manager.cpp b/network_manager.cpp
new file mode 100644
index 0000000..479e8a4
--- /dev/null
+++ b/network_manager.cpp
@@ -0,0 +1,118 @@
+#include "config.h"
+#include "network_manager.hpp"
+
+#include <phosphor-logging/log.hpp>
+
+#include <algorithm>
+
+#include <arpa/inet.h>
+#include <dirent.h>
+#include <net/if.h>
+
+
+namespace phosphor
+{
+namespace network
+{
+using namespace phosphor::logging;
+
+Manager::Manager(sdbusplus::bus::bus& bus, const char* objPath):
+ details::VLANCreateIface(bus, objPath, true)
+{
+ auto interfaceInfoList = getInterfaceAndaddrs();
+
+ for( const auto& intfInfo : interfaceInfoList )
+ {
+ std::string objectPath = std::string(OBJ_NETWORK) + "/" + intfInfo.first;
+
+ this->interfaces.emplace(std::make_pair(
+ intfInfo.first,
+ std::make_unique<
+ phosphor::network::EthernetInterface >
+ (bus, objectPath.c_str(),
+ false)));
+ }
+}
+
+void Manager::vLAN(details::IntfName interfaceName, uint16_t id)
+{
+}
+
+details::IntfAddrMap Manager::getInterfaceAndaddrs() const
+{
+ details::IntfAddrMap intfMap;
+ details::AddrList addrList;
+ struct ifaddrs* ifaddr;
+ // attempt to fill struct with ifaddrs
+ if (getifaddrs(&ifaddr) == -1)
+ {
+ log<level::ERR>("getifaddrs failed:",
+ entry("ERRNO=%s", strerror(errno)));
+
+ //TODO: openbmc/openbmc#1462 <create the error log>
+
+ return intfMap;
+ }
+
+ 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;
+ details::AddrInfo info;
+ char tmp[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),
+ tmp,
+ sizeof(tmp));
+ }
+ else
+ {
+ inet_ntop(ifa->ifa_addr->sa_family,
+ &(((struct sockaddr_in6*)(ifa->ifa_addr))->sin6_addr),
+ tmp,
+ sizeof(tmp));
+
+ }
+
+ info.addrType = ifa->ifa_addr->sa_family;
+ info.ipaddress = tmp;
+ addrList.emplace_back(info);
+ }
+ }
+ intfMap.emplace(intfName, addrList);
+ return intfMap;
+}
+}//namespace network
+}//namespace phosphor
diff --git a/network_manager.hpp b/network_manager.hpp
index 51e9d2c..2e90b23 100644
--- a/network_manager.hpp
+++ b/network_manager.hpp
@@ -4,8 +4,10 @@
#include "xyz/openbmc_project/Network/VLAN/Create/server.hpp"
#include <sdbusplus/bus.hpp>
+#include <ifaddrs.h>
#include <list>
+#include <memory>
#include <string>
#include <vector>
@@ -31,6 +33,18 @@
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>;