1111#include "shared-bindings/ipaddress/IPv4Address.h"
1212#include "shared-bindings/wifi/Monitor.h"
1313#include "shared-bindings/wifi/Radio.h"
14+ #include "common-hal/socketpool/__init__.h"
1415
1516#include "py/gc.h"
1617#include "py/mpstate.h"
@@ -234,14 +235,14 @@ void wifi_reset(void) {
234235}
235236
236237void 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 );
239245 }
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 ]);
245246}
246247
247248void 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) {
288289 return mp_obj_new_str (buf , strlen (buf ));
289290}
290291
292+
291293void 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 ));
292299 if (sockaddr -> ss_family == AF_INET6 ) {
293300 const struct sockaddr_in6 * addr6 = (const void * )sockaddr ;
294301 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
306313void espaddr_to_sockaddr (const esp_ip_addr_t * espaddr , struct sockaddr_storage * sockaddr , int port ) {
307314 if (espaddr -> type == ESP_IPADDR_TYPE_V6 ) {
308315 struct sockaddr_in6 * addr6 = (void * )sockaddr ;
309- MP_STATIC_ASSERT (sizeof (espaddr -> u_addr .ip6 .addr ) == sizeof (addr6 -> sin6_addr ));
310316 memcpy (& addr6 -> sin6_addr , & espaddr -> u_addr .ip6 .addr , sizeof (espaddr -> u_addr .ip6 .addr ));
311317 addr6 -> sin6_scope_id = espaddr -> u_addr .ip6 .zone ;
312318 } else {
313319 struct sockaddr_in * addr = (void * )sockaddr ;
314- MP_STATIC_ASSERT (sizeof (espaddr -> u_addr .ip4 .addr ) == sizeof (addr -> sin_addr ));
315320 memcpy (& addr -> sin_addr , & espaddr -> u_addr .ip4 .addr , sizeof (espaddr -> u_addr .ip4 .addr ));
316321 }
317322}
0 commit comments