diff options
| author | Arshia Ghafoori <arshia001@live.com> | 2024-05-23 21:00:33 +0330 |
|---|---|---|
| committer | Arshia Ghafoori <arshia001@live.com> | 2024-05-23 21:00:33 +0330 |
| commit | ed2ce080c6798a5b33535b153056ccec4f267f0e (patch) | |
| tree | 13792d173cd8d280aca7b45453864a4fe5b9ed28 | |
| parent | 5ca4100e24cebe0b78df3b3ac6c941b67cdaeb51 (diff) | |
Fix gethostbyname* reporting addresses from the wrong family
| -rw-r--r-- | libc-top-half/musl/src/network/gethostbyname2_r.c | 37 |
1 files changed, 32 insertions, 5 deletions
diff --git a/libc-top-half/musl/src/network/gethostbyname2_r.c b/libc-top-half/musl/src/network/gethostbyname2_r.c index fc89487..9d9e05b 100644 --- a/libc-top-half/musl/src/network/gethostbyname2_r.c +++ b/libc-top-half/musl/src/network/gethostbyname2_r.c @@ -6,16 +6,31 @@ #include <netinet/in.h> #include <errno.h> #include <stdint.h> +#include <wasi/api.h> #include "lookup.h" +int printf (const char *, ...); + int gethostbyname2_r(const char *name, int af, struct hostent *h, char *buf, size_t buflen, struct hostent **res, int *err) { struct address addrs[MAXADDRS]; char canon[256]; - int i, cnt; + int i, j, cnt, total_cnt; size_t align, need; + int wasi_af; + + if (af == AF_INET) { + wasi_af = __WASI_ADDRESS_FAMILY_IP_INET4; + } else if (af == AF_INET6) { + wasi_af = __WASI_ADDRESS_FAMILY_IP_INET6; + } else { + *err = NO_RECOVERY; + return ENOTSUP; + } + + printf("af %d wasi_af %d\n", af, wasi_af); *res = 0; cnt = __lookup_name(addrs, canon, name, af, AI_CANONNAME); @@ -36,6 +51,15 @@ int gethostbyname2_r(const char *name, int af, return errno; } + total_cnt = cnt; + + for (i = 0; i < total_cnt; ++i) { + if (addrs[i].family != wasi_af) { + printf("%d family %d skipped\n", i, addrs[i].family); + --cnt; + } + } + h->h_addrtype = af; h->h_length = af==AF_INET6 ? 16 : 4; @@ -56,12 +80,15 @@ int gethostbyname2_r(const char *name, int af, h->h_addr_list = (void *)buf; buf += (cnt+1)*sizeof(char *); - for (i=0; i<cnt; i++) { - h->h_addr_list[i] = (void *)buf; + for (i=0,j=0; i<total_cnt; i++) { + if (addrs[i].family != wasi_af) continue; + + printf("%d %d family %d reported\n", i, j, addrs[i].family); + h->h_addr_list[j] = (void *)buf; buf += h->h_length; - memcpy(h->h_addr_list[i], addrs[i].addr, h->h_length); + memcpy(h->h_addr_list[j++], addrs[i].addr, h->h_length); } - h->h_addr_list[i] = 0; + h->h_addr_list[j] = 0; h->h_name = h->h_aliases[0] = buf; strcpy(h->h_name, canon); |
