11
11
#include "shared-bindings/ipaddress/IPv4Address.h"
12
12
#include "shared-bindings/wifi/Monitor.h"
13
13
#include "shared-bindings/wifi/Radio.h"
14
+ #include "common-hal/socketpool/__init__.h"
14
15
15
16
#include "py/gc.h"
16
17
#include "py/mpstate.h"
@@ -234,14 +235,14 @@ void wifi_reset(void) {
234
235
}
235
236
236
237
void ipaddress_ipaddress_to_esp_idf (mp_obj_t ip_address , ip_addr_t * esp_ip_address ) {
237
- if (!mp_obj_is_type (ip_address , & ipaddress_ipv4address_type )) {
238
- mp_raise_ValueError (MP_ERROR_TEXT ("Only IPv4 addresses supported" ));
238
+ if (mp_obj_is_type (ip_address , & ipaddress_ipv4address_type )) {
239
+ ipaddress_ipaddress_to_esp_idf_ip4 (ip_address , (esp_ip4_addr_t * )esp_ip_address );
240
+ esp_ip_address -> type = IPADDR_TYPE_V4 ;
241
+ } else {
242
+ struct sockaddr_storage addr_storage ;
243
+ socketpool_resolve_host_or_throw (AF_UNSPEC , SOCK_STREAM , mp_obj_str_get_str (ip_address ), & addr_storage , 1 );
244
+ sockaddr_to_espaddr (& addr_storage , (esp_ip_addr_t * )esp_ip_address );
239
245
}
240
- mp_obj_t packed = common_hal_ipaddress_ipv4address_get_packed (ip_address );
241
- size_t len ;
242
- const char * bytes = mp_obj_str_get_data (packed , & len );
243
-
244
- IP_ADDR4 (esp_ip_address , bytes [0 ], bytes [1 ], bytes [2 ], bytes [3 ]);
245
246
}
246
247
247
248
void ipaddress_ipaddress_to_esp_idf_ip4 (mp_obj_t ip_address , esp_ip4_addr_t * esp_ip_address ) {
@@ -288,7 +289,13 @@ mp_obj_t sockaddr_to_str(const struct sockaddr_storage *sockaddr) {
288
289
return mp_obj_new_str (buf , strlen (buf ));
289
290
}
290
291
292
+
291
293
void sockaddr_to_espaddr (const struct sockaddr_storage * sockaddr , esp_ip_addr_t * espaddr ) {
294
+ MP_STATIC_ASSERT (IPADDR_TYPE_V4 == ESP_IPADDR_TYPE_V4 );
295
+ MP_STATIC_ASSERT (IPADDR_TYPE_V6 == ESP_IPADDR_TYPE_V6 );
296
+ MP_STATIC_ASSERT (sizeof (ip_addr_t ) == sizeof (esp_ip_addr_t ));
297
+ MP_STATIC_ASSERT (offsetof(ip_addr_t , u_addr ) == offsetof(esp_ip_addr_t , u_addr ));
298
+ MP_STATIC_ASSERT (offsetof(ip_addr_t , type ) == offsetof(esp_ip_addr_t , type ));
292
299
if (sockaddr -> ss_family == AF_INET6 ) {
293
300
const struct sockaddr_in6 * addr6 = (const void * )sockaddr ;
294
301
MP_STATIC_ASSERT (sizeof (espaddr -> u_addr .ip6 .addr ) == sizeof (addr6 -> sin6_addr ));
@@ -306,12 +313,10 @@ void sockaddr_to_espaddr(const struct sockaddr_storage *sockaddr, esp_ip_addr_t
306
313
void espaddr_to_sockaddr (const esp_ip_addr_t * espaddr , struct sockaddr_storage * sockaddr , int port ) {
307
314
if (espaddr -> type == ESP_IPADDR_TYPE_V6 ) {
308
315
struct sockaddr_in6 * addr6 = (void * )sockaddr ;
309
- MP_STATIC_ASSERT (sizeof (espaddr -> u_addr .ip6 .addr ) == sizeof (addr6 -> sin6_addr ));
310
316
memcpy (& addr6 -> sin6_addr , & espaddr -> u_addr .ip6 .addr , sizeof (espaddr -> u_addr .ip6 .addr ));
311
317
addr6 -> sin6_scope_id = espaddr -> u_addr .ip6 .zone ;
312
318
} else {
313
319
struct sockaddr_in * addr = (void * )sockaddr ;
314
- MP_STATIC_ASSERT (sizeof (espaddr -> u_addr .ip4 .addr ) == sizeof (addr -> sin_addr ));
315
320
memcpy (& addr -> sin_addr , & espaddr -> u_addr .ip4 .addr , sizeof (espaddr -> u_addr .ip4 .addr ));
316
321
}
317
322
}
0 commit comments