]>
Commit | Line | Data |
---|---|---|
7c673cae FG |
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 | ||
7c673cae | 17 | #include <netinet/in.h> |
f67539c2 | 18 | #ifndef _WIN32 |
7c673cae | 19 | #include <resolv.h> |
f67539c2 | 20 | #endif |
7c673cae | 21 | |
11fdf7f2 | 22 | #include "common/ceph_mutex.h" |
224ce89b | 23 | #include "msg/msg_types.h" // for entity_addr_t |
7c673cae FG |
24 | |
25 | namespace ceph { | |
26 | ||
27 | /** | |
28 | * this class is used to facilitate the testing of | |
29 | * resolv.h functions. | |
30 | */ | |
31 | class ResolvHWrapper { | |
32 | public: | |
33 | virtual ~ResolvHWrapper() {} | |
34 | ||
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); | |
38 | ||
39 | virtual int res_nsearch(res_state s, const char *hostname, int cls, int type, | |
40 | u_char *buf, int bufsz); | |
41 | #else | |
42 | virtual int res_query(const char *hostname, int cls, int type, | |
43 | u_char *buf, int bufsz); | |
44 | ||
45 | virtual int res_search(const char *hostname, int cls, int type, | |
46 | u_char *buf, int bufsz); | |
47 | #endif | |
48 | ||
49 | }; | |
50 | ||
51 | ||
52 | /** | |
53 | * @class DNSResolver | |
54 | * | |
55 | * This is a singleton class that exposes the functionality of DNS querying. | |
56 | */ | |
57 | class DNSResolver { | |
58 | ||
59 | public: | |
60 | // singleton declaration | |
61 | static DNSResolver *get_instance() | |
62 | { | |
63 | static DNSResolver instance; | |
64 | return &instance; | |
65 | } | |
66 | DNSResolver(DNSResolver const&) = delete; | |
67 | void operator=(DNSResolver const&) = delete; | |
68 | ||
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; | |
74 | return resolv; | |
75 | } | |
76 | ||
77 | enum class SRV_Protocol { | |
78 | TCP, UDP | |
79 | }; | |
80 | ||
81 | ||
224ce89b WB |
82 | struct Record { |
83 | uint16_t priority; | |
9f95a23c | 84 | uint16_t weight; |
224ce89b WB |
85 | entity_addr_t addr; |
86 | }; | |
87 | ||
7c673cae FG |
88 | int resolve_cname(CephContext *cct, const std::string& hostname, |
89 | std::string *cname, bool *found); | |
90 | ||
91 | /** | |
92 | * Resolves the address given a hostname. | |
93 | * | |
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 | |
97 | */ | |
98 | int resolve_ip_addr(CephContext *cct, const std::string& hostname, | |
99 | entity_addr_t *addr); | |
100 | ||
101 | /** | |
102 | * Returns the list of hostnames and addresses that provide a given | |
103 | * service configured as DNS SRV records. | |
104 | * | |
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 | |
109 | * changed. | |
110 | * @returns 0 on success, negative error code on failure | |
111 | */ | |
112 | int resolve_srv_hosts(CephContext *cct, const std::string& service_name, | |
224ce89b | 113 | const SRV_Protocol trans_protocol, std::map<std::string, Record> *srv_hosts); |
7c673cae FG |
114 | |
115 | /** | |
116 | * Returns the list of hostnames and addresses that provide a given | |
117 | * service configured as DNS SRV records. | |
118 | * | |
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 | |
124 | * changed. | |
125 | * @returns 0 on success, negative error code on failure | |
126 | */ | |
127 | int resolve_srv_hosts(CephContext *cct, const std::string& service_name, | |
128 | const SRV_Protocol trans_protocol, const std::string& domain, | |
224ce89b | 129 | std::map<std::string, Record> *srv_hosts); |
7c673cae FG |
130 | |
131 | private: | |
11fdf7f2 | 132 | DNSResolver() { resolv_h = new ResolvHWrapper(); } |
7c673cae FG |
133 | ~DNSResolver(); |
134 | ||
11fdf7f2 | 135 | ceph::mutex lock = ceph::make_mutex("DNSResolver::lock"); |
7c673cae FG |
136 | ResolvHWrapper *resolv_h; |
137 | #ifdef HAVE_RES_NQUERY | |
138 | std::list<res_state> states; | |
139 | ||
140 | int get_state(CephContext *cct, res_state *ps); | |
141 | void put_state(res_state s); | |
142 | #endif | |
143 | ||
f67539c2 | 144 | #ifndef _WIN32 |
7c673cae FG |
145 | /* this private function allows to reuse the res_state structure used |
146 | * by other function of this class | |
147 | */ | |
148 | int resolve_ip_addr(CephContext *cct, res_state *res, | |
149 | const std::string& hostname, entity_addr_t *addr); | |
f67539c2 | 150 | #endif |
7c673cae FG |
151 | |
152 | std::string srv_protocol_to_str(SRV_Protocol proto) { | |
153 | switch (proto) { | |
154 | case SRV_Protocol::TCP: | |
155 | return "tcp"; | |
156 | case SRV_Protocol::UDP: | |
157 | return "udp"; | |
158 | } | |
159 | return ""; | |
160 | } | |
161 | ||
162 | }; | |
163 | ||
164 | } | |
165 | ||
166 | #endif | |
167 |