Mbed OS Reference
Loading...
Searching...
No Matches
nsapi_dns.h
1/*
2 * Original work Copyright (c) 2013 Henry Leinen (henry[dot]leinen [at] online [dot] de)
3 * Modified work Copyright (c) 2015 ARM Limited
4 * SPDX-License-Identifier: Apache-2.0
5 *
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 */
18
19/** \addtogroup NetSocket */
20/** @{*/
21
22#ifndef NSAPI_DNS_H
23#define NSAPI_DNS_H
24
25#include "nsapi_types.h"
26#ifdef __cplusplus
28#endif
29
30#ifndef __cplusplus
31
32
33/** Query a domain name server for an IP address of a given hostname
34 *
35 * @param stack Network stack as target for DNS query
36 * @param host Hostname to resolve
37 * @param addr Destination for the host address
38 * @param version IP version to resolve
39 * @return 0 on success, negative error code on failure
40 * NSAPI_ERROR_DNS_FAILURE indicates the host could not be found
41 */
42nsapi_error_t nsapi_dns_query(nsapi_stack_t *stack, const char *host,
43 nsapi_addr_t *addr, nsapi_version_t version);
44
45/** Query a domain name server for multiple IP address of a given hostname
46 *
47 * @param stack Network stack as target for DNS query
48 * @param host Hostname to resolve
49 * @param addr Array for the host addresses
50 * @param addr_count Number of addresses allocated in the array
51 * @param version IP version to resolve
52 * @return Number of addresses found on success, negative error code on failure
53 * NSAPI_ERROR_DNS_FAILURE indicates the host could not be found
54 */
56 nsapi_addr_t *addr, nsapi_size_t addr_count, nsapi_version_t version);
57
58/** Add a domain name server to list of servers to query
59 *
60 * @param addr Destination for the host address
61 * @param interface_name Currently unused, the server is added for all interfaces.
62 * @return 0 on success, negative error code on failure
63 */
64nsapi_error_t nsapi_dns_add_server(nsapi_addr_t addr, const char *interface_name);
65
66
67#else
68
69/** Query a domain name server for an IP address of a given hostname
70 *
71 * @param stack Network stack as target for DNS query
72 * @param host Hostname to resolve
73 * @param addr Destination for the host address
74 * @param version IP version to resolve (defaults to NSAPI_IPv4)
75 * @return 0 on success, negative error code on failure
76 * NSAPI_ERROR_DNS_FAILURE indicates the host could not be found
77 */
79 SocketAddress *addr, nsapi_version_t version = NSAPI_IPv4);
80
81/** Query a domain name server for an IP address of a given hostname using Network interface name
82 *
83 * @param stack Network stack as target for DNS query
84 * @param host Hostname to resolve
85 * @param addr Destination for the host address
86 * @param interface_name If not NULL, only this interface is used to make the DNS query.
87 * @param version IP version to resolve (defaults to NSAPI_IPv4)
88 * @return See @ref nsapi_dns_query_multiple
89 */
91 SocketAddress *addr, const char *interface_name, nsapi_version_t version = NSAPI_IPv4);
92
93
94/** Query a domain name server for an IP address of a given hostname
95 *
96 * @param stack Network stack as target for DNS query
97 * @param host Hostname to resolve
98 * @param callback Callback that is called for result
99 * @param version IP version to resolve (defaults to NSAPI_IPv4)
100 * @param call_in_cb Call-in callback from the network stack (see #NetworkStack::call_in_callback_cb_t)
101 * @return 0 on success, negative error code on failure or an unique id that
102 * represents the hostname translation operation and can be passed to
103 * cancel, NSAPI_ERROR_DNS_FAILURE indicates the host could not be found
104 */
108 nsapi_version_t version = NSAPI_IPv4);
109
110/** Query a domain name server for an IP address of a given hostname using Network interface name
111 *
112 * @param stack Network stack as target for DNS query
113 * @param host Hostname to resolve
114 * @param callback Callback that is called for result
115 * @param call_in_cb Call-in callback from the network stack (see #NetworkStack::call_in_callback_cb_t)
116 * @param interface_name If not NULL, only this interface is used to make the DNS query.
117 * @param version IP version to resolve (defaults to NSAPI_IPv4)
118 * @return 0 on success, negative error code on failure or an unique id that
119 * represents the hostname translation operation and can be passed to
120 * cancel, NSAPI_ERROR_DNS_FAILURE indicates the host could not be found
121 */
125 const char *interface_name, nsapi_version_t version = NSAPI_IPv4);
126
127/** Query a domain name server for an IP address of a given hostname (asynchronous)
128 *
129 * @param stack Network stack as target for DNS query
130 * @param host Hostname to resolve
131 * @param addr Destination for the host address
132 * @param version IP version to resolve (defaults to NSAPI_IPv4)
133 * @return 0 on success, negative error code on failure
134 * NSAPI_ERROR_DNS_FAILURE indicates the host could not be found
135 */
136extern "C" nsapi_error_t nsapi_dns_query(nsapi_stack_t *stack, const char *host,
137 nsapi_addr_t *addr, nsapi_version_t version = NSAPI_IPv4);
138
139/** Query a domain name server for an IP address of a given hostname
140 *
141 * @param stack Network stack as target for DNS query
142 * @param host Hostname to resolve
143 * @param addr Destination for the host address
144 * @param version IP version to resolve (defaults to NSAPI_IPv4)
145 * @return 0 on success, negative error code on failure
146 * NSAPI_ERROR_DNS_FAILURE indicates the host could not be found
147 */
148template <typename S>
149nsapi_error_t nsapi_dns_query(S *stack, const char *host,
150 SocketAddress *addr, nsapi_version_t version = NSAPI_IPv4)
151{
152 return nsapi_dns_query(nsapi_create_stack(stack), host, addr, version);
153}
154
155/** Query a domain name server for multiple IP address of a given hostname
156 *
157 * @param stack Network stack as target for DNS query
158 * @param host Hostname to resolve
159 * @param addr Array for the host addresses
160 * @param addr_count Number of addresses allocated in the array
161 * @param interface_name If not NULL, only this interface is used to make the DNS query.
162 * @param version IP version to resolve (defaults to NSAPI_IPv4)
163 * @return Positive number of addresses found on success
164 * @retval NSAPI_ERROR_PARAMETER if provided parameters are invalid
165 * @retval NSAPI_ERROR_NO_MEMORY if allocation fails due to lack of memory
166 * @retval NSAPI_ERROR_DNS_FAILURE if DNS resolution fails
167 * @retval NSAPI_ERROR_WOULD_BLOCK in case non-blocking mode is enabled and
168 * DNS cannot be resolved immediately.
169 */
171 SocketAddress *addr, nsapi_size_t addr_count, const char *interface_name, nsapi_version_t version = NSAPI_IPv4);
172
173/** Query a domain name server for an IP address of a given hostname (asynchronous)
174 *
175 * @param stack Network stack as target for DNS query
176 * @param host Hostname to resolve
177 * @param callback Callback that is called for result
178 * @param addr_count Number of addresses allocated in the array
179 * @param call_in_cb Call-in callback from the network stack (see #NetworkStack::call_in_callback_cb_t)
180 * @param version IP version to resolve (defaults to NSAPI_IPv4)
181 * @param interface_name If not NULL, only this interface is used to make the DNS query.
182 * @return 0 on success, negative error code on failure or an unique id that
183 represents the hostname translation operation and can be passed to
184 * cancel, NSAPI_ERROR_DNS_FAILURE indicates the host could not be found
185 */
187 NetworkStack::hostbyname_cb_t callback, nsapi_size_t addr_count,
189 const char *interface_name, nsapi_version_t version = NSAPI_IPv4);
190
191/** Query a domain name server for multiple IP address of a given hostname
192 *
193 * @param stack Network stack as target for DNS query
194 * @param host Hostname to resolve
195 * @param addr Array for the host addresses
196 * @param addr_count Number of addresses allocated in the array
197 * @param interface_name If not NULL, only this interface is used to make the DNS query.
198 * @param version IP version to resolve (defaults to NSAPI_IPv4)
199 * @return Number of addresses found on success, negative error code on failure
200 * NSAPI_ERROR_DNS_FAILURE indicates the host could not be found
201 */
203 nsapi_addr_t *addr, nsapi_size_t addr_count, const char *interface_name, nsapi_version_t version = NSAPI_IPv4);
204
205
206/** Query a domain name server for multiple IP address of a given hostname
207 *
208 * @param stack Network stack as target for DNS query
209 * @param host Hostname to resolve
210 * @param addr Array for the host addresses
211 * @param addr_count Number of addresses allocated in the array
212 * @param version IP version to resolve (defaults to NSAPI_IPv4)
213 * @return Number of addresses found on success, negative error code on failure
214 * NSAPI_ERROR_DNS_FAILURE indicates the host could not be found
215 */
216template <typename S>
218 SocketAddress *addr, nsapi_size_t addr_count, nsapi_version_t version = NSAPI_IPv4)
219{
221 host, addr, addr_count, version);
222}
223
224/** Cancels asynchronous hostname translation
225 *
226 * When translation is cancelled, callback will not be called.
227 *
228 * @param id Unique id of the hostname translation operation
229 * @return 0 on success, negative error code on failure
230 */
232
233/** Set a call in callback
234 *
235 * Can be used to provide an application specific call in callback to
236 * DNS resolver. When callback is set it is used instead of stack
237 * specific call in callbacks.
238 *
239 * @param callback Callback
240 */
242
243/**
244 * @brief nsapi_dns_reset Resets all internal states and frees reserved memory, see NOTE!
245 * Can be used to clean up system resources when there is no need for network connections.
246 * NOTE: Does NOT clear asynchronous ongoing operations!
247 * Currently only cleans up DNS cache (if used)
248 */
250
251/** Add a domain name server to list of servers to query
252 *
253 * @param addr Destination for the host address
254 * @param interface_name Currently unused, the server is added for all interfaces.
255 * @return 0 on success, negative error code on failure
256 */
257extern "C" nsapi_error_t nsapi_dns_add_server(nsapi_addr_t addr, const char *interface_name);
258
259/** Add a domain name server to list of servers to query
260 *
261 * @param addr Destination for the host address
262 * @param interface_name Currently unused, the server is added for all interfaces.
263 * @return 0 on success, negative error code on failure
264 */
265static inline nsapi_error_t nsapi_dns_add_server(const SocketAddress &address, const char *interface_name)
266{
267 return nsapi_dns_add_server(address.get_addr(), interface_name);
268}
269
270/** Add a domain name server to list of servers to query
271 *
272 * @param addr Destination for the host address
273 * @param interface_name Currently unused, the server is added for all interfaces.
274 * @return 0 on success, negative error code on failure
275 */
276static inline nsapi_error_t nsapi_dns_add_server(const char *address, const char *interface_name)
277{
278 return nsapi_dns_add_server(SocketAddress(address), interface_name);
279}
280
281
282#endif
283
284#endif
285
286/** @}*/
NetworkStack class.
NetworkStack class.
Definition: NetworkStack.h:42
SocketAddress class.
Definition: SocketAddress.h:37
nsapi_addr_t get_addr() const
Get the raw IP address.
Callback class based on template specialization.
Definition: Callback.h:53
nsapi_error_t nsapi_dns_add_server(nsapi_addr_t addr, const char *interface_name)
Add a domain name server to list of servers to query.
signed int nsapi_size_or_error_t
Type used to represent either a size or error passed through sockets.
Definition: nsapi_types.h:153
nsapi_error_t nsapi_dns_query(NetworkStack *stack, const char *host, SocketAddress *addr, nsapi_version_t version=NSAPI_IPv4)
Query a domain name server for an IP address of a given hostname.
signed int nsapi_error_t
Type used to represent error codes.
Definition: nsapi_types.h:142
void nsapi_dns_reset()
nsapi_dns_reset Resets all internal states and frees reserved memory, see NOTE! Can be used to clean ...
NetworkStack * nsapi_create_stack(nsapi_stack_t *stack)
Convert a raw nsapi_stack_t object into a C++ NetworkStack object.
nsapi_error_t nsapi_dns_query_async(NetworkStack *stack, const char *host, NetworkStack::hostbyname_cb_t callback, NetworkStack::call_in_callback_cb_t call_in_cb, nsapi_version_t version=NSAPI_IPv4)
Query a domain name server for an IP address of a given hostname.
nsapi_error_t nsapi_dns_query_async_cancel(nsapi_size_or_error_t id)
Cancels asynchronous hostname translation.
void nsapi_dns_call_in_set(NetworkStack::call_in_callback_cb_t callback)
Set a call in callback.
nsapi_size_or_error_t nsapi_dns_query_multiple_async(NetworkStack *stack, const char *host, NetworkStack::hostbyname_cb_t callback, nsapi_size_t addr_count, NetworkStack::call_in_callback_cb_t call_in_cb, const char *interface_name, nsapi_version_t version=NSAPI_IPv4)
Query a domain name server for an IP address of a given hostname (asynchronous)
nsapi_size_or_error_t nsapi_dns_query_multiple(NetworkStack *stack, const char *host, SocketAddress *addr, nsapi_size_t addr_count, const char *interface_name, nsapi_version_t version=NSAPI_IPv4)
Query a domain name server for multiple IP address of a given hostname.
unsigned int nsapi_size_t
Type used to represent the size of data passed through sockets.
Definition: nsapi_types.h:146
@ NSAPI_IPv4
Definition: nsapi_types.h:231
IP address structure for passing IP addresses by value.
Definition: nsapi_types.h:237
nsapi_stack structure
Definition: nsapi_types.h:371