From 4ffeec17973227670834c094af3b92a9709590dc Mon Sep 17 00:00:00 2001 From: Christopher Haster Date: Fri, 26 Aug 2016 12:14:44 -0500 Subject: [PATCH] [nsapi] Restructured nsapi_dns.h to have clear separation between C/C++ - Separated overloads based on language - Removed NSAPI_C_LINKAGE definition --- features/net/network-socket/nsapi_dns.cpp | 35 +----- features/net/network-socket/nsapi_dns.h | 137 +++++++++++++++++----- features/net/network-socket/nsapi_types.h | 12 -- 3 files changed, 111 insertions(+), 73 deletions(-) diff --git a/features/net/network-socket/nsapi_dns.cpp b/features/net/network-socket/nsapi_dns.cpp index 5078ad7805..1fab0ef7f6 100644 --- a/features/net/network-socket/nsapi_dns.cpp +++ b/features/net/network-socket/nsapi_dns.cpp @@ -35,8 +35,7 @@ nsapi_addr_t dns_servers[DNS_SERVERS_SIZE] = { // DNS server configuration -NSAPI_C_LINKAGE -int nsapi_dns_add_server(nsapi_addr_t addr) +extern "C" int nsapi_dns_add_server(nsapi_addr_t addr) { memmove(&dns_servers[1], &dns_servers[0], (DNS_SERVERS_SIZE-1)*sizeof(nsapi_addr_t)); @@ -45,16 +44,6 @@ int nsapi_dns_add_server(nsapi_addr_t addr) return 0; } -int nsapi_dns_add_server(const SocketAddress &address) -{ - return nsapi_dns_add_server(address.get_addr()); -} - -int nsapi_dns_add_server(const char *address) -{ - return nsapi_dns_add_server(SocketAddress(address)); -} - // DNS packet parsing static void dns_append_byte(uint8_t **p, uint8_t byte) @@ -272,8 +261,7 @@ static int nsapi_dns_query_multiple(NetworkStack *stack, } // convenience functions for other forms of queries -NSAPI_C_LINKAGE -int nsapi_dns_query_multiple(nsapi_stack_t *stack, +extern "C" int nsapi_dns_query_multiple(nsapi_stack_t *stack, nsapi_addr_t *addr, unsigned addr_count, const char *host, nsapi_version_t version) { @@ -281,14 +269,6 @@ int nsapi_dns_query_multiple(nsapi_stack_t *stack, return nsapi_dns_query_multiple(nstack, addr, addr_count, host, version); } -int nsapi_dns_query_multiple(nsapi_stack_t *stack, - nsapi_addr_t *addr, unsigned addr_count, - const char *host) -{ - NetworkStack *nstack = nsapi_create_stack(stack); - return nsapi_dns_query_multiple(nstack, addr, addr_count, host, NSAPI_IPv4); -} - int nsapi_dns_query_multiple(NetworkStack *stack, SocketAddress *addresses, unsigned addr_count, const char *host, nsapi_version_t version) @@ -306,8 +286,7 @@ int nsapi_dns_query_multiple(NetworkStack *stack, return result; } -NSAPI_C_LINKAGE -int nsapi_dns_query(nsapi_stack_t *stack, +extern "C" int nsapi_dns_query(nsapi_stack_t *stack, nsapi_addr_t *addr, const char *host, nsapi_version_t version) { NetworkStack *nstack = nsapi_create_stack(stack); @@ -315,14 +294,6 @@ int nsapi_dns_query(nsapi_stack_t *stack, return (result > 0) ? 0 : result; } -int nsapi_dns_query(nsapi_stack_t *stack, - nsapi_addr_t *addr, const char *host) -{ - NetworkStack *nstack = nsapi_create_stack(stack); - int result = nsapi_dns_query_multiple(nstack, addr, 1, host, NSAPI_IPv4); - return (result > 0) ? 0 : result; -} - int nsapi_dns_query(NetworkStack *stack, SocketAddress *address, const char *host, nsapi_version_t version) { diff --git a/features/net/network-socket/nsapi_dns.h b/features/net/network-socket/nsapi_dns.h index 11f9541519..e9a1685555 100644 --- a/features/net/network-socket/nsapi_dns.h +++ b/features/net/network-socket/nsapi_dns.h @@ -18,7 +18,48 @@ #define NSAPI_DNS_H #include "nsapi_types.h" +#ifdef __cplusplus #include "network-socket/NetworkStack.h" +#endif + +#ifndef __cplusplus + + +/** Query a domain name server for an IP address of a given hostname + * + * @param stack Network stack as target for DNS query + * @param addr Destination for the host address + * @param host Hostname to resolve + * @param version IP version to resolve + * @return 0 on success, negative error code on failure + * NSAPI_ERROR_DNS_FAILURE indicates the host could not be found + */ +int nsapi_dns_query(nsapi_stack_t *stack, nsapi_addr_t *addr, + const char *host, nsapi_version_t version); + +/** Query a domain name server for multiple IP address of a given hostname + * + * @param stack Network stack as target for DNS query + * @param addr Array for the host addresses + * @param addr_count Number of addresses allocated in the array + * @param host Hostname to resolve + * @param version IP version to resolve + * @return Number of addresses found on success, negative error code on failure + * NSAPI_ERROR_DNS_FAILURE indicates the host could not be found + */ +int nsapi_dns_query_multiple(nsapi_stack_t *stack, + nsapi_addr_t *addr, unsigned addr_count, + const char *host, nsapi_version_t version); + +/** Add a domain name server to list of servers to query + * + * @param addr Destination for the host address + * @return 0 on success, negative error code on failure + */ +int nsapi_dns_add_server(nsapi_addr_t addr); + + +#else /** Query a domain name server for an IP address of a given hostname @@ -30,24 +71,36 @@ * @return 0 on success, negative error code on failure * NSAPI_ERROR_DNS_FAILURE indicates the host could not be found */ -NSAPI_C_LINKAGE -int nsapi_dns_query(nsapi_stack_t *stack, nsapi_addr_t *addr, - const char *host, nsapi_version_t version); - -#ifdef __cplusplus -int nsapi_dns_query(nsapi_stack_t *stack, nsapi_addr_t *addr, - const char *host); int nsapi_dns_query(NetworkStack *stack, SocketAddress *addr, const char *host, nsapi_version_t version = NSAPI_IPv4); +/** Query a domain name server for an IP address of a given hostname + * + * @param stack Network stack as target for DNS query + * @param addr Destination for the host address + * @param host Hostname to resolve + * @param version IP version to resolve (defaults to NSAPI_IPv4) + * @return 0 on success, negative error code on failure + * NSAPI_ERROR_DNS_FAILURE indicates the host could not be found + */ +extern "C" int nsapi_dns_query(nsapi_stack_t *stack, nsapi_addr_t *addr, + const char *host, nsapi_version_t version = NSAPI_IPv4); + +/** Query a domain name server for an IP address of a given hostname + * + * @param stack Network stack as target for DNS query + * @param addr Destination for the host address + * @param host Hostname to resolve + * @param version IP version to resolve (defaults to NSAPI_IPv4) + * @return 0 on success, negative error code on failure + * NSAPI_ERROR_DNS_FAILURE indicates the host could not be found + */ template int nsapi_dns_query(S *stack, SocketAddress *addr, const char *host, nsapi_version_t version = NSAPI_IPv4) { return nsapi_dns_query(nsapi_create_stack(stack), addr, host, version); } -#endif - /** Query a domain name server for multiple IP address of a given hostname * @@ -59,19 +112,34 @@ int nsapi_dns_query(S *stack, SocketAddress *addr, * @return Number of addresses found on success, negative error code on failure * NSAPI_ERROR_DNS_FAILURE indicates the host could not be found */ -NSAPI_C_LINKAGE -int nsapi_dns_query_multiple(nsapi_stack_t *stack, - nsapi_addr_t *addr, unsigned addr_count, - const char *host, nsapi_version_t version); - -#ifdef __cplusplus -int nsapi_dns_query_multiple(nsapi_stack_t *stack, - nsapi_addr_t *addr, unsigned addr_count, - const char *host); int nsapi_dns_query_multiple(NetworkStack *stack, SocketAddress *addr, unsigned addr_count, const char *host, nsapi_version_t version = NSAPI_IPv4); +/** Query a domain name server for multiple IP address of a given hostname + * + * @param stack Network stack as target for DNS query + * @param addr Array for the host addresses + * @param addr_count Number of addresses allocated in the array + * @param host Hostname to resolve + * @param version IP version to resolve (defaults to NSAPI_IPv4) + * @return Number of addresses found on success, negative error code on failure + * NSAPI_ERROR_DNS_FAILURE indicates the host could not be found + */ +extern "C" int nsapi_dns_query_multiple(nsapi_stack_t *stack, + nsapi_addr_t *addr, unsigned addr_count, + const char *host, nsapi_version_t version = NSAPI_IPv4); + +/** Query a domain name server for multiple IP address of a given hostname + * + * @param stack Network stack as target for DNS query + * @param addr Array for the host addresses + * @param addr_count Number of addresses allocated in the array + * @param host Hostname to resolve + * @param version IP version to resolve (defaults to NSAPI_IPv4) + * @return Number of addresses found on success, negative error code on failure + * NSAPI_ERROR_DNS_FAILURE indicates the host could not be found + */ template int nsapi_dns_query_multiple(S *stack, SocketAddress *addr, unsigned addr_count, @@ -80,24 +148,35 @@ int nsapi_dns_query_multiple(S *stack, return nsapi_dns_query_multiple(nsapi_create_stack(stack), addr, addr_count, host, version); } -#endif - /** Add a domain name server to list of servers to query * - * @param stack Network stack as target for DNS query * @param addr Destination for the host address - * @param host Hostname to resolve * @return 0 on success, negative error code on failure - * NSAPI_ERROR_DNS_FAILURE indicates the host could not be found */ -NSAPI_C_LINKAGE -int nsapi_dns_add_server(nsapi_addr_t addr); +extern "C" int nsapi_dns_add_server(nsapi_addr_t addr); -#ifdef __cplusplus -int nsapi_dns_add_server(const SocketAddress &address); -int nsapi_dns_add_server(const char *address); -#endif +/** Add a domain name server to list of servers to query + * + * @param addr Destination for the host address + * @return 0 on success, negative error code on failure + */ +static inline int nsapi_dns_add_server(const SocketAddress &address) +{ + return nsapi_dns_add_server(address.get_addr()); +} + +/** Add a domain name server to list of servers to query + * + * @param addr Destination for the host address + * @return 0 on success, negative error code on failure + */ +static inline int nsapi_dns_add_server(const char *address) +{ + return nsapi_dns_add_server(SocketAddress(address)); +} +#endif + #endif diff --git a/features/net/network-socket/nsapi_types.h b/features/net/network-socket/nsapi_types.h index eab291453f..669ef40311 100644 --- a/features/net/network-socket/nsapi_types.h +++ b/features/net/network-socket/nsapi_types.h @@ -148,18 +148,6 @@ typedef enum nsapi_option { } nsapi_option_t; -/* C linkage definition - * - * Attribute garuntees emitted function has C linkage. - * Available in both C and C++ - */ -#ifdef __cplusplus -#define NSAPI_C_LINKAGE extern "C" -#else -#define NSAPI_C_LINKAGE -#endif - - /** nsapi_stack structure * * Stack structure representing a specific instance of a stack.