Refactor so that all sites of name lookup failure result in gaierror

This commit is contained in:
Jeff Epler 2022-12-14 14:45:32 -06:00
parent fc13fba6e5
commit 297657ea9a
No known key found for this signature in database
GPG Key ID: D5BF15AB975AB4DE
6 changed files with 49 additions and 33 deletions

View File

@ -369,7 +369,7 @@ void common_hal_socketpool_socket_connect(socketpool_socket_obj_t *self,
struct addrinfo *result_i;
int error = lwip_getaddrinfo(host, NULL, &hints, &result_i);
if (error != 0 || result_i == NULL) {
mp_raise_OSError(EHOSTUNREACH);
common_hal_socketpool_socketpool_raise_gaierror(SOCKETPOOL_EAI_NONAME, MP_QSTR_Name_space_or_space_service_space_not_space_known);
}
// Set parameters
@ -550,7 +550,7 @@ mp_uint_t common_hal_socketpool_socket_sendto(socketpool_socket_obj_t *self,
struct addrinfo *result_i;
int error = lwip_getaddrinfo(host, NULL, &hints, &result_i);
if (error != 0 || result_i == NULL) {
mp_raise_OSError(EHOSTUNREACH);
common_hal_socketpool_socketpool_raise_gaierror(SOCKETPOOL_EAI_NONAME, MP_QSTR_Name_space_or_space_service_space_not_space_known);
}
// Set parameters

View File

@ -872,11 +872,7 @@ bool common_hal_socketpool_socket_bind(socketpool_socket_obj_t *socket,
ip_addr_t bind_addr;
const ip_addr_t *bind_addr_ptr = &bind_addr;
if (hostlen > 0) {
int error = socketpool_resolve_host(socket->pool, host, &bind_addr);
if (error != 0) {
mp_raise_OSError(EHOSTUNREACH);
}
socketpool_resolve_host_raise(socket->pool, host, &bind_addr);
} else {
bind_addr_ptr = IP_ANY_TYPE;
}
@ -965,10 +961,7 @@ void common_hal_socketpool_socket_connect(socketpool_socket_obj_t *socket,
// get address
ip_addr_t dest;
int error = socketpool_resolve_host(socket->pool, host, &dest);
if (error != 0) {
mp_raise_OSError(EHOSTUNREACH);
}
socketpool_resolve_host_raise(socket->pool, host, &dest);
err_t err = ERR_ARG;
switch (socket->type) {
@ -1163,10 +1156,7 @@ mp_uint_t common_hal_socketpool_socket_sendto(socketpool_socket_obj_t *socket,
const char *host, size_t hostlen, uint32_t port, const uint8_t *buf, uint32_t len) {
int _errno;
ip_addr_t ip;
int error = socketpool_resolve_host(socket->pool, host, &ip);
if (error != 0) {
mp_raise_OSError(EHOSTUNREACH);
}
socketpool_resolve_host_raise(socket->pool, host, &ip);
mp_uint_t ret = 0;
switch (socket->type) {

View File

@ -57,7 +57,7 @@ STATIC void lwip_getaddrinfo_cb(const char *name, const ip_addr_t *ipaddr, void
}
}
int socketpool_resolve_host(socketpool_socketpool_obj_t *self, const char *host, ip_addr_t *addr) {
STATIC int socketpool_resolve_host(socketpool_socketpool_obj_t *self, const char *host, ip_addr_t *addr) {
getaddrinfo_state_t state;
state.status = 0;
@ -94,17 +94,26 @@ int socketpool_resolve_host(socketpool_socketpool_obj_t *self, const char *host,
return 0;
}
mp_obj_t common_hal_socketpool_socketpool_gethostbyname(socketpool_socketpool_obj_t *self,
const char *host) {
ip_addr_t addr;
int result = socketpool_resolve_host(self, host, &addr);
void socketpool_resolve_host_raise(socketpool_socketpool_obj_t *self, const char *host, ip_addr_t *addr) {
int result = socketpool_resolve_host(self, host, addr);
if (result < 0) {
printf("socket_resolve_host() returned %d\n", result);
common_hal_socketpool_socketpool_raise_gaierror(SOCKETPOOL_EAI_NONAME, MP_QSTR_Name_space_or_space_service_space_not_space_known);
mp_raise_OSError(-result);
}
}
mp_obj_t common_hal_socketpool_socketpool_gethostbyname(socketpool_socketpool_obj_t *self, const char *host) {
ip_addr_t addr;
socketpool_resolve_host_raise(self, host, &addr);
char ip_str[IP4ADDR_STRLEN_MAX];
inet_ntoa_r(addr, ip_str, IP4ADDR_STRLEN_MAX);
mp_obj_t ip_obj = mp_obj_new_str(ip_str, strlen(ip_str));
return ip_obj;
}
mp_obj_t common_hal_socketpool_socketpool_gethostbyname_raise(socketpool_socketpool_obj_t *self, const char *host) {
return common_hal_socketpool_socketpool_gethostbyname(self, host);
}

View File

@ -32,4 +32,4 @@ typedef struct {
mp_obj_base_t base;
} socketpool_socketpool_obj_t;
int socketpool_resolve_host(socketpool_socketpool_obj_t *self, const char *host, ip_addr_t *addr);
void socketpool_resolve_host_raise(socketpool_socketpool_obj_t *self, const char *host, ip_addr_t *addr);

View File

@ -36,8 +36,6 @@
#include "shared-bindings/socketpool/Socket.h"
#include "shared-bindings/socketpool/SocketPool.h"
#define SOCKETPOOL_EAI_NONAME (-2)
//| class SocketPool:
//| """A pool of socket resources available for the given radio. Only one
//| SocketPool can be created for each radio.
@ -151,15 +149,7 @@ STATIC mp_obj_t socketpool_socketpool_getaddrinfo(size_t n_args, const mp_obj_t
}
if (ip_str == mp_const_none) {
ip_str = common_hal_socketpool_socketpool_gethostbyname(self, host);
}
if (ip_str == mp_const_none) {
mp_obj_t exc_args[2] = {
MP_OBJ_NEW_SMALL_INT(SOCKETPOOL_EAI_NONAME),
MP_OBJ_NEW_QSTR(MP_QSTR_Name_space_or_space_service_space_not_space_known),
};
nlr_raise(mp_obj_new_exception_args(&mp_type_gaierror, 2, exc_args));
ip_str = common_hal_socketpool_socketpool_gethostbyname_raise(self, host);
}
mp_obj_tuple_t *tuple = MP_OBJ_TO_PTR(mp_obj_new_tuple(5, NULL));
@ -203,3 +193,21 @@ const mp_obj_type_t socketpool_socketpool_type = {
.make_new = socketpool_socketpool_make_new,
.locals_dict = (mp_obj_dict_t *)&socketpool_socketpool_locals_dict,
};
MP_WEAK
mp_obj_t common_hal_socketpool_socketpool_gethostbyname_raise(socketpool_socketpool_obj_t *self, const char *host) {
mp_obj_t ip_str = common_hal_socketpool_socketpool_gethostbyname(self, host);
if (ip_str == mp_const_none) {
common_hal_socketpool_socketpool_raise_gaierror(SOCKETPOOL_EAI_NONAME, MP_QSTR_Name_space_or_space_service_space_not_space_known);
}
return ip_str;
}
MP_WEAK NORETURN
void common_hal_socketpool_socketpool_raise_gaierror(int value, qstr name) {
mp_obj_t exc_args[2] = {
MP_OBJ_NEW_SMALL_INT(value),
MP_OBJ_NEW_QSTR(name),
};
nlr_raise(mp_obj_new_exception_args(&mp_type_gaierror, 2, exc_args));
}

View File

@ -52,6 +52,10 @@ typedef enum {
SOCKETPOOL_TCP_NODELAY = 1,
} socketpool_socketpool_tcpopt_t;
typedef enum {
SOCKETPOOL_EAI_NONAME = -2,
} socketpool_eai_t;
void common_hal_socketpool_socketpool_construct(socketpool_socketpool_obj_t *self, mp_obj_t radio);
socketpool_socket_obj_t *common_hal_socketpool_socket(socketpool_socketpool_obj_t *self,
@ -59,6 +63,9 @@ socketpool_socket_obj_t *common_hal_socketpool_socket(socketpool_socketpool_obj_
mp_obj_t common_hal_socketpool_socketpool_gethostbyname(socketpool_socketpool_obj_t *self,
const char *host);
// raises an exception instead of returning mp_const_none in the case of error
mp_obj_t common_hal_socketpool_socketpool_gethostbyname_raise(socketpool_socketpool_obj_t *self,
const char *host);
// Non-allocating version for internal use. These sockets are not registered and, therefore, not
// closed automatically.
@ -66,4 +73,6 @@ bool socketpool_socket(socketpool_socketpool_obj_t *self,
socketpool_socketpool_addressfamily_t family, socketpool_socketpool_sock_t type,
socketpool_socket_obj_t *sock);
NORETURN void common_hal_socketpool_socketpool_raise_gaierror(int value, qstr name);
#endif // MICROPY_INCLUDED_SHARED_BINDINGS_SOCKETPOOL_SOCKETPOOL_H