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.
14 #ifndef CEPH_DNS_RESOLVE_H
15 #define CEPH_DNS_RESOLVE_H
17 #include <netinet/in.h>
20 #include "common/Mutex.h"
21 #include "msg/msg_types.h" // for entity_addr_t
26 * this class is used to facilitate the testing of
29 class ResolvHWrapper
{
31 virtual ~ResolvHWrapper() {}
33 #ifdef HAVE_RES_NQUERY
34 virtual int res_nquery(res_state s
, const char *hostname
, int cls
, int type
,
35 u_char
*buf
, int bufsz
);
37 virtual int res_nsearch(res_state s
, const char *hostname
, int cls
, int type
,
38 u_char
*buf
, int bufsz
);
40 virtual int res_query(const char *hostname
, int cls
, int type
,
41 u_char
*buf
, int bufsz
);
43 virtual int res_search(const char *hostname
, int cls
, int type
,
44 u_char
*buf
, int bufsz
);
53 * This is a singleton class that exposes the functionality of DNS querying.
58 // singleton declaration
59 static DNSResolver
*get_instance()
61 static DNSResolver instance
;
64 DNSResolver(DNSResolver
const&) = delete;
65 void operator=(DNSResolver
const&) = delete;
67 // this function is used by the unit test
68 static DNSResolver
*get_instance(ResolvHWrapper
*resolv_wrapper
) {
69 DNSResolver
*resolv
= DNSResolver::get_instance();
70 delete resolv
->resolv_h
;
71 resolv
->resolv_h
= resolv_wrapper
;
75 enum class SRV_Protocol
{
85 int resolve_cname(CephContext
*cct
, const std::string
& hostname
,
86 std::string
*cname
, bool *found
);
89 * Resolves the address given a hostname.
91 * @param hostname the hostname to resolved
92 * @param[out] addr the hostname's address
93 * @returns 0 on success, negative error code on failure
95 int resolve_ip_addr(CephContext
*cct
, const std::string
& hostname
,
99 * Returns the list of hostnames and addresses that provide a given
100 * service configured as DNS SRV records.
102 * @param service_name the service name
103 * @param trans_protocol the IP protocol used by the service (TCP or UDP)
104 * @param[out] srv_hosts the hostname to address map of available hosts
105 * providing the service. If no host exists the map is not
107 * @returns 0 on success, negative error code on failure
109 int resolve_srv_hosts(CephContext
*cct
, const std::string
& service_name
,
110 const SRV_Protocol trans_protocol
, std::map
<std::string
, Record
> *srv_hosts
);
113 * Returns the list of hostnames and addresses that provide a given
114 * service configured as DNS SRV records.
116 * @param service_name the service name
117 * @param trans_protocol the IP protocol used by the service (TCP or UDP)
118 * @param domain the domain of the service
119 * @param[out] srv_hosts the hostname to address map of available hosts
120 * providing the service. If no host exists the map is not
122 * @returns 0 on success, negative error code on failure
124 int resolve_srv_hosts(CephContext
*cct
, const std::string
& service_name
,
125 const SRV_Protocol trans_protocol
, const std::string
& domain
,
126 std::map
<std::string
, Record
> *srv_hosts
);
129 DNSResolver() : lock("DNSResolver") { resolv_h
= new ResolvHWrapper(); }
133 ResolvHWrapper
*resolv_h
;
134 #ifdef HAVE_RES_NQUERY
135 std::list
<res_state
> states
;
137 int get_state(CephContext
*cct
, res_state
*ps
);
138 void put_state(res_state s
);
141 /* this private function allows to reuse the res_state structure used
142 * by other function of this class
144 int resolve_ip_addr(CephContext
*cct
, res_state
*res
,
145 const std::string
& hostname
, entity_addr_t
*addr
);
147 std::string
srv_protocol_to_str(SRV_Protocol proto
) {
149 case SRV_Protocol::TCP
:
151 case SRV_Protocol::UDP
: