pick_address.cc: Cleanup

Signed-off-by: Shinobu Kinjo <shinobu@redhat.com>
This commit is contained in:
Shinobu Kinjo 2017-12-28 11:51:57 +09:00
parent 2113ebe02d
commit f3388e7105

View File

@ -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<string>::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<entity_addr_t>("public_addr");
auto public_network = cct->_conf->get_val<std::string>("public_network");
auto public_network_interface =
cct->_conf->get_val<std::string>("public_network_interface");
auto cluster_addr = cct->_conf->get_val<entity_addr_t>("cluster_addr");
auto cluster_network = cct->_conf->get_val<std::string>("cluster_network");
auto cluster_network_interface =
cct->_conf->get_val<std::string>("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<string>("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<string>("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<string>("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<entity_addr_t>& 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<entity_addr_t>::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;
}