Skip to content

Commit 00ebd65

Browse files
author
Arto Kinnunen
committed
Mesh: Enable Nanostack DNS cache usage
Inherit methods gethostbyname, gethostbyname_async and get_dns_server to Nanostack class. Methods will try to find DNS server address or DNS query results from Nanostack DNS cache.
1 parent ad40b1b commit 00ebd65

File tree

2 files changed

+172
-0
lines changed

2 files changed

+172
-0
lines changed

connectivity/nanostack/include/nanostack-interface/Nanostack.h

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,58 @@ class Nanostack : public OnboardNetworkStack, private mbed::NonCopyable<Nanostac
5959
/** @copydoc NetworkStack::get_ip_address */
6060
nsapi_error_t get_ip_address(SocketAddress *sockAddr) override;
6161

62+
/** Translate a hostname to an IP address with specific version using network interface name.
63+
*
64+
* The hostname may be either a domain name or an IP address. If the
65+
* hostname is an IP address, no network transactions will be performed.
66+
*
67+
* Method first checks Nanostack DNS query result cache. If match is found, then the result is returned immediately.
68+
* Otherwise method calls DNS resolver to find a match.
69+
*
70+
* @param host Hostname to resolve.
71+
* @param address Pointer to a SocketAddress to store the result.
72+
* @param version IP version of address to resolve, NSAPI_UNSPEC indicates
73+
* version is chosen by the stack (defaults to NSAPI_UNSPEC).
74+
* @param interface_name Network interface name
75+
* @return NSAPI_ERROR_OK on success, negative error code on failure.
76+
*/
77+
virtual nsapi_error_t gethostbyname(const char *name, SocketAddress *address, nsapi_version_t version, const char *interface_name);
78+
79+
/** Translate a hostname to an IP address (asynchronous) using network interface name.
80+
*
81+
* The hostname may be either a domain name or a dotted IP address. If the
82+
* hostname is an IP address, no network transactions will be performed.
83+
*
84+
* Method first checks Nanostack DNS query result cache. If match is found, then the result is returned immediately.
85+
*
86+
* Call is non-blocking. Result of the DNS operation is returned by the callback.
87+
* If this function returns failure, callback will not be called. In case result
88+
* is success (IP address was found from DNS cache), callback will be called
89+
* before function returns.
90+
*
91+
* @param host Hostname to resolve.
92+
* @param callback Callback that is called for result.
93+
* @param version IP version of address to resolve, NSAPI_UNSPEC indicates
94+
* version is chosen by the stack (defaults to NSAPI_UNSPEC).
95+
* @param interface_name Network interface name
96+
* @return 0 on immediate success,
97+
* negative error code on immediate failure or
98+
* a positive unique id that represents the hostname translation operation
99+
* and can be passed to cancel.
100+
*/
101+
virtual nsapi_value_or_error_t gethostbyname_async(const char *name, hostbyname_cb_t callback, nsapi_version_t version, const char *interface_name);
102+
103+
/** Get a domain name server from a list of servers to query
104+
*
105+
* Returns a DNS server address for a index. DNS servers are queried from Nanostack DNS cache.
106+
* If returns error no more DNS servers to read.
107+
*
108+
* @param index Index of the DNS server, starts from zero
109+
* @param address Destination for the host address
110+
* @return 0 on success, negative error code on failure
111+
*/
112+
virtual nsapi_error_t get_dns_server(int index, SocketAddress *address, const char *interface_name);
113+
62114
/** Opens a socket
63115
*
64116
* Creates a network socket and stores it in the specified handle.

connectivity/nanostack/source/Nanostack.cpp

Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,12 +30,20 @@
3030
#include "mesh_system.h" // from inside mbed-mesh-api
3131
#include "socket_api.h"
3232
#include "net_interface.h"
33+
#include "nsapi_dns.h"
3334

3435
// Uncomment to enable trace
3536
//#define HAVE_DEBUG
3637
#include "ns_trace.h"
3738
#define TRACE_GROUP "nsif"
3839

40+
#define NSIF_DEEP_TRACE
41+
#ifdef NSIF_DEEP_TRACE
42+
#define TRACE_DEEP tr_debug
43+
#else
44+
#define TRACE_DEP(...)
45+
#endif
46+
3947
#define NS_INTERFACE_SOCKETS_MAX 16 //same as NanoStack SOCKET_MAX
4048

4149
#define MALLOC ns_dyn_mem_alloc
@@ -150,6 +158,50 @@ static int8_t find_interface_by_address(const uint8_t target_addr[16])
150158
return -1;
151159
}
152160

161+
static int8_t nanostack_interface_id_parse(const char* interface_name)
162+
{
163+
int8_t interface_id = -1;
164+
165+
TRACE_DEEP("nanostack_interface_id_parse() %s", interface_name ? interface_name : "null");
166+
167+
if (!interface_name) {
168+
return -1;
169+
}
170+
171+
// parse interface ID from the interface_name
172+
if (strlen(interface_name) < 4) {
173+
return -1;
174+
}
175+
176+
interface_id = atoi(&interface_name[3]);
177+
if (interface_id < 0) {
178+
return -1;
179+
}
180+
181+
TRACE_DEEP("parsed interfaceID = %d", interface_id);
182+
return interface_id;
183+
}
184+
185+
static int8_t nanostack_dns_query_result_check(const char *domain_name, SocketAddress *address, const char *interface_name)
186+
{
187+
uint8_t dns_query_addr[16] = {0};
188+
int8_t interface_id, ns_query_result;
189+
190+
interface_id = nanostack_interface_id_parse(interface_name);
191+
192+
ns_query_result = arm_net_dns_query_result_get(interface_id, dns_query_addr, (char*)domain_name);
193+
194+
TRACE_DEEP("nanostack_dns_query_result_check(): interface_id=%d, ret=%d, resolved %s to %s",
195+
interface_id, ns_query_result, domain_name, trace_ipv6(dns_query_addr));
196+
197+
if (ns_query_result == 0) {
198+
address->set_ip_bytes(dns_query_addr, NSAPI_IPv6);
199+
return 0;
200+
}
201+
202+
return -1;
203+
}
204+
153205
void *NanostackSocket::operator new (std::size_t sz)
154206
{
155207
return MALLOC(sz);
@@ -532,6 +584,74 @@ nsapi_error_t Nanostack::get_ip_address(SocketAddress *sockAddr)
532584
return NSAPI_ERROR_NO_ADDRESS;
533585
}
534586

587+
nsapi_error_t Nanostack::gethostbyname(const char *name, SocketAddress *address, nsapi_version_t version, const char *interface_name)
588+
{
589+
if (name[0] == '\0') {
590+
return NSAPI_ERROR_PARAMETER;
591+
}
592+
// check for simple ip addresses
593+
if (address->set_ip_address(name)) {
594+
if (version != NSAPI_UNSPEC && address->get_ip_version() != version) {
595+
return NSAPI_ERROR_DNS_FAILURE;
596+
}
597+
return NSAPI_ERROR_OK;
598+
}
599+
600+
// try nanostack DNS cache, if not found then fallback to dns query
601+
if (nanostack_dns_query_result_check(name, address, interface_name) == 0) {
602+
return 0;
603+
}
604+
605+
return nsapi_dns_query(this, name, address, interface_name, version);
606+
}
607+
608+
nsapi_value_or_error_t Nanostack::gethostbyname_async(const char *name, hostbyname_cb_t callback, nsapi_version_t version, const char *interface_name)
609+
{
610+
SocketAddress address;
611+
612+
if (name[0] == '\0') {
613+
return NSAPI_ERROR_PARAMETER;
614+
}
615+
616+
// check for simple ip addresses
617+
if (address.set_ip_address(name)) {
618+
if (version != NSAPI_UNSPEC && address.get_ip_version() != version) {
619+
return NSAPI_ERROR_DNS_FAILURE;
620+
}
621+
callback(NSAPI_ERROR_OK, &address);
622+
return NSAPI_ERROR_OK;
623+
}
624+
625+
// try nanostack DNS cache, if not found then fallback to dns query
626+
if (nanostack_dns_query_result_check(name, &address, interface_name) == 0) {
627+
// hit found, return result immediately
628+
callback(NSAPI_ERROR_OK, &address);
629+
return NSAPI_ERROR_OK;
630+
}
631+
632+
call_in_callback_cb_t call_in_cb = get_call_in_callback();
633+
return nsapi_dns_query_async(this, name, callback, call_in_cb, interface_name, version);
634+
}
635+
636+
nsapi_error_t Nanostack::get_dns_server(int index, SocketAddress *address, const char *interface_name)
637+
{
638+
uint8_t dns_srv_address[16];
639+
int8_t interface_id;
640+
int8_t ret;
641+
642+
interface_id = nanostack_interface_id_parse(interface_name);
643+
644+
ret = arm_net_dns_server_get(interface_id, dns_srv_address, NULL, 0, index);
645+
646+
if (ret == 0) {
647+
address->set_ip_bytes(dns_srv_address, NSAPI_IPv6);
648+
TRACE_DEEP("get_dns_server(), index=%d, ret=%d, address=%s", index, ret, trace_ipv6((uint8_t*)address->get_ip_bytes()));
649+
return NSAPI_ERROR_OK;
650+
}
651+
652+
return NSAPI_ERROR_NO_ADDRESS;
653+
}
654+
535655
nsapi_error_t Nanostack::socket_open(void **handle, nsapi_protocol_t protocol)
536656
{
537657
// Validate parameters

0 commit comments

Comments
 (0)