diff options
author | Thorsten von Eicken <tve@voneicken.com> | 2020-04-28 10:58:43 -0700 |
---|---|---|
committer | Damien George <damien.p.george@gmail.com> | 2020-05-09 16:43:48 +1000 |
commit | ab4e197707cd456499d680091fdc9daeff18c62a (patch) | |
tree | a8af94aeda01b21aa417cd1aa35f37653e79808d /ports/esp32/modsocket.c | |
parent | adb673302238bb1129ad62e7783dc650aaa27277 (diff) |
esp32/modsocket: Fix getaddrinfo to raise on error.
This commit fixes the behaviour of socket.getaddrinfo on the ESP32 so it
raises an OSError when the name resolution fails instead of returning a []
or a resolution for 0.0.0.0.
Tests are added (generic and ESP32-specific) to verify behaviour consistent
with CPython, modulo the different types of exceptions per MicroPython
documentation.
Diffstat (limited to 'ports/esp32/modsocket.c')
-rw-r--r-- | ports/esp32/modsocket.c | 19 |
1 files changed, 12 insertions, 7 deletions
diff --git a/ports/esp32/modsocket.c b/ports/esp32/modsocket.c index 69a74ec25..85433e575 100644 --- a/ports/esp32/modsocket.c +++ b/ports/esp32/modsocket.c @@ -244,19 +244,24 @@ static int _socket_getaddrinfo2(const mp_obj_t host, const mp_obj_t portx, struc int res = _socket_getaddrinfo3(host_str, port_str, &hints, resp); MP_THREAD_GIL_ENTER(); + // Per docs: instead of raising gaierror getaddrinfo raises negative error number + if (res != 0) { + mp_raise_OSError(res > 0 ? -res : res); + } + // Somehow LwIP returns a resolution of 0.0.0.0 for failed lookups, traced it as far back + // as netconn_gethostbyname_addrtype returning OK instead of error. + if (*resp == NULL || + (strcmp(resp[0]->ai_canonname, "0.0.0.0") == 0 && strcmp(host_str, "0.0.0.0") != 0)) { + mp_raise_OSError(-2); // name or service not known + } + return res; } STATIC void _socket_getaddrinfo(const mp_obj_t addrtuple, struct addrinfo **resp) { mp_obj_t *elem; mp_obj_get_array_fixed_n(addrtuple, 2, &elem); - int res = _socket_getaddrinfo2(elem[0], elem[1], resp); - if (res != 0) { - mp_raise_OSError(res); - } - if (*resp == NULL) { - mp_raise_OSError(-2); // name or service not known - } + _socket_getaddrinfo2(elem[0], elem[1], resp); } STATIC mp_obj_t socket_make_new(const mp_obj_type_t *type_in, size_t n_args, size_t n_kw, const mp_obj_t *args) { |