Skip to content

Commit 0fbd134

Browse files
Getaddrinfo interface for multiple DNS adresses added.
1 parent ef4fe98 commit 0fbd134

File tree

6 files changed

+195
-0
lines changed

6 files changed

+195
-0
lines changed

features/netsocket/DNS.h

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,23 @@ class DNS {
4242
*/
4343
virtual nsapi_error_t gethostbyname(const char *host,
4444
SocketAddress *address, nsapi_version_t version = NSAPI_UNSPEC, const char *interface_name = NULL) = 0;
45+
46+
/** Translate a hostname to the multiple IP addresses with specific version using network interface name.
47+
*
48+
* The hostname may be either a domain name or an IP address. If the
49+
* hostname is an IP address, no network transactions will be performed.
50+
*
51+
* If no stack-specific DNS resolution is provided, the hostname
52+
* will be resolve using a UDP socket on the stack.
53+
*
54+
* @param hostname Hostname to resolve.
55+
* @param hints Pointer to a SocketAddress with query parameters.
56+
* @param res Pointer to a SocketAddress array to store the result..
57+
* @param interface_name Network interface name
58+
* @return number of results on success, negative error code on failure.
59+
*/
60+
virtual nsapi_error_t getaddrinfo(const char *hostname, SocketAddress *hints, SocketAddress **res, const char *interface_name = NULL) = 0;
61+
4562
/** Hostname translation callback for gethostbyname_async.
4663
*
4764
* The callback is called after DNS resolution completes, or a failure occurs.
@@ -83,6 +100,33 @@ class DNS {
83100
virtual nsapi_value_or_error_t gethostbyname_async(const char *host, hostbyname_cb_t callback, nsapi_version_t version = NSAPI_UNSPEC,
84101
const char *interface_name = NULL) = 0;
85102

103+
/** Translate a hostname to the multiple IP addresses (asynchronous)
104+
*
105+
* The hostname may be either a domain name or an IP address. If the
106+
* hostname is an IP address, no network transactions will be performed.
107+
*
108+
* If no stack-specific DNS resolution is provided, the hostname
109+
* will be resolved using a UDP socket on the stack.
110+
*
111+
* The call is non-blocking. The result of the DNS operation is returned by the callback.
112+
* If this function returns failure, the callback will not be called. If it is successful,
113+
* (the IP address was found from the DNS cache), the callback will be called
114+
* before the function returns.
115+
*
116+
* @param host Hostname to resolve.
117+
* @addr_count number of expected IP addresses.
118+
* @param callback Callback that is called to return the result.
119+
* @param version IP version of address to resolve. NSAPI_UNSPEC indicates that the
120+
* version is chosen by the stack (defaults to NSAPI_UNSPEC).
121+
* @param interface_name Network interface name
122+
* @return NSAPI_ERROR_OK on immediate success,
123+
* negative error code on immediate failure or
124+
* a positive unique ID that represents the hostname translation operation
125+
* and can be passed to cancel.
126+
*/
127+
virtual nsapi_value_or_error_t getaddrinfo_async(const char *host, unsigned addr_count, hostbyname_cb_t callback, nsapi_version_t version = NSAPI_UNSPEC,
128+
const char *interface_name = NULL) = 0;
129+
86130
/** Cancel asynchronous hostname translation.
87131
*
88132
* When translation is canceled, callback is not called.

features/netsocket/NetworkInterface.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,11 +71,21 @@ nsapi_error_t NetworkInterface::gethostbyname(const char *name, SocketAddress *a
7171
return get_stack()->gethostbyname(name, address, version, interface_name);
7272
}
7373

74+
nsapi_error_t NetworkInterface::getaddrinfo(const char *hostname, SocketAddress *hints, SocketAddress **res, const char *interface_name)
75+
{
76+
return get_stack()->getaddrinfo(hostname, hints, res, interface_name);
77+
}
78+
7479
nsapi_value_or_error_t NetworkInterface::gethostbyname_async(const char *host, hostbyname_cb_t callback, nsapi_version_t version, const char *interface_name)
7580
{
7681
return get_stack()->gethostbyname_async(host, callback, version, interface_name);
7782
}
7883

84+
nsapi_value_or_error_t NetworkInterface::getaddrinfo_async(const char *host, unsigned addr_count, hostbyname_cb_t callback, nsapi_version_t version, const char *interface_name)
85+
{
86+
return get_stack()->getaddrinfo_async(host, addr_count, callback, version, interface_name);
87+
}
88+
7989
nsapi_error_t NetworkInterface::gethostbyname_async_cancel(int id)
8090
{
8191
return get_stack()->gethostbyname_async_cancel(id);

features/netsocket/NetworkInterface.h

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,22 @@ class NetworkInterface: public DNS {
203203
virtual nsapi_error_t gethostbyname(const char *host,
204204
SocketAddress *address, nsapi_version_t version = NSAPI_UNSPEC, const char *interface_name = NULL);
205205

206+
/** Translate a hostname to the multiple IP addresses with specific version using network interface name.
207+
*
208+
* The hostname may be either a domain name or an IP address. If the
209+
* hostname is an IP address, no network transactions will be performed.
210+
*
211+
* If no stack-specific DNS resolution is provided, the hostname
212+
* will be resolve using a UDP socket on the stack.
213+
*
214+
* @param hostname Hostname to resolve.
215+
* @param hints Pointer to a SocketAddress with query parameters.
216+
* @param res Pointer to a SocketAddress array to store the result..
217+
* @param interface_name Network interface name
218+
* @return number of results on success, negative error code on failure.
219+
*/
220+
virtual nsapi_error_t getaddrinfo(const char *hostname, SocketAddress *hints, SocketAddress **res, const char *interface_name = NULL);
221+
206222
/** Hostname translation callback (for use with gethostbyname_async()).
207223
*
208224
* Callback will be called after DNS resolution completes or a failure occurs.
@@ -244,6 +260,33 @@ class NetworkInterface: public DNS {
244260
virtual nsapi_value_or_error_t gethostbyname_async(const char *host, hostbyname_cb_t callback, nsapi_version_t version = NSAPI_UNSPEC,
245261
const char *interface_name = NULL);
246262

263+
/** Translate a hostname to the multiple IP addresses (asynchronous) using network interface name.
264+
*
265+
* The hostname may be either a domain name or a dotted IP address. If the
266+
* hostname is an IP address, no network transactions will be performed.
267+
*
268+
* If no stack-specific DNS resolution is provided, the hostname
269+
* will be resolve using a UDP socket on the stack.
270+
*
271+
* Call is non-blocking. Result of the DNS operation is returned by the callback.
272+
* If this function returns failure, callback will not be called. In case result
273+
* is success (IP address was found from DNS cache), callback will be called
274+
* before function returns.
275+
*
276+
* @param host Hostname to resolve.
277+
* @addr_count number of expected IP addresses.
278+
* @param callback Callback that is called for result.
279+
* @param version IP version of address to resolve, NSAPI_UNSPEC indicates
280+
* version is chosen by the stack (defaults to NSAPI_UNSPEC).
281+
* @param interface_name Network interface name
282+
* @return 0 on immediate success,
283+
* negative error code on immediate failure or
284+
* a positive unique id that represents the hostname translation operation
285+
* and can be passed to cancel.
286+
*/
287+
virtual nsapi_value_or_error_t getaddrinfo_async(const char *host, unsigned addr_count, hostbyname_cb_t callback, nsapi_version_t version = NSAPI_UNSPEC,
288+
const char *interface_name = NULL);
289+
247290
/** Cancel asynchronous hostname translation.
248291
*
249292
* When translation is cancelled, callback will not be called.

features/netsocket/NetworkStack.cpp

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,36 @@ nsapi_error_t NetworkStack::gethostbyname(const char *name, SocketAddress *addre
6262
return nsapi_dns_query(this, name, address, interface_name, version);
6363
}
6464

65+
nsapi_error_t NetworkStack::getaddrinfo(const char *hostname, SocketAddress *hints, SocketAddress **res, const char *interface_name)
66+
{
67+
int i;
68+
if (hostname[0] == '\0') {
69+
return NSAPI_ERROR_PARAMETER;
70+
}
71+
nsapi_version_t version = hints->get_ip_version();
72+
// if the version is unspecified, try to guess the version from the
73+
// ip address of the underlying stack
74+
if (version == NSAPI_UNSPEC) {
75+
SocketAddress testaddress;
76+
if (testaddress.set_ip_address(this->get_ip_address())) {
77+
version = testaddress.get_ip_version();
78+
}
79+
}
80+
81+
SocketAddress *temp = new (std::nothrow) SocketAddress [MBED_CONF_NSAPI_DNS_ADDRESSES_LIMIT];
82+
83+
int adr_cnt = nsapi_dns_query_multiple(this, hostname, temp, MBED_CONF_NSAPI_DNS_ADDRESSES_LIMIT, interface_name, version);
84+
85+
if (adr_cnt > 0) {
86+
*res = new (std::nothrow) SocketAddress [adr_cnt];
87+
for (i = 0; i < adr_cnt; i++) {
88+
(*res)[i] = temp[i];
89+
}
90+
}
91+
delete[] temp;
92+
return adr_cnt;
93+
}
94+
6595
nsapi_value_or_error_t NetworkStack::gethostbyname_async(const char *name, hostbyname_cb_t callback, nsapi_version_t version, const char *interface_name)
6696
{
6797
SocketAddress address;
@@ -94,6 +124,27 @@ nsapi_value_or_error_t NetworkStack::gethostbyname_async(const char *name, hostb
94124
return nsapi_dns_query_async(this, name, callback, call_in_cb, interface_name, version);
95125
}
96126

127+
nsapi_value_or_error_t NetworkStack::getaddrinfo_async(const char *name, unsigned addr_count, hostbyname_cb_t callback, nsapi_version_t version, const char *interface_name)
128+
{
129+
SocketAddress address;
130+
131+
if (name[0] == '\0' || addr_count < 1) {
132+
return NSAPI_ERROR_PARAMETER;
133+
}
134+
135+
// if the version is unspecified, try to guess the version from the
136+
// ip address of the underlying stack
137+
if (version == NSAPI_UNSPEC) {
138+
SocketAddress testaddress;
139+
if (testaddress.set_ip_address(this->get_ip_address())) {
140+
version = testaddress.get_ip_version();
141+
}
142+
}
143+
144+
call_in_callback_cb_t call_in_cb = get_call_in_callback();
145+
146+
return nsapi_dns_query_multiple_async(this, name, callback, addr_count, call_in_cb, interface_name, version);
147+
}
97148
nsapi_error_t NetworkStack::gethostbyname_async_cancel(int id)
98149
{
99150
return nsapi_dns_query_async_cancel(id);

features/netsocket/NetworkStack.h

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,22 @@ class NetworkStack: public DNS {
7474
virtual nsapi_error_t gethostbyname(const char *host,
7575
SocketAddress *address, nsapi_version_t version = NSAPI_UNSPEC, const char *interface_name = NULL);
7676

77+
/** Translate a hostname to the multiple IP addresses with specific version using network interface name.
78+
*
79+
* The hostname may be either a domain name or an IP address. If the
80+
* hostname is an IP address, no network transactions will be performed.
81+
*
82+
* If no stack-specific DNS resolution is provided, the hostname
83+
* will be resolve using a UDP socket on the stack.
84+
*
85+
* @param hostname Hostname to resolve.
86+
* @param hints Pointer to a SocketAddress with query parameters.
87+
* @param res Pointer to a SocketAddress array to store the result..
88+
* @param interface_name Network interface name
89+
* @return number of results on success, negative error code on failure.
90+
*/
91+
virtual nsapi_error_t getaddrinfo(const char *hostname, SocketAddress *hints, SocketAddress **res, const char *interface_name = NULL);
92+
7793
/** Hostname translation callback (asynchronous)
7894
*
7995
* Callback will be called after DNS resolution completes or a failure occurs.
@@ -115,6 +131,33 @@ class NetworkStack: public DNS {
115131
virtual nsapi_value_or_error_t gethostbyname_async(const char *host, hostbyname_cb_t callback, nsapi_version_t version = NSAPI_UNSPEC,
116132
const char *interface_name = NULL);
117133

134+
/** Translates a hostname to the multiple IP addresses (asynchronous)
135+
*
136+
* The hostname may be either a domain name or an IP address. If the
137+
* hostname is an IP address, no network transactions will be performed.
138+
*
139+
* If no stack-specific DNS resolution is provided, the hostname
140+
* will be resolve using a UDP socket on the stack.
141+
*
142+
* Call is non-blocking. Result of the DNS operation is returned by the callback.
143+
* If this function returns failure, callback will not be called. In case result
144+
* is success (IP address was found from DNS cache), callback will be called
145+
* before function returns.
146+
*
147+
* @param host Hostname to resolve
148+
* @addr_count number of expected IP addresses.
149+
* @param callback Callback that is called for result
150+
* @param version IP version of address to resolve, NSAPI_UNSPEC indicates
151+
* version is chosen by the stack (defaults to NSAPI_UNSPEC)
152+
* @param interface_name Network interface_name
153+
* @return 0 on immediate success,
154+
* negative error code on immediate failure or
155+
* a positive unique id that represents the hostname translation operation
156+
* and can be passed to cancel
157+
*/
158+
virtual nsapi_value_or_error_t getaddrinfo_async(const char *host, unsigned addr_count, hostbyname_cb_t callback, nsapi_version_t version = NSAPI_UNSPEC,
159+
const char *interface_name = NULL);
160+
118161
/** Cancels asynchronous hostname translation
119162
*
120163
* When translation is cancelled, callback will not be called.

features/netsocket/mbed_lib.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,10 @@
5858
"help": "Number of cached host name resolutions",
5959
"value": 3
6060
},
61+
"dns-addresses-limit": {
62+
"help": "Max number IP addresses returned by multiple DNS query",
63+
"value": 10
64+
},
6165
"socket-stats-enabled": {
6266
"help": "Enable network socket statistics",
6367
"value": false

0 commit comments

Comments
 (0)