]> git.proxmox.com Git - ceph.git/blame - ceph/src/common/pick_address.cc
update sources to v12.1.0
[ceph.git] / ceph / src / common / pick_address.cc
CommitLineData
7c673cae
FG
1// -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
2// vim: ts=8 sw=2 smarttab
3/*
4 * Ceph - scalable distributed file system
5 *
6 * Copyright (C) 2004-2012 Inktank
7 *
8 * This is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License version 2.1, as published by the Free Software
11 * Foundation. See file COPYING.
12 *
13 */
14
15#include "common/pick_address.h"
7c673cae
FG
16#include "include/ipaddr.h"
17#include "include/str_list.h"
18#include "common/debug.h"
19#include "common/errno.h"
20
31f18b77
FG
21#include <netdb.h>
22
7c673cae
FG
23#define dout_subsys ceph_subsys_
24
25static const struct sockaddr *find_ip_in_subnet_list(CephContext *cct,
26 const struct ifaddrs *ifa,
27 const std::string &networks)
28{
29 std::list<string> nets;
30 get_str_list(networks, nets);
31
32 for(std::list<string>::iterator s = nets.begin(); s != nets.end(); ++s) {
33 struct sockaddr_storage net;
34 unsigned int prefix_len;
35
36 if (!parse_network(s->c_str(), &net, &prefix_len)) {
37 lderr(cct) << "unable to parse network: " << *s << dendl;
38 exit(1);
39 }
40
41 const struct sockaddr *found = find_ip_in_subnet(ifa, (struct sockaddr *) &net, prefix_len);
42 if (found)
43 return found;
44 }
45
46 return NULL;
47}
48
49// observe this change
50struct Observer : public md_config_obs_t {
51 const char *keys[2];
52 explicit Observer(const char *c) {
53 keys[0] = c;
54 keys[1] = NULL;
55 }
56
57 const char** get_tracked_conf_keys() const override {
58 return (const char **)keys;
59 }
60 void handle_conf_change(const struct md_config_t *conf,
61 const std::set <std::string> &changed) override {
62 // do nothing.
63 }
64};
65
66static void fill_in_one_address(CephContext *cct,
67 const struct ifaddrs *ifa,
68 const string networks,
69 const char *conf_var)
70{
71 const struct sockaddr *found = find_ip_in_subnet_list(cct, ifa, networks);
72 if (!found) {
73 lderr(cct) << "unable to find any IP address in networks: " << networks << dendl;
74 exit(1);
75 }
76
77 char buf[INET6_ADDRSTRLEN];
78 int err;
79
80 err = getnameinfo(found,
81 (found->sa_family == AF_INET)
82 ? sizeof(struct sockaddr_in)
83 : sizeof(struct sockaddr_in6),
84
85 buf, sizeof(buf),
86 NULL, 0,
87 NI_NUMERICHOST);
88 if (err != 0) {
89 lderr(cct) << "unable to convert chosen address to string: " << gai_strerror(err) << dendl;
90 exit(1);
91 }
92
93 Observer obs(conf_var);
94
95 cct->_conf->add_observer(&obs);
96
97 cct->_conf->set_val_or_die(conf_var, buf);
98 cct->_conf->apply_changes(NULL);
99
100 cct->_conf->remove_observer(&obs);
101}
102
103void pick_addresses(CephContext *cct, int needs)
104{
105 struct ifaddrs *ifa;
106 int r = getifaddrs(&ifa);
107 if (r<0) {
108 string err = cpp_strerror(errno);
109 lderr(cct) << "unable to fetch interfaces and addresses: " << err << dendl;
110 exit(1);
111 }
112
113
114 if ((needs & CEPH_PICK_ADDRESS_PUBLIC)
115 && cct->_conf->public_addr.is_blank_ip()
116 && !cct->_conf->public_network.empty()) {
117 fill_in_one_address(cct, ifa, cct->_conf->public_network, "public_addr");
118 }
119
120 if ((needs & CEPH_PICK_ADDRESS_CLUSTER)
121 && cct->_conf->cluster_addr.is_blank_ip()) {
122 if (!cct->_conf->cluster_network.empty()) {
123 fill_in_one_address(cct, ifa, cct->_conf->cluster_network, "cluster_addr");
124 } else {
125 if (!cct->_conf->public_network.empty()) {
126 lderr(cct) << "Public network was set, but cluster network was not set " << dendl;
127 lderr(cct) << " Using public network also for cluster network" << dendl;
128 fill_in_one_address(cct, ifa, cct->_conf->public_network, "cluster_addr");
129 }
130 }
131 }
132
133 freeifaddrs(ifa);
134}
135
136bool have_local_addr(CephContext *cct, const list<entity_addr_t>& ls, entity_addr_t *match)
137{
138 struct ifaddrs *ifa;
139 int r = getifaddrs(&ifa);
140 if (r < 0) {
141 lderr(cct) << "unable to fetch interfaces and addresses: " << cpp_strerror(errno) << dendl;
142 exit(1);
143 }
144
145 bool found = false;
146 for (struct ifaddrs *addrs = ifa; addrs != NULL; addrs = addrs->ifa_next) {
147 if (addrs->ifa_addr) {
148 entity_addr_t a;
149 a.set_sockaddr(addrs->ifa_addr);
150 for (list<entity_addr_t>::const_iterator p = ls.begin(); p != ls.end(); ++p) {
151 if (a.is_same_host(*p)) {
152 *match = *p;
153 found = true;
154 goto out;
155 }
156 }
157 }
158 }
159
160 out:
161 freeifaddrs(ifa);
162 return found;
163}