@@ -153,24 +153,31 @@ SocketAddress::SocketAddress(NetworkInterface *iface, const char *host, uint16_t
153
153
_SocketAddress (iface->get_stack (), host, port);
154
154
}
155
155
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
+
156
163
SocketAddress::SocketAddress (const char *addr, uint16_t port)
157
164
{
158
- memset (& _ip_address, 0 , sizeof _ip_address) ;
165
+ _ip_address[ 0 ] = ' \0 ' ;
159
166
set_ip_address (addr);
160
167
set_port (port);
161
168
}
162
169
163
170
SocketAddress::SocketAddress (const void *bytes, nsapi_version_t version, uint16_t port)
164
171
{
165
- memset (& _ip_address, 0 , sizeof _ip_address) ;
172
+ _ip_address[ 0 ] = ' \0 ' ;
166
173
set_ip_bytes (bytes, version);
167
174
set_port (port);
168
175
}
169
176
170
177
SocketAddress::SocketAddress (const SocketAddress &addr)
171
178
{
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 ());
174
181
set_port (addr.get_port ());
175
182
}
176
183
@@ -179,31 +186,28 @@ void SocketAddress::set_ip_address(const char *addr)
179
186
_ip_address[0 ] = ' \0 ' ;
180
187
181
188
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);
184
191
} 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);
187
194
} else {
188
- _ip_version = NSAPI_IPv4;
189
- memset (_ip_bytes, 0 , NSAPI_IPv4_BYTES);
195
+ _addr = (nsapi_addr_t ){};
190
196
}
191
197
}
192
198
193
199
void SocketAddress::set_ip_bytes (const void *bytes, nsapi_version_t version)
194
200
{
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
+ }
196
206
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;
207
211
}
208
212
209
213
void SocketAddress::set_port (uint16_t port)
@@ -216,10 +220,10 @@ const char *SocketAddress::get_ip_address() const
216
220
char *ip_address = (char *)_ip_address;
217
221
218
222
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 );
223
227
}
224
228
}
225
229
@@ -228,12 +232,17 @@ const char *SocketAddress::get_ip_address() const
228
232
229
233
const void *SocketAddress::get_ip_bytes () const
230
234
{
231
- return _ip_bytes ;
235
+ return _addr. bytes ;
232
236
}
233
237
234
238
nsapi_version_t SocketAddress::get_ip_version () const
235
239
{
236
- return _ip_version;
240
+ return _addr.version ;
241
+ }
242
+
243
+ nsapi_addr_t SocketAddress::get_addr () const
244
+ {
245
+ return _addr;
237
246
}
238
247
239
248
uint16_t SocketAddress::get_port () const
@@ -244,14 +253,14 @@ uint16_t SocketAddress::get_port() const
244
253
SocketAddress::operator bool () const
245
254
{
246
255
int count = 0 ;
247
- if (_ip_version == NSAPI_IPv4) {
256
+ if (_addr. version == NSAPI_IPv4) {
248
257
count = NSAPI_IPv4_BYTES;
249
- } else if (_ip_version == NSAPI_IPv6) {
258
+ } else if (_addr. version == NSAPI_IPv6) {
250
259
count = NSAPI_IPv6_BYTES;
251
260
}
252
261
253
262
for (int i = 0 ; i < count; i++) {
254
- if (_ip_bytes [i]) {
263
+ if (_addr. bytes [i]) {
255
264
return true ;
256
265
}
257
266
}
@@ -261,26 +270,23 @@ SocketAddress::operator bool() const
261
270
262
271
void SocketAddress::_SocketAddress (NetworkStack *iface, const char *host, uint16_t port)
263
272
{
264
- memset (& _ip_address, 0 , sizeof _ip_address) ;
273
+ _ip_address[ 0 ] = ' \0 ' ;
265
274
266
275
// Check for valid IP addresses
267
276
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;
271
280
} 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;
275
284
} else {
276
285
// DNS lookup
277
286
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 ;
284
290
}
285
291
}
286
292
}
0 commit comments