@@ -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+
156163SocketAddress::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
163170SocketAddress::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
170177SocketAddress::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
193199void 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
209213void 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
229233const void *SocketAddress::get_ip_bytes () const
230234{
231- return _ip_bytes ;
235+ return _addr. bytes ;
232236}
233237
234238nsapi_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
239248uint16_t SocketAddress::get_port () const
@@ -244,14 +253,14 @@ uint16_t SocketAddress::get_port() const
244253SocketAddress::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
262271void 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}
0 commit comments