From 9b6332c359590ae4dcb1d95c217cd003de9c42b7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C5=8Dshin?= Date: Sat, 11 Dec 2021 04:54:06 +0000 Subject: [PATCH] newt: change framing to tag byte + 32-bit length --- pkg/urbit/include/vere/vere.h | 2 +- pkg/urbit/vere/newt.c | 49 ++++++++++++++++------------------- 2 files changed, 23 insertions(+), 28 deletions(-) diff --git a/pkg/urbit/include/vere/vere.h b/pkg/urbit/include/vere/vere.h index e3c24ea65d..9db525be09 100644 --- a/pkg/urbit/include/vere/vere.h +++ b/pkg/urbit/include/vere/vere.h @@ -67,7 +67,7 @@ u3_mess_type sat_e; // msg type union { // struct { // awaiting header: - c3_y len_y[8]; // header bytes + c3_y hed_y[5]; // header bytes c3_y has_y; // length } hed_u; // struct { // awaiting body diff --git a/pkg/urbit/vere/newt.c b/pkg/urbit/vere/newt.c index e501c5e7d4..7cfc18c133 100644 --- a/pkg/urbit/vere/newt.c +++ b/pkg/urbit/vere/newt.c @@ -2,9 +2,11 @@ ** ** implements noun blob messages with trivial framing. ** -** a message is a 64-bit little-endian byte count, followed -** by the indicated number of bytes. the bytes are the -** the +jam of of a noun. +** framing is 5 bytes long. first byte is a version tag; a +** character that should never otherwise appear in a message +** intended for urbit. right now we use 0x9, horizontal tab. +** next 32 bits are little-endian byte count. after that is the +** indicated number of bytes, which are the +jam of a noun. ** ** the implementation is relatively inefficient and could ** lose a few copies, mallocs, etc. @@ -117,15 +119,15 @@ u3_newt_decode(u3_moat* mot_u, c3_y* buf_y, c3_d len_d) while ( len_d ) { switch( mes_u->sat_e ) { - // read up to 8 length bytes as needed + // read up to 5 bytes as needed // case u3_mess_head: { - c3_y* len_y = mes_u->hed_u.len_y; + c3_y* hed_y = mes_u->hed_u.hed_y; c3_y has_y = mes_u->hed_u.has_y; - c3_y ned_y = 8 - has_y; + c3_y ned_y = sizeof(mes_u->hed_u.hed_y) - has_y; c3_y cop_y = c3_min(ned_y, len_d); - memcpy(len_y + has_y, buf_y, cop_y); + memcpy(hed_y + has_y, buf_y, cop_y); buf_y += cop_y; len_d -= cop_y; ned_y -= cop_y; @@ -138,18 +140,14 @@ u3_newt_decode(u3_moat* mot_u, c3_y* buf_y, c3_d len_d) // length known, allocate message // else { - c3_d met_d = (((c3_d)len_y[0]) << 0) - | (((c3_d)len_y[1]) << 8) - | (((c3_d)len_y[2]) << 16) - | (((c3_d)len_y[3]) << 24) - | (((c3_d)len_y[4]) << 32) - | (((c3_d)len_y[5]) << 40) - | (((c3_d)len_y[6]) << 48) - | (((c3_d)len_y[7]) << 56); + c3_d met_d = (((c3_d)hed_y[1]) << 0) + | (((c3_d)hed_y[2]) << 8) + | (((c3_d)hed_y[3]) << 16) + | (((c3_d)hed_y[4]) << 24); - // must be non-zero, only 32 bits supported + // check for version tag and nonzero length // - if ( !met_d || !(0xFFFFFFFFULL > met_d) ) { + if ( 0x9 != hed_y[0] || !met_d ) { return c3n; } @@ -379,7 +377,7 @@ typedef struct _n_req { uv_write_t wri_u; u3_mojo* moj_u; c3_y* buf_y; - c3_y len_y[8]; + c3_y hed_y[5]; } n_req; /* _newt_write_cb(): generic write callback. @@ -438,18 +436,15 @@ u3_newt_send(u3_mojo* moj_u, c3_d len_d, c3_y* byt_y) // write header // - req_u->len_y[0] = ( len_d & 0xff); - req_u->len_y[1] = ((len_d >> 8) & 0xff); - req_u->len_y[2] = ((len_d >> 16) & 0xff); - req_u->len_y[3] = ((len_d >> 24) & 0xff); - req_u->len_y[4] = ((len_d >> 32) & 0xff); - req_u->len_y[5] = ((len_d >> 40) & 0xff); - req_u->len_y[6] = ((len_d >> 48) & 0xff); - req_u->len_y[7] = ((len_d >> 56) & 0xff); + req_u->hed_y[0] = 0x9; + req_u->hed_y[1] = ( len_d & 0xff); + req_u->hed_y[2] = ((len_d >> 8) & 0xff); + req_u->hed_y[3] = ((len_d >> 16) & 0xff); + req_u->hed_y[4] = ((len_d >> 24) & 0xff); { uv_buf_t buf_u[2] = { - uv_buf_init((c3_c*)req_u->len_y, 8), + uv_buf_init((c3_c*)req_u->hed_y, sizeof(req_u->hed_y)), uv_buf_init((c3_c*)req_u->buf_y, len_d) };