1 // -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
2 // vim: ts=8 sw=2 smarttab
4 * Ceph - scalable distributed file system
6 * Copyright (C) 2004-2012 Inktank
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.
15 #include "common/pick_address.h"
16 #include "include/ipaddr.h"
17 #include "include/str_list.h"
18 #include "common/debug.h"
19 #include "common/errno.h"
23 #define dout_subsys ceph_subsys_
25 static const struct sockaddr
*find_ip_in_subnet_list(CephContext
*cct
,
26 const struct ifaddrs
*ifa
,
27 const std::string
&networks
)
29 std::list
<string
> nets
;
30 get_str_list(networks
, nets
);
32 for(std::list
<string
>::iterator s
= nets
.begin(); s
!= nets
.end(); ++s
) {
33 struct sockaddr_storage net
;
34 unsigned int prefix_len
;
36 if (!parse_network(s
->c_str(), &net
, &prefix_len
)) {
37 lderr(cct
) << "unable to parse network: " << *s
<< dendl
;
41 const struct sockaddr
*found
= find_ip_in_subnet(ifa
, (struct sockaddr
*) &net
, prefix_len
);
49 // observe this change
50 struct Observer
: public md_config_obs_t
{
52 explicit Observer(const char *c
) {
57 const char** get_tracked_conf_keys() const override
{
58 return (const char **)keys
;
60 void handle_conf_change(const struct md_config_t
*conf
,
61 const std::set
<std::string
> &changed
) override
{
66 static void fill_in_one_address(CephContext
*cct
,
67 const struct ifaddrs
*ifa
,
68 const string networks
,
71 const struct sockaddr
*found
= find_ip_in_subnet_list(cct
, ifa
, networks
);
73 lderr(cct
) << "unable to find any IP address in networks: " << networks
<< dendl
;
77 char buf
[INET6_ADDRSTRLEN
];
80 err
= getnameinfo(found
,
81 (found
->sa_family
== AF_INET
)
82 ? sizeof(struct sockaddr_in
)
83 : sizeof(struct sockaddr_in6
),
89 lderr(cct
) << "unable to convert chosen address to string: " << gai_strerror(err
) << dendl
;
93 Observer
obs(conf_var
);
95 cct
->_conf
->add_observer(&obs
);
97 cct
->_conf
->set_val_or_die(conf_var
, buf
);
98 cct
->_conf
->apply_changes(NULL
);
100 cct
->_conf
->remove_observer(&obs
);
103 void pick_addresses(CephContext
*cct
, int needs
)
106 int r
= getifaddrs(&ifa
);
108 string err
= cpp_strerror(errno
);
109 lderr(cct
) << "unable to fetch interfaces and addresses: " << err
<< dendl
;
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");
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");
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");
136 bool have_local_addr(CephContext
*cct
, const list
<entity_addr_t
>& ls
, entity_addr_t
*match
)
139 int r
= getifaddrs(&ifa
);
141 lderr(cct
) << "unable to fetch interfaces and addresses: " << cpp_strerror(errno
) << dendl
;
146 for (struct ifaddrs
*addrs
= ifa
; addrs
!= NULL
; addrs
= addrs
->ifa_next
) {
147 if (addrs
->ifa_addr
) {
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
)) {