blob: 73629437ee3d69b24b1e969185573cda1e18e4a3 [file] [log] [blame]
Ratan Gupta6811f822017-04-14 16:34:56 +05301#include "config.h"
2#include "network_manager.hpp"
3
4#include <phosphor-logging/log.hpp>
5
6#include <algorithm>
Ratan Gupta82549cc2017-04-21 08:45:23 +05307#include <experimental/filesystem>
Ratan Gupta6811f822017-04-14 16:34:56 +05308#include <arpa/inet.h>
9#include <dirent.h>
10#include <net/if.h>
11
12
13namespace phosphor
14{
15namespace network
16{
Ratan Gupta82549cc2017-04-21 08:45:23 +053017
Ratan Gupta6811f822017-04-14 16:34:56 +053018using namespace phosphor::logging;
Ratan Gupta82549cc2017-04-21 08:45:23 +053019namespace fs = std::experimental::filesystem;
Ratan Gupta6811f822017-04-14 16:34:56 +053020
21Manager::Manager(sdbusplus::bus::bus& bus, const char* objPath):
22 details::VLANCreateIface(bus, objPath, true)
23{
Ratan Gupta82549cc2017-04-21 08:45:23 +053024 auto interfaceInfoList = getInterfaceAddrs();
Ratan Gupta6811f822017-04-14 16:34:56 +053025
26 for( const auto& intfInfo : interfaceInfoList )
27 {
Ratan Gupta82549cc2017-04-21 08:45:23 +053028
29 fs::path objectPath = std::string(OBJ_NETWORK);
30 objectPath /= intfInfo.first;
Ratan Gupta6811f822017-04-14 16:34:56 +053031
32 this->interfaces.emplace(std::make_pair(
33 intfInfo.first,
34 std::make_unique<
35 phosphor::network::EthernetInterface >
36 (bus, objectPath.c_str(),
Ratan Gupta82549cc2017-04-21 08:45:23 +053037 false,intfInfo.second)));
Ratan Gupta6811f822017-04-14 16:34:56 +053038 }
39}
40
Ratan Gupta82549cc2017-04-21 08:45:23 +053041void Manager::vLAN(IntfName interfaceName, uint16_t id)
Ratan Gupta6811f822017-04-14 16:34:56 +053042{
43}
44
Ratan Gupta82549cc2017-04-21 08:45:23 +053045IntfAddrMap Manager::getInterfaceAddrs() const
Ratan Gupta6811f822017-04-14 16:34:56 +053046{
Ratan Gupta82549cc2017-04-21 08:45:23 +053047 IntfAddrMap intfMap;
48 AddrList addrList;
Ratan Gupta6811f822017-04-14 16:34:56 +053049 struct ifaddrs* ifaddr;
50 // attempt to fill struct with ifaddrs
51 if (getifaddrs(&ifaddr) == -1)
52 {
53 log<level::ERR>("getifaddrs failed:",
54 entry("ERRNO=%s", strerror(errno)));
55
56 //TODO: openbmc/openbmc#1462 <create the error log>
57
58 return intfMap;
59 }
60
61 details::AddrPtr ifaddrPtr(ifaddr);
62 ifaddr = nullptr;
63
64 std::string intfName;
65
66 for (ifaddrs* ifa = ifaddrPtr.get(); ifa != nullptr; ifa = ifa->ifa_next)
67 {
68 // walk interfaces
69 if (ifa->ifa_addr == nullptr)
70 {
71 continue;
72 }
73
74 // get only INET interfaces not ipv6
75 if (ifa->ifa_addr->sa_family == AF_INET ||
76 ifa->ifa_addr->sa_family == AF_INET6)
77 {
78 // if loopback, or not running ignore
79 if ((ifa->ifa_flags & IFF_LOOPBACK) ||
80 !(ifa->ifa_flags & IFF_RUNNING))
81 {
82 continue;
83 }
84 // if the interface name is not same as the previous
85 // iteration then add the addr list into
86 // the map.
87 if (intfName != "" && intfName != std::string(ifa->ifa_name))
88 {
89 intfMap.emplace(intfName, addrList);
90 addrList.clear();
91 }
92 intfName = ifa->ifa_name;
Ratan Gupta82549cc2017-04-21 08:45:23 +053093 AddrInfo info;
94 char ip[INET6_ADDRSTRLEN] = { 0 };
Ratan Gupta6811f822017-04-14 16:34:56 +053095
96 if (ifa->ifa_addr->sa_family == AF_INET)
97 {
98
99 inet_ntop(ifa->ifa_addr->sa_family,
100 &(((struct sockaddr_in*)(ifa->ifa_addr))->sin_addr),
Ratan Gupta82549cc2017-04-21 08:45:23 +0530101 ip,
102 sizeof(ip));
Ratan Gupta6811f822017-04-14 16:34:56 +0530103 }
104 else
105 {
106 inet_ntop(ifa->ifa_addr->sa_family,
107 &(((struct sockaddr_in6*)(ifa->ifa_addr))->sin6_addr),
Ratan Gupta82549cc2017-04-21 08:45:23 +0530108 ip,
109 sizeof(ip));
Ratan Gupta6811f822017-04-14 16:34:56 +0530110
111 }
112
113 info.addrType = ifa->ifa_addr->sa_family;
Ratan Gupta82549cc2017-04-21 08:45:23 +0530114 info.ipaddress = ip;
Ratan Gupta6811f822017-04-14 16:34:56 +0530115 addrList.emplace_back(info);
116 }
117 }
118 intfMap.emplace(intfName, addrList);
119 return intfMap;
120}
121}//namespace network
122}//namespace phosphor