/src/WasmEdge/lib/host/wasi/linux.h
Line | Count | Source (jump to first uncovered line) |
1 | | // SPDX-License-Identifier: Apache-2.0 |
2 | | // SPDX-FileCopyrightText: 2019-2024 Second State INC |
3 | | |
4 | | #include "common/defines.h" |
5 | | #if !WASMEDGE_OS_LINUX |
6 | | #error |
7 | | #endif |
8 | | |
9 | | // Uncomment these flag to test CentOS 6 |
10 | | // #undef __GLIBC_MINOR__ |
11 | | // #define __GLIBC_MINOR__ 5 |
12 | | |
13 | | #include "common/errcode.h" |
14 | | #include "wasi/api.hpp" |
15 | | #include <cerrno> |
16 | | #include <chrono> |
17 | | #include <csignal> |
18 | | #include <cstdio> |
19 | | #include <ctime> |
20 | | #include <dirent.h> |
21 | | #include <fcntl.h> |
22 | | #include <sched.h> |
23 | | #include <sys/epoll.h> |
24 | | #include <sys/ioctl.h> |
25 | | #include <sys/time.h> |
26 | | #include <sys/types.h> |
27 | | #include <sys/uio.h> |
28 | | #include <sys/un.h> |
29 | | #include <sys/wait.h> |
30 | | #include <unistd.h> |
31 | | |
32 | | // socket include |
33 | | #include <arpa/inet.h> |
34 | | #include <netdb.h> |
35 | | #include <netinet/in.h> |
36 | | #include <sys/socket.h> |
37 | | |
38 | | #include <sys/stat.h> |
39 | | |
40 | | #if defined(__GLIBC_PREREQ) |
41 | | #if defined(_LIBCPP_GLIBC_PREREQ) |
42 | | #undef _LIBCPP_GLIBC_PREREQ |
43 | | #endif |
44 | | #define _LIBCPP_GLIBC_PREREQ(a, b) 0 |
45 | | #else |
46 | | #if defined(_LIBCPP_GLIBC_PREREQ) |
47 | | #define __GLIBC_PREREQ(a, b) _LIBCPP_GLIBC_PREREQ(a, b) |
48 | | #else |
49 | | #define __GLIBC_PREREQ(a, b) 1 |
50 | | #endif |
51 | | #endif |
52 | | |
53 | | #if __GLIBC_PREREQ(2, 8) |
54 | | #include <sys/timerfd.h> |
55 | | #endif |
56 | | |
57 | | namespace WasmEdge { |
58 | | namespace Host { |
59 | | namespace WASI { |
60 | | inline namespace detail { |
61 | | |
62 | 0 | inline constexpr __wasi_errno_t fromErrNo(int ErrNo) noexcept { |
63 | 0 | switch (ErrNo) { |
64 | 0 | case 0: |
65 | 0 | return __WASI_ERRNO_SUCCESS; |
66 | 0 | case E2BIG: |
67 | 0 | return __WASI_ERRNO_2BIG; |
68 | 0 | case EACCES: |
69 | 0 | return __WASI_ERRNO_ACCES; |
70 | 0 | case EADDRINUSE: |
71 | 0 | return __WASI_ERRNO_ADDRINUSE; |
72 | 0 | case EADDRNOTAVAIL: |
73 | 0 | return __WASI_ERRNO_ADDRNOTAVAIL; |
74 | 0 | case EAFNOSUPPORT: |
75 | 0 | return __WASI_ERRNO_AFNOSUPPORT; |
76 | 0 | case EAGAIN: |
77 | 0 | return __WASI_ERRNO_AGAIN; |
78 | 0 | case EALREADY: |
79 | 0 | return __WASI_ERRNO_ALREADY; |
80 | 0 | case EBADF: |
81 | 0 | return __WASI_ERRNO_BADF; |
82 | 0 | case EBADMSG: |
83 | 0 | return __WASI_ERRNO_BADMSG; |
84 | 0 | case EBUSY: |
85 | 0 | return __WASI_ERRNO_BUSY; |
86 | 0 | case ECANCELED: |
87 | 0 | return __WASI_ERRNO_CANCELED; |
88 | 0 | case ECHILD: |
89 | 0 | return __WASI_ERRNO_CHILD; |
90 | 0 | case ECONNABORTED: |
91 | 0 | return __WASI_ERRNO_CONNABORTED; |
92 | 0 | case ECONNREFUSED: |
93 | 0 | return __WASI_ERRNO_CONNREFUSED; |
94 | 0 | case ECONNRESET: |
95 | 0 | return __WASI_ERRNO_CONNRESET; |
96 | 0 | case EDEADLK: |
97 | 0 | return __WASI_ERRNO_DEADLK; |
98 | 0 | case EDESTADDRREQ: |
99 | 0 | return __WASI_ERRNO_DESTADDRREQ; |
100 | 0 | case EDOM: |
101 | 0 | return __WASI_ERRNO_DOM; |
102 | 0 | case EDQUOT: |
103 | 0 | return __WASI_ERRNO_DQUOT; |
104 | 0 | case EEXIST: |
105 | 0 | return __WASI_ERRNO_EXIST; |
106 | 0 | case EFAULT: |
107 | 0 | return __WASI_ERRNO_FAULT; |
108 | 0 | case EFBIG: |
109 | 0 | return __WASI_ERRNO_FBIG; |
110 | 0 | case EHOSTUNREACH: |
111 | 0 | return __WASI_ERRNO_HOSTUNREACH; |
112 | 0 | case EIDRM: |
113 | 0 | return __WASI_ERRNO_IDRM; |
114 | 0 | case EILSEQ: |
115 | 0 | return __WASI_ERRNO_ILSEQ; |
116 | 0 | case EINPROGRESS: |
117 | 0 | return __WASI_ERRNO_INPROGRESS; |
118 | 0 | case EINTR: |
119 | 0 | return __WASI_ERRNO_INTR; |
120 | 0 | case EINVAL: |
121 | 0 | return __WASI_ERRNO_INVAL; |
122 | 0 | case EIO: |
123 | 0 | return __WASI_ERRNO_IO; |
124 | 0 | case EISCONN: |
125 | 0 | return __WASI_ERRNO_ISCONN; |
126 | 0 | case EISDIR: |
127 | 0 | return __WASI_ERRNO_ISDIR; |
128 | 0 | case ELOOP: |
129 | 0 | return __WASI_ERRNO_LOOP; |
130 | 0 | case EMFILE: |
131 | 0 | return __WASI_ERRNO_MFILE; |
132 | 0 | case EMLINK: |
133 | 0 | return __WASI_ERRNO_MLINK; |
134 | 0 | case EMSGSIZE: |
135 | 0 | return __WASI_ERRNO_MSGSIZE; |
136 | 0 | case EMULTIHOP: |
137 | 0 | return __WASI_ERRNO_MULTIHOP; |
138 | 0 | case ENAMETOOLONG: |
139 | 0 | return __WASI_ERRNO_NAMETOOLONG; |
140 | 0 | case ENETDOWN: |
141 | 0 | return __WASI_ERRNO_NETDOWN; |
142 | 0 | case ENETRESET: |
143 | 0 | return __WASI_ERRNO_NETRESET; |
144 | 0 | case ENETUNREACH: |
145 | 0 | return __WASI_ERRNO_NETUNREACH; |
146 | 0 | case ENFILE: |
147 | 0 | return __WASI_ERRNO_NFILE; |
148 | 0 | case ENOBUFS: |
149 | 0 | return __WASI_ERRNO_NOBUFS; |
150 | 0 | case ENODEV: |
151 | 0 | return __WASI_ERRNO_NODEV; |
152 | 0 | case ENOENT: |
153 | 0 | return __WASI_ERRNO_NOENT; |
154 | 0 | case ENOEXEC: |
155 | 0 | return __WASI_ERRNO_NOEXEC; |
156 | 0 | case ENOLCK: |
157 | 0 | return __WASI_ERRNO_NOLCK; |
158 | 0 | case ENOLINK: |
159 | 0 | return __WASI_ERRNO_NOLINK; |
160 | 0 | case ENOMEM: |
161 | 0 | return __WASI_ERRNO_NOMEM; |
162 | 0 | case ENOMSG: |
163 | 0 | return __WASI_ERRNO_NOMSG; |
164 | 0 | case ENOPROTOOPT: |
165 | 0 | return __WASI_ERRNO_NOPROTOOPT; |
166 | 0 | case ENOSPC: |
167 | 0 | return __WASI_ERRNO_NOSPC; |
168 | 0 | case ENOSYS: |
169 | 0 | return __WASI_ERRNO_NOSYS; |
170 | 0 | case ENOTCONN: |
171 | 0 | return __WASI_ERRNO_NOTCONN; |
172 | 0 | case ENOTDIR: |
173 | 0 | return __WASI_ERRNO_NOTDIR; |
174 | 0 | case ENOTEMPTY: |
175 | 0 | return __WASI_ERRNO_NOTEMPTY; |
176 | 0 | case ENOTRECOVERABLE: |
177 | 0 | return __WASI_ERRNO_NOTRECOVERABLE; |
178 | 0 | case ENOTSOCK: |
179 | 0 | return __WASI_ERRNO_NOTSOCK; |
180 | 0 | case ENOTSUP: |
181 | 0 | return __WASI_ERRNO_NOTSUP; |
182 | 0 | case ENOTTY: |
183 | 0 | return __WASI_ERRNO_NOTTY; |
184 | 0 | case ENXIO: |
185 | 0 | return __WASI_ERRNO_NXIO; |
186 | 0 | case EOVERFLOW: |
187 | 0 | return __WASI_ERRNO_OVERFLOW; |
188 | 0 | case EOWNERDEAD: |
189 | 0 | return __WASI_ERRNO_OWNERDEAD; |
190 | 0 | case EPERM: |
191 | 0 | return __WASI_ERRNO_PERM; |
192 | 0 | case EPIPE: |
193 | 0 | return __WASI_ERRNO_PIPE; |
194 | 0 | case EPROTO: |
195 | 0 | return __WASI_ERRNO_PROTO; |
196 | 0 | case EPROTONOSUPPORT: |
197 | 0 | return __WASI_ERRNO_PROTONOSUPPORT; |
198 | 0 | case EPROTOTYPE: |
199 | 0 | return __WASI_ERRNO_PROTOTYPE; |
200 | 0 | case ERANGE: |
201 | 0 | return __WASI_ERRNO_RANGE; |
202 | 0 | case EROFS: |
203 | 0 | return __WASI_ERRNO_ROFS; |
204 | 0 | case ESPIPE: |
205 | 0 | return __WASI_ERRNO_SPIPE; |
206 | 0 | case ESRCH: |
207 | 0 | return __WASI_ERRNO_SRCH; |
208 | 0 | case ESTALE: |
209 | 0 | return __WASI_ERRNO_STALE; |
210 | 0 | case ETIMEDOUT: |
211 | 0 | return __WASI_ERRNO_TIMEDOUT; |
212 | 0 | case ETXTBSY: |
213 | 0 | return __WASI_ERRNO_TXTBSY; |
214 | 0 | case EXDEV: |
215 | 0 | return __WASI_ERRNO_XDEV; |
216 | 0 | default: |
217 | 0 | assumingUnreachable(); |
218 | 0 | } |
219 | 0 | } |
220 | | |
221 | 0 | inline constexpr __wasi_errno_t fromEAIErrNo(int ErrNo) noexcept { |
222 | 0 | switch (ErrNo) { |
223 | 0 | case EAI_ADDRFAMILY: |
224 | 0 | return __WASI_ERRNO_AIADDRFAMILY; |
225 | 0 | case EAI_AGAIN: |
226 | 0 | return __WASI_ERRNO_AIAGAIN; |
227 | 0 | case EAI_BADFLAGS: |
228 | 0 | return __WASI_ERRNO_AIBADFLAG; |
229 | 0 | case EAI_FAIL: |
230 | 0 | return __WASI_ERRNO_AIFAIL; |
231 | 0 | case EAI_FAMILY: |
232 | 0 | return __WASI_ERRNO_AIFAMILY; |
233 | 0 | case EAI_MEMORY: |
234 | 0 | return __WASI_ERRNO_AIMEMORY; |
235 | 0 | case EAI_NODATA: |
236 | 0 | return __WASI_ERRNO_AINODATA; |
237 | 0 | case EAI_NONAME: |
238 | 0 | return __WASI_ERRNO_AINONAME; |
239 | 0 | case EAI_SERVICE: |
240 | 0 | return __WASI_ERRNO_AISERVICE; |
241 | 0 | case EAI_SOCKTYPE: |
242 | 0 | return __WASI_ERRNO_AISOCKTYPE; |
243 | 0 | case EAI_SYSTEM: |
244 | 0 | return __WASI_ERRNO_AISYSTEM; |
245 | 0 | default: |
246 | 0 | assumingUnreachable(); |
247 | 0 | } |
248 | 0 | } |
249 | | |
250 | 0 | inline constexpr clockid_t toClockId(__wasi_clockid_t Clock) noexcept { |
251 | 0 | switch (Clock) { |
252 | 0 | case __WASI_CLOCKID_REALTIME: |
253 | 0 | return CLOCK_REALTIME; |
254 | 0 | case __WASI_CLOCKID_MONOTONIC: |
255 | 0 | return CLOCK_MONOTONIC; |
256 | 0 | case __WASI_CLOCKID_PROCESS_CPUTIME_ID: |
257 | 0 | return CLOCK_PROCESS_CPUTIME_ID; |
258 | 0 | case __WASI_CLOCKID_THREAD_CPUTIME_ID: |
259 | 0 | return CLOCK_THREAD_CPUTIME_ID; |
260 | 0 | default: |
261 | 0 | assumingUnreachable(); |
262 | 0 | } |
263 | 0 | } |
264 | | |
265 | 0 | inline constexpr timespec toTimespec(__wasi_timestamp_t Timestamp) noexcept { |
266 | 0 | using namespace std::chrono; |
267 | 0 | const auto Total = duration<uint64_t, std::nano>(Timestamp); |
268 | 0 | const auto Second = duration_cast<seconds>(Total); |
269 | 0 | const auto Nano = Total - Second; |
270 | 0 | timespec Result{}; |
271 | 0 | Result.tv_sec = Second.count(); |
272 | 0 | Result.tv_nsec = Nano.count(); |
273 | 0 | return Result; |
274 | 0 | } |
275 | | |
276 | | inline constexpr __wasi_timestamp_t |
277 | 0 | fromTimespec(const timespec &Time) noexcept { |
278 | 0 | using namespace std::chrono; |
279 | 0 | const auto Result = seconds(Time.tv_sec) + nanoseconds(Time.tv_nsec); |
280 | 0 | return Result.count(); |
281 | 0 | } |
282 | | |
283 | | #if !__GLIBC_PREREQ(2, 6) |
284 | | inline constexpr timeval toTimeval(__wasi_timestamp_t Timestamp) noexcept { |
285 | | using namespace std::chrono; |
286 | | const auto Total = duration_cast<microseconds>(nanoseconds(Timestamp)); |
287 | | const auto Second = duration_cast<seconds>(Total); |
288 | | const auto Micro = Total - Second; |
289 | | timeval Result{}; |
290 | | Result.tv_sec = Second.count(); |
291 | | Result.tv_usec = Micro.count(); |
292 | | return Result; |
293 | | } |
294 | | inline constexpr timeval toTimeval(timespec Timespec) noexcept { |
295 | | using namespace std::chrono; |
296 | | timeval Result{}; |
297 | | Result.tv_sec = Timespec.tv_sec; |
298 | | Result.tv_usec = |
299 | | duration_cast<microseconds>(nanoseconds(Timespec.tv_nsec)).count(); |
300 | | return Result; |
301 | | } |
302 | | #endif |
303 | | |
304 | 0 | inline constexpr int toAdvice(__wasi_advice_t Advice) noexcept { |
305 | 0 | switch (Advice) { |
306 | 0 | case __WASI_ADVICE_NORMAL: |
307 | 0 | return POSIX_FADV_NORMAL; |
308 | 0 | case __WASI_ADVICE_SEQUENTIAL: |
309 | 0 | return POSIX_FADV_SEQUENTIAL; |
310 | 0 | case __WASI_ADVICE_RANDOM: |
311 | 0 | return POSIX_FADV_RANDOM; |
312 | 0 | case __WASI_ADVICE_WILLNEED: |
313 | 0 | return POSIX_FADV_WILLNEED; |
314 | 0 | case __WASI_ADVICE_DONTNEED: |
315 | 0 | return POSIX_FADV_DONTNEED; |
316 | 0 | case __WASI_ADVICE_NOREUSE: |
317 | 0 | return POSIX_FADV_NOREUSE; |
318 | 0 | default: |
319 | 0 | assumingUnreachable(); |
320 | 0 | } |
321 | 0 | } |
322 | | |
323 | 0 | inline constexpr __wasi_filetype_t fromFileType(mode_t Mode) noexcept { |
324 | 0 | switch (Mode & S_IFMT) { |
325 | 0 | case S_IFBLK: |
326 | 0 | return __WASI_FILETYPE_BLOCK_DEVICE; |
327 | 0 | case S_IFCHR: |
328 | 0 | return __WASI_FILETYPE_CHARACTER_DEVICE; |
329 | 0 | case S_IFDIR: |
330 | 0 | return __WASI_FILETYPE_DIRECTORY; |
331 | 0 | case S_IFREG: |
332 | 0 | return __WASI_FILETYPE_REGULAR_FILE; |
333 | 0 | case S_IFSOCK: |
334 | 0 | return __WASI_FILETYPE_SOCKET_STREAM; |
335 | 0 | case S_IFLNK: |
336 | 0 | return __WASI_FILETYPE_SYMBOLIC_LINK; |
337 | 0 | case S_IFIFO: |
338 | 0 | default: |
339 | 0 | return __WASI_FILETYPE_UNKNOWN; |
340 | 0 | } |
341 | 0 | } |
342 | | |
343 | 0 | inline constexpr __wasi_filetype_t fromFileType(uint8_t Type) noexcept { |
344 | 0 | switch (Type) { |
345 | 0 | case DT_BLK: |
346 | 0 | return __WASI_FILETYPE_BLOCK_DEVICE; |
347 | 0 | case DT_CHR: |
348 | 0 | return __WASI_FILETYPE_CHARACTER_DEVICE; |
349 | 0 | case DT_DIR: |
350 | 0 | return __WASI_FILETYPE_DIRECTORY; |
351 | 0 | case DT_LNK: |
352 | 0 | return __WASI_FILETYPE_SYMBOLIC_LINK; |
353 | 0 | case DT_REG: |
354 | 0 | return __WASI_FILETYPE_REGULAR_FILE; |
355 | 0 | case DT_SOCK: |
356 | 0 | return __WASI_FILETYPE_SOCKET_STREAM; |
357 | 0 | case DT_FIFO: |
358 | 0 | case DT_UNKNOWN: |
359 | 0 | default: |
360 | 0 | return __WASI_FILETYPE_UNKNOWN; |
361 | 0 | } |
362 | 0 | } |
363 | | |
364 | 0 | inline constexpr int toWhence(__wasi_whence_t Whence) noexcept { |
365 | 0 | switch (Whence) { |
366 | 0 | case __WASI_WHENCE_CUR: |
367 | 0 | return SEEK_CUR; |
368 | 0 | case __WASI_WHENCE_END: |
369 | 0 | return SEEK_END; |
370 | 0 | case __WASI_WHENCE_SET: |
371 | 0 | return SEEK_SET; |
372 | 0 | default: |
373 | 0 | assumingUnreachable(); |
374 | 0 | } |
375 | 0 | } |
376 | | |
377 | 0 | inline constexpr int toSockOptLevel(__wasi_sock_opt_level_t Level) noexcept { |
378 | 0 | switch (Level) { |
379 | 0 | case __WASI_SOCK_OPT_LEVEL_SOL_SOCKET: |
380 | 0 | return SOL_SOCKET; |
381 | 0 | default: |
382 | 0 | assumingUnreachable(); |
383 | 0 | } |
384 | 0 | } |
385 | | |
386 | 0 | inline constexpr int toSockOptSoName(__wasi_sock_opt_so_t SoName) noexcept { |
387 | 0 | switch (SoName) { |
388 | 0 | case __WASI_SOCK_OPT_SO_REUSEADDR: |
389 | 0 | return SO_REUSEADDR; |
390 | 0 | case __WASI_SOCK_OPT_SO_TYPE: |
391 | 0 | return SO_TYPE; |
392 | 0 | case __WASI_SOCK_OPT_SO_ERROR: |
393 | 0 | return SO_ERROR; |
394 | 0 | case __WASI_SOCK_OPT_SO_DONTROUTE: |
395 | 0 | return SO_DONTROUTE; |
396 | 0 | case __WASI_SOCK_OPT_SO_BROADCAST: |
397 | 0 | return SO_BROADCAST; |
398 | 0 | case __WASI_SOCK_OPT_SO_SNDBUF: |
399 | 0 | return SO_SNDBUF; |
400 | 0 | case __WASI_SOCK_OPT_SO_RCVBUF: |
401 | 0 | return SO_RCVBUF; |
402 | 0 | case __WASI_SOCK_OPT_SO_KEEPALIVE: |
403 | 0 | return SO_KEEPALIVE; |
404 | 0 | case __WASI_SOCK_OPT_SO_OOBINLINE: |
405 | 0 | return SO_OOBINLINE; |
406 | 0 | case __WASI_SOCK_OPT_SO_LINGER: |
407 | 0 | return SO_LINGER; |
408 | 0 | case __WASI_SOCK_OPT_SO_RCVLOWAT: |
409 | 0 | return SO_RCVLOWAT; |
410 | 0 | case __WASI_SOCK_OPT_SO_RCVTIMEO: |
411 | 0 | return SO_RCVTIMEO; |
412 | 0 | case __WASI_SOCK_OPT_SO_SNDTIMEO: |
413 | 0 | return SO_SNDTIMEO; |
414 | 0 | case __WASI_SOCK_OPT_SO_ACCEPTCONN: |
415 | 0 | return SO_ACCEPTCONN; |
416 | 0 | case __WASI_SOCK_OPT_SO_BINDTODEVICE: |
417 | 0 | return SO_BINDTODEVICE; |
418 | | |
419 | 0 | default: |
420 | 0 | assumingUnreachable(); |
421 | 0 | } |
422 | 0 | } |
423 | | |
424 | 0 | inline constexpr __wasi_aiflags_t fromAIFlags(int AIFlags) noexcept { |
425 | 0 | __wasi_aiflags_t Result = static_cast<__wasi_aiflags_t>(0); |
426 | |
|
427 | 0 | if (AIFlags & AI_PASSIVE) { |
428 | 0 | Result |= __WASI_AIFLAGS_AI_PASSIVE; |
429 | 0 | } |
430 | 0 | if (AIFlags & AI_CANONNAME) { |
431 | 0 | Result |= __WASI_AIFLAGS_AI_CANONNAME; |
432 | 0 | } |
433 | 0 | if (AIFlags & AI_NUMERICHOST) { |
434 | 0 | Result |= __WASI_AIFLAGS_AI_NUMERICHOST; |
435 | 0 | } |
436 | 0 | if (AIFlags & AI_NUMERICSERV) { |
437 | 0 | Result |= __WASI_AIFLAGS_AI_NUMERICSERV; |
438 | 0 | } |
439 | 0 | if (AIFlags & AI_V4MAPPED) { |
440 | 0 | Result |= __WASI_AIFLAGS_AI_V4MAPPED; |
441 | 0 | } |
442 | 0 | if (AIFlags & AI_ALL) { |
443 | 0 | Result |= __WASI_AIFLAGS_AI_ALL; |
444 | 0 | } |
445 | 0 | if (AIFlags & AI_ADDRCONFIG) { |
446 | 0 | Result |= __WASI_AIFLAGS_AI_ADDRCONFIG; |
447 | 0 | } |
448 | |
|
449 | 0 | return Result; |
450 | 0 | } |
451 | | |
452 | 0 | inline constexpr int toAIFlags(__wasi_aiflags_t AIFlags) noexcept { |
453 | 0 | int Result = 0; |
454 | |
|
455 | 0 | if (AIFlags & __WASI_AIFLAGS_AI_PASSIVE) { |
456 | 0 | Result |= AI_PASSIVE; |
457 | 0 | } |
458 | 0 | if (AIFlags & __WASI_AIFLAGS_AI_CANONNAME) { |
459 | 0 | Result |= AI_CANONNAME; |
460 | 0 | } |
461 | 0 | if (AIFlags & __WASI_AIFLAGS_AI_NUMERICHOST) { |
462 | 0 | Result |= AI_NUMERICHOST; |
463 | 0 | } |
464 | 0 | if (AIFlags & __WASI_AIFLAGS_AI_NUMERICSERV) { |
465 | 0 | Result |= AI_NUMERICSERV; |
466 | 0 | } |
467 | 0 | if (AIFlags & __WASI_AIFLAGS_AI_V4MAPPED) { |
468 | 0 | Result |= AI_V4MAPPED; |
469 | 0 | } |
470 | 0 | if (AIFlags & __WASI_AIFLAGS_AI_ALL) { |
471 | 0 | Result |= AI_ALL; |
472 | 0 | } |
473 | 0 | if (AIFlags & __WASI_AIFLAGS_AI_ADDRCONFIG) { |
474 | 0 | Result |= AI_ADDRCONFIG; |
475 | 0 | } |
476 | |
|
477 | 0 | return Result; |
478 | 0 | } |
479 | | |
480 | 0 | inline constexpr __wasi_sock_type_t fromSockType(int SockType) noexcept { |
481 | 0 | switch (SockType) { |
482 | 0 | case 0: |
483 | 0 | return __WASI_SOCK_TYPE_SOCK_ANY; |
484 | 0 | case SOCK_DGRAM: |
485 | 0 | return __WASI_SOCK_TYPE_SOCK_DGRAM; |
486 | 0 | case SOCK_STREAM: |
487 | 0 | return __WASI_SOCK_TYPE_SOCK_STREAM; |
488 | 0 | default: |
489 | 0 | assumingUnreachable(); |
490 | 0 | } |
491 | 0 | } |
492 | | |
493 | 0 | inline constexpr int toSockType(__wasi_sock_type_t SockType) noexcept { |
494 | 0 | switch (SockType) { |
495 | 0 | case __WASI_SOCK_TYPE_SOCK_ANY: |
496 | 0 | return 0; |
497 | 0 | case __WASI_SOCK_TYPE_SOCK_DGRAM: |
498 | 0 | return SOCK_DGRAM; |
499 | 0 | case __WASI_SOCK_TYPE_SOCK_STREAM: |
500 | 0 | return SOCK_STREAM; |
501 | 0 | default: |
502 | 0 | assumingUnreachable(); |
503 | 0 | } |
504 | 0 | } |
505 | | |
506 | 0 | inline constexpr __wasi_protocol_t fromProtocol(int Protocol) noexcept { |
507 | 0 | switch (Protocol) { |
508 | 0 | case IPPROTO_IP: |
509 | 0 | return __WASI_PROTOCOL_IPPROTO_IP; |
510 | 0 | case IPPROTO_TCP: |
511 | 0 | return __WASI_PROTOCOL_IPPROTO_TCP; |
512 | 0 | case IPPROTO_UDP: |
513 | 0 | return __WASI_PROTOCOL_IPPROTO_UDP; |
514 | 0 | default: |
515 | 0 | assumingUnreachable(); |
516 | 0 | } |
517 | 0 | } |
518 | | |
519 | 0 | inline constexpr int toProtocol(__wasi_protocol_t Protocol) noexcept { |
520 | 0 | switch (Protocol) { |
521 | 0 | case __WASI_PROTOCOL_IPPROTO_IP: |
522 | 0 | return IPPROTO_IP; |
523 | 0 | case __WASI_PROTOCOL_IPPROTO_TCP: |
524 | 0 | return IPPROTO_TCP; |
525 | 0 | case __WASI_PROTOCOL_IPPROTO_UDP: |
526 | 0 | return IPPROTO_UDP; |
527 | 0 | default: |
528 | 0 | assumingUnreachable(); |
529 | 0 | } |
530 | 0 | } |
531 | | |
532 | | inline constexpr __wasi_address_family_t |
533 | 0 | fromAddressFamily(int AddressFamily) noexcept { |
534 | 0 | switch (AddressFamily) { |
535 | 0 | case PF_UNSPEC: |
536 | 0 | return __WASI_ADDRESS_FAMILY_UNSPEC; |
537 | 0 | case PF_INET: |
538 | 0 | return __WASI_ADDRESS_FAMILY_INET4; |
539 | 0 | case PF_INET6: |
540 | 0 | return __WASI_ADDRESS_FAMILY_INET6; |
541 | 0 | case PF_UNIX: |
542 | 0 | return __WASI_ADDRESS_FAMILY_AF_UNIX; |
543 | 0 | default: |
544 | 0 | assumingUnreachable(); |
545 | 0 | } |
546 | 0 | } |
547 | | |
548 | | inline constexpr int |
549 | 0 | toAddressFamily(__wasi_address_family_t AddressFamily) noexcept { |
550 | 0 | switch (AddressFamily) { |
551 | 0 | case __WASI_ADDRESS_FAMILY_UNSPEC: |
552 | 0 | return PF_UNSPEC; |
553 | 0 | case __WASI_ADDRESS_FAMILY_INET4: |
554 | 0 | return PF_INET; |
555 | 0 | case __WASI_ADDRESS_FAMILY_INET6: |
556 | 0 | return PF_INET6; |
557 | 0 | case __WASI_ADDRESS_FAMILY_AF_UNIX: |
558 | 0 | return PF_UNIX; |
559 | 0 | default: |
560 | 0 | assumingUnreachable(); |
561 | 0 | } |
562 | 0 | } |
563 | | |
564 | | } // namespace detail |
565 | | } // namespace WASI |
566 | | } // namespace Host |
567 | | } // namespace WasmEdge |