Skip to content
This repository was archived by the owner on May 23, 2023. It is now read-only.

Commit ba47aa5

Browse files
authored
Merge pull request ARMmbed#2897 from geky/nsapi-consistent-unspec
nsapi - Standardize support of NSAPI_UNSPEC
2 parents da377aa + 6d9ac9f commit ba47aa5

File tree

9 files changed

+59
-91
lines changed

9 files changed

+59
-91
lines changed

features/FEATURE_LWIP/lwip-interface/lwip_stack.c

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,8 @@ static bool convert_mbed_addr_to_lwip(ip_addr_t *out, const nsapi_addr_t *in)
130130
#if !LWIP_IPV4
131131
/* For bind() and other purposes, need to accept "null" of other type */
132132
/* (People use IPv4 0.0.0.0 as a general null) */
133-
if (in->version == NSAPI_IPv4 && all_zeros(in->bytes, 4)) {
133+
if (in->version == NSAPI_UNSPEC ||
134+
(in->version == NSAPI_IPv4 && all_zeros(in->bytes, 4))) {
134135
ip_addr_set_zero_ip6(out);
135136
return true;
136137
}
@@ -145,13 +146,25 @@ static bool convert_mbed_addr_to_lwip(ip_addr_t *out, const nsapi_addr_t *in)
145146
}
146147
#if !LWIP_IPV6
147148
/* For symmetry with above, accept IPv6 :: as a general null */
148-
if (in->version == NSAPI_IPv6 && all_zeros(in->bytes, 16)) {
149+
if (in->version == NSAPI_UNSPEC ||
150+
(in->version == NSAPI_IPv6 && all_zeros(in->bytes, 16))) {
149151
ip_addr_set_zero_ip4(out);
150152
return true;
151153
}
152154
#endif
153155
#endif
154156

157+
#if LWIP_IPV4 && LWIP_IPV6
158+
if (in->version == NSAPI_UNSPEC) {
159+
#if IP_VERSION_PREF == PREF_IPV4
160+
ip_addr_set_zero_ip4(out);
161+
#else
162+
ip_addr_set_zero_ip6(out);
163+
#endif
164+
return true;
165+
}
166+
#endif
167+
155168
return false;
156169
}
157170

features/netsocket/NetworkInterface.cpp

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -55,11 +55,6 @@ int NetworkInterface::set_dhcp(bool dhcp)
5555
}
5656

