From f3388e7105a1dd2dfcc11dbdac370f64c697a674 Mon Sep 17 00:00:00 2001 From: Shinobu Kinjo Date: Thu, 28 Dec 2017 11:51:57 +0900 Subject: [PATCH] pick_address.cc: Cleanup Signed-off-by: Shinobu Kinjo --- src/common/pick_address.cc | 64 ++++++++++++++++++++------------------ 1 file changed, 33 insertions(+), 31 deletions(-) diff --git a/src/common/pick_address.cc b/src/common/pick_address.cc index 4bda7ba1bff..a775bb2f66e 100644 --- a/src/common/pick_address.cc +++ b/src/common/pick_address.cc @@ -34,7 +34,7 @@ const struct sockaddr *find_ip_in_subnet_list( get_str_list(interfaces, ifs); // filter interfaces by name - const struct ifaddrs *filtered = 0; + const struct ifaddrs *filtered = nullptr; if (ifs.empty()) { filtered = ifa; } else { @@ -44,7 +44,7 @@ const struct sockaddr *find_ip_in_subnet_list( } const struct ifaddrs *t = ifa; struct ifaddrs *head = 0; - while (t != NULL) { + while (t) { bool match = false; for (auto& i : ifs) { if (strcmp(i.c_str(), t->ifa_name) == 0) { @@ -60,20 +60,20 @@ const struct sockaddr *find_ip_in_subnet_list( } t = t->ifa_next; } - if (head == NULL) { + if (!head) { lderr(cct) << "no interfaces matching " << ifs << dendl; exit(1); } filtered = head; } - struct sockaddr *r = NULL; - for (std::list::iterator s = nets.begin(); s != nets.end(); ++s) { + struct sockaddr *r = nullptr; + for (auto& s : nets) { struct sockaddr_storage net; unsigned int prefix_len; - if (!parse_network(s->c_str(), &net, &prefix_len)) { - lderr(cct) << "unable to parse network: " << *s << dendl; + if (!parse_network(s.c_str(), &net, &prefix_len)) { + lderr(cct) << "unable to parse network: " << s << dendl; exit(1); } @@ -137,7 +137,7 @@ static void fill_in_one_address(CephContext *cct, : sizeof(struct sockaddr_in6), buf, sizeof(buf), - NULL, 0, + nullptr, 0, NI_NUMERICHOST); if (err != 0) { lderr(cct) << "unable to convert chosen address to string: " << gai_strerror(err) << dendl; @@ -149,7 +149,7 @@ static void fill_in_one_address(CephContext *cct, cct->_conf->add_observer(&obs); cct->_conf->set_val_or_die(conf_var, buf); - cct->_conf->apply_changes(NULL); + cct->_conf->apply_changes(nullptr); cct->_conf->remove_observer(&obs); } @@ -158,35 +158,37 @@ void pick_addresses(CephContext *cct, int needs) { struct ifaddrs *ifa; int r = getifaddrs(&ifa); - if (r<0) { + auto public_addr = cct->_conf->get_val("public_addr"); + auto public_network = cct->_conf->get_val("public_network"); + auto public_network_interface = + cct->_conf->get_val("public_network_interface"); + auto cluster_addr = cct->_conf->get_val("cluster_addr"); + auto cluster_network = cct->_conf->get_val("cluster_network"); + auto cluster_network_interface = + cct->_conf->get_val("cluster_network_interface"); + + if (r < 0) { string err = cpp_strerror(errno); lderr(cct) << "unable to fetch interfaces and addresses: " << err << dendl; exit(1); } - if ((needs & CEPH_PICK_ADDRESS_PUBLIC) - && cct->_conf->public_addr.is_blank_ip() - && !cct->_conf->public_network.empty()) { - fill_in_one_address(cct, ifa, cct->_conf->public_network, - cct->_conf->get_val("public_network_interface"), - "public_addr"); + if ((needs & CEPH_PICK_ADDRESS_PUBLIC) && + public_addr.is_blank_ip() && !public_network.empty()) { + fill_in_one_address(cct, ifa, public_network, public_network_interface, + "public_addr"); } - if ((needs & CEPH_PICK_ADDRESS_CLUSTER) - && cct->_conf->cluster_addr.is_blank_ip()) { - if (!cct->_conf->cluster_network.empty()) { - fill_in_one_address( - cct, ifa, cct->_conf->cluster_network, - cct->_conf->get_val("cluster_network_interface"), + if ((needs & CEPH_PICK_ADDRESS_CLUSTER) && cluster_addr.is_blank_ip()) { + if (!cluster_network.empty()) { + fill_in_one_address(cct, ifa, cluster_network, cluster_network_interface, "cluster_addr"); } else { - if (!cct->_conf->public_network.empty()) { + if (!public_network.empty()) { lderr(cct) << "Public network was set, but cluster network was not set " << dendl; lderr(cct) << " Using public network also for cluster network" << dendl; - fill_in_one_address( - cct, ifa, cct->_conf->public_network, - cct->_conf->get_val("public_network_interface"), - "cluster_addr"); + fill_in_one_address(cct, ifa, public_network, public_network_interface, + "cluster_addr"); } } } @@ -230,13 +232,13 @@ bool have_local_addr(CephContext *cct, const list& ls, entity_add } bool found = false; - for (struct ifaddrs *addrs = ifa; addrs != NULL; addrs = addrs->ifa_next) { + for (struct ifaddrs *addrs = ifa; addrs != nullptr; addrs = addrs->ifa_next) { if (addrs->ifa_addr) { entity_addr_t a; a.set_sockaddr(addrs->ifa_addr); - for (list::const_iterator p = ls.begin(); p != ls.end(); ++p) { - if (a.is_same_host(*p)) { - *match = *p; + for (auto& p : ls) { + if (a.is_same_host(p)) { + *match = p; found = true; goto out; }