|
30 | 30 | #include "mesh_system.h" // from inside mbed-mesh-api
|
31 | 31 | #include "socket_api.h"
|
32 | 32 | #include "net_interface.h"
|
| 33 | +#include "nsapi_dns.h" |
33 | 34 |
|
34 | 35 | // Uncomment to enable trace
|
35 | 36 | //#define HAVE_DEBUG
|
36 | 37 | #include "ns_trace.h"
|
37 | 38 | #define TRACE_GROUP "nsif"
|
38 | 39 |
|
| 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 | + |
39 | 49 | #define NS_INTERFACE_SOCKETS_MAX 16 //same as NanoStack SOCKET_MAX
|
40 | 50 |
|
41 | 51 | #define MALLOC ns_dyn_mem_alloc
|
@@ -150,6 +160,51 @@ static int8_t find_interface_by_address(const uint8_t target_addr[16])
|
150 | 160 | return -1;
|
151 | 161 | }
|
152 | 162 |
|
| 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 | + |
153 | 208 | void *NanostackSocket::operator new (std::size_t sz)
|
154 | 209 | {
|
155 | 210 | return MALLOC(sz);
|
@@ -532,6 +587,84 @@ nsapi_error_t Nanostack::get_ip_address(SocketAddress *sockAddr)
|
532 | 587 | return NSAPI_ERROR_NO_ADDRESS;
|
533 | 588 | }
|
534 | 589 |
|
| 590 | +nsapi_error_t Nanostack::gethostbyname(const char *name, SocketAddress *address, nsapi_version_t version, const char *interface_name) |
| 591 | +{ |
| 592 | + if (name[0] == '\0') { |
| 593 | + return NSAPI_ERROR_PARAMETER; |
| 594 | + } |
| 595 | + // check for simple ip addresses |
| 596 | + if (address->set_ip_address(name)) { |
| 597 | + if (version != NSAPI_UNSPEC && address->get_ip_version() != version) { |
| 598 | + return NSAPI_ERROR_DNS_FAILURE; |
| 599 | + } |
| 600 | + return NSAPI_ERROR_OK; |
| 601 | + } |
| 602 | + |
| 603 | + // Nanostack is IPv6 stack |
| 604 | + if (version == NSAPI_UNSPEC) { |
| 605 | + version = NSAPI_IPv6; |
| 606 | + } |
| 607 | + |
| 608 | + // try nanostack DNS cache, if not found then fallback to dns query |
| 609 | + if (nanostack_dns_query_result_check(name, address, interface_name) == 0) { |
| 610 | + return NSAPI_ERROR_OK; |
| 611 | + } |
| 612 | + |
| 613 | + return nsapi_dns_query(this, name, address, interface_name, version); |
| 614 | +} |
| 615 | + |
| 616 | +nsapi_value_or_error_t Nanostack::gethostbyname_async(const char *name, hostbyname_cb_t callback, nsapi_version_t version, const char *interface_name) |
| 617 | +{ |
| 618 | + SocketAddress address; |
| 619 | + |
| 620 | + if (name[0] == '\0') { |
| 621 | + return NSAPI_ERROR_PARAMETER; |
| 622 | + } |
| 623 | + |
| 624 | + // check for simple ip addresses |
| 625 | + if (address.set_ip_address(name)) { |
| 626 | + if (version != NSAPI_UNSPEC && address.get_ip_version() != version) { |
| 627 | + return NSAPI_ERROR_DNS_FAILURE; |
| 628 | + } |
| 629 | + callback(NSAPI_ERROR_OK, &address); |
| 630 | + return NSAPI_ERROR_OK; |
| 631 | + } |
| 632 | + |
| 633 | + // Nanostack is IPv6 stack |
| 634 | + if (version == NSAPI_UNSPEC) { |
| 635 | + version = NSAPI_IPv6; |
| 636 | + } |
| 637 | + |
| 638 | + // try nanostack DNS cache, if not found then fallback to dns query |
| 639 | + if (nanostack_dns_query_result_check(name, &address, interface_name) == 0) { |
| 640 | + // hit found, return result immediately |
| 641 | + callback(NSAPI_ERROR_OK, &address); |
| 642 | + return NSAPI_ERROR_OK; |
| 643 | + } |
| 644 | + |
| 645 | + call_in_callback_cb_t call_in_cb = get_call_in_callback(); |
| 646 | + return nsapi_dns_query_async(this, name, callback, call_in_cb, interface_name, version); |
| 647 | +} |
| 648 | + |
| 649 | +nsapi_error_t Nanostack::get_dns_server(int index, SocketAddress *address, const char *interface_name) |
| 650 | +{ |
| 651 | + uint8_t dns_srv_address[16]; |
| 652 | + int8_t interface_id; |
| 653 | + int8_t ret; |
| 654 | + |
| 655 | + interface_id = nanostack_interface_id_parse(interface_name); |
| 656 | + |
| 657 | + ret = arm_net_dns_server_get(interface_id, dns_srv_address, NULL, 0, index); |
| 658 | + |
| 659 | + if (ret == 0) { |
| 660 | + address->set_ip_bytes(dns_srv_address, NSAPI_IPv6); |
| 661 | + TRACE_DEEP("get_dns_server(), index=%d, ret=%d, address=%s", index, ret, trace_ipv6((uint8_t *)address->get_ip_bytes())); |
| 662 | + return NSAPI_ERROR_OK; |
| 663 | + } |
| 664 | + |
| 665 | + return NSAPI_ERROR_NO_ADDRESS; |
| 666 | +} |
| 667 | + |
535 | 668 | nsapi_error_t Nanostack::socket_open(void **handle, nsapi_protocol_t protocol)
|
536 | 669 | {
|
537 | 670 | // Validate parameters
|
|
0 commit comments