1  
//
1  
//
2  
// Copyright (c) 2025 Vinnie Falco (vinnie.falco@gmail.com)
2  
// Copyright (c) 2025 Vinnie Falco (vinnie.falco@gmail.com)
3  
//
3  
//
4  
// Distributed under the Boost Software License, Version 1.0. (See accompanying
4  
// Distributed under the Boost Software License, Version 1.0. (See accompanying
5  
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
5  
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
6  
//
6  
//
7  
// Official repository: https://github.com/cppalliance/corosio
7  
// Official repository: https://github.com/cppalliance/corosio
8  
//
8  
//
9  

9  

10  
#ifndef BOOST_COROSIO_RESOLVER_RESULTS_HPP
10  
#ifndef BOOST_COROSIO_RESOLVER_RESULTS_HPP
11  
#define BOOST_COROSIO_RESOLVER_RESULTS_HPP
11  
#define BOOST_COROSIO_RESOLVER_RESULTS_HPP
12  

12  

13  
#include <boost/corosio/detail/config.hpp>
13  
#include <boost/corosio/detail/config.hpp>
14  
#include <boost/corosio/endpoint.hpp>
14  
#include <boost/corosio/endpoint.hpp>
15  

15  

16  
#include <cstddef>
16  
#include <cstddef>
17  
#include <memory>
17  
#include <memory>
18  
#include <string>
18  
#include <string>
19  
#include <string_view>
19  
#include <string_view>
20  
#include <vector>
20  
#include <vector>
21  

21  

