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> |
Ratan Gupta | 82549cc | 2017-04-21 08:45:23 +0530 | [diff] [blame^] | 7 | #include <experimental/filesystem> |
Ratan Gupta | 6811f82 | 2017-04-14 16:34:56 +0530 | [diff] [blame] | 8 | #include <arpa/inet.h> |
| 9 | #include <dirent.h> |
| 10 | #include <net/if.h> |
| 11 | |
| 12 | |
| 13 | namespace phosphor |
| 14 | { |
| 15 | namespace network |
| 16 | { |
Ratan Gupta | 82549cc | 2017-04-21 08:45:23 +0530 | [diff] [blame^] | 17 | |
Ratan Gupta | 6811f82 | 2017-04-14 16:34:56 +0530 | [diff] [blame] | 18 | using namespace phosphor::logging; |
Ratan Gupta | 82549cc | 2017-04-21 08:45:23 +0530 | [diff] [blame^] | 19 | namespace fs = std::experimental::filesystem; |
Ratan Gupta | 6811f82 | 2017-04-14 16:34:56 +0530 | [diff] [blame] | 20 | |
| 21 | Manager::Manager(sdbusplus::bus::bus& bus, const char* objPath): |
| 22 | details::VLANCreateIface(bus, objPath, true) |
| 23 | { |
Ratan Gupta | 82549cc | 2017-04-21 08:45:23 +0530 | [diff] [blame^] | 24 | auto interfaceInfoList = getInterfaceAddrs(); |
Ratan Gupta | 6811f82 | 2017-04-14 16:34:56 +0530 | [diff] [blame] | 25 | |
| 26 | for( const auto& intfInfo : interfaceInfoList ) |
| 27 | { |
Ratan Gupta | 82549cc | 2017-04-21 08:45:23 +0530 | [diff] [blame^] | 28 | |
| 29 | fs::path objectPath = std::string(OBJ_NETWORK); |
| 30 | objectPath /= intfInfo.first; |
Ratan Gupta | 6811f82 | 2017-04-14 16:34:56 +0530 | [diff] [blame] | 31 | |
| 32 | this->interfaces.emplace(std::make_pair( |
| 33 | intfInfo.first, |
| 34 | std::make_unique< |
| 35 | phosphor::network::EthernetInterface > |
| 36 | (bus, objectPath.c_str(), |
Ratan Gupta | 82549cc | 2017-04-21 08:45:23 +0530 | [diff] [blame^] | 37 | false,intfInfo.second))); |
Ratan Gupta | 6811f82 | 2017-04-14 16:34:56 +0530 | [diff] [blame] | 38 | } |
| 39 | } |
| 40 | |
Ratan Gupta | 82549cc | 2017-04-21 08:45:23 +0530 | [diff] [blame^] | 41 | void Manager::vLAN(IntfName interfaceName, uint16_t id) |
Ratan Gupta | 6811f82 | 2017-04-14 16:34:56 +0530 | [diff] [blame] | 42 | { |
| 43 | } |
| 44 | |
Ratan Gupta | 82549cc | 2017-04-21 08:45:23 +0530 | [diff] [blame^] | 45 | IntfAddrMap Manager::getInterfaceAddrs() const |
Ratan Gupta | 6811f82 | 2017-04-14 16:34:56 +0530 | [diff] [blame] | 46 | { |
Ratan Gupta | 82549cc | 2017-04-21 08:45:23 +0530 | [diff] [blame^] | 47 | IntfAddrMap intfMap; |
| 48 | AddrList addrList; |
Ratan Gupta | 6811f82 | 2017-04-14 16:34:56 +0530 | [diff] [blame] | 49 | 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 Gupta | 82549cc | 2017-04-21 08:45:23 +0530 | [diff] [blame^] | 93 | AddrInfo info; |
| 94 | char ip[INET6_ADDRSTRLEN] = { 0 }; |
Ratan Gupta | 6811f82 | 2017-04-14 16:34:56 +0530 | [diff] [blame] | 95 | |
| 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 Gupta | 82549cc | 2017-04-21 08:45:23 +0530 | [diff] [blame^] | 101 | ip, |
| 102 | sizeof(ip)); |
Ratan Gupta | 6811f82 | 2017-04-14 16:34:56 +0530 | [diff] [blame] | 103 | } |
| 104 | else |
| 105 | { |
| 106 | inet_ntop(ifa->ifa_addr->sa_family, |
| 107 | &(((struct sockaddr_in6*)(ifa->ifa_addr))->sin6_addr), |
Ratan Gupta | 82549cc | 2017-04-21 08:45:23 +0530 | [diff] [blame^] | 108 | ip, |
| 109 | sizeof(ip)); |
Ratan Gupta | 6811f82 | 2017-04-14 16:34:56 +0530 | [diff] [blame] | 110 | |
| 111 | } |
| 112 | |
| 113 | info.addrType = ifa->ifa_addr->sa_family; |
Ratan Gupta | 82549cc | 2017-04-21 08:45:23 +0530 | [diff] [blame^] | 114 | info.ipaddress = ip; |
Ratan Gupta | 6811f82 | 2017-04-14 16:34:56 +0530 | [diff] [blame] | 115 | addrList.emplace_back(info); |
| 116 | } |
| 117 | } |
| 118 | intfMap.emplace(intfName, addrList); |
| 119 | return intfMap; |
| 120 | } |
| 121 | }//namespace network |
| 122 | }//namespace phosphor |