Ratan Gupta | 6811f82 | 2017-04-14 16:34:56 +0530 | [diff] [blame] | 1 | #include "config.h" |
| 2 | #include "network_manager.hpp" |
| 3 | |
| 4 | #include <phosphor-logging/log.hpp> |
| 5 | |
| 6 | #include <algorithm> |
| 7 | |
| 8 | #include <arpa/inet.h> |
| 9 | #include <dirent.h> |
| 10 | #include <net/if.h> |
| 11 | |
| 12 | |
| 13 | namespace phosphor |
| 14 | { |
| 15 | namespace network |
| 16 | { |
| 17 | using namespace phosphor::logging; |
| 18 | |
| 19 | Manager::Manager(sdbusplus::bus::bus& bus, const char* objPath): |
| 20 | details::VLANCreateIface(bus, objPath, true) |
| 21 | { |
| 22 | auto interfaceInfoList = getInterfaceAndaddrs(); |
| 23 | |
| 24 | for( const auto& intfInfo : interfaceInfoList ) |
| 25 | { |
| 26 | std::string objectPath = std::string(OBJ_NETWORK) + "/" + intfInfo.first; |
| 27 | |
| 28 | this->interfaces.emplace(std::make_pair( |
| 29 | intfInfo.first, |
| 30 | std::make_unique< |
| 31 | phosphor::network::EthernetInterface > |
| 32 | (bus, objectPath.c_str(), |
| 33 | false))); |
| 34 | } |
| 35 | } |
| 36 | |
| 37 | void Manager::vLAN(details::IntfName interfaceName, uint16_t id) |
| 38 | { |
| 39 | } |
| 40 | |
| 41 | details::IntfAddrMap Manager::getInterfaceAndaddrs() const |
| 42 | { |
| 43 | details::IntfAddrMap intfMap; |
| 44 | details::AddrList addrList; |
| 45 | struct ifaddrs* ifaddr; |
| 46 | // attempt to fill struct with ifaddrs |
| 47 | if (getifaddrs(&ifaddr) == -1) |
| 48 | { |
| 49 | log<level::ERR>("getifaddrs failed:", |
| 50 | entry("ERRNO=%s", strerror(errno))); |
| 51 | |
| 52 | //TODO: openbmc/openbmc#1462 <create the error log> |
| 53 | |
| 54 | return intfMap; |
| 55 | } |
| 56 | |
| 57 | details::AddrPtr ifaddrPtr(ifaddr); |
| 58 | ifaddr = nullptr; |
| 59 | |
| 60 | std::string intfName; |
| 61 | |
| 62 | for (ifaddrs* ifa = ifaddrPtr.get(); ifa != nullptr; ifa = ifa->ifa_next) |
| 63 | { |
| 64 | // walk interfaces |
| 65 | if (ifa->ifa_addr == nullptr) |
| 66 | { |
| 67 | continue; |
| 68 | } |
| 69 | |
| 70 | // get only INET interfaces not ipv6 |
| 71 | if (ifa->ifa_addr->sa_family == AF_INET || |
| 72 | ifa->ifa_addr->sa_family == AF_INET6) |
| 73 | { |
| 74 | // if loopback, or not running ignore |
| 75 | if ((ifa->ifa_flags & IFF_LOOPBACK) || |
| 76 | !(ifa->ifa_flags & IFF_RUNNING)) |
| 77 | { |
| 78 | continue; |
| 79 | } |
| 80 | // if the interface name is not same as the previous |
| 81 | // iteration then add the addr list into |
| 82 | // the map. |
| 83 | if (intfName != "" && intfName != std::string(ifa->ifa_name)) |
| 84 | { |
| 85 | intfMap.emplace(intfName, addrList); |
| 86 | addrList.clear(); |
| 87 | } |
| 88 | intfName = ifa->ifa_name; |
| 89 | details::AddrInfo info; |
| 90 | char tmp[INET6_ADDRSTRLEN] = { 0 }; |
| 91 | |
| 92 | if (ifa->ifa_addr->sa_family == AF_INET) |
| 93 | { |
| 94 | |
| 95 | inet_ntop(ifa->ifa_addr->sa_family, |
| 96 | &(((struct sockaddr_in*)(ifa->ifa_addr))->sin_addr), |
| 97 | tmp, |
| 98 | sizeof(tmp)); |
| 99 | } |
| 100 | else |
| 101 | { |
| 102 | inet_ntop(ifa->ifa_addr->sa_family, |
| 103 | &(((struct sockaddr_in6*)(ifa->ifa_addr))->sin6_addr), |
| 104 | tmp, |
| 105 | sizeof(tmp)); |
| 106 | |
| 107 | } |
| 108 | |
| 109 | info.addrType = ifa->ifa_addr->sa_family; |
| 110 | info.ipaddress = tmp; |
| 111 | addrList.emplace_back(info); |
| 112 | } |
| 113 | } |
| 114 | intfMap.emplace(intfName, addrList); |
| 115 | return intfMap; |
| 116 | } |
| 117 | }//namespace network |
| 118 | }//namespace phosphor |