]>
Commit | Line | Data |
---|---|---|
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" | |
16 | ||
17 | #include <netdb.h> | |
18 | #include <errno.h> | |
19 | ||
20 | #include "include/ipaddr.h" | |
21 | #include "include/str_list.h" | |
22 | #include "common/debug.h" | |
23 | #include "common/errno.h" | |
24 | ||
25 | #define dout_subsys ceph_subsys_ | |
26 | ||
27 | static const struct sockaddr *find_ip_in_subnet_list(CephContext *cct, | |
28 | const struct ifaddrs *ifa, | |
29 | const std::string &networks) | |
30 | { | |
31 | std::list<string> nets; | |
32 | get_str_list(networks, nets); | |
33 | ||
34 | for(std::list<string>::iterator s = nets.begin(); s != nets.end(); ++s) { | |
35 | struct sockaddr_storage net; | |
36 | unsigned int prefix_len; | |
37 | ||
38 | if (!parse_network(s->c_str(), &net, &prefix_len)) { | |
39 | lderr(cct) << "unable to parse network: " << *s << dendl; | |
40 | exit(1); | |
41 | } | |
42 | ||
43 | const struct sockaddr *found = find_ip_in_subnet(ifa, (struct sockaddr *) &net, prefix_len); | |
44 | if (found) | |
45 | return found; | |
46 | } | |
47 | ||
48 | return NULL; | |
49 | } | |
50 | ||
51 | // observe this change | |
52 | struct Observer : public md_config_obs_t { | |
53 | const char *keys[2]; | |
54 | explicit Observer(const char *c) { | |
55 | keys[0] = c; | |
56 | keys[1] = NULL; | |
57 | } | |
58 | ||
59 | const char** get_tracked_conf_keys() const override { | |
60 | return (const char **)keys; | |
61 | } | |
62 | void handle_conf_change(const struct md_config_t *conf, | |
63 | const std::set <std::string> &changed) override { | |
64 | // do nothing. | |
65 | } | |
66 | }; | |
67 | ||
68 | static void fill_in_one_address(CephContext *cct, | |
69 | const struct ifaddrs *ifa, | |
70 | const string networks, | |
71 | const char *conf_var) | |
72 | { | |
73 | const struct sockaddr *found = find_ip_in_subnet_list(cct, ifa, networks); | |
74 | if (!found) { | |
75 | lderr(cct) << "unable to find any IP address in networks: " << networks << dendl; | |
76 | exit(1); | |
77 | } | |
78 | ||
79 | char buf[INET6_ADDRSTRLEN]; | |
80 | int err; | |
81 | ||
82 | err = getnameinfo(found, | |
83 | (found->sa_family == AF_INET) | |
84 | ? sizeof(struct sockaddr_in) | |
85 | : sizeof(struct sockaddr_in6), | |
86 | ||
87 | buf, sizeof(buf), | |
88 | NULL, 0, | |
89 | NI_NUMERICHOST); | |
90 | if (err != 0) { | |
91 | lderr(cct) << "unable to convert chosen address to string: " << gai_strerror(err) << dendl; | |
92 | exit(1); | |
93 | } | |
94 | ||
95 | Observer obs(conf_var); | |
96 | ||
97 | cct->_conf->add_observer(&obs); | |
98 | ||
99 | cct->_conf->set_val_or_die(conf_var, buf); | |
100 | cct->_conf->apply_changes(NULL); | |
101 | ||
102 | cct->_conf->remove_observer(&obs); | |
103 | } | |
104 | ||
105 | void pick_addresses(CephContext *cct, int needs) | |
106 | { | |
107 | struct ifaddrs *ifa; | |
108 | int r = getifaddrs(&ifa); | |
109 | if (r<0) { | |
110 | string err = cpp_strerror(errno); | |
111 | lderr(cct) << "unable to fetch interfaces and addresses: " << err << dendl; | |
112 | exit(1); | |
113 | } | |
114 | ||
115 | ||
116 | if ((needs & CEPH_PICK_ADDRESS_PUBLIC) | |
117 | && cct->_conf->public_addr.is_blank_ip() | |
118 | && !cct->_conf->public_network.empty()) { | |
119 | fill_in_one_address(cct, ifa, cct->_conf->public_network, "public_addr"); | |
120 | } | |
121 | ||
122 | if ((needs & CEPH_PICK_ADDRESS_CLUSTER) | |
123 | && cct->_conf->cluster_addr.is_blank_ip()) { | |
124 | if (!cct->_conf->cluster_network.empty()) { | |
125 | fill_in_one_address(cct, ifa, cct->_conf->cluster_network, "cluster_addr"); | |
126 | } else { | |
127 | if (!cct->_conf->public_network.empty()) { | |
128 | lderr(cct) << "Public network was set, but cluster network was not set " << dendl; | |
129 | lderr(cct) << " Using public network also for cluster network" << dendl; | |
130 | fill_in_one_address(cct, ifa, cct->_conf->public_network, "cluster_addr"); | |
131 | } | |
132 | } | |
133 | } | |
134 | ||
135 | freeifaddrs(ifa); | |
136 | } | |
137 | ||
138 | bool have_local_addr(CephContext *cct, const list<entity_addr_t>& ls, entity_addr_t *match) | |
139 | { | |
140 | struct ifaddrs *ifa; | |
141 | int r = getifaddrs(&ifa); | |
142 | if (r < 0) { | |
143 | lderr(cct) << "unable to fetch interfaces and addresses: " << cpp_strerror(errno) << dendl; | |
144 | exit(1); | |
145 | } | |
146 | ||
147 | bool found = false; | |
148 | for (struct ifaddrs *addrs = ifa; addrs != NULL; addrs = addrs->ifa_next) { | |
149 | if (addrs->ifa_addr) { | |
150 | entity_addr_t a; | |
151 | a.set_sockaddr(addrs->ifa_addr); | |
152 | for (list<entity_addr_t>::const_iterator p = ls.begin(); p != ls.end(); ++p) { | |
153 | if (a.is_same_host(*p)) { | |
154 | *match = *p; | |
155 | found = true; | |
156 | goto out; | |
157 | } | |
158 | } | |
159 | } | |
160 | } | |
161 | ||
162 | out: | |
163 | freeifaddrs(ifa); | |
164 | return found; | |
165 | } |