zephyr/modusocket: getaddrinfo: Raise OSError on resolution timeout, etc.

This commit is contained in:
Paul Sokolovsky 2017-05-24 19:54:26 +03:00
parent 1c9ee49756
commit 982e676902
1 changed files with 12 additions and 1 deletions

View File

@ -533,12 +533,18 @@ typedef struct _getaddrinfo_state_t {
mp_obj_t result;
struct k_sem sem;
mp_obj_t port;
int status;
} getaddrinfo_state_t;
void dns_resolve_cb(enum dns_resolve_status status, struct dns_addrinfo *info, void *user_data) {
getaddrinfo_state_t *state = user_data;
DEBUG_printf("dns status: %d\n", status);
if (info == NULL) {
if (status == DNS_EAI_ALLDONE) {
status = 0;
}
state->status = status;
k_sem_give(&state->sem);
return;
}
@ -569,7 +575,6 @@ STATIC mp_obj_t mod_getaddrinfo(size_t n_args, const mp_obj_t *args) {
state.result = mp_obj_new_list(0, NULL);
k_sem_init(&state.sem, 0, UINT_MAX);
int status;
for (int i = 2; i--;) {
int type = (family != AF_INET6 ? DNS_QUERY_TYPE_A : DNS_QUERY_TYPE_AAAA);
RAISE_ERRNO(dns_get_addr_info(host, type, NULL, dns_resolve_cb, &state, 3000));
@ -580,6 +585,12 @@ STATIC mp_obj_t mod_getaddrinfo(size_t n_args, const mp_obj_t *args) {
family = AF_INET6;
}
// Raise error only if there's nothing to return, otherwise
// it may be IPv4 vs IPv6 differences.
if (state.status != 0 && mp_obj_len(state.result) == 0) {
mp_raise_OSError(state.status);
}
return state.result;
}
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mod_getaddrinfo_obj, 2, 3, mod_getaddrinfo);