|
8 | 8 | #include "common-hal/socketpool/Socket.h" |
9 | 9 |
|
10 | 10 | #include "py/runtime.h" |
| 11 | +#include "shared-bindings/ipaddress/IPv4Address.h" |
11 | 12 | #include "shared-bindings/wifi/__init__.h" |
12 | 13 | #include "common-hal/socketpool/__init__.h" |
13 | 14 |
|
14 | 15 | 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 | | - // } |
| 16 | + if (radio != MP_OBJ_FROM_PTR(&common_hal_wifi_radio_obj)) { |
| 17 | + mp_raise_ValueError(MP_ERROR_TEXT("SocketPool can only be used with wifi.radio")); |
| 18 | + } |
| 19 | + // Not really needed, but more convenient. |
| 20 | + self->radio = radio; |
18 | 21 | } |
19 | 22 |
|
20 | 23 | // common_hal_socketpool_socket is in socketpool/Socket.c to centralize open socket tracking. |
21 | 24 |
|
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 | | -// } |
| 25 | +bool socketpool_gethostbyname_ipv4(socketpool_socketpool_obj_t *self, const char *host, uint8_t ipv4[4]) { |
| 26 | + const uint8_t *req_host_params[1] = { (uint8_t *)host }; |
| 27 | + size_t req_host_param_lengths[1] = { strlen(host) }; |
| 28 | + |
| 29 | + uint8_t result = 0; |
| 30 | + uint8_t *req_host_responses[1] = { &result }; |
| 31 | + size_t req_host_response_lengths[1] = { 1 }; |
| 32 | + |
| 33 | + // If host is a numeric IP address, AirLift will just parse and return the address. |
| 34 | + |
| 35 | + size_t num_responses = wifi_radio_send_command_get_response(self->radio, REQ_HOST_BY_NAME_CMD, |
| 36 | + req_host_params, req_host_param_lengths, LENGTHS_8, MP_ARRAY_SIZE(req_host_params), |
| 37 | + req_host_responses, req_host_response_lengths, LENGTHS_8, MP_ARRAY_SIZE(req_host_responses), |
| 38 | + AIRLIFT_DEFAULT_TIMEOUT_MS); |
| 39 | + |
| 40 | + if (num_responses >= 1) { |
| 41 | + if (result == 0) { |
| 42 | + return false; |
| 43 | + } |
| 44 | + |
| 45 | + uint8_t *get_host_responses[1] = { ipv4 }; |
| 46 | + size_t get_host_response_lengths[1] = { IPV4_LENGTH }; |
| 47 | + |
| 48 | + // Now actually get the name. |
| 49 | + num_responses = wifi_radio_send_command_get_response(self->radio, GET_HOST_BY_NAME_CMD, |
| 50 | + NULL, NULL, LENGTHS_8, 0, |
| 51 | + get_host_responses, get_host_response_lengths, LENGTHS_8, MP_ARRAY_SIZE(get_host_responses), |
| 52 | + AIRLIFT_DEFAULT_TIMEOUT_MS); |
| 53 | + if (num_responses == 1) { |
| 54 | + return true; |
| 55 | + } |
| 56 | + } |
| 57 | + |
| 58 | + return false; |
| 59 | +} |
| 60 | + |
| 61 | +static mp_obj_t socketpool_socketpool_gethostbyname_str(socketpool_socketpool_obj_t *self, const char *host) { |
| 62 | + uint8_t ipv4[4] = { 0 }; |
| 63 | + |
| 64 | + if (!socketpool_gethostbyname_ipv4(self, host, ipv4)) { |
| 65 | + // Could not resolve or parse hostname. |
| 66 | + return mp_const_none; |
| 67 | + } |
| 68 | + |
| 69 | + vstr_t vstr; |
| 70 | + mp_print_t print; |
| 71 | + vstr_init_print(&vstr, 16, &print); |
| 72 | + mp_printf(&print, "%d.%d.%d.%d", ipv4[0], ipv4[1], ipv4[2], ipv4[3]); |
| 73 | + return mp_obj_new_str_from_vstr(&vstr); |
| 74 | +} |
95 | 75 |
|
96 | 76 | 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; |
| 77 | + mp_obj_t ip_str = socketpool_socketpool_gethostbyname_str(self, host); |
| 78 | + if (ip_str == mp_const_none) { |
| 79 | + // Could not resolve hostname. |
| 80 | + common_hal_socketpool_socketpool_raise_gaierror_noname(); |
| 81 | + } |
| 82 | + |
| 83 | + mp_obj_tuple_t *tuple = MP_OBJ_TO_PTR(mp_obj_new_tuple(5, NULL)); |
| 84 | + tuple->items[0] = MP_OBJ_NEW_SMALL_INT(SOCKETPOOL_AF_INET); |
| 85 | + tuple->items[1] = MP_OBJ_NEW_SMALL_INT(SOCKETPOOL_SOCK_STREAM); |
| 86 | + tuple->items[2] = MP_OBJ_NEW_SMALL_INT(0); |
| 87 | + tuple->items[3] = MP_OBJ_NEW_QSTR(MP_QSTR_); |
| 88 | + mp_obj_tuple_t *sockaddr = MP_OBJ_TO_PTR(mp_obj_new_tuple(2, NULL)); |
| 89 | + sockaddr->items[0] = ip_str; |
| 90 | + sockaddr->items[1] = MP_OBJ_NEW_SMALL_INT(port); |
| 91 | + tuple->items[4] = MP_OBJ_FROM_PTR(sockaddr); |
| 92 | + return mp_obj_new_list(1, (mp_obj_t *)&tuple); |
124 | 93 | } |
0 commit comments