@@ -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,14 @@ 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
+ }
248
+
238
249
bool operator ==(const SocketAddress &a, const SocketAddress &b)
239
250
{
240
251
if (!a && !b) {
@@ -257,7 +268,7 @@ bool operator!=(const SocketAddress &a, const SocketAddress &b)
257
268
258
269
void SocketAddress::_SocketAddress (NetworkStack *iface, const char *host, uint16_t port)
259
270
{
260
- _ip_address[ 0 ] = ' \0 ' ;
271
+ _ip_address = NULL ;
261
272
262
273
// gethostbyname must check for literals, so can call it directly
263
274
int err = iface->gethostbyname (host, this );
@@ -267,3 +278,8 @@ void SocketAddress::_SocketAddress(NetworkStack *iface, const char *host, uint16
267
278
_port = 0 ;
268
279
}
269
280
}
281
+
282
+ SocketAddress::~SocketAddress ()
283
+ {
284
+ delete[] _ip_address;
285
+ }
0 commit comments