Skip to content

Commit fced233

Browse files
kr-tdkouba-atym
andauthored
Improve networking (#4)
* Avoid redefinition warnings for CLOCK_MONOTONIC (#1) * Support get/set for flags on socket fds * Changes to support open62541 client/server * Implement functions required for pub/sub * Fix mismatch of enum sizes between WASM and host * Improve zephyr sockets * Add explanation comment --------- Signed-off-by: Dan Kouba <dan@atym.io> Signed-off-by: Krisztian Szilvasi <34309983+kr-t@users.noreply.github.com> Co-authored-by: Dan Kouba <dan@atym.io>
1 parent 3f4145e commit fced233

4 files changed

Lines changed: 153 additions & 57 deletions

File tree

core/shared/platform/include/platform_wasi_types.h

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -530,12 +530,6 @@ assert_wasi_layout(offsetof(__wasi_subscription_t, userdata) == 0, "witx calcula
530530
assert_wasi_layout(offsetof(__wasi_subscription_t, u) == 8, "witx calculated offset");
531531

532532
/* keep syncing with wasi_socket_ext.h */
533-
typedef enum {
534-
/* Used only for sock_addr_resolve hints */
535-
SOCKET_ANY = -1,
536-
SOCKET_DGRAM = 0,
537-
SOCKET_STREAM,
538-
} __wasi_sock_type_t;
539533

540534
typedef uint16_t __wasi_ip_port_t;
541535

@@ -589,20 +583,36 @@ typedef struct __wasi_addr_t {
589583
} addr;
590584
} __wasi_addr_t;
591585

592-
typedef enum { INET4 = 0, INET6, INET_UNSPEC } __wasi_address_family_t;
586+
/* Force 32-bit wire width for cross-boundary fields */
587+
typedef int32_t __wasi_sock_type_t;
588+
enum { SOCKET_ANY = -1, SOCKET_DGRAM = 0, SOCKET_STREAM = 1 };
589+
590+
typedef int32_t __wasi_address_family_t;
591+
enum { INET4 = 0, INET6 = 1, INET_UNSPEC = 2 };
593592

594593
typedef struct __wasi_addr_info_t {
595-
__wasi_addr_t addr;
594+
__wasi_addr_t addr;
596595
__wasi_sock_type_t type;
597596
} __wasi_addr_info_t;
598597

599598
typedef struct __wasi_addr_info_hints_t {
600-
__wasi_sock_type_t type;
601-
__wasi_address_family_t family;
602-
// this is to workaround lack of optional parameters
603-
uint8_t hints_enabled;
599+
__wasi_sock_type_t type; // 4 bytes
600+
__wasi_address_family_t family; // 4 bytes
601+
uint8_t hints_enabled; // 1 byte
602+
uint8_t _pad[3]; // enforce layout
604603
} __wasi_addr_info_hints_t;
605604

605+
assert_wasi_layout(sizeof(__wasi_sock_type_t) == 4, "sock_type must be 4 bytes");
606+
assert_wasi_layout(sizeof(__wasi_address_family_t) == 4, "addr_family must be 4 bytes");
607+
608+
assert_wasi_layout(sizeof(__wasi_addr_info_hints_t) == 12, "hints_t must be 12 bytes");
609+
assert_wasi_layout(offsetof(__wasi_addr_info_hints_t, type) == 0, "hints.type@0");
610+
assert_wasi_layout(offsetof(__wasi_addr_info_hints_t, family) == 4, "hints.family@4");
611+
assert_wasi_layout(offsetof(__wasi_addr_info_hints_t, hints_enabled) == 8, "hints.enabled@8");
612+
613+
assert_wasi_layout(offsetof(__wasi_addr_info_t, type) == sizeof(__wasi_addr_t),
614+
"addr_info.type follows addr");
615+
606616
#undef assert_wasi_layout
607617

608618
/* clang-format on */

core/shared/platform/zephyr/platform_internal.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -290,7 +290,9 @@ typedef struct timespec os_timespec;
290290
#define CLOCK_REALTIME 1
291291
#endif
292292

293+
#ifndef CLOCK_MONOTONIC
293294
#define CLOCK_MONOTONIC 4
295+
#endif
294296

295297
static inline int
296298
os_sched_yield(void)

core/shared/platform/zephyr/zephyr_file.c

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@
1515
#include <zephyr/fs/fs_sys.h>
1616
#include <zephyr/fs/littlefs.h>
1717

18+
#include <zephyr/net/socket.h>
19+
#include <zephyr/posix/fcntl.h> // F_GETFL, F_SETFL, O_NONBLOCK
20+
1821
/* Notes:
1922
* This is the implementation of a POSIX-like file system interface for Zephyr.
2023
* To manage our file descriptors, we created a struct `zephyr_fs_desc` that
@@ -273,6 +276,16 @@ os_file_get_fdflags(os_file_handle handle, __wasi_fdflags_t *flags)
273276
{
274277
struct zephyr_fs_desc *ptr = NULL;
275278

279+
/* Sockets: reflect current non-blocking state */
280+
if (handle->is_sock) {
281+
int cur = zsock_fcntl(handle->fd, F_GETFL, 0);
282+
if (cur < 0)
283+
return convert_errno(errno);
284+
if (cur & O_NONBLOCK)
285+
*flags |= __WASI_FDFLAG_NONBLOCK;
286+
return __WASI_ESUCCESS;
287+
}
288+
276289
if (os_is_virtual_fd(handle->fd)) {
277290
*flags = 0;
278291
return __WASI_ESUCCESS;
@@ -296,6 +309,21 @@ os_file_get_fdflags(os_file_handle handle, __wasi_fdflags_t *flags)
296309
__wasi_errno_t
297310
os_file_set_fdflags(os_file_handle handle, __wasi_fdflags_t flags)
298311
{
312+
/* Sockets: set/clear O_NONBLOCK */
313+
if (handle->is_sock) {
314+
int cur = zsock_fcntl(handle->fd, F_GETFL, 0);
315+
if (cur < 0)
316+
return convert_errno(errno);
317+
318+
bool want_nb = (flags & __WASI_FDFLAG_NONBLOCK) != 0;
319+
int newf = want_nb ? (cur | O_NONBLOCK) : (cur & ~O_NONBLOCK);
320+
321+
if (zsock_fcntl(handle->fd, F_SETFL, newf) < 0) {
322+
return convert_errno(errno);
323+
}
324+
return __WASI_ESUCCESS;
325+
}
326+
299327
if (os_is_virtual_fd(handle->fd)) {
300328
return __WASI_ESUCCESS;
301329
}
@@ -1195,4 +1223,4 @@ bool
11951223
os_is_stderr_handle(os_file_handle handle)
11961224
{
11971225
return (handle == (os_file_handle)stderr);
1198-
}
1226+
}

0 commit comments

Comments
 (0)