• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //
2 // ip/basic_resolver_query.hpp
3 // ~~~~~~~~~~~~~~~~~~~~~~~~~~~
4 //
5 // Copyright (c) 2003-2015 Christopher M. Kohlhoff (chris at kohlhoff dot com)
6 //
7 // Distributed under the Boost Software License, Version 1.0. (See accompanying
8 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
9 //
10 
11 #ifndef ASIO_IP_BASIC_RESOLVER_QUERY_HPP
12 #define ASIO_IP_BASIC_RESOLVER_QUERY_HPP
13 
14 
15 #include "asio/detail/config.hpp"
16 #include <string>
17 #include "asio/detail/socket_ops.hpp"
18 #include "asio/ip/resolver_query_base.hpp"
19 
20 #include "asio/detail/push_options.hpp"
21 
22 namespace asio {
23 namespace ip {
24 
25 /// An query to be passed to a resolver.
26 /**
27  * The asio::ip::basic_resolver_query class template describes a query
28  * that can be passed to a resolver.
29  *
30  * @par Thread Safety
31  * @e Distinct @e objects: Safe.@n
32  * @e Shared @e objects: Unsafe.
33  */
34 template <typename InternetProtocol>
35 class basic_resolver_query
36   : public resolver_query_base
37 {
38 public:
39   /// The protocol type associated with the endpoint query.
40   typedef InternetProtocol protocol_type;
41 
42   /// Construct with specified service name for any protocol.
43   /**
44    * This constructor is typically used to perform name resolution for local
45    * service binding.
46    *
47    * @param service A string identifying the requested service. This may be a
48    * descriptive name or a numeric string corresponding to a port number.
49    *
50    * @param resolve_flags A set of flags that determine how name resolution
51    * should be performed. The default flags are suitable for local service
52    * binding.
53    *
54    * @note On POSIX systems, service names are typically defined in the file
55    * <tt>/etc/services</tt>. On Windows, service names may be found in the file
56    * <tt>c:\\windows\\system32\\drivers\\etc\\services</tt>. Operating systems
57    * may use additional locations when resolving service names.
58    */
basic_resolver_query(const std::string & service,resolver_query_base::flags resolve_flags=passive|address_configured)59   basic_resolver_query(const std::string& service,
60       resolver_query_base::flags resolve_flags = passive | address_configured)
61     : hints_(),
62       host_name_(),
63       service_name_(service)
64   {
65     typename InternetProtocol::endpoint endpoint;
66     hints_.ai_flags = static_cast<int>(resolve_flags);
67     hints_.ai_family = PF_UNSPEC;
68     hints_.ai_socktype = endpoint.protocol().type();
69     hints_.ai_protocol = endpoint.protocol().protocol();
70     hints_.ai_addrlen = 0;
71     hints_.ai_canonname = 0;
72     hints_.ai_addr = 0;
73     hints_.ai_next = 0;
74   }
75 
76   /// Construct with specified service name for a given protocol.
77   /**
78    * This constructor is typically used to perform name resolution for local
79    * service binding with a specific protocol version.
80    *
81    * @param protocol A protocol object, normally representing either the IPv4 or
82    * IPv6 version of an internet protocol.
83    *
84    * @param service A string identifying the requested service. This may be a
85    * descriptive name or a numeric string corresponding to a port number.
86    *
87    * @param resolve_flags A set of flags that determine how name resolution
88    * should be performed. The default flags are suitable for local service
89    * binding.
90    *
91    * @note On POSIX systems, service names are typically defined in the file
92    * <tt>/etc/services</tt>. On Windows, service names may be found in the file
93    * <tt>c:\\windows\\system32\\drivers\\etc\\services</tt>. Operating systems
94    * may use additional locations when resolving service names.
95    */
basic_resolver_query(const protocol_type & protocol,const std::string & service,resolver_query_base::flags resolve_flags=passive|address_configured)96   basic_resolver_query(const protocol_type& protocol,
97       const std::string& service,
98       resolver_query_base::flags resolve_flags = passive | address_configured)
99     : hints_(),
100       host_name_(),
101       service_name_(service)
102   {
103     hints_.ai_flags = static_cast<int>(resolve_flags);
104     hints_.ai_family = protocol.family();
105     hints_.ai_socktype = protocol.type();
106     hints_.ai_protocol = protocol.protocol();
107     hints_.ai_addrlen = 0;
108     hints_.ai_canonname = 0;
109     hints_.ai_addr = 0;
110     hints_.ai_next = 0;
111   }
112 
113   /// Construct with specified host name and service name for any protocol.
114   /**
115    * This constructor is typically used to perform name resolution for
116    * communication with remote hosts.
117    *
118    * @param host A string identifying a location. May be a descriptive name or
119    * a numeric address string. If an empty string and the passive flag has been
120    * specified, the resolved endpoints are suitable for local service binding.
121    * If an empty string and passive is not specified, the resolved endpoints
122    * will use the loopback address.
123    *
124    * @param service A string identifying the requested service. This may be a
125    * descriptive name or a numeric string corresponding to a port number. May
126    * be an empty string, in which case all resolved endpoints will have a port
127    * number of 0.
128    *
129    * @param resolve_flags A set of flags that determine how name resolution
130    * should be performed. The default flags are suitable for communication with
131    * remote hosts.
132    *
133    * @note On POSIX systems, host names may be locally defined in the file
134    * <tt>/etc/hosts</tt>. On Windows, host names may be defined in the file
135    * <tt>c:\\windows\\system32\\drivers\\etc\\hosts</tt>. Remote host name
136    * resolution is performed using DNS. Operating systems may use additional
137    * locations when resolving host names (such as NETBIOS names on Windows).
138    *
139    * On POSIX systems, service names are typically defined in the file
140    * <tt>/etc/services</tt>. On Windows, service names may be found in the file
141    * <tt>c:\\windows\\system32\\drivers\\etc\\services</tt>. Operating systems
142    * may use additional locations when resolving service names.
143    */
basic_resolver_query(const std::string & host,const std::string & service,resolver_query_base::flags resolve_flags=address_configured)144   basic_resolver_query(const std::string& host, const std::string& service,
145       resolver_query_base::flags resolve_flags = address_configured)
146     : hints_(),
147       host_name_(host),
148       service_name_(service)
149   {
150     typename InternetProtocol::endpoint endpoint;
151     hints_.ai_flags = static_cast<int>(resolve_flags);
152     hints_.ai_family = ASIO_OS_DEF(AF_UNSPEC);
153     hints_.ai_socktype = endpoint.protocol().type();
154     hints_.ai_protocol = endpoint.protocol().protocol();
155     hints_.ai_addrlen = 0;
156     hints_.ai_canonname = 0;
157     hints_.ai_addr = 0;
158     hints_.ai_next = 0;
159   }
160 
161   /// Construct with specified host name and service name for a given protocol.
162   /**
163    * This constructor is typically used to perform name resolution for
164    * communication with remote hosts.
165    *
166    * @param protocol A protocol object, normally representing either the IPv4 or
167    * IPv6 version of an internet protocol.
168    *
169    * @param host A string identifying a location. May be a descriptive name or
170    * a numeric address string. If an empty string and the passive flag has been
171    * specified, the resolved endpoints are suitable for local service binding.
172    * If an empty string and passive is not specified, the resolved endpoints
173    * will use the loopback address.
174    *
175    * @param service A string identifying the requested service. This may be a
176    * descriptive name or a numeric string corresponding to a port number. May
177    * be an empty string, in which case all resolved endpoints will have a port
178    * number of 0.
179    *
180    * @param resolve_flags A set of flags that determine how name resolution
181    * should be performed. The default flags are suitable for communication with
182    * remote hosts.
183    *
184    * @note On POSIX systems, host names may be locally defined in the file
185    * <tt>/etc/hosts</tt>. On Windows, host names may be defined in the file
186    * <tt>c:\\windows\\system32\\drivers\\etc\\hosts</tt>. Remote host name
187    * resolution is performed using DNS. Operating systems may use additional
188    * locations when resolving host names (such as NETBIOS names on Windows).
189    *
190    * On POSIX systems, service names are typically defined in the file
191    * <tt>/etc/services</tt>. On Windows, service names may be found in the file
192    * <tt>c:\\windows\\system32\\drivers\\etc\\services</tt>. Operating systems
193    * may use additional locations when resolving service names.
194    */
basic_resolver_query(const protocol_type & protocol,const std::string & host,const std::string & service,resolver_query_base::flags resolve_flags=address_configured)195   basic_resolver_query(const protocol_type& protocol,
196       const std::string& host, const std::string& service,
197       resolver_query_base::flags resolve_flags = address_configured)
198     : hints_(),
199       host_name_(host),
200       service_name_(service)
201   {
202     hints_.ai_flags = static_cast<int>(resolve_flags);
203     hints_.ai_family = protocol.family();
204     hints_.ai_socktype = protocol.type();
205     hints_.ai_protocol = protocol.protocol();
206     hints_.ai_addrlen = 0;
207     hints_.ai_canonname = 0;
208     hints_.ai_addr = 0;
209     hints_.ai_next = 0;
210   }
211 
212   /// Get the hints associated with the query.
hints() const213   const asio::detail::addrinfo_type& hints() const
214   {
215     return hints_;
216   }
217 
218   /// Get the host name associated with the query.
host_name() const219   std::string host_name() const
220   {
221     return host_name_;
222   }
223 
224   /// Get the service name associated with the query.
service_name() const225   std::string service_name() const
226   {
227     return service_name_;
228   }
229 
230 private:
231   asio::detail::addrinfo_type hints_;
232   std::string host_name_;
233   std::string service_name_;
234 };
235 
236 } // namespace ip
237 } // namespace asio
238 
239 #include "asio/detail/pop_options.hpp"
240 
241 #endif // ASIO_IP_BASIC_RESOLVER_QUERY_HPP
242