5757
// DNS operations go through the underlying stack by default
58-
int NetworkInterface::gethostbyname(const char *name, SocketAddress *address)
59-
{
60-
return get_stack()->gethostbyname(name, address);
61-
}
62-
6358
int NetworkInterface::gethostbyname(const char *name, SocketAddress *address, nsapi_version_t version)
6459
{
6560
return get_stack()->gethostbyname(name, address, version);

features/netsocket/NetworkInterface.h

Lines changed: 3 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -103,20 +103,6 @@ class NetworkInterface {
103103
*/
104104
virtual int disconnect() = 0;
105105

106-
/** Translates a hostname to an IP address
107-
*
108-
* The hostname may be either a domain name or an IP address. If the
109-
* hostname is an IP address, no network transactions will be performed.
110-
*
111-
* If no stack-specific DNS resolution is provided, the hostname
112-
* will be resolve using a UDP socket on the stack.
113-
*
114-
* @param address Destination for the host SocketAddress
115-
* @param host Hostname to resolve
116-
* @return 0 on success, negative error code on failure
117-
*/
118-
virtual int gethostbyname(const char *host, SocketAddress *address);
119-
120106
/** Translates a hostname to an IP address with specific version
121107
*
122108
* The hostname may be either a domain name or an IP address. If the
@@ -127,10 +113,11 @@ class NetworkInterface {
127113
*
128114
* @param address Destination for the host SocketAddress
129115
* @param host Hostname to resolve
130-
* @param version IP version of address to resolve
116+
* @param version IP version of address to resolve, NSAPI_UNSPEC indicates
117+
* version is chosen by the stack (defaults to NSAPI_UNSPEC)
131118
* @return 0 on success, negative error code on failure
132119
*/
133-
virtual int gethostbyname(const char *host, SocketAddress *address, nsapi_version_t version);
120+
virtual int gethostbyname(const char *host, SocketAddress *address, nsapi_version_t version = NSAPI_UNSPEC);
134121

135122
/** Add a domain name server to list of servers to query
136123
*

features/netsocket/NetworkStack.cpp

Lines changed: 1 addition & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -22,16 +22,6 @@
2222

2323

2424
// Default NetworkStack operations
25-
int NetworkStack::gethostbyname(const char *name, SocketAddress *address)
26-
{
27-
// check for simple ip addresses
28-
if (address->set_ip_address(name)) {
29-
return 0;
30-
}
31-
32-
return nsapi_dns_query(this, name, address);
33-
}
34-
3525
int NetworkStack::gethostbyname(const char *name, SocketAddress *address, nsapi_version_t version)
3626
{
3727
// check for simple ip addresses
@@ -100,25 +90,13 @@ class NetworkStackWrapper : public NetworkStack
10090
return address->get_ip_address();
10191
}
10292

103-
virtual int gethostbyname(const char *name, SocketAddress *address)
104-
{
105-
if (!_stack_api()->gethostbyname) {
106-
return NetworkStack::gethostbyname(name, address);
107-
}
108-
109-
nsapi_addr_t addr = {NSAPI_IPv4, 0};
110-
int err = _stack_api()->gethostbyname(_stack(), name, &addr, NSAPI_UNSPEC);
111-
address->set_addr(addr);
112-
return err;
113-
}
114-
11593
virtual int gethostbyname(const char *name, SocketAddress *address, nsapi_version_t version)
11694
{
11795
if (!_stack_api()->gethostbyname) {
11896
return NetworkStack::gethostbyname(name, address, version);
11997
}
12098

121-
nsapi_addr_t addr = {NSAPI_IPv4, 0};
99+
nsapi_addr_t addr = {NSAPI_UNSPEC, 0};
122100
int err = _stack_api()->gethostbyname(_stack(), name, &addr, version);
123101
address->set_addr(addr);
124102
return err;

features/netsocket/NetworkStack.h

Lines changed: 3 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -44,20 +44,6 @@ class NetworkStack
4444
*/
4545
virtual const char *get_ip_address() = 0;
4646

47-
/** Translates a hostname to an IP address
48-
*
49-
* The hostname may be either a domain name or an IP address. If the
50-
* hostname is an IP address, no network transactions will be performed.
51-
*
52-
* If no stack-specific DNS resolution is provided, the hostname
53-
* will be resolve using a UDP socket on the stack.
54-
*
55-
* @param host Hostname to resolve
56-
* @param address Destination for the host SocketAddress
57-
* @return 0 on success, negative error code on failure
58-
*/
59-
virtual int gethostbyname(const char *host, SocketAddress *address);
60-
6147
/** Translates a hostname to an IP address with specific version
6248
*
6349
* The hostname may be either a domain name or an IP address. If the
@@ -68,10 +54,11 @@ class NetworkStack
6854
*
6955
* @param host Hostname to resolve
7056
* @param address Destination for the host SocketAddress
71-
* @param version IP version of address to resolve
57+
* @param version IP version of address to resolve, NSAPI_UNSPEC indicates
58+
* version is chosen by the stack (defaults to NSAPI_UNSPEC)
7259
* @return 0 on success, negative error code on failure
7360
*/
74-
virtual int gethostbyname(const char *host, SocketAddress *address, nsapi_version_t version);
61+
virtual int gethostbyname(const char *host, SocketAddress *address, nsapi_version_t version = NSAPI_UNSPEC);
7562

7663
/** Add a domain name server to list of servers to query
7764
*

features/netsocket/SocketAddress.cpp

Lines changed: 29 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -221,17 +221,19 @@ void SocketAddress::set_port(uint16_t port)
221221

222222
const char *SocketAddress::get_ip_address() const
223223
{
224-
char *ip_address = (char *)_ip_address;
224+
if (_addr.version == NSAPI_UNSPEC) {
225+
return NULL;
226+
}
225227

226-
if (!ip_address[0]) {
228+
if (!_ip_address[0]) {
227229
if (_addr.version == NSAPI_IPv4) {
228-
ipv4_to_address(ip_address, _addr.bytes);
230+
ipv4_to_address(_ip_address, _addr.bytes);
229231
} else if (_addr.version == NSAPI_IPv6) {
230-
ipv6_to_address(ip_address, _addr.bytes);
232+
ipv6_to_address(_ip_address, _addr.bytes);
231233
}
232234
}
233235

234-
return ip_address;
236+
return _ip_address;
235237
}
236238

237239
const void *SocketAddress::get_ip_bytes() const
@@ -256,34 +258,38 @@ uint16_t SocketAddress::get_port() const
256258

257259
SocketAddress::operator bool() const
258260
{
259-
int count = 0;
260261
if (_addr.version == NSAPI_IPv4) {
261-
count = NSAPI_IPv4_BYTES;
262-
} else if (_addr.version == NSAPI_IPv6) {
263-
count = NSAPI_IPv6_BYTES;
264-
}
262+
for (int i = 0; i < NSAPI_IPv4_BYTES; i++) {
263+
if (_addr.bytes[i]) {
264+
return true;
265+
}
266+
}
265267

266-
for (int i = 0; i < count; i++) {
267-
if (_addr.bytes[i]) {
268-
return true;
268+
return false;
269+
} else if (_addr.version == NSAPI_IPv6) {
270+
for (int i = 0; i < NSAPI_IPv6_BYTES; i++) {
271+
if (_addr.bytes[i]) {
272+
return true;
273+
}
269274
}
270-
}
271275

272-
return false;
276+
return false;
277+
} else {
278+
return false;
279+
}
273280
}
274281

275282
bool operator==(const SocketAddress &a, const SocketAddress &b)
276283
{
277-
int count = 0;
278-
if (a._addr.version == NSAPI_IPv4 && b._addr.version == NSAPI_IPv4) {
279-
count = NSAPI_IPv4_BYTES;
280-
} else if (a._addr.version == NSAPI_IPv6 && b._addr.version == NSAPI_IPv6) {
281-
count = NSAPI_IPv6_BYTES;
282-
} else {
284+
if (!a && !b) {
285+
return true;
286+
} else if (a._addr.version != b._addr.version) {
283287
return false;
288+
} else if (a._addr.version == NSAPI_IPv4) {
289+
return memcmp(a._addr.bytes, b._addr.bytes, NSAPI_IPv4_BYTES) == 0;
290+
} else if (a._addr.version == NSAPI_IPv6) {
291+
return memcmp(a._addr.bytes, b._addr.bytes, NSAPI_IPv6_BYTES) == 0;
284292
}
285-
286-
return (memcmp(a._addr.bytes, b._addr.bytes, count) == 0);
287293
}
288294

289295
bool operator!=(const SocketAddress &a, const SocketAddress &b)

features/netsocket/SocketAddress.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,7 @@ class SocketAddress {
163163
private:
164164
void _SocketAddress(NetworkStack *iface, const char *host, uint16_t port);
165165

166-
char _ip_address[NSAPI_IP_SIZE];
166+
mutable char _ip_address[NSAPI_IP_SIZE];
167167
nsapi_addr_t _addr;
168168
uint16_t _port;
169169
};

features/netsocket/nsapi_dns.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ static void dns_append_question(uint8_t **p, const char *host, nsapi_version_t v
102102
dns_append_byte(p, 0);
103103

104104
// fill out question footer
105-
if (version == NSAPI_IPv4) {
105+
if (version != NSAPI_IPv6) {
106106
dns_append_word(p, RR_A); // qtype = ipv4
107107
} else {
108108
dns_append_word(p, RR_AAAA); // qtype = ipv6

features/netsocket/nsapi_types.h

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -103,16 +103,18 @@ typedef enum nsapi_security {
103103
* @enum nsapi_version_t
104104
*/
105105
typedef enum nsapi_version {
106-
NSAPI_IPv4, /*!< Address is IPv4 */
107-
NSAPI_IPv6, /*!< Address is IPv6 */
108-
NSAPI_UNSPEC /*!< Address is unspecified */
106+
NSAPI_UNSPEC, /*!< Address is unspecified */
107+
NSAPI_IPv4, /*!< Address is IPv4 */
108+
NSAPI_IPv6, /*!< Address is IPv6 */
109109
} nsapi_version_t;
110110

111111
/** IP address structure for passing IP addresses by value
112112
*/
113113
typedef struct nsapi_addr {
114114
/** IP version
115-
* NSAPI_IPv4 or NSAPI_IPv6 (NSAPI_UNSPEC not currently supported)
115+
* - NSAPI_IPv4
116+
* - NSAPI_IPv6
117+
* - NSAPI_UNSPEC
116118
*/
117119
nsapi_version_t version;
118120

0 commit comments

Comments
 (0)