22  
namespace boost::corosio {
22  
namespace boost::corosio {
23  

23  

24  
/** A single entry produced by a resolver.
24  
/** A single entry produced by a resolver.
25  

25  

26  
    This class represents one resolved endpoint along with
26  
    This class represents one resolved endpoint along with
27  
    the host and service names used in the query.
27  
    the host and service names used in the query.
28  

28  

29  
    @par Thread Safety
29  
    @par Thread Safety
30  
    Distinct objects: Safe.@n
30  
    Distinct objects: Safe.@n
31  
    Shared objects: Safe.
31  
    Shared objects: Safe.
32  
*/
32  
*/
33  
class resolver_entry
33  
class resolver_entry
34  
{
34  
{
35  
    endpoint ep_;
35  
    endpoint ep_;
36  
    std::string host_name_;
36  
    std::string host_name_;
37  
    std::string service_name_;
37  
    std::string service_name_;
38  

38  

39  
public:
39  
public:
40  
    /** Default constructor. */
40  
    /** Default constructor. */
41  
    resolver_entry() = default;
41  
    resolver_entry() = default;
42  

42  

43  
    /** Construct with endpoint, host name, and service name.
43  
    /** Construct with endpoint, host name, and service name.
44  

44  

45  
        @param ep The resolved endpoint.
45  
        @param ep The resolved endpoint.
46  
        @param host The host name from the query.
46  
        @param host The host name from the query.
47  
        @param service The service name from the query.
47  
        @param service The service name from the query.
48  
    */
48  
    */
49 -
    resolver_entry(
49 +
    resolver_entry(endpoint ep, std::string_view host, std::string_view service)
50 -
        endpoint ep,
 
51 -
        std::string_view host,
 
52 -
        std::string_view service)
 
53  
        : ep_(ep)
50  
        : ep_(ep)
54  
        , host_name_(host)
51  
        , host_name_(host)
55  
        , service_name_(service)
52  
        , service_name_(service)
56  
    {
53  
    {
57  
    }
54  
    }
58  

55  

59  
    /** Get the endpoint. */
56  
    /** Get the endpoint. */
60 -
    endpoint
57 +
    endpoint get_endpoint() const noexcept
61 -
    get_endpoint() const noexcept
 
62  
    {
58  
    {
63  
        return ep_;
59  
        return ep_;
64  
    }
60  
    }
65  

61  

66  
    /** Implicit conversion to endpoint. */
62  
    /** Implicit conversion to endpoint. */
67  
    operator endpoint() const noexcept
63  
    operator endpoint() const noexcept
68  
    {
64  
    {
69  
        return ep_;
65  
        return ep_;
70  
    }
66  
    }
71  

67  

72  
    /** Get the host name from the query. */
68  
    /** Get the host name from the query. */
73 -
    std::string const&
69 +
    std::string const& host_name() const noexcept
74 -
    host_name() const noexcept
 
75  
    {
70  
    {
76  
        return host_name_;
71  
        return host_name_;
77  
    }
72  
    }
78  

73  

79  
    /** Get the service name from the query. */
74  
    /** Get the service name from the query. */
80 -
    std::string const&
75 +
    std::string const& service_name() const noexcept
81 -
    service_name() const noexcept
 
82  
    {
76  
    {
83  
        return service_name_;
77  
        return service_name_;
84  
    }
78  
    }
85  
};
79  
};
86 -
//------------------------------------------------------------------------------
 
87  

80  

88  

81  

89  
/** A range of entries produced by a resolver.
82  
/** A range of entries produced by a resolver.
90  

83  

91  
    This class holds the results of a DNS resolution query.
84  
    This class holds the results of a DNS resolution query.
92  
    It provides a range interface for iterating over the
85  
    It provides a range interface for iterating over the
93  
    resolved endpoints.
86  
    resolved endpoints.
94  

87  

95  
    @par Thread Safety
88  
    @par Thread Safety
96  
    Distinct objects: Safe.@n
89  
    Distinct objects: Safe.@n
97  
    Shared objects: Safe (immutable after construction).
90  
    Shared objects: Safe (immutable after construction).
98  
*/
91  
*/
99  
class resolver_results
92  
class resolver_results
100  
{
93  
{
101  
public:
94  
public:
102  
    using value_type = resolver_entry;
95  
    using value_type = resolver_entry;
103  
    using const_reference = value_type const&;
96  
    using const_reference = value_type const&;
104  
    using reference = const_reference;
97  
    using reference = const_reference;
105  
    using const_iterator = std::vector<resolver_entry>::const_iterator;
98  
    using const_iterator = std::vector<resolver_entry>::const_iterator;
106  
    using iterator = const_iterator;
99  
    using iterator = const_iterator;
107  
    using difference_type = std::ptrdiff_t;
100  
    using difference_type = std::ptrdiff_t;
108  
    using size_type = std::size_t;
101  
    using size_type = std::size_t;
109  

102  

110  
private:
103  
private:
111  
    std::shared_ptr<std::vector<resolver_entry>> entries_;
104  
    std::shared_ptr<std::vector<resolver_entry>> entries_;
112  

105  

113  
public:
106  
public:
114  
    /** Default constructor creates an empty range. */
107  
    /** Default constructor creates an empty range. */
115  
    resolver_results() = default;
108  
    resolver_results() = default;
116  

109  

117  
    /** Construct from a vector of entries.
110  
    /** Construct from a vector of entries.
118  

111  

119  
        @param entries The resolved entries.
112  
        @param entries The resolved entries.
120  
    */
113  
    */
121 -
    explicit
114 +
    explicit resolver_results(std::vector<resolver_entry> entries)
122 -
    resolver_results(std::vector<resolver_entry> entries)
115 +
        : entries_(
123 -
        : entries_(std::make_shared<std::vector<resolver_entry>>(
116 +
              std::make_shared<std::vector<resolver_entry>>(std::move(entries)))
124 -
            std::move(entries)))
 
125  
    {
117  
    {
126  
    }
118  
    }
127  

119  

128  
    /** Get the number of entries. */
120  
    /** Get the number of entries. */
129 -
    size_type
121 +
    size_type size() const noexcept
130 -
    size() const noexcept
 
131  
    {
122  
    {
132  
        return entries_ ? entries_->size() : 0;
123  
        return entries_ ? entries_->size() : 0;
133  
    }
124  
    }
134  

125  

135  
    /** Check if the results are empty. */
126  
    /** Check if the results are empty. */
136 -
    bool
127 +
    bool empty() const noexcept
137 -
    empty() const noexcept
 
138  
    {
128  
    {
139  
        return !entries_ || entries_->empty();
129  
        return !entries_ || entries_->empty();
140  
    }
130  
    }
141  

131  

142  
    /** Get an iterator to the first entry. */
132  
    /** Get an iterator to the first entry. */
143 -
    const_iterator
133 +
    const_iterator begin() const noexcept
144 -
    begin() const noexcept
 
145  
    {
134  
    {
146  
        if (entries_)
135  
        if (entries_)
147  
            return entries_->begin();
136  
            return entries_->begin();
148  
        return std::vector<resolver_entry>::const_iterator();
137  
        return std::vector<resolver_entry>::const_iterator();
149  
    }
138  
    }
150  

139  

151  
    /** Get an iterator past the last entry. */
140  
    /** Get an iterator past the last entry. */
152 -
    const_iterator
141 +
    const_iterator end() const noexcept
153 -
    end() const noexcept
 
154  
    {
142  
    {
155  
        if (entries_)
143  
        if (entries_)
156  
            return entries_->end();
144  
            return entries_->end();
157  
        return std::vector<resolver_entry>::const_iterator();
145  
        return std::vector<resolver_entry>::const_iterator();
158  
    }
146  
    }
159  

147  

160  
    /** Get an iterator to the first entry. */
148  
    /** Get an iterator to the first entry. */
161 -
    const_iterator
149 +
    const_iterator cbegin() const noexcept
162 -
    cbegin() const noexcept
 
163  
    {
150  
    {
164  
        return begin();
151  
        return begin();
165  
    }
152  
    }
166  

153  

167  
    /** Get an iterator past the last entry. */
154  
    /** Get an iterator past the last entry. */
168 -
    const_iterator
155 +
    const_iterator cend() const noexcept
169 -
    cend() const noexcept
 
170  
    {
156  
    {
171  
        return end();
157  
        return end();
172  
    }
158  
    }
173  

159  

174  
    /** Swap with another results object. */
160  
    /** Swap with another results object. */
175 -
    void
161 +
    void swap(resolver_results& other) noexcept
176 -
    swap(resolver_results& other) noexcept
 
177  
    {
162  
    {
178  
        entries_.swap(other.entries_);
163  
        entries_.swap(other.entries_);
179  
    }
164  
    }
180  

165  

181  
    /** Test for equality. */
166  
    /** Test for equality. */
182 -
    friend
167 +
    friend bool
183 -
    bool
168 +
    operator==(resolver_results const& a, resolver_results const& b) noexcept
184 -
    operator==(
 
185 -
        resolver_results const& a,
 
186 -
        resolver_results const& b) noexcept
 
187  
    {
169  
    {
188  
        return a.entries_ == b.entries_;
170  
        return a.entries_ == b.entries_;
189  
    }
171  
    }
190  

172  

191  
    /** Test for inequality. */
173  
    /** Test for inequality. */
192 -
    friend
174 +
    friend bool
193 -
    bool
175 +
    operator!=(resolver_results const& a, resolver_results const& b) noexcept
194 -
    operator!=(
 
195 -
        resolver_results const& a,
 
196 -
        resolver_results const& b) noexcept
 
197  
    {
176  
    {
198  
        return !(a == b);
177  
        return !(a == b);
199  
    }
178  
    }
200  
};
179  
};
201 -
//------------------------------------------------------------------------------
 
202  

180  

203  

181  

204  
/** The result of a reverse DNS resolution.
182  
/** The result of a reverse DNS resolution.
205  

183  

206  
    This class holds the result of resolving an endpoint
184  
    This class holds the result of resolving an endpoint
207  
    into a hostname and service name.
185  
    into a hostname and service name.
208  

186  

209  
    @par Thread Safety
187  
    @par Thread Safety
210  
    Distinct objects: Safe.@n
188  
    Distinct objects: Safe.@n
211  
    Shared objects: Safe.
189  
    Shared objects: Safe.
212  
*/
190  
*/
213  
class reverse_resolver_result
191  
class reverse_resolver_result
214  
{
192  
{
215  
    corosio::endpoint ep_;
193  
    corosio::endpoint ep_;
216  
    std::string host_;
194  
    std::string host_;
217  
    std::string service_;
195  
    std::string service_;
218  

196  

219  
public:
197  
public:
220  
    /** Default constructor. */
198  
    /** Default constructor. */
221  
    reverse_resolver_result() = default;
199  
    reverse_resolver_result() = default;
222  

200  

223  
    /** Construct with endpoint, host name, and service name.
201  
    /** Construct with endpoint, host name, and service name.
224  

202  

225  
        @param ep The endpoint that was resolved.
203  
        @param ep The endpoint that was resolved.
226  
        @param host The resolved host name.
204  
        @param host The resolved host name.
227  
        @param service The resolved service name.
205  
        @param service The resolved service name.
228  
    */
206  
    */
229  
    reverse_resolver_result(
207  
    reverse_resolver_result(
230 -
        corosio::endpoint ep,
208 +
        corosio::endpoint ep, std::string host, std::string service)
231 -
        std::string host,
 
232 -
        std::string service)
 
233  
        : ep_(ep)
209  
        : ep_(ep)
234  
        , host_(std::move(host))
210  
        , host_(std::move(host))
235  
        , service_(std::move(service))
211  
        , service_(std::move(service))
236  
    {
212  
    {
237  
    }
213  
    }
238  

214  

239  
    /** Get the endpoint that was resolved. */
215  
    /** Get the endpoint that was resolved. */
240 -
    corosio::endpoint
216 +
    corosio::endpoint endpoint() const noexcept
241 -
    endpoint() const noexcept
 
242  
    {
217  
    {
243  
        return ep_;
218  
        return ep_;
244  
    }
219  
    }
245  

220  

246  
    /** Get the resolved host name. */
221  
    /** Get the resolved host name. */
247 -
    std::string const&
222 +
    std::string const& host_name() const noexcept
248 -
    host_name() const noexcept
 
249  
    {
223  
    {
250  
        return host_;
224  
        return host_;
251  
    }
225  
    }
252  

226  

253  
    /** Get the resolved service name. */
227  
    /** Get the resolved service name. */
254 -
    std::string const&
228 +
    std::string const& service_name() const noexcept
255 -
    service_name() const noexcept
 
256  
    {
229  
    {
257  
        return service_;
230  
        return service_;
258  
    }
231  
    }
259  
};
232  
};
260  

233  

261  
} // namespace boost::corosio
234  
} // namespace boost::corosio
262  

235  

263  
#endif
236  
#endif