Skip to content

Commit ff5932a

Browse files
committed
modsocket: Workaround uClibc issue with numeric port for getaddrinfo().
It sucks to workaround this on uPy side, but upgrading not upgradable embedded systems sucks even more.
1 parent 949a49c commit ff5932a

1 file changed

Lines changed: 15 additions & 3 deletions

File tree

unix/modsocket.c

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -356,21 +356,33 @@ STATIC mp_obj_t mod_socket_getaddrinfo(uint n_args, const mp_obj_t *args) {
356356

357357
const char *host = mp_obj_str_get_str(args[0]);
358358
const char *serv = NULL;
359+
struct addrinfo hints;
360+
memset(&hints, 0, sizeof(hints));
359361
// getaddrinfo accepts port in string notation, so however
360362
// it may seem stupid, we need to convert int to str
361363
if (MP_OBJ_IS_SMALL_INT(args[1])) {
362364
int port = (short)MP_OBJ_SMALL_INT_VALUE(args[1]);
363365
char buf[6];
364366
sprintf(buf, "%d", port);
365367
serv = buf;
368+
hints.ai_flags = AI_NUMERICSERV;
369+
#if __UCLIBC_MAJOR__ == 0 && (__UCLIBC_MINOR__ < 9 || (__UCLIBC_MINOR__ == 9 && __UCLIBC_SUBLEVEL__ <= 32))
370+
#warning Working around uClibc bug with numeric service name
371+
// Older versions og uClibc have bugs when numeric ports in service
372+
// arg require also hints.ai_socktype (or hints.ai_protocol) != 0
373+
// This actually was fixed in 0.9.32.1, but uClibc doesn't allow to
374+
// test for that.
375+
// http://git.uclibc.org/uClibc/commit/libc/inet/getaddrinfo.c?id=bc3be18145e4d5
376+
// Note that this is crude workaround, precluding UDP socket addresses
377+
// to be returned. TODO: set only if not set by Python args.
378+
hints.ai_socktype = SOCK_STREAM;
379+
#endif
366380
} else {
367381
serv = mp_obj_str_get_str(args[1]);
368382
}
369383

370-
struct addrinfo hints;
371384
struct addrinfo *addr_list;
372-
memset(&hints, 0, sizeof(hints));
373-
int res = getaddrinfo(host, serv, NULL/*&hints*/, &addr_list);
385+
int res = getaddrinfo(host, serv, &hints, &addr_list);
374386

375387
if (res != 0) {
376388
// CPython: socket.gaierror

0 commit comments

Comments
 (0)