Skip to content

Commit d985d70

Browse files
committed
Added primitive nsapi_addr_t type
Provides primitive type for network address that is more constrained than "pointer to array of bytes". - POD type avoids issues with constructors - Removes complexity from SocketAddress class - Easily passed through C interfaces - Easily casted to SocketAddress and vice-versa
1 parent 97380a7 commit d985d70

File tree

3 files changed

+89
-49
lines changed

3 files changed

+89
-49
lines changed

SocketAddress.cpp

Lines changed: 49 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -153,24 +153,31 @@ SocketAddress::SocketAddress(NetworkInterface *iface, const char *host, uint16_t
153153
_SocketAddress(iface->get_stack(), host, port);
154154
}
155155

156+
SocketAddress::SocketAddress(nsapi_addr_t addr, uint16_t port)
157+
{
158+
_ip_address[0] = '\0';
159+
set_addr(addr);
160+
set_port(port);
161+
}
162+
156163
SocketAddress::SocketAddress(const char *addr, uint16_t port)
157164
{
158-
memset(&_ip_address, 0, sizeof _ip_address);
165+
_ip_address[0] = '\0';
159166
set_ip_address(addr);
160167
set_port(port);
161168
}
162169

163170
SocketAddress::SocketAddress(const void *bytes, nsapi_version_t version, uint16_t port)
164171
{
165-
memset(&_ip_address, 0, sizeof _ip_address);
172+
_ip_address[0] = '\0';
166173
set_ip_bytes(bytes, version);
167174
set_port(port);
168175
}
169176

170177
SocketAddress::SocketAddress(const SocketAddress &addr)
171178
{
172-
memset(&_ip_address, 0, sizeof _ip_address);
173-
set_ip_bytes(addr.get_ip_bytes(), addr.get_ip_version());
179+
_ip_address[0] = '\0';
180+
set_addr(addr.get_addr());
174181
set_port(addr.get_port());
175182
}
176183

@@ -179,31 +186,28 @@ void SocketAddress::set_ip_address(const char *addr)
179186
_ip_address[0] = '\0';
180187

181188
if (addr && ipv4_is_valid(addr)) {
182-
_ip_version = NSAPI_IPv4;
183-
ipv4_from_address(_ip_bytes, addr);
189+
_addr.version = NSAPI_IPv4;
190+
ipv4_from_address(_addr.bytes, addr);
184191
} else if (addr && ipv6_is_valid(addr)) {
185-
_ip_version = NSAPI_IPv6;
186-
ipv6_from_address(_ip_bytes, addr);
192+
_addr.version = NSAPI_IPv6;
193+
ipv6_from_address(_addr.bytes, addr);
187194
} else {
188-
_ip_version = NSAPI_IPv4;
189-
memset(_ip_bytes, 0, NSAPI_IPv4_BYTES);
195+
_addr = (nsapi_addr_t){};
190196
}
191197
}
192198

193199
void SocketAddress::set_ip_bytes(const void *bytes, nsapi_version_t version)
194200
{
195-
_ip_address[0] = '\0';
201+
nsapi_addr_t addr;
202+
addr.version = version;
203+
memcpy(addr.bytes, bytes, NSAPI_IP_BYTES);
204+
set_addr(addr);
205+
}
196206

197-
if (version == NSAPI_IPv4) {
198-
_ip_version = NSAPI_IPv4;
199-
memcpy(_ip_bytes, bytes, NSAPI_IPv4_BYTES);
200-
} else if (version == NSAPI_IPv6) {
201-
_ip_version = NSAPI_IPv6;
202-
memcpy(_ip_bytes, bytes, NSAPI_IPv6_BYTES);
203-
} else {
204-
_ip_version = NSAPI_IPv4;
205-
memset(_ip_bytes, 0, NSAPI_IPv4_BYTES);
206-
}
207+
void SocketAddress::set_addr(nsapi_addr_t addr)
208+
{
209+
_ip_address[0] = '\0';
210+
_addr = addr;
207211
}
208212

209213
void SocketAddress::set_port(uint16_t port)
@@ -216,10 +220,10 @@ const char *SocketAddress::get_ip_address() const
216220
char *ip_address = (char *)_ip_address;
217221

