Skip to content

Commit a780ffd

Browse files
authored
lib-socket: make getaddrinfo return EAI_ values (#4498)
cf. #4464
1 parent 952b35d commit a780ffd

File tree

2 files changed

+36
-7
lines changed

2 files changed

+36
-7
lines changed

core/iwasm/libraries/lib-socket/inc/wasi_socket_ext.h

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,28 @@ typedef struct __wasi_addr_info_hints_t {
135135
#define IPV6_LEAVE_GROUP 21
136136
#define IPV6_V6ONLY 26
137137

138+
/* getaddrinfo error codes.
139+
*
140+
* we use values compatible with wasi-libc/musl netdb.h.
141+
* https://github.com/WebAssembly/wasi-libc/blob/4ea6fdfa288e15a57c02fe31dda78e5ddc87c3c7/libc-top-half/musl/include/netdb.h#L43-L53
142+
*
143+
* for now, non-posix error codes are excluded:
144+
* EAI_PROTOCOL and EAI_BADHINTS (BSDs)
145+
* EAI_ADDRFAMILY, EAI_NODATA
146+
* https://github.com/WebAssembly/wasi-libc/blob/4ea6fdfa288e15a57c02fe31dda78e5ddc87c3c7/libc-top-half/musl/include/netdb.h#L145-L152
147+
*/
148+
149+
#define EAI_AGAIN -3
150+
#define EAI_BADFLAGS -1
151+
#define EAI_FAIL -4
152+
#define EAI_FAMILY -6
153+
#define EAI_MEMORY -10
154+
#define EAI_NONAME -2
155+
#define EAI_OVERFLOW -12
156+
#define EAI_SERVICE -8
157+
#define EAI_SOCKTYPE -7
158+
#define EAI_SYSTEM -11
159+
138160
struct addrinfo {
139161
int ai_flags; /* Input flags. */
140162
int ai_family; /* Protocol family for socket. */

core/iwasm/libraries/lib-socket/src/wasi/wasi_socket_ext.c

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,13 @@ __errno_location(void);
3838
return -1; \
3939
}
4040

41+
/* REVISIT: in many cases, EAI_SYSTEM may not be an ideal error code */
42+
#define GAI_HANDLE_ERROR(error) \
43+
if (error != __WASI_ERRNO_SUCCESS) { \
44+
errno = error; \
45+
return EAI_SYSTEM; \
46+
}
47+
4148
static void
4249
ipv4_addr_to_wasi_ip4_addr(uint32_t addr_num, __wasi_addr_ip4_t *out)
4350
{
@@ -518,7 +525,7 @@ getaddrinfo(const char *node, const char *service, const struct addrinfo *hints,
518525
struct aibuf *aibuf_res;
519526

520527
error = addrinfo_hints_to_wasi_hints(hints, &wasi_hints);
521-
HANDLE_ERROR(error)
528+
GAI_HANDLE_ERROR(error)
522529

523530
do {
524531
if (addr_info)
@@ -529,29 +536,29 @@ getaddrinfo(const char *node, const char *service, const struct addrinfo *hints,
529536
* sizeof(__wasi_addr_info_t));
530537

531538
if (!addr_info) {
532-
HANDLE_ERROR(__WASI_ERRNO_NOMEM)
539+
return EAI_MEMORY;
533540
}
534541

535542
error = __wasi_sock_addr_resolve(node, service == NULL ? "" : service,
536543
&wasi_hints, addr_info, addr_info_size,
537544
&max_info_size);
538545
if (error != __WASI_ERRNO_SUCCESS) {
539546
free(addr_info);
540-
HANDLE_ERROR(error);
547+
GAI_HANDLE_ERROR(error);
541548
}
542549
} while (max_info_size > addr_info_size);
543550

544551
addr_info_size = max_info_size;
545552
if (addr_info_size == 0) {
546553
free(addr_info);
547-
HANDLE_ERROR(__WASI_ERRNO_NOENT)
554+
return EAI_NONAME;
548555
}
549556

550557
aibuf_res =
551558
(struct aibuf *)calloc(1, addr_info_size * sizeof(struct aibuf));
552559
if (!aibuf_res) {
553560
free(addr_info);
554-
HANDLE_ERROR(__WASI_ERRNO_NOMEM)
561+
return EAI_MEMORY;
555562
}
556563

557564
*res = &aibuf_res[0].ai;
@@ -564,14 +571,14 @@ getaddrinfo(const char *node, const char *service, const struct addrinfo *hints,
564571
if (error != __WASI_ERRNO_SUCCESS) {
565572
free(addr_info);
566573
free(aibuf_res);
567-
HANDLE_ERROR(error)
574+
GAI_HANDLE_ERROR(error)
568575
}
569576
ai->ai_next = i == addr_info_size - 1 ? NULL : &aibuf_res[i + 1].ai;
570577
}
571578

572579
free(addr_info);
573580

574-
return __WASI_ERRNO_SUCCESS;
581+
return 0;
575582
}
576583

577584
void

0 commit comments

Comments
 (0)