Skip to content

Commit e46dd97

Browse files
author
Arto Kinnunen
authored
Merge pull request #13507 from artokin/enable_nanostack_dns_cache
[feature-wisun] Enable Nanostack DNS cache usage
2 parents 7141a74 + 69720f1 commit e46dd97

File tree

4 files changed

+215
-0
lines changed

4 files changed

+215
-0
lines changed

features/nanostack/mbed-mesh-api/mbed-mesh-api/WisunBorderRouter.h

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -260,6 +260,19 @@ class WisunBorderRouter {
260260
* */
261261
mesh_error_t get_radius_shared_secret(uint16_t *shared_secret_len, uint8_t *shared_secret);
262262

263+
/**
264+
* \brief Set DNS query result to Nanostack cache.
265+
*
266+
* Function sets DNS query result to Nanostack cache to get distributed to the devices in the Wi-SUN network.
267+
* Function must be called for a running Wi-SUN Border Router instance.
268+
*
269+
* \param address resolved address of domain_name.
270+
* \param domain_name name of the domain. Must be non-NULL.
271+
* \return MESH_ERROR_NONE on success.
272+
* \return error value in case of failure.
273+
* */
274+
mesh_error_t set_dns_query_result(SocketAddress *address, char *domain_name);
275+
263276
private:
264277
mesh_error_t configure();
265278
mesh_error_t apply_configuration(int8_t mesh_if_id);

features/nanostack/mbed-mesh-api/source/WisunBorderRouter.cpp

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -355,3 +355,20 @@ mesh_error_t WisunBorderRouter::get_radius_shared_secret(uint16_t *shared_secret
355355

356356
return MESH_ERROR_NONE;
357357
}
358+
359+
mesh_error_t WisunBorderRouter::set_dns_query_result(SocketAddress *address, char *domain_name)
360+
{
361+
if (!domain_name || !address) {
362+
return MESH_ERROR_PARAM;
363+
}
364+
365+
if (_mesh_if_id < 0) {
366+
return MESH_ERROR_STATE;
367+
}
368+
369+
if (ws_bbr_dns_query_result_set(_mesh_if_id, (const uint8_t *)address->get_ip_bytes(), domain_name) >= 0) {
370+
return MESH_ERROR_NONE;
371+
}
372+
373+
return MESH_ERROR_UNKNOWN;
374+
}

features/nanostack/nanostack-interface/Nanostack.cpp

Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,12 +30,22 @@
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_DEEP(...)
45+
#endif
46+
47+
#define NANOSTACK_ISDIGIT(c) ((c) >= '0' && (c) <= '9')
48+
3949
#define NS_INTERFACE_SOCKETS_MAX 16 //same as NanoStack SOCKET_MAX
4050

4151
#define MALLOC ns_dyn_mem_alloc
@@ -150,6 +160,51 @@ static int8_t find_interface_by_address(const uint8_t target_addr[16])
150160
return -1;
151161
}
152162

163+
static int8_t nanostack_interface_id_parse(const char *interface_name)
164+
{
165+
int namelen;
166+
int8_t interface_id = -1;
167+
168+
TRACE_DEEP("nanostack_interface_id_parse() %s", interface_name ? interface_name : "null");
169+
170+
if (!interface_name) {
171+
return -1;
172+
}
173+
174+
// parse interface ID from the interface_name
175+
namelen = strlen(interface_name);
176+
if (namelen < 4 || namelen > 5) {
177+
return -1;
178+
}
179+
180+
if ((strncmp("MES", interface_name, 3) == 0) && NANOSTACK_ISDIGIT(interface_name[3])) {
181+
interface_id = atoi(&interface_name[3]);
182+
}
183+
184+
TRACE_DEEP("parsed interfaceID = %d", interface_id);
185+
return interface_id;
186+
}
187+
188+
static int nanostack_dns_query_result_check(const char *domain_name, SocketAddress *address, const char *interface_name)
189+
{
190+
uint8_t dns_query_addr[16] = {0};
191+
int8_t interface_id, ns_query_result;
192+
193+
interface_id = nanostack_interface_id_parse(interface_name);
194+
195+
ns_query_result = arm_net_dns_query_result_get(interface_id, dns_query_addr, (char *)domain_name);
196+
197+
TRACE_DEEP("nanostack_dns_query_result_check(): interface_id=%d, ret=%d, resolved %s to %s",
198+
interface_id, ns_query_result, domain_name, trace_ipv6(dns_query_addr));
199+
200+
if (ns_query_result == 0) {
201+
address->set_ip_bytes(dns_query_addr, NSAPI_IPv6);
202+
return 0;
203+
}
204+
205+
return -1;
206+
}
207+
153208
void *NanostackSocket::operator new (std::size_t sz)
154209
{
155210
return MALLOC(sz);
@@ -534,6 +589,84 @@ const char *Nanostack::get_ip_address()
534589
return "::";
535590
}
536591

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

features/nanostack/nanostack-interface/Nanostack.h

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,58 @@ class Nanostack : public OnboardNetworkStack, private mbed::NonCopyable<Nanostac
6363
*/
6464
virtual const char *get_ip_address();
6565

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

0 commit comments

Comments
 (0)