]> git.proxmox.com Git - ceph.git/blob - ceph/src/common/dns_resolve.h
3e8f7e46302189804f5f9443815dc37ebf08c89b
[ceph.git] / ceph / src / common / dns_resolve.h
1 // -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
2 // vim: ts=8 sw=2 smarttab
3 /*
4 * Ceph - scalable distributed file system
5 *
6 * Copyright (C) 2016 SUSE LINUX GmbH
7 *
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.
12 *
13 */
14 #ifndef CEPH_DNS_RESOLVE_H
15 #define CEPH_DNS_RESOLVE_H
16
17 #include <netinet/in.h>
18 #include <resolv.h>
19
20 #include "common/Mutex.h"
21
22 struct entity_addr_t;
23
24 namespace ceph {
25
26 /**
27 * this class is used to facilitate the testing of
28 * resolv.h functions.
29 */
30 class ResolvHWrapper {
31 public:
32 virtual ~ResolvHWrapper() {}
33
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);
37
38 virtual int res_nsearch(res_state s, const char *hostname, int cls, int type,
39 u_char *buf, int bufsz);
40 #else
41 virtual int res_query(const char *hostname, int cls, int type,
42 u_char *buf, int bufsz);
43
44 virtual int res_search(const char *hostname, int cls, int type,
45 u_char *buf, int bufsz);
46 #endif
47
48 };
49
50
51 /**
52 * @class DNSResolver
53 *
54 * This is a singleton class that exposes the functionality of DNS querying.
55 */
56 class DNSResolver {
57
58 public:
59 // singleton declaration
60 static DNSResolver *get_instance()
61 {
62 static DNSResolver instance;
63 return &instance;
64 }
65 DNSResolver(DNSResolver const&) = delete;
66 void operator=(DNSResolver const&) = delete;
67
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;
73 return resolv;
74 }
75
76 enum class SRV_Protocol {
77 TCP, UDP
78 };
79
80
81 int resolve_cname(CephContext *cct, const std::string& hostname,
82 std::string *cname, bool *found);
83
84 /**
85 * Resolves the address given a hostname.
86 *
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
90 */
91 int resolve_ip_addr(CephContext *cct, const std::string& hostname,
92 entity_addr_t *addr);
93
94 /**
95 * Returns the list of hostnames and addresses that provide a given
96 * service configured as DNS SRV records.
97 *
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
102 * changed.
103 * @returns 0 on success, negative error code on failure
104 */
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);
107
108 /**
109 * Returns the list of hostnames and addresses that provide a given
110 * service configured as DNS SRV records.
111 *
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
117 * changed.
118 * @returns 0 on success, negative error code on failure
119 */
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);
123
124 private:
125 DNSResolver() : lock("DNSResolver") { resolv_h = new ResolvHWrapper(); }
126 ~DNSResolver();
127
128 Mutex lock;
129 ResolvHWrapper *resolv_h;
130 #ifdef HAVE_RES_NQUERY
131 std::list<res_state> states;
132
133 int get_state(CephContext *cct, res_state *ps);
134 void put_state(res_state s);
135 #endif
136
137 /* this private function allows to reuse the res_state structure used
138 * by other function of this class
139 */
140 int resolve_ip_addr(CephContext *cct, res_state *res,
141 const std::string& hostname, entity_addr_t *addr);
142
143 std::string srv_protocol_to_str(SRV_Protocol proto) {
144 switch (proto) {
145 case SRV_Protocol::TCP:
146 return "tcp";
147 case SRV_Protocol::UDP:
148 return "udp";
149 }
150 return "";
151 }
152
153 };
154
155 }
156
157 #endif
158