Skip to content

Commit b9d03f1

Browse files
authored
Merge pull request #11215 from mikeysklar/fix/socketpool-gaierror-real-code
socketpool: let ports raise gaierror with the real error code
2 parents fb89345 + d2a38f5 commit b9d03f1

4 files changed

Lines changed: 28 additions & 5 deletions

File tree

ports/zephyr-cp/common-hal/socketpool/Socket.c

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
#include <string.h>
2828

2929
#include <zephyr/kernel.h>
30+
#include <zephyr/net/dns_resolve.h>
3031
#include <zephyr/net/socket.h>
3132

3233
#define SOCKETPOOL_IP_STR_LEN 48
@@ -82,7 +83,12 @@ static void socketpool_resolve_host_or_throw(int family, int type, const char *h
8283

8384
int error = zsock_getaddrinfo(hostname, service_buf, &hints, &result_i);
8485
if (error != 0 || result_i == NULL) {
85-
common_hal_socketpool_socketpool_raise_gaierror_noname();
86+
// Report what the resolver said. A null result with no error is the
87+
// one case that really is "not found".
88+
if (error == 0) {
89+
error = DNS_EAI_NONAME;
90+
}
91+
common_hal_socketpool_socketpool_raise_gaierror(error);
8692
}
8793

8894
memcpy(addr, result_i->ai_addr, sizeof(struct sockaddr_storage));

ports/zephyr-cp/common-hal/socketpool/SocketPool.c

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
#include <string.h>
2121

2222
#include <zephyr/net/socket.h>
23+
#include <zephyr/net/dns_resolve.h>
2324

2425
void common_hal_socketpool_socketpool_construct(socketpool_socketpool_obj_t *self, mp_obj_t radio) {
2526
bool is_wifi = false;
@@ -104,7 +105,10 @@ mp_obj_t common_hal_socketpool_getaddrinfo_raise(socketpool_socketpool_obj_t *se
104105
struct zsock_addrinfo *res = NULL;
105106
int err = socketpool_getaddrinfo_common(host, port, &hints, &res);
106107
if (err != 0 || res == NULL) {
107-
common_hal_socketpool_socketpool_raise_gaierror_noname();
108+
if (err == 0) {
109+
err = DNS_EAI_NONAME;
110+
}
111+
common_hal_socketpool_socketpool_raise_gaierror(err);
108112
}
109113

110114
nlr_buf_t nlr;

shared-bindings/socketpool/SocketPool.c

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -190,15 +190,27 @@ MP_DEFINE_CONST_OBJ_TYPE(
190190
);
191191

192192
MP_WEAK MP_NORETURN
193-
void common_hal_socketpool_socketpool_raise_gaierror_noname(void) {
193+
void common_hal_socketpool_socketpool_raise_gaierror(int err) {
194194
vstr_t vstr;
195195
mp_print_t print;
196196
vstr_init_print(&vstr, 64, &print);
197-
mp_printf(&print, "%S", MP_ERROR_TEXT("Name or service not known"));
197+
// Only EAI_NONAME means the name was not found. Other codes cover
198+
// unrelated failures, such as an unsupported family, so use the generic
199+
// message for those rather than one that would be misleading.
200+
if (err == SOCKETPOOL_EAI_NONAME) {
201+
mp_printf(&print, "%S", MP_ERROR_TEXT("Name or service not known"));
202+
} else {
203+
mp_cprintf(&print, MP_ERROR_TEXT("%q failure: %d"), MP_QSTR_getaddrinfo, err);
204+
}
198205

199206
mp_obj_t exc_args[] = {
200-
MP_OBJ_NEW_SMALL_INT(SOCKETPOOL_EAI_NONAME),
207+
MP_OBJ_NEW_SMALL_INT(err),
201208
mp_obj_new_str_from_vstr(&vstr),
202209
};
203210
nlr_raise(mp_obj_new_exception_args(&mp_type_gaierror, MP_ARRAY_SIZE(exc_args), exc_args));
204211
}
212+
213+
MP_WEAK MP_NORETURN
214+
void common_hal_socketpool_socketpool_raise_gaierror_noname(void) {
215+
common_hal_socketpool_socketpool_raise_gaierror(SOCKETPOOL_EAI_NONAME);
216+
}

shared-bindings/socketpool/SocketPool.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,5 +26,6 @@ bool socketpool_socket(socketpool_socketpool_obj_t *self,
2626
int proto, socketpool_socket_obj_t *sock);
2727

2828
MP_NORETURN void common_hal_socketpool_socketpool_raise_gaierror_noname(void);
29+
MP_NORETURN void common_hal_socketpool_socketpool_raise_gaierror(int err);
2930

3031
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);

0 commit comments

Comments
 (0)