]> git.proxmox.com Git - ceph.git/blob - ceph/src/common/dns_resolve.h
add subtree-ish sources for 12.0.3
[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 <string>
18 #include <sys/types.h>
19 #include <netinet/in.h>
20 #include <resolv.h>
21
22 #include "common/Mutex.h"
23 #include "common/ceph_context.h"
24
25 struct entity_addr_t;
26
27 namespace ceph {
28
29 /**
30 * this class is used to facilitate the testing of
31 * resolv.h functions.
32 */
33 class ResolvHWrapper {
34 public:
35 virtual ~ResolvHWrapper() {}
36
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);
40
41 virtual int res_nsearch(res_state s, const char *hostname, int cls, int type,
42 u_char *buf, int bufsz);
43 #else
44 virtual int res_query(const char *hostname, int cls, int type,
45 u_char *buf, int bufsz);
46
47 virtual int res_search(const char *hostname, int cls, int type,
48 u_char *buf, int bufsz);
49 #endif
50
51 };
52
53
54 /**
55 * @class DNSResolver
56 *
57 * This is a singleton class that exposes the functionality of DNS querying.
58 */
59 class DNSResolver {
60
61 public:
62 // singleton declaration
63 static DNSResolver *get_instance()
64 {
65 static DNSResolver instance;
66 return &instance;
67 }
68 DNSResolver(DNSResolver const&) = delete;
69 void operator=(DNSResolver const&) = delete;
70
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;
76 return resolv;
77 }
78
79 enum class SRV_Protocol {
80 TCP, UDP
81 };
82
83
84 int resolve_cname(CephContext *cct, const std::string& hostname,
85 std::string *cname, bool *found);
86
87 /**
88 * Resolves the address given a hostname.
89 *
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
93 */
94 int resolve_ip_addr(CephContext *cct, const std::string& hostname,
95 entity_addr_t *addr);
96
97 /**
98 * Returns the list of hostnames and addresses that provide a given
99 * service configured as DNS SRV records.
100 *
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
105 * changed.
106 * @returns 0 on success, negative error code on failure
107 */
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);
110
111 /**
112 * Returns the list of hostnames and addresses that provide a given
113 * service configured as DNS SRV records.
114 *
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
120 * changed.
121 * @returns 0 on success, negative error code on failure
122 */
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);
126
127 private:
128 DNSResolver() : lock("DNSResolver") { resolv_h = new ResolvHWrapper(); }
129 ~DNSResolver();
130
131 Mutex lock;
132 ResolvHWrapper *resolv_h;
133 #ifdef HAVE_RES_NQUERY
134 std::list<res_state> states;
135
136 int get_state(CephContext *cct, res_state *ps);
137 void put_state(res_state s);
138 #endif
139
140 /* this private function allows to reuse the res_state structure used
141 * by other function of this class
142 */
143 int resolve_ip_addr(CephContext *cct, res_state *res,
144 const std::string& hostname, entity_addr_t *addr);
145
146 std::string srv_protocol_to_str(SRV_Protocol proto) {
147 switch (proto) {
148 case SRV_Protocol::TCP:
149 return "tcp";
150 case SRV_Protocol::UDP:
151 return "udp";
152 }
153 return "";
154 }
155
156 };
157
158 }
159
160 #endif
161