Mbed OS Reference
Loading...
Searching...
No Matches
DNS.h
Go to the documentation of this file.
1/*
2 * Copyright (c) 2018 ARM Limited
3 * SPDX-License-Identifier: Apache-2.0
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
18/** @file DNS.h Domain Name Service */
19/** @defgroup NetSocket NetSocket
20 * @ingroup connectivity-public-api
21
22 * @{ */
23
24#ifndef DNS_H
25#define DNS_H
26
27/** Base class for DNS provider */
28class DNS {
29public:
30
31 /** Translate a hostname to an IP address with specific version using network interface name.
32 *
33 * The hostname may be either a domain name or an IP address. If the
34 * hostname is an IP address, no network transactions will be performed.
35 *
36 * If no stack-specific DNS resolution is provided, the hostname
37 * will be resolve using a UDP socket on the stack.
38 *
39 * @param host Hostname to resolve.
40 * @param address Pointer to a SocketAddress to store the result.
41 * @param version IP version of address to resolve, NSAPI_UNSPEC indicates
42 * version is chosen by the stack (defaults to NSAPI_UNSPEC).
43 * @param interface_name Network interface name
44 * @return NSAPI_ERROR_OK on success, negative error code on failure.
45 */
46 virtual nsapi_error_t gethostbyname(const char *host,
47 SocketAddress *address, nsapi_version_t version = NSAPI_UNSPEC, const char *interface_name = NULL) = 0;
48
49 /** Translate a hostname to the multiple IP addresses with specific version using network interface name.
50 *
51 * The hostname may be either a domain name or an IP address. If the
52 * hostname is an IP address, no network transactions will be performed.
53 *
54 * If no stack-specific DNS resolution is provided, the hostname
55 * will be resolve using a UDP socket on the stack.
56 *
57 * @param hostname Hostname to resolve.
58 * @param hints Pointer to a SocketAddress with query parameters.
59 * @param res Pointer to a SocketAddress array to store the result..
60 * @param interface_name Network interface name
61 * @return number of results on success, negative error code on failure.
62 */
63 virtual nsapi_value_or_error_t getaddrinfo(const char *hostname, SocketAddress *hints, SocketAddress **res, const char *interface_name = NULL) = 0;
64
65 /** Hostname translation callback for gethostbyname_async.
66 *
67 * The callback is called after DNS resolution completes, or a failure occurs.
68 *
69 * Callback should not take more than 10ms to execute, otherwise it might
70 * prevent underlying thread processing. A portable user of the callback
71 * should not make calls to network operations due to stack size limitations.
72 * The callback should not perform expensive operations such as socket recv/send
73 * calls or blocking operations.
74 *
75 * <br>
76 * \c result : Negative error code on failure or value that represents the number of DNS records
77 * <br>
78 * \c address : On success, destination for the host SocketAddress.
79 */
81
82 /** Translate a hostname to an IP address (asynchronous)
83 *
84 * The hostname may be either a domain name or an IP address. If the
85 * hostname is an IP address, no network transactions will be performed.
86 *
87 * If no stack-specific DNS resolution is provided, the hostname
88 * will be resolved using a UDP socket on the stack.
89 *
90 * The call is non-blocking. The result of the DNS operation is returned by the callback.
91 * If this function returns failure, the callback will not be called. If it is successful,
92 * (the IP address was found from the DNS cache), the callback will be called
93 * before the function returns.
94 *
95 * @param host Hostname to resolve.
96 * @param callback Callback that is called to return the result.
97 * @param version IP version of address to resolve. NSAPI_UNSPEC indicates that the
98 * version is chosen by the stack (defaults to NSAPI_UNSPEC).
99 * @param interface_name Network interface name
100 * @return NSAPI_ERROR_OK on immediate success,
101 * negative error code on immediate failure or
102 * a positive unique ID that represents the hostname translation operation
103 * and can be passed to cancel.
104 */
105 virtual nsapi_value_or_error_t gethostbyname_async(const char *host, hostbyname_cb_t callback, nsapi_version_t version = NSAPI_UNSPEC,
106 const char *interface_name = NULL) = 0;
107
108 /** Translate a hostname to the multiple IP addresses (asynchronous)
109 *
110 * The hostname may be either a domain name or an IP address. If the
111 * hostname is an IP address, no network transactions will be performed.
112 *
113 * If no stack-specific DNS resolution is provided, the hostname
114 * will be resolved using a UDP socket on the stack.
115 *
116 * The call is non-blocking. Result of the DNS operation is returned by the callback.
117 * If this function returns failure, callback will not be called. In case that
118 * IP addresses are found from DNS cache, callback will be called before function returns.
119 *
120 * @param hostname Hostname to resolve.
121 * @param hints Pointer to a SocketAddress with query parameters.
122 * @param callback Callback that is called to return the result.
123 * @param interface_name Network interface name
124 * @return NSAPI_ERROR_OK on immediate success,
125 * negative error code on immediate failure or
126 * a positive unique ID that represents the hostname translation operation
127 * and can be passed to cancel.
128 */
129 virtual nsapi_value_or_error_t getaddrinfo_async(const char *hostname, SocketAddress *hints, hostbyname_cb_t callback, const char *interface_name = NULL) = 0;
130
131 /** Cancel asynchronous hostname translation.
132 *
133 * When translation is canceled, callback is not called.
134 *
135 * @param id Unique ID of the hostname translation operation returned by gethostbyname_async.
136 * @return NSAPI_ERROR_OK on success, negative error code on failure.
137 */
139
140 /** Add a domain name server to list of servers to query.
141 *
142 * @param address DNS server host address.
143 * @param interface_name Network interface name
144 * @return NSAPI_ERROR_OK on success, negative error code on failure.
145 */
146 virtual nsapi_error_t add_dns_server(const SocketAddress &address, const char *interface_name = NULL) = 0;
147
148protected:
149 ~DNS() = default;
150};
151
152#endif
153
154/** @} */
Base class for DNS provider.
Definition: DNS.h:28
virtual nsapi_error_t gethostbyname_async_cancel(int id)=0
Cancel asynchronous hostname translation.
virtual nsapi_value_or_error_t getaddrinfo(const char *hostname, SocketAddress *hints, SocketAddress **res, const char *interface_name=NULL)=0
Translate a hostname to the multiple IP addresses with specific version using network interface name.
mbed::Callback< void(nsapi_value_or_error_t result, SocketAddress *address)> hostbyname_cb_t
Hostname translation callback for gethostbyname_async.
Definition: DNS.h:80
virtual nsapi_error_t add_dns_server(const SocketAddress &address, const char *interface_name=NULL)=0
Add a domain name server to list of servers to query.
virtual nsapi_error_t gethostbyname(const char *host, SocketAddress *address, nsapi_version_t version=NSAPI_UNSPEC, const char *interface_name=NULL)=0
Translate a hostname to an IP address with specific version using network interface name.
virtual nsapi_value_or_error_t gethostbyname_async(const char *host, hostbyname_cb_t callback, nsapi_version_t version=NSAPI_UNSPEC, const char *interface_name=NULL)=0
Translate a hostname to an IP address (asynchronous)
virtual nsapi_value_or_error_t getaddrinfo_async(const char *hostname, SocketAddress *hints, hostbyname_cb_t callback, const char *interface_name=NULL)=0
Translate a hostname to the multiple IP addresses (asynchronous)
SocketAddress class.
Definition: SocketAddress.h:37
Callback class based on template specialization.
Definition: Callback.h:53
signed int nsapi_value_or_error_t
Type used to represent either a value or error.
Definition: nsapi_types.h:160
signed int nsapi_error_t
Type used to represent error codes.
Definition: nsapi_types.h:142
@ NSAPI_UNSPEC
Definition: nsapi_types.h:240