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"
27 * this class is used to facilitate the testing of
30 class ResolvHWrapper
{
32 virtual ~ResolvHWrapper() {}
34 #ifdef HAVE_RES_NQUERY
35 virtual int res_nquery(res_state s
, const char *hostname
, int cls
, int type
,
36 u_char
*buf
, int bufsz
);
38 virtual int res_nsearch(res_state s
, const char *hostname
, int cls
, int type
,
39 u_char
*buf
, int bufsz
);
41 virtual int res_query(const char *hostname
, int cls
, int type
,
42 u_char
*buf
, int bufsz
);
44 virtual int res_search(const char *hostname
, int cls
, int type
,
45 u_char
*buf
, int bufsz
);
54 * This is a singleton class that exposes the functionality of DNS querying.
59 // singleton declaration
60 static DNSResolver
*get_instance()
62 static DNSResolver instance
;
65 DNSResolver(DNSResolver
const&) = delete;
66 void operator=(DNSResolver
const&) = delete;
68 // this function is used by the unit test
69 static DNSResolver
*get_instance(ResolvHWrapper
*resolv_wrapper
) {
70 DNSResolver
*resolv
= DNSResolver::get_instance();
71 delete resolv
->resolv_h
;
72 resolv
->resolv_h
= resolv_wrapper
;
76 enum class SRV_Protocol
{
81 int resolve_cname(CephContext
*cct
, const std::string
& hostname
,
82 std::string
*cname
, bool *found
);
85 * Resolves the address given a hostname.
87 * @param hostname the hostname to resolved
88 * @param[out] addr the hostname's address
89 * @returns 0 on success, negative error code on failure
91 int resolve_ip_addr(CephContext
*cct
, const std::string
& hostname
,
95 * Returns the list of hostnames and addresses that provide a given
96 * service configured as DNS SRV records.
98 * @param service_name the service name
99 * @param trans_protocol the IP protocol used by the service (TCP or UDP)
100 * @param[out] srv_hosts the hostname to address map of available hosts
101 * providing the service. If no host exists the map is not
103 * @returns 0 on success, negative error code on failure
105 int resolve_srv_hosts(CephContext
*cct
, const std::string
& service_name
,
106 const SRV_Protocol trans_protocol
, std::map
<std::string
, entity_addr_t
> *srv_hosts
);
109 * Returns the list of hostnames and addresses that provide a given
110 * service configured as DNS SRV records.
112 * @param service_name the service name
113 * @param trans_protocol the IP protocol used by the service (TCP or UDP)
114 * @param domain the domain of the service
115 * @param[out] srv_hosts the hostname to address map of available hosts
116 * providing the service. If no host exists the map is not
118 * @returns 0 on success, negative error code on failure
120 int resolve_srv_hosts(CephContext
*cct
, const std::string
& service_name
,
121 const SRV_Protocol trans_protocol
, const std::string
& domain
,
122 std::map
<std::string
, entity_addr_t
> *srv_hosts
);
125 DNSResolver() : lock("DNSResolver") { resolv_h
= new ResolvHWrapper(); }
129 ResolvHWrapper
*resolv_h
;
130 #ifdef HAVE_RES_NQUERY
131 std::list
<res_state
> states
;
133 int get_state(CephContext
*cct
, res_state
*ps
);
134 void put_state(res_state s
);
137 /* this private function allows to reuse the res_state structure used
138 * by other function of this class
140 int resolve_ip_addr(CephContext
*cct
, res_state
*res
,
141 const std::string
& hostname
, entity_addr_t
*addr
);
143 std::string
srv_protocol_to_str(SRV_Protocol proto
) {
145 case SRV_Protocol::TCP
:
147 case SRV_Protocol::UDP
: