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) 2016 SUSE LINUX GmbH
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 <arpa/inet.h>
17 #include "include/scope_guard.h"
18 #include "dns_resolve.h"
19 #include "common/debug.h"
21 #define dout_subsys ceph_subsys_
26 #ifdef HAVE_RES_NQUERY
28 int ResolvHWrapper::res_nquery(res_state s
, const char *hostname
, int cls
,
29 int type
, u_char
*buf
, int bufsz
) {
30 return ::res_nquery(s
, hostname
, cls
, type
, buf
, bufsz
);
33 int ResolvHWrapper::res_nsearch(res_state s
, const char *hostname
, int cls
,
34 int type
, u_char
*buf
, int bufsz
) {
35 return ::res_nsearch(s
, hostname
, cls
, type
, buf
, bufsz
);
40 int ResolvHWrapper::res_query(const char *hostname
, int cls
,
41 int type
, u_char
*buf
, int bufsz
) {
42 return ::res_query(hostname
, cls
, type
, buf
, bufsz
);
45 int ResolvHWrapper::res_search(const char *hostname
, int cls
,
46 int type
, u_char
*buf
, int bufsz
) {
47 return ::res_search(hostname
, cls
, type
, buf
, bufsz
);
52 DNSResolver::~DNSResolver()
54 #ifdef HAVE_RES_NQUERY
55 list
<res_state
>::iterator iter
;
56 for (iter
= states
.begin(); iter
!= states
.end(); ++iter
) {
57 struct __res_state
*s
= *iter
;
64 #ifdef HAVE_RES_NQUERY
65 int DNSResolver::get_state(CephContext
*cct
, res_state
*ps
)
68 if (!states
.empty()) {
69 res_state s
= states
.front();
76 struct __res_state
*s
= new struct __res_state
;
78 if (res_ninit(s
) < 0) {
80 lderr(cct
) << "ERROR: failed to call res_ninit()" << dendl
;
87 void DNSResolver::put_state(res_state s
)
89 std::lock_guard
l(lock
);
94 int DNSResolver::resolve_cname(CephContext
*cct
, const string
& hostname
,
95 string
*cname
, bool *found
)
99 #ifdef HAVE_RES_NQUERY
101 int r
= get_state(cct
, &res
);
105 auto put_state
= make_scope_guard([res
, this] {
106 this->put_state(res
);
110 #define LARGE_ENOUGH_DNS_BUFSIZE 1024
111 unsigned char buf
[LARGE_ENOUGH_DNS_BUFSIZE
];
113 #define MAX_FQDN_SIZE 255
114 char host
[MAX_FQDN_SIZE
+ 1];
115 const char *origname
= hostname
.c_str();
116 unsigned char *pt
, *answer
;
117 unsigned char *answend
;
120 #ifdef HAVE_RES_NQUERY
121 len
= resolv_h
->res_nquery(res
, origname
, ns_c_in
, ns_t_cname
, buf
, sizeof(buf
));
124 # ifndef HAVE_THREAD_SAFE_RES_QUERY
125 std::lock_guard
l(lock
);
127 len
= resolv_h
->res_query(origname
, ns_c_in
, ns_t_cname
, buf
, sizeof(buf
));
131 lderr(cct
) << "res_query() failed" << dendl
;
136 pt
= answer
+ NS_HFIXEDSZ
;
137 answend
= answer
+ len
;
140 if ((len
= dn_expand(answer
, answend
, pt
, host
, sizeof(host
))) < 0) {
141 lderr(cct
) << "ERROR: dn_expand() failed" << dendl
;
146 if (pt
+ 4 > answend
) {
147 lderr(cct
) << "ERROR: bad reply" << dendl
;
154 if (type
!= ns_t_cname
) {
155 lderr(cct
) << "ERROR: failed response type: type=" << type
<<
156 " (was expecting " << ns_t_cname
<< ")" << dendl
;
160 pt
+= NS_INT16SZ
; /* class */
163 if ((len
= dn_expand(answer
, answend
, pt
, host
, sizeof(host
))) < 0) {
167 ldout(cct
, 20) << "name=" << host
<< dendl
;
169 if (pt
+ 10 > answend
) {
170 lderr(cct
) << "ERROR: bad reply" << dendl
;
175 pt
+= NS_INT16SZ
; /* class */
176 pt
+= NS_INT32SZ
; /* ttl */
177 pt
+= NS_INT16SZ
; /* size */
179 if ((len
= dn_expand(answer
, answend
, pt
, host
, sizeof(host
))) < 0) {
182 ldout(cct
, 20) << "cname host=" << host
<< dendl
;
190 int DNSResolver::resolve_ip_addr(CephContext
*cct
, const string
& hostname
,
191 entity_addr_t
*addr
) {
193 #ifdef HAVE_RES_NQUERY
195 int r
= get_state(cct
, &res
);
199 auto put_state
= make_scope_guard([res
, this] {
200 this->put_state(res
);
202 return this->resolve_ip_addr(cct
, &res
, hostname
, addr
);
204 return this->resolve_ip_addr(cct
, NULL
, hostname
, addr
);
209 int DNSResolver::resolve_ip_addr(CephContext
*cct
, res_state
*res
, const string
& hostname
,
210 entity_addr_t
*addr
) {
212 u_char nsbuf
[NS_PACKETSZ
];
214 int family
= cct
->_conf
->ms_bind_ipv6
? AF_INET6
: AF_INET
;
215 int type
= cct
->_conf
->ms_bind_ipv6
? ns_t_aaaa
: ns_t_a
;
217 #ifdef HAVE_RES_NQUERY
218 len
= resolv_h
->res_nquery(*res
, hostname
.c_str(), ns_c_in
, type
, nsbuf
, sizeof(nsbuf
));
221 # ifndef HAVE_THREAD_SAFE_RES_QUERY
222 std::lock_guard
l(lock
);
224 len
= resolv_h
->res_query(hostname
.c_str(), ns_c_in
, type
, nsbuf
, sizeof(nsbuf
));
228 lderr(cct
) << "res_query() failed" << dendl
;
232 ldout(cct
, 20) << "no address found for hostname " << hostname
<< dendl
;
237 ns_initparse(nsbuf
, len
, &handle
);
239 if (ns_msg_count(handle
, ns_s_an
) == 0) {
240 ldout(cct
, 20) << "no address found for hostname " << hostname
<< dendl
;
246 if ((r
= ns_parserr(&handle
, ns_s_an
, 0, &rr
)) < 0) {
247 lderr(cct
) << "error while parsing DNS record" << dendl
;
252 memset(addr_buf
, 0, sizeof(addr_buf
));
253 inet_ntop(family
, ns_rr_rdata(rr
), addr_buf
, sizeof(addr_buf
));
254 if (!addr
->parse(addr_buf
)) {
255 lderr(cct
) << "failed to parse address '" << (const char *)ns_rr_rdata(rr
)
263 int DNSResolver::resolve_srv_hosts(CephContext
*cct
, const string
& service_name
,
264 const SRV_Protocol trans_protocol
,
265 map
<string
, DNSResolver::Record
> *srv_hosts
) {
266 return this->resolve_srv_hosts(cct
, service_name
, trans_protocol
, "", srv_hosts
);
269 int DNSResolver::resolve_srv_hosts(CephContext
*cct
, const string
& service_name
,
270 const SRV_Protocol trans_protocol
, const string
& domain
,
271 map
<string
, DNSResolver::Record
> *srv_hosts
) {
273 #ifdef HAVE_RES_NQUERY
275 int r
= get_state(cct
, &res
);
279 auto put_state
= make_scope_guard([res
, this] {
280 this->put_state(res
);
284 u_char nsbuf
[NS_PACKETSZ
];
287 string proto_str
= srv_protocol_to_str(trans_protocol
);
288 string query_str
= "_"+service_name
+"._"+proto_str
+(domain
.empty() ? ""
292 #ifdef HAVE_RES_NQUERY
293 len
= resolv_h
->res_nsearch(res
, query_str
.c_str(), ns_c_in
, ns_t_srv
, nsbuf
,
297 # ifndef HAVE_THREAD_SAFE_RES_QUERY
298 std::lock_guard
l(lock
);
300 len
= resolv_h
->res_search(query_str
.c_str(), ns_c_in
, ns_t_srv
, nsbuf
,
305 lderr(cct
) << "failed for service " << query_str
<< dendl
;
309 ldout(cct
, 20) << "No hosts found for service " << query_str
<< dendl
;
315 ns_initparse(nsbuf
, len
, &handle
);
317 num_hosts
= ns_msg_count (handle
, ns_s_an
);
318 if (num_hosts
== 0) {
319 ldout(cct
, 20) << "No hosts found for service " << query_str
<< dendl
;
324 char full_target
[NS_MAXDNAME
];
326 for (int i
= 0; i
< num_hosts
; i
++) {
328 if ((r
= ns_parserr(&handle
, ns_s_an
, i
, &rr
)) < 0) {
329 lderr(cct
) << "Error while parsing DNS record" << dendl
;
333 string full_srv_name
= ns_rr_name(rr
);
334 string protocol
= "_" + proto_str
;
335 string srv_domain
= full_srv_name
.substr(full_srv_name
.find(protocol
)
336 + protocol
.length());
338 auto rdata
= ns_rr_rdata(rr
);
339 uint16_t priority
= ns_get16(rdata
); rdata
+= NS_INT16SZ
;
340 rdata
+= NS_INT16SZ
; // weight
341 uint16_t port
= ns_get16(rdata
); rdata
+= NS_INT16SZ
;
342 memset(full_target
, 0, sizeof(full_target
));
343 ns_name_uncompress(ns_msg_base(handle
), ns_msg_end(handle
),
344 rdata
, full_target
, sizeof(full_target
));
347 #ifdef HAVE_RES_NQUERY
348 r
= this->resolve_ip_addr(cct
, &res
, full_target
, &addr
);
350 r
= this->resolve_ip_addr(cct
, NULL
, full_target
, &addr
);
355 string target
= full_target
;
356 auto end
= target
.find(srv_domain
);
357 if (end
== target
.npos
) {
358 lderr(cct
) << "resolved target not in search domain: "
359 << target
<< " / " << srv_domain
<< dendl
;
362 target
= target
.substr(0, end
);
363 (*srv_hosts
)[target
] = {priority
, addr
};