Skip to content

Commit 4ffeec1

Browse files
committed
[nsapi] Restructured nsapi_dns.h to have clear separation between C/C++
- Separated overloads based on language - Removed NSAPI_C_LINKAGE definition
1 parent a3ecdf3 commit 4ffeec1

File tree

3 files changed

+108
-70
lines changed

3 files changed

+108
-70
lines changed

features/net/network-socket/nsapi_dns.cpp

Lines changed: 3 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,7 @@ nsapi_addr_t dns_servers[DNS_SERVERS_SIZE] = {
3535

3636

3737
// DNS server configuration
38-
NSAPI_C_LINKAGE
39-
int nsapi_dns_add_server(nsapi_addr_t addr)
38+
extern "C" int nsapi_dns_add_server(nsapi_addr_t addr)
4039
{
4140
memmove(&dns_servers[1], &dns_servers[0],
4241
(DNS_SERVERS_SIZE-1)*sizeof(nsapi_addr_t));
@@ -45,16 +44,6 @@ int nsapi_dns_add_server(nsapi_addr_t addr)
4544
return 0;
4645
}
4746

48-
int nsapi_dns_add_server(const SocketAddress &address)
49-
{
50-
return nsapi_dns_add_server(address.get_addr());
51-
}
52-
53-
int nsapi_dns_add_server(const char *address)
54-
{
55-
return nsapi_dns_add_server(SocketAddress(address));
56-
}
57-
5847

5948
// DNS packet parsing
6049
static void dns_append_byte(uint8_t **p, uint8_t byte)
@@ -272,23 +261,14 @@ static int nsapi_dns_query_multiple(NetworkStack *stack,
272261
}
273262

274263
// convenience functions for other forms of queries
275-
NSAPI_C_LINKAGE
276-
int nsapi_dns_query_multiple(nsapi_stack_t *stack,
264+
extern "C" int nsapi_dns_query_multiple(nsapi_stack_t *stack,
277265
nsapi_addr_t *addr, unsigned addr_count,
278266
const char *host, nsapi_version_t version)
279267
{
280268
NetworkStack *nstack = nsapi_create_stack(stack);
281269
return nsapi_dns_query_multiple(nstack, addr, addr_count, host, version);
282270
}
283271

284-
int nsapi_dns_query_multiple(nsapi_stack_t *stack,
285-
nsapi_addr_t *addr, unsigned addr_count,
286-
const char *host)
287-
{
288-
NetworkStack *nstack = nsapi_create_stack(stack);
289-
return nsapi_dns_query_multiple(nstack, addr, addr_count, host, NSAPI_IPv4);
290-
}
291-
292272
int nsapi_dns_query_multiple(NetworkStack *stack,
293273
SocketAddress *addresses, unsigned addr_count,
294274
const char *host, nsapi_version_t version)
@@ -306,23 +286,14 @@ int nsapi_dns_query_multiple(NetworkStack *stack,
306286
return result;
307287
}
308288

309-
NSAPI_C_LINKAGE
310-
int nsapi_dns_query(nsapi_stack_t *stack,
289+
extern "C" int nsapi_dns_query(nsapi_stack_t *stack,
311290
nsapi_addr_t *addr, const char *host, nsapi_version_t version)
312291
{
313292
NetworkStack *nstack = nsapi_create_stack(stack);
314293
int result = nsapi_dns_query_multiple(nstack, addr, 1, host, version);
315294
return (result > 0) ? 0 : result;
316295
}
317296

318-
int nsapi_dns_query(nsapi_stack_t *stack,
319-
nsapi_addr_t *addr, const char *host)
320-
{
321-
NetworkStack *nstack = nsapi_create_stack(stack);
322-
int result = nsapi_dns_query_multiple(nstack, addr, 1, host, NSAPI_IPv4);
323-
return (result > 0) ? 0 : result;
324-
}
325-
326297
int nsapi_dns_query(NetworkStack *stack,
327298
SocketAddress *address, const char *host, nsapi_version_t version)
328299
{

features/net/network-socket/nsapi_dns.h

Lines changed: 105 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -18,36 +18,89 @@
1818
#define NSAPI_DNS_H
1919

2020
#include "nsapi_types.h"
21+
#ifdef __cplusplus
2122
#include "network-socket/NetworkStack.h"
23+
#endif
24+
25+
#ifndef __cplusplus
2226

2327

2428
/** Query a domain name server for an IP address of a given hostname
2529
*
2630
* @param stack Network stack as target for DNS query
2731
* @param addr Destination for the host address
2832
* @param host Hostname to resolve
29-
* @param version IP version to resolve (defaults to NSAPI_IPv4)
33+
* @param version IP version to resolve
3034
* @return 0 on success, negative error code on failure
3135
* NSAPI_ERROR_DNS_FAILURE indicates the host could not be found
3236
*/
33-
NSAPI_C_LINKAGE
3437
int nsapi_dns_query(nsapi_stack_t *stack, nsapi_addr_t *addr,
3538
const char *host, nsapi_version_t version);
3639

37-
#ifdef __cplusplus
38-
int nsapi_dns_query(nsapi_stack_t *stack, nsapi_addr_t *addr,
39-
const char *host);
40+
/** Query a domain name server for multiple IP address of a given hostname
41+
*
42+
* @param stack Network stack as target for DNS query
43+
* @param addr Array for the host addresses
44+
* @param addr_count Number of addresses allocated in the array
45+
* @param host Hostname to resolve
46+
* @param version IP version to resolve
47+
* @return Number of addresses found on success, negative error code on failure
48+
* NSAPI_ERROR_DNS_FAILURE indicates the host could not be found
49+
*/
50+
int nsapi_dns_query_multiple(nsapi_stack_t *stack,
51+
nsapi_addr_t *addr, unsigned addr_count,
52+
const char *host, nsapi_version_t version);
53+
54+
/** Add a domain name server to list of servers to query
55+
*
56+
* @param addr Destination for the host address
57+
* @return 0 on success, negative error code on failure
58+
*/
59+
int nsapi_dns_add_server(nsapi_addr_t addr);
60+
61+
62+
#else
63+
64+
65+
/** Query a domain name server for an IP address of a given hostname
66+
*
67+
* @param stack Network stack as target for DNS query
68+
* @param addr Destination for the host address
69+
* @param host Hostname to resolve
70+
* @param version IP version to resolve (defaults to NSAPI_IPv4)
71+
* @return 0 on success, negative error code on failure
72+
* NSAPI_ERROR_DNS_FAILURE indicates the host could not be found
73+
*/
4074
int nsapi_dns_query(NetworkStack *stack, SocketAddress *addr,
4175
const char *host, nsapi_version_t version = NSAPI_IPv4);
4276

77+
/** Query a domain name server for an IP address of a given hostname
78+
*
79+
* @param stack Network stack as target for DNS query
80+
* @param addr Destination for the host address
81+
* @param host Hostname to resolve
82+
* @param version IP version to resolve (defaults to NSAPI_IPv4)
83+
* @return 0 on success, negative error code on failure
84+
* NSAPI_ERROR_DNS_FAILURE indicates the host could not be found
85+
*/
86+
extern "C" int nsapi_dns_query(nsapi_stack_t *stack, nsapi_addr_t *addr,
87+
const char *host, nsapi_version_t version = NSAPI_IPv4);
88+
89+
/** Query a domain name server for an IP address of a given hostname
90+
*
91+
* @param stack Network stack as target for DNS query
92+
* @param addr Destination for the host address
93+
* @param host Hostname to resolve
94+
* @param version IP version to resolve (defaults to NSAPI_IPv4)
95+
* @return 0 on success, negative error code on failure
96+
* NSAPI_ERROR_DNS_FAILURE indicates the host could not be found
97+
*/
4398
template <typename S>
4499
int nsapi_dns_query(S *stack, SocketAddress *addr,
45100
const char *host, nsapi_version_t version = NSAPI_IPv4)
46101
{
47102
return nsapi_dns_query(nsapi_create_stack(stack), addr, host, version);
48103
}
49-
#endif
50-
51104

52105
/** Query a domain name server for multiple IP address of a given hostname
53106
*
@@ -59,19 +112,34 @@ int nsapi_dns_query(S *stack, SocketAddress *addr,
59112
* @return Number of addresses found on success, negative error code on failure
60113
* NSAPI_ERROR_DNS_FAILURE indicates the host could not be found
61114
*/
62-
NSAPI_C_LINKAGE
63-
int nsapi_dns_query_multiple(nsapi_stack_t *stack,
64-
nsapi_addr_t *addr, unsigned addr_count,
65-
const char *host, nsapi_version_t version);
66-
67-
#ifdef __cplusplus
68-
int nsapi_dns_query_multiple(nsapi_stack_t *stack,
69-
nsapi_addr_t *addr, unsigned addr_count,
70-
const char *host);
71115
int nsapi_dns_query_multiple(NetworkStack *stack,
72116
SocketAddress *addr, unsigned addr_count,
73117
const char *host, nsapi_version_t version = NSAPI_IPv4);
74118

119+
/** Query a domain name server for multiple IP address of a given hostname
120+
*
121+
* @param stack Network stack as target for DNS query
122+
* @param addr Array for the host addresses
123+
* @param addr_count Number of addresses allocated in the array
124+
* @param host Hostname to resolve
125+
* @param version IP version to resolve (defaults to NSAPI_IPv4)
126+
* @return Number of addresses found on success, negative error code on failure
127+
* NSAPI_ERROR_DNS_FAILURE indicates the host could not be found
128+
*/
129+
extern "C" int nsapi_dns_query_multiple(nsapi_stack_t *stack,
130+
nsapi_addr_t *addr, unsigned addr_count,
131+
const char *host, nsapi_version_t version = NSAPI_IPv4);
132+
133+
/** Query a domain name server for multiple IP address of a given hostname
134+
*
135+
* @param stack Network stack as target for DNS query
136+
* @param addr Array for the host addresses
137+
* @param addr_count Number of addresses allocated in the array
138+
* @param host Hostname to resolve
139+
* @param version IP version to resolve (defaults to NSAPI_IPv4)
140+
* @return Number of addresses found on success, negative error code on failure
141+
* NSAPI_ERROR_DNS_FAILURE indicates the host could not be found
142+
*/
75143
template <typename S>
76144
int nsapi_dns_query_multiple(S *stack,
77145
SocketAddress *addr, unsigned addr_count,
@@ -80,24 +148,35 @@ int nsapi_dns_query_multiple(S *stack,
80148
return nsapi_dns_query_multiple(nsapi_create_stack(stack),
81149
addr, addr_count, host, version);
82150
}
83-
#endif
84151

152+
/** Add a domain name server to list of servers to query
153+
*
154+
* @param addr Destination for the host address
155+
* @return 0 on success, negative error code on failure
156+
*/
157+
extern "C" int nsapi_dns_add_server(nsapi_addr_t addr);
85158

86159
/** Add a domain name server to list of servers to query
87160
*
88-
* @param stack Network stack as target for DNS query
89161
* @param addr Destination for the host address
90-
* @param host Hostname to resolve
91162
* @return 0 on success, negative error code on failure
92-
* NSAPI_ERROR_DNS_FAILURE indicates the host could not be found
93163
*/
94-
NSAPI_C_LINKAGE
95-
int nsapi_dns_add_server(nsapi_addr_t addr);
164+
static inline int nsapi_dns_add_server(const SocketAddress &address)
165+
{
166+
return nsapi_dns_add_server(address.get_addr());
167+
}
168+
169+
/** Add a domain name server to list of servers to query
170+
*
171+
* @param addr Destination for the host address
172+
* @return 0 on success, negative error code on failure
173+
*/
174+
static inline int nsapi_dns_add_server(const char *address)
175+
{
176+
return nsapi_dns_add_server(SocketAddress(address));
177+
}
96178

97-
#ifdef __cplusplus
98-
int nsapi_dns_add_server(const SocketAddress &address);
99-
int nsapi_dns_add_server(const char *address);
100-
#endif
101179

180+
#endif
102181

103182
#endif

features/net/network-socket/nsapi_types.h

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -148,18 +148,6 @@ typedef enum nsapi_option {
148148
} nsapi_option_t;
149149

150150

151-
/* C linkage definition
152-
*
153-
* Attribute garuntees emitted function has C linkage.
154-
* Available in both C and C++
155-
*/
156-
#ifdef __cplusplus
157-
#define NSAPI_C_LINKAGE extern "C"
158-
#else
159-
#define NSAPI_C_LINKAGE
160-
#endif
161-
162-
163151
/** nsapi_stack structure
164152
*
165153
* Stack structure representing a specific instance of a stack.

0 commit comments

Comments
 (0)