@@ -106,35 +106,36 @@ static void ipv6_to_address(char *addr, const uint8_t *bytes)
106
106
107
107
SocketAddress::SocketAddress (nsapi_addr_t addr, uint16_t port)
108
108
{
109
- _ip_address[ 0 ] = ' \0 ' ;
109
+ _ip_address = NULL ;
110
110
set_addr (addr);
111
111
set_port (port);
112
112
}
113
113
114
114
SocketAddress::SocketAddress (const char *addr, uint16_t port)
115
115
{
116
- _ip_address[ 0 ] = ' \0 ' ;
116
+ _ip_address = NULL ;
117
117
set_ip_address (addr);
118
118
set_port (port);
119
119
}
120
120
121
121
SocketAddress::SocketAddress (const void *bytes, nsapi_version_t version, uint16_t port)
122
122
{
123
- _ip_address[ 0 ] = ' \0 ' ;
123
+ _ip_address = NULL ;
124
124
set_ip_bytes (bytes, version);
125
125
set_port (port);
126
126
}
127
127
128
128
SocketAddress::SocketAddress (const SocketAddress &addr)
129
129
{
130
- _ip_address[ 0 ] = ' \0 ' ;
130
+ _ip_address = NULL ;
131
131
set_addr (addr.get_addr ());
132
132
set_port (addr.get_port ());
133
133
}
134
134
135
135
bool SocketAddress::set_ip_address (const char *addr)
136
136
{
137
- _ip_address[0 ] = ' \0 ' ;
137
+ delete[] _ip_address;
138
+ _ip_address = NULL ;
138
139
139
140
if (addr && ipv4_is_valid (addr)) {
140
141
_addr.version = NSAPI_IPv4;
@@ -166,7 +167,8 @@ void SocketAddress::set_ip_bytes(const void *bytes, nsapi_version_t version)
166
167
167
168
void SocketAddress::set_addr (nsapi_addr_t addr)
168
169
{
169
- _ip_address[0 ] = ' \0 ' ;
170
+ delete[] _ip_address;
171
+ _ip_address = NULL ;
170
172
_addr = addr;
171
173
}
172
174
@@ -181,7 +183,8 @@ const char *SocketAddress::get_ip_address() const
181
183
return NULL ;
182
184
}
183
185
184
- if (!_ip_address[0 ]) {
186
+ if (!_ip_address) {
187
+ _ip_address = new char [NSAPI_IP_SIZE];
185
188
if (_addr.version == NSAPI_IPv4) {
186
189
ipv4_to_address (_ip_address, _addr.bytes );
187
190
} else if (_addr.version == NSAPI_IPv6) {
@@ -235,6 +238,15 @@ SocketAddress::operator bool() const
235
238
}
236
239
}
237
240
241
+ SocketAddress &SocketAddress::operator =(const SocketAddress &addr)
242
+ {
243
+ delete[] _ip_address;
244
+ _ip_address = NULL ;
245
+ set_addr (addr.get_addr ());
246
+ set_port (addr.get_port ());
247
+ return *this ;
248
+ }
249
+
238
250
bool operator ==(const SocketAddress &a, const SocketAddress &b)
239
251
{
240
252
if (!a && !b) {
@@ -257,7 +269,7 @@ bool operator!=(const SocketAddress &a, const SocketAddress &b)
257
269
258
270
void SocketAddress::_SocketAddress (NetworkStack *iface, const char *host, uint16_t port)
259
271
{
260
- _ip_address[ 0 ] = ' \0 ' ;
272
+ _ip_address = NULL ;
261
273
262
274
// gethostbyname must check for literals, so can call it directly
263
275
int err = iface->gethostbyname (host, this );
@@ -267,3 +279,8 @@ void SocketAddress::_SocketAddress(NetworkStack *iface, const char *host, uint16
267
279
_port = 0 ;
268
280
}
269
281
}
282
+
283
+ SocketAddress::~SocketAddress ()
284
+ {
285
+ delete[] _ip_address;
286
+ }
0 commit comments