|
15 | 15 | */ |
16 | 16 |
|
17 | 17 | #include "SocketAddress.h" |
| 18 | +#include "NetworkInterface.h" |
18 | 19 | #include "NetworkStack.h" |
19 | 20 | #include <string.h> |
20 | 21 | #include "mbed.h" |
@@ -144,28 +145,12 @@ static void ipv6_to_address(char *addr, const uint8_t *bytes) |
144 | 145 |
|
145 | 146 | SocketAddress::SocketAddress(NetworkStack *iface, const char *host, uint16_t port) |
146 | 147 | { |
147 | | - memset(&_ip_address, 0, sizeof _ip_address); |
| 148 | + _SocketAddress(iface, host, port); |
| 149 | +} |
148 | 150 |
|
149 | | - // Check for valid IP addresses |
150 | | - if (host && ipv4_is_valid(host)) { |
151 | | - _ip_version = NSAPI_IPv4; |
152 | | - ipv4_from_address(_ip_bytes, host); |
153 | | - set_port(port); |
154 | | - } else if (host && ipv6_is_valid(host)) { |
155 | | - _ip_version = NSAPI_IPv6; |
156 | | - ipv6_from_address(_ip_bytes, host); |
157 | | - set_port(port); |
158 | | - } else { |
159 | | - // DNS lookup |
160 | | - int err = iface->gethostbyname(this, host); |
161 | | - if (!err) { |
162 | | - set_port(port); |
163 | | - } else { |
164 | | - _ip_version = NSAPI_IPv4; |
165 | | - memset(_ip_bytes, 0, NSAPI_IPv4_BYTES); |
166 | | - set_port(0); |
167 | | - } |
168 | | - } |
| 151 | +SocketAddress::SocketAddress(NetworkInterface *iface, const char *host, uint16_t port) |
| 152 | +{ |
| 153 | + _SocketAddress(iface->get_stack(), host, port); |
169 | 154 | } |
170 | 155 |
|
171 | 156 | SocketAddress::SocketAddress(const char *addr, uint16_t port) |
@@ -273,3 +258,29 @@ SocketAddress::operator bool() const |
273 | 258 |
|
274 | 259 | return false; |
275 | 260 | } |
| 261 | + |
| 262 | +void SocketAddress::_SocketAddress(NetworkStack *iface, const char *host, uint16_t port) |
| 263 | +{ |
| 264 | + memset(&_ip_address, 0, sizeof _ip_address); |
| 265 | + |
| 266 | + // Check for valid IP addresses |
| 267 | + if (host && ipv4_is_valid(host)) { |
| 268 | + _ip_version = NSAPI_IPv4; |
| 269 | + ipv4_from_address(_ip_bytes, host); |
| 270 | + set_port(port); |
| 271 | + } else if (host && ipv6_is_valid(host)) { |
| 272 | + _ip_version = NSAPI_IPv6; |
| 273 | + ipv6_from_address(_ip_bytes, host); |
| 274 | + set_port(port); |
| 275 | + } else { |
| 276 | + // DNS lookup |
| 277 | + 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); |
| 284 | + } |
| 285 | + } |
| 286 | +} |
0 commit comments