|
| 1 | +// This file is part of the CircuitPython project: https://circuitpython.org |
| 2 | +// |
| 3 | +// SPDX-FileCopyrightText: Copyright (c) 2025 Dan Halbert for Adafruit Industries |
| 4 | +// |
| 5 | +// SPDX-License-Identifier: MIT |
| 6 | + |
| 7 | +#include "shared-bindings/socketpool/SocketPool.h" |
| 8 | +#include "common-hal/socketpool/Socket.h" |
| 9 | + |
| 10 | +#include "py/runtime.h" |
| 11 | +#include "shared-bindings/wifi/__init__.h" |
| 12 | +#include "common-hal/socketpool/__init__.h" |
| 13 | + |
| 14 | +void common_hal_socketpool_socketpool_construct(socketpool_socketpool_obj_t *self, mp_obj_t radio) { |
| 15 | + // if (radio != MP_OBJ_FROM_PTR(&common_hal_wifi_radio_obj)) { |
| 16 | + // mp_raise_ValueError(MP_ERROR_TEXT("SocketPool can only be used with wifi.radio")); |
| 17 | + // } |
| 18 | +} |
| 19 | + |
| 20 | +// common_hal_socketpool_socket is in socketpool/Socket.c to centralize open socket tracking. |
| 21 | + |
| 22 | +// int socketpool_getaddrinfo_common(const char *host, int service, const struct addrinfo *hints, struct addrinfo **res) { |
| 23 | +// // As of 2022, the version of lwip in esp-idf does not handle the |
| 24 | +// // trailing-dot syntax of domain names, so emulate it. |
| 25 | +// // Remove this once https://github.com/espressif/esp-idf/issues/10013 has |
| 26 | +// // been implemented |
| 27 | +// if (host) { |
| 28 | +// size_t strlen_host = strlen(host); |
| 29 | +// if (strlen_host && host[strlen_host - 1] == '.') { |
| 30 | +// mp_obj_t nodot = mp_obj_new_str(host, strlen_host - 1); |
| 31 | +// host = mp_obj_str_get_str(nodot); |
| 32 | +// } |
| 33 | +// } |
| 34 | + |
| 35 | +// char service_buf[6]; |
| 36 | +// snprintf(service_buf, sizeof(service_buf), "%d", service); |
| 37 | + |
| 38 | +// return lwip_getaddrinfo(host, service_buf, hints, res); |
| 39 | +// } |
| 40 | + |
| 41 | +// static mp_obj_t format_address(const struct sockaddr *addr, int family) { |
| 42 | +// char ip_str[IPADDR_STRLEN_MAX]; // big enough for any supported address type |
| 43 | +// const struct sockaddr_in *a = (void *)addr; |
| 44 | + |
| 45 | +// switch (family) { |
| 46 | +// #if CIRCUITPY_SOCKETPOOL_IPV6 |
| 47 | +// case AF_INET6: |
| 48 | +// inet_ntop(family, &((const struct sockaddr_in6 *)a)->sin6_addr, ip_str, sizeof(ip_str)); |
| 49 | +// break; |
| 50 | +// #endif |
| 51 | +// default: |
| 52 | +// case AF_INET: |
| 53 | +// inet_ntop(family, &((const struct sockaddr_in *)a)->sin_addr, ip_str, sizeof(ip_str)); |
| 54 | +// break; |
| 55 | +// } |
| 56 | +// return mp_obj_new_str(ip_str, strlen(ip_str)); |
| 57 | +// return mp_const_none; |
| 58 | +// } |
| 59 | + |
| 60 | +// static mp_obj_t convert_sockaddr(const struct addrinfo *ai, int port) { |
| 61 | +// // #if CIRCUITPY_SOCKETPOOL_IPV6 |
| 62 | +// // mp_int_t n_tuple = ai->ai_family == AF_INET6 ? 4 : 2; |
| 63 | +// // #else |
| 64 | +// // mp_int_t n_tuple = 2; |
| 65 | +// // #endif |
| 66 | +// // mp_obj_tuple_t *result = MP_OBJ_TO_PTR(mp_obj_new_tuple(n_tuple, NULL)); |
| 67 | +// // result->items[0] = format_address(ai->ai_addr, ai->ai_family); |
| 68 | +// // result->items[1] = MP_OBJ_NEW_SMALL_INT(port); |
| 69 | +// // #if CIRCUITPY_SOCKETPOOL_IPV6 |
| 70 | +// // if (ai->ai_family == AF_INET6) { |
| 71 | +// // const struct sockaddr_in6 *ai6 = (void *)ai->ai_addr; |
| 72 | +// // result->items[2] = MP_OBJ_NEW_SMALL_INT(ai6->sin6_flowinfo); |
| 73 | +// // result->items[3] = MP_OBJ_NEW_SMALL_INT(ai6->sin6_scope_id); |
| 74 | +// // } |
| 75 | +// // #endif |
| 76 | +// // return result; |
| 77 | +// return mp_const_none; |
| 78 | +// } |
| 79 | + |
| 80 | +// static mp_obj_t convert_addrinfo(const struct addrinfo *ai, int port) { |
| 81 | +// // MP_STATIC_ASSERT(AF_INET == SOCKETPOOL_AF_INET); |
| 82 | +// // #if CIRCUITPY_SOCKETPOOL_IPV6 |
| 83 | +// // MP_STATIC_ASSERT(AF_INET6 == SOCKETPOOL_AF_INET6); |
| 84 | +// // #endif |
| 85 | +// // // MP_STATIC_ASSERT(AF_UNSPEC == SOCKETPOOL_AF_UNSPEC); |
| 86 | +// // mp_obj_tuple_t *result = MP_OBJ_TO_PTR(mp_obj_new_tuple(5, NULL)); |
| 87 | +// // result->items[0] = MP_OBJ_NEW_SMALL_INT(ai->ai_family); |
| 88 | +// // result->items[1] = MP_OBJ_NEW_SMALL_INT(ai->ai_socktype); |
| 89 | +// // result->items[2] = MP_OBJ_NEW_SMALL_INT(ai->ai_protocol); |
| 90 | +// // result->items[3] = ai->ai_canonname ? mp_obj_new_str(ai->ai_canonname, strlen(ai->ai_canonname)) : MP_OBJ_NEW_QSTR(MP_QSTR_); |
| 91 | +// // result->items[4] = convert_sockaddr(ai, port); |
| 92 | +// // return result; |
| 93 | +// return mp_const_none; |
| 94 | +// } |
| 95 | + |
| 96 | +mp_obj_t common_hal_socketpool_getaddrinfo_raise(socketpool_socketpool_obj_t *self, const char *host, int port, int family, int type, int proto, int flags) { |
| 97 | + // const struct addrinfo hints = { |
| 98 | + // .ai_flags = flags, |
| 99 | + // .ai_family = family, |
| 100 | + // .ai_protocol = proto, |
| 101 | + // .ai_socktype = type, |
| 102 | + // }; |
| 103 | + // |
| 104 | + // struct addrinfo *res = NULL; |
| 105 | + // int err = socketpool_getaddrinfo_common(host, port, &hints, &res); |
| 106 | + // if (err != 0 || res == NULL) { |
| 107 | + // common_hal_socketpool_socketpool_raise_gaierror_noname(); |
| 108 | + // } |
| 109 | + // |
| 110 | + // nlr_buf_t nlr; |
| 111 | + // if (nlr_push(&nlr) == 0) { |
| 112 | + // mp_obj_t result = mp_obj_new_list(0, NULL); |
| 113 | + // for (struct addrinfo *ai = res; ai; ai = ai->ai_next) { |
| 114 | + // mp_obj_list_append(result, convert_addrinfo(ai, port)); |
| 115 | + // } |
| 116 | + // nlr_pop(); |
| 117 | + // lwip_freeaddrinfo(res); |
| 118 | + // return result; |
| 119 | + // } else { |
| 120 | + // lwip_freeaddrinfo(res); |
| 121 | + // nlr_raise(MP_OBJ_FROM_PTR(nlr.ret_val)); |
| 122 | + // } |
| 123 | + return mp_const_none; |
| 124 | +} |
0 commit comments