diff options
Diffstat (limited to 'util.cpp')
-rw-r--r-- | util.cpp | 144 |
1 files changed, 65 insertions, 79 deletions
@@ -34,12 +34,12 @@ uint8_t toV6Cidr(const std::string& subnetMask) uint8_t pos = 0; uint8_t prevPos = 0; uint8_t cidr = 0; - uint16_t buff {}; + uint16_t buff{}; do { - //subnet mask look like ffff:ffff:: + // subnet mask look like ffff:ffff:: // or ffff:c000:: - pos = subnetMask.find(":", prevPos); + pos = subnetMask.find(":", prevPos); if (pos == std::string::npos) { break; @@ -53,7 +53,7 @@ uint8_t toV6Cidr(const std::string& subnetMask) { return cidr; } - //converts it into number. + // converts it into number. if (sscanf(str.c_str(), "%hx", &buff) <= 0) { log<level::ERR>("Invalid Mask", @@ -69,7 +69,8 @@ uint8_t toV6Cidr(const std::string& subnetMask) if (__builtin_popcount(buff) != 16) { - if (((sizeof(buff) * 8) - (__builtin_ctz(buff))) != __builtin_popcount(buff)) + if (((sizeof(buff) * 8) - (__builtin_ctz(buff))) != + __builtin_popcount(buff)) { log<level::ERR>("Invalid Mask", entry("SUBNETMASK=%s", subnetMask.c_str())); @@ -81,12 +82,11 @@ uint8_t toV6Cidr(const std::string& subnetMask) } cidr += 16; - } - while (1); + } while (1); return cidr; } -}// anonymous namespace +} // anonymous namespace uint8_t toCidr(int addressFamily, const std::string& subnetMask) { @@ -107,7 +107,8 @@ uint8_t toCidr(int addressFamily, const std::string& subnetMask) 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)) + if (((sizeof(buff) * 8) - (__builtin_ctz(buff))) == + __builtin_popcount(buff)) { return __builtin_popcount(buff); } @@ -123,19 +124,18 @@ std::string toMask(int addressFamily, uint8_t prefix) { if (addressFamily == AF_INET6) { - //TODO:- conversion for v6 + // TODO:- conversion for v6 return ""; } if (prefix < 1 || prefix > 30) { - log<level::ERR>("Invalid Prefix", - entry("PREFIX=%d", prefix)); + log<level::ERR>("Invalid Prefix", entry("PREFIX=%d", prefix)); return ""; } /* Create the netmask from the number of bits */ unsigned long mask = 0; - for (auto i = 0 ; i < prefix ; i++) + for (auto i = 0; i < prefix; i++) { mask |= 1 << (31 - i); } @@ -149,12 +149,13 @@ std::string getNetworkID(int addressFamily, const std::string& ipaddress, { unsigned char* pntMask = nullptr; unsigned char* pntNetwork = nullptr; - int bit {}; - int offset {}; - struct in6_addr netmask {}; + int bit{}; + int offset{}; + struct in6_addr netmask + { + }; const u_char maskbit[] = {0x00, 0x80, 0xc0, 0xe0, 0xf0, - 0xf8, 0xfc, 0xfe, 0xff - }; + 0xf8, 0xfc, 0xfe, 0xff}; pntMask = reinterpret_cast<unsigned char*>(&netmask); @@ -186,13 +187,13 @@ std::string getNetworkID(int addressFamily, const std::string& ipaddress, pntMask = reinterpret_cast<unsigned char*>(&netmask); pntNetwork = reinterpret_cast<unsigned char*>(&ipaddressNetwork); - for (int i = 0; i < 16 ; i++) + for (int i = 0; i < 16; i++) { pntNetwork[i] = pntNetwork[i] & pntMask[i]; } - //convert the network address into string fomat. - char networkString[INET6_ADDRSTRLEN] = { 0 }; + // convert the network address into string fomat. + char networkString[INET6_ADDRSTRLEN] = {0}; if (inet_ntop(addressFamily, &ipaddressNetwork, networkString, INET6_ADDRSTRLEN) == NULL) { @@ -239,7 +240,7 @@ bool isValidPrefix(int addressFamily, uint8_t prefixLength) IntfAddrMap getInterfaceAddrs() { - IntfAddrMap intfMap {}; + IntfAddrMap intfMap{}; struct ifaddrs* ifaddr = nullptr; // attempt to fill struct with ifaddrs @@ -254,7 +255,7 @@ IntfAddrMap getInterfaceAddrs() AddrPtr ifaddrPtr(ifaddr); ifaddr = nullptr; - std::string intfName {}; + std::string intfName{}; for (ifaddrs* ifa = ifaddrPtr.get(); ifa != nullptr; ifa = ifa->ifa_next) { @@ -275,36 +276,32 @@ IntfAddrMap getInterfaceAddrs() continue; } intfName = ifa->ifa_name; - AddrInfo info {}; - char ip[INET6_ADDRSTRLEN] = { 0 }; - char subnetMask[INET6_ADDRSTRLEN] = { 0 }; + AddrInfo info{}; + char ip[INET6_ADDRSTRLEN] = {0}; + char subnetMask[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), - ip, - sizeof(ip)); - - inet_ntop(ifa->ifa_addr->sa_family, - &(((struct sockaddr_in*)(ifa->ifa_netmask))->sin_addr), - subnetMask, - sizeof(subnetMask)); + ip, sizeof(ip)); + inet_ntop( + ifa->ifa_addr->sa_family, + &(((struct sockaddr_in*)(ifa->ifa_netmask))->sin_addr), + subnetMask, sizeof(subnetMask)); } else { inet_ntop(ifa->ifa_addr->sa_family, &(((struct sockaddr_in6*)(ifa->ifa_addr))->sin6_addr), - ip, - sizeof(ip)); - - inet_ntop(ifa->ifa_addr->sa_family, - &(((struct sockaddr_in6*)(ifa->ifa_netmask))->sin6_addr), - subnetMask, - sizeof(subnetMask)); + ip, sizeof(ip)); + inet_ntop( + ifa->ifa_addr->sa_family, + &(((struct sockaddr_in6*)(ifa->ifa_netmask))->sin6_addr), + subnetMask, sizeof(subnetMask)); } info.addrType = ifa->ifa_addr->sa_family; @@ -318,7 +315,7 @@ IntfAddrMap getInterfaceAddrs() InterfaceList getInterfaces() { - InterfaceList interfaces {}; + InterfaceList interfaces{}; struct ifaddrs* ifaddr = nullptr; // attempt to fill struct with ifaddrs @@ -337,8 +334,7 @@ InterfaceList getInterfaces() { // walk interfaces // if loopback, or not running ignore - if ((ifa->ifa_flags & IFF_LOOPBACK) || - !(ifa->ifa_flags & IFF_RUNNING)) + if ((ifa->ifa_flags & IFF_LOOPBACK) || !(ifa->ifa_flags & IFF_RUNNING)) { continue; } @@ -347,27 +343,24 @@ InterfaceList getInterfaces() return interfaces; } - void deleteInterface(const std::string& intf) { pid_t pid = fork(); - int status {}; + int status{}; if (pid == 0) { execl("/sbin/ip", "ip", "link", "delete", "dev", intf.c_str(), nullptr); auto error = errno; - log<level::ERR>("Couldn't delete the device", - entry("ERRNO=%d", error), + log<level::ERR>("Couldn't delete the device", entry("ERRNO=%d", error), entry("INTF=%s", intf.c_str())); elog<InternalFailure>(); } else if (pid < 0) { auto error = errno; - log<level::ERR>("Error occurred during fork", - entry("ERRNO=%d", error)); + log<level::ERR>("Error occurred during fork", entry("ERRNO=%d", error)); elog<InternalFailure>(); } else if (pid > 0) @@ -375,13 +368,13 @@ void deleteInterface(const std::string& intf) while (waitpid(pid, &status, 0) == -1) { if (errno != EINTR) - { /* Error other than EINTR */ + { /* Error other than EINTR */ status = -1; break; } } - if(status < 0) + if (status < 0) { log<level::ERR>("Unable to delete the interface", entry("INTF=%s", intf.c_str()), @@ -395,7 +388,7 @@ bool getDHCPValue(const std::string& confDir, const std::string& intf) { bool dhcp = false; // Get the interface mode value from systemd conf - //using namespace std::string_literals; + // using namespace std::string_literals; fs::path confPath = confDir; std::string fileName = systemd::config::networkFilePrefix + intf + systemd::config::networkFileSuffix; @@ -409,7 +402,7 @@ bool getDHCPValue(const std::string& confDir, const std::string& intf) if (rc != config::ReturnCode::SUCCESS) { log<level::DEBUG>("Unable to get the value for Network[DHCP]", - entry("RC=%d", rc)); + entry("RC=%d", rc)); return dhcp; } // There will be only single value for DHCP key. @@ -427,7 +420,7 @@ void executeCommandinChildProcess(const char* path, char** args) { using namespace std::string_literals; pid_t pid = fork(); - int status {}; + int status{}; if (pid == 0) { @@ -436,9 +429,9 @@ void executeCommandinChildProcess(const char* path, char** args) // create the command from var args. std::string command = path + " "s; - for(int i = 0; args[i]; i++) + for (int i = 0; args[i]; i++) { - command += args[i] + " "s; + command += args[i] + " "s; } log<level::ERR>("Couldn't exceute the command", @@ -449,8 +442,7 @@ void executeCommandinChildProcess(const char* path, char** args) else if (pid < 0) { auto error = errno; - log<level::ERR>("Error occurred during fork", - entry("ERRNO=%d", error)); + log<level::ERR>("Error occurred during fork", entry("ERRNO=%d", error)); elog<InternalFailure>(); } else if (pid > 0) @@ -458,16 +450,16 @@ void executeCommandinChildProcess(const char* path, char** args) while (waitpid(pid, &status, 0) == -1) { if (errno != EINTR) - { //Error other than EINTR + { // Error other than EINTR status = -1; break; } } - if(status < 0) + if (status < 0) { std::string command = path + " "s; - for(int i = 0; args[i]; i++) + for (int i = 0; args[i]; i++) { command += args[i] + " "s; } @@ -478,9 +470,8 @@ void executeCommandinChildProcess(const char* path, char** args) elog<InternalFailure>(); } } - } -} //namespace internal +} // namespace internal namespace mac_address { @@ -494,12 +485,12 @@ constexpr auto methodGet = "Get"; using DbusObjectPath = std::string; using DbusService = std::string; using DbusInterface = std::string; -using ObjectTree = std::map<DbusObjectPath, - std::map<DbusService, std::vector<DbusInterface>>>; +using ObjectTree = + std::map<DbusObjectPath, std::map<DbusService, std::vector<DbusInterface>>>; constexpr auto invBus = "xyz.openbmc_project.Inventory.Manager"; constexpr auto invNetworkIntf = - "xyz.openbmc_project.Inventory.Item.NetworkInterface"; + "xyz.openbmc_project.Inventory.Item.NetworkInterface"; constexpr auto invRoot = "/xyz/openbmc_project/inventory"; std::string getfromInventory(sdbusplus::bus::bus& bus) @@ -509,10 +500,8 @@ std::string getfromInventory(sdbusplus::bus::bus& bus) auto depth = 0; - auto mapperCall = bus.new_method_call(mapperBus, - mapperObj, - mapperIntf, - "GetSubTree"); + auto mapperCall = + bus.new_method_call(mapperBus, mapperObj, mapperIntf, "GetSubTree"); mapperCall.append(invRoot, depth, interfaces); @@ -540,18 +529,15 @@ std::string getfromInventory(sdbusplus::bus::bus& bus) sdbusplus::message::variant<std::string> value; - auto method = bus.new_method_call( - service.c_str(), - objPath.c_str(), - propIntf, - methodGet); + auto method = bus.new_method_call(service.c_str(), objPath.c_str(), + propIntf, methodGet); method.append(invNetworkIntf, "MACAddress"); auto reply = bus.call(method); if (reply.is_method_error()) { - log<level::ERR>("Failed to get MACAddress", + log<level::ERR>("Failed to get MACAddress", entry("PATH=%s", objPath.c_str()), entry("INTERFACE=%s", invNetworkIntf)); elog<InternalFailure>(); @@ -561,6 +547,6 @@ std::string getfromInventory(sdbusplus::bus::bus& bus) return value.get<std::string>(); } -}//namespace mac_address -}//namespace network -}//namespace phosphor +} // namespace mac_address +} // namespace network +} // namespace phosphor |