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>
22 #include "common/ceph_mutex.h"
23 #include "msg/msg_types.h" // for entity_addr_t
28 * this class is used to facilitate the testing of
31 class ResolvHWrapper
{
33 virtual ~ResolvHWrapper() {}
35 #ifdef HAVE_RES_NQUERY
36 virtual int res_nquery(res_state s
, const char *hostname
, int cls
, int type
,
37 u_char
*buf
, int bufsz
);
39 virtual int res_nsearch(res_state s
, const char *hostname
, int cls
, int type
,
40 u_char
*buf
, int bufsz
);
42 virtual int res_query(const char *hostname
, int cls
, int type
,
43 u_char
*buf
, int bufsz
);
45 virtual int res_search(const char *hostname
, int cls
, int type
,
46 u_char
*buf
, int bufsz
);
55 * This is a singleton class that exposes the functionality of DNS querying.
60 // singleton declaration
61 static DNSResolver
*get_instance()
63 static DNSResolver instance
;
66 DNSResolver(DNSResolver
const&) = delete;
67 void operator=(DNSResolver
const&) = delete;
69 // this function is used by the unit test
70 static DNSResolver
*get_instance(ResolvHWrapper
*resolv_wrapper
) {
71 DNSResolver
*resolv
= DNSResolver::get_instance();
72 delete resolv
->resolv_h
;
73 resolv
->resolv_h
= resolv_wrapper
;
77 enum class SRV_Protocol
{
88 int resolve_cname(CephContext
*cct
, const std::string
& hostname
,
89 std::string
*cname
, bool *found
);
92 * Resolves the address given a hostname.
94 * @param hostname the hostname to resolved
95 * @param[out] addr the hostname's address
96 * @returns 0 on success, negative error code on failure
98 int resolve_ip_addr(CephContext
*cct
, const std::string
& hostname
,
102 * Returns the list of hostnames and addresses that provide a given
103 * service configured as DNS SRV records.
105 * @param service_name the service name
106 * @param trans_protocol the IP protocol used by the service (TCP or UDP)
107 * @param[out] srv_hosts the hostname to address map of available hosts
108 * providing the service. If no host exists the map is not
110 * @returns 0 on success, negative error code on failure
112 int resolve_srv_hosts(CephContext
*cct
, const std::string
& service_name
,
113 const SRV_Protocol trans_protocol
, std::map
<std::string
, Record
> *srv_hosts
);
116 * Returns the list of hostnames and addresses that provide a given
117 * service configured as DNS SRV records.
119 * @param service_name the service name
120 * @param trans_protocol the IP protocol used by the service (TCP or UDP)
121 * @param domain the domain of the service
122 * @param[out] srv_hosts the hostname to address map of available hosts
123 * providing the service. If no host exists the map is not
125 * @returns 0 on success, negative error code on failure
127 int resolve_srv_hosts(CephContext
*cct
, const std::string
& service_name
,
128 const SRV_Protocol trans_protocol
, const std::string
& domain
,
129 std::map
<std::string
, Record
> *srv_hosts
);
132 DNSResolver() { resolv_h
= new ResolvHWrapper(); }
135 ceph::mutex lock
= ceph::make_mutex("DNSResolver::lock");
136 ResolvHWrapper
*resolv_h
;
137 #ifdef HAVE_RES_NQUERY
138 std::list
<res_state
> states
;
140 int get_state(CephContext
*cct
, res_state
*ps
);
141 void put_state(res_state s
);
145 /* this private function allows to reuse the res_state structure used
146 * by other function of this class
148 int resolve_ip_addr(CephContext
*cct
, res_state
*res
,
149 const std::string
& hostname
, entity_addr_t
*addr
);
152 std::string
srv_protocol_to_str(SRV_Protocol proto
) {
154 case SRV_Protocol::TCP
:
156 case SRV_Protocol::UDP
: