-
Notifications
You must be signed in to change notification settings - Fork 202
Expand file tree
/
Copy path0008-sockets.patch
More file actions
785 lines (773 loc) · 27.9 KB
/
Copy path0008-sockets.patch
File metadata and controls
785 lines (773 loc) · 27.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
diff --git a/libc-bottom-half/headers/public/__struct_sockaddr_un.h b/libc-bottom-half/headers/public/__struct_sockaddr_un.h
index 6371194..60634cf 100644
--- a/libc-bottom-half/headers/public/__struct_sockaddr_un.h
+++ b/libc-bottom-half/headers/public/__struct_sockaddr_un.h
@@ -5,6 +5,7 @@
struct sockaddr_un {
__attribute__((aligned(__BIGGEST_ALIGNMENT__))) sa_family_t sun_family;
+ char sun_path[108];
};
#endif
diff --git a/libc-bottom-half/sources/host_socket.c b/libc-bottom-half/sources/host_socket.c
new file mode 100644
index 0000000..975e62a
--- /dev/null
+++ b/libc-bottom-half/sources/host_socket.c
@@ -0,0 +1,696 @@
+// Socket API via wasmVM host_net imports.
+//
+// Replaces wasi-libc's ENOSYS stubs with calls to our custom WASM imports:
+// host_net.net_socket -> socket()
+// host_net.net_connect -> connect()
+// host_net.net_bind -> bind()
+// host_net.net_listen -> listen()
+// host_net.net_accept -> accept()
+// host_net.net_send -> send()
+// host_net.net_recv -> recv()
+// host_net.net_sendto -> sendto()
+// host_net.net_recvfrom -> recvfrom()
+// host_net.net_close -> (used internally)
+// host_net.net_getaddrinfo -> getaddrinfo()
+// host_net.net_setsockopt -> setsockopt()
+// host_net.net_getsockname -> getsockname()
+// host_net.net_getpeername -> getpeername()
+// host_net.net_poll -> poll()
+//
+// Supports AF_INET, AF_INET6, and AF_UNIX address families.
+// Import signatures match wasmvm/crates/wasi-ext/src/lib.rs exactly.
+
+#include <sys/socket.h>
+#include <sys/select.h>
+#include <netinet/in.h>
+#include <netdb.h>
+#include <poll.h>
+#include <errno.h>
+#include <stdint.h>
+#include <string.h>
+#include <stdlib.h>
+#include <stdio.h>
+#include <arpa/inet.h>
+#include <unistd.h>
+
+// AF_UNIX support — define sockaddr_un if sys/un.h is not available
+#ifdef __has_include
+# if __has_include(<sys/un.h>)
+# include <sys/un.h>
+# define HAVE_SYS_UN_H 1
+# endif
+#endif
+#ifndef HAVE_SYS_UN_H
+# ifndef AF_UNIX
+# define AF_UNIX 1
+# endif
+# ifndef AF_LOCAL
+# define AF_LOCAL AF_UNIX
+# endif
+struct sockaddr_un {
+ sa_family_t sun_family;
+ char sun_path[108];
+};
+#endif
+
+#define WASM_IMPORT(mod, fn) \
+ __attribute__((__import_module__(mod), __import_name__(fn)))
+
+// host_net.net_socket(domain: u32, type: u32, protocol: u32, ret_fd: *mut u32) -> errno
+WASM_IMPORT("host_net", "net_socket")
+uint32_t __host_net_socket(uint32_t domain, uint32_t type, uint32_t protocol, uint32_t *ret_fd);
+
+// host_net.net_connect(fd: u32, addr_ptr: *const u8, addr_len: u32) -> errno
+WASM_IMPORT("host_net", "net_connect")
+uint32_t __host_net_connect(uint32_t fd, const uint8_t *addr_ptr, uint32_t addr_len);
+
+// host_net.net_send(fd: u32, buf_ptr: *const u8, buf_len: u32, flags: u32, ret_sent: *mut u32) -> errno
+WASM_IMPORT("host_net", "net_send")
+uint32_t __host_net_send(uint32_t fd, const uint8_t *buf_ptr, uint32_t buf_len, uint32_t flags, uint32_t *ret_sent);
+
+// host_net.net_recv(fd: u32, buf_ptr: *mut u8, buf_len: u32, flags: u32, ret_received: *mut u32) -> errno
+WASM_IMPORT("host_net", "net_recv")
+uint32_t __host_net_recv(uint32_t fd, uint8_t *buf_ptr, uint32_t buf_len, uint32_t flags, uint32_t *ret_received);
+
+// host_net.net_close(fd: u32) -> errno
+WASM_IMPORT("host_net", "net_close")
+uint32_t __host_net_close(uint32_t fd);
+
+// host_net.net_getaddrinfo(host_ptr, host_len, port_ptr, port_len, ret_addr, ret_addr_len) -> errno
+WASM_IMPORT("host_net", "net_getaddrinfo")
+uint32_t __host_net_getaddrinfo(
+ const uint8_t *host_ptr, uint32_t host_len,
+ const uint8_t *port_ptr, uint32_t port_len,
+ uint8_t *ret_addr, uint32_t *ret_addr_len);
+
+// host_net.net_setsockopt(fd, level, optname, optval_ptr, optval_len) -> errno
+WASM_IMPORT("host_net", "net_setsockopt")
+uint32_t __host_net_setsockopt(uint32_t fd, uint32_t level, uint32_t optname,
+ const uint8_t *optval_ptr, uint32_t optval_len);
+
+// host_net.net_getsockname(fd, ret_addr, ret_addr_len) -> errno
+WASM_IMPORT("host_net", "net_getsockname")
+uint32_t __host_net_getsockname(uint32_t fd, uint8_t *ret_addr, uint32_t *ret_addr_len);
+
+// host_net.net_getpeername(fd, ret_addr, ret_addr_len) -> errno
+WASM_IMPORT("host_net", "net_getpeername")
+uint32_t __host_net_getpeername(uint32_t fd, uint8_t *ret_addr, uint32_t *ret_addr_len);
+
+// host_net.net_bind(fd, addr_ptr, addr_len) -> errno
+WASM_IMPORT("host_net", "net_bind")
+uint32_t __host_net_bind(uint32_t fd, const uint8_t *addr_ptr, uint32_t addr_len);
+
+// host_net.net_listen(fd, backlog) -> errno
+WASM_IMPORT("host_net", "net_listen")
+uint32_t __host_net_listen(uint32_t fd, uint32_t backlog);
+
+// host_net.net_accept(fd, ret_fd, ret_addr, ret_addr_len) -> errno
+WASM_IMPORT("host_net", "net_accept")
+uint32_t __host_net_accept(uint32_t fd, uint32_t *ret_fd, uint8_t *ret_addr, uint32_t *ret_addr_len);
+
+// host_net.net_poll(fds_ptr, nfds, timeout_ms, ret_ready) -> errno
+WASM_IMPORT("host_net", "net_poll")
+uint32_t __host_net_poll(uint8_t *fds_ptr, uint32_t nfds, int32_t timeout_ms,
+ uint32_t *ret_ready);
+
+// host_net.net_sendto(fd, buf_ptr, buf_len, flags, addr_ptr, addr_len, ret_sent) -> errno
+WASM_IMPORT("host_net", "net_sendto")
+uint32_t __host_net_sendto(uint32_t fd, const uint8_t *buf_ptr, uint32_t buf_len,
+ uint32_t flags, const uint8_t *addr_ptr, uint32_t addr_len, uint32_t *ret_sent);
+
+// host_net.net_recvfrom(fd, buf_ptr, buf_len, flags, ret_received, ret_addr, ret_addr_len) -> errno
+WASM_IMPORT("host_net", "net_recvfrom")
+uint32_t __host_net_recvfrom(uint32_t fd, uint8_t *buf_ptr, uint32_t buf_len,
+ uint32_t flags, uint32_t *ret_received, uint8_t *ret_addr, uint32_t *ret_addr_len);
+
+// ---------------------------------------------------------------------------
+// Address serialization helpers (AF_INET, AF_INET6, AF_UNIX)
+// ---------------------------------------------------------------------------
+
+// Serialize a sockaddr to a string: "host:port" for inet, path for unix.
+// Returns length written (not counting NUL), or -1 on error.
+static int sockaddr_to_string(const struct sockaddr *addr, socklen_t addrlen,
+ char *buf, size_t buflen) {
+ if (addr->sa_family == AF_INET) {
+ const struct sockaddr_in *sin = (const struct sockaddr_in *)addr;
+ char ip[INET_ADDRSTRLEN];
+ inet_ntop(AF_INET, &sin->sin_addr, ip, sizeof(ip));
+ unsigned int port = ntohs(sin->sin_port);
+ return snprintf(buf, buflen, "%s:%u", ip, port);
+ } else if (addr->sa_family == AF_INET6) {
+ const struct sockaddr_in6 *sin6 = (const struct sockaddr_in6 *)addr;
+ char ip[INET6_ADDRSTRLEN];
+ inet_ntop(AF_INET6, &sin6->sin6_addr, ip, sizeof(ip));
+ unsigned int port = ntohs(sin6->sin6_port);
+ return snprintf(buf, buflen, "%s:%u", ip, port);
+ } else if (addr->sa_family == AF_UNIX) {
+ const struct sockaddr_un *sun = (const struct sockaddr_un *)addr;
+ // Path length: addrlen - offsetof(sockaddr_un, sun_path), or strlen
+ size_t pathlen = addrlen > (socklen_t)__builtin_offsetof(struct sockaddr_un, sun_path)
+ ? (size_t)(addrlen - __builtin_offsetof(struct sockaddr_un, sun_path))
+ : strlen(sun->sun_path);
+ // Trim trailing NUL if present
+ if (pathlen > 0 && sun->sun_path[pathlen - 1] == '\0') pathlen--;
+ if (pathlen >= buflen) return -1;
+ memcpy(buf, sun->sun_path, pathlen);
+ buf[pathlen] = '\0';
+ return (int)pathlen;
+ }
+ return -1; // unsupported family
+}
+
+// Deserialize an address string into a sockaddr.
+// For "host:port" → sockaddr_in or sockaddr_in6; for paths → sockaddr_un.
+// Returns the actual sockaddr size, or 0 on error.
+static socklen_t string_to_sockaddr(const char *str, struct sockaddr *addr,
+ socklen_t addrlen) {
+ // Find last colon to distinguish inet from unix
+ const char *last_colon = strrchr(str, ':');
+ if (last_colon == NULL) {
+ // No colon → Unix domain socket path
+ struct sockaddr_un sun;
+ memset(&sun, 0, sizeof(sun));
+ sun.sun_family = AF_UNIX;
+ size_t pathlen = strlen(str);
+ if (pathlen >= sizeof(sun.sun_path)) pathlen = sizeof(sun.sun_path) - 1;
+ memcpy(sun.sun_path, str, pathlen);
+ sun.sun_path[pathlen] = '\0';
+ socklen_t copy_len = addrlen < (socklen_t)sizeof(sun) ? addrlen : (socklen_t)sizeof(sun);
+ memcpy(addr, &sun, copy_len);
+ return (socklen_t)sizeof(sun);
+ }
+
+ // Parse host and port
+ char ip[INET6_ADDRSTRLEN];
+ size_t ip_len = (size_t)(last_colon - str);
+ if (ip_len >= sizeof(ip)) ip_len = sizeof(ip) - 1;
+ memcpy(ip, str, ip_len);
+ ip[ip_len] = '\0';
+ unsigned int port = 0;
+ for (const char *p = last_colon + 1; *p >= '0' && *p <= '9'; p++)
+ port = port * 10 + (unsigned int)(*p - '0');
+
+ // Try IPv4 first
+ struct sockaddr_in sin;
+ memset(&sin, 0, sizeof(sin));
+ if (inet_pton(AF_INET, ip, &sin.sin_addr) == 1) {
+ sin.sin_family = AF_INET;
+ sin.sin_port = htons((uint16_t)port);
+ socklen_t copy_len = addrlen < (socklen_t)sizeof(sin) ? addrlen : (socklen_t)sizeof(sin);
+ memcpy(addr, &sin, copy_len);
+ return (socklen_t)sizeof(sin);
+ }
+
+ // Try IPv6
+ struct sockaddr_in6 sin6;
+ memset(&sin6, 0, sizeof(sin6));
+ if (inet_pton(AF_INET6, ip, &sin6.sin6_addr) == 1) {
+ sin6.sin6_family = AF_INET6;
+ sin6.sin6_port = htons((uint16_t)port);
+ socklen_t copy_len = addrlen < (socklen_t)sizeof(sin6) ? addrlen : (socklen_t)sizeof(sin6);
+ memcpy(addr, &sin6, copy_len);
+ return (socklen_t)sizeof(sin6);
+ }
+
+ return 0; // parse failed
+}
+
+int socket(int domain, int type, int protocol) {
+ uint32_t fd;
+ uint32_t err = __host_net_socket((uint32_t)domain, (uint32_t)type, (uint32_t)protocol, &fd);
+ if (err != 0) {
+ errno = (int)err;
+ return -1;
+ }
+ return (int)fd;
+}
+
+int connect(int sockfd, const struct sockaddr *addr, socklen_t addrlen) {
+ char buf[256];
+ int len = sockaddr_to_string(addr, addrlen, buf, sizeof(buf));
+ if (len < 0 || (size_t)len >= sizeof(buf)) {
+ errno = (len < 0) ? EAFNOSUPPORT : EINVAL;
+ return -1;
+ }
+
+ uint32_t err = __host_net_connect((uint32_t)sockfd, (const uint8_t *)buf, (uint32_t)len);
+ if (err != 0) {
+ errno = (int)err;
+ return -1;
+ }
+ return 0;
+}
+
+int bind(int sockfd, const struct sockaddr *addr, socklen_t addrlen) {
+ char buf[256];
+ int len = sockaddr_to_string(addr, addrlen, buf, sizeof(buf));
+ if (len < 0 || (size_t)len >= sizeof(buf)) {
+ errno = (len < 0) ? EAFNOSUPPORT : EINVAL;
+ return -1;
+ }
+
+ uint32_t err = __host_net_bind((uint32_t)sockfd, (const uint8_t *)buf, (uint32_t)len);
+ if (err != 0) {
+ errno = (int)err;
+ return -1;
+ }
+ return 0;
+}
+
+int listen(int sockfd, int backlog) {
+ uint32_t err = __host_net_listen((uint32_t)sockfd, (uint32_t)(backlog > 0 ? backlog : 0));
+ if (err != 0) {
+ errno = (int)err;
+ return -1;
+ }
+ return 0;
+}
+
+int accept(int sockfd, struct sockaddr *restrict addr, socklen_t *restrict addrlen) {
+ uint32_t new_fd;
+ uint8_t addr_buf[256];
+ uint32_t addr_buf_len = sizeof(addr_buf) - 1;
+
+ uint32_t err = __host_net_accept((uint32_t)sockfd, &new_fd, addr_buf, &addr_buf_len);
+ if (err != 0) {
+ errno = (int)err;
+ return -1;
+ }
+
+ // Parse remote address string into sockaddr
+ if (addr != NULL && addrlen != NULL) {
+ addr_buf[addr_buf_len] = '\0';
+ socklen_t actual = string_to_sockaddr((const char *)addr_buf, addr, *addrlen);
+ if (actual > 0) *addrlen = actual;
+ }
+
+ return (int)new_fd;
+}
+
+int accept4(int sockfd, struct sockaddr *restrict addr, socklen_t *restrict addrlen, int flags) {
+ if (flags & ~(SOCK_NONBLOCK | SOCK_CLOEXEC)) {
+ errno = EINVAL;
+ return -1;
+ }
+
+ // CLOEXEC is ignored by wasi-libc today; preserve NONBLOCK validation parity.
+ return accept(sockfd, addr, addrlen);
+}
+
+ssize_t send(int sockfd, const void *buf, size_t len, int flags) {
+ uint32_t sent;
+ uint32_t err = __host_net_send((uint32_t)sockfd, (const uint8_t *)buf, (uint32_t)len, (uint32_t)flags, &sent);
+ if (err != 0) {
+ errno = (int)err;
+ return -1;
+ }
+ return (ssize_t)sent;
+}
+
+ssize_t recv(int sockfd, void *buf, size_t len, int flags) {
+ uint32_t received;
+ uint32_t err = __host_net_recv((uint32_t)sockfd, (uint8_t *)buf, (uint32_t)len, (uint32_t)flags, &received);
+ if (err != 0) {
+ errno = (int)err;
+ return -1;
+ }
+ return (ssize_t)received;
+}
+
+ssize_t sendto(int sockfd, const void *buf, size_t len, int flags,
+ const struct sockaddr *dest_addr, socklen_t addrlen) {
+ // If dest_addr is NULL, behave like send() (connected socket)
+ if (dest_addr == NULL) {
+ return send(sockfd, buf, len, flags);
+ }
+
+ char addr_str[256];
+ int slen = sockaddr_to_string(dest_addr, addrlen, addr_str, sizeof(addr_str));
+ if (slen < 0 || (size_t)slen >= sizeof(addr_str)) {
+ errno = (slen < 0) ? EAFNOSUPPORT : EINVAL;
+ return -1;
+ }
+
+ uint32_t sent;
+ uint32_t err = __host_net_sendto((uint32_t)sockfd, (const uint8_t *)buf, (uint32_t)len,
+ (uint32_t)flags, (const uint8_t *)addr_str, (uint32_t)slen, &sent);
+ if (err != 0) {
+ errno = (int)err;
+ return -1;
+ }
+ return (ssize_t)sent;
+}
+
+ssize_t recvfrom(int sockfd, void *restrict buf, size_t len, int flags,
+ struct sockaddr *restrict src_addr, socklen_t *restrict addrlen) {
+ // If src_addr is NULL, behave like recv()
+ if (src_addr == NULL) {
+ return recv(sockfd, buf, len, flags);
+ }
+
+ uint32_t received;
+ uint8_t ret_addr[256];
+ uint32_t ret_addr_len = sizeof(ret_addr) - 1;
+
+ uint32_t err = __host_net_recvfrom((uint32_t)sockfd, (uint8_t *)buf, (uint32_t)len,
+ (uint32_t)flags, &received, ret_addr, &ret_addr_len);
+ if (err != 0) {
+ errno = (int)err;
+ return -1;
+ }
+
+ // Parse source address string into sockaddr
+ if (addrlen != NULL) {
+ ret_addr[ret_addr_len] = '\0';
+ socklen_t actual = string_to_sockaddr((const char *)ret_addr, src_addr, *addrlen);
+ if (actual > 0) *addrlen = actual;
+ }
+
+ return (ssize_t)received;
+}
+
+int setsockopt(int sockfd, int level, int optname, const void *optval, socklen_t optlen) {
+ uint32_t err = __host_net_setsockopt(
+ (uint32_t)sockfd, (uint32_t)level, (uint32_t)optname,
+ (const uint8_t *)optval, (uint32_t)optlen);
+ if (err != 0) {
+ errno = (int)err;
+ return -1;
+ }
+ return 0;
+}
+
+int getsockname(int sockfd, struct sockaddr *restrict addr, socklen_t *restrict addrlen) {
+ if (addr == NULL || addrlen == NULL) {
+ errno = EINVAL;
+ return -1;
+ }
+
+ uint8_t addr_buf[256];
+ uint32_t addr_buf_len = sizeof(addr_buf) - 1;
+ uint32_t err = __host_net_getsockname((uint32_t)sockfd, addr_buf, &addr_buf_len);
+ if (err != 0) {
+ errno = (int)err;
+ return -1;
+ }
+
+ addr_buf[addr_buf_len] = '\0';
+ socklen_t actual = string_to_sockaddr((const char *)addr_buf, addr, *addrlen);
+ if (actual == 0) {
+ errno = EINVAL;
+ return -1;
+ }
+ *addrlen = actual;
+ return 0;
+}
+
+int getpeername(int sockfd, struct sockaddr *restrict addr, socklen_t *restrict addrlen) {
+ if (addr == NULL || addrlen == NULL) {
+ errno = EINVAL;
+ return -1;
+ }
+
+ uint8_t addr_buf[256];
+ uint32_t addr_buf_len = sizeof(addr_buf) - 1;
+ uint32_t err = __host_net_getpeername((uint32_t)sockfd, addr_buf, &addr_buf_len);
+ if (err != 0) {
+ errno = (int)err;
+ return -1;
+ }
+
+ addr_buf[addr_buf_len] = '\0';
+ socklen_t actual = string_to_sockaddr((const char *)addr_buf, addr, *addrlen);
+ if (actual == 0) {
+ errno = EINVAL;
+ return -1;
+ }
+ *addrlen = actual;
+ return 0;
+}
+
+int gethostname(char *name, size_t len) {
+ const char *hostname = "sandbox";
+ size_t hlen = strlen(hostname);
+ if (hlen >= len) {
+ errno = ENAMETOOLONG;
+ return -1;
+ }
+ memcpy(name, hostname, hlen + 1);
+ return 0;
+}
+
+// Parse a port number from a string, return -1 on failure
+static int parse_port(const char *s) {
+ if (!s || !*s) return 0;
+ int port = 0;
+ for (; *s; s++) {
+ if (*s < '0' || *s > '9') return -1;
+ port = port * 10 + (*s - '0');
+ if (port > 65535) return -1;
+ }
+ return port;
+}
+
+// Skip whitespace in JSON parsing
+static const char *skip_ws(const char *p) {
+ while (*p == ' ' || *p == '\t' || *p == '\n' || *p == '\r') p++;
+ return p;
+}
+
+// Parse a JSON string value (expects p pointing at opening quote)
+// Writes into dst (up to dst_len-1 chars), returns pointer past closing quote
+static const char *parse_json_str(const char *p, char *dst, size_t dst_len) {
+ if (*p != '"') return NULL;
+ p++;
+ size_t i = 0;
+ while (*p && *p != '"') {
+ if (i < dst_len - 1) dst[i++] = *p;
+ p++;
+ }
+ if (i < dst_len) dst[i] = '\0';
+ if (*p == '"') p++;
+ return p;
+}
+
+// Allocate and populate one addrinfo entry
+static struct addrinfo *make_addrinfo(const char *addr_str, int family, int port, int socktype) {
+ size_t sa_len;
+ if (family == AF_INET)
+ sa_len = sizeof(struct sockaddr_in);
+ else if (family == AF_INET6)
+ sa_len = sizeof(struct sockaddr_in6);
+ else
+ return NULL;
+
+ struct addrinfo *ai = calloc(1, sizeof(struct addrinfo) + sa_len);
+ if (!ai) return NULL;
+
+ ai->ai_family = family;
+ ai->ai_socktype = socktype ? socktype : SOCK_STREAM;
+ ai->ai_protocol = (ai->ai_socktype == SOCK_STREAM) ? IPPROTO_TCP : IPPROTO_UDP;
+ ai->ai_addrlen = sa_len;
+ ai->ai_addr = (struct sockaddr *)((char *)ai + sizeof(struct addrinfo));
+
+ if (family == AF_INET) {
+ struct sockaddr_in *sin = (struct sockaddr_in *)ai->ai_addr;
+ sin->sin_family = AF_INET;
+ sin->sin_port = htons((uint16_t)port);
+ inet_pton(AF_INET, addr_str, &sin->sin_addr);
+ } else {
+ struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)ai->ai_addr;
+ sin6->sin6_family = AF_INET6;
+ sin6->sin6_port = htons((uint16_t)port);
+ inet_pton(AF_INET6, addr_str, &sin6->sin6_addr);
+ }
+ return ai;
+}
+
+int getaddrinfo(const char *restrict host, const char *restrict serv,
+ const struct addrinfo *restrict hints, struct addrinfo **restrict res) {
+ if (!host && !serv) return EAI_NONAME;
+
+ const char *host_str = host ? host : "";
+ const char *port_str = serv ? serv : "0";
+
+ int port = parse_port(port_str);
+ int socktype = hints ? hints->ai_socktype : 0;
+
+ // Call host bridge for DNS resolution
+ uint8_t buf[4096];
+ uint32_t buf_len = sizeof(buf) - 1;
+ uint32_t err = __host_net_getaddrinfo(
+ (const uint8_t *)host_str, (uint32_t)strlen(host_str),
+ (const uint8_t *)port_str, (uint32_t)strlen(port_str),
+ buf, &buf_len);
+ if (err != 0) return EAI_NONAME;
+
+ buf[buf_len] = '\0';
+ const char *json = (const char *)buf;
+
+ // Parse JSON array: [{"addr":"...","family":N}, ...]
+ struct addrinfo *head = NULL;
+ struct addrinfo *tail = NULL;
+ int count = 0;
+
+ const char *p = skip_ws(json);
+ if (*p != '[') return EAI_FAIL;
+ p++;
+
+ while (*p) {
+ p = skip_ws(p);
+ if (*p == ']') break;
+ if (*p == ',') { p++; continue; }
+ if (*p != '{') break;
+ p++;
+
+ char addr_str[64] = {0};
+ int family = 0;
+
+ // Parse object fields
+ while (*p && *p != '}') {
+ p = skip_ws(p);
+ if (*p == ',') { p++; continue; }
+
+ char key[16] = {0};
+ p = parse_json_str(p, key, sizeof(key));
+ if (!p) goto parse_error;
+
+ p = skip_ws(p);
+ if (*p != ':') goto parse_error;
+ p++;
+ p = skip_ws(p);
+
+ if (strcmp(key, "addr") == 0) {
+ p = parse_json_str(p, addr_str, sizeof(addr_str));
+ if (!p) goto parse_error;
+ } else if (strcmp(key, "family") == 0) {
+ family = 0;
+ while (*p >= '0' && *p <= '9') {
+ family = family * 10 + (*p - '0');
+ p++;
+ }
+ // Map Node.js family (4/6) to AF_INET/AF_INET6
+ if (family == 4) family = AF_INET;
+ else if (family == 6) family = AF_INET6;
+ } else {
+ // Skip unknown value
+ if (*p == '"') {
+ char tmp[64];
+ p = parse_json_str(p, tmp, sizeof(tmp));
+ if (!p) goto parse_error;
+ } else {
+ while (*p && *p != ',' && *p != '}') p++;
+ }
+ }
+ }
+ if (*p == '}') p++;
+
+ // Filter by requested family
+ if (hints && hints->ai_family != AF_UNSPEC && hints->ai_family != family)
+ continue;
+
+ struct addrinfo *ai = make_addrinfo(addr_str, family, port, socktype);
+ if (!ai) { freeaddrinfo(head); return EAI_MEMORY; }
+
+ if (tail) { tail->ai_next = ai; tail = ai; }
+ else { head = tail = ai; }
+ count++;
+ }
+
+ if (count == 0) return EAI_NONAME;
+ *res = head;
+ return 0;
+
+parse_error:
+ freeaddrinfo(head);
+ return EAI_FAIL;
+}
+
+void freeaddrinfo(struct addrinfo *res) {
+ while (res) {
+ struct addrinfo *next = res->ai_next;
+ free(res);
+ res = next;
+ }
+}
+
+const char *gai_strerror(int ecode) {
+ switch (ecode) {
+ case 0: return "Success";
+ case EAI_BADFLAGS: return "Invalid flags";
+ case EAI_NONAME: return "Name or service not known";
+ case EAI_AGAIN: return "Temporary failure in name resolution";
+ case EAI_FAIL: return "Non-recoverable failure in name resolution";
+ case EAI_FAMILY: return "Address family not supported";
+ case EAI_SOCKTYPE: return "Socket type not supported";
+ case EAI_SERVICE: return "Servname not supported for ai_socktype";
+ case EAI_MEMORY: return "Memory allocation failure";
+ case EAI_SYSTEM: return "System error";
+ case EAI_OVERFLOW: return "Argument buffer overflow";
+ default: return "Unknown error";
+ }
+}
+
+int poll(struct pollfd fds[], nfds_t nfds, int timeout) {
+ // Pass pollfd array directly to host — layout matches (fd:i32, events:i16, revents:i16)
+ uint32_t ready = 0;
+ uint32_t err = __host_net_poll((uint8_t *)fds, (uint32_t)nfds, (int32_t)timeout, &ready);
+ if (err != 0) {
+ errno = (int)err;
+ return -1;
+ }
+ return (int)ready;
+}
+
+int select(int nfds, fd_set *restrict readfds, fd_set *restrict writefds,
+ fd_set *restrict exceptfds, struct timeval *restrict timeout) {
+ // Convert fd_sets to pollfd array, call poll(), then scatter results back
+ struct pollfd pfds[FD_SETSIZE];
+ int count = 0;
+ int fd_map[FD_SETSIZE]; // maps pfds index -> original fd
+
+ for (int fd = 0; fd < nfds && count < FD_SETSIZE; fd++) {
+ short events = 0;
+ if (readfds && FD_ISSET(fd, readfds)) events |= POLLIN;
+ if (writefds && FD_ISSET(fd, writefds)) events |= POLLOUT;
+ if (events == 0 && !(exceptfds && FD_ISSET(fd, exceptfds))) continue;
+ pfds[count].fd = fd;
+ pfds[count].events = events;
+ pfds[count].revents = 0;
+ fd_map[count] = fd;
+ count++;
+ }
+
+ int timeout_ms = -1;
+ if (timeout) {
+ timeout_ms = (int)(timeout->tv_sec * 1000 + timeout->tv_usec / 1000);
+ }
+
+ int ret = poll(pfds, (nfds_t)count, timeout_ms);
+ if (ret < 0) return -1;
+
+ // Clear fd_sets and scatter results
+ if (readfds) FD_ZERO(readfds);
+ if (writefds) FD_ZERO(writefds);
+ if (exceptfds) FD_ZERO(exceptfds);
+
+ int ready = 0;
+ for (int i = 0; i < count; i++) {
+ int fd = fd_map[i];
+ int any = 0;
+ if (readfds && (pfds[i].revents & (POLLIN | POLLHUP | POLLERR))) {
+ FD_SET(fd, readfds);
+ any = 1;
+ }
+ if (writefds && (pfds[i].revents & POLLOUT)) {
+ FD_SET(fd, writefds);
+ any = 1;
+ }
+ if (exceptfds && (pfds[i].revents & (POLLERR | POLLNVAL))) {
+ FD_SET(fd, exceptfds);
+ any = 1;
+ }
+ if (any) ready++;
+ }
+ return ready;
+}
diff --git a/libc-top-half/musl/include/sys/socket.h b/libc-top-half/musl/include/sys/socket.h
index 29189e3..b1957d4 100644
--- a/libc-top-half/musl/include/sys/socket.h
+++ b/libc-top-half/musl/include/sys/socket.h
@@ -398,9 +398,7 @@ struct sockaddr_storage {
#include <__struct_sockaddr_storage.h>
#endif
-#if (defined __wasilibc_unmodified_upstream) || (defined __wasilibc_use_wasip2)
int socket (int, int, int);
-#endif
#ifdef __wasilibc_unmodified_upstream /* WASI has no socketpair */
int socketpair (int, int, int, int [2]);
@@ -408,35 +406,27 @@ int socketpair (int, int, int, int [2]);
int shutdown (int, int);
-#if (defined __wasilibc_unmodified_upstream) || (defined __wasilibc_use_wasip2)
int connect (int, const struct sockaddr *, socklen_t);
int bind (int, const struct sockaddr *, socklen_t);
int listen (int, int);
-#endif
int accept (int, struct sockaddr *__restrict, socklen_t *__restrict);
int accept4(int, struct sockaddr *__restrict, socklen_t *__restrict, int);
-#if (defined __wasilibc_unmodified_upstream) || (defined __wasilibc_use_wasip2)
int getsockname (int, struct sockaddr *__restrict, socklen_t *__restrict);
int getpeername (int, struct sockaddr *__restrict, socklen_t *__restrict);
-#endif
ssize_t send (int, const void *, size_t, int);
ssize_t recv (int, void *, size_t, int);
-#if (defined __wasilibc_unmodified_upstream) || (defined __wasilibc_use_wasip2)
ssize_t sendto (int, const void *, size_t, int, const struct sockaddr *, socklen_t);
ssize_t recvfrom (int, void *__restrict, size_t, int, struct sockaddr *__restrict, socklen_t *__restrict);
-#endif
#ifdef __wasilibc_unmodified_upstream /* WASI has no sendmsg/recvmsg */
ssize_t sendmsg (int, const struct msghdr *, int);
ssize_t recvmsg (int, struct msghdr *, int);
#endif
int getsockopt (int, int, int, void *__restrict, socklen_t *__restrict);
-#if (defined __wasilibc_unmodified_upstream) || (defined __wasilibc_use_wasip2)
int setsockopt (int, int, int, const void *, socklen_t);
-#endif
#ifdef __wasilibc_unmodified_upstream /* WASI has no sockatmark */
int sockatmark (int);
diff --git a/scripts/install-include-headers.sh b/scripts/install-include-headers.sh
index efa48aa..57d2ac3 100755
--- a/scripts/install-include-headers.sh
+++ b/scripts/install-include-headers.sh
@@ -68,11 +68,11 @@ MUSL_OMIT_HEADERS+=("sys/procfs.h" "sys/user.h" "sys/kd.h" "sys/vt.h" \
"sys/termios.h" "bits/termios.h" "net/if.h" "net/if_arp.h" \
"net/ethernet.h" "net/route.h" "netinet/if_ether.h" "netinet/ether.h" \
"sys/timerfd.h" "libintl.h" "sys/sysmacros.h" "aio.h")
-# Exclude `netdb.h` from all of the p1 targets.
-if [[ $TARGET_TRIPLE == *"wasi" || $TARGET_TRIPLE == *"wasi-threads" || \
- $TARGET_TRIPLE == *"wasip1" || $TARGET_TRIPLE == *"wasip1-threads" ]]; then
- MUSL_OMIT_HEADERS+=("netdb.h")
-fi
+# NOTE: secure-exec provides getaddrinfo via host_net, so keep netdb.h.
+#if [[ $TARGET_TRIPLE == *"wasi" || $TARGET_TRIPLE == *"wasi-threads" || \
+# $TARGET_TRIPLE == *"wasip1" || $TARGET_TRIPLE == *"wasip1-threads" ]]; then
+# MUSL_OMIT_HEADERS+=("netdb.h")
+#fi
# Remove all the `MUSL_OMIT_HEADERS` previously copied over.
for OMIT_HEADER in "${MUSL_OMIT_HEADERS[@]}"; do