218222
if (!ip_address[0]) {
219-
if (_ip_version == NSAPI_IPv4) {
220-
ipv4_to_address(ip_address, _ip_bytes);
221-
} else if (_ip_version == NSAPI_IPv6) {
222-
ipv6_to_address(ip_address, _ip_bytes);
223+
if (_addr.version == NSAPI_IPv4) {
224+
ipv4_to_address(ip_address, _addr.bytes);
225+
} else if (_addr.version == NSAPI_IPv6) {
226+
ipv6_to_address(ip_address, _addr.bytes);
223227
}
224228
}
225229

@@ -228,12 +232,17 @@ const char *SocketAddress::get_ip_address() const
228232

229233
const void *SocketAddress::get_ip_bytes() const
230234
{
231-
return _ip_bytes;
235+
return _addr.bytes;
232236
}
233237

234238
nsapi_version_t SocketAddress::get_ip_version() const
235239
{
236-
return _ip_version;
240+
return _addr.version;
241+
}
242+
243+
nsapi_addr_t SocketAddress::get_addr() const
244+
{
245+
return _addr;
237246
}
238247

239248
uint16_t SocketAddress::get_port() const
@@ -244,14 +253,14 @@ uint16_t SocketAddress::get_port() const
244253
SocketAddress::operator bool() const
245254
{
246255
int count = 0;
247-
if (_ip_version == NSAPI_IPv4) {
256+
if (_addr.version == NSAPI_IPv4) {
248257
count = NSAPI_IPv4_BYTES;
249-
} else if (_ip_version == NSAPI_IPv6) {
258+
} else if (_addr.version == NSAPI_IPv6) {
250259
count = NSAPI_IPv6_BYTES;
251260
}
252261

253262
for (int i = 0; i < count; i++) {
254-
if (_ip_bytes[i]) {
263+
if (_addr.bytes[i]) {
255264
return true;
256265
}
257266
}
@@ -261,26 +270,23 @@ SocketAddress::operator bool() const
261270

262271
void SocketAddress::_SocketAddress(NetworkStack *iface, const char *host, uint16_t port)
263272
{
264-
memset(&_ip_address, 0, sizeof _ip_address);
273+
_ip_address[0] = '\0';
265274

266275
// Check for valid IP addresses
267276
if (host && ipv4_is_valid(host)) {
268-
_ip_version = NSAPI_IPv4;
269-
ipv4_from_address(_ip_bytes, host);
270-
set_port(port);
277+
_addr.version = NSAPI_IPv4;
278+
ipv4_from_address(_addr.bytes, host);
279+
_port = port;
271280
} else if (host && ipv6_is_valid(host)) {
272-
_ip_version = NSAPI_IPv6;
273-
ipv6_from_address(_ip_bytes, host);
274-
set_port(port);
281+
_addr.version = NSAPI_IPv6;
282+
ipv6_from_address(_addr.bytes, host);
283+
_port = port;
275284
} else {
276285
// DNS lookup
277286
int err = iface->gethostbyname(this, host);
278-
if (!err) {
279-
set_port(port);
280-
} else {
281-
_ip_version = NSAPI_IPv4;
282-
memset(_ip_bytes, 0, NSAPI_IPv4_BYTES);
283-
set_port(0);
287+
if (err) {
288+
_addr = (nsapi_addr_t){};
289+
_port = 0;
284290
}
285291
}
286292
}

SocketAddress.h

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
#define SOCKET_ADDRESS_H
1919

2020
#include "nsapi_types.h"
21+
#include "toolchain.h"
2122

2223
// Predeclared classes
2324
class NetworkStack;
@@ -56,14 +57,21 @@ class SocketAddress {
5657
*/
5758
SocketAddress(NetworkInterface *iface, const char *host, uint16_t port = 0);
5859

60+
/** Create a SocketAddress from a raw IP address and port
61+
*
62+
* @param addr Raw IP address
63+
* @param port Optional 16-bit port
64+
*/
65+
SocketAddress(nsapi_addr_t addr = (nsapi_addr_t){}, uint16_t port = 0);
66+
5967
/** Create a SocketAddress from an IP address and port
6068
*
6169
* @param host Null-terminated representation of the IP address
6270
* @param port Optional 16-bit port
6371
*/
64-
SocketAddress(const char *addr = 0, uint16_t port = 0);
72+
SocketAddress(const char *addr, uint16_t port = 0);
6573

66-
/** Create a SocketAddress from a raw IP address and port
74+
/** Create a SocketAddress from raw IP bytes, IP version, and port
6775
*
6876
* @param bytes Raw IP address in big-endian order
6977
* @param version IP address version, NSAPI_IPv4 or NSAPI_IPv6
@@ -83,13 +91,19 @@ class SocketAddress {
8391
*/
8492
void set_ip_address(const char *addr);
8593

86-
/** Set the raw IP address
94+
/** Set the raw IP bytes and IP version
8795
*
8896
* @param bytes Raw IP address in big-endian order
8997
* @param version IP address version, NSAPI_IPv4 or NSAPI_IPv6
9098
*/
9199
void set_ip_bytes(const void *bytes, nsapi_version_t version);
92100

101+
/** Set the raw IP address
102+
*
103+
* @param addr Raw IP address
104+
*/
105+
void set_addr(nsapi_addr_t addr);
106+
93107
/** Set the port
94108
*
95109
* @param port 16-bit port
@@ -102,7 +116,7 @@ class SocketAddress {
102116
*/
103117
const char *get_ip_address() const;
104118

105-
/** Get the raw IP address
119+
/* Get the raw IP bytes
106120
*
107121
* @return Raw IP address in big-endian order
108122
*/
@@ -113,6 +127,12 @@ class SocketAddress {
113127
* @return IP address version, NSAPI_IPv4 or NSAPI_IPv6
114128
*/
115129
nsapi_version_t get_ip_version() const;
130+
131+
/** Get the raw IP address
132+
*
133+
* @return Raw IP address
134+
*/
135+
nsapi_addr_t get_addr() const;
116136

117137
/** Get the port
118138
*
@@ -128,9 +148,9 @@ class SocketAddress {
128148

129149
private:
130150
void _SocketAddress(NetworkStack *iface, const char *host, uint16_t port);
151+
131152
char _ip_address[NSAPI_IP_SIZE];
132-
uint8_t _ip_bytes[NSAPI_IP_BYTES];
133-
nsapi_version_t _ip_version;
153+
nsapi_addr_t _addr;
134154
uint16_t _port;
135155
};
136156

nsapi_types.h

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,20 @@ typedef enum nsapi_version {
8989
NSAPI_IPv6, /*!< Address is IPv6 */
9090
} nsapi_version_t;
9191

92+
/** IP address structure for passing IP addresses by value
93+
*/
94+
typedef struct nsapi_addr {
95+
/** IP version
96+
* NSAPI_IPv4 or NSAPI_IPv6
97+
*/
98+
nsapi_version_t version;
99+
100+
/** IP address
101+
* The raw bytes of the IP address stored in big-endian format
102+
*/
103+
uint8_t bytes[NSAPI_IP_BYTES];
104+
} nsapi_addr_t;
105+
92106

93107
/** Enum of socket protocols
94108
*

0 commit comments

Comments
 (0)