Skip to content

Commit 1f4eb0a

Browse files
committed
nsapi - Standardized support of NSAPI_UNSPEC
- Reordered nsapi_version_t to make defaule nsapi_addr_t NSAPI_UNSPEC - Added support to NSAPI_UNSPEC in SocketAddress
1 parent 5a55b39 commit 1f4eb0a

File tree

8 files changed

+44
-89
lines changed

8 files changed

+44
-89
lines changed

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
@@ -100,20 +100,6 @@ class NetworkInterface {
100100
*/
101101
virtual int disconnect() = 0;
102102

103-
/** Translates a hostname to an IP address
104-
*
105-
* The hostname may be either a domain name or an IP address. If the
106-
* hostname is an IP address, no network transactions will be performed.
107-
*
108-
* If no stack-specific DNS resolution is provided, the hostname
109-
* will be resolve using a UDP socket on the stack.
110-
*
111-
* @param address Destination for the host SocketAddress
112-
* @param host Hostname to resolve
113-
* @return 0 on success, negative error code on failure
114-
*/
115-
virtual int gethostbyname(const char *host, SocketAddress *address);
116-
117103
/** Translates a hostname to an IP address with specific version
118104
*
119105
* The hostname may be either a domain name or an IP address. If the
@@ -124,10 +110,11 @@ class NetworkInterface {
124110
*
125111
* @param address Destination for the host SocketAddress
126112
* @param host Hostname to resolve
127-
* @param version IP version of address to resolve
113+
* @param version IP version of address to resolve, NSAPI_UNSPEC indicates
114+
* version is chosen by the stack (defaults to NSAPI_UNSPEC)
128115
* @return 0 on success, negative error code on failure
129116
*/
130-
virtual int gethostbyname(const char *host, SocketAddress *address, nsapi_version_t version);
117+
virtual int gethostbyname(const char *host, SocketAddress *address, nsapi_version_t version = NSAPI_UNSPEC);
131118

132119
/** Add a domain name server to list of servers to query
133120
*

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
@@ -41,20 +41,6 @@ class NetworkStack
4141
*/
4242
virtual const char *get_ip_address() = 0;
4343

44-
/** Translates a hostname to an IP address
45-
*
46-
* The hostname may be either a domain name or an IP address. If the
47-
* hostname is an IP address, no network transactions will be performed.
48-
*
49-
* If no stack-specific DNS resolution is provided, the hostname
50-
* will be resolve using a UDP socket on the stack.
51-
*
52-
* @param host Hostname to resolve
53-
* @param address Destination for the host SocketAddress
54-
* @return 0 on success, negative error code on failure
55-
*/
56-
virtual int gethostbyname(const char *host, SocketAddress *address);
57-
5844
/** Translates a hostname to an IP address with specific version
5945
*
6046
* The hostname may be either a domain name or an IP address. If the
@@ -65,10 +51,11 @@ class NetworkStack
6551
*
6652
* @param host Hostname to resolve
6753
* @param address Destination for the host SocketAddress
68-
* @param version IP version of address to resolve
54+
* @param version IP version of address to resolve, NSAPI_UNSPEC indicates
55+
* version is chosen by the stack (defaults to NSAPI_UNSPEC)
6956
* @return 0 on success, negative error code on failure
7057
*/
71-
virtual int gethostbyname(const char *host, SocketAddress *address, nsapi_version_t version);
58+
virtual int gethostbyname(const char *host, SocketAddress *address, nsapi_version_t version = NSAPI_UNSPEC);
7259

7360
/** Add a domain name server to list of servers to query
7461
*

features/netsocket/SocketAddress.cpp

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

216216
const char *SocketAddress::get_ip_address() const
217217
{
218-
char *ip_address = (char *)_ip_address;
218+
if (_addr.version == NSAPI_UNSPEC) {
219+
return NULL;
220+
}
219221

220-
if (!ip_address[0]) {
222+
if (!_ip_address[0]) {
221223
if (_addr.version == NSAPI_IPv4) {
222-
ipv4_to_address(ip_address, _addr.bytes);
224+
ipv4_to_address(_ip_address, _addr.bytes);
223225
} else if (_addr.version == NSAPI_IPv6) {
224-
ipv6_to_address(ip_address, _addr.bytes);
226+
ipv6_to_address(_ip_address, _addr.bytes);
225227
}
226228
}
227229

228-
return ip_address;
230+
return _ip_address;
229231
}
230232

231233
const void *SocketAddress::get_ip_bytes() const
@@ -250,34 +252,38 @@ uint16_t SocketAddress::get_port() const
250252

251253
SocketAddress::operator bool() const
252254
{
253-
int count = 0;
254255
if (_addr.version == NSAPI_IPv4) {
255-
count = NSAPI_IPv4_BYTES;
256-
} else if (_addr.version == NSAPI_IPv6) {
257-
count = NSAPI_IPv6_BYTES;
258-
}
256+
for (int i = 0; i < NSAPI_IPv4_BYTES; i++) {
257+
if (_addr.bytes[i]) {
258+
return true;
259+
}
260+
}
259261

260-
for (int i = 0; i < count; i++) {
261-
if (_addr.bytes[i]) {
262-
return true;
262+
return false;
263+
} else if (_addr.version == NSAPI_IPv6) {
264+
for (int i = 0; i < NSAPI_IPv6_BYTES; i++) {
265+
if (_addr.bytes[i]) {
266+
return true;
267+
}
263268
}
264-
}
265269

266-
return false;
270+
return false;
271+
} else {
272+
return false;
273+
}
267274
}
268275

269276
bool operator==(const SocketAddress &a, const SocketAddress &b)
270277
{
271-
int count = 0;
272-
if (a._addr.version == NSAPI_IPv4 && b._addr.version == NSAPI_IPv4) {
273-
count = NSAPI_IPv4_BYTES;
274-
} else if (a._addr.version == NSAPI_IPv6 && b._addr.version == NSAPI_IPv6) {
275-
count = NSAPI_IPv6_BYTES;
276-
} else {
278+
if (!a && !b) {
279+
return true;
280+
} else if (a._addr.version != b._addr.version) {
277281
return false;
282+
} else if (a._addr.version == NSAPI_IPv4) {
283+
return memcmp(a._addr.bytes, b._addr.bytes, NSAPI_IPv4_BYTES) == 0;
284+
} else if (a._addr.version == NSAPI_IPv6) {
285+
return memcmp(a._addr.bytes, b._addr.bytes, NSAPI_IPv6_BYTES) == 0;
278286
}
279-
280-
return (memcmp(a._addr.bytes, b._addr.bytes, count) == 0);
281287
}
282288

283289
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
@@ -160,7 +160,7 @@ class SocketAddress {
160160
private:
161161
void _SocketAddress(NetworkStack *iface, const char *host, uint16_t port);
162162

163-
char _ip_address[NSAPI_IP_SIZE];
163+
mutable char _ip_address[NSAPI_IP_SIZE];
164164
nsapi_addr_t _addr;
165165
uint16_t _port;
166166
};

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
@@ -85,16 +85,18 @@ typedef enum nsapi_error {
8585
* @enum nsapi_version_t
8686
*/
8787
typedef enum nsapi_version {
88-
NSAPI_IPv4, /*!< Address is IPv4 */
89-
NSAPI_IPv6, /*!< Address is IPv6 */
90-
NSAPI_UNSPEC /*!< Address is unspecified */
88+
NSAPI_UNSPEC, /*!< Address is unspecified */
89+
NSAPI_IPv4, /*!< Address is IPv4 */
90+
NSAPI_IPv6, /*!< Address is IPv6 */
9191
} nsapi_version_t;
9292

9393
/** IP address structure for passing IP addresses by value
9494
*/
9595
typedef struct nsapi_addr {
9696
/** IP version
97-
* NSAPI_IPv4 or NSAPI_IPv6 (NSAPI_UNSPEC not currently supported)
97+
* - NSAPI_IPv4
98+
* - NSAPI_IPv6
99+
* - NSAPI_UNSPEC
98100
*/
99101
nsapi_version_t version;
100102

0 commit comments

Comments
 (0)