wasi-libc/libc-bottom-half/sources
Henri Nurmi 1b19fc65ad
getaddrinfo: improve the service/port resolution (#524)
Hello,

While experimenting with the `wasm32-wasip2` target and CPython, I
discovered an issue with the `getaddrinfo()` implementation: it fails to
resolve the provided service into a port number, causing `sin_port` to
always be set to 0. This issue leads to failures in network-related
functions that rely on `getaddrinfo()`, such as Python's `urllib3`
library, which passes the result directly to `connect()`. This results
in connection attempts using a port value of 0, which naturally fails.

### Minimal example to reproduce the problem
```c
#include <arpa/inet.h>
#include <netdb.h>
#include <stdio.h>

int main(void) {
    struct addrinfo *res = NULL;
    getaddrinfo("google.com", "443", NULL, &res);

    for (struct addrinfo *i = res; i != NULL; i = i->ai_next) {
        char str[INET6_ADDRSTRLEN];
        if (i->ai_addr->sa_family == AF_INET) {
            struct sockaddr_in *p = (struct sockaddr_in *)i->ai_addr;
            int port = ntohs(p->sin_port);
            printf("%s: %i\n", inet_ntop(AF_INET, &p->sin_addr, str, sizeof(str)), port);
        } else if (i->ai_addr->sa_family == AF_INET6) {
            struct sockaddr_in6 *p = (struct sockaddr_in6 *)i->ai_addr;
            int port = ntohs(p->sin6_port);
            printf("%s: %i\n", inet_ntop(AF_INET6, &p->sin6_addr, str, sizeof(str)), port);
        }
    }

    return 0;
}
```
```
$ /opt/wasi-sdk/bin/clang -target wasm32-wasip2 -o foo foo.c
$ wasmtime run -S allow-ip-name-lookup=y foo
216.58.211.238: 0
2a00:1450:4026:808::200e: 0
```
Expected output:
```
216.58.211.238: 443
2a00:1450:4026:808::200e: 443
```
### Root Cause

The root cause is that `getaddrinfo()` does not correctly translate the
provided service into a port number. As described in the `getaddrinfo()`
man [page](https://man7.org/linux/man-pages/man3/getaddrinfo.3.html),
the function should:

> service sets the port in each returned address structure. If
this argument is a service name (see
[services(5)](https://man7.org/linux/man-pages/man5/services.5.html)),
it is
translated to the corresponding port number. This argument can
also be specified as a decimal number, which is simply converted
to binary. If service is NULL, then the port number of the
returned socket addresses will be left uninitialized.

### Proposed Fix

This pull request addresses the issue by implementing the following
behavior for `getaddrinfo()`:

* If the service is `NULL`, the port number in the returned socket
addresses remains uninitialized.
* The value is converted to an integer and validated if the service is
numeric.

The PR does not currently add support for translating named services
into port numbers because `getservbyname()` has not been implemented. In
cases where a named service is provided, the `EAI_NONAME` error code is
returned.
2024-08-27 17:39:34 -07:00
..
math Merge the basics component into libc-bottom-half. (#199) 2020-06-01 16:44:05 -07:00
__errno_location.c Define an __errno_location function. (#347) 2022-11-28 13:50:04 -08:00
__main_void.c add shared library support (#429) 2023-09-28 06:11:09 -07:00
__wasilibc_dt.c Add implementation file. 2022-04-13 13:33:44 -07:00
__wasilibc_environ.c Also add a way to read environ without triggering eager init. 2021-05-25 15:25:05 -07:00
__wasilibc_fd_renumber.c wasip2 support for close, poll, pselect (#486) 2024-03-27 12:24:10 -07:00
__wasilibc_initialize_environ.c Move weak attribute to front (#310) 2022-07-26 16:12:17 -07:00
__wasilibc_real.c Rename thread_spawn import (#387) 2023-01-24 17:14:03 -08:00
__wasilibc_rmdirat.c Fix missing errno assignments. 2022-10-06 14:19:26 -07:00
__wasilibc_tell.c Wasi snapshot preview1 (#140) 2019-11-21 20:06:00 -08:00
__wasilibc_unlinkat.c Delete several blocks of unused code. (#294) 2022-07-20 15:46:06 -07:00
abort.c Merge the basics component into libc-bottom-half. (#199) 2020-06-01 16:44:05 -07:00
accept-wasip1.c implement basic TCP/UDP server support (#481) 2024-03-13 14:50:55 -07:00
accept-wasip2.c implement basic TCP/UDP server support (#481) 2024-03-13 14:50:55 -07:00
at_fdcwd.c AT_FDCWD support. 2021-02-05 06:41:53 -08:00
bind.c implement basic TCP/UDP server support (#481) 2024-03-13 14:50:55 -07:00
chdir.c Fix make THREAD_MODEL=posix (#311) 2022-08-09 08:08:37 -07:00
complex-builtins.c Implement the cimag/creal functions for all types consistently. 2021-02-04 21:23:10 -08:00
connect.c implement basic TCP/UDP client support (#477) 2024-03-13 10:59:27 -07:00
descriptor_table.c implement basic TCP/UDP client support (#477) 2024-03-13 10:59:27 -07:00
environ.c Use MUSL's weak* feature in bottom half (#306) 2022-07-26 14:15:12 -07:00
errno.c Merge the basics component into libc-bottom-half. (#199) 2020-06-01 16:44:05 -07:00
getcwd.c Fix make THREAD_MODEL=posix (#311) 2022-08-09 08:08:37 -07:00
getentropy.c Fix make THREAD_MODEL=posix (#311) 2022-08-09 08:08:37 -07:00
getsockpeername.c implement getsockname, getpeername, and getaddrinfo (#488) 2024-04-02 16:54:41 -07:00
isatty.c Wasi snapshot preview1 (#140) 2019-11-21 20:06:00 -08:00
listen.c implement basic TCP/UDP server support (#481) 2024-03-13 14:50:55 -07:00
netdb.c getaddrinfo: improve the service/port resolution (#524) 2024-08-27 17:39:34 -07:00
poll-wasip2.c wasip2 support for close, poll, pselect (#486) 2024-03-27 12:24:10 -07:00
posix.c add stubs for statvfs, chmod, etc. (#463) 2024-01-11 12:16:59 -08:00
preopens.c Remove extra lock-taking in preopen setup (#491) 2024-05-02 14:07:27 -07:00
reallocarray.c Merge the basics component into libc-bottom-half. (#199) 2020-06-01 16:44:05 -07:00
recv.c implement basic TCP/UDP client support (#477) 2024-03-13 10:59:27 -07:00
sbrk.c Use consistent style for wasi-libc C source files. (#131) 2019-11-08 11:59:57 -08:00
send.c implement basic TCP/UDP client support (#477) 2024-03-13 10:59:27 -07:00
shutdown.c add wasip2 implementations of more socket APIs (#482) 2024-03-18 17:54:07 -07:00
socket.c implement basic TCP/UDP client support (#477) 2024-03-13 10:59:27 -07:00
sockets_utils.c getaddrinfo: improve the service/port resolution (#524) 2024-08-27 17:39:34 -07:00
sockopt.c add wasip2 implementations of more socket APIs (#482) 2024-03-18 17:54:07 -07:00
truncate.c Avoid varargs conventions when calling open (#126) 2019-11-04 16:37:45 -08:00
wasip2_component_type.o Start renaming preview1 to p1 and preview2 to p2 (#478) 2024-03-04 15:57:34 -08:00
wasip2.c Start renaming preview1 to p1 and preview2 to p2 (#478) 2024-03-04 15:57:34 -08:00