Skip to content

Commit d038294

Browse files
dicejbadeend
andauthored
implement getsockname, getpeername, and getaddrinfo (#488)
This also includes stubs for `gethostbyname`, `gethostbyaddr`, etc. which were necessary to get CPython to build. I believe it will be possible to implement them all properly at some point, but don't have the bandwidth at the moment. Finally, this includes a few fixes for issues I missed in earlier PRs that surfaced when running the CPython `asyncio` test suite. Signed-off-by: Joel Dice <joel.dice@fermyon.com> Co-authored-by: Dave Bakker <github@davebakker.io>
1 parent 1ab654e commit d038294

8 files changed

Lines changed: 555 additions & 5 deletions

File tree

Makefile

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,9 @@ LIBC_BOTTOM_HALF_OMIT_SOURCES := \
9494
$(LIBC_BOTTOM_HALF_SOURCES)/accept-wasip2.c \
9595
$(LIBC_BOTTOM_HALF_SOURCES)/shutdown.c \
9696
$(LIBC_BOTTOM_HALF_SOURCES)/sockopt.c \
97-
$(LIBC_BOTTOM_HALF_SOURCES)/poll-wasip2.c
97+
$(LIBC_BOTTOM_HALF_SOURCES)/poll-wasip2.c \
98+
$(LIBC_BOTTOM_HALF_SOURCES)/getsockpeername.c \
99+
$(LIBC_BOTTOM_HALF_SOURCES)/netdb.c
98100
LIBC_BOTTOM_HALF_ALL_SOURCES := $(filter-out $(LIBC_BOTTOM_HALF_OMIT_SOURCES),$(LIBC_BOTTOM_HALF_ALL_SOURCES))
99101
# Omit p2-specific headers from include-all.c test.
100102
# for exception-handling.
@@ -249,6 +251,13 @@ LIBC_TOP_HALF_MUSL_SOURCES = \
249251
$(wildcard $(LIBC_TOP_HALF_MUSL_SRC_DIR)/complex/*.c)) \
250252
$(wildcard $(LIBC_TOP_HALF_MUSL_SRC_DIR)/crypt/*.c)
251253

254+
ifeq ($(WASI_SNAPSHOT), p2)
255+
LIBC_TOP_HALF_MUSL_SOURCES += \
256+
$(addprefix $(LIBC_TOP_HALF_MUSL_SRC_DIR)/, \
257+
network/gai_strerror.c \
258+
)
259+
endif
260+
252261
ifeq ($(THREAD_MODEL), posix)
253262
LIBC_TOP_HALF_MUSL_SOURCES += \
254263
$(addprefix $(LIBC_TOP_HALF_MUSL_SRC_DIR)/, \
@@ -387,6 +396,10 @@ ASMFLAGS += -matomics
387396
CFLAGS += -I$(LIBC_BOTTOM_HALF_CLOUDLIBC_SRC)
388397
endif
389398

399+
ifeq ($(WASI_SNAPSHOT), p2)
400+
EXTRA_CFLAGS += -D__wasilibc_use_wasip2
401+
endif
402+
390403
# Expose the public headers to the implementation. We use `-isystem` for
391404
# purpose for two reasons:
392405
#
@@ -500,7 +513,6 @@ MUSL_OMIT_HEADERS += \
500513
"sys/auxv.h" \
501514
"pwd.h" "shadow.h" "grp.h" \
502515
"mntent.h" \
503-
"netdb.h" \
504516
"resolv.h" \
505517
"pty.h" \
506518
"ulimit.h" \
@@ -523,6 +535,10 @@ MUSL_OMIT_HEADERS += \
523535
"sys/sysmacros.h" \
524536
"aio.h"
525537

538+
ifeq ($(WASI_SNAPSHOT), p1)
539+
MUSL_OMIT_HEADERS += "netdb.h"
540+
endif
541+
526542
ifeq ($(THREAD_MODEL), single)
527543
# Remove headers not supported in single-threaded mode.
528544
MUSL_OMIT_HEADERS += "pthread.h"

expected/wasm32-wasip2/defined-symbols.txt

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -535,6 +535,9 @@ dprintf
535535
drand48
536536
drem
537537
dremf
538+
drop_tcp_socket
539+
drop_udp_socket
540+
drop_udp_socket_streams
538541
duplocale
539542
ecvt
540543
encrypt
@@ -709,6 +712,7 @@ fputws_unlocked
709712
fread
710713
fread_unlocked
711714
free
715+
freeaddrinfo
712716
freelocale
713717
freopen
714718
freopen64
@@ -737,11 +741,13 @@ fwprintf
737741
fwrite
738742
fwrite_unlocked
739743
fwscanf
744+
gai_strerror
740745
gcvt
741746
get_avphys_pages
742747
get_nprocs
743748
get_nprocs_conf
744749
get_phys_pages
750+
getaddrinfo
745751
getc
746752
getc_unlocked
747753
getchar
@@ -753,14 +759,22 @@ getdelim
753759
getdomainname
754760
getentropy
755761
getenv
762+
gethostbyaddr
763+
gethostbyname
756764
gethostid
757765
getline
766+
getnameinfo
758767
getopt
759768
getopt_long
760769
getopt_long_only
761770
getpagesize
771+
getpeername
762772
getpid
773+
getprotobyname
763774
getrusage
775+
getservbyname
776+
getservbyport
777+
getsockname
764778
getsockopt
765779
getsubopt
766780
gettimeofday
@@ -775,12 +789,14 @@ globfree
775789
globfree64
776790
gmtime
777791
gmtime_r
792+
h_errno
778793
hcreate
779794
hcreate_r
780795
hdestroy
781796
hdestroy_r
782797
hsearch
783798
hsearch_r
799+
hstrerror
784800
htonl
785801
htons
786802
hypot
@@ -1246,6 +1262,8 @@ tcp_bind
12461262
tcp_borrow_tcp_socket
12471263
tcp_create_socket_create_tcp_socket
12481264
tcp_create_socket_result_own_tcp_socket_error_code_free
1265+
tcp_getpeername
1266+
tcp_getsockname
12491267
tcp_getsockopt
12501268
tcp_ip_socket_address_free
12511269
tcp_listen
@@ -1338,6 +1356,8 @@ udp_borrow_outgoing_datagram_stream
13381356
udp_borrow_udp_socket
13391357
udp_create_socket_create_udp_socket
13401358
udp_create_socket_result_own_udp_socket_error_code_free
1359+
udp_getpeername
1360+
udp_getsockname
13411361
udp_getsockopt
13421362
udp_incoming_datagram_free
13431363
udp_incoming_datagram_stream_drop_borrow

expected/wasm32-wasip2/include-all.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,7 @@
102102
#include <memory.h>
103103
#include <monetary.h>
104104
#include <mqueue.h>
105+
#include <netdb.h>
105106
#include <netinet/icmp6.h>
106107
#include <netinet/igmp.h>
107108
#include <netinet/in.h>

expected/wasm32-wasip2/predefined-macros.txt

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,13 @@
3737
#define AF_INET6 PF_INET6
3838
#define AF_UNIX 3
3939
#define AF_UNSPEC PF_UNSPEC
40+
#define AI_ADDRCONFIG 0x20
41+
#define AI_ALL 0x10
42+
#define AI_CANONNAME 0x02
43+
#define AI_NUMERICHOST 0x04
44+
#define AI_NUMERICSERV 0x400
45+
#define AI_PASSIVE 0x01
46+
#define AI_V4MAPPED 0x08
4047
#define ALT_DIGITS 0x2002F
4148
#define AM_STR 0x20026
4249
#define ANYMARK 0x01
@@ -192,6 +199,24 @@
192199
#define EADDRNOTAVAIL __WASI_ERRNO_ADDRNOTAVAIL
193200
#define EAFNOSUPPORT __WASI_ERRNO_AFNOSUPPORT
194201
#define EAGAIN __WASI_ERRNO_AGAIN
202+
#define EAI_ADDRFAMILY -9
203+
#define EAI_AGAIN -3
204+
#define EAI_ALLDONE -103
205+
#define EAI_BADFLAGS -1
206+
#define EAI_CANCELED -101
207+
#define EAI_FAIL -4
208+
#define EAI_FAMILY -6
209+
#define EAI_IDN_ENCODE -105
210+
#define EAI_INPROGRESS -100
211+
#define EAI_INTR -104
212+
#define EAI_MEMORY -10
213+
#define EAI_NODATA -5
214+
#define EAI_NONAME -2
215+
#define EAI_NOTCANCELED -102
216+
#define EAI_OVERFLOW -12
217+
#define EAI_SERVICE -8
218+
#define EAI_SOCKTYPE -7
219+
#define EAI_SYSTEM -11
195220
#define EALREADY __WASI_ERRNO_ALREADY
196221
#define EBADF __WASI_ERRNO_BADF
197222
#define EBADID 5
@@ -489,6 +514,7 @@
489514
#define HIBITL MINLONG
490515
#define HIBITS MINSHORT
491516
#define HOST_NAME_MAX 255
517+
#define HOST_NOT_FOUND 1
492518
#define HUGE 3.40282346638528859812e+38F
493519
#define HUGE_VAL ((double)INFINITY)
494520
#define HUGE_VALF INFINITY
@@ -1239,6 +1265,14 @@
12391265
#define NEW_ENV_VAR 0
12401266
#define NGROUPS 32
12411267
#define NGROUPS_MAX 32
1268+
#define NI_DGRAM 0x10
1269+
#define NI_MAXHOST 255
1270+
#define NI_MAXSERV 32
1271+
#define NI_NAMEREQD 0x08
1272+
#define NI_NOFQDN 0x04
1273+
#define NI_NUMERICHOST 0x01
1274+
#define NI_NUMERICSCOPE 0x100
1275+
#define NI_NUMERICSERV 0x02
12421276
#define NL_ARGMAX 9
12431277
#define NL_CAT_LOCALE 1
12441278
#define NL_LANGMAX 32
@@ -1257,6 +1291,9 @@
12571291
#define NOTAUTH ns_r_notauth
12581292
#define NOTIMP ns_r_notimpl
12591293
#define NOTZONE ns_r_notzone
1294+
#define NO_ADDRESS NO_DATA
1295+
#define NO_DATA 4
1296+
#define NO_RECOVERY 3
12601297
#define NR_ICMP_TYPES 18
12611298
#define NR_ICMP_UNREACH 15
12621299
#define NSLC 18
@@ -1968,6 +2005,7 @@
19682005
#define TOREAD 00004
19692006
#define TOWRITE 00002
19702007
#define TRANSIENT 4
2008+
#define TRY_AGAIN 2
19712009
#define TSGID 02000
19722010
#define TSS_DTOR_ITERATIONS 4
19732011
#define TSUID 04000
@@ -2173,6 +2211,7 @@
21732211
#define _MATH_H
21742212
#define _MONETARY_H
21752213
#define _MQUEUE_H
2214+
#define _NETDB_H
21762215
#define _NETINET_ICMP6_H
21772216
#define _NETINET_IGMP_H
21782217
#define _NETINET_IN_H
@@ -3194,7 +3233,7 @@
31943233
#define __wasilibc___typedef_suseconds_t_h
31953234
#define __wasilibc___typedef_time_t_h
31963235
#define __wasilibc___typedef_uid_t_h
3197-
#define __wasilibc_use_wasip2
3236+
#define __wasilibc_use_wasip2 1
31983237
#define __wasm 1
31993238
#define __wasm32 1
32003239
#define __wasm32__ 1
@@ -3281,6 +3320,8 @@
32813320
#define glob64 glob
32823321
#define glob64_t glob_t
32833322
#define globfree64 globfree
3323+
#define h_addr h_addr_list[0]
3324+
#define h_errno h_errno
32843325
#define howmany(n,d) (((n)+((d)-1))/(d))
32853326
#define htobe16(x) __bswap16(x)
32863327
#define htobe32(x) __bswap32(x)

0 commit comments

Comments
 (0)