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
18 #include <sys/types.h>
19 #include <netinet/in.h>
22 #include "common/Mutex.h"
23 #include "common/ceph_context.h"
30 * this class is used to facilitate the testing of
33 class ResolvHWrapper
{
35 virtual ~ResolvHWrapper() {}
37 #ifdef HAVE_RES_NQUERY
38 virtual int res_nquery(res_state s
, const char *hostname
, int cls
, int type
,
39 u_char
*buf
, int bufsz
);
41 virtual int res_nsearch(res_state s
, const char *hostname
, int cls
, int type
,
42 u_char
*buf
, int bufsz
);
44 virtual int res_query(const char *hostname
, int cls
, int type
,
45 u_char
*buf
, int bufsz
);
47 virtual int res_search(const char *hostname
, int cls
, int type
,
48 u_char
*buf
, int bufsz
);
57 * This is a singleton class that exposes the functionality of DNS querying.
62 // singleton declaration
63 static DNSResolver
*get_instance()
65 static DNSResolver instance
;
68 DNSResolver(DNSResolver
const&) = delete;
69 void operator=(DNSResolver
const&) = delete;
71 // this function is used by the unit test
72 static DNSResolver
*get_instance(ResolvHWrapper
*resolv_wrapper
) {
73 DNSResolver
*resolv
= DNSResolver::get_instance();
74 delete resolv
->resolv_h
;
75 resolv
->resolv_h
= resolv_wrapper
;
79 enum class SRV_Protocol
{
84 int resolve_cname(CephContext
*cct
, const std::string
& hostname
,
85 std::string
*cname
, bool *found
);
88 * Resolves the address given a hostname.
90 * @param hostname the hostname to resolved
91 * @param[out] addr the hostname's address
92 * @returns 0 on success, negative error code on failure
94 int resolve_ip_addr(CephContext
*cct
, const std::string
& hostname
,
98 * Returns the list of hostnames and addresses that provide a given
99 * service configured as DNS SRV records.
101 * @param service_name the service name
102 * @param trans_protocol the IP protocol used by the service (TCP or UDP)
103 * @param[out] srv_hosts the hostname to address map of available hosts
104 * providing the service. If no host exists the map is not
106 * @returns 0 on success, negative error code on failure
108 int resolve_srv_hosts(CephContext
*cct
, const std::string
& service_name
,
109 const SRV_Protocol trans_protocol
, std::map
<std::string
, entity_addr_t
> *srv_hosts
);
112 * Returns the list of hostnames and addresses that provide a given
113 * service configured as DNS SRV records.
115 * @param service_name the service name
116 * @param trans_protocol the IP protocol used by the service (TCP or UDP)
117 * @param domain the domain of the service
118 * @param[out] srv_hosts the hostname to address map of available hosts
119 * providing the service. If no host exists the map is not
121 * @returns 0 on success, negative error code on failure
123 int resolve_srv_hosts(CephContext
*cct
, const std::string
& service_name
,
124 const SRV_Protocol trans_protocol
, const std::string
& domain
,
125 std::map
<std::string
, entity_addr_t
> *srv_hosts
);
128 DNSResolver() : lock("DNSResolver") { resolv_h
= new ResolvHWrapper(); }
132 ResolvHWrapper
*resolv_h
;
133 #ifdef HAVE_RES_NQUERY
134 std::list
<res_state
> states
;
136 int get_state(CephContext
*cct
, res_state
*ps
);
137 void put_state(res_state s
);
140 /* this private function allows to reuse the res_state structure used
141 * by other function of this class
143 int resolve_ip_addr(CephContext
*cct
, res_state
*res
,
144 const std::string
& hostname
, entity_addr_t
*addr
);
146 std::string
srv_protocol_to_str(SRV_Protocol proto
) {
148 case SRV_Protocol::TCP
:
150 case SRV_Protocol::UDP
: