From 046432b8d6840b9d70891caaab2c5155229e2f77 Mon Sep 17 00:00:00 2001 From: "Maciej J. Karpiuk" Date: Mon, 12 Oct 2015 12:29:54 +0200 Subject: [PATCH] vasum: housekeeping, removal of 'using namespace std;' [Feature] N/A [Cause] N/A [Solution] N/A [Verification] N/A Change-Id: I583b5ad44cd0d460c6b0c4db0d05217ab05cc2e4 --- cli/command-line-interface.cpp | 126 ++++++++++++++++++++--------------------- client/vasum-client-impl.cpp | 43 +++++++------- client/vasum-client-impl.hpp | 2 +- client/vasum-client.cpp | 4 +- server/netdev.cpp | 69 +++++++++++----------- 5 files changed, 119 insertions(+), 125 deletions(-) diff --git a/cli/command-line-interface.cpp b/cli/command-line-interface.cpp index 27d7cac..ad5400d 100644 --- a/cli/command-line-interface.cpp +++ b/cli/command-line-interface.cpp @@ -44,8 +44,6 @@ #include #include -using namespace std; - namespace vasum { namespace cli { @@ -92,23 +90,23 @@ std::string netdevToString(const VsmNetdev& netdev) return out; } -typedef vector> Table; +typedef std::vector> Table; -ostream& operator<<(ostream& out, const Table& table) +std::ostream& operator<<(std::ostream& out, const Table& table) { - vector sizes; + std::vector sizes; for (const auto& row : table) { if (sizes.size() < row.size()) { sizes.resize(row.size()); } for (size_t i = 0; i < row.size(); ++i) { - sizes[i] = max(sizes[i], row[i].length()); + sizes[i] = std::max(sizes[i], row[i].length()); } } for (const auto& row : table) { for (size_t i = 0; i < row.size(); ++i) { - out << left << setw(sizes[i]+2) << row[i]; + out << std::left << std::setw(sizes[i]+2) << row[i]; } out << "\n"; } @@ -129,7 +127,7 @@ enum macvlan_mode macvlanFromString(const std::string& mode) { if (mode == "passthru") { return MACVLAN_MODE_PASSTHRU; } - throw runtime_error("Unsupported macvlan mode"); + throw std::runtime_error("Unsupported macvlan mode"); } void buildZoneList(std::vector& list) @@ -144,7 +142,7 @@ void buildZoneList(std::vector& list) vsm_array_string_free(ids); } -void buildNetdevList(const std::string& zone,std::vector& list) +void buildNetdevList(const std::string& zone, std::vector& list) { using namespace std::placeholders; VsmArrayString ids; @@ -167,9 +165,9 @@ const std::vector CommandLineInterface::buildCompletionList(const A } ArgSpec as = mArgsSpec[a.size() - 2]; - string::size_type s = 0U; - string::size_type e = s; - while (e != string::npos) { + std::string::size_type s = 0U; + std::string::size_type e = s; + while (e != std::string::npos) { e = as.format.find('|', s); std::string ss = as.format.substr(s, e - s); s = e + 1; @@ -197,12 +195,12 @@ void CommandLineInterface::connect() CommandLineInterface::client = vsm_client_create(); if (CommandLineInterface::client == nullptr) { - throw runtime_error("Can't create client"); + throw std::runtime_error("Can't create client"); } status = vsm_connect(client); if (VSMCLIENT_SUCCESS != status) { - string msg = vsm_get_status_message(CommandLineInterface::client); + std::string msg = vsm_get_status_message(CommandLineInterface::client); vsm_client_free(CommandLineInterface::client); CommandLineInterface::client = nullptr; throw IOException(msg); @@ -211,7 +209,7 @@ void CommandLineInterface::connect() void CommandLineInterface::disconnect() { - string msg; + std::string msg; VsmStatus status; if (CommandLineInterface::client == nullptr) { @@ -227,17 +225,17 @@ void CommandLineInterface::disconnect() CommandLineInterface::client = nullptr; if (VSMCLIENT_SUCCESS != status) { - throw runtime_error(msg); + throw std::runtime_error(msg); } } -void CommandLineInterface::executeCallback(const function& fun) +void CommandLineInterface::executeCallback(const std::function& fun) { CommandLineInterface::connect(); VsmStatus status = fun(CommandLineInterface::client); if (VSMCLIENT_SUCCESS != status) { - throw runtime_error(vsm_get_status_message(CommandLineInterface::client)); + throw std::runtime_error(vsm_get_status_message(CommandLineInterface::client)); } } @@ -306,7 +304,7 @@ void set_active_zone(const Args& argv) using namespace std::placeholders; if (argv.size() < 2) { - throw runtime_error("Not enough parameters"); + throw std::runtime_error("Not enough parameters"); } CommandLineInterface::executeCallback(bind(vsm_set_active_zone, _1, argv[1].c_str())); @@ -317,7 +315,7 @@ void create_zone(const Args& argv) using namespace std::placeholders; if (argv.size() < 2) { - throw runtime_error("Not enough parameters"); + throw std::runtime_error("Not enough parameters"); } if (argv.size() >= 3 && !argv[2].empty()) { @@ -332,7 +330,7 @@ void destroy_zone(const Args& argv) using namespace std::placeholders; if (argv.size() < 2) { - throw runtime_error("Not enough parameters"); + throw std::runtime_error("Not enough parameters"); } CommandLineInterface::executeCallback(bind(vsm_destroy_zone, _1, argv[1].c_str(), 1)); @@ -343,7 +341,7 @@ void shutdown_zone(const Args& argv) using namespace std::placeholders; if (argv.size() < 2) { - throw runtime_error("Not enough parameters"); + throw std::runtime_error("Not enough parameters"); } CommandLineInterface::executeCallback(bind(vsm_shutdown_zone, _1, argv[1].c_str())); @@ -354,7 +352,7 @@ void start_zone(const Args& argv) using namespace std::placeholders; if (argv.size() < 2) { - throw runtime_error("Not enough parameters"); + throw std::runtime_error("Not enough parameters"); } CommandLineInterface::executeCallback(bind(vsm_start_zone, _1, argv[1].c_str())); @@ -365,7 +363,7 @@ void console_zone(const Args& argv) using namespace std::placeholders; if (argv.size() < 2) { - throw runtime_error("Not enough parameters"); + throw std::runtime_error("Not enough parameters"); } VsmZone zone; @@ -373,7 +371,7 @@ void console_zone(const Args& argv) if (zoneStateToString(vsm_zone_get_state(zone)) != "RUNNING") { vsm_zone_free(zone); - throw runtime_error("Zone '" + argv[1] + "' is not running"); + throw std::runtime_error("Zone '" + argv[1] + "' is not running"); } std::string zonesPath = vsm_zone_get_rootfs(zone); @@ -389,7 +387,7 @@ void console_zone(const Args& argv) .add("-P").add(zonesPath.c_str()); if (!execv("/usr/bin/lxc-console", const_cast(args.c_array()))) { - throw runtime_error("Could not log into zone"); + throw std::runtime_error("Could not log into zone"); } } @@ -398,7 +396,7 @@ void lock_zone(const Args& argv) using namespace std::placeholders; if (argv.size() < 2) { - throw runtime_error("Not enough parameters"); + throw std::runtime_error("Not enough parameters"); } CommandLineInterface::executeCallback(bind(vsm_lock_zone, _1, argv[1].c_str())); @@ -409,7 +407,7 @@ void unlock_zone(const Args& argv) using namespace std::placeholders; if (argv.size() < 2) { - throw runtime_error("Not enough parameters"); + throw std::runtime_error("Not enough parameters"); } CommandLineInterface::executeCallback(bind(vsm_unlock_zone, _1, argv[1].c_str())); @@ -439,17 +437,17 @@ void get_zones_status(const Args& argv) for (VsmString* id = ids; *id; ++id) { VsmZone zone; CommandLineInterface::executeCallback(bind(vsm_lookup_zone_by_id, _1, *id, &zone)); - assert(string(vsm_zone_get_id(zone)) == string(*id)); - table.push_back({string(vsm_zone_get_id(zone)) == string(activeId) ? "YES" : "NO", + assert(std::string(vsm_zone_get_id(zone)) == std::string(*id)); + table.push_back({std::string(vsm_zone_get_id(zone)) == std::string(activeId) ? "YES" : "NO", vsm_zone_get_id(zone), zoneStateToString(vsm_zone_get_state(zone)), - to_string(vsm_zone_get_terminal(zone)), + std::to_string(vsm_zone_get_terminal(zone)), vsm_zone_get_rootfs(zone)}); vsm_zone_free(zone); } vsm_string_free(activeId); vsm_array_string_free(ids); - cout << table << endl; + std::cout << table << std::endl; } void get_zone_ids(const Args& /*argv*/) @@ -458,12 +456,12 @@ void get_zone_ids(const Args& /*argv*/) VsmArrayString ids; CommandLineInterface::executeCallback(bind(vsm_get_zone_ids, _1, &ids)); - string delim; + std::string delim; for (VsmString* id = ids; *id; ++id) { - cout << delim << *id; + std::cout << delim << *id; delim = ", "; } - cout << endl; + std::cout << std::endl; vsm_array_string_free(ids); } @@ -473,7 +471,7 @@ void get_active_zone(const Args& /*argv*/) VsmString id; CommandLineInterface::executeCallback(bind(vsm_get_active_zone_id, _1, &id)); - cout << id << endl; + std::cout << id << std::endl; vsm_string_free(id); } @@ -482,7 +480,7 @@ void grant_device(const Args& argv) using namespace std::placeholders; if (argv.size() < 3) { - throw runtime_error("Not enough parameters"); + throw std::runtime_error("Not enough parameters"); } uint32_t flags = O_RDWR; @@ -494,7 +492,7 @@ void revoke_device(const Args& argv) using namespace std::placeholders; if (argv.size() < 3) { - throw runtime_error("Not enough parameters"); + throw std::runtime_error("Not enough parameters"); } CommandLineInterface::executeCallback(bind(vsm_revoke_device, _1, argv[1].c_str(), argv[2].c_str())); @@ -505,13 +503,13 @@ void create_netdev(const Args& argv) using namespace std::placeholders; if (argv.size() < 3) { - throw runtime_error("Not enough parameters"); + throw std::runtime_error("Not enough parameters"); } std::string nettype = argv[2]; if (nettype == "phys") { if (argv.size() < 4) { - throw runtime_error("Not enough parameters"); + throw std::runtime_error("Not enough parameters"); } CommandLineInterface::executeCallback(bind(vsm_create_netdev_phys, _1, @@ -520,7 +518,7 @@ void create_netdev(const Args& argv) } else if (nettype == "veth") { if (argv.size() < 5) { - throw runtime_error("Not enough parameters"); + throw std::runtime_error("Not enough parameters"); } CommandLineInterface::executeCallback(bind(vsm_create_netdev_veth, _1, @@ -530,7 +528,7 @@ void create_netdev(const Args& argv) } else if (nettype == "macvlan") { if (argv.size() < 6) { - throw runtime_error("Not enough parameters"); + throw std::runtime_error("Not enough parameters"); } CommandLineInterface::executeCallback(bind(vsm_create_netdev_macvlan, _1, @@ -540,7 +538,7 @@ void create_netdev(const Args& argv) macvlanFromString(argv[5].c_str()))); } else - throw runtime_error("Wrong nettype option " + nettype); + throw std::runtime_error("Wrong nettype option " + nettype); } void destroy_netdev(const Args& argv) @@ -548,7 +546,7 @@ void destroy_netdev(const Args& argv) using namespace std::placeholders; if (argv.size() < 3) { - throw runtime_error("Not enough parameters"); + throw std::runtime_error("Not enough parameters"); } CommandLineInterface::executeCallback(bind(vsm_destroy_netdev, _1, @@ -561,7 +559,7 @@ void netdev_list(const Args& argv) using namespace std::placeholders; if (argv.size() < 2) { - throw runtime_error("Not enough parameters"); + throw std::runtime_error("Not enough parameters"); } if (argv.size() < 3) { VsmArrayString ids; @@ -569,15 +567,15 @@ void netdev_list(const Args& argv) _1, argv[1].c_str(), &ids)); - string delim; + std::string delim; for (VsmString* id = ids; *id; ++id) { - cout << delim << *id; + std::cout << delim << *id; delim = ", "; } if (delim.empty()) { - cout << "There is no network device in zone"; + std::cout << "There is no network device in zone"; } - cout << endl; + std::cout << std::endl; vsm_array_string_free(ids); } else { @@ -588,7 +586,7 @@ void netdev_list(const Args& argv) argv[1].c_str(), argv[2].c_str(), &netdev)); - cout << netdevToString(netdev) << endl; + std::cout << netdevToString(netdev) << std::endl; vsm_netdev_free(netdev); VsmAddrList addrs = NULL; @@ -601,9 +599,9 @@ void netdev_list(const Args& argv) for (unsigned i=0; i < listsize; ++i) { int type=vsm_addrlist_get_type(addrs, i); if (inet_ntop(type, vsm_addrlist_get_addr(addrs, i), buf, INET6_ADDRSTRLEN) == NULL) { - throw runtime_error("Wrong address received ["+std::to_string(i)+"] type="+std::to_string(type)); + throw std::runtime_error("Wrong address received ["+std::to_string(i)+"] type="+std::to_string(type)); } - cout << buf << "/" << vsm_addrlist_get_prefix(addrs, i) << endl; + std::cout << buf << "/" << vsm_addrlist_get_prefix(addrs, i) << std::endl; } vsm_addrlist_free(addrs); } @@ -613,31 +611,31 @@ void netdev_add_ip_addr(const Args& argv) { using namespace std::placeholders; if (argv.size() < 5) { - throw runtime_error("Not enough parameters"); + throw std::runtime_error("Not enough parameters"); } if (argv[3].find(':') == std::string::npos) { in_addr addr; if (inet_pton(AF_INET, argv[3].c_str(), &addr) != 1) { - throw runtime_error("Wrong address format"); + throw std::runtime_error("Wrong address format"); }; CommandLineInterface::executeCallback(bind(vsm_netdev_add_ipv4_addr, _1, argv[1].c_str(), argv[2].c_str(), &addr, - stoi(argv[4].c_str()))); + std::stoi(argv[4].c_str()))); } else { in6_addr addr; if (inet_pton(AF_INET6, argv[3].c_str(), &addr) != 1) { - throw runtime_error("Wrong address format"); + throw std::runtime_error("Wrong address format"); }; CommandLineInterface::executeCallback(bind(vsm_netdev_add_ipv6_addr, _1, argv[1].c_str(), argv[2].c_str(), &addr, - stoi(argv[4].c_str()))); + std::stoi(argv[4].c_str()))); } } @@ -645,31 +643,31 @@ void netdev_del_ip_addr(const Args& argv) { using namespace std::placeholders; if (argv.size() < 5) { - throw runtime_error("Not enough parameters"); + throw std::runtime_error("Not enough parameters"); } if (argv[3].find(':') == std::string::npos) { in_addr addr; if (inet_pton(AF_INET, argv[3].c_str(), &addr) != 1) { - throw runtime_error("Wrong address format"); + throw std::runtime_error("Wrong address format"); }; CommandLineInterface::executeCallback(bind(vsm_netdev_del_ipv4_addr, _1, argv[1].c_str(), argv[2].c_str(), &addr, - stoi(argv[4].c_str()))); + std::stoi(argv[4].c_str()))); } else { in6_addr addr; if (inet_pton(AF_INET6, argv[3].c_str(), &addr) != 1) { - throw runtime_error("Wrong address format"); + throw std::runtime_error("Wrong address format"); }; CommandLineInterface::executeCallback(bind(vsm_netdev_del_ipv6_addr, _1, argv[1].c_str(), argv[2].c_str(), &addr, - stoi(argv[4].c_str()))); + std::stoi(argv[4].c_str()))); } } @@ -678,7 +676,7 @@ void netdev_up(const Args& argv) using namespace std::placeholders; if (argv.size() < 3) { - throw runtime_error("Not enough parameters"); + throw std::runtime_error("Not enough parameters"); } CommandLineInterface::executeCallback(bind(vsm_netdev_up, _1, @@ -691,7 +689,7 @@ void netdev_down(const Args& argv) using namespace std::placeholders; if (argv.size() < 3) { - throw runtime_error("Not enough parameters"); + throw std::runtime_error("Not enough parameters"); } CommandLineInterface::executeCallback(bind(vsm_netdev_down, _1, diff --git a/client/vasum-client-impl.cpp b/client/vasum-client-impl.cpp index 41cc919..30cc74c 100644 --- a/client/vasum-client-impl.cpp +++ b/client/vasum-client-impl.cpp @@ -49,7 +49,6 @@ #include #include -using namespace std; using namespace utils; using namespace vasum; @@ -103,7 +102,7 @@ void convert(const api::ZoneInfoOut& info, Zone& zone) zone = vsmZone; } -string toString(const in_addr* addr) +std::string toString(const in_addr* addr) { char buf[INET_ADDRSTRLEN]; const char* ret = inet_ntop(AF_INET, addr, buf, INET_ADDRSTRLEN); @@ -113,7 +112,7 @@ string toString(const in_addr* addr) return ret; } -string toString(const in6_addr* addr) +std::string toString(const in6_addr* addr) { char buf[INET6_ADDRSTRLEN]; const char* ret = inet_ntop(AF_INET6, addr, buf, INET6_ADDRSTRLEN); @@ -123,9 +122,9 @@ string toString(const in6_addr* addr) return ret; } -bool readFirstLineOfFile(const string& path, string& ret) +bool readFirstLineOfFile(const std::string& path, std::string& ret) { - ifstream file(path); + std::ifstream file(path); if (!file) { return false; } @@ -146,7 +145,7 @@ Client::Status::Status() { } -Client::Status::Status(VsmStatus status, const string& msg) +Client::Status::Status(VsmStatus status, const std::string& msg) : mVsmStatus(status), mMsg(msg) { } @@ -182,7 +181,7 @@ ipc::epoll::EventPoll& Client::getEventPoll() const } } -VsmStatus Client::coverException(const function& worker) noexcept +VsmStatus Client::coverException(const std::function& worker) noexcept { try { worker(); @@ -202,7 +201,7 @@ VsmStatus Client::coverException(const function& worker) noexcept mStatus = Status(VSMCLIENT_CUSTOM_ERROR, ex.what()); } catch (const ipc::IPCException& ex) { mStatus = Status(VSMCLIENT_IO_ERROR, ex.what()); - } catch (const exception& ex) { + } catch (const std::exception& ex) { mStatus = Status(VSMCLIENT_CUSTOM_ERROR, ex.what()); } VsmStatus ret = mStatus.mVsmStatus; @@ -295,7 +294,7 @@ const char* Client::vsm_get_status_message() const noexcept VsmStatus Client::vsm_get_status() const noexcept { - lock_guard lock(mStatusMutex); + std::lock_guard lock(mStatusMutex); return mStatus.mVsmStatus; } @@ -354,14 +353,14 @@ VsmStatus Client::vsm_lookup_zone_by_pid(int pid, VsmString* id) noexcept return coverException([&] { IS_SET(id); - const string path = "/proc/" + to_string(pid) + "/cpuset"; + const std::string path = "/proc/" + std::to_string(pid) + "/cpuset"; - string cpuset; + std::string cpuset; if (!readFirstLineOfFile(path, cpuset)) { throw InvalidArgumentException("Process not found"); } - string zoneId; + std::string zoneId; if (!parseZoneIdFromCpuSet(cpuset, zoneId)) { throw OperationFailedException("unknown format of cpuset"); } @@ -407,7 +406,7 @@ VsmStatus Client::vsm_create_zone(const char* id, const char* tname) noexcept return coverException([&] { IS_SET(id); - string template_name = tname ? tname : "default"; + std::string template_name = tname ? tname : "default"; mClient->callSync( api::ipc::METHOD_CREATE_ZONE, std::make_shared(api::CreateZoneIn{ id, template_name }), @@ -552,7 +551,7 @@ VsmStatus Client::vsm_netdev_get_ip_addr(const char* id, std::vector addrAttrs; for(const auto& addrAttr : split(addrAttrs, attr.second, is_any_of(","))) { size_t pos = addrAttr.find(":"); - if (pos == string::npos) continue; + if (pos == std::string::npos) continue; if (addrAttr.substr(0, pos) == "prefixlen") { addr.prefix = atoi(addrAttr.substr(pos + 1).c_str()); @@ -610,7 +609,7 @@ VsmStatus Client::vsm_netdev_add_ipv4_addr(const char* id, IS_SET(netdevId); IS_SET(addr); - string value = "ip:" + toString(addr) + ",""prefixlen:" + to_string(prefix); + std::string value = "ip:" + toString(addr) + ",""prefixlen:" + std::to_string(prefix); mClient->callSync( api::ipc::METHOD_SET_NETDEV_ATTRS, std::make_shared( @@ -628,7 +627,7 @@ VsmStatus Client::vsm_netdev_add_ipv6_addr(const char* id, IS_SET(netdevId); IS_SET(addr); - string value = "ip:" + toString(addr) + ",""prefixlen:" + to_string(prefix); + std::string value = "ip:" + toString(addr) + ",""prefixlen:" + std::to_string(prefix); mClient->callSync( api::ipc::METHOD_SET_NETDEV_ATTRS, std::make_shared( @@ -647,7 +646,7 @@ VsmStatus Client::vsm_netdev_del_ipv4_addr(const char* id, IS_SET(addr); //CIDR notation - string ip = toString(addr) + "/" + to_string(prefix); + std::string ip = toString(addr) + "/" + std::to_string(prefix); mClient->callSync( api::ipc::METHOD_DELETE_NETDEV_IP_ADDRESS, std::make_shared( @@ -666,7 +665,7 @@ VsmStatus Client::vsm_netdev_del_ipv6_addr(const char* id, IS_SET(addr); //CIDR notation - string ip = toString(addr) + "/" + to_string(prefix); + std::string ip = toString(addr) + "/" + std::to_string(prefix); mClient->callSync( api::ipc::METHOD_DELETE_NETDEV_IP_ADDRESS, std::make_shared( @@ -684,8 +683,8 @@ VsmStatus Client::vsm_netdev_up(const char* id, const char* netdevId) noexcept mClient->callSync( api::ipc::METHOD_SET_NETDEV_ATTRS, std::make_shared( - api::SetNetDevAttrsIn{ id, netdevId, { { "flags", to_string(IFF_UP) }, - { "change", to_string(IFF_UP) } } })); + api::SetNetDevAttrsIn{ id, netdevId, { { "flags", std::to_string(IFF_UP) }, + { "change", std::to_string(IFF_UP) } } })); }); } @@ -698,8 +697,8 @@ VsmStatus Client::vsm_netdev_down(const char* id, const char* netdevId) noexcept mClient->callSync( api::ipc::METHOD_SET_NETDEV_ATTRS, std::make_shared( - api::SetNetDevAttrsIn{ id, netdevId, { { "flags", to_string(~IFF_UP) }, - { "change", to_string(IFF_UP) } } })); + api::SetNetDevAttrsIn{ id, netdevId, { { "flags", std::to_string(~IFF_UP) }, + { "change", std::to_string(IFF_UP) } } })); }); } diff --git a/client/vasum-client-impl.hpp b/client/vasum-client-impl.hpp index dbc9001..7278e68 100644 --- a/client/vasum-client-impl.hpp +++ b/client/vasum-client-impl.hpp @@ -81,7 +81,7 @@ typedef struct { * * Client uses dbus API. */ -class Client { +class Client final { public: Client() noexcept; ~Client() noexcept; diff --git a/client/vasum-client.cpp b/client/vasum-client.cpp index 9cf15b3..ccc21d5 100644 --- a/client/vasum-client.cpp +++ b/client/vasum-client.cpp @@ -33,8 +33,6 @@ #define API __attribute__((visibility("default"))) #endif // API -using namespace std; - namespace { Client& getClient(VsmClient client) @@ -77,7 +75,7 @@ API VsmStatus vsm_get_dispatcher_type(VsmClient client, VsmDispacherType* dispac API VsmClient vsm_client_create() { - Client* clientPtr = new(nothrow) Client(); + Client* clientPtr = new(std::nothrow) Client(); return reinterpret_cast(clientPtr); } diff --git a/server/netdev.cpp b/server/netdev.cpp index 8387c84..9565ed3 100644 --- a/server/netdev.cpp +++ b/server/netdev.cpp @@ -63,18 +63,18 @@ #define BRIDGE_FLAGS_MASTER 1 #endif -using namespace std; using namespace utils; using namespace vasum::netlink; +using std::get; namespace vasum { namespace netdev { namespace { -string getUniqueVethName() +std::string getUniqueVethName() { - auto find = [](const ifaddrs* ifaddr, const string& name) -> bool { + auto find = [](const ifaddrs* ifaddr, const std::string& name) -> bool { for (const ifaddrs* ifa = ifaddr; ifa != NULL; ifa = ifa->ifa_next) { if (name == ifa->ifa_name) { return true; @@ -85,17 +85,17 @@ string getUniqueVethName() ifaddrs* ifaddr; getifaddrs(&ifaddr); - string newName; + std::string newName; int i = 0; do { - newName = "veth0" + to_string(++i); + newName = "veth0" + std::to_string(++i); } while (find(ifaddr, newName)); freeifaddrs(ifaddr); return newName; } -uint32_t getInterfaceIndex(const string& name) { +uint32_t getInterfaceIndex(const std::string& name) { uint32_t index = if_nametoindex(name.c_str()); if (!index) { const std::string msg = "Can't get " + name + " interface index (" + getSystemErrorMessage() + ")"; @@ -105,7 +105,7 @@ uint32_t getInterfaceIndex(const string& name) { return index; } -uint32_t getInterfaceIndex(const string& name, pid_t nsPid) { +uint32_t getInterfaceIndex(const std::string& name, pid_t nsPid) { NetlinkMessage nlm(RTM_GETLINK, NLM_F_REQUEST | NLM_F_ACK); ifinfomsg infoPeer = utils::make_clean(); infoPeer.ifi_family = AF_UNSPEC; @@ -126,14 +126,14 @@ int getIpFamily(const std::string& ip) return ip.find(':') == std::string::npos ? AF_INET : AF_INET6; } -void validateNetdevName(const string& name) +void validateNetdevName(const std::string& name) { if (name.size() <= 1 || name.size() >= IFNAMSIZ) { throw ZoneOperationException("Invalid netdev name format"); } } -void createPipedNetdev(const string& netdev1, const string& netdev2) +void createPipedNetdev(const std::string& netdev1, const std::string& netdev2) { validateNetdevName(netdev1); validateNetdevName(netdev2); @@ -156,7 +156,7 @@ void createPipedNetdev(const string& netdev1, const string& netdev2) send(nlm); } -void attachToBridge(const string& bridge, const string& netdev) +void attachToBridge(const std::string& bridge, const std::string& netdev) { validateNetdevName(bridge); validateNetdevName(netdev); @@ -184,7 +184,7 @@ void attachToBridge(const string& bridge, const string& netdev) close(fd); } -int setFlags(const string& name, uint32_t mask, uint32_t flags) +int setFlags(const std::string& name, uint32_t mask, uint32_t flags) { uint32_t index = getInterfaceIndex(name); NetlinkMessage nlm(RTM_NEWLINK, NLM_F_REQUEST | NLM_F_ACK); @@ -199,12 +199,12 @@ int setFlags(const string& name, uint32_t mask, uint32_t flags) return 0; } -void up(const string& netdev) +void up(const std::string& netdev) { setFlags(netdev, IFF_UP, IFF_UP); } -void moveToNS(const string& netdev, pid_t pid) +void moveToNS(const std::string& netdev, pid_t pid) { uint32_t index = getInterfaceIndex(netdev); NetlinkMessage nlm(RTM_NEWLINK, NLM_F_REQUEST | NLM_F_ACK); @@ -216,7 +216,7 @@ void moveToNS(const string& netdev, pid_t pid) send(nlm); } -void createMacvlan(const string& master, const string& slave, const macvlan_mode& mode) +void createMacvlan(const std::string& master, const std::string& slave, const macvlan_mode& mode) { validateNetdevName(master); validateNetdevName(slave); @@ -283,7 +283,7 @@ std::vector getIpAddresses(const pid_t nsPid, int family, uint32_t index) LOGE(msg); throw VasumException(msg); } - attrs.push_back(make_tuple("ip", buf)); + attrs.push_back(std::make_tuple("ip", buf)); break; default: response.skipAttribute(); @@ -372,19 +372,19 @@ void deleteIpAddress(const pid_t nsPid, } // namespace -void createVeth(const pid_t& nsPid, const string& nsDev, const string& hostDev) +void createVeth(const pid_t& nsPid, const std::string& nsDev, const std::string& hostDev) { - string hostVeth = getUniqueVethName(); + std::string hostVeth = getUniqueVethName(); LOGT("Creating veth: bridge: " << hostDev << ", port: " << hostVeth << ", zone: " << nsDev); createPipedNetdev(nsDev, hostVeth); try { attachToBridge(hostDev, hostVeth); up(hostVeth); moveToNS(nsDev, nsPid); - } catch(const exception& ex) { + } catch(const std::exception& ex) { try { destroyNetdev(hostVeth); - } catch (const exception& ex) { + } catch (const std::exception& ex) { LOGE("Can't destroy netdev pipe: " << hostVeth << ", " << nsDev); } throw; @@ -392,8 +392,8 @@ void createVeth(const pid_t& nsPid, const string& nsDev, const string& hostDev) } void createMacvlan(const pid_t& nsPid, - const string& nsDev, - const string& hostDev, + const std::string& nsDev, + const std::string& hostDev, const macvlan_mode& mode) { LOGT("Creating macvlan: host: " << hostDev << ", zone: " << nsDev << ", mode: " << mode); @@ -401,17 +401,17 @@ void createMacvlan(const pid_t& nsPid, try { up(nsDev); moveToNS(nsDev, nsPid); - } catch(const exception& ex) { + } catch(const std::exception& ex) { try { destroyNetdev(nsDev); - } catch (const exception& ex) { + } catch (const std::exception& ex) { LOGE("Can't destroy netdev: " << nsDev); } throw; } } -void movePhys(const pid_t& nsPid, const string& devId) +void movePhys(const pid_t& nsPid, const std::string& devId) { LOGT("Creating phys: dev: " << devId); moveToNS(devId, nsPid); @@ -437,7 +437,7 @@ std::vector listNetdev(const pid_t& nsPid) return interfaces; } -void destroyNetdev(const string& netdev, const pid_t pid) +void destroyNetdev(const std::string& netdev, const pid_t pid) { LOGT("Destroying netdev: " << netdev); validateNetdevName(netdev); @@ -451,7 +451,7 @@ void destroyNetdev(const string& netdev, const pid_t pid) send(nlm, pid); } -void createBridge(const string& netdev) +void createBridge(const std::string& netdev) { LOGT("Creating bridge: " << netdev); validateNetdevName(netdev); @@ -477,7 +477,7 @@ Attrs getAttrs(const pid_t nsPid, const std::string& netdev) { auto joinAddresses = [](const Attrs& attrs) -> std::string { bool first = true; - stringstream ss; + std::stringstream ss; for (const auto& attr : attrs) { ss << (first ? "" : ",") << get<0>(attr) << ":" << get<1>(attr); first = false; @@ -539,7 +539,7 @@ Attrs getAttrs(const pid_t nsPid, const std::string& netdev) void setAttrs(const pid_t nsPid, const std::string& netdev, const Attrs& attrs) { - const set supportedAttrs{"flags", "change", "type", "mtu", "link", "ipv4", "ipv6"}; + const std::set supportedAttrs{"flags", "change", "type", "mtu", "link", "ipv4", "ipv6"}; LOGT("Setting network device informations: " << netdev); validateNetdevName(netdev); @@ -581,8 +581,8 @@ void setAttrs(const pid_t nsPid, const std::string& netdev, const Attrs& attrs) } //TODO: Multiple addresses should be set at once (add support NLM_F_MULTI to NetlinkMessage). - vector ipv4; - vector ipv6; + std::vector ipv4; + std::vector ipv6; for (const auto& attr : attrs) { if (get<0>(attr) == "ipv4") { ipv4.push_back(get<1>(attr)); @@ -592,14 +592,14 @@ void setAttrs(const pid_t nsPid, const std::string& netdev, const Attrs& attrs) } } - auto setIp = [nsPid](const vector& ips, uint32_t index, int family) -> void { + auto setIp = [nsPid](const std::vector& ips, uint32_t index, int family) -> void { using namespace boost::algorithm; for (const auto& ip : ips) { Attrs attrs; - vector params; + std::vector params; for (const auto& addrAttr : split(params, ip, is_any_of(","))) { size_t pos = addrAttr.find(":"); - if (pos == string::npos || pos == addrAttr.length()) { + if (pos == std::string::npos || pos == addrAttr.length()) { const std::string msg = "Wrong input data format: ill formed address attribute: " + addrAttr; LOGE(msg); throw VasumException(msg); @@ -620,7 +620,7 @@ void deleteIpAddress(const pid_t nsPid, { uint32_t index = getInterfaceIndex(netdev, nsPid); size_t slash = ip.find('/'); - if (slash == string::npos) { + if (slash == std::string::npos) { const std::string msg = "Wrong address format: it is not CIDR notation: can't find '/'"; LOGE(msg); throw VasumException(msg); @@ -639,4 +639,3 @@ void deleteIpAddress(const pid_t nsPid, } //namespace netdev } //namespace vasum - -- 2.7.4