From 1e8847c613a1313de24c0f13431dcad518b4f960 Mon Sep 17 00:00:00 2001 From: lukechampine Date: Sat, 7 Oct 2023 19:57:35 -0400 Subject: [PATCH 001/107] jets: jet blake3 --- WORKSPACE.bazel | 4 ++-- pkg/noun/jets/e/blake.c | 47 +++++++++++++++++++++++++++++++++++++---- pkg/noun/jets/tree.c | 15 ++++++++++++- pkg/noun/jets/w.h | 3 ++- 4 files changed, 61 insertions(+), 8 deletions(-) diff --git a/WORKSPACE.bazel b/WORKSPACE.bazel index c5a66ad095..7d60159925 100644 --- a/WORKSPACE.bazel +++ b/WORKSPACE.bazel @@ -299,10 +299,10 @@ versioned_http_file( versioned_http_archive( name = "urcrypt", build_file = "//bazel/third_party/urcrypt:urcrypt.BUILD", - sha256 = "00ec597c14c418802d5db2d6a68cf83bd4f5419071b95f979374d3184599d6c8", + sha256 = "e659d3f3eac67dbf47e7e21860c99198c9f386b480e30d43547d1c6cb8974b24", strip_prefix = "urcrypt-{version}", url = "https://github.com/urbit/urcrypt/archive/{version}.tar.gz", - version = "b970baefa6e0a680fffa2b2ee19c956a4ae20355", + version = "c1ab781ae888730da06d998c80bfb2130232e528", ) versioned_http_archive( diff --git a/pkg/noun/jets/e/blake.c b/pkg/noun/jets/e/blake.c index a0f06f2186..fcdfed5a53 100644 --- a/pkg/noun/jets/e/blake.c +++ b/pkg/noun/jets/e/blake.c @@ -6,9 +6,8 @@ #include "noun.h" #include "urcrypt.h" - static u3_atom - _cqe_blake(u3_atom wid, u3_atom dat, + _cqe_blake2b(u3_atom wid, u3_atom dat, u3_atom wik, u3_atom dak, u3_atom out) { @@ -39,7 +38,7 @@ } u3_noun - u3we_blake(u3_noun cor) + u3we_blake2b(u3_noun cor) { u3_noun msg, key, out, // arguments wid, dat, // destructured msg @@ -54,6 +53,46 @@ { return u3m_bail(c3__exit); } else { - return u3l_punt("blake", _cqe_blake(wid, dat, wik, dak, out)); + return u3l_punt("blake2b", _cqe_blake2b(wid, dat, wik, dak, out)); + } + } + + static u3_atom + _cqe_blake3_hash(u3_atom wid, u3_atom dat, + u3_atom key, u3_atom out) + { + c3_w wid_w; + if ( !u3r_word_fit(&wid_w, wid) ) { + // impossible to represent an atom this large + return u3m_bail(c3__fail); + } + else { + c3_y out_y[64], key_y[32]; + c3_w out_w = c3_max(1, c3_min(out, 64)); + c3_y *dat_y = u3r_bytes_alloc(0, wid_w, dat); + u3r_bytes(0, 32, key_y, key); + urcrypt_blake3_hash(wid_w, dat_y, key_y, out_w, out_y); + u3a_free(dat_y); + return u3i_bytes(out_w, out_y); + } + } + + u3_noun + u3we_blake3_hash(u3_noun cor) + { + u3_noun out, msg, // arguments + wid, dat, // destructured msg + key; // context + + if ( c3n == u3r_mean(cor, u3x_sam_2, &out, + u3x_sam_3, &msg, + u3x_con_sam_2, &key, 0) || + u3ud(out) || + u3r_cell(msg, &wid, &dat) || u3ud(wid) || u3ud(dat) || + u3ud(key)) + { + return u3m_bail(c3__exit); + } else { + return u3l_punt("blake3_hash", _cqe_blake3_hash(wid, dat, key, out)); } } diff --git a/pkg/noun/jets/tree.c b/pkg/noun/jets/tree.c index c15e29ba8e..edcbcea41a 100644 --- a/pkg/noun/jets/tree.c +++ b/pkg/noun/jets/tree.c @@ -445,13 +445,26 @@ static c3_c* _140_hex_secp_ha[] = { 0 }; - static u3j_harm _140_hex_blake2b_a[] = {{".2", u3we_blake, c3y}, {}}; + static u3j_harm _140_hex_blake2b_a[] = {{".2", u3we_blake2b, c3y}, {}}; static c3_c* _140_hex_blake2b_ha[] = { "c432216ca53b5ad2284259167952761bb1046e280268c4d3b9ca70a2024e1934", 0 }; + static u3j_harm _140_hex_blake3_hash_a[] = {{".2", u3we_blake3_hash, c3y}, {}}; + static c3_c* _140_hex_blake3_hash_ha[] = { + "c432216ca53b5ad2284259167952761bb1046e280268c4d3b9ca70a2024e1934", + 0 + }; + static u3j_core _140_hex_blake3_d[] = + { { "hash", 7, _140_hex_blake3_hash_a, 0, _140_hex_blake3_hash_ha }, + {} + }; + static c3_c* _140_hex_blake3_ha[] = { 0 }; static u3j_core _140_hex_blake_d[] = { { "blake2b", 7, _140_hex_blake2b_a, 0, _140_hex_blake2b_ha }, + + { "blake3", 7, 0, _140_hex_blake3_d, _140_hex_blake3_ha }, + { "hash", 7, _140_hex_blake3_hash_a, 0, _140_hex_blake3_hash_ha }, {} }; static c3_c* _140_hex_blake_ha[] = { diff --git a/pkg/noun/jets/w.h b/pkg/noun/jets/w.h index d838416c03..b9b25a2dda 100644 --- a/pkg/noun/jets/w.h +++ b/pkg/noun/jets/w.h @@ -205,7 +205,8 @@ u3_noun u3we_argon2(u3_noun); - u3_noun u3we_blake(u3_noun); + u3_noun u3we_blake2b(u3_noun); + u3_noun u3we_blake3_hash(u3_noun); u3_noun u3we_ripe(u3_noun); From 964c9cdb31f5b76189962af45180edfd21e2b943 Mon Sep 17 00:00:00 2001 From: fang Date: Mon, 23 Oct 2023 21:56:19 +0200 Subject: [PATCH 002/107] vere: add retry logic to _king_curl_bytes If the curl fails or receives a non-2xx status code, it retries, backing off a little bit, until it either succeeds, or exceeds five attempts. --- pkg/vere/king.c | 39 +++++++++++++++++++++++---------------- 1 file changed, 23 insertions(+), 16 deletions(-) diff --git a/pkg/vere/king.c b/pkg/vere/king.c index 7446ae7199..74ff7f782d 100644 --- a/pkg/vere/king.c +++ b/pkg/vere/king.c @@ -251,6 +251,7 @@ _king_curl_bytes(c3_c* url_c, c3_w* len_w, c3_y** hun_y, c3_t veb_t) CURL *cul_u; CURLcode res_i; long cod_i; + c3_y try_y = 0; uv_buf_t buf_u = uv_buf_init(c3_malloc(1), 0); if ( !(cul_u = curl_easy_init()) ) { @@ -263,29 +264,34 @@ _king_curl_bytes(c3_c* url_c, c3_w* len_w, c3_y** hun_y, c3_t veb_t) curl_easy_setopt(cul_u, CURLOPT_WRITEFUNCTION, _king_curl_alloc); curl_easy_setopt(cul_u, CURLOPT_WRITEDATA, (void*)&buf_u); - res_i = curl_easy_perform(cul_u); - curl_easy_getinfo(cul_u, CURLINFO_RESPONSE_CODE, &cod_i); + while ( 5 > try_y ) { + sleep(try_y++); + res_i = curl_easy_perform(cul_u); + curl_easy_getinfo(cul_u, CURLINFO_RESPONSE_CODE, &cod_i); - // XX retry? - // - if ( CURLE_OK != res_i ) { - if ( veb_t ) { - u3l_log("curl: failed %s: %s", url_c, curl_easy_strerror(res_i)); + if ( CURLE_OK != res_i ) { + if ( veb_t ) { + u3l_log("curl: failed to fetch %s: %s", + url_c, curl_easy_strerror(res_i)); + } + ret_i = -1; } - ret_i = -1; - } - if ( 300 <= cod_i ) { - if ( veb_t ) { - u3l_log("curl: error %s: HTTP %ld", url_c, cod_i); + else if ( 300 <= cod_i ) { + if ( veb_t ) { + u3l_log("curl: error fetching %s: HTTP %ld", url_c, cod_i); + } + ret_i = -2; + } + else { + *len_w = buf_u.len; + *hun_y = (c3_y*)buf_u.base; + ret_i = 0; + break; } - ret_i = -2; } curl_easy_cleanup(cul_u); - *len_w = buf_u.len; - *hun_y = (c3_y*)buf_u.base; - return ret_i; } @@ -376,6 +382,7 @@ u3_king_next(c3_c* pac_c, c3_c** out_c) } // skip printfs on failed requests (/next is usually not present) + //REVIEW new retry logic means this case will take longer. make retries optional? // if ( _king_curl_bytes(url_c, &len_w, &hun_y, 0) ) { c3_free(url_c); From e5bbeed7295d6b838271d4c84ef3c4a4c9870832 Mon Sep 17 00:00:00 2001 From: fang Date: Mon, 23 Oct 2023 22:13:00 +0200 Subject: [PATCH 003/107] vere: bring your own boot sequence Adds support for --prop-file, --prop-url and --prop-name, which let one augment the boot sequence of a ship with additional events. Back-ports the vere changes from urbit/urbit#5470 onto current vere. See that for additional details/history. This is a mostly straightforward, copy-paste port of the changes from that original PR, with the following exceptions: - Boot sequence construction now happens in _pier_boot_make. - We don't modify the way the default boot sequence is acquired (yet). We will want to follow up on the latter, eventually. The %autoprop "prop builder" that was included in #5470 should still operate fine and be useful for automating keeping certain kinds of props up to date. (Save for minimal kelvin compatability disrepair it has fallen into due to the flow of time.) --- pkg/c3/motes.h | 1 + pkg/vere/king.c | 51 ++++++++++++++++++++++++++++++++++++++++++++-- pkg/vere/main.c | 46 +++++++++++++++++++++++++++++++++++++++++ pkg/vere/pier.c | 54 ++++++++++++++++++++++++++++++++++++++++++++----- pkg/vere/vere.h | 12 ++++++++++- 5 files changed, 156 insertions(+), 8 deletions(-) diff --git a/pkg/c3/motes.h b/pkg/c3/motes.h index 0000d5c593..4241c7f09d 100644 --- a/pkg/c3/motes.h +++ b/pkg/c3/motes.h @@ -936,6 +936,7 @@ # define c3__pril c3_s4('p','r','i','l') # define c3__pro c3_s3('p','r','o') # define c3__prod c3_s4('p','r','o','d') +# define c3__prop c3_s4('p','r','o','p') # define c3__prof c3_s4('p','r','o','f') # define c3__prox c3_s4('p','r','o','x') # define c3__psdg c3_s4('p','s','d','g') diff --git a/pkg/vere/king.c b/pkg/vere/king.c index 74ff7f782d..44979102b3 100644 --- a/pkg/vere/king.c +++ b/pkg/vere/king.c @@ -90,6 +90,8 @@ void _king_doom(u3_noun doom); void _king_fake(u3_noun ship, u3_noun pill, u3_noun path); void _king_pier(u3_noun pier); +static u3_noun _king_get_atom(c3_c* url_c); + /* _king_defy_fate(): invalid fate */ void @@ -158,6 +160,49 @@ _king_boot(u3_noun bul) u3z(bul); } +/* _king_prop(): events from prop arguments +*/ +u3_noun +_king_prop() +{ + u3_noun mor = u3_nul; + while ( 0 != u3_Host.ops_u.vex_u ) { + u3_even* vex_u = u3_Host.ops_u.vex_u; + switch ( vex_u->kin_i ) { + case 1: { // file + u3_atom jam = u3m_file(vex_u->loc_c); + mor = u3nc(u3ke_cue(jam), mor); + } break; + + case 2: { // url + u3l_log("boot: downloading prop %s", vex_u->loc_c); + u3_atom jam = _king_get_atom(vex_u->loc_c); + mor = u3nc(u3ke_cue(jam), mor); + } break; + + case 3: { // name + //NOTE this implementation limits us to max 38 char prop names + c3_c url_c[80]; + sprintf(url_c, + //TODO should maybe respect ops_u.url_c + "https://bootstrap.urbit.org/props/" URBIT_VERSION "/%s.jam", + vex_u->loc_c); + u3l_log("boot: downloading prop %s", url_c); + u3_atom jam = _king_get_atom(url_c); + mor = u3nc(u3ke_cue(jam), mor); + } break; + + default: { + u3l_log("invalid prop source %d", vex_u->kin_i); + exit(1); + } + } + + u3_Host.ops_u.vex_u = vex_u->pre_u; + } + return mor; +} + /* _king_fake(): boot with fake keys */ void @@ -166,7 +211,8 @@ _king_fake(u3_noun ship, u3_noun pill, u3_noun path) // XX link properly // u3_noun vent = u3nc(c3__fake, u3k(ship)); - u3K.pir_u = u3_pier_boot(sag_w, ship, vent, pill, path, u3_none); + u3K.pir_u = u3_pier_boot(sag_w, ship, vent, pill, path, + u3_none, _king_prop()); } /* _king_come(): mine a comet under star (unit) @@ -201,7 +247,8 @@ _king_dawn(u3_noun feed, u3_noun pill, u3_noun path) u3_noun vent = u3_dawn_vent(u3k(ship), u3k(feed)); // XX link properly // - u3K.pir_u = u3_pier_boot(sag_w, u3k(ship), vent, pill, path, feed); + u3K.pir_u = u3_pier_boot(sag_w, u3k(ship), vent, pill, path, + feed, _king_prop()); // disable ivory slog printfs // diff --git a/pkg/vere/main.c b/pkg/vere/main.c index 9a1680347c..9114cb4f74 100644 --- a/pkg/vere/main.c +++ b/pkg/vere/main.c @@ -215,6 +215,19 @@ _main_pier_run(c3_c* bin_c) return dir_c; } +/* _main_add_prop(): add a boot prop to u3_Host.ops_u.vex_u. +*/ +u3_even* +_main_add_prop(c3_i kin_i, c3_c* loc_c) +{ + u3_even* nex_u = c3_calloc(sizeof(*nex_u)); + nex_u->kin_i = kin_i; + nex_u->loc_c = loc_c; // XX _main_repath where appropriate? + nex_u->pre_u = u3_Host.ops_u.vex_u; + u3_Host.ops_u.vex_u = nex_u; + return nex_u; +} + /* _main_getopt(): extract option map from command line. */ static u3_noun @@ -269,6 +282,10 @@ _main_getopt(c3_i argc, c3_c** argv) { "scry-into", required_argument, NULL, 'Y' }, { "scry-format", required_argument, NULL, 'Z' }, // + { "prop-file", required_argument, NULL, 1 }, + { "prop-url", required_argument, NULL, 2 }, + { "prop-name", required_argument, NULL, 3 }, + // { "urth-loom", required_argument, NULL, 5 }, { "no-demand", no_argument, NULL, 6 }, { "swap", no_argument, NULL, 7 }, @@ -283,6 +300,10 @@ _main_getopt(c3_i argc, c3_c** argv) lop_u, &lid_i)) ) { switch ( ch_i ) { + case 1: case 2: case 3: { // prop-* + _main_add_prop(ch_i, strdup(optarg)); + break; + } case 5: { // urth-loom if (_main_readw_loom("urth-loom", &u3_Host.ops_u.lut_y)) { return c3n; @@ -591,6 +612,7 @@ _main_getopt(c3_i argc, c3_c** argv) if ( hyphen_c ) { *hyphen_c = '\0'; } + //TODO use brass pill from b.u.org/props/etc eventually c3_i res_i = asprintf(&u3_Host.ops_u.url_c, "https://bootstrap.urbit.org/urbit-v%s.pill", version_c); @@ -616,6 +638,18 @@ _main_getopt(c3_i argc, c3_c** argv) } } + if ( u3_Host.ops_u.vex_u != 0 ) { + struct stat s; + u3_even* vex_u = u3_Host.ops_u.vex_u; + while ( vex_u != 0 ) { + if ( vex_u->kin_i == 1 && stat(vex_u->loc_c, &s) != 0 ) { + fprintf(stderr, "events file %s not found\n", vex_u->loc_c); + return c3n; + } + vex_u = vex_u->pre_u; + } + } + struct sockaddr_in t; if ( u3_Host.ops_u.bin_c != 0 && inet_pton(AF_INET, u3_Host.ops_u.bin_c, &t.sin_addr) == 0 ) { fprintf(stderr, "-b invalid IP address\n"); @@ -630,6 +664,15 @@ _main_getopt(c3_i argc, c3_c** argv) } } + //TODO split up "default distribution" packages eventually + // // if we're not in lite mode, include the default props + // // + // if ( u3_Host.ops_u.lit == c3n ) { + // _main_add_prop(3, "landscape"); + // _main_add_prop(3, "webterm"); + // _main_add_prop(3, "groups"); + // } + return c3y; } @@ -790,6 +833,9 @@ u3_ve_usage(c3_i argc, c3_c** argv) " --no-dock Skip binary \"docking\" on boot\n", " --swap Use an explicit ephemeral (swap-like) file\n", " --swap-to FILE Specify ephemeral file location\n", + " --prop-file FILE Add a prop into the boot sequence\n" + " --prop-url URL Download a prop into the boot sequence\n", + " --prop-name NAME Download a prop from bootstrap.urbit.org\n", "\n", "Development Usage:\n", " To create a development ship, use a fakezod:\n", diff --git a/pkg/vere/pier.c b/pkg/vere/pier.c index bba434c4fd..b195291707 100644 --- a/pkg/vere/pier.c +++ b/pkg/vere/pier.c @@ -1820,7 +1820,8 @@ _pier_boot_make(u3_noun who, u3_noun wyr, u3_noun ven, u3_noun pil, - u3_weak fed) + u3_weak fed, + u3_noun mor) { u3_boot bot_u = _pier_pill_parse(pil); // transfer @@ -1872,7 +1873,48 @@ _pier_boot_make(u3_noun who, bot_u.use = u3nc(u3nc(wir, cad), bot_u.use); } + // prepend & append additional boot enhancements to the userspace sequence + // + { + u3_noun mos = mor; + u3_noun pre = u3_nul; + u3_noun aft = u3_nul; + while ( u3_nul != mos ) { + u3_noun mot = u3h(mos); + + switch ( u3h(mot) ) { + case c3__prop: { + u3_noun ter, met, ves; + + if ( c3n == u3r_trel(u3t(mot), &met, &ter, &ves) ) { + u3m_p("invalid prop", u3t(mot)); + break; + } + + if ( c3__fore == ter ) { + u3m_p("prop: fore", met); + pre = u3kb_weld(pre, u3k(ves)); + } + else if ( c3__hind == ter ) { + u3m_p("prop: hind", met); + aft = u3kb_weld(aft, u3k(ves)); + } + else { + u3m_p("unrecognized prop tier", ter); + } + } break; + + default: u3m_p("unrecognized boot sequence enhancement", u3h(mot)); + } + + mos = u3t(mos); + } + + bot_u.use = u3kb_weld(pre, u3kb_weld(bot_u.use, aft)); // transfer + } + u3z(fed); + u3z(mor); return bot_u; } @@ -1883,7 +1925,8 @@ _pier_boot_plan(u3_pier* pir_u, u3_noun who, u3_noun ven, u3_noun pil, - u3_weak fed) + u3_weak fed, + u3_noun mor) { u3_boot bot_u; { @@ -1891,7 +1934,7 @@ _pier_boot_plan(u3_pier* pir_u, pir_u->fak_o = ( c3__fake == u3h(ven) ) ? c3y : c3n; u3r_chubs(0, 2, pir_u->who_d, who); - bot_u = _pier_boot_make(who, _pier_wyrd_card(pir_u), ven, pil, fed); + bot_u = _pier_boot_make(who, _pier_wyrd_card(pir_u), ven, pil, fed, mor); pir_u->lif_w = u3qb_lent(bot_u.bot); } @@ -1967,7 +2010,8 @@ u3_pier_boot(c3_w wag_w, // config flags u3_noun ven, // boot event u3_noun pil, // type-of/path-to pill u3_noun pax, // path to pier - u3_weak fed) // extra private keys + u3_weak fed, // extra private keys + u3_noun mor) // extra boot sequence props { u3_pier* pir_u; @@ -1979,7 +2023,7 @@ u3_pier_boot(c3_w wag_w, // config flags // XX must be called from on_lord_live // - if ( c3n == _pier_boot_plan(pir_u, who, ven, pil, fed) ) { + if ( c3n == _pier_boot_plan(pir_u, who, ven, pil, fed, mor) ) { fprintf(stderr, "pier: boot plan fail\r\n"); // XX dispose // diff --git a/pkg/vere/vere.h b/pkg/vere/vere.h index b2b78b5e81..6361a1c3d2 100644 --- a/pkg/vere/vere.h +++ b/pkg/vere/vere.h @@ -258,6 +258,14 @@ struct _u3_auto* car_u; // driver hack } u3_utty; + /* u3_even: boot event specifier + */ + typedef struct _u3_even { + c3_i kin_i; + c3_c* loc_c; + struct _u3_even* pre_u; + } u3_even; + /* u3_opts: command line configuration. */ typedef struct _u3_opts { @@ -308,6 +316,7 @@ c3_o map; // --no-demand (reversed) c3_o eph; // --swap, use ephemeral file c3_o tos; // --toss, discard ephemeral + u3_even* vex_u; // --prop-*, boot enhancements } u3_opts; /* u3_host: entire host. @@ -1417,7 +1426,8 @@ u3_noun ven, // boot event u3_noun pil, // type-of/path-to pill u3_noun pax, // path to pier - u3_weak fed); // extra private keys + u3_weak fed, // extra private keys + u3_noun mor); // extra boot seq props /* u3_pier_stay(): restart the pier. */ From 80888b4952a422a317c57a4fa3c3421116621a52 Mon Sep 17 00:00:00 2001 From: lukechampine Date: Wed, 22 Nov 2023 11:12:42 -0500 Subject: [PATCH 004/107] jets: add +chunk-output and +compress blake3 jets --- WORKSPACE.bazel | 4 +-- pkg/noun/jets/e/blake.c | 64 +++++++++++++++++++++++++++++++++++++++++ pkg/noun/jets/tree.c | 16 ++++++++++- pkg/noun/jets/w.h | 2 ++ 4 files changed, 83 insertions(+), 3 deletions(-) diff --git a/WORKSPACE.bazel b/WORKSPACE.bazel index 7d60159925..07dd843afc 100644 --- a/WORKSPACE.bazel +++ b/WORKSPACE.bazel @@ -299,10 +299,10 @@ versioned_http_file( versioned_http_archive( name = "urcrypt", build_file = "//bazel/third_party/urcrypt:urcrypt.BUILD", - sha256 = "e659d3f3eac67dbf47e7e21860c99198c9f386b480e30d43547d1c6cb8974b24", + sha256 = "52209677868ccbb8bc87102dd4558b09f78bf57c4470b0feded30f411d13cc97", strip_prefix = "urcrypt-{version}", url = "https://github.com/urbit/urcrypt/archive/{version}.tar.gz", - version = "c1ab781ae888730da06d998c80bfb2130232e528", + version = "f331b6a9d1a86d42573165ff649700bf9c13b005", ) versioned_http_archive( diff --git a/pkg/noun/jets/e/blake.c b/pkg/noun/jets/e/blake.c index fcdfed5a53..9dd1eb9266 100644 --- a/pkg/noun/jets/e/blake.c +++ b/pkg/noun/jets/e/blake.c @@ -96,3 +96,67 @@ return u3l_punt("blake3_hash", _cqe_blake3_hash(wid, dat, key, out)); } } + + static u3_noun + _cqe_blake3_chunk_output(u3_atom wid, u3_atom dat, u3_atom cv, u3_atom counter, u3_atom flags) + { + c3_w wid_w; + if ( !u3r_word_fit(&wid_w, wid) ) { + return u3m_bail(c3__fail); + } else { + c3_y cv_y[32], block_y[64], block_len; + c3_y *dat_y = u3r_bytes_alloc(0, wid_w, dat); + c3_d counter_d = u3r_chub(0, counter); + c3_y flags_y = u3r_byte(0, flags); + u3r_bytes(0, 32, cv_y, cv); + urcrypt_blake3_chunk_output(wid_w, dat_y, cv_y, block_y, &block_len, &counter_d, &flags_y); + return u3i_cell(u3i_bytes(32, cv_y), u3i_qual(u3i_chub(counter_d), u3i_bytes(64, block_y), block_len, u3i_bytes(1, &flags_y))); + } + } + + u3_noun + u3we_blake3_chunk_output(u3_noun cor) + { + u3_noun counter, msg, // arguments + wid, dat, // destructured msg + key, flags; // context + + if ( c3n == u3r_mean(cor, u3x_sam_2, &counter, + u3x_sam_3, &msg, + u3x_con_sam_2, &key, + u3x_con_sam_3, &flags, 0) || + u3ud(counter) || + u3r_cell(msg, &wid, &dat) || u3ud(wid) || u3ud(dat) || + u3ud(key) || u3ud(flags)) + { + return u3m_bail(c3__exit); + } else { + return u3l_punt("blake3_chunk_output", _cqe_blake3_chunk_output(wid, dat, key, counter, flags)); + } + } + + static u3_atom + _cqe_blake3_compress(u3_atom cv, u3_atom counter, + u3_atom block, u3_atom block_len, u3_atom flags) + { + c3_y cv_y[32], block_y[64], out_y[64]; + u3r_bytes(0, 32, cv_y, cv); + u3r_bytes(0, 64, block_y, block); + urcrypt_blake3_compress(cv_y, block_y, block_len, counter, flags, out_y); + return u3i_bytes(64, out_y); + } + + u3_noun + u3we_blake3_compress(u3_noun cor) + { + u3_noun output = u3x_at(u3x_sam, cor); + u3_noun cv, counter, block, block_len, flags; // destructured output + + if ( u3r_quil(output, &cv, &counter, &block, &block_len, &flags) || + u3ud(cv) || u3ud(block) || u3ud(block_len) || u3ud(counter) || u3ud(flags)) + { + return u3m_bail(c3__exit); + } else { + return u3l_punt("blake3_compress", _cqe_blake3_compress(cv, counter, block, block_len, flags)); + } + } diff --git a/pkg/noun/jets/tree.c b/pkg/noun/jets/tree.c index edcbcea41a..bfd27c4aee 100644 --- a/pkg/noun/jets/tree.c +++ b/pkg/noun/jets/tree.c @@ -452,11 +452,23 @@ static c3_c* _140_hex_secp_ha[] = { }; static u3j_harm _140_hex_blake3_hash_a[] = {{".2", u3we_blake3_hash, c3y}, {}}; static c3_c* _140_hex_blake3_hash_ha[] = { - "c432216ca53b5ad2284259167952761bb1046e280268c4d3b9ca70a2024e1934", + "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", + 0 + }; + static u3j_harm _140_hex_blake3_compress_a[] = {{".2", u3we_blake3_compress, c3y}, {}}; + static c3_c* _140_hex_blake3_compress_ha[] = { + "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", + 0 + }; + static u3j_harm _140_hex_blake3_chunk_output_a[] = {{".2", u3we_blake3_chunk_output, c3y}, {}}; + static c3_c* _140_hex_blake3_chunk_output_ha[] = { + "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", 0 }; static u3j_core _140_hex_blake3_d[] = { { "hash", 7, _140_hex_blake3_hash_a, 0, _140_hex_blake3_hash_ha }, + { "chunk-output", 7, _140_hex_blake3_chunk_output_a, 0, _140_hex_blake3_chunk_output_ha }, + { "compress", 7, _140_hex_blake3_compress_a, 0, _140_hex_blake3_compress_ha }, {} }; static c3_c* _140_hex_blake3_ha[] = { 0 }; @@ -465,6 +477,8 @@ static u3j_core _140_hex_blake_d[] = { "blake3", 7, 0, _140_hex_blake3_d, _140_hex_blake3_ha }, { "hash", 7, _140_hex_blake3_hash_a, 0, _140_hex_blake3_hash_ha }, + { "chunk-output", 7, _140_hex_blake3_chunk_output_a, 0, _140_hex_blake3_chunk_output_ha }, + { "compress", 7, _140_hex_blake3_compress_a, 0, _140_hex_blake3_compress_ha }, {} }; static c3_c* _140_hex_blake_ha[] = { diff --git a/pkg/noun/jets/w.h b/pkg/noun/jets/w.h index b9b25a2dda..25875f0f04 100644 --- a/pkg/noun/jets/w.h +++ b/pkg/noun/jets/w.h @@ -207,6 +207,8 @@ u3_noun u3we_blake2b(u3_noun); u3_noun u3we_blake3_hash(u3_noun); + u3_noun u3we_blake3_chunk_output(u3_noun); + u3_noun u3we_blake3_compress(u3_noun); u3_noun u3we_ripe(u3_noun); From ed2418bea149eb198939ce2807255d83e1d8454b Mon Sep 17 00:00:00 2001 From: lukechampine Date: Sun, 26 Nov 2023 15:28:45 -0500 Subject: [PATCH 005/107] jets: support arbitrary-length blake3 output --- pkg/noun/jets/e/blake.c | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/pkg/noun/jets/e/blake.c b/pkg/noun/jets/e/blake.c index 9dd1eb9266..76a42b8ccf 100644 --- a/pkg/noun/jets/e/blake.c +++ b/pkg/noun/jets/e/blake.c @@ -67,13 +67,13 @@ return u3m_bail(c3__fail); } else { - c3_y out_y[64], key_y[32]; - c3_w out_w = c3_max(1, c3_min(out, 64)); - c3_y *dat_y = u3r_bytes_alloc(0, wid_w, dat); + c3_y key_y[32]; u3r_bytes(0, 32, key_y, key); - urcrypt_blake3_hash(wid_w, dat_y, key_y, out_w, out_y); + c3_y *dat_y = u3r_bytes_alloc(0, wid_w, dat), + *out_y = c3_malloc(out); + urcrypt_blake3_hash(wid_w, dat_y, key_y, out, out_y); u3a_free(dat_y); - return u3i_bytes(out_w, out_y); + return u3i_bytes(out, out_y); } } From eb87fc46206058a012a1d241dde4b75a4fb3923a Mon Sep 17 00:00:00 2001 From: Edward Amsden Date: Tue, 5 Dec 2023 07:19:36 -0600 Subject: [PATCH 006/107] nix: update flake.lock to fix FHSUserEnv --- flake.lock | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/flake.lock b/flake.lock index 45c4d968fb..e0fc6ef9a4 100644 --- a/flake.lock +++ b/flake.lock @@ -2,11 +2,11 @@ "nodes": { "nixpkgs": { "locked": { - "lastModified": 1683236849, - "narHash": "sha256-Y7PNBVLOBvZrmrFmHgXUBUA1lM72tl6JGIn1trOeuyE=", + "lastModified": 1701626906, + "narHash": "sha256-ugr1QyzzwNk505ICE4VMQzonHQ9QS5W33xF2FXzFQ00=", "owner": "nixos", "repo": "nixpkgs", - "rev": "374ffe54403c3c42d97a513ac7a14ce1b5b86e30", + "rev": "0c6d8c783336a59f4c59d4a6daed6ab269c4b361", "type": "github" }, "original": { @@ -23,11 +23,11 @@ ] }, "locked": { - "lastModified": 1682984683, - "narHash": "sha256-fSMthG+tp60AHhNmaHc4StT3ltfHkQsJtN8GhfLWmtI=", + "lastModified": 1701473968, + "narHash": "sha256-YcVE5emp1qQ8ieHUnxt1wCZCC3ZfAS+SRRWZ2TMda7E=", "owner": "hercules-ci", "repo": "flake-parts", - "rev": "86684881e184f41aa322e653880e497b66429f3e", + "rev": "34fed993f1674c8d06d58b37ce1e0fe5eebcb9f5", "type": "github" }, "original": { From 420ee4252e8176a1c13db31b8b1a19b629b075a5 Mon Sep 17 00:00:00 2001 From: Edward Amsden Date: Tue, 5 Dec 2023 12:11:01 -0600 Subject: [PATCH 007/107] vere: add --serf-bin option to provide path to serf --- pkg/vere/main.c | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/pkg/vere/main.c b/pkg/vere/main.c index f83d656e98..53fd273d2e 100644 --- a/pkg/vere/main.c +++ b/pkg/vere/main.c @@ -269,11 +269,12 @@ _main_getopt(c3_i argc, c3_c** argv) { "scry-into", required_argument, NULL, 'Y' }, { "scry-format", required_argument, NULL, 'Z' }, // - { "urth-loom", required_argument, NULL, 5 }, - { "no-demand", no_argument, NULL, 6 }, - { "swap", no_argument, NULL, 7 }, - { "swap-to", required_argument, NULL, 8 }, - { "toss", required_argument, NULL, 9 }, + { "urth-loom", required_argument, NULL, 5 }, + { "no-demand", no_argument, NULL, 6 }, + { "swap", no_argument, NULL, 7 }, + { "swap-to", required_argument, NULL, 8 }, + { "toss", required_argument, NULL, 9 }, + { "serf-bin", required_argument, NULL, 10 }, // { NULL, 0, NULL, 0 }, }; @@ -309,6 +310,10 @@ _main_getopt(c3_i argc, c3_c** argv) } break; } + case 10: { // serf-bin + u3_Host.wrk_c = strdup(optarg); + break; + } // special args // case c3__loom: { From 4450fbb6b4f0ae01feb6780dd5988b1dc21213ec Mon Sep 17 00:00:00 2001 From: lukechampine Date: Mon, 11 Dec 2023 14:37:00 -0500 Subject: [PATCH 008/107] jets: clean up blake3 jets --- pkg/noun/jets/e/blake.c | 16 ++++++++-------- pkg/noun/jets/tree.c | 34 +++++++++------------------------- 2 files changed, 17 insertions(+), 33 deletions(-) diff --git a/pkg/noun/jets/e/blake.c b/pkg/noun/jets/e/blake.c index 76a42b8ccf..f21f229ceb 100644 --- a/pkg/noun/jets/e/blake.c +++ b/pkg/noun/jets/e/blake.c @@ -61,19 +61,20 @@ _cqe_blake3_hash(u3_atom wid, u3_atom dat, u3_atom key, u3_atom out) { - c3_w wid_w; - if ( !u3r_word_fit(&wid_w, wid) ) { - // impossible to represent an atom this large + c3_w wid_w, out_w; + if ( !u3r_word_fit(&wid_w, wid) || !u3r_word_fit(&out_w, out) ) { return u3m_bail(c3__fail); } else { c3_y key_y[32]; u3r_bytes(0, 32, key_y, key); - c3_y *dat_y = u3r_bytes_alloc(0, wid_w, dat), - *out_y = c3_malloc(out); + c3_y *dat_y = u3r_bytes_alloc(0, wid_w, dat); + u3i_slab sab_u; + u3i_slab_bare(&sab_u, 3, out_w); + c3_y* out_y = sab_u.buf_y; urcrypt_blake3_hash(wid_w, dat_y, key_y, out, out_y); u3a_free(dat_y); - return u3i_bytes(out, out_y); + return u3i_slab_mint(&sab_u); } } @@ -110,7 +111,7 @@ c3_y flags_y = u3r_byte(0, flags); u3r_bytes(0, 32, cv_y, cv); urcrypt_blake3_chunk_output(wid_w, dat_y, cv_y, block_y, &block_len, &counter_d, &flags_y); - return u3i_cell(u3i_bytes(32, cv_y), u3i_qual(u3i_chub(counter_d), u3i_bytes(64, block_y), block_len, u3i_bytes(1, &flags_y))); + return u3i_cell(u3i_bytes(32, cv_y), u3i_qual(u3k(counter), u3i_bytes(64, block_y), block_len, flags_y)); } } @@ -125,7 +126,6 @@ u3x_sam_3, &msg, u3x_con_sam_2, &key, u3x_con_sam_3, &flags, 0) || - u3ud(counter) || u3r_cell(msg, &wid, &dat) || u3ud(wid) || u3ud(dat) || u3ud(key) || u3ud(flags)) { diff --git a/pkg/noun/jets/tree.c b/pkg/noun/jets/tree.c index bfd27c4aee..77691bb76d 100644 --- a/pkg/noun/jets/tree.c +++ b/pkg/noun/jets/tree.c @@ -446,39 +446,23 @@ static c3_c* _140_hex_secp_ha[] = { }; static u3j_harm _140_hex_blake2b_a[] = {{".2", u3we_blake2b, c3y}, {}}; - static c3_c* _140_hex_blake2b_ha[] = { - "c432216ca53b5ad2284259167952761bb1046e280268c4d3b9ca70a2024e1934", - 0 - }; static u3j_harm _140_hex_blake3_hash_a[] = {{".2", u3we_blake3_hash, c3y}, {}}; - static c3_c* _140_hex_blake3_hash_ha[] = { - "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", - 0 - }; static u3j_harm _140_hex_blake3_compress_a[] = {{".2", u3we_blake3_compress, c3y}, {}}; - static c3_c* _140_hex_blake3_compress_ha[] = { - "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", - 0 - }; static u3j_harm _140_hex_blake3_chunk_output_a[] = {{".2", u3we_blake3_chunk_output, c3y}, {}}; - static c3_c* _140_hex_blake3_chunk_output_ha[] = { - "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", - 0 - }; static u3j_core _140_hex_blake3_d[] = - { { "hash", 7, _140_hex_blake3_hash_a, 0, _140_hex_blake3_hash_ha }, - { "chunk-output", 7, _140_hex_blake3_chunk_output_a, 0, _140_hex_blake3_chunk_output_ha }, - { "compress", 7, _140_hex_blake3_compress_a, 0, _140_hex_blake3_compress_ha }, + { { "hash", 7, _140_hex_blake3_hash_a, 0, no_hashes }, + { "chunk-output", 7, _140_hex_blake3_chunk_output_a, 0, no_hashes }, + {} + }; + static u3j_core _140_hex_blake3_impl_d[] = + { { "compress", 7, _140_hex_blake3_compress_a, 0, no_hashes }, + { "blake3", 7, 0, _140_hex_blake3_d, no_hashes }, {} }; - static c3_c* _140_hex_blake3_ha[] = { 0 }; static u3j_core _140_hex_blake_d[] = - { { "blake2b", 7, _140_hex_blake2b_a, 0, _140_hex_blake2b_ha }, + { { "blake2b", 7, _140_hex_blake2b_a, 0, no_hashes }, - { "blake3", 7, 0, _140_hex_blake3_d, _140_hex_blake3_ha }, - { "hash", 7, _140_hex_blake3_hash_a, 0, _140_hex_blake3_hash_ha }, - { "chunk-output", 7, _140_hex_blake3_chunk_output_a, 0, _140_hex_blake3_chunk_output_ha }, - { "compress", 7, _140_hex_blake3_compress_a, 0, _140_hex_blake3_compress_ha }, + { "blake3-impl", 7, 0, _140_hex_blake3_impl_d, no_hashes }, {} }; static c3_c* _140_hex_blake_ha[] = { From a99623123aac28840c1418b7d77d8575e5c83ffd Mon Sep 17 00:00:00 2001 From: Pyry Kovanen Date: Wed, 3 Jan 2024 15:16:44 +0200 Subject: [PATCH 009/107] jets: make a real jet for swp --- pkg/noun/jets/c/swp.c | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/pkg/noun/jets/c/swp.c b/pkg/noun/jets/c/swp.c index c31e0807ad..1d0a11b434 100644 --- a/pkg/noun/jets/c/swp.c +++ b/pkg/noun/jets/c/swp.c @@ -10,9 +10,15 @@ u3_noun u3qc_swp(u3_atom a, u3_atom b) { - //XX write a proper c-style swp, maybe - // - return u3kc_rep(u3k(a), 1, u3kb_flop(u3qc_rip(a, 1, b))); + c3_w len_w = u3r_met(a, b); + u3i_slab sab_u; + u3i_slab_init(&sab_u, a, len_w); + + for (c3_w i = 0; i < len_w; i++) { + u3r_chop(a, i, 1, len_w - i - 1, sab_u.buf_w, b); + } + + return u3i_slab_mint(&sab_u); } u3_noun From 6447f97f7ed152a580c78e64f5f762bc5c05e6b4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C5=8Dshin?= Date: Thu, 4 Jan 2024 14:23:01 -0500 Subject: [PATCH 010/107] build: recent macOS updates clang --- BUILD.bazel | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/BUILD.bazel b/BUILD.bazel index ff11949f5f..3569ed78a2 100644 --- a/BUILD.bazel +++ b/BUILD.bazel @@ -76,8 +76,8 @@ config_setting( # Version flag for clang. string_flag( name = "clang_version", - # macOS uses `clang-14.0.3` by default. - build_setting_default = "14.0.3", + # macOS uses `clang-15.0.0` by default. + build_setting_default = "15.0.0", visibility = ["//visibility:public"], ) From 90b60f610b626a0abb2ce42a10966a3b6e7ba41d Mon Sep 17 00:00:00 2001 From: Pyry Kovanen Date: Fri, 5 Jan 2024 18:24:16 +0200 Subject: [PATCH 011/107] vere: disable systemd and apparmor for the dbus dependency --- bazel/third_party/dbus/dbus.BUILD | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bazel/third_party/dbus/dbus.BUILD b/bazel/third_party/dbus/dbus.BUILD index 0642883163..25fd750092 100644 --- a/bazel/third_party/dbus/dbus.BUILD +++ b/bazel/third_party/dbus/dbus.BUILD @@ -13,7 +13,7 @@ configure_make( "//conditions:default": ["--jobs=`nproc`"], }), copts = ["-O3"], - configure_options = ["--disable-selinux --without-x --disable-tests"], + configure_options = ["--disable-selinux --without-x --disable-tests --disable-systemd --disable-apparmor"], lib_source = ":all", configure_in_place = True, deps = ["@expat"], From a37f05248056fc580551d6e23414a7f4d75a8a55 Mon Sep 17 00:00:00 2001 From: Matthew LeVan Date: Wed, 10 Jan 2024 12:03:33 -0500 Subject: [PATCH 012/107] Revert "vere: add --serf-bin option to provide path to serf" --- pkg/vere/main.c | 15 +++++---------- 1 file changed, 5 insertions(+), 10 deletions(-) diff --git a/pkg/vere/main.c b/pkg/vere/main.c index 53fd273d2e..f83d656e98 100644 --- a/pkg/vere/main.c +++ b/pkg/vere/main.c @@ -269,12 +269,11 @@ _main_getopt(c3_i argc, c3_c** argv) { "scry-into", required_argument, NULL, 'Y' }, { "scry-format", required_argument, NULL, 'Z' }, // - { "urth-loom", required_argument, NULL, 5 }, - { "no-demand", no_argument, NULL, 6 }, - { "swap", no_argument, NULL, 7 }, - { "swap-to", required_argument, NULL, 8 }, - { "toss", required_argument, NULL, 9 }, - { "serf-bin", required_argument, NULL, 10 }, + { "urth-loom", required_argument, NULL, 5 }, + { "no-demand", no_argument, NULL, 6 }, + { "swap", no_argument, NULL, 7 }, + { "swap-to", required_argument, NULL, 8 }, + { "toss", required_argument, NULL, 9 }, // { NULL, 0, NULL, 0 }, }; @@ -310,10 +309,6 @@ _main_getopt(c3_i argc, c3_c** argv) } break; } - case 10: { // serf-bin - u3_Host.wrk_c = strdup(optarg); - break; - } // special args // case c3__loom: { From 232a5580e6a923def3316e332296273f09ed3f2e Mon Sep 17 00:00:00 2001 From: fang Date: Tue, 16 Jan 2024 21:12:08 +0100 Subject: [PATCH 013/107] vere: include prop type definition --- pkg/vere/pier.c | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/pkg/vere/pier.c b/pkg/vere/pier.c index b195291707..db1fc08b0f 100644 --- a/pkg/vere/pier.c +++ b/pkg/vere/pier.c @@ -1875,6 +1875,10 @@ _pier_boot_make(u3_noun who, // prepend & append additional boot enhancements to the userspace sequence // + // +$ prop [%prop meta tier (list ovum)] + // +$ meta term + // +$ tier ?(%fore %hind) :: before or after userspace + // { u3_noun mos = mor; u3_noun pre = u3_nul; From c686f91bcbfdafcfea375c5e1d489ad197e0cf21 Mon Sep 17 00:00:00 2001 From: fang Date: Tue, 16 Jan 2024 21:13:28 +0100 Subject: [PATCH 014/107] vere: no need to call _main_repath here --- pkg/vere/main.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pkg/vere/main.c b/pkg/vere/main.c index 9114cb4f74..6ac32a5587 100644 --- a/pkg/vere/main.c +++ b/pkg/vere/main.c @@ -222,7 +222,7 @@ _main_add_prop(c3_i kin_i, c3_c* loc_c) { u3_even* nex_u = c3_calloc(sizeof(*nex_u)); nex_u->kin_i = kin_i; - nex_u->loc_c = loc_c; // XX _main_repath where appropriate? + nex_u->loc_c = loc_c; nex_u->pre_u = u3_Host.ops_u.vex_u; u3_Host.ops_u.vex_u = nex_u; return nex_u; From 8b092267fe11b33afa02056bdf8251f741832737 Mon Sep 17 00:00:00 2001 From: fang Date: Tue, 16 Jan 2024 21:38:13 +0100 Subject: [PATCH 015/107] vere: sprintf -> snprintf For safety. --- pkg/vere/king.c | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/pkg/vere/king.c b/pkg/vere/king.c index 44979102b3..66181c6f6a 100644 --- a/pkg/vere/king.c +++ b/pkg/vere/king.c @@ -181,12 +181,12 @@ _king_prop() } break; case 3: { // name - //NOTE this implementation limits us to max 38 char prop names - c3_c url_c[80]; - sprintf(url_c, - //TODO should maybe respect ops_u.url_c - "https://bootstrap.urbit.org/props/" URBIT_VERSION "/%s.jam", - vex_u->loc_c); + //NOTE this implementation limits us to max 213 char prop names + c3_c url_c[256]; + snprintf(url_c, 255, + //TODO should maybe respect ops_u.url_c + "https://bootstrap.urbit.org/props/" URBIT_VERSION "/%s.jam", + vex_u->loc_c); u3l_log("boot: downloading prop %s", url_c); u3_atom jam = _king_get_atom(url_c); mor = u3nc(u3ke_cue(jam), mor); From 5913417e6cf02818d7baf9d95f5a9aa9cd291a8d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C5=8Dshin?= Date: Sun, 21 Jan 2024 14:54:29 +0000 Subject: [PATCH 016/107] build: bump openssl dep 1.1.1t is 404ing on openssl.org. --- WORKSPACE.bazel | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/WORKSPACE.bazel b/WORKSPACE.bazel index d3c4848fd1..42eaa55ec2 100644 --- a/WORKSPACE.bazel +++ b/WORKSPACE.bazel @@ -253,10 +253,10 @@ versioned_http_archive( versioned_http_archive( name = "openssl", build_file = "//bazel/third_party/openssl:openssl.BUILD", - sha256 = "8dee9b24bdb1dcbf0c3d1e9b02fb8f6bf22165e807f45adeb7c9677536859d3b", + sha256 = "cf3098950cb4d853ad95c0841f1f9c6d3dc102dccfcacd521d93925208b76ac8", strip_prefix = "openssl-{version}", url = "https://www.openssl.org/source/openssl-{version}.tar.gz", - version = "1.1.1t", + version = "1.1.1w", ) versioned_http_archive( From f7647c3316773cf0a10be8f4aa276ed8f0c653bd Mon Sep 17 00:00:00 2001 From: Pyry Kovanen Date: Mon, 22 Jan 2024 19:48:10 +0200 Subject: [PATCH 017/107] Revert "build: recent macOS updates clang" --- BUILD.bazel | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/BUILD.bazel b/BUILD.bazel index 3569ed78a2..ff11949f5f 100644 --- a/BUILD.bazel +++ b/BUILD.bazel @@ -76,8 +76,8 @@ config_setting( # Version flag for clang. string_flag( name = "clang_version", - # macOS uses `clang-15.0.0` by default. - build_setting_default = "15.0.0", + # macOS uses `clang-14.0.3` by default. + build_setting_default = "14.0.3", visibility = ["//visibility:public"], ) From e9d3f3211f9fb2de1edf7b7a431d0e0d5611760d Mon Sep 17 00:00:00 2001 From: Joe Bryan Date: Thu, 25 Jan 2024 14:19:37 -0500 Subject: [PATCH 018/107] jets: moves blake3 to %138 declarations --- pkg/noun/jets/tree.c | 58 ++++++++++++++++++++++++++++++++------------ 1 file changed, 42 insertions(+), 16 deletions(-) diff --git a/pkg/noun/jets/tree.c b/pkg/noun/jets/tree.c index 5e363a3a19..e40fe03846 100644 --- a/pkg/noun/jets/tree.c +++ b/pkg/noun/jets/tree.c @@ -446,23 +446,9 @@ static c3_c* _140_hex_secp_ha[] = { }; static u3j_harm _140_hex_blake2b_a[] = {{".2", u3we_blake2b, c3y}, {}}; - static u3j_harm _140_hex_blake3_hash_a[] = {{".2", u3we_blake3_hash, c3y}, {}}; - static u3j_harm _140_hex_blake3_compress_a[] = {{".2", u3we_blake3_compress, c3y}, {}}; - static u3j_harm _140_hex_blake3_chunk_output_a[] = {{".2", u3we_blake3_chunk_output, c3y}, {}}; - static u3j_core _140_hex_blake3_d[] = - { { "hash", 7, _140_hex_blake3_hash_a, 0, no_hashes }, - { "chunk-output", 7, _140_hex_blake3_chunk_output_a, 0, no_hashes }, - {} - }; - static u3j_core _140_hex_blake3_impl_d[] = - { { "compress", 7, _140_hex_blake3_compress_a, 0, no_hashes }, - { "blake3", 7, 0, _140_hex_blake3_d, no_hashes }, - {} - }; + static u3j_core _140_hex_blake_d[] = { { "blake2b", 7, _140_hex_blake2b_a, 0, no_hashes }, - - { "blake3-impl", 7, 0, _140_hex_blake3_impl_d, no_hashes }, {} }; static c3_c* _140_hex_blake_ha[] = { @@ -2336,8 +2322,48 @@ u3j_core _k139_d[] = {} }; + static u3j_harm _138_hex_blake3_hash_a[] = {{".2", u3we_blake3_hash, c3y}, {}}; + static u3j_harm _138_hex_blake3_compress_a[] = {{".2", u3we_blake3_compress, c3y}, {}}; + static u3j_harm _138_hex_blake3_chunk_output_a[] = {{".2", u3we_blake3_chunk_output, c3y}, {}}; + static u3j_core _138_hex_blake3_d[] = + { { "hash", 7, _138_hex_blake3_hash_a, 0, no_hashes }, + { "chunk-output", 7, _138_hex_blake3_chunk_output_a, 0, no_hashes }, + {} + }; + static u3j_core _138_hex_blake3_impl_d[] = + { { "compress", 7, _138_hex_blake3_compress_a, 0, no_hashes }, + { "blake3", 7, 0, _138_hex_blake3_d, no_hashes }, + {} + }; +static u3j_core _138_hex_blake_d[] = + { { "blake2b", 7, _140_hex_blake2b_a, 0, no_hashes }, + { "blake3-impl", 7, 0, _138_hex_blake3_impl_d, no_hashes }, + {} + }; + +static u3j_core _138_hex_d[] = +{ { "lore", 63, _140_hex_lore_a, 0, no_hashes }, + { "leer", 63, _140_hex_leer_a, 0, no_hashes }, + { "loss", 63, _140_hex_loss_a, 0, no_hashes }, + { "lune", 127, _140_hex_lune_a, 0, no_hashes }, + + { "coed", 63, 0, _140_hex_coed_d, no_hashes }, + { "aes", 31, 0, _140_hex_aes_d, no_hashes }, + + { "hmac", 63, 0, _140_hex_hmac_d, no_hashes }, + { "argon", 31, 0, _140_hex_argon_d, no_hashes }, + { "blake", 31, 0, _138_hex_blake_d, no_hashes }, + { "kecc", 31, 0, _140_hex_kecc_d, no_hashes }, + { "ripemd", 31, 0, _140_hex_ripe_d, no_hashes }, + { "scr", 31, 0, _140_hex_scr_d, no_hashes }, + { "secp", 6, 0, _140_hex_secp_d, no_hashes }, + { "mimes", 31, 0, _140_hex_mimes_d, no_hashes }, + { "json", 31, 0, _139_hex_json_d, no_hashes }, + {} +}; + static u3j_core _138_pen_d[] = -{ { "hex", 7, 0, _139_hex_d, no_hashes }, +{ { "hex", 7, 0, _138_hex_d, no_hashes }, { "cell", 7, _140_pen_cell_a, 0, no_hashes }, { "comb", 7, _140_pen_comb_a, 0, no_hashes }, From 8b8d9b60f64141a769f1b99ef12ef4e122363792 Mon Sep 17 00:00:00 2001 From: pkova Date: Mon, 29 Jan 2024 15:16:30 +0200 Subject: [PATCH 019/107] ames: add libnatpmp for automatic port forwarding --- WORKSPACE.bazel | 9 +++++ bazel/third_party/natpmp/BUILD.bazel | 0 bazel/third_party/natpmp/natpmp.BUILD | 21 ++++++++++ pkg/vere/BUILD.bazel | 1 + pkg/vere/io/ames.c | 55 +++++++++++++++++++++++++++ 5 files changed, 86 insertions(+) create mode 100644 bazel/third_party/natpmp/BUILD.bazel create mode 100644 bazel/third_party/natpmp/natpmp.BUILD diff --git a/WORKSPACE.bazel b/WORKSPACE.bazel index 39ddfe03e9..dca6750619 100644 --- a/WORKSPACE.bazel +++ b/WORKSPACE.bazel @@ -217,6 +217,15 @@ versioned_http_file( version = "721fa05", ) +versioned_http_archive( + name = "natpmp", + build_file = "//bazel/third_party/natpmp:natpmp.BUILD", + sha256 = "0684ed2c8406437e7519a1bd20ea83780db871b3a3a5d752311ba3e889dbfc70", + strip_prefix = "libnatpmp-{version}", + url = "http://miniupnp.free.fr/files/libnatpmp-{version}.tar.gz", + version = "20230423", +) + versioned_http_file( name = "solid_pill", sha256 = "8b658fcee6978e2b19004a54233cab953e77ea0bb6c3a04d1bfda4ddc6be63c5", diff --git a/bazel/third_party/natpmp/BUILD.bazel b/bazel/third_party/natpmp/BUILD.bazel new file mode 100644 index 0000000000..e69de29bb2 diff --git a/bazel/third_party/natpmp/natpmp.BUILD b/bazel/third_party/natpmp/natpmp.BUILD new file mode 100644 index 0000000000..f002ccafcc --- /dev/null +++ b/bazel/third_party/natpmp/natpmp.BUILD @@ -0,0 +1,21 @@ +load("@rules_foreign_cc//foreign_cc:defs.bzl", "make") + +filegroup( + name = "all", + srcs = glob(["**"]), +) + +make( + name = "natpmp", + out_static_libs = ["libnatpmp.a"], + out_lib_dir = "/usr/lib", + out_include_dir = "/usr/include", + args = select({ + "@platforms//os:macos": ["--jobs=`sysctl -n hw.logicalcpu`"], + "//conditions:default": ["--jobs=`nproc`"], + }), + copts = ["-O3"], + lib_source = ":all", + visibility = ["//visibility:public"], + postfix_script = "cp $BUILD_TMPDIR/natpmp_declspec.h $INSTALLDIR/usr/include/natpmp_declspec.h", +) \ No newline at end of file diff --git a/pkg/vere/BUILD.bazel b/pkg/vere/BUILD.bazel index f50642ab89..0be1facfdf 100644 --- a/pkg/vere/BUILD.bazel +++ b/pkg/vere/BUILD.bazel @@ -139,6 +139,7 @@ vere_library( "@lmdb", "@openssl", "@uv", + "@natpmp", ] + select({ "@platforms//os:macos": [], "@platforms//os:linux": [ diff --git a/pkg/vere/io/ames.c b/pkg/vere/io/ames.c index 412a310e32..3ce9cc4f90 100644 --- a/pkg/vere/io/ames.c +++ b/pkg/vere/io/ames.c @@ -7,6 +7,7 @@ #include "ur.h" #include "zlib.h" +#include "natpmp.h" #include @@ -71,6 +72,11 @@ typedef enum u3_stun_state { u3_lane sef_u; // our lane, if we know it c3_o wok_o; // STUN worked, set on first success } sun_u; // + struct { + natpmp_t req_u; // libnatpmp struct for mapping request + uv_poll_t pol_u; // handle waits on libnatpmp socket + uv_timer_t tim_u; // every two hours if mapping succeeds + } nat_u; // libnatpmp stuff for port forwarding c3_o nal_o; // lane cache backcompat flag struct { // config: c3_o net_o; // can send @@ -2752,6 +2758,48 @@ _ames_recv_cb(uv_udp_t* wax_u, } } +static void natpmp_init(uv_timer_t* handle); + +static void natpmp_cb(uv_poll_t* handle, int status, int events) { + u3_ames* sam_u = handle->data; + + natpmpresp_t response; + c3_w r = readnatpmpresponseorretry(&sam_u->nat_u.req_u, &response); + if ( NATPMP_TRYAGAIN == r ) { + return; + } + if ( 0 != r ) { + u3l_log("ames: natpmp error %i", r); + uv_poll_stop(handle); + closenatpmp(&sam_u->nat_u.req_u); + return; + } + uv_poll_stop(handle); + + u3l_log("ames: mapped public port %hu to localport %hu lifetime %u", + response.pnu.newportmapping.mappedpublicport, + response.pnu.newportmapping.privateport, + response.pnu.newportmapping.lifetime); + + closenatpmp(&sam_u->nat_u.req_u); + sam_u->nat_u.tim_u.data = sam_u; + uv_timer_init(u3L, &sam_u->nat_u.tim_u); + uv_timer_start(&sam_u->nat_u.tim_u, natpmp_init, 7200000, 0); +} + +static void natpmp_init(uv_timer_t* handle) { + + u3_ames* sam_u = handle->data; + c3_s por_s = sam_u->pir_u->por_s; + + initnatpmp(&sam_u->nat_u.req_u, 0, 0); + sendnewportmappingrequest(&sam_u->nat_u.req_u, NATPMP_PROTOCOL_UDP, por_s, por_s, 7200); + + sam_u->nat_u.pol_u.data = sam_u; + uv_poll_init(u3L, &sam_u->nat_u.pol_u, sam_u->nat_u.req_u.s); + uv_poll_start(&sam_u->nat_u.pol_u, UV_READABLE, natpmp_cb); +} + static void _mdns_dear_bail(u3_ovum* egg_u, u3_noun lud) { @@ -2872,6 +2920,13 @@ _ames_io_start(u3_ames* sam_u) u3z(our); mdns_init(por_s, !sam_u->pir_u->fak_o, our_s, _ames_put_dear, (void *)sam_u); + + if ( c3n == sam_u->pir_u->fak_o ) { + sam_u->nat_u.tim_u.data = sam_u; + uv_timer_init(u3L, &sam_u->nat_u.tim_u); + uv_timer_start(&sam_u->nat_u.tim_u, natpmp_init, 0, 0); + } + c3_free(our_s); } From 8d320c6e9976a17e2cf8b7c360b2cf33fc69a555 Mon Sep 17 00:00:00 2001 From: pkova Date: Mon, 29 Jan 2024 16:12:46 +0200 Subject: [PATCH 020/107] bazel: make libnatpmp import better --- bazel/third_party/natpmp/natpmp.BUILD | 23 +++++------------------ 1 file changed, 5 insertions(+), 18 deletions(-) diff --git a/bazel/third_party/natpmp/natpmp.BUILD b/bazel/third_party/natpmp/natpmp.BUILD index f002ccafcc..b3a7edd7e0 100644 --- a/bazel/third_party/natpmp/natpmp.BUILD +++ b/bazel/third_party/natpmp/natpmp.BUILD @@ -1,21 +1,8 @@ -load("@rules_foreign_cc//foreign_cc:defs.bzl", "make") - -filegroup( - name = "all", - srcs = glob(["**"]), -) - -make( +cc_library( name = "natpmp", - out_static_libs = ["libnatpmp.a"], - out_lib_dir = "/usr/lib", - out_include_dir = "/usr/include", - args = select({ - "@platforms//os:macos": ["--jobs=`sysctl -n hw.logicalcpu`"], - "//conditions:default": ["--jobs=`nproc`"], - }), + srcs = ["natpmp.c", "getgateway.c"], + hdrs = ["natpmp.h", "getgateway.h", "natpmp_declspec.h"], copts = ["-O3"], - lib_source = ":all", + linkstatic = True, visibility = ["//visibility:public"], - postfix_script = "cp $BUILD_TMPDIR/natpmp_declspec.h $INSTALLDIR/usr/include/natpmp_declspec.h", -) \ No newline at end of file +) From f0f9cb4154a140db6adf3e6fdb5cabfef2aae1e8 Mon Sep 17 00:00:00 2001 From: Pyry Kovanen Date: Thu, 1 Feb 2024 13:48:21 +0200 Subject: [PATCH 021/107] Revert "Revert "build: recent macOS updates clang"" --- BUILD.bazel | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/BUILD.bazel b/BUILD.bazel index ff11949f5f..3569ed78a2 100644 --- a/BUILD.bazel +++ b/BUILD.bazel @@ -76,8 +76,8 @@ config_setting( # Version flag for clang. string_flag( name = "clang_version", - # macOS uses `clang-14.0.3` by default. - build_setting_default = "14.0.3", + # macOS uses `clang-15.0.0` by default. + build_setting_default = "15.0.0", visibility = ["//visibility:public"], ) From da7003f20c360d08c2deda564bc4e9969ae3085c Mon Sep 17 00:00:00 2001 From: fang Date: Thu, 8 Feb 2024 12:44:38 +0100 Subject: [PATCH 022/107] http: dynamic responses for empty cache entries If the cache entry for any given url was cleared, this would be indicated by a null entry for that url, and vere would serve a 404 for those cases, instead of falling back to the normal, dynamic behavior of injecting the request into eyre. Here, we make eyre serve from the cache only if there is a full cache entry for the url, and have it proceed into normal request injection logic otherwise. This way, setting a cached response for some url no longer means giving up dynamic responses on that url for the rest of time. Recent versions of eyre exhibit the same behavior. As such, in practice, this code won't change the observed behavior without a similarly-patched version of eyre. --- pkg/vere/io/http.c | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/pkg/vere/io/http.c b/pkg/vere/io/http.c index b5cb1d12cb..c234cf8881 100644 --- a/pkg/vere/io/http.c +++ b/pkg/vere/io/http.c @@ -111,6 +111,8 @@ typedef struct _u3_httd { u3p(u3h_root) nax_p; // scry->noun cache } u3_httd; +static u3_weak _http_rec_to_httq(h2o_req_t* rec_u); +static u3_hreq* _http_req_prepare(h2o_req_t* rec_u, u3_hreq* (*new_f)(u3_hcon*, h2o_req_t*)); static void _http_serv_free(u3_http* htp_u); static void _http_serv_start_all(u3_httd* htd_u); static void _http_form_free(u3_httd* htd_u); @@ -673,7 +675,19 @@ _http_cache_respond(u3_hreq* req_u, u3_noun nun) { u3_httd* htd_u = req_u->hon_u->htp_u->htd_u; if ( u3_nul == nun ) { - h2o_send_error_404(rec_u, "Not Found", "not found", 0); + u3_weak req = _http_rec_to_httq(rec_u); + if ( u3_none == req ) { + if ( (u3C.wag_w & u3o_verbose) ) { + u3l_log("strange %.*s request", (c3_i)rec_u->method.len, + rec_u->method.base); + } + c3_c* msg_c = "bad request"; + h2o_send_error_generic(rec_u, 400, msg_c, msg_c, 0); + } + else { + u3_hreq* req_u = _http_req_prepare(rec_u, _http_req_new); + _http_req_dispatch(req_u, req); + } } else if ( u3_none == u3r_at(7, nun) ) { h2o_send_error_500(rec_u, "Internal Server Error", "scry failed", 0); From 3a2c326b39be6354001966b3ab290113f94e23b6 Mon Sep 17 00:00:00 2001 From: pkova Date: Tue, 27 Feb 2024 15:06:30 +0200 Subject: [PATCH 023/107] macos: fix lldb --- pkg/vere/BUILD.bazel | 1 + pkg/vere/main.c | 7 +++++++ pkg/vere/platform/darwin/mach.c | 28 ++++++++++++++++++++++++++++ pkg/vere/vere.h | 7 +++++++ 4 files changed, 43 insertions(+) create mode 100644 pkg/vere/platform/darwin/mach.c diff --git a/pkg/vere/BUILD.bazel b/pkg/vere/BUILD.bazel index f50642ab89..64a7a4f6d7 100644 --- a/pkg/vere/BUILD.bazel +++ b/pkg/vere/BUILD.bazel @@ -110,6 +110,7 @@ vere_library( "@platforms//os:macos": [ "platform/darwin/daemon.c", "platform/darwin/ptty.c", + "platform/darwin/mach.c", ], "@platforms//os:linux": [ "platform/linux/daemon.c", diff --git a/pkg/vere/main.c b/pkg/vere/main.c index 599c8fc246..ec58b6dafc 100644 --- a/pkg/vere/main.c +++ b/pkg/vere/main.c @@ -1089,6 +1089,9 @@ _cw_serf_commence(c3_i argc, c3_c* argv[]) exit(1); } +#if defined(U3_OS_osx) + darwin_register_mach_exception_handler(); +#endif // XX use named arguments and getopt c3_d eve_d = 0; @@ -2871,6 +2874,10 @@ main(c3_i argc, _main_init(); +#if defined(U3_OS_osx) + darwin_register_mach_exception_handler(); +#endif + c3_c* bin_c = strdup(argv[0]); // parse for subcommands diff --git a/pkg/vere/platform/darwin/mach.c b/pkg/vere/platform/darwin/mach.c new file mode 100644 index 0000000000..fe8f2c1357 --- /dev/null +++ b/pkg/vere/platform/darwin/mach.c @@ -0,0 +1,28 @@ +#include "log.h" + +#include +#include +#include +#include + + +// lldb does not listen to BSD signals, it only listens to Mach exceptions. +// The Mach exception EXC_BAD_ACCESS corresponds to SIGSEGV, but lldb has +// problems converting between them because of a longstanding macOS kernel bug. +// This means that without this workaround we cannot debug our binaries with +// lldb. The first segfault we hit causes an infinite loop in lldb no matter +// how many times you try to continue. This workaround is implemented in projects +// such as the Go runtime. +// See https://bugs.llvm.org/show_bug.cgi?id=22868#c1 for more details. + +void darwin_register_mach_exception_handler() { + kern_return_t kr = task_set_exception_ports( + mach_task_self(), + EXC_MASK_BAD_ACCESS | EXC_MASK_ARITHMETIC, // SIGSEGV, SIGFPE + MACH_PORT_NULL, + EXCEPTION_STATE_IDENTITY, + MACHINE_THREAD_STATE); + if ( KERN_SUCCESS != kr) { + u3l_log("mono_runtime_install_handlers: task_set_exception_ports failed"); + } +} diff --git a/pkg/vere/vere.h b/pkg/vere/vere.h index bb3ac9df80..e4b62b8c6a 100644 --- a/pkg/vere/vere.h +++ b/pkg/vere/vere.h @@ -1545,6 +1545,13 @@ void u3_daemon_init(); +#if defined(U3_OS_osx) + /* darwin_register_mach_exception_handler(): make lldb work + */ + void + darwin_register_mach_exception_handler(); +#endif + /* u3_write_fd(): retry interrupts, continue partial writes, assert errors. */ void From a422adf4f5c10088e34e5957d0ecbb21c6a62f6e Mon Sep 17 00:00:00 2001 From: pkova Date: Wed, 28 Feb 2024 14:23:56 +0200 Subject: [PATCH 024/107] macos: better mach exception handler error message --- pkg/vere/platform/darwin/mach.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pkg/vere/platform/darwin/mach.c b/pkg/vere/platform/darwin/mach.c index fe8f2c1357..8c1c0d5935 100644 --- a/pkg/vere/platform/darwin/mach.c +++ b/pkg/vere/platform/darwin/mach.c @@ -23,6 +23,6 @@ void darwin_register_mach_exception_handler() { EXCEPTION_STATE_IDENTITY, MACHINE_THREAD_STATE); if ( KERN_SUCCESS != kr) { - u3l_log("mono_runtime_install_handlers: task_set_exception_ports failed"); + u3l_log("mach: unable to register exception handler"); } } From 0fff276e90bdbdefc2f1267be7c9717985217c50 Mon Sep 17 00:00:00 2001 From: pkova Date: Wed, 28 Feb 2024 14:24:26 +0200 Subject: [PATCH 025/107] macos: remove unnecessary mach SIGFPE handling --- pkg/vere/platform/darwin/mach.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pkg/vere/platform/darwin/mach.c b/pkg/vere/platform/darwin/mach.c index 8c1c0d5935..9775882d2e 100644 --- a/pkg/vere/platform/darwin/mach.c +++ b/pkg/vere/platform/darwin/mach.c @@ -18,7 +18,7 @@ void darwin_register_mach_exception_handler() { kern_return_t kr = task_set_exception_ports( mach_task_self(), - EXC_MASK_BAD_ACCESS | EXC_MASK_ARITHMETIC, // SIGSEGV, SIGFPE + EXC_MASK_BAD_ACCESS, // SIGSEGV MACH_PORT_NULL, EXCEPTION_STATE_IDENTITY, MACHINE_THREAD_STATE); From 5c44fbce4acc87fe70780a7ba727b59ce1a48d73 Mon Sep 17 00:00:00 2001 From: Joe Bryan Date: Thu, 7 Mar 2024 15:32:17 -0500 Subject: [PATCH 026/107] vere: add flag to continue running after behn: queue blocked --- pkg/vere/io/behn.c | 11 ++++++++--- pkg/vere/main.c | 6 ++++++ pkg/vere/vere.h | 1 + 3 files changed, 15 insertions(+), 3 deletions(-) diff --git a/pkg/vere/io/behn.c b/pkg/vere/io/behn.c index fc3ea413dc..0937c50f25 100644 --- a/pkg/vere/io/behn.c +++ b/pkg/vere/io/behn.c @@ -50,9 +50,14 @@ _behn_wake_bail(u3_ovum* egg_u, u3_noun lud) u3l_log("behn: timer failed; queue blocked"); - // XX review, add flag to continue? - // - u3_pier_bail(car_u->pir_u); + if ( c3n == u3_Host.ops_u.beb ) { + u3_pier_bail(car_u->pir_u); + } + else { + u3l_log("vere: warning: continuing without timers"); + u3z(lud); + u3_ovum_free(egg_u); + } } } diff --git a/pkg/vere/main.c b/pkg/vere/main.c index f83d656e98..a531a2a720 100644 --- a/pkg/vere/main.c +++ b/pkg/vere/main.c @@ -177,6 +177,7 @@ _main_init(void) u3_Host.ops_u.rep = c3n; u3_Host.ops_u.eph = c3n; u3_Host.ops_u.tos = c3n; + u3_Host.ops_u.beb = c3n; u3_Host.ops_u.tem = c3n; u3_Host.ops_u.tex = c3n; u3_Host.ops_u.tra = c3n; @@ -274,6 +275,7 @@ _main_getopt(c3_i argc, c3_c** argv) { "swap", no_argument, NULL, 7 }, { "swap-to", required_argument, NULL, 8 }, { "toss", required_argument, NULL, 9 }, + { "behn-allow-blocked", no_argument, NULL, 10 }, // { NULL, 0, NULL, 0 }, }; @@ -309,6 +311,10 @@ _main_getopt(c3_i argc, c3_c** argv) } break; } + case 10: { // behn-allow-blocked + u3_Host.ops_u.beb = c3y; + break; + } // special args // case c3__loom: { diff --git a/pkg/vere/vere.h b/pkg/vere/vere.h index cef16810b6..59133553fe 100644 --- a/pkg/vere/vere.h +++ b/pkg/vere/vere.h @@ -306,6 +306,7 @@ c3_o map; // --no-demand (reversed) c3_o eph; // --swap, use ephemeral file c3_o tos; // --toss, discard ephemeral + c3_o beb; // --behn-allow-blocked } u3_opts; /* u3_host: entire host. From 3517aef6f3b4e8cb5646af5624260da9ac62a300 Mon Sep 17 00:00:00 2001 From: pkova Date: Wed, 13 Mar 2024 21:07:55 +0200 Subject: [PATCH 027/107] events: better error messages for partial write --- pkg/noun/events.c | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/pkg/noun/events.c b/pkg/noun/events.c index d5e0ddac0f..2afeab1b32 100644 --- a/pkg/noun/events.c +++ b/pkg/noun/events.c @@ -652,10 +652,10 @@ _ce_patch_write_page(u3_ce_patch* pat_u, (ret_zs = pwrite(pat_u->mem_i, mem_w, _ce_page, _ce_len(pgc_w))) ) { if ( 0 < ret_zs ) { - fprintf(stderr, "loom: patch partial write: %"PRIc3_zs"\r\n", ret_zs); + fprintf(stderr, "loom: patch partial write: %"PRIc3_zs", you likely ran out of disk \r\n", ret_zs); } else { - fprintf(stderr, "loom: patch write: fail: %s\r\n", strerror(errno)); + fprintf(stderr, "loom: patch write: fail: %s, you likely ran out of disk\r\n", strerror(errno)); } u3_assert(0); } @@ -871,11 +871,11 @@ _ce_patch_apply(u3_ce_patch* pat_u) (ret_zs = pwrite(fid_i, buf_y, _ce_page, off_z)) ) { if ( 0 < ret_zs ) { - fprintf(stderr, "loom: patch apply partial write: %"PRIc3_zs"\r\n", + fprintf(stderr, "loom: patch apply partial write: %"PRIc3_zs", you likely ran out of disk \r\n", ret_zs); } else { - fprintf(stderr, "loom: patch apply write: %s\r\n", strerror(errno)); + fprintf(stderr, "loom: patch apply write: %s, you likely ran out of disk\r\n", strerror(errno)); } u3_assert(0); } @@ -1323,11 +1323,11 @@ _ce_image_copy(u3e_image* fom_u, u3e_image* tou_u) } if ( _ce_page != (ret_i = write(tou_u->fid_i, buf_y, _ce_page)) ) { if ( 0 < ret_i ) { - fprintf(stderr, "loom: image (%s) copy partial write: %zu\r\n", + fprintf(stderr, "loom: image (%s) copy partial write: %zu, you likely ran out of disk\r\n", tou_u->nam_c, (size_t)ret_i); } else { - fprintf(stderr, "loom: image (%s) copy write: %s\r\n", + fprintf(stderr, "loom: image (%s) copy write: %s, you likely ran out of disk\r\n", tou_u->nam_c, strerror(errno)); } return c3n; From fb010f8b68fd3076cb50cf06260435eb5a7ed78a Mon Sep 17 00:00:00 2001 From: Joe Bryan Date: Mon, 18 Mar 2024 21:53:15 -0400 Subject: [PATCH 028/107] ames: sets up test to import full driver --- pkg/vere/BUILD.bazel | 8 +++++++- pkg/vere/ames_tests.c | 3 +-- 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/pkg/vere/BUILD.bazel b/pkg/vere/BUILD.bazel index f50642ab89..91a933517a 100644 --- a/pkg/vere/BUILD.bazel +++ b/pkg/vere/BUILD.bazel @@ -183,6 +183,12 @@ vere_binary( # TESTS # + +cc_library( + name = "ames_src", + hdrs = ["io/ames.c"], +) + cc_test( name = "ames_tests", timeout = "short", @@ -192,7 +198,7 @@ cc_test( "//conditions:default": [], }), visibility = ["//visibility:private"], - deps = [":vere"], + deps = [":vere", ":ames_src"] ) cc_test( diff --git a/pkg/vere/ames_tests.c b/pkg/vere/ames_tests.c index 373788740f..c7644403a3 100644 --- a/pkg/vere/ames_tests.c +++ b/pkg/vere/ames_tests.c @@ -1,7 +1,6 @@ /// @file -#include "noun.h" -#include "vere.h" +#include "./io/ames.c" /* _setup(): prepare for tests. */ From e135995ba2aac27424b5b4c230c635af00a0cac7 Mon Sep 17 00:00:00 2001 From: Joe Bryan Date: Mon, 18 Mar 2024 21:54:00 -0400 Subject: [PATCH 029/107] ames: refactors stun implementation for testing, adds failing test --- pkg/vere/ames_tests.c | 42 ++++++++++++++ pkg/vere/io/ames.c | 129 +++++++++++++++++++++++------------------- 2 files changed, 112 insertions(+), 59 deletions(-) diff --git a/pkg/vere/ames_tests.c b/pkg/vere/ames_tests.c index c7644403a3..5be504a422 100644 --- a/pkg/vere/ames_tests.c +++ b/pkg/vere/ames_tests.c @@ -31,6 +31,44 @@ _test_ames(void) } } +static c3_i +_test_stun(void) +{ + u3_lane inn_u = { .pip_w = 0x7f000001, .por_s = 13337 }; + c3_c res_c[16] = {0}; + c3_y rep_y[40]; + c3_y req_y[20] = {0}; + c3_i ret_i = 0; + + struct sockaddr_in add_u; + memset(&add_u, 0, sizeof(add_u)); + add_u.sin_family = AF_INET; + add_u.sin_addr.s_addr = htonl(inn_u.pip_w); + add_u.sin_port = htons(inn_u.por_s); + + _stun_make_response(req_y, &add_u, rep_y); + + u3_lane lan_u; + + if ( c3n == _stun_find_xor_mapped_address(rep_y, sizeof(rep_y), &lan_u) ) { + fprintf(stderr, "stun: failed to find addr in response\r\n"); + ret_i = 1; + } + else { + if ( lan_u.pip_w != inn_u.pip_w ) { + fprintf(stderr, "stun: addr mismatch %x %x\r\n", lan_u.pip_w, inn_u.pip_w); + ret_i = 1; + } + + if ( lan_u.por_s != inn_u.por_s ) { + fprintf(stderr, "stun: addr mismatch %u %u\r\n", lan_u.por_s, inn_u.por_s); + ret_i = 1; + } + } + + return ret_i; +} + /* main(): run all test cases. */ int @@ -40,6 +78,10 @@ main(int argc, char* argv[]) _test_ames(); + if ( _test_stun() ) { + fprintf(stderr, "ames: stun tests failed\r\n"); + } + // GC // u3m_grab(u3_none); diff --git a/pkg/vere/io/ames.c b/pkg/vere/io/ames.c index eecef8ac75..228c758bbf 100644 --- a/pkg/vere/io/ames.c +++ b/pkg/vere/io/ames.c @@ -1343,12 +1343,11 @@ _stun_timer_cb(uv_timer_t* tim_u) } } -typedef struct _u3_stun_send { - uv_udp_send_t req_u; // uv udp request handle - u3_ames* sam_u; // backpointer to driver state - c3_y* hun_y; // buffer - -} u3_stun_send; +typedef struct _stun_send { + uv_udp_send_t req_u; // uv udp request handle + u3_ames* sam_u; // backpointer to driver state + c3_y hun_y[0]; // buffer +} _stun_send; static void _stun_on_request_fail(u3_ames* sam_u, c3_i sas_i) @@ -1364,8 +1363,8 @@ _stun_on_request_fail(u3_ames* sam_u, c3_i sas_i) static void _stun_send_request_cb(uv_udp_send_t *req_u, c3_i sas_i) { - u3_stun_send* snd_u = (u3_stun_send*)req_u; - u3_ames* sam_u = snd_u->sam_u; + _stun_send* snd_u = (_stun_send*)req_u; + u3_ames* sam_u = snd_u->sam_u; if ( sas_i ) { _stun_on_request_fail(sam_u, sas_i); @@ -1374,60 +1373,71 @@ _stun_send_request_cb(uv_udp_send_t *req_u, c3_i sas_i) // XX curently not used gettimeofday(&sam_u->sun_u.las_u, 0); // overwrite last sent date } - c3_free(snd_u->hun_y); + c3_free(snd_u); } static void _stun_send_response_cb(uv_udp_send_t *rep_u, c3_i sas_i) { - u3_stun_send* snd_u = (u3_stun_send*)rep_u; + _stun_send* snd_u = (_stun_send*)rep_u; + if ( sas_i != 0 ) { u3l_log("stun: _stun_send_response_cb fail_sync: %s", uv_strerror(sas_i)); } - c3_free(snd_u->hun_y); + c3_free(snd_u); } -static void _stun_on_request(u3_ames *sam_u, c3_y* buf_r, - const struct sockaddr* adr_u) +static void +_stun_make_response(const c3_y req_y[20], + const struct sockaddr_in* add_u, + c3_y buf_y[40]) { - struct sockaddr_in* add_u = (struct sockaddr_in*)adr_u; - c3_y *buf_y = c3_calloc(40); - c3_w cookie = 0x2112A442; + c3_w cok_w = 0x2112A442; + c3_w cur_w = 20; + + // XX hardcoded to match the requests we produce + // + memcpy(buf_y, req_y, cur_w); - c3_w cur_w = 20; // STUN header is 20 bytes - memcpy(buf_y, buf_r, cur_w); // copy STUN request header - buf_y[0] = 0x01; buf_y[1] = 0x01; // 0x0101 SUCCESS RESPONSE - buf_y[2] = 0x00; buf_y[3] = 0x14; // Length: 20 bytes + memset(buf_y + cur_w, 0, cur_w); // XOR-MAPPED-ADDRESS - buf_y[cur_w] = 0x00; buf_y[cur_w + 1] = 0x20; // attribute type 0x00020 - buf_y[cur_w + 2] = 0x00; buf_y[cur_w + 3] = 0x08; // STUN attribute length - // extra reserved 0x0 byte - buf_y[cur_w + 5] = 0x01; // family 0x01:IPv4 + buf_y[cur_w + 0] = 0x00; // + buf_y[cur_w + 1] = 0x20; // attribute type 0x00020 + buf_y[cur_w + 2] = 0x00; // + buf_y[cur_w + 3] = 0x08; // STUN attribute length + buf_y[cur_w + 4] = 0x00; // extra reserved 0x0 byte + buf_y[cur_w + 5] = 0x01; // family 0x01:IPv4 - c3_s x_port = htons(ntohs(add_u->sin_port) ^ cookie >> 16); - c3_w x_ip = htonl(ntohl(add_u->sin_addr.s_addr) ^ cookie); - memcpy(buf_y + cur_w + 6, &x_port, 2); // X-Port - memcpy(buf_y + cur_w + 8, &x_ip, 4); // X-IP Addres + c3_s por_s = htons(ntohs(add_u->sin_port) ^ cok_w >> 16); + c3_w pip_w = htonl(ntohl(add_u->sin_addr.s_addr) ^ cok_w); - // FINGERPRINT - buf_y = _stun_add_fingerprint(buf_y, cur_w + 12); + memcpy(buf_y + cur_w + 6, &por_s, 2); // X-Port + memcpy(buf_y + cur_w + 8, &pip_w, 4); // X-IP Addres - uv_buf_t buf_u = uv_buf_init((c3_c*)buf_y, 40); - u3_stun_send* snd_u = c3_calloc(sizeof(*snd_u)); + // FINGERPRINT + _stun_add_fingerprint(buf_y, cur_w + 12); +} +static void +_stun_on_request(u3_ames* sam_u, + const c3_y* req_y, + const struct sockaddr* adr_u) +{ + _stun_send* snd_u = c3_malloc(sizeof(*snd_u) + 40); snd_u->sam_u = sam_u; - snd_u->hun_y = buf_y; - c3_i sas_i = uv_udp_send( - (uv_udp_send_t*)snd_u, &sam_u->wax_u, &buf_u, 1, - adr_u, _stun_send_response_cb - ); + + // XX check req_y length + _stun_make_response(req_y, (struct sockaddr_in*)adr_u, snd_u->hun_y); + + uv_buf_t buf_u = uv_buf_init((c3_c*)snd_u->hun_y, 40); + c3_i sas_i = uv_udp_send(&snd_u->req_u, &sam_u->wax_u, + &buf_u, 1, adr_u, _stun_send_response_cb); if ( sas_i != 0 ) { u3l_log("stun: send response fail_sync: %s", uv_strerror(sas_i)); - c3_free(buf_y); c3_free(snd_u); } } @@ -1508,19 +1518,11 @@ _stun_on_lost(u3_ames* sam_u) } static void -_stun_send_request(u3_ames* sam_u) +_stun_make_request(c3_y buf_y[28], c3_y tid_y[12]) { - u3_assert( STUN_OFF != sam_u->sun_u.sat_y ); - - struct sockaddr_in add_u; - memset(&add_u, 0, sizeof(add_u)); - add_u.sin_family = AF_INET; - add_u.sin_addr.s_addr = htonl(sam_u->sun_u.lan_u.pip_w); - add_u.sin_port = htons(sam_u->sun_u.lan_u.por_s); - // see STUN RFC 8489 // https://datatracker.ietf.org/doc/html/rfc8489#section-5 - c3_y *buf_y = c3_calloc(28); + memset(buf_y, 0, 28); // STUN message type: "binding request" buf_y[1] = 0x01; @@ -1528,29 +1530,38 @@ _stun_send_request(u3_ames* sam_u) // STUN message length: 8 (header and 32-bit FINGERPRINT) buf_y[2] = 0x00; buf_y[3] = 0x08; - // STUN "magic cookie" 0x2112A442 in network byte order buf_y[4] = 0x21; buf_y[5] = 0x12; buf_y[6] = 0xa4; buf_y[7] = 0x42; // STUN "transaction id" - memcpy(buf_y + 8, sam_u->sun_u.tid_y, 12); + memcpy(buf_y + 8, tid_y, 12); // FINGERPRINT - buf_y = _stun_add_fingerprint(buf_y, 20); + _stun_add_fingerprint(buf_y, 20); +} + +static void +_stun_send_request(u3_ames* sam_u) +{ + u3_assert( STUN_OFF != sam_u->sun_u.sat_y ); - uv_buf_t buf_u = uv_buf_init((c3_c*)buf_y, 28); - u3_stun_send* snd_u = c3_calloc(sizeof(*snd_u)); + _stun_send* snd_u = c3_malloc(sizeof(*snd_u) + 28); snd_u->sam_u = sam_u; - snd_u->hun_y = buf_y; - c3_i sas_i = uv_udp_send( - (uv_udp_send_t*)snd_u, &sam_u->wax_u, &buf_u, 1, - (const struct sockaddr*)&add_u, _stun_send_request_cb - ); + _stun_make_request(snd_u->hun_y, sam_u->sun_u.tid_y); + + struct sockaddr_in add_u; + memset(&add_u, 0, sizeof(add_u)); + add_u.sin_family = AF_INET; + add_u.sin_addr.s_addr = htonl(sam_u->sun_u.lan_u.pip_w); + add_u.sin_port = htons(sam_u->sun_u.lan_u.por_s); + + uv_buf_t buf_u = uv_buf_init((c3_c*)snd_u->hun_y, 28); + c3_i sas_i = uv_udp_send(&snd_u->req_u, &sam_u->wax_u, &buf_u, 1, + (const struct sockaddr*)&add_u, _stun_send_request_cb); if ( sas_i != 0) { _stun_on_request_fail(sam_u, sas_i); - c3_free(buf_y); c3_free(snd_u); } } From 51fe3f1cb11e5749bde2a952681dfb2d34baa7cf Mon Sep 17 00:00:00 2001 From: Joe Bryan Date: Mon, 18 Mar 2024 22:05:19 -0400 Subject: [PATCH 030/107] blah switch to u3_lane --- pkg/vere/ames_tests.c | 8 +------- pkg/vere/io/ames.c | 34 +++++++++++++++++++--------------- 2 files changed, 20 insertions(+), 22 deletions(-) diff --git a/pkg/vere/ames_tests.c b/pkg/vere/ames_tests.c index 5be504a422..6e21d5f511 100644 --- a/pkg/vere/ames_tests.c +++ b/pkg/vere/ames_tests.c @@ -40,13 +40,7 @@ _test_stun(void) c3_y req_y[20] = {0}; c3_i ret_i = 0; - struct sockaddr_in add_u; - memset(&add_u, 0, sizeof(add_u)); - add_u.sin_family = AF_INET; - add_u.sin_addr.s_addr = htonl(inn_u.pip_w); - add_u.sin_port = htons(inn_u.por_s); - - _stun_make_response(req_y, &add_u, rep_y); + _stun_make_response(req_y, &inn_u, rep_y); u3_lane lan_u; diff --git a/pkg/vere/io/ames.c b/pkg/vere/io/ames.c index 228c758bbf..963f198ecd 100644 --- a/pkg/vere/io/ames.c +++ b/pkg/vere/io/ames.c @@ -1391,8 +1391,8 @@ _stun_send_response_cb(uv_udp_send_t *rep_u, c3_i sas_i) static void _stun_make_response(const c3_y req_y[20], - const struct sockaddr_in* add_u, - c3_y buf_y[40]) + u3_lane* lan_u, + c3_y buf_y[40]) { c3_w cok_w = 0x2112A442; c3_w cur_w = 20; @@ -1411,8 +1411,8 @@ _stun_make_response(const c3_y req_y[20], buf_y[cur_w + 4] = 0x00; // extra reserved 0x0 byte buf_y[cur_w + 5] = 0x01; // family 0x01:IPv4 - c3_s por_s = htons(ntohs(add_u->sin_port) ^ cok_w >> 16); - c3_w pip_w = htonl(ntohl(add_u->sin_addr.s_addr) ^ cok_w); + c3_s por_s = htons(lan_u->por_s ^ (cok_w >> 16)); + c3_w pip_w = htonl(lan_u->pip_w ^ cok_w); memcpy(buf_y + cur_w + 6, &por_s, 2); // X-Port memcpy(buf_y + cur_w + 8, &pip_w, 4); // X-IP Addres @@ -1422,15 +1422,19 @@ _stun_make_response(const c3_y req_y[20], } static void -_stun_on_request(u3_ames* sam_u, - const c3_y* req_y, - const struct sockaddr* adr_u) +_stun_on_request(u3_ames* sam_u, + const c3_y* req_y, + const struct sockaddr* adr_u) { _stun_send* snd_u = c3_malloc(sizeof(*snd_u) + 40); snd_u->sam_u = sam_u; - // XX check req_y length - _stun_make_response(req_y, (struct sockaddr_in*)adr_u, snd_u->hun_y); + struct sockaddr_in* add_u = (struct sockaddr_in*)adr_u; + u3_lane lan_u = { + .por_s = ntohs(add_u->sin_port), + .pip_w = ntohl(add_u->sin_addr.s_addr) + }; + _stun_make_response(req_y, &lan_u, snd_u->hun_y); uv_buf_t buf_u = uv_buf_init((c3_c*)snd_u->hun_y, 40); c3_i sas_i = uv_udp_send(&snd_u->req_u, &sam_u->wax_u, @@ -2749,13 +2753,13 @@ _ames_recv_cb(uv_udp_t* wax_u, == c3y) { _stun_on_response(sam_u, (c3_y*)buf_u->base, nrd_i); c3_free(buf_u->base); - } else { - u3_ames* sam_u = wax_u->data; + } + else { struct sockaddr_in* add_u = (struct sockaddr_in*)adr_u; - u3_lane lan_u; - - lan_u.por_s = ntohs(add_u->sin_port); - lan_u.pip_w = ntohl(add_u->sin_addr.s_addr); + u3_lane lan_u = { + .por_s = ntohs(add_u->sin_port), + .pip_w = ntohl(add_u->sin_addr.s_addr) + }; // NB: [nrd_i] will never exceed max length from _ames_alloc() // From 8dde0869512a34c0a29f1d5dac96ff03c67b1e33 Mon Sep 17 00:00:00 2001 From: Joe Bryan Date: Mon, 18 Mar 2024 22:21:30 -0400 Subject: [PATCH 031/107] ames: fix stun response decoding --- pkg/vere/io/ames.c | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) diff --git a/pkg/vere/io/ames.c b/pkg/vere/io/ames.c index 963f198ecd..e4426fe3ee 100644 --- a/pkg/vere/io/ames.c +++ b/pkg/vere/io/ames.c @@ -1730,16 +1730,14 @@ _stun_find_xor_mapped_address(c3_y* buf_y, c3_w buf_len, u3_lane* lan_u) cur += 2; - c3_s port = htons(_ames_sift_short(buf_y + cur)) ^ cookie >> 16; - c3_w ip = ntohl(htonl(_ames_sift_word(buf_y + cur + 2)) ^ cookie); - - lan_u->por_s = ntohs(port); - lan_u->pip_w = ip; + lan_u->por_s = ntohs(_ames_sift_short(buf_y + cur)) ^ (cookie >> 16); + lan_u->pip_w = ntohl(_ames_sift_word(buf_y + cur + 2)) ^ cookie; if ( u3C.wag_w & u3o_verbose ) { - c3_c ip_str[INET_ADDRSTRLEN]; - inet_ntop(AF_INET, &ip, ip_str, INET_ADDRSTRLEN); - u3l_log("stun: hear ip:port %s:%u", ip_str, port); + c3_w nip_w = htonl(lan_u->pip_w); + c3_c nip_c[INET_ADDRSTRLEN]; + inet_ntop(AF_INET, &nip_w, nip_c, INET_ADDRSTRLEN); + u3l_log("stun: hear ip:port %s:%u", nip_c, lan_u->por_s); } return c3y; } From 6f2e4dfe6ffdbf417fe622529e2c9f9911eb3cc2 Mon Sep 17 00:00:00 2001 From: Joe Bryan Date: Mon, 18 Mar 2024 22:21:54 -0400 Subject: [PATCH 032/107] ames: adds stun roundtrip tests for a range of ip:port pairs --- pkg/vere/ames_tests.c | 31 ++++++++++++++++++++++++------- 1 file changed, 24 insertions(+), 7 deletions(-) diff --git a/pkg/vere/ames_tests.c b/pkg/vere/ames_tests.c index 6e21d5f511..cb19fd4c5e 100644 --- a/pkg/vere/ames_tests.c +++ b/pkg/vere/ames_tests.c @@ -32,15 +32,14 @@ _test_ames(void) } static c3_i -_test_stun(void) +_test_stun_addr_roundtrip(u3_lane* inn_u) { - u3_lane inn_u = { .pip_w = 0x7f000001, .por_s = 13337 }; c3_c res_c[16] = {0}; c3_y rep_y[40]; c3_y req_y[20] = {0}; c3_i ret_i = 0; - _stun_make_response(req_y, &inn_u, rep_y); + _stun_make_response(req_y, inn_u, rep_y); u3_lane lan_u; @@ -49,13 +48,13 @@ _test_stun(void) ret_i = 1; } else { - if ( lan_u.pip_w != inn_u.pip_w ) { - fprintf(stderr, "stun: addr mismatch %x %x\r\n", lan_u.pip_w, inn_u.pip_w); + if ( lan_u.pip_w != inn_u->pip_w ) { + fprintf(stderr, "stun: addr mismatch %x %x\r\n", lan_u.pip_w, inn_u->pip_w); ret_i = 1; } - if ( lan_u.por_s != inn_u.por_s ) { - fprintf(stderr, "stun: addr mismatch %u %u\r\n", lan_u.por_s, inn_u.por_s); + if ( lan_u.por_s != inn_u->por_s ) { + fprintf(stderr, "stun: addr mismatch %u %u\r\n", lan_u.por_s, inn_u->por_s); ret_i = 1; } } @@ -63,6 +62,24 @@ _test_stun(void) return ret_i; } +static c3_i +_test_stun(void) +{ + u3_lane inn_u = { .pip_w = 0x7f000001, .por_s = 13337 }; + c3_w len_w = 256; + + while ( len_w-- ) { + if ( _test_stun_addr_roundtrip(&inn_u) ) { + return 1; + } + + inn_u.pip_w++; + inn_u.por_s++; + } + + return 0; +} + /* main(): run all test cases. */ int From 999e84199a7faaedfc5857d65517d64612f1245c Mon Sep 17 00:00:00 2001 From: Joe Bryan Date: Mon, 18 Mar 2024 22:22:22 -0400 Subject: [PATCH 033/107] mdns: silence compiler warning --- pkg/vere/mdns.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pkg/vere/mdns.c b/pkg/vere/mdns.c index 0875489625..80a2b3ac9a 100644 --- a/pkg/vere/mdns.c +++ b/pkg/vere/mdns.c @@ -61,7 +61,7 @@ static void resolve_cb(DNSServiceRef sref, payload->sref = sref; payload->port = port; - char *start = name; + const char *start = name; if (strncmp(name, "fake-", 4) == 0) { payload->fake = 1; start = name + 5; From fd62b5d4e80a2c3f6017a639476b1a06160ba708 Mon Sep 17 00:00:00 2001 From: Joe Bryan Date: Mon, 18 Mar 2024 22:26:54 -0400 Subject: [PATCH 034/107] ames: reorder _stun_on_response(), favoring %once over %stop --- pkg/vere/io/ames.c | 60 +++++++++++++++++++++++----------------------- 1 file changed, 30 insertions(+), 30 deletions(-) diff --git a/pkg/vere/io/ames.c b/pkg/vere/io/ames.c index e4426fe3ee..7cd462b7d6 100644 --- a/pkg/vere/io/ames.c +++ b/pkg/vere/io/ames.c @@ -1449,51 +1449,51 @@ _stun_on_request(u3_ames* sam_u, static void _stun_on_response(u3_ames* sam_u, c3_y* buf_y, c3_w buf_len) { - u3_stun_state old_y = sam_u->sun_u.sat_y; - u3_lane lan_u; // Ignore STUN responses that dont' have the XOR-MAPPED-ADDRESS attribute if ( c3n == _stun_find_xor_mapped_address(buf_y, buf_len, &lan_u) ) { return; } - u3_noun wir = u3nc(c3__ames, u3_nul); - if (sam_u->sun_u.wok_o == c3n) { - // stop %ping app - u3_noun cad = u3nq(c3__stun, c3__stop, sam_u->sun_u.dad_y, - u3nc(c3n, u3_ames_encode_lane(lan_u))); - u3_ovum *ovo_u = u3_ovum_init(0, c3__ames, wir, cad); - u3_auto_plan(&sam_u->car_u, ovo_u); - sam_u->sun_u.wok_o = c3y; - } - else if ( (sam_u->sun_u.sef_u.por_s != lan_u.por_s) || - (sam_u->sun_u.sef_u.pip_w != lan_u.pip_w) ) + + if ( (sam_u->sun_u.sef_u.por_s != lan_u.por_s) || + (sam_u->sun_u.sef_u.pip_w != lan_u.pip_w) ) { // lane changed + u3_noun wir = u3nc(c3__ames, u3_nul); u3_noun cad = u3nq(c3__stun, c3__once, sam_u->sun_u.dad_y, u3nc(c3n, u3_ames_encode_lane(lan_u))); - u3_ovum *ovo_u = u3_ovum_init(0, c3__ames, wir, cad); - u3_auto_plan(&sam_u->car_u, ovo_u); + u3_auto_plan(&sam_u->car_u, + u3_ovum_init(0, c3__ames, wir, cad)); } - else { - u3z(wir); + else if ( c3n == sam_u->sun_u.wok_o ) { + // stop %ping app + u3_noun wir = u3nc(c3__ames, u3_nul); + u3_noun cad = u3nq(c3__stun, c3__stop, sam_u->sun_u.dad_y, + u3nc(c3n, u3_ames_encode_lane(lan_u))); + u3_auto_plan(&sam_u->car_u, + u3_ovum_init(0, c3__ames, wir, cad)); + sam_u->sun_u.wok_o = c3y; } + sam_u->sun_u.sef_u = lan_u; switch ( sam_u->sun_u.sat_y ) { - case STUN_OFF: break; // ignore; stray response - case STUN_KEEPALIVE: break; // ignore; duplicate response - case STUN_TRYING: { - sam_u->sun_u.sat_y = STUN_KEEPALIVE; - if ( ent_getentropy(sam_u->sun_u.tid_y, 12) ) { - u3l_log("stun: getentropy fail: %s", strerror(errno)); - _stun_on_lost(sam_u); - } - else { - uv_timer_start(&sam_u->sun_u.tim_u, _stun_timer_cb, 25*1000, 0); - } - } break; - default: assert("programmer error"); + case STUN_OFF: break; // ignore; stray response + case STUN_KEEPALIVE: break; // ignore; duplicate response + + case STUN_TRYING: { + sam_u->sun_u.sat_y = STUN_KEEPALIVE; + if ( ent_getentropy(sam_u->sun_u.tid_y, 12) ) { + u3l_log("stun: getentropy fail: %s", strerror(errno)); + _stun_on_lost(sam_u); + } + else { + uv_timer_start(&sam_u->sun_u.tim_u, _stun_timer_cb, 25*1000, 0); + } + } break; + + default: assert("programmer error"); } } From 55cc458fad949844e48722d24a3b9432f6031c1c Mon Sep 17 00:00:00 2001 From: Sigilante <57601680+sigilante@users.noreply.github.com> Date: Mon, 25 Mar 2024 12:22:13 -0500 Subject: [PATCH 035/107] Update notice about swap space URL. --- pkg/noun/manage.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pkg/noun/manage.c b/pkg/noun/manage.c index d880a42032..71d673e357 100644 --- a/pkg/noun/manage.c +++ b/pkg/noun/manage.c @@ -2065,7 +2065,7 @@ u3m_init(size_t len_i) -1, 0); u3l_log("boot: mapping %zuMB failed", len_i >> 20); - u3l_log("see urbit.org/using/install/#about-swap-space" + u3l_log("see https://docs.urbit.org/manual/getting-started/self-hosted/cloud-hosting" " for adding swap space"); if ( -1 != (c3_ps)map_v ) { u3l_log("if porting to a new platform, try U3_OS_LoomBase %p", From 8736b2c635408b73d453ce25d4be0e15ab52c55c Mon Sep 17 00:00:00 2001 From: pkova Date: Wed, 3 Apr 2024 13:52:01 +0300 Subject: [PATCH 036/107] bazel: add -fdebug-compilation-dir for debuggers to find source code --- bazel/common_settings.bzl | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/bazel/common_settings.bzl b/bazel/common_settings.bzl index 6a61de1292..68a2232cd1 100644 --- a/bazel/common_settings.bzl +++ b/bazel/common_settings.bzl @@ -14,7 +14,7 @@ string_flag = rule( def vere_library(copts = [], linkopts = [], **kwargs): native.cc_library( copts = copts + select({ - "//:debug": ["-O0", "-g3", "-DC3DBG"], + "//:debug": ["-O0", "-g3", "-DC3DBG", "-fdebug-compilation-dir=."], "//conditions:default": ["-O3"] }) + select({ "//:lto": ['-flto'], @@ -39,7 +39,7 @@ def vere_library(copts = [], linkopts = [], **kwargs): def vere_binary(copts = [], linkopts = [], **kwargs): native.cc_binary( copts = copts + select({ - "//:debug": ["-O0", "-g3", "-DC3DBG"], + "//:debug": ["-O0", "-g3", "-DC3DBG", "-fdebug-compilation-dir=."], "//conditions:default": ["-O3"] }) + select({ "//:lto": ['-flto'], From 19227689558700d654e9d71291e51b5343912b69 Mon Sep 17 00:00:00 2001 From: pkova Date: Tue, 9 Apr 2024 15:29:34 +0300 Subject: [PATCH 037/107] install: add macos lldb guide --- INSTALL.md | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/INSTALL.md b/INSTALL.md index 5f3d2774f8..6c4d4e8d1d 100644 --- a/INSTALL.md +++ b/INSTALL.md @@ -52,6 +52,20 @@ This will take a few minutes. After installing `automake`, `autoconf-archive`, `pkg-config`, and `libtool`, you're ready to build Vere. +#### Debugger + +macOS is curious operating system because the kernel is derived from from two codebases, the Mach kernel and the BSD kernel. It inherits two different hardware exception handling facilities, Mach exceptions and POSIX signals. We use `libsigsegv` and utilize the POSIX signals which is usually fine except when it comes to debugging with `lldb`. + +`lldb` hijacks the Mach exception ports for the task when it attaches to the process. Mach exceptions get handled before POSIX signals which means that as soon as vere faults (this happens often) `lldb` stop with a `EXC_BAD_ACCESS`. It is impossible to continue debugging from this state without the workaround we implemented in https://github.com/urbit/vere/pull/611. + +There are more annoying warts with `lldb` currently. First, if you attach the debugger when booting the ship with `lldb -- your-ship/.run` you have to specify `-t`, otherwise the ship is unable to boot for mysterious reasons. The other option is to start the ship and attach afterwards with `lldb -p PID`. Afterwards you should do this dance: + +``` +p (void)darwin_register_mach_exception_handler() +pro hand -p true -s false -n false SIGBUS +pro hand -p true -s false -n false SIGSEGV +``` + ## Build Commands Once you install the prerequisites, you're ready to build: From 3556e4be871a0f79a6312406782ccc81a454709d Mon Sep 17 00:00:00 2001 From: pkova Date: Tue, 9 Apr 2024 15:29:48 +0300 Subject: [PATCH 038/107] main: only register mach exception handler once per process --- pkg/vere/main.c | 4 ---- 1 file changed, 4 deletions(-) diff --git a/pkg/vere/main.c b/pkg/vere/main.c index ec58b6dafc..3af75545fe 100644 --- a/pkg/vere/main.c +++ b/pkg/vere/main.c @@ -1088,10 +1088,6 @@ _cw_serf_commence(c3_i argc, c3_c* argv[]) fprintf(stderr, "serf: missing args\n"); exit(1); } - -#if defined(U3_OS_osx) - darwin_register_mach_exception_handler(); -#endif // XX use named arguments and getopt c3_d eve_d = 0; From 7c14e6949732db64db7d069b2b653f50e15520af Mon Sep 17 00:00:00 2001 From: Pyry Kovanen Date: Mon, 15 Apr 2024 18:39:33 +0300 Subject: [PATCH 039/107] events: you likely ran out of disk -> check disk space --- pkg/noun/events.c | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/pkg/noun/events.c b/pkg/noun/events.c index 2afeab1b32..6e82a87146 100644 --- a/pkg/noun/events.c +++ b/pkg/noun/events.c @@ -652,10 +652,10 @@ _ce_patch_write_page(u3_ce_patch* pat_u, (ret_zs = pwrite(pat_u->mem_i, mem_w, _ce_page, _ce_len(pgc_w))) ) { if ( 0 < ret_zs ) { - fprintf(stderr, "loom: patch partial write: %"PRIc3_zs", you likely ran out of disk \r\n", ret_zs); + fprintf(stderr, "loom: patch partial write: %"PRIc3_zs", check disk space\r\n", ret_zs); } else { - fprintf(stderr, "loom: patch write: fail: %s, you likely ran out of disk\r\n", strerror(errno)); + fprintf(stderr, "loom: patch write: fail: %s, check disk space\r\n", strerror(errno)); } u3_assert(0); } @@ -871,11 +871,11 @@ _ce_patch_apply(u3_ce_patch* pat_u) (ret_zs = pwrite(fid_i, buf_y, _ce_page, off_z)) ) { if ( 0 < ret_zs ) { - fprintf(stderr, "loom: patch apply partial write: %"PRIc3_zs", you likely ran out of disk \r\n", + fprintf(stderr, "loom: patch apply partial write: %"PRIc3_zs", check disk space\r\n", ret_zs); } else { - fprintf(stderr, "loom: patch apply write: %s, you likely ran out of disk\r\n", strerror(errno)); + fprintf(stderr, "loom: patch apply write: %s, check disk space\r\n", strerror(errno)); } u3_assert(0); } @@ -1323,11 +1323,11 @@ _ce_image_copy(u3e_image* fom_u, u3e_image* tou_u) } if ( _ce_page != (ret_i = write(tou_u->fid_i, buf_y, _ce_page)) ) { if ( 0 < ret_i ) { - fprintf(stderr, "loom: image (%s) copy partial write: %zu, you likely ran out of disk\r\n", + fprintf(stderr, "loom: image (%s) copy partial write: %zu, check disk space\r\n", tou_u->nam_c, (size_t)ret_i); } else { - fprintf(stderr, "loom: image (%s) copy write: %s, you likely ran out of disk\r\n", + fprintf(stderr, "loom: image (%s) copy write: %s, check disk space\r\n", tou_u->nam_c, strerror(errno)); } return c3n; From c774a317010052bbaf2e2f472fff7efb35d42409 Mon Sep 17 00:00:00 2001 From: Josh Lehman Date: Tue, 16 Apr 2024 05:57:18 -0700 Subject: [PATCH 040/107] add more helpful chop message --- pkg/vere/disk.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pkg/vere/disk.c b/pkg/vere/disk.c index 18fdf31fec..319403566f 100644 --- a/pkg/vere/disk.c +++ b/pkg/vere/disk.c @@ -1632,7 +1632,7 @@ u3_disk_chop(u3_disk* log_u, c3_d eve_d) u3_disk_epoc_list(log_u, sot_d); if ( len_z <= 2 ) { - fprintf(stderr, "chop: nothing to do, have a great day\r\n"); + fprintf(stderr, "chop: nothing to do, try running roll first\r\n"); exit(0); // enjoy } From 1d137d3b9f3ea501bba9c17deb01c2b7128d1d9d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Aurimas=20Bla=C5=BEulionis?= Date: Tue, 16 Apr 2024 13:34:27 +0100 Subject: [PATCH 041/107] nix: fix vere compilation --- INSTALL.md | 7 +++++++ bazel/third_party/avahi/avahi.BUILD | 2 +- flake.lock | 12 ++++++------ flake.nix | 6 +++++- 4 files changed, 19 insertions(+), 8 deletions(-) diff --git a/INSTALL.md b/INSTALL.md index 6c4d4e8d1d..9cc67d17e4 100644 --- a/INSTALL.md +++ b/INSTALL.md @@ -150,6 +150,13 @@ doesn't match the version of `clang` installed on your system. To address this, run `clang --version` and pass the version number via `--clang_version=""` to the failing command. +If build fails on nix/NixOS, you should pass `ACLOCAL_PATH` environment +variable to bazel, using `--action_env=ACLOCAL_PATH=$ACLOCAL_PATH`, like so: + +``` +bazel build :urbit --action_env=ACLOCAL_PATH=$ACLOCAL_PATH +``` + [^1]: If you're interested in digging into the details of the build system, check out [`WORKSPACE.bazel`](WORKSPACE.bazel), [`BUILD.bazel`](BUILD.bazel), [`bazel/`](bazel), and the multiple diff --git a/bazel/third_party/avahi/avahi.BUILD b/bazel/third_party/avahi/avahi.BUILD index 84d27171c2..2c0896f32d 100644 --- a/bazel/third_party/avahi/avahi.BUILD +++ b/bazel/third_party/avahi/avahi.BUILD @@ -17,7 +17,7 @@ configure_make( "@platforms//os:macos": ["--jobs=`sysctl -n hw.logicalcpu`"], "//conditions:default": ["--jobs=`nproc`"], }), - configure_options = ["--with-dbus-system-address='unix:path=/var/run/dbus/system_bus_socket' --with-xml=none --disable-libevent --disable-glib --disable-gobject --disable-gdbm --disable-qt3 --disable-qt4 --disable-qt5 --disable-gtk --disable-gtk3 --disable-mono --disable-monodoc --disable-python --disable-libdaemon --enable-compat-libdns_sd --disable-rpath"], + configure_options = ["--with-dbus-system-address='unix:path=/var/run/dbus/system_bus_socket' --with-xml=none --disable-libevent --disable-glib --disable-gobject --disable-gdbm --disable-qt3 --disable-qt4 --disable-qt5 --disable-gtk --disable-gtk3 --disable-mono --disable-monodoc --disable-python --disable-libdaemon --enable-compat-libdns_sd --disable-rpath --with-distro=none"], lib_source = ":all", # out_include_dir = "avahi-compat-libdns_sd", deps = ["@dbus"], diff --git a/flake.lock b/flake.lock index e0fc6ef9a4..10f88e0e43 100644 --- a/flake.lock +++ b/flake.lock @@ -2,11 +2,11 @@ "nodes": { "nixpkgs": { "locked": { - "lastModified": 1701626906, - "narHash": "sha256-ugr1QyzzwNk505ICE4VMQzonHQ9QS5W33xF2FXzFQ00=", + "lastModified": 1712849433, + "narHash": "sha256-flQtf/ZPJgkLY/So3Fd+dGilw2DKIsiwgMEn7BbBHL0=", "owner": "nixos", "repo": "nixpkgs", - "rev": "0c6d8c783336a59f4c59d4a6daed6ab269c4b361", + "rev": "f173d0881eff3b21ebb29a2ef8bedbc106c86ea5", "type": "github" }, "original": { @@ -23,11 +23,11 @@ ] }, "locked": { - "lastModified": 1701473968, - "narHash": "sha256-YcVE5emp1qQ8ieHUnxt1wCZCC3ZfAS+SRRWZ2TMda7E=", + "lastModified": 1712014858, + "narHash": "sha256-sB4SWl2lX95bExY2gMFG5HIzvva5AVMJd4Igm+GpZNw=", "owner": "hercules-ci", "repo": "flake-parts", - "rev": "34fed993f1674c8d06d58b37ce1e0fe5eebcb9f5", + "rev": "9126214d0a59633752a136528f5f3b9aa8565b7d", "type": "github" }, "original": { diff --git a/flake.nix b/flake.nix index 336f0f3ba2..05237289b3 100644 --- a/flake.nix +++ b/flake.nix @@ -104,12 +104,16 @@ ] ++ (with pkgs; [ autoconf + autoconf-archive automake - bazel_5 + bazel_6 binutils # for `nm` jdk11_headless libtool m4 + pkg-config + git + perl ]); extraBuildCommands = '' chmod +w usr From bf88fad227f5dc60a5f9bb441ebf7f16a32b926b Mon Sep 17 00:00:00 2001 From: Joe Bryan Date: Thu, 18 Apr 2024 10:05:32 -0400 Subject: [PATCH 042/107] ames: adds new batch czar dns --- pkg/vere/io/ames.c | 227 ++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 214 insertions(+), 13 deletions(-) diff --git a/pkg/vere/io/ames.c b/pkg/vere/io/ames.c index 7cd462b7d6..5470f06248 100644 --- a/pkg/vere/io/ames.c +++ b/pkg/vere/io/ames.c @@ -24,6 +24,8 @@ #define QUEUE_MAX 30 // max number of packets in queue +#define _CZAR_GONE UINT32_MAX + typedef enum u3_stun_state { STUN_OFF = 0, STUN_TRYING = 1, @@ -58,6 +60,14 @@ typedef enum u3_stun_state { c3_w imp_w[256]; // imperial IPs time_t imp_t[256]; // imperial IP timestamps c3_o imp_o[256]; // imperial print status + struct { // + c3_c dom_c[251]; // domain + c3_o dom_o; // have domain + uv_timer_t tim_u; // resolve timer + c3_s pen_s; // pending + c3_w pip_w[256]; // ipv4 + c3_w log_w[256 >> 5]; // log error + } zar_u; // struct { // stun client state: u3_stun_state sat_y; // formal state c3_y tid_y[12]; // last transaction id @@ -1028,6 +1038,41 @@ _ames_czar_port(c3_y imp_y) } } +/* _ames_czar_str: galaxy name as c3_c[3] +*/ +static void +_ames_czar_str(c3_c zar_c[3], c3_y imp_y) +{ + u3_po_to_suffix(imp_y, (c3_y*)zar_c, (c3_y*)zar_c + 1, (c3_y*)zar_c + 2); +} + +/* _ames_etch_czar: galaxy fqdn +*/ +static c3_i +_ames_etch_czar(c3_c dns_c[256], const c3_c* dom_c, c3_y imp_y) +{ + c3_c* bas_c = dns_c; + c3_w len_w = strlen(dom_c); + + // name 3, '.' 2, trailing null + // + if ( 250 <= len_w ) { + return -1; + } + + _ames_czar_str(dns_c, imp_y); + dns_c += 3; + *dns_c++ = '.'; + + memcpy(dns_c, dom_c, len_w); + dns_c += len_w; + *dns_c++ = '.'; + + memset(dns_c, 0, 256 - (dns_c - bas_c)); + + return 0; +} + static c3_c* _ames_czar_dns(c3_y imp_y, c3_c* czar_c) { @@ -2894,34 +2939,187 @@ _ames_io_start(u3_ames* sam_u) u3z(who); } +typedef struct _czar_resv { + uv_getaddrinfo_t adr_u; + u3_ames* sam_u; + c3_y imp_y; +} _czar_resv; + +/* _ames_czar2_gone(): galaxy address resolution failed. +*/ +static void +_ames_czar2_gone(u3_ames* sam_u, c3_y imp_y) +{ + c3_w old_w = sam_u->zar_u.pip_w[imp_y]; + + if ( !old_w ) { + sam_u->zar_u.pip_w[imp_y] = _CZAR_GONE; + } +} + +/* _ames_czar2_here(): galaxy address resolution succeeded. +*/ +static void +_ames_czar2_here(u3_ames* sam_u, c3_y imp_y, c3_w pip_w) +{ + c3_w old_w = sam_u->zar_u.pip_w[imp_y]; + + if ( pip_w != old_w ) { + c3_c dns_c[256]; + u3_assert ( !_ames_etch_czar(dns_c, sam_u->zar_u.dom_c, imp_y) ); + u3l_log("ames: czar %s ip .%d.%d.%d.%d", dns_c, + (pip_w >> 24) & 0xff, + (pip_w >> 16) & 0xff, + (pip_w >> 8) & 0xff, + (pip_w >> 0) & 0xff); + } + + sam_u->zar_u.pip_w[imp_y] = imp_y; + + { + c3_w blk_w = imp_y >> 5; + c3_w bit_w = 1 << (imp_y & 31); + + sam_u->zar_u.log_w[blk_w] &= ~bit_w; + } +} + +/* _ames_czar2_cb(): galaxy address resolution callback. +*/ +static void +_ames_czar2_cb(uv_getaddrinfo_t* adr_u, + c3_i sas_i, + struct addrinfo* aif_u) +{ + struct addrinfo* rai_u = aif_u; + _czar_resv* res_u = (_czar_resv*)adr_u; + u3_ames* sam_u = res_u->sam_u; + c3_y imp_y = res_u->imp_y; + + while ( rai_u && (AF_INET != rai_u->ai_family) ) { + rai_u = rai_u->ai_next; + } + + if ( rai_u && rai_u->ai_addr ) { + struct sockaddr_in* add_u = (void*)rai_u->ai_addr; + c3_w pip_w = ntohl(add_u->sin_addr.s_addr); + _ames_czar2_here(sam_u, imp_y, pip_w); + } + else { + if ( !sas_i ) { + // XX unpossible + u3l_log("ames: czar: strange failure, no error"); + } + else if ( u3C.wag_w & u3o_verbose ) { + u3l_log("ames: czar fail: %s", uv_strerror(sas_i)); + } + + _ames_czar2_gone(sam_u, imp_y); + } + + uv_freeaddrinfo(aif_u); + c3_free(res_u); +} + +/* _ames_czar2(): single galaxy address resolution. +*/ +static void +_ames_czar2(u3_ames* sam_u, const c3_c* dom_c, c3_y imp_y) +{ + struct addrinfo hin_u = { .ai_family = AF_INET }; + uv_getaddrinfo_t* adr_u; + _czar_resv* res_u; + c3_c dns_c[256]; + c3_i sas_i; + + u3_assert ( !_ames_etch_czar(dns_c, dom_c, imp_y) ); + + res_u = c3_malloc(sizeof(*res_u)); + res_u->sam_u = sam_u; + res_u->imp_y = imp_y; + + adr_u = &(res_u->adr_u); + sas_i = uv_getaddrinfo(u3L, adr_u, _ames_czar2_cb, dns_c, 0, &hin_u); + + if ( sas_i ) { + _ames_czar2_cb(adr_u, sas_i, NULL); + } +} + +/* _ames_czar_all(): galaxy address resolution. +*/ +static void +_ames_czar_all(uv_timer_t* tim_u) +{ + u3_ames* sam_u = tim_u->data; + + // requests still pending + if ( sam_u->zar_u.pen_s ) { + uv_timer_start(&sam_u->zar_u.tim_u, _ames_czar_all, 30*1000, 0); + return; + } + + sam_u->zar_u.pen_s = 256; + + for ( c3_w i_w; i_w < 256; i_w++ ) { + _ames_czar2(sam_u, sam_u->zar_u.dom_c, (c3_y)i_w); + } + + uv_timer_start(&sam_u->zar_u.tim_u, _ames_czar_all, 300*1000, 0); +} + /* _ames_ef_turf(): initialize ames I/O on domain(s). */ static void _ames_ef_turf(u3_ames* sam_u, u3_noun tuf) { if ( u3_nul != tuf ) { - // XX save all for fallback, not just first - // - u3_noun hot = u3k(u3h(tuf)); + c3_c dom_c[sizeof(sam_u->zar_u.dom_c)]; + u3_noun hot = u3h(tuf); c3_w len_w = u3_mcut_host(0, 0, u3k(hot)); - sam_u->dns_c = c3_malloc(1 + len_w); - u3_mcut_host(sam_u->dns_c, 0, hot); - sam_u->dns_c[len_w] = 0; - - if ( 250 <= len_w ) { + if ( len_w >= sizeof(dom_c) ) { // >250 // 3 char for the galaxy (e.g. zod) and two dots - u3l_log("ames: galaxy domain too big %s len=%u", sam_u->dns_c, len_w); + u3l_log("ames: galaxy domain too big (len=%u)", len_w); + u3m_p("hot", hot); u3_pier_bail(u3_king_stub()); } - // XX invalidate sam_u->imp_w &c ? - c3_free(sam_u->sun_u.dns_c); - sam_u->sun_u.dns_c = 0; + u3_mcut_host(dom_c, 0, u3k(hot)); + memset(dom_c + len_w, 0, sizeof(dom_c) - len_w); + + if ( 0 != memcmp(sam_u->zar_u.dom_c, dom_c, sizeof(dom_c)) ) { + // XX legacy + // + sam_u->dns_c = c3_malloc(1 + len_w); + memcpy(sam_u->dns_c, dom_c, len_w); + sam_u->dns_c[len_w] = 0; + + // XX review + // + c3_free(sam_u->sun_u.dns_c); + sam_u->sun_u.dns_c = 0; + + memcpy(sam_u->zar_u.dom_c, dom_c, sizeof(dom_c)); + memset(sam_u->zar_u.pip_w, 0, sizeof(sam_u->zar_u.pip_w)); + sam_u->zar_u.dom_o = c3y; + _ames_czar_all(&(sam_u->zar_u.tim_u)); + } + + // XX save all for fallback, not just first + // + if ( u3_nul != u3t(tuf) ) { + u3l_log("ames: turf: ignoring additional domains"); + u3m_p("second", u3h(u3t(tuf))); + + if ( u3_nul != u3t(u3t(tuf)) ) { + u3m_p("third", u3h(u3t(u3t(tuf)))); + } + } u3z(tuf); } - else if ( (c3n == sam_u->pir_u->fak_o) && (0 == sam_u->dns_c) ) { + else if ( (c3n == sam_u->pir_u->fak_o) && (c3y == sam_u->zar_u.dom_o) ) { u3l_log("ames: turf: no domains"); } @@ -3222,6 +3420,9 @@ u3_ames_io_init(u3_pier* pir_u) sam_u->fig_u.see_o = c3y; sam_u->fig_u.fit_o = c3n; + uv_timer_init(u3L, &sam_u->zar_u.tim_u); + sam_u->zar_u.tim_u.data = sam_u; + // initialize STUN timers uv_timer_init(u3L, &sam_u->sun_u.dns_u); uv_timer_init(u3L, &sam_u->sun_u.tim_u); From 006d7a7ca483c8714a08459882959f132570b72d Mon Sep 17 00:00:00 2001 From: Joe Bryan Date: Thu, 18 Apr 2024 10:38:44 -0400 Subject: [PATCH 043/107] ames: refactor packet sending, removes dns queuing --- pkg/vere/io/ames.c | 156 +++++++++++++++++++++++++++++++-------------- 1 file changed, 108 insertions(+), 48 deletions(-) diff --git a/pkg/vere/io/ames.c b/pkg/vere/io/ames.c index 5470f06248..5f8dc4e43b 100644 --- a/pkg/vere/io/ames.c +++ b/pkg/vere/io/ames.c @@ -1073,6 +1073,48 @@ _ames_etch_czar(c3_c dns_c[256], const c3_c* dom_c, c3_y imp_y) return 0; } +/* _ames_czar_lane: retrieve lane for galaxy if stored. +*/ +static c3_o +_ames_czar_lane(u3_ames* sam_u, c3_y imp_y, u3_lane* lan_u) +{ + c3_s por_s = _ames_czar_port(imp_y); + c3_w pip_w; + + if ( c3n == u3_Host.ops_u.net ) { + pip_w = 0x7f000001; + } + else { + pip_w = sam_u->zar_u.pip_w[imp_y]; + + if ( !pip_w ) { + if ( u3C.wag_w & u3o_verbose ) { + u3l_log("ames: czar not resolved"); + } + return c3n; + } + else if ( _CZAR_GONE == pip_w ) { + // print only on first send failure + // + c3_w blk_w = imp_y >> 5; + c3_w bit_w = 1 << (imp_y & 31); + + if ( !(sam_u->zar_u.log_w[blk_w] & bit_w) ) { + c3_c dns_c[256]; + u3_assert ( !_ames_etch_czar(dns_c, sam_u->zar_u.dom_c, imp_y) ); + u3l_log("ames: czar at %s: not found (b)", dns_c); + sam_u->zar_u.log_w[blk_w] |= bit_w; + } + + return c3n; + } + } + + lan_u->por_s = por_s; + lan_u->pip_w = pip_w; + return c3y; +} + static c3_c* _ames_czar_dns(c3_y imp_y, c3_c* czar_c) { @@ -1908,6 +1950,61 @@ _ames_ef_saxo(u3_ames* sam_u, u3_noun zad) u3z(zad); u3z(daz); u3z(our); } +/* _ames_send_lane(): resolve/decode lane. RETAIN +*/ +static c3_o +_ames_send_lane(u3_ames* sam_u, u3_noun lan, u3_lane* lan_u) +{ + u3_noun tag, val; + + if ( c3n == u3r_cell(lan, &tag, &val) ) { + u3l_log("ames: bad lane; not a cell"); + return c3n; + } + + switch ( tag ) { + case c3y: { // galaxy + if ( val >= 256 ) { + u3l_log("ames: bad galaxy lane: 0x%x", val); + return c3n; + } + return _ames_czar_lane(sam_u, (c3_y)val, lan_u); + } + + case c3n: { // ip:port + u3_lane nal_u = u3_ames_decode_lane(u3k(val)); + + // convert incoming localhost to outgoing localhost + // + // XX this looks like en/de-coding problems ... + // + nal_u.pip_w = ( nal_u.pip_w ) ? nal_u.pip_w : 0x7f000001; + + // if in local-only mode, don't send remote packets + // + if ( (c3n == u3_Host.ops_u.net) && (0x7f000001 != nal_u.pip_w) ) { + return c3n; + } + // if the lane is uninterpretable, silently drop the packet + // + else if ( !nal_u.por_s ) { + if ( u3C.wag_w & u3o_verbose ) { + u3l_log("ames: inscrutable lane"); + } + return c3n; + } + + *lan_u = nal_u; + return c3y; + } + + default: { + u3l_log("ames: bad lane tag"); + return c3n; + } + } +} + /* _ames_ef_send(): send packet to network (v4). */ static void @@ -1919,60 +2016,23 @@ _ames_ef_send(u3_ames* sam_u, u3_noun lan, u3_noun pac) return; } - u3_pact* pac_u = c3_calloc(sizeof(*pac_u)); - pac_u->sam_u = sam_u; - pac_u->len_w = u3r_met(3, pac); - pac_u->hun_y = c3_malloc(pac_u->len_w); - - u3r_bytes(0, pac_u->len_w, pac_u->hun_y, pac); + u3_lane lan_u; - _ames_sift_head(&pac_u->hed_u, pac_u->hun_y); - pac_u->typ_y = _ames_pact_typ(&pac_u->hed_u); + if ( c3y == _ames_send_lane(sam_u, lan, &lan_u) ) { + u3_pact* pac_u = c3_calloc(sizeof(*pac_u)); + pac_u->sam_u = sam_u; + pac_u->rut_u.lan_u = lan_u; + pac_u->len_w = u3r_met(3, pac); + pac_u->hun_y = c3_malloc(pac_u->len_w); - u3_noun tag, val; - u3x_cell(lan, &tag, &val); - u3_assert( (c3y == tag) || (c3n == tag) ); + u3r_bytes(0, pac_u->len_w, pac_u->hun_y, pac); - // galaxy lane; do DNS lookup and send packet - // - if ( c3y == tag ) { - u3_assert( c3y == u3a_is_cat(val) ); - u3_assert( val < 256 ); + _ames_sift_head(&pac_u->hed_u, pac_u->hun_y); + pac_u->typ_y = _ames_pact_typ(&pac_u->hed_u); - //u3l_log("_ames_ef_send imp %s %u", _str_typ(pac_u->typ_y), val); - pac_u->rut_u.imp_y = val; - _ames_czar(pac_u); + _ames_send(pac_u); } - // non-galaxy lane - // - else { - u3_lane lan_u = u3_ames_decode_lane(u3k(val)); - ////u3l_log("_ames_ef_send low %s %u", _str_typ(pac_u->typ_y), - // lan_u.por_s); - // convert incoming localhost to outgoing localhost - // - lan_u.pip_w = ( 0 == lan_u.pip_w )? 0x7f000001 : lan_u.pip_w; - // if in local-only mode, don't send remote packets - // - if ( (c3n == u3_Host.ops_u.net) && (0x7f000001 != lan_u.pip_w) ) { - _ames_pact_free(pac_u); - } - // if the lane is uninterpretable, silently drop the packet - // - else if ( 0 == lan_u.por_s ) { - if ( u3C.wag_w & u3o_verbose ) { - u3l_log("ames: inscrutable lane"); - } - _ames_pact_free(pac_u); - } - // otherwise, mutate destination and send packet - // - else { - pac_u->rut_u.lan_u = lan_u; - _ames_send(pac_u); - } - } u3z(lan); u3z(pac); } From 48b368ff87954e4fcdbfc88bfa47d0e30d7bb0fd Mon Sep 17 00:00:00 2001 From: Joe Bryan Date: Thu, 18 Apr 2024 11:50:55 -0400 Subject: [PATCH 044/107] ames: removes old czar dns implementation --- pkg/vere/io/ames.c | 131 --------------------------------------------- 1 file changed, 131 deletions(-) diff --git a/pkg/vere/io/ames.c b/pkg/vere/io/ames.c index 5f8dc4e43b..1c3421633d 100644 --- a/pkg/vere/io/ames.c +++ b/pkg/vere/io/ames.c @@ -1154,33 +1154,6 @@ _ames_czar_gone(u3_ames* sam_u, time_t now, c3_d imp_y, c3_c* dns_c) sam_u->imp_t[imp_y] = now; } -/* _ames_czar_here(): galaxy address resolution succeeded. -*/ -static void -_ames_czar_here(u3_pact* pac_u, time_t now, struct sockaddr_in* add_u) -{ - u3_ames* sam_u = pac_u->sam_u; - c3_y imp_y = pac_u->rut_u.imp_y; - c3_w old_w = sam_u->imp_w[imp_y]; - c3_w pip_w = ntohl(add_u->sin_addr.s_addr); - - if ( pip_w != old_w ) { - u3_noun nam = u3dc("scot", c3__if, u3i_word(pip_w)); - c3_c* nam_c = u3r_string(nam); - - u3l_log("ames: czar %s ip %s", pac_u->rut_u.dns_c, nam_c); - - c3_free(nam_c); - u3z(nam); - } - - sam_u->imp_w[imp_y] = pip_w; - sam_u->imp_t[imp_y] = now; - sam_u->imp_o[imp_y] = c3y; - - pac_u->rut_u.lan_u.pip_w = pip_w; -} - /* _stun_czar_here(): sponsor galaxy address resolution succeeded. */ static c3_w @@ -1208,110 +1181,6 @@ _stun_czar_here(u3_ames* sam_u, time_t now, struct sockaddr_in* add_u) return pip_w; } -/* _ames_czar_cb(): galaxy address resolution callback. -*/ -static void -_ames_czar_cb(uv_getaddrinfo_t* adr_u, - c3_i sas_i, - struct addrinfo* aif_u) -{ - { - u3_pact* pac_u = (u3_pact*)adr_u->data; - struct addrinfo* rai_u = aif_u; - time_t now = time(0); - - if ( sas_i == 0 ) { - _ames_czar_here(pac_u, now, (struct sockaddr_in *)rai_u->ai_addr); - } else { - _ames_czar_gone(pac_u->sam_u, now, - pac_u->rut_u.imp_y, - pac_u->rut_u.dns_c); - _ames_pact_free(pac_u); - } - } - - c3_free(adr_u); - uv_freeaddrinfo(aif_u); -} - -/* _ames_czar(): galaxy address resolution. -*/ -static void -_ames_czar(u3_pact* pac_u) -{ - u3_ames* sam_u = pac_u->sam_u; - - c3_y imp_y = pac_u->rut_u.imp_y; - - pac_u->rut_u.lan_u.por_s = _ames_czar_port(imp_y); - - if ( c3n == u3_Host.ops_u.net ) { - pac_u->rut_u.lan_u.pip_w = 0x7f000001; - _ames_send(pac_u); - return; - } - - // if we don't have a galaxy domain, no-op - // - if ( !sam_u->dns_c ) { - u3_noun nam = u3dc("scot", 'p', pac_u->rut_u.imp_y); - c3_c* nam_c = u3r_string(nam); - u3l_log("ames: no galaxy domain for %s, no-op", nam_c); - - c3_free(nam_c); - u3z(nam); - return; - } - - { - c3_w pip_w = sam_u->imp_w[imp_y]; - time_t wen = sam_u->imp_t[imp_y]; - time_t now = time(0); - - // backoff for 5 minutes after failed lookup - // - if ( ( now < wen ) // time shenanigans! - || ( (0xffffffff == pip_w) // sentinal ip address - && ((now - wen) < 300) ) ) - { - _ames_pact_free(pac_u); - return; - } - // cached addresses have a 5 minute TTL - // - else if ( (0 != pip_w) && ((now - wen) < 300) ) { - pac_u->rut_u.lan_u.pip_w = pip_w; - _ames_send(pac_u); - return; - } - else { - pac_u->rut_u.dns_c = _ames_czar_dns(imp_y, sam_u->dns_c); - - { - uv_getaddrinfo_t* adr_u = c3_malloc(sizeof(*adr_u)); - adr_u->data = pac_u; - c3_d imp_y = pac_u->rut_u.imp_y; - c3_c* dns_c = pac_u->rut_u.dns_c; - c3_i sas_i; - - struct addrinfo hints; - memset(&hints, 0, sizeof(hints)); - hints.ai_family = AF_INET; // only IPv4 addresses - - if ( 0 != (sas_i = uv_getaddrinfo(u3L, adr_u, - _ames_czar_cb, - pac_u->rut_u.dns_c, 0, &hints)) ) - { - u3l_log("ames: %s", uv_strerror(sas_i)); - _ames_czar_gone(pac_u->sam_u, now, imp_y, dns_c); - _ames_pact_free(pac_u); - return; - } - } - } - } -} - /* _fine_get_cache(): get packet list or status from cache. RETAIN */ static u3_weak From 0bfc0ce328df8d7d5a2b6f858ac68f8929442ffd Mon Sep 17 00:00:00 2001 From: Joe Bryan Date: Thu, 18 Apr 2024 15:42:29 -0400 Subject: [PATCH 045/107] stun: refactors retry timer callback --- pkg/vere/io/ames.c | 32 +++++++++++++++----------------- 1 file changed, 15 insertions(+), 17 deletions(-) diff --git a/pkg/vere/io/ames.c b/pkg/vere/io/ames.c index 1c3421633d..304a4370ae 100644 --- a/pkg/vere/io/ames.c +++ b/pkg/vere/io/ames.c @@ -76,7 +76,6 @@ typedef enum u3_stun_state { uv_timer_t tim_u; // keepalive timer handle uv_timer_t dns_u; // DNS resolution timer handle c3_c* dns_c; // sponsoring galaxy fqdn - struct timeval las_u; // XX last sent date (not used?) struct timeval sar_u; // date we started trying to send u3_lane sef_u; // our lane, if we know it c3_o wok_o; // STUN worked, set on first success @@ -1263,38 +1262,41 @@ _stun_reset(uv_timer_t* tim_u) static void _stun_timer_cb(uv_timer_t* tim_u) { - c3_w rto = 500; - u3_ames* sam_u = (u3_ames*)(tim_u->data); + c3_w rto_w = 500; switch ( sam_u->sun_u.sat_y ) { case STUN_OFF: { // ignore; stray timer (although this shouldn't happen) u3l_log("stun: stray timer STUN_OFF"); } break; + case STUN_KEEPALIVE: { sam_u->sun_u.sat_y = STUN_TRYING; - sam_u->sun_u.tim_u.data = sam_u; gettimeofday(&sam_u->sun_u.sar_u, 0); // set start time to now - uv_timer_start(&sam_u->sun_u.tim_u, _stun_timer_cb, rto, 0); + uv_timer_start(&sam_u->sun_u.tim_u, _stun_timer_cb, rto_w, 0); _stun_send_request(sam_u); } break; + case STUN_TRYING: { c3_d gap_d = _stun_time_gap(sam_u->sun_u.sar_u); - c3_d nex_d = (gap_d * 2) + rto - gap_d; + c3_d nex_d = (gap_d * 2) + rto_w - gap_d; - if ( gap_d >= (39500) ) { + if ( gap_d >= 39500 ) { _stun_on_lost(sam_u); - } else if ( gap_d >= (31500) ) { + } + else { // wait ~s8 for the last STUN request - uv_timer_start(&sam_u->sun_u.tim_u, _stun_timer_cb, 8000 , 0); - _stun_send_request(sam_u); - } else { - uv_timer_start(&sam_u->sun_u.tim_u, _stun_timer_cb, - (nex_d >= 31500) ? 31500 : nex_d, 0); + // + // XX explain + // + c3_w tim_w = (gap_d >= 31500) ? 8000 : c3_max(nex_d, 31500); + + uv_timer_start(&sam_u->sun_u.tim_u, _stun_timer_cb, tim_w, 0); _stun_send_request(sam_u); } } break; + default: u3_assert(!"programmer error"); } } @@ -1325,10 +1327,6 @@ _stun_send_request_cb(uv_udp_send_t *req_u, c3_i sas_i) if ( sas_i ) { _stun_on_request_fail(sam_u, sas_i); } - else { - // XX curently not used - gettimeofday(&sam_u->sun_u.las_u, 0); // overwrite last sent date - } c3_free(snd_u); } From 40be1e6d682b87ac125627f8ac458337a1afdebe Mon Sep 17 00:00:00 2001 From: Joe Bryan Date: Thu, 18 Apr 2024 17:09:15 -0400 Subject: [PATCH 046/107] ames: adds consistent error printing for all udp packet sends --- pkg/vere/io/ames.c | 72 +++++++++++++++++++++------------------------- 1 file changed, 33 insertions(+), 39 deletions(-) diff --git a/pkg/vere/io/ames.c b/pkg/vere/io/ames.c index 304a4370ae..786f90147c 100644 --- a/pkg/vere/io/ames.c +++ b/pkg/vere/io/ames.c @@ -878,18 +878,17 @@ _ames_send_cb(uv_udp_send_t* req_u, c3_i sas_i) u3_pact* pac_u = (u3_pact*)req_u; u3_ames* sam_u = pac_u->sam_u; - if ( sas_i ) { - u3l_log("ames: send fail_async: %s", uv_strerror(sas_i)); - sam_u->fig_u.net_o = c3n; - } - else { + if ( !sas_i ) { sam_u->fig_u.net_o = c3y; } + else if ( c3y == sam_u->fig_u.net_o ) { + u3l_log("ames: send fail: %s", uv_strerror(sas_i)); + sam_u->fig_u.net_o = c3n; + } _ames_pact_free(pac_u); } -#define _fine_send _ames_send /* _ames_send(): send buffer to address on port. */ static void @@ -913,12 +912,8 @@ _ames_send(u3_pact* pac_u) add_u.sin_addr.s_addr = htonl(pac_u->rut_u.lan_u.pip_w); add_u.sin_port = htons(pac_u->rut_u.lan_u.por_s); - //u3l_log("_ames_send %s %u", _str_typ(pac_u->typ_y), - // pac_u->rut_u.lan_u.por_s); - { uv_buf_t buf_u = uv_buf_init((c3_c*)pac_u->hun_y, pac_u->len_w); - c3_i sas_i = uv_udp_send(&pac_u->snd_u, &sam_u->wax_u, &buf_u, 1, @@ -926,12 +921,7 @@ _ames_send(u3_pact* pac_u) _ames_send_cb); if ( sas_i ) { - if ( c3y == sam_u->fig_u.net_o ) { - u3l_log("ames: send fail_sync: %s", uv_strerror(sas_i)); - sam_u->fig_u.net_o = c3n; - } - - _ames_pact_free(pac_u); + _ames_send_cb(&pac_u->snd_u, sas_i); } } } @@ -1233,7 +1223,6 @@ static void _stun_send_request(u3_ames*); static void _stun_on_lost(u3_ames* sam_u); static void _stun_czar(u3_ames* sam_u, c3_d tim_d); static void _stun_resolve_dns_cb(uv_timer_t* tim_u); -static void _stun_send_request_cb(uv_udp_send_t *req_u, c3_i sas_i); static void _stun_on_failure(u3_ames* sam_u); static void _stun_start(u3_ames* sam_u, c3_o fail); static c3_y* _stun_add_fingerprint(c3_y *message, c3_w index); @@ -1307,25 +1296,26 @@ typedef struct _stun_send { c3_y hun_y[0]; // buffer } _stun_send; -static void -_stun_on_request_fail(u3_ames* sam_u, c3_i sas_i) -{ - u3l_log("stun: send callback fail_async: %s", uv_strerror(sas_i)); - - _stun_on_failure(sam_u); // %kick ping app - - sam_u->sun_u.sat_y = STUN_TRYING; - _stun_timer_cb(&sam_u->sun_u.tim_u); // retry sending the failed request -} - static void _stun_send_request_cb(uv_udp_send_t *req_u, c3_i sas_i) { _stun_send* snd_u = (_stun_send*)req_u; u3_ames* sam_u = snd_u->sam_u; + if ( !sas_i ) { + sam_u->fig_u.net_o = c3y; + } + else if ( c3y == sam_u->fig_u.net_o ) { + u3l_log("stun: send request fail: %s", uv_strerror(sas_i)); + sam_u->fig_u.net_o = c3n; + } + + // XX review/revise + // if ( sas_i ) { - _stun_on_request_fail(sam_u, sas_i); + _stun_on_failure(sam_u); // %kick ping app + sam_u->sun_u.sat_y = STUN_TRYING; + _stun_timer_cb(&sam_u->sun_u.tim_u); // retry sending the failed request } c3_free(snd_u); @@ -1335,9 +1325,14 @@ static void _stun_send_response_cb(uv_udp_send_t *rep_u, c3_i sas_i) { _stun_send* snd_u = (_stun_send*)rep_u; + u3_ames* sam_u = snd_u->sam_u; - if ( sas_i != 0 ) { - u3l_log("stun: _stun_send_response_cb fail_sync: %s", uv_strerror(sas_i)); + if ( !sas_i ) { + sam_u->fig_u.net_o = c3y; + } + else if ( c3y == sam_u->fig_u.net_o ) { + u3l_log("stun: send response fail: %s", uv_strerror(sas_i)); + sam_u->fig_u.net_o = c3n; } c3_free(snd_u); @@ -1394,9 +1389,8 @@ _stun_on_request(u3_ames* sam_u, c3_i sas_i = uv_udp_send(&snd_u->req_u, &sam_u->wax_u, &buf_u, 1, adr_u, _stun_send_response_cb); - if ( sas_i != 0 ) { - u3l_log("stun: send response fail_sync: %s", uv_strerror(sas_i)); - c3_free(snd_u); + if ( sas_i ) { + _stun_send_response_cb(&snd_u->req_u, sas_i); } } @@ -1515,12 +1509,12 @@ _stun_send_request(u3_ames* sam_u) add_u.sin_port = htons(sam_u->sun_u.lan_u.por_s); uv_buf_t buf_u = uv_buf_init((c3_c*)snd_u->hun_y, 28); - c3_i sas_i = uv_udp_send(&snd_u->req_u, &sam_u->wax_u, &buf_u, 1, - (const struct sockaddr*)&add_u, _stun_send_request_cb); + c3_i sas_i = uv_udp_send(&snd_u->req_u, &sam_u->wax_u, &buf_u, 1, + (const struct sockaddr*)&add_u, + _stun_send_request_cb); - if ( sas_i != 0) { - _stun_on_request_fail(sam_u, sas_i); - c3_free(snd_u); + if ( sas_i ) { + _stun_send_request_cb(&snd_u->req_u, sas_i); } } From b851465a0c9c14b87fae17f1eaa4768a4d97233a Mon Sep 17 00:00:00 2001 From: Joe Bryan Date: Thu, 18 Apr 2024 19:24:25 -0400 Subject: [PATCH 047/107] stun: removes galaxy resolution --- pkg/vere/io/ames.c | 221 ++++----------------------------------------- 1 file changed, 20 insertions(+), 201 deletions(-) diff --git a/pkg/vere/io/ames.c b/pkg/vere/io/ames.c index 786f90147c..84f29ebc1a 100644 --- a/pkg/vere/io/ames.c +++ b/pkg/vere/io/ames.c @@ -1104,72 +1104,6 @@ _ames_czar_lane(u3_ames* sam_u, c3_y imp_y, u3_lane* lan_u) return c3y; } -static c3_c* -_ames_czar_dns(c3_y imp_y, c3_c* czar_c) -{ - u3_noun nam = u3dc("scot", 'p', imp_y); - c3_c* nam_c = u3r_string(nam); - c3_w len_w = 3 + strlen(nam_c) + strlen(czar_c); - u3_assert(len_w <= 256); - c3_c* dns_c = c3_malloc(len_w); - - c3_i sas_i = snprintf(dns_c, len_w, "%s.%s.", nam_c + 1, czar_c); - u3_assert(sas_i <= 255); - - c3_free(nam_c); - u3z(nam); - - return dns_c; -} - -/* _ames_czar_gone(): galaxy address resolution failed. -*/ -static void -_ames_czar_gone(u3_ames* sam_u, time_t now, c3_d imp_y, c3_c* dns_c) -{ - if ( c3y == sam_u->imp_o[imp_y] ) { - u3l_log("ames: czar at %s: not found (b)", dns_c); - sam_u->imp_o[imp_y] = c3n; - } - - if ( (0 == sam_u->imp_w[imp_y]) || - (0xffffffff == sam_u->imp_w[imp_y]) ) - { - sam_u->imp_w[imp_y] = 0xffffffff; - } - - // keep existing ip for 5 more minutes - // - sam_u->imp_t[imp_y] = now; -} - -/* _stun_czar_here(): sponsor galaxy address resolution succeeded. -*/ -static c3_w -_stun_czar_here(u3_ames* sam_u, time_t now, struct sockaddr_in* add_u) -{ - c3_y imp_y = sam_u->sun_u.dad_y; - c3_w old_w = sam_u->imp_w[imp_y]; - c3_w pip_w = ntohl(add_u->sin_addr.s_addr); - - if ( pip_w != old_w ) { - u3_noun nam = u3dc("scot", c3__if, u3i_word(pip_w)); - c3_c* nam_c = u3r_string(nam); - - u3l_log("stun: czar %s ip %s", sam_u->sun_u.dns_c, nam_c); - - c3_free(nam_c); - u3z(nam); - } - sam_u->sun_u.lan_u.pip_w = pip_w; - - sam_u->imp_w[imp_y] = pip_w; - sam_u->imp_t[imp_y] = now; - sam_u->imp_o[imp_y] = c3y; - - return pip_w; -} - /* _fine_get_cache(): get packet list or status from cache. RETAIN */ static u3_weak @@ -1221,8 +1155,6 @@ _stun_stop(u3_ames* sam_u) // XX (code reordering?) forward declarations static void _stun_send_request(u3_ames*); static void _stun_on_lost(u3_ames* sam_u); -static void _stun_czar(u3_ames* sam_u, c3_d tim_d); -static void _stun_resolve_dns_cb(uv_timer_t* tim_u); static void _stun_on_failure(u3_ames* sam_u); static void _stun_start(u3_ames* sam_u, c3_o fail); static c3_y* _stun_add_fingerprint(c3_y *message, c3_w index); @@ -1261,13 +1193,24 @@ _stun_timer_cb(uv_timer_t* tim_u) } break; case STUN_KEEPALIVE: { - sam_u->sun_u.sat_y = STUN_TRYING; - gettimeofday(&sam_u->sun_u.sar_u, 0); // set start time to now - uv_timer_start(&sam_u->sun_u.tim_u, _stun_timer_cb, rto_w, 0); - _stun_send_request(sam_u); + u3_lane* lan_u = &(sam_u->sun_u.lan_u); + c3_y imp_y = sam_u->sun_u.dad_y; + + if ( c3n == _ames_czar_lane(sam_u, imp_y, lan_u) ) { + // XX how long + uv_timer_start(&sam_u->sun_u.tim_u, _stun_timer_cb, 25*1000, 0); + } + else { + sam_u->sun_u.sat_y = STUN_TRYING; + gettimeofday(&sam_u->sun_u.sar_u, 0); // set start time to now + uv_timer_start(&sam_u->sun_u.tim_u, _stun_timer_cb, rto_w, 0); + _stun_send_request(sam_u); + } } break; case STUN_TRYING: { + // XX re-lookup lane every time? + // c3_d gap_d = _stun_time_gap(sam_u->sun_u.sar_u); c3_d nex_d = (gap_d * 2) + rto_w - gap_d; @@ -1519,141 +1462,17 @@ _stun_send_request(u3_ames* sam_u) } static void -_stun_czar_cb(uv_getaddrinfo_t* adr_u, - c3_i sas_i, - struct addrinfo* aif_u) -{ - { - u3_ames* sam_u = (u3_ames*)(adr_u->data); - struct addrinfo* rai_u = aif_u; - time_t now = time(0); - - gettimeofday(&sam_u->sun_u.sar_u, 0); // set start time to now - - if (sas_i == 0) { - _stun_czar_here(sam_u, now, (struct sockaddr_in *)rai_u->ai_addr); - if (sam_u->sun_u.sat_y == STUN_OFF) { - sam_u->sun_u.sat_y = STUN_TRYING; - _stun_send_request(sam_u); - uv_timer_start(&sam_u->sun_u.tim_u, _stun_timer_cb, 500, 0); - } - // resolve DNS again in five minutes - uv_timer_start(&sam_u->sun_u.dns_u, _stun_resolve_dns_cb, 5*60*1000, 0); - } else { - u3l_log("stun: _stun_czar_cb request fail_sync: %s", uv_strerror(sas_i)); - _ames_czar_gone(sam_u, now, sam_u->sun_u.dad_y, sam_u->dns_c); - _stun_on_lost(sam_u); - } - } - c3_free(adr_u); - uv_freeaddrinfo(aif_u); -} - -static void -_stun_czar(u3_ames* sam_u, c3_d tim_d) +_stun_start(u3_ames* sam_u, c3_o fal_o) { - c3_d imp_y = sam_u->sun_u.dad_y; - sam_u->sun_u.lan_u.por_s = _ames_czar_port(imp_y); - - // Enable STUN using -L - // XX maybe enabled with a flag, for development? - if (c3n == u3_Host.ops_u.net) { - sam_u->sun_u.lan_u.pip_w = 0x7f000001; - sam_u->sun_u.sat_y = STUN_TRYING; - _stun_send_request(sam_u); - - gettimeofday(&sam_u->sun_u.sar_u, 0); - uv_timer_start(&sam_u->sun_u.tim_u, _stun_timer_cb, tim_d, 0); - - return; - } - - - // if we don't have a galaxy domain, no-op - // - if (!sam_u->dns_c) { - u3_noun nam = u3dc("scot", 'p', imp_y); - c3_c *nam_c = u3r_string(nam); - u3l_log("ames: no galaxy domain for %s, no-op", nam_c); - - c3_free(nam_c); - u3z(nam); - return; - } - - { - c3_w pip_w = sam_u->imp_w[imp_y]; - time_t wen = sam_u->imp_t[imp_y]; - time_t now = time(0); + c3_w tim_w = (c3n == fal_o) ? 0 : 39000; - // XX keep same as ames? - // backoff for 5 minutes after failed lookup - // - if ((now < wen) // time shenanigans! - || ((0xffffffff == pip_w) // sentinal ip address - && ((now - wen) < 300))) { - return; - } - // cached addresses have a 5 minute TTL - // - else if ((0 != pip_w) && ((now - wen) < 300)) { - sam_u->sun_u.sat_y = STUN_TRYING; - sam_u->sun_u.lan_u.pip_w = pip_w; - - _stun_send_request(sam_u); - - gettimeofday(&sam_u->sun_u.sar_u, 0); - uv_timer_start(&sam_u->sun_u.tim_u, _stun_timer_cb, tim_d, 0); - return; - } else { - // call callback right away first time we resolve the sponsor's DNS - sam_u->sun_u.dns_u.data = sam_u; - uv_timer_start(&sam_u->sun_u.dns_u, _stun_resolve_dns_cb, tim_d, 0); - } - } -} - -static void -_stun_start(u3_ames* sam_u, c3_o fail) -{ if ( ent_getentropy(sam_u->sun_u.tid_y, 12) ) { u3l_log("stun: getentropy fail: %s", strerror(errno)); - _stun_on_lost(sam_u); - } else { - _stun_czar(sam_u, (fail == c3n) ? 500 : 39500); + u3_king_bail(); } -} -static void -_stun_resolve_dns_cb(uv_timer_t* tim_u) -{ - u3_ames* sam_u = (u3_ames*)(tim_u->data); - c3_i sas_i; - - c3_y imp_y = sam_u->sun_u.dad_y; - sam_u->sun_u.lan_u.por_s = _ames_czar_port(imp_y); - - if ( !sam_u->sun_u.dns_c ) { - sam_u->sun_u.dns_c = _ames_czar_dns(imp_y, sam_u->dns_c); - } - - { - uv_getaddrinfo_t* adr_u = c3_malloc(sizeof(*adr_u)); - adr_u->data = sam_u; - - struct addrinfo hints; - memset(&hints, 0, sizeof(hints)); - hints.ai_family = AF_INET; // only IPv4 addresses - - if (0 != (sas_i = uv_getaddrinfo(u3L, adr_u, _stun_czar_cb, - sam_u->sun_u.dns_c, 0, &hints))) - { - u3l_log("stun: uv_getaddrinfo failed %s %s", uv_strerror(sas_i), sam_u->sun_u.dns_c); - _ames_czar_gone(sam_u, time(0), sam_u->sun_u.dad_y, sam_u->dns_c); - _stun_on_lost(sam_u); - return; - } - } + sam_u->sun_u.sat_y = STUN_KEEPALIVE; + uv_timer_start(&sam_u->sun_u.tim_u, _stun_timer_cb, tim_w, 0); } static c3_o From 7147c09c43c14c7d8bb86a309e229bab5d354b2c Mon Sep 17 00:00:00 2001 From: Joe Bryan Date: Thu, 18 Apr 2024 22:02:03 -0400 Subject: [PATCH 048/107] ames: remove artifacts of old-style dns resolution --- pkg/vere/io/ames.c | 49 ++++++++++++---------------------------------- 1 file changed, 13 insertions(+), 36 deletions(-) diff --git a/pkg/vere/io/ames.c b/pkg/vere/io/ames.c index 84f29ebc1a..ede99a1cea 100644 --- a/pkg/vere/io/ames.c +++ b/pkg/vere/io/ames.c @@ -53,13 +53,9 @@ typedef enum u3_stun_state { c3_l sev_l; // instance number ur_cue_test_t* tes_u; // cue-test handle u3_cue_xeno* sil_u; // cue handle - c3_c* dns_c; // domain XX multiple/fallback c3_y ver_y; // protocol version u3p(u3h_root) lax_p; // lane scry cache struct _u3_panc* pan_u; // outbound packet queue, backward - c3_w imp_w[256]; // imperial IPs - time_t imp_t[256]; // imperial IP timestamps - c3_o imp_o[256]; // imperial print status struct { // c3_c dom_c[251]; // domain c3_o dom_o; // have domain @@ -184,14 +180,10 @@ typedef enum u3_stun_state { struct _u3_ames* sam_u; // ames backpointer c3_w len_w; // length in bytes c3_y* hun_y; // packet buffer + u3_lane lan_u; // destination/origin lane u3_head hed_u; // head of packet u3_prel pre_u; // packet prelude u3_ptag typ_y; // packet type tag - struct { - u3_lane lan_u; // destination/origin lane - c3_y imp_y; // galaxy (optional) - c3_c* dns_c; // galaxy fqdn (optional) - } rut_u; union { u3_body bod_u; // tagged by PACT_AMES u3_wail wal_u; // tagged by PACT_WAIL @@ -329,7 +321,6 @@ _ames_pact_free(u3_pact* pac_u) u3_pier_bail(u3_king_stub()); } - c3_free(pac_u->rut_u.dns_c); c3_free(pac_u->hun_y); c3_free(pac_u); } @@ -899,7 +890,7 @@ _ames_send(u3_pact* pac_u) if ( !pac_u->hun_y || !sam_u || !pac_u->len_w - || !pac_u->rut_u.lan_u.por_s ) + || !pac_u->lan_u.por_s ) { u3l_log("ames: _ames_send null"); _ames_pact_free(pac_u); @@ -909,8 +900,8 @@ _ames_send(u3_pact* pac_u) memset(&add_u, 0, sizeof(add_u)); add_u.sin_family = AF_INET; - add_u.sin_addr.s_addr = htonl(pac_u->rut_u.lan_u.pip_w); - add_u.sin_port = htons(pac_u->rut_u.lan_u.por_s); + add_u.sin_addr.s_addr = htonl(pac_u->lan_u.pip_w); + add_u.sin_port = htons(pac_u->lan_u.por_s); { uv_buf_t buf_u = uv_buf_init((c3_c*)pac_u->hun_y, pac_u->len_w); @@ -1701,7 +1692,7 @@ _ames_ef_send(u3_ames* sam_u, u3_noun lan, u3_noun pac) if ( c3y == _ames_send_lane(sam_u, lan, &lan_u) ) { u3_pact* pac_u = c3_calloc(sizeof(*pac_u)); pac_u->sam_u = sam_u; - pac_u->rut_u.lan_u = lan_u; + pac_u->lan_u = lan_u; pac_u->len_w = u3r_met(3, pac); pac_u->hun_y = c3_malloc(pac_u->len_w); @@ -1831,13 +1822,13 @@ _ames_send_many(u3_pact* pac_u, u3_noun las, c3_o for_o) u3_noun rec = u3dc("scot", 'p', u3i_chubs(2, pac_u->pre_u.rec_d)); c3_c* sen_c = u3r_string(sen); c3_c* rec_c = u3r_string(rec); - c3_y* pip_y = (c3_y*)&pac_u->rut_u.lan_u.pip_w; + c3_y* pip_y = (c3_y*)&pac_u->lan_u.pip_w; //NOTE ip byte order assumes little-endian u3l_log("ames: forwarding for %s to %s from %d.%d.%d.%d:%d", sen_c, rec_c, pip_y[3], pip_y[2], pip_y[1], pip_y[0], - pac_u->rut_u.lan_u.por_s); + pac_u->lan_u.por_s); c3_free(sen_c); c3_free(rec_c); u3z(sen); u3z(rec); @@ -1897,7 +1888,7 @@ _ames_lane_scry_cb(void* vod_p, u3_noun nun) } _ames_put_packet(sam_u, _ames_pact_to_noun(pac_u), - pac_u->rut_u.lan_u); + pac_u->lan_u); } else { sam_u->sat_u.saw_d = 0; @@ -2174,7 +2165,7 @@ _fine_hear_request(u3_pact* req_u, c3_w cur_w) res_u = c3_calloc(sizeof(*res_u)); res_u->sam_u = req_u->sam_u; res_u->typ_y = PACT_PURR; - res_u->rut_u.lan_u = req_u->rut_u.lan_u; + res_u->lan_u = req_u->lan_u; // copy header, swapping sender and receiver // @@ -2217,14 +2208,6 @@ _fine_hear_request(u3_pact* req_u, c3_w cur_w) _ames_pact_free(req_u); } - // if receiver is a galaxy, note that in res_u - // - if ( res_u->pre_u.rec_d[0] < 256 - && res_u->pre_u.rec_d[1] == 0 ) - { - res_u->rut_u.imp_y = res_u->pre_u.rec_d[0]; - } - // look up request in scry cache // c3_w fra_w = res_u->pur_u.pep_u.fra_w; @@ -2293,7 +2276,7 @@ _fine_hear_response(u3_pact* pac_u, c3_w cur_w) { u3_noun wir = u3nc(c3__fine, u3_nul); u3_noun cad = u3nt(c3__hear, - u3nc(c3n, u3_ames_encode_lane(pac_u->rut_u.lan_u)), + u3nc(c3n, u3_ames_encode_lane(pac_u->lan_u)), u3i_bytes(pac_u->len_w, pac_u->hun_y)); u3_ovum* ovo_u = u3_ovum_init(0, c3__ames, wir, cad); @@ -2316,7 +2299,7 @@ _ames_hear_ames(u3_pact* pac_u, c3_w cur_w) { u3_noun msg = u3i_bytes(pac_u->len_w, pac_u->hun_y); - _ames_put_packet(pac_u->sam_u, msg, pac_u->rut_u.lan_u); + _ames_put_packet(pac_u->sam_u, msg, pac_u->lan_u); _ames_pact_free(pac_u); } } @@ -2336,7 +2319,7 @@ _ames_try_forward(u3_pact* pac_u) c3_w old_w, cur_w; pac_u->hed_u.rel_o = c3y; - pac_u->pre_u.rog_d = u3_ames_lane_to_chub(pac_u->rut_u.lan_u); + pac_u->pre_u.rog_d = u3_ames_lane_to_chub(pac_u->lan_u); old_w = pac_u->len_w; old_y = pac_u->hun_y; @@ -2403,7 +2386,7 @@ _ames_hear(u3_ames* sam_u, pac_u->sam_u = sam_u; pac_u->len_w = len_w; pac_u->hun_y = hun_y; - pac_u->rut_u.lan_u = *lan_u; + pac_u->lan_u = *lan_u; cur_w = 0; // parse the header @@ -2829,12 +2812,6 @@ _ames_ef_turf(u3_ames* sam_u, u3_noun tuf) memset(dom_c + len_w, 0, sizeof(dom_c) - len_w); if ( 0 != memcmp(sam_u->zar_u.dom_c, dom_c, sizeof(dom_c)) ) { - // XX legacy - // - sam_u->dns_c = c3_malloc(1 + len_w); - memcpy(sam_u->dns_c, dom_c, len_w); - sam_u->dns_c[len_w] = 0; - // XX review // c3_free(sam_u->sun_u.dns_c); From 199cd7fc9861ee8dfae12c6d1309e99b2675f754 Mon Sep 17 00:00:00 2001 From: Joe Bryan Date: Thu, 18 Apr 2024 23:15:37 -0400 Subject: [PATCH 049/107] stun: refactor state machine --- pkg/vere/io/ames.c | 119 ++++++++++----------------------------------- 1 file changed, 27 insertions(+), 92 deletions(-) diff --git a/pkg/vere/io/ames.c b/pkg/vere/io/ames.c index ede99a1cea..ee86d67066 100644 --- a/pkg/vere/io/ames.c +++ b/pkg/vere/io/ames.c @@ -70,8 +70,6 @@ typedef enum u3_stun_state { c3_y dad_y; // sponsoring galaxy u3_lane lan_u; // sponsoring galaxy IP and port uv_timer_t tim_u; // keepalive timer handle - uv_timer_t dns_u; // DNS resolution timer handle - c3_c* dns_c; // sponsoring galaxy fqdn struct timeval sar_u; // date we started trying to send u3_lane sef_u; // our lane, if we know it c3_o wok_o; // STUN worked, set on first success @@ -1128,26 +1126,10 @@ _fine_put_cache(u3_ames* sam_u, u3_noun pax, c3_w lop_w, u3_noun lis) } } -static void -_stun_stop(u3_ames* sam_u) -{ - switch ( sam_u->sun_u.sat_y ) { - case STUN_OFF: break; // ignore; already stopped - case STUN_TRYING: - case STUN_KEEPALIVE: { - uv_timer_stop(&sam_u->sun_u.tim_u); - uv_timer_stop(&sam_u->sun_u.dns_u); - } break; - default: u3_assert(!"programmer error"); - } - sam_u->sun_u.sat_y = STUN_OFF; -} - // XX (code reordering?) forward declarations static void _stun_send_request(u3_ames*); static void _stun_on_lost(u3_ames* sam_u); -static void _stun_on_failure(u3_ames* sam_u); -static void _stun_start(u3_ames* sam_u, c3_o fail); +static void _stun_start(u3_ames* sam_u, c3_w tim_w); static c3_y* _stun_add_fingerprint(c3_y *message, c3_w index); static c3_o _stun_find_xor_mapped_address(c3_y* buf_y, c3_w buf_len, u3_lane* lan_u); @@ -1168,7 +1150,7 @@ _stun_reset(uv_timer_t* tim_u) { u3_ames* sam_u = (u3_ames*)(tim_u->data); - _stun_start(sam_u, c3y); + _stun_start(sam_u, 39000); } static void @@ -1231,32 +1213,7 @@ typedef struct _stun_send { } _stun_send; static void -_stun_send_request_cb(uv_udp_send_t *req_u, c3_i sas_i) -{ - _stun_send* snd_u = (_stun_send*)req_u; - u3_ames* sam_u = snd_u->sam_u; - - if ( !sas_i ) { - sam_u->fig_u.net_o = c3y; - } - else if ( c3y == sam_u->fig_u.net_o ) { - u3l_log("stun: send request fail: %s", uv_strerror(sas_i)); - sam_u->fig_u.net_o = c3n; - } - - // XX review/revise - // - if ( sas_i ) { - _stun_on_failure(sam_u); // %kick ping app - sam_u->sun_u.sat_y = STUN_TRYING; - _stun_timer_cb(&sam_u->sun_u.tim_u); // retry sending the failed request - } - - c3_free(snd_u); -} - -static void -_stun_send_response_cb(uv_udp_send_t *rep_u, c3_i sas_i) +_stun_send_cb(uv_udp_send_t *rep_u, c3_i sas_i) { _stun_send* snd_u = (_stun_send*)rep_u; u3_ames* sam_u = snd_u->sam_u; @@ -1321,10 +1278,10 @@ _stun_on_request(u3_ames* sam_u, uv_buf_t buf_u = uv_buf_init((c3_c*)snd_u->hun_y, 40); c3_i sas_i = uv_udp_send(&snd_u->req_u, &sam_u->wax_u, - &buf_u, 1, adr_u, _stun_send_response_cb); + &buf_u, 1, adr_u, _stun_send_cb); if ( sas_i ) { - _stun_send_response_cb(&snd_u->req_u, sas_i); + _stun_send_cb(&snd_u->req_u, sas_i); } } @@ -1360,47 +1317,37 @@ _stun_on_response(u3_ames* sam_u, c3_y* buf_y, c3_w buf_len) sam_u->sun_u.sef_u = lan_u; + // XX should no-op early + // switch ( sam_u->sun_u.sat_y ) { case STUN_OFF: break; // ignore; stray response case STUN_KEEPALIVE: break; // ignore; duplicate response case STUN_TRYING: { - sam_u->sun_u.sat_y = STUN_KEEPALIVE; - if ( ent_getentropy(sam_u->sun_u.tid_y, 12) ) { - u3l_log("stun: getentropy fail: %s", strerror(errno)); - _stun_on_lost(sam_u); - } - else { - uv_timer_start(&sam_u->sun_u.tim_u, _stun_timer_cb, 25*1000, 0); - } + _stun_start(sam_u, 25000); } break; - default: assert("programmer error"); + default: u3_assert(!"programmer error"); } } static void -_stun_on_failure(u3_ames* sam_u) +_stun_on_lost(u3_ames* sam_u) { - // only inject event into arvo to %kick ping app on first failure - if (sam_u->sun_u.wok_o == c3y) { + sam_u->sun_u.sat_y = STUN_OFF; + + // only inject event into arvo to %kick ping app on first failure + // + if ( c3y == sam_u->sun_u.wok_o ) { u3_noun wir = u3nc(c3__ames, u3_nul); u3_noun cad = u3nq(c3__stun, c3__fail, sam_u->sun_u.dad_y, u3nc(c3n, u3_ames_encode_lane(sam_u->sun_u.sef_u))); - u3_ovum *ovo_u = u3_ovum_init(0, c3__ames, wir, cad); - u3_auto_plan(&sam_u->car_u, ovo_u); + u3_auto_plan(&sam_u->car_u, + u3_ovum_init(0, c3__ames, wir, cad)); + sam_u->sun_u.wok_o = c3n; } - sam_u->sun_u.wok_o = c3n; -} -static void -_stun_on_lost(u3_ames* sam_u) -{ - _stun_stop(sam_u); - _stun_on_failure(sam_u); - // resolve DNS again, and (re)start STUN - // XX call _stun_start(sam_u, c3y) directly? - uv_timer_start(&sam_u->sun_u.dns_u, _stun_reset, 5*1000, 0); + uv_timer_start(&sam_u->sun_u.tim_u, _stun_reset, 5*1000, 0); } static void @@ -1444,19 +1391,16 @@ _stun_send_request(u3_ames* sam_u) uv_buf_t buf_u = uv_buf_init((c3_c*)snd_u->hun_y, 28); c3_i sas_i = uv_udp_send(&snd_u->req_u, &sam_u->wax_u, &buf_u, 1, - (const struct sockaddr*)&add_u, - _stun_send_request_cb); + (const struct sockaddr*)&add_u, _stun_send_cb); if ( sas_i ) { - _stun_send_request_cb(&snd_u->req_u, sas_i); + _stun_send_cb(&snd_u->req_u, sas_i); } } static void -_stun_start(u3_ames* sam_u, c3_o fal_o) +_stun_start(u3_ames* sam_u, c3_w tim_w) { - c3_w tim_w = (c3n == fal_o) ? 0 : 39000; - if ( ent_getentropy(sam_u->sun_u.tid_y, 12) ) { u3l_log("stun: getentropy fail: %s", strerror(errno)); u3_king_bail(); @@ -1610,12 +1554,11 @@ _ames_ef_saxo(u3_ames* sam_u, u3_noun zad) dad = u3h(daz); u3_noun our = u3i_chubs(2, sam_u->pir_u->who_d); + // if we are a galaxy, don't STUN + // if ( c3y == _ames_is_czar(dad) && c3n == _ames_is_czar(our)) { - // if we are a galaxy, don't STUN - sam_u->sun_u.dad_y = u3r_byte(0, dad); - sam_u->sun_u.wok_o = c3n; - _stun_stop(sam_u); - _stun_start(sam_u, c3n); + sam_u->sun_u.dad_y = (c3_y)dad; + _stun_start(sam_u, 0); } u3z(zad); u3z(daz); u3z(our); @@ -2812,11 +2755,6 @@ _ames_ef_turf(u3_ames* sam_u, u3_noun tuf) memset(dom_c + len_w, 0, sizeof(dom_c) - len_w); if ( 0 != memcmp(sam_u->zar_u.dom_c, dom_c, sizeof(dom_c)) ) { - // XX review - // - c3_free(sam_u->sun_u.dns_c); - sam_u->sun_u.dns_c = 0; - memcpy(sam_u->zar_u.dom_c, dom_c, sizeof(dom_c)); memset(sam_u->zar_u.pip_w, 0, sizeof(sam_u->zar_u.pip_w)); sam_u->zar_u.dom_o = c3y; @@ -3039,7 +2977,6 @@ _ames_io_exit(u3_auto* car_u) { u3_ames* sam_u = (u3_ames*)car_u; uv_close(&sam_u->had_u, _ames_exit_cb); - uv_close((uv_handle_t*)&sam_u->sun_u.dns_u, 0); uv_close((uv_handle_t*)&sam_u->sun_u.tim_u, 0); } @@ -3136,15 +3073,13 @@ u3_ames_io_init(u3_pier* pir_u) sam_u->fig_u.net_o = c3y; sam_u->fig_u.see_o = c3y; sam_u->fig_u.fit_o = c3n; + sam_u->sun_u.wok_o = c3n; uv_timer_init(u3L, &sam_u->zar_u.tim_u); sam_u->zar_u.tim_u.data = sam_u; - // initialize STUN timers - uv_timer_init(u3L, &sam_u->sun_u.dns_u); uv_timer_init(u3L, &sam_u->sun_u.tim_u); sam_u->sun_u.tim_u.data = sam_u; - sam_u->sun_u.dns_u.data = sam_u; // enable forwarding on galaxies only u3_noun who = u3i_chubs(2, sam_u->pir_u->who_d); From ebf6913bd1287bd8d3c23470ab13193a331ea32b Mon Sep 17 00:00:00 2001 From: Joe Bryan Date: Thu, 18 Apr 2024 23:25:39 -0400 Subject: [PATCH 050/107] ames: renames new czar dns functions --- pkg/vere/io/ames.c | 26 +++++++++++++------------- 1 file changed, 13 insertions(+), 13 deletions(-) diff --git a/pkg/vere/io/ames.c b/pkg/vere/io/ames.c index ee86d67066..739deed47a 100644 --- a/pkg/vere/io/ames.c +++ b/pkg/vere/io/ames.c @@ -2611,10 +2611,10 @@ typedef struct _czar_resv { c3_y imp_y; } _czar_resv; -/* _ames_czar2_gone(): galaxy address resolution failed. +/* _ames_czar_gone(): galaxy address resolution failed. */ static void -_ames_czar2_gone(u3_ames* sam_u, c3_y imp_y) +_ames_czar_gone(u3_ames* sam_u, c3_y imp_y) { c3_w old_w = sam_u->zar_u.pip_w[imp_y]; @@ -2623,10 +2623,10 @@ _ames_czar2_gone(u3_ames* sam_u, c3_y imp_y) } } -/* _ames_czar2_here(): galaxy address resolution succeeded. +/* _ames_czar_here(): galaxy address resolution succeeded. */ static void -_ames_czar2_here(u3_ames* sam_u, c3_y imp_y, c3_w pip_w) +_ames_czar_here(u3_ames* sam_u, c3_y imp_y, c3_w pip_w) { c3_w old_w = sam_u->zar_u.pip_w[imp_y]; @@ -2650,10 +2650,10 @@ _ames_czar2_here(u3_ames* sam_u, c3_y imp_y, c3_w pip_w) } } -/* _ames_czar2_cb(): galaxy address resolution callback. +/* _ames_czar_cb(): galaxy address resolution callback. */ static void -_ames_czar2_cb(uv_getaddrinfo_t* adr_u, +_ames_czar_cb(uv_getaddrinfo_t* adr_u, c3_i sas_i, struct addrinfo* aif_u) { @@ -2669,7 +2669,7 @@ _ames_czar2_cb(uv_getaddrinfo_t* adr_u, if ( rai_u && rai_u->ai_addr ) { struct sockaddr_in* add_u = (void*)rai_u->ai_addr; c3_w pip_w = ntohl(add_u->sin_addr.s_addr); - _ames_czar2_here(sam_u, imp_y, pip_w); + _ames_czar_here(sam_u, imp_y, pip_w); } else { if ( !sas_i ) { @@ -2680,17 +2680,17 @@ _ames_czar2_cb(uv_getaddrinfo_t* adr_u, u3l_log("ames: czar fail: %s", uv_strerror(sas_i)); } - _ames_czar2_gone(sam_u, imp_y); + _ames_czar_gone(sam_u, imp_y); } uv_freeaddrinfo(aif_u); c3_free(res_u); } -/* _ames_czar2(): single galaxy address resolution. +/* _ames_czar(): single galaxy address resolution. */ static void -_ames_czar2(u3_ames* sam_u, const c3_c* dom_c, c3_y imp_y) +_ames_czar(u3_ames* sam_u, const c3_c* dom_c, c3_y imp_y) { struct addrinfo hin_u = { .ai_family = AF_INET }; uv_getaddrinfo_t* adr_u; @@ -2705,10 +2705,10 @@ _ames_czar2(u3_ames* sam_u, const c3_c* dom_c, c3_y imp_y) res_u->imp_y = imp_y; adr_u = &(res_u->adr_u); - sas_i = uv_getaddrinfo(u3L, adr_u, _ames_czar2_cb, dns_c, 0, &hin_u); + sas_i = uv_getaddrinfo(u3L, adr_u, _ames_czar_cb, dns_c, 0, &hin_u); if ( sas_i ) { - _ames_czar2_cb(adr_u, sas_i, NULL); + _ames_czar_cb(adr_u, sas_i, NULL); } } @@ -2728,7 +2728,7 @@ _ames_czar_all(uv_timer_t* tim_u) sam_u->zar_u.pen_s = 256; for ( c3_w i_w; i_w < 256; i_w++ ) { - _ames_czar2(sam_u, sam_u->zar_u.dom_c, (c3_y)i_w); + _ames_czar(sam_u, sam_u->zar_u.dom_c, (c3_y)i_w); } uv_timer_start(&sam_u->zar_u.tim_u, _ames_czar_all, 300*1000, 0); From da98cc6bab901ea85d0b9480e5ab997288cc9f9e Mon Sep 17 00:00:00 2001 From: Joe Bryan Date: Thu, 18 Apr 2024 23:34:33 -0400 Subject: [PATCH 051/107] ames: fix empty %turf effect error message condition --- pkg/vere/io/ames.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/pkg/vere/io/ames.c b/pkg/vere/io/ames.c index 739deed47a..c8a3e9d491 100644 --- a/pkg/vere/io/ames.c +++ b/pkg/vere/io/ames.c @@ -2774,7 +2774,7 @@ _ames_ef_turf(u3_ames* sam_u, u3_noun tuf) u3z(tuf); } - else if ( (c3n == sam_u->pir_u->fak_o) && (c3y == sam_u->zar_u.dom_o) ) { + else if ( (c3n == sam_u->pir_u->fak_o) && (c3n == sam_u->zar_u.dom_o) ) { u3l_log("ames: turf: no domains"); } @@ -3074,6 +3074,7 @@ u3_ames_io_init(u3_pier* pir_u) sam_u->fig_u.see_o = c3y; sam_u->fig_u.fit_o = c3n; sam_u->sun_u.wok_o = c3n; + sam_u->zar_u.dom_o = c3n; uv_timer_init(u3L, &sam_u->zar_u.tim_u); sam_u->zar_u.tim_u.data = sam_u; From 6aae1589a46d7b207d28cd46643cf907ed98a276 Mon Sep 17 00:00:00 2001 From: Joe Bryan Date: Mon, 22 Apr 2024 11:13:48 -0400 Subject: [PATCH 052/107] stun: cleanup comments --- pkg/vere/io/ames.c | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/pkg/vere/io/ames.c b/pkg/vere/io/ames.c index c8a3e9d491..b8f491137e 100644 --- a/pkg/vere/io/ames.c +++ b/pkg/vere/io/ames.c @@ -1170,7 +1170,6 @@ _stun_timer_cb(uv_timer_t* tim_u) c3_y imp_y = sam_u->sun_u.dad_y; if ( c3n == _ames_czar_lane(sam_u, imp_y, lan_u) ) { - // XX how long uv_timer_start(&sam_u->sun_u.tim_u, _stun_timer_cb, 25*1000, 0); } else { @@ -1182,8 +1181,6 @@ _stun_timer_cb(uv_timer_t* tim_u) } break; case STUN_TRYING: { - // XX re-lookup lane every time? - // c3_d gap_d = _stun_time_gap(sam_u->sun_u.sar_u); c3_d nex_d = (gap_d * 2) + rto_w - gap_d; @@ -1193,7 +1190,7 @@ _stun_timer_cb(uv_timer_t* tim_u) else { // wait ~s8 for the last STUN request // - // XX explain + // https://datatracker.ietf.org/doc/html/rfc5389#section-7.2.1 // c3_w tim_w = (gap_d >= 31500) ? 8000 : c3_max(nex_d, 31500); From 27e5c33304e60d822a2a1b1cbf5352273bcc8fd4 Mon Sep 17 00:00:00 2001 From: pkova Date: Mon, 22 Apr 2024 19:56:20 +0300 Subject: [PATCH 053/107] ames: address review comments for natpmp --- pkg/vere/io/ames.c | 27 +++++++++++++++++---------- 1 file changed, 17 insertions(+), 10 deletions(-) diff --git a/pkg/vere/io/ames.c b/pkg/vere/io/ames.c index 3ce9cc4f90..341f65d23c 100644 --- a/pkg/vere/io/ames.c +++ b/pkg/vere/io/ames.c @@ -2760,7 +2760,11 @@ _ames_recv_cb(uv_udp_t* wax_u, static void natpmp_init(uv_timer_t* handle); -static void natpmp_cb(uv_poll_t* handle, int status, int events) { +static void +natpmp_cb(uv_poll_t* handle, + int status, + int events) +{ u3_ames* sam_u = handle->data; natpmpresp_t response; @@ -2768,13 +2772,14 @@ static void natpmp_cb(uv_poll_t* handle, int status, int events) { if ( NATPMP_TRYAGAIN == r ) { return; } + + uv_poll_stop(handle); + if ( 0 != r ) { u3l_log("ames: natpmp error %i", r); - uv_poll_stop(handle); closenatpmp(&sam_u->nat_u.req_u); return; } - uv_poll_stop(handle); u3l_log("ames: mapped public port %hu to localport %hu lifetime %u", response.pnu.newportmapping.mappedpublicport, @@ -2783,20 +2788,18 @@ static void natpmp_cb(uv_poll_t* handle, int status, int events) { closenatpmp(&sam_u->nat_u.req_u); sam_u->nat_u.tim_u.data = sam_u; - uv_timer_init(u3L, &sam_u->nat_u.tim_u); uv_timer_start(&sam_u->nat_u.tim_u, natpmp_init, 7200000, 0); } -static void natpmp_init(uv_timer_t* handle) { - +static void +natpmp_init(uv_timer_t *handle) +{ u3_ames* sam_u = handle->data; c3_s por_s = sam_u->pir_u->por_s; - initnatpmp(&sam_u->nat_u.req_u, 0, 0); sendnewportmappingrequest(&sam_u->nat_u.req_u, NATPMP_PROTOCOL_UDP, por_s, por_s, 7200); sam_u->nat_u.pol_u.data = sam_u; - uv_poll_init(u3L, &sam_u->nat_u.pol_u, sam_u->nat_u.req_u.s); uv_poll_start(&sam_u->nat_u.pol_u, UV_READABLE, natpmp_cb); } @@ -2922,8 +2925,6 @@ _ames_io_start(u3_ames* sam_u) mdns_init(por_s, !sam_u->pir_u->fak_o, our_s, _ames_put_dear, (void *)sam_u); if ( c3n == sam_u->pir_u->fak_o ) { - sam_u->nat_u.tim_u.data = sam_u; - uv_timer_init(u3L, &sam_u->nat_u.tim_u); uv_timer_start(&sam_u->nat_u.tim_u, natpmp_init, 0, 0); } @@ -3270,6 +3271,12 @@ u3_ames_io_init(u3_pier* pir_u) sam_u->sun_u.tim_u.data = sam_u; sam_u->sun_u.dns_u.data = sam_u; + // initialize libnatpmp + sam_u->nat_u.tim_u.data = sam_u; + initnatpmp(&sam_u->nat_u.req_u, 0, 0); + uv_timer_init(u3L, &sam_u->nat_u.tim_u); + uv_poll_init(u3L, &sam_u->nat_u.pol_u, sam_u->nat_u.req_u.s); + // enable forwarding on galaxies only u3_noun who = u3i_chubs(2, sam_u->pir_u->who_d); u3_noun rac = u3do("clan:title", who); From 68429f65b96b67d042a5aebbbdf9a8fa41c620b0 Mon Sep 17 00:00:00 2001 From: pkova Date: Tue, 23 Apr 2024 13:42:20 +0300 Subject: [PATCH 054/107] ames: address more libnatpmp nits --- pkg/vere/io/ames.c | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/pkg/vere/io/ames.c b/pkg/vere/io/ames.c index 341f65d23c..b1e473189c 100644 --- a/pkg/vere/io/ames.c +++ b/pkg/vere/io/ames.c @@ -2762,21 +2762,21 @@ static void natpmp_init(uv_timer_t* handle); static void natpmp_cb(uv_poll_t* handle, - int status, - int events) + c3_i status, + c3_i events) { u3_ames* sam_u = handle->data; natpmpresp_t response; - c3_w r = readnatpmpresponseorretry(&sam_u->nat_u.req_u, &response); - if ( NATPMP_TRYAGAIN == r ) { + c3_i res_i = readnatpmpresponseorretry(&sam_u->nat_u.req_u, &response); + if ( NATPMP_TRYAGAIN == res_i ) { return; } uv_poll_stop(handle); - if ( 0 != r ) { - u3l_log("ames: natpmp error %i", r); + if ( 0 != res_i ) { + u3l_log("ames: natpmp error %i", res_i); closenatpmp(&sam_u->nat_u.req_u); return; } From 56fdbe9bf709939b04ec8ff96c1e49a3af9cc819 Mon Sep 17 00:00:00 2001 From: Joe Bryan Date: Tue, 23 Apr 2024 09:05:36 -0400 Subject: [PATCH 055/107] stun: add working flag to ames_info output --- pkg/vere/io/ames.c | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/pkg/vere/io/ames.c b/pkg/vere/io/ames.c index b8f491137e..c88b075168 100644 --- a/pkg/vere/io/ames.c +++ b/pkg/vere/io/ames.c @@ -2995,6 +2995,7 @@ _ames_io_info(u3_auto* car_u) u3_pier_mase("filtering", sam_u->fig_u.fit_o), u3_pier_mase("can-send", sam_u->fig_u.net_o), u3_pier_mase("can-scry", sam_u->fig_u.see_o), + u3_pier_mase("stun-working", sam_u->sun_u.wok_o), u3_pier_mase("scry-cache", u3i_word(u3h_wyt(sam_u->fin_s.sac_p))), u3_pier_mase("scry-cache-size", u3i_word(sac_w)), u3_pier_mase("lane-cache", u3i_word(u3h_wyt(sam_u->lax_p))), @@ -3039,9 +3040,11 @@ _ames_io_slog(u3_auto* car_u) u3l_log(" filtering: %s", FLAG(sam_u->fig_u.fit_o)); u3l_log(" can send: %s", FLAG(sam_u->fig_u.net_o)); u3l_log(" can scry: %s", FLAG(sam_u->fig_u.see_o)); - u3l_log(" caches:"); - u3l_log(" cached lanes: %u, %u B", u3h_wyt(sam_u->lax_p), lax_w); - u3l_log(" cached meows: %u, %u B", u3h_wyt(sam_u->fin_s.sac_p), sac_w); + u3l_log(" stun:"); + u3l_log(" working: %s", FLAG(sam_u->sun_u.wok_o)); + u3l_log(" caches:"); + u3l_log(" cached lanes: %u, %u B", u3h_wyt(sam_u->lax_p), lax_w); + u3l_log(" cached meows: %u, %u B", u3h_wyt(sam_u->fin_s.sac_p), sac_w); u3l_log(" counters:"); u3l_log(" dropped: %" PRIu64, sam_u->sat_u.dop_d); u3l_log(" forwards dropped: %" PRIu64, sam_u->sat_u.fod_d); From 46d20eaa0300312ea4a8d86f2544e378f3ce81fd Mon Sep 17 00:00:00 2001 From: Tinnus Napbus Date: Thu, 25 Apr 2024 03:44:51 +1200 Subject: [PATCH 056/107] events: add docs link to no-chop printf --- pkg/vere/disk.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/pkg/vere/disk.c b/pkg/vere/disk.c index 319403566f..b60253fb1c 100644 --- a/pkg/vere/disk.c +++ b/pkg/vere/disk.c @@ -1632,7 +1632,9 @@ u3_disk_chop(u3_disk* log_u, c3_d eve_d) u3_disk_epoc_list(log_u, sot_d); if ( len_z <= 2 ) { - fprintf(stderr, "chop: nothing to do, try running roll first\r\n"); + fprintf(stderr, "chop: nothing to do, try running roll first\r\n" + "chop: for more info see " + "https://docs.urbit.org/manual/running/vere#chop\r\n"); exit(0); // enjoy } From 175c2d1a8d931e47ab9b5b4683419aaa7174b2b9 Mon Sep 17 00:00:00 2001 From: Pyry Kovanen Date: Wed, 1 May 2024 19:50:20 +0300 Subject: [PATCH 057/107] disk: fix broken chop error message --- pkg/vere/disk.c | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/pkg/vere/disk.c b/pkg/vere/disk.c index b60253fb1c..4576f0662f 100644 --- a/pkg/vere/disk.c +++ b/pkg/vere/disk.c @@ -1632,9 +1632,7 @@ u3_disk_chop(u3_disk* log_u, c3_d eve_d) u3_disk_epoc_list(log_u, sot_d); if ( len_z <= 2 ) { - fprintf(stderr, "chop: nothing to do, try running roll first\r\n" - "chop: for more info see " - "https://docs.urbit.org/manual/running/vere#chop\r\n"); + fprintf(stderr, "chop: nothing to do, try running roll first\r\nchop: for more info see https://docs.urbit.org/manual/running/vere#chop\r\n"); exit(0); // enjoy } From 7323954d798f7f3cefe1228ee0b77820398526c7 Mon Sep 17 00:00:00 2001 From: Pyry Kovanen Date: Wed, 1 May 2024 21:09:39 +0300 Subject: [PATCH 058/107] Revert "disk: fix broken chop error message" --- pkg/vere/disk.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/pkg/vere/disk.c b/pkg/vere/disk.c index 4576f0662f..b60253fb1c 100644 --- a/pkg/vere/disk.c +++ b/pkg/vere/disk.c @@ -1632,7 +1632,9 @@ u3_disk_chop(u3_disk* log_u, c3_d eve_d) u3_disk_epoc_list(log_u, sot_d); if ( len_z <= 2 ) { - fprintf(stderr, "chop: nothing to do, try running roll first\r\nchop: for more info see https://docs.urbit.org/manual/running/vere#chop\r\n"); + fprintf(stderr, "chop: nothing to do, try running roll first\r\n" + "chop: for more info see " + "https://docs.urbit.org/manual/running/vere#chop\r\n"); exit(0); // enjoy } From 385bdf9b852ed994e1a8efb5118a9594db15354f Mon Sep 17 00:00:00 2001 From: Pyry Kovanen Date: Wed, 1 May 2024 21:54:36 +0300 Subject: [PATCH 059/107] Revert "ames: add libnatpmp for automatic port forwarding" --- WORKSPACE.bazel | 9 ---- bazel/third_party/natpmp/BUILD.bazel | 0 bazel/third_party/natpmp/natpmp.BUILD | 8 ---- pkg/vere/BUILD.bazel | 1 - pkg/vere/io/ames.c | 62 --------------------------- 5 files changed, 80 deletions(-) delete mode 100644 bazel/third_party/natpmp/BUILD.bazel delete mode 100644 bazel/third_party/natpmp/natpmp.BUILD diff --git a/WORKSPACE.bazel b/WORKSPACE.bazel index dca6750619..39ddfe03e9 100644 --- a/WORKSPACE.bazel +++ b/WORKSPACE.bazel @@ -217,15 +217,6 @@ versioned_http_file( version = "721fa05", ) -versioned_http_archive( - name = "natpmp", - build_file = "//bazel/third_party/natpmp:natpmp.BUILD", - sha256 = "0684ed2c8406437e7519a1bd20ea83780db871b3a3a5d752311ba3e889dbfc70", - strip_prefix = "libnatpmp-{version}", - url = "http://miniupnp.free.fr/files/libnatpmp-{version}.tar.gz", - version = "20230423", -) - versioned_http_file( name = "solid_pill", sha256 = "8b658fcee6978e2b19004a54233cab953e77ea0bb6c3a04d1bfda4ddc6be63c5", diff --git a/bazel/third_party/natpmp/BUILD.bazel b/bazel/third_party/natpmp/BUILD.bazel deleted file mode 100644 index e69de29bb2..0000000000 diff --git a/bazel/third_party/natpmp/natpmp.BUILD b/bazel/third_party/natpmp/natpmp.BUILD deleted file mode 100644 index b3a7edd7e0..0000000000 --- a/bazel/third_party/natpmp/natpmp.BUILD +++ /dev/null @@ -1,8 +0,0 @@ -cc_library( - name = "natpmp", - srcs = ["natpmp.c", "getgateway.c"], - hdrs = ["natpmp.h", "getgateway.h", "natpmp_declspec.h"], - copts = ["-O3"], - linkstatic = True, - visibility = ["//visibility:public"], -) diff --git a/pkg/vere/BUILD.bazel b/pkg/vere/BUILD.bazel index 6303b87201..e736d0e8e1 100644 --- a/pkg/vere/BUILD.bazel +++ b/pkg/vere/BUILD.bazel @@ -140,7 +140,6 @@ vere_library( "@lmdb", "@openssl", "@uv", - "@natpmp", ] + select({ "@platforms//os:macos": [], "@platforms//os:linux": [ diff --git a/pkg/vere/io/ames.c b/pkg/vere/io/ames.c index a92a38d019..7cd462b7d6 100644 --- a/pkg/vere/io/ames.c +++ b/pkg/vere/io/ames.c @@ -7,7 +7,6 @@ #include "ur.h" #include "zlib.h" -#include "natpmp.h" #include @@ -72,11 +71,6 @@ typedef enum u3_stun_state { u3_lane sef_u; // our lane, if we know it c3_o wok_o; // STUN worked, set on first success } sun_u; // - struct { - natpmp_t req_u; // libnatpmp struct for mapping request - uv_poll_t pol_u; // handle waits on libnatpmp socket - uv_timer_t tim_u; // every two hours if mapping succeeds - } nat_u; // libnatpmp stuff for port forwarding c3_o nal_o; // lane cache backcompat flag struct { // config: c3_o net_o; // can send @@ -2771,51 +2765,6 @@ _ames_recv_cb(uv_udp_t* wax_u, } } -static void natpmp_init(uv_timer_t* handle); - -static void -natpmp_cb(uv_poll_t* handle, - c3_i status, - c3_i events) -{ - u3_ames* sam_u = handle->data; - - natpmpresp_t response; - c3_i res_i = readnatpmpresponseorretry(&sam_u->nat_u.req_u, &response); - if ( NATPMP_TRYAGAIN == res_i ) { - return; - } - - uv_poll_stop(handle); - - if ( 0 != res_i ) { - u3l_log("ames: natpmp error %i", res_i); - closenatpmp(&sam_u->nat_u.req_u); - return; - } - - u3l_log("ames: mapped public port %hu to localport %hu lifetime %u", - response.pnu.newportmapping.mappedpublicport, - response.pnu.newportmapping.privateport, - response.pnu.newportmapping.lifetime); - - closenatpmp(&sam_u->nat_u.req_u); - sam_u->nat_u.tim_u.data = sam_u; - uv_timer_start(&sam_u->nat_u.tim_u, natpmp_init, 7200000, 0); -} - -static void -natpmp_init(uv_timer_t *handle) -{ - u3_ames* sam_u = handle->data; - c3_s por_s = sam_u->pir_u->por_s; - - sendnewportmappingrequest(&sam_u->nat_u.req_u, NATPMP_PROTOCOL_UDP, por_s, por_s, 7200); - - sam_u->nat_u.pol_u.data = sam_u; - uv_poll_start(&sam_u->nat_u.pol_u, UV_READABLE, natpmp_cb); -} - static void _mdns_dear_bail(u3_ovum* egg_u, u3_noun lud) { @@ -2936,11 +2885,6 @@ _ames_io_start(u3_ames* sam_u) u3z(our); mdns_init(por_s, !sam_u->pir_u->fak_o, our_s, _ames_put_dear, (void *)sam_u); - - if ( c3n == sam_u->pir_u->fak_o ) { - uv_timer_start(&sam_u->nat_u.tim_u, natpmp_init, 0, 0); - } - c3_free(our_s); } @@ -3284,12 +3228,6 @@ u3_ames_io_init(u3_pier* pir_u) sam_u->sun_u.tim_u.data = sam_u; sam_u->sun_u.dns_u.data = sam_u; - // initialize libnatpmp - sam_u->nat_u.tim_u.data = sam_u; - initnatpmp(&sam_u->nat_u.req_u, 0, 0); - uv_timer_init(u3L, &sam_u->nat_u.tim_u); - uv_poll_init(u3L, &sam_u->nat_u.pol_u, sam_u->nat_u.req_u.s); - // enable forwarding on galaxies only u3_noun who = u3i_chubs(2, sam_u->pir_u->who_d); u3_noun rac = u3do("clan:title", who); From 14be6deff89a7dadd1651347e523ca3ffc63723b Mon Sep 17 00:00:00 2001 From: Pyry Kovanen Date: Wed, 24 Apr 2024 19:15:04 +0300 Subject: [PATCH 060/107] ames: add libnatpmp for automatic port forwarding (#593) Same as https://github.com/urbit/urbit/pull/3261 but for vere. From what I can tell this NAT-PMP stuff is fairly well supported by routers, works on my machine at least. --- WORKSPACE.bazel | 9 ++++ bazel/third_party/natpmp/BUILD.bazel | 0 bazel/third_party/natpmp/natpmp.BUILD | 8 ++++ pkg/vere/BUILD.bazel | 1 + pkg/vere/io/ames.c | 62 +++++++++++++++++++++++++++ 5 files changed, 80 insertions(+) create mode 100644 bazel/third_party/natpmp/BUILD.bazel create mode 100644 bazel/third_party/natpmp/natpmp.BUILD diff --git a/WORKSPACE.bazel b/WORKSPACE.bazel index 39ddfe03e9..dca6750619 100644 --- a/WORKSPACE.bazel +++ b/WORKSPACE.bazel @@ -217,6 +217,15 @@ versioned_http_file( version = "721fa05", ) +versioned_http_archive( + name = "natpmp", + build_file = "//bazel/third_party/natpmp:natpmp.BUILD", + sha256 = "0684ed2c8406437e7519a1bd20ea83780db871b3a3a5d752311ba3e889dbfc70", + strip_prefix = "libnatpmp-{version}", + url = "http://miniupnp.free.fr/files/libnatpmp-{version}.tar.gz", + version = "20230423", +) + versioned_http_file( name = "solid_pill", sha256 = "8b658fcee6978e2b19004a54233cab953e77ea0bb6c3a04d1bfda4ddc6be63c5", diff --git a/bazel/third_party/natpmp/BUILD.bazel b/bazel/third_party/natpmp/BUILD.bazel new file mode 100644 index 0000000000..e69de29bb2 diff --git a/bazel/third_party/natpmp/natpmp.BUILD b/bazel/third_party/natpmp/natpmp.BUILD new file mode 100644 index 0000000000..b3a7edd7e0 --- /dev/null +++ b/bazel/third_party/natpmp/natpmp.BUILD @@ -0,0 +1,8 @@ +cc_library( + name = "natpmp", + srcs = ["natpmp.c", "getgateway.c"], + hdrs = ["natpmp.h", "getgateway.h", "natpmp_declspec.h"], + copts = ["-O3"], + linkstatic = True, + visibility = ["//visibility:public"], +) diff --git a/pkg/vere/BUILD.bazel b/pkg/vere/BUILD.bazel index e736d0e8e1..6303b87201 100644 --- a/pkg/vere/BUILD.bazel +++ b/pkg/vere/BUILD.bazel @@ -140,6 +140,7 @@ vere_library( "@lmdb", "@openssl", "@uv", + "@natpmp", ] + select({ "@platforms//os:macos": [], "@platforms//os:linux": [ diff --git a/pkg/vere/io/ames.c b/pkg/vere/io/ames.c index 7cd462b7d6..a92a38d019 100644 --- a/pkg/vere/io/ames.c +++ b/pkg/vere/io/ames.c @@ -7,6 +7,7 @@ #include "ur.h" #include "zlib.h" +#include "natpmp.h" #include @@ -71,6 +72,11 @@ typedef enum u3_stun_state { u3_lane sef_u; // our lane, if we know it c3_o wok_o; // STUN worked, set on first success } sun_u; // + struct { + natpmp_t req_u; // libnatpmp struct for mapping request + uv_poll_t pol_u; // handle waits on libnatpmp socket + uv_timer_t tim_u; // every two hours if mapping succeeds + } nat_u; // libnatpmp stuff for port forwarding c3_o nal_o; // lane cache backcompat flag struct { // config: c3_o net_o; // can send @@ -2765,6 +2771,51 @@ _ames_recv_cb(uv_udp_t* wax_u, } } +static void natpmp_init(uv_timer_t* handle); + +static void +natpmp_cb(uv_poll_t* handle, + c3_i status, + c3_i events) +{ + u3_ames* sam_u = handle->data; + + natpmpresp_t response; + c3_i res_i = readnatpmpresponseorretry(&sam_u->nat_u.req_u, &response); + if ( NATPMP_TRYAGAIN == res_i ) { + return; + } + + uv_poll_stop(handle); + + if ( 0 != res_i ) { + u3l_log("ames: natpmp error %i", res_i); + closenatpmp(&sam_u->nat_u.req_u); + return; + } + + u3l_log("ames: mapped public port %hu to localport %hu lifetime %u", + response.pnu.newportmapping.mappedpublicport, + response.pnu.newportmapping.privateport, + response.pnu.newportmapping.lifetime); + + closenatpmp(&sam_u->nat_u.req_u); + sam_u->nat_u.tim_u.data = sam_u; + uv_timer_start(&sam_u->nat_u.tim_u, natpmp_init, 7200000, 0); +} + +static void +natpmp_init(uv_timer_t *handle) +{ + u3_ames* sam_u = handle->data; + c3_s por_s = sam_u->pir_u->por_s; + + sendnewportmappingrequest(&sam_u->nat_u.req_u, NATPMP_PROTOCOL_UDP, por_s, por_s, 7200); + + sam_u->nat_u.pol_u.data = sam_u; + uv_poll_start(&sam_u->nat_u.pol_u, UV_READABLE, natpmp_cb); +} + static void _mdns_dear_bail(u3_ovum* egg_u, u3_noun lud) { @@ -2885,6 +2936,11 @@ _ames_io_start(u3_ames* sam_u) u3z(our); mdns_init(por_s, !sam_u->pir_u->fak_o, our_s, _ames_put_dear, (void *)sam_u); + + if ( c3n == sam_u->pir_u->fak_o ) { + uv_timer_start(&sam_u->nat_u.tim_u, natpmp_init, 0, 0); + } + c3_free(our_s); } @@ -3228,6 +3284,12 @@ u3_ames_io_init(u3_pier* pir_u) sam_u->sun_u.tim_u.data = sam_u; sam_u->sun_u.dns_u.data = sam_u; + // initialize libnatpmp + sam_u->nat_u.tim_u.data = sam_u; + initnatpmp(&sam_u->nat_u.req_u, 0, 0); + uv_timer_init(u3L, &sam_u->nat_u.tim_u); + uv_poll_init(u3L, &sam_u->nat_u.pol_u, sam_u->nat_u.req_u.s); + // enable forwarding on galaxies only u3_noun who = u3i_chubs(2, sam_u->pir_u->who_d); u3_noun rac = u3do("clan:title", who); From c496367699fc109fae2c189b4ab95c1cd220fedc Mon Sep 17 00:00:00 2001 From: pkova Date: Fri, 10 May 2024 17:15:28 +0300 Subject: [PATCH 061/107] ames: fix libnatpmp initialization problems --- pkg/vere/io/ames.c | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) diff --git a/pkg/vere/io/ames.c b/pkg/vere/io/ames.c index a92a38d019..635c6f3665 100644 --- a/pkg/vere/io/ames.c +++ b/pkg/vere/io/ames.c @@ -2781,15 +2781,16 @@ natpmp_cb(uv_poll_t* handle, u3_ames* sam_u = handle->data; natpmpresp_t response; - c3_i res_i = readnatpmpresponseorretry(&sam_u->nat_u.req_u, &response); - if ( NATPMP_TRYAGAIN == res_i ) { + c3_i err_i = readnatpmpresponseorretry(&sam_u->nat_u.req_u, &response); + if ( NATPMP_TRYAGAIN == err_i ) { return; } uv_poll_stop(handle); - if ( 0 != res_i ) { - u3l_log("ames: natpmp error %i", res_i); + if ( 0 != err_i ) { + u3l_log("ames: natpmp error %i", err_i); + uv_poll_stop(&sam_u->nat_u.pol_u); closenatpmp(&sam_u->nat_u.req_u); return; } @@ -2810,6 +2811,14 @@ natpmp_init(uv_timer_t *handle) u3_ames* sam_u = handle->data; c3_s por_s = sam_u->pir_u->por_s; + c3_i err_i = initnatpmp(&sam_u->nat_u.req_u, 0, 0); + + if (err_i != 0) { + return; + } + + uv_poll_init(u3L, &sam_u->nat_u.pol_u, sam_u->nat_u.req_u.s); + sendnewportmappingrequest(&sam_u->nat_u.req_u, NATPMP_PROTOCOL_UDP, por_s, por_s, 7200); sam_u->nat_u.pol_u.data = sam_u; @@ -3286,9 +3295,7 @@ u3_ames_io_init(u3_pier* pir_u) // initialize libnatpmp sam_u->nat_u.tim_u.data = sam_u; - initnatpmp(&sam_u->nat_u.req_u, 0, 0); uv_timer_init(u3L, &sam_u->nat_u.tim_u); - uv_poll_init(u3L, &sam_u->nat_u.pol_u, sam_u->nat_u.req_u.s); // enable forwarding on galaxies only u3_noun who = u3i_chubs(2, sam_u->pir_u->who_d); From d02c620f20ab0a016e5fc859ea1012ae5be41a87 Mon Sep 17 00:00:00 2001 From: pkova Date: Fri, 10 May 2024 18:52:59 +0300 Subject: [PATCH 062/107] ames: check uv_poll_init for errors in natpmp codepath --- pkg/vere/io/ames.c | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/pkg/vere/io/ames.c b/pkg/vere/io/ames.c index 635c6f3665..24ee237754 100644 --- a/pkg/vere/io/ames.c +++ b/pkg/vere/io/ames.c @@ -2817,7 +2817,11 @@ natpmp_init(uv_timer_t *handle) return; } - uv_poll_init(u3L, &sam_u->nat_u.pol_u, sam_u->nat_u.req_u.s); + err_i = uv_poll_init(u3L, &sam_u->nat_u.pol_u, sam_u->nat_u.req_u.s); + + if (err_i != 0) { + return; + } sendnewportmappingrequest(&sam_u->nat_u.req_u, NATPMP_PROTOCOL_UDP, por_s, por_s, 7200); From cca7582774065b0667dd47f5f74961a01f9d122a Mon Sep 17 00:00:00 2001 From: pkova Date: Fri, 10 May 2024 19:45:35 +0300 Subject: [PATCH 063/107] ames: check status in natpmp_cb --- pkg/vere/io/ames.c | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/pkg/vere/io/ames.c b/pkg/vere/io/ames.c index 24ee237754..d13d7a9d7d 100644 --- a/pkg/vere/io/ames.c +++ b/pkg/vere/io/ames.c @@ -2778,6 +2778,11 @@ natpmp_cb(uv_poll_t* handle, c3_i status, c3_i events) { + + if (status != 0) { + return; + } + u3_ames* sam_u = handle->data; natpmpresp_t response; From 879e86d628f11c1f3f5aaeef383bccc63e916485 Mon Sep 17 00:00:00 2001 From: pkova Date: Tue, 14 May 2024 14:38:25 +0300 Subject: [PATCH 064/107] ames: clean up libnatpmp stuff in _ames_io_exit --- pkg/vere/io/ames.c | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/pkg/vere/io/ames.c b/pkg/vere/io/ames.c index d13d7a9d7d..2da7f743c8 100644 --- a/pkg/vere/io/ames.c +++ b/pkg/vere/io/ames.c @@ -3200,6 +3200,11 @@ _ames_io_exit(u3_auto* car_u) uv_close(&sam_u->had_u, _ames_exit_cb); uv_close((uv_handle_t*)&sam_u->sun_u.dns_u, 0); uv_close((uv_handle_t*)&sam_u->sun_u.tim_u, 0); + uv_close((uv_handle_t*)&sam_u->nat_u.tim_u, 0); + + if (uv_is_active((uv_handle_t*)&sam_u->nat_u.pol_u)) { + uv_close((uv_handle_t*)&sam_u->nat_u.pol_u, 0); + } } /* _ames_io_info(): produce status info. From 80a34a7d30d5fd1a073422c4441c49c6dda353c2 Mon Sep 17 00:00:00 2001 From: Joe Bryan Date: Tue, 21 May 2024 14:54:12 -0400 Subject: [PATCH 065/107] u3: better recovery from crashes during snapshot patch application --- pkg/noun/events.c | 55 +++++++++++++++++++++++++++++------------------ 1 file changed, 34 insertions(+), 21 deletions(-) diff --git a/pkg/noun/events.c b/pkg/noun/events.c index d5e0ddac0f..51d911eebe 100644 --- a/pkg/noun/events.c +++ b/pkg/noun/events.c @@ -324,9 +324,15 @@ u3e_fault(u3_post low_p, u3_post hig_p, u3_post off_p) return u3e_flaw_good; } +typedef enum { + _ce_img_good = 0, + _ce_img_fail = 1, + _ce_img_size = 2 +} _ce_img_stat; + /* _ce_image_stat(): measure image. */ -static c3_o +static _ce_img_stat _ce_image_stat(u3e_image* img_u, c3_w* pgs_w) { struct stat buf_u; @@ -334,7 +340,7 @@ _ce_image_stat(u3e_image* img_u, c3_w* pgs_w) if ( -1 == fstat(img_u->fid_i, &buf_u) ) { fprintf(stderr, "loom: stat %s: %s\r\n", img_u->nam_c, strerror(errno)); u3_assert(0); - return c3n; + return _ce_img_fail; } else { c3_z siz_z = buf_u.st_size; @@ -342,19 +348,19 @@ _ce_image_stat(u3e_image* img_u, c3_w* pgs_w) if ( !siz_z ) { *pgs_w = 0; - return c3y; + return _ce_img_good; } else if ( siz_z != _ce_len(pgs_z) ) { fprintf(stderr, "loom: %s corrupt size %zu\r\n", img_u->nam_c, siz_z); - return c3n; + return _ce_img_good; } else if ( pgs_z > UINT32_MAX ) { fprintf(stderr, "loom: %s overflow %zu\r\n", img_u->nam_c, siz_z); - return c3n; + return _ce_img_fail; } else { *pgs_w = (c3_w)pgs_z; - return c3y; + return _ce_img_good; } } } @@ -397,7 +403,7 @@ _ce_ephemeral_open(c3_i* eph_i) /* _ce_image_open(): open or create image. */ -static c3_o +static _ce_img_stat _ce_image_open(u3e_image* img_u, c3_c* ful_c) { c3_i mod_i = O_RDWR | O_CREAT; @@ -406,14 +412,10 @@ _ce_image_open(u3e_image* img_u, c3_c* ful_c) snprintf(pax_c, 8192, "%s/%s.bin", ful_c, img_u->nam_c); if ( -1 == (img_u->fid_i = c3_open(pax_c, mod_i, 0666)) ) { fprintf(stderr, "loom: c3_open %s: %s\r\n", pax_c, strerror(errno)); - return c3n; - } - else if ( c3n == _ce_image_stat(img_u, &img_u->pgs_w) ) { - return c3n; - } - else { - return c3y; + return _ce_img_fail; } + + return _ce_image_stat(img_u, &img_u->pgs_w); } /* _ce_patch_write_control(): write control block file. @@ -1371,13 +1373,18 @@ u3e_backup(c3_c* pux_c, c3_c* pax_c, c3_o ovw_o) // c3_c nux_c[8193]; snprintf(nux_c, 8192, "%s/%s.bin", pux_c, nux_u.nam_c); - if ( (0 != access(nux_c, F_OK)) || (c3n == _ce_image_open(&nux_u, pux_c)) ) { + if ( (0 != access(nux_c, F_OK)) + || (_ce_img_good != _ce_image_open(&nux_u, pux_c)) ) + { fprintf(stderr, "loom: couldn't open north image at %s\r\n", pux_c); return c3n; } + c3_c sux_c[8193]; snprintf(sux_c, 8192, "%s/%s.bin", pux_c, sux_u.nam_c); - if ( (0 != access(sux_c, F_OK)) || (c3n == _ce_image_open(&sux_u, pux_c)) ) { + if ( (0 != access(sux_c, F_OK)) + || (_ce_img_good != _ce_image_open(&sux_u, pux_c)) ) + { fprintf(stderr, "loom: couldn't open south image at %s\r\n", pux_c); return c3n; } @@ -1487,9 +1494,9 @@ u3e_save(u3_post low_p, u3_post hig_p) #ifdef U3_SNAPSHOT_VALIDATION { c3_w pgs_w; - u3_assert( c3y == _ce_image_stat(&u3P.nor_u, &pgs_w) ); + u3_assert( _ce_img_good == _ce_image_stat(&u3P.nor_u, &pgs_w) ); u3_assert( pgs_w == u3P.nor_u.pgs_w ); - u3_assert( c3y == _ce_image_stat(&u3P.sou_u, &pgs_w) ); + u3_assert( _ce_img_good == _ce_image_stat(&u3P.sou_u, &pgs_w) ); u3_assert( pgs_w == u3P.sou_u.pgs_w ); } #endif @@ -1590,9 +1597,11 @@ u3e_live(c3_o nuu_o, c3_c* dir_c) // c3_c chk_c[8193]; snprintf(chk_c, 8193, "%s/.urb/chk", u3P.dir_c); - if ( (c3n == _ce_image_open(&u3P.nor_u, chk_c)) || - (c3n == _ce_image_open(&u3P.sou_u, chk_c)) ) - { + + _ce_img_stat nor_e = _ce_image_open(&u3P.nor_u, chk_c); + _ce_img_stat sou_e = _ce_image_open(&u3P.sou_u, chk_c); + + if ( (_ce_img_fail == nor_e) || (_ce_img_fail == sou_e) ) { fprintf(stderr, "boot: image failed\r\n"); exit(1); } @@ -1609,6 +1618,10 @@ u3e_live(c3_o nuu_o, c3_c* dir_c) _ce_patch_free(pat_u); _ce_patch_delete(); } + else if ( (_ce_img_size == nor_e) || (_ce_img_size == sou_e) ) { + fprintf(stderr, "boot: image failed (size)\r\n"); + exit(1); + } nor_w = u3P.nor_u.pgs_w; sou_w = u3P.sou_u.pgs_w; From 0602d1d65ca44be712830611d5b83043b2b5e299 Mon Sep 17 00:00:00 2001 From: Matthew LeVan Date: Fri, 24 May 2024 12:21:29 -0400 Subject: [PATCH 066/107] play: replay in a subprocess --- pkg/vere/main.c | 29 ++++++++++++++++++++++++++++- 1 file changed, 28 insertions(+), 1 deletion(-) diff --git a/pkg/vere/main.c b/pkg/vere/main.c index 2222f87dd1..f48c8dc9ba 100644 --- a/pkg/vere/main.c +++ b/pkg/vere/main.c @@ -18,6 +18,7 @@ #include "db/lmdb.h" #include "getopt.h" #include "libgen.h" +#include "spawn.h" #include "ca_bundle.h" #include "pace.h" @@ -2315,6 +2316,28 @@ _cw_play_impl(c3_d eve_d, c3_d sap_d, c3_o mel_o, c3_o sof_o, c3_o ful_o) return pay_d; } +/* _cw_play_fork(): spawn a subprocess for event replay. +*/ +static c3_i +_cw_play_fork(c3_c *pax_c) // XX use --serf-bin +{ + pid_t pid; + c3_i sat_i; + c3_c *argv[] = { u3_Host.wrk_c, "play", u3_Host.dir_c }; // XX parameterize args + + if ( 0 != posix_spawn(&pid, pax_c, 0, 0, argv, 0) ) { + fprintf(stderr, "play: posix_spawn: %d\r\n", errno); + return 1; + } + + if ( -1 == waitpid(pid, &sat_i, 0) ) { + fprintf(stderr, "play: waitpid: %d\r\n", errno); + return 1; + } + + return WEXITSTATUS(sat_i); +} + /* _cw_play(): replay events, but better. */ static void @@ -3110,7 +3133,11 @@ main(c3_i argc, // we need the current snapshot's latest event number to // validate whether we can execute disk migration if ( u3_Host.ops_u.nuu == c3n ) { - _cw_play_impl(0, 0, c3n, c3n, c3n); + c3_i sat_i = _cw_play_fork(u3_Host.dem_c); + if ( sat_i ) { + fprintf(stderr, "play: replay failed: %d\r\n", sat_i); + exit(sat_i); + } // XX unmap loom, else parts of the snapshot could be left in memory } From 53223be13e04199def835181c10d338740604e22 Mon Sep 17 00:00:00 2001 From: Matthew LeVan Date: Fri, 24 May 2024 12:23:29 -0400 Subject: [PATCH 067/107] Revert "Revert "vere: add --serf-bin option to provide path to serf"" This reverts commit a37f05248056fc580551d6e23414a7f4d75a8a55. --- pkg/vere/main.c | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/pkg/vere/main.c b/pkg/vere/main.c index f48c8dc9ba..e54f68b708 100644 --- a/pkg/vere/main.c +++ b/pkg/vere/main.c @@ -292,12 +292,13 @@ _main_getopt(c3_i argc, c3_c** argv) { "prop-url", required_argument, NULL, 2 }, { "prop-name", required_argument, NULL, 3 }, // - { "urth-loom", required_argument, NULL, 5 }, - { "no-demand", no_argument, NULL, 6 }, - { "swap", no_argument, NULL, 7 }, - { "swap-to", required_argument, NULL, 8 }, - { "toss", required_argument, NULL, 9 }, + { "urth-loom", required_argument, NULL, 5 }, + { "no-demand", no_argument, NULL, 6 }, + { "swap", no_argument, NULL, 7 }, + { "swap-to", required_argument, NULL, 8 }, + { "toss", required_argument, NULL, 9 }, { "behn-allow-blocked", no_argument, NULL, 10 }, + { "serf-bin", required_argument, NULL, 11 }, // { NULL, 0, NULL, 0 }, }; @@ -341,6 +342,10 @@ _main_getopt(c3_i argc, c3_c** argv) u3_Host.ops_u.beb = c3y; break; } + case 11: { // serf-bin + u3_Host.wrk_c = strdup(optarg); + break; + } // special args // case c3__loom: { From 206e03c5485a92cccca1dd5096708de1ab032884 Mon Sep 17 00:00:00 2001 From: Matthew LeVan Date: Fri, 24 May 2024 12:35:00 -0400 Subject: [PATCH 068/107] play: use `u3_Host` strings --- pkg/vere/main.c | 14 ++++++++------ pkg/vere/vere.h | 3 ++- 2 files changed, 10 insertions(+), 7 deletions(-) diff --git a/pkg/vere/main.c b/pkg/vere/main.c index e54f68b708..645ce38a1a 100644 --- a/pkg/vere/main.c +++ b/pkg/vere/main.c @@ -343,7 +343,7 @@ _main_getopt(c3_i argc, c3_c** argv) break; } case 11: { // serf-bin - u3_Host.wrk_c = strdup(optarg); + u3_Host.sef_c = strdup(optarg); break; } // special args @@ -2324,13 +2324,13 @@ _cw_play_impl(c3_d eve_d, c3_d sap_d, c3_o mel_o, c3_o sof_o, c3_o ful_o) /* _cw_play_fork(): spawn a subprocess for event replay. */ static c3_i -_cw_play_fork(c3_c *pax_c) // XX use --serf-bin +_cw_play_fork() { pid_t pid; c3_i sat_i; c3_c *argv[] = { u3_Host.wrk_c, "play", u3_Host.dir_c }; // XX parameterize args - if ( 0 != posix_spawn(&pid, pax_c, 0, 0, argv, 0) ) { + if ( 0 != posix_spawn(&pid, u3_Host.sef_c, 0, 0, argv, 0) ) { fprintf(stderr, "play: posix_spawn: %d\r\n", errno); return 1; } @@ -2994,8 +2994,6 @@ main(c3_i argc, _main_self_path(); - // XX add argument - // if ( !u3_Host.wrk_c ) { u3_Host.wrk_c = bin_c; } @@ -3003,6 +3001,10 @@ main(c3_i argc, c3_free(bin_c); } + if ( !u3_Host.sef_c ) { + u3_Host.sef_c = u3_Host.dem_c; + } + if ( c3y == u3_Host.ops_u.dem ) { // In daemon mode, run the urbit as a background process, but don't // exit from the parent process until the ship is finished booting. @@ -3138,7 +3140,7 @@ main(c3_i argc, // we need the current snapshot's latest event number to // validate whether we can execute disk migration if ( u3_Host.ops_u.nuu == c3n ) { - c3_i sat_i = _cw_play_fork(u3_Host.dem_c); + c3_i sat_i = _cw_play_fork(); if ( sat_i ) { fprintf(stderr, "play: replay failed: %d\r\n", sat_i); exit(sat_i); diff --git a/pkg/vere/vere.h b/pkg/vere/vere.h index f97a76f142..b28679ee7b 100644 --- a/pkg/vere/vere.h +++ b/pkg/vere/vere.h @@ -329,7 +329,8 @@ c3_c* dir_c; // pier path (no trailing /) c3_d eve_d; // initial current snapshot c3_c* dem_c; // daemon executable path - c3_c* wrk_c; // worker executable path + c3_c* wrk_c; // worker executable name + c3_c* sef_c; // worker executable path c3_d now_d; // event tick uv_loop_t* lup_u; // libuv event loop u3_usig* sig_u; // signal list From 837799b4a2e767a55b9636429f3d9c304ee77e93 Mon Sep 17 00:00:00 2001 From: Matthew LeVan Date: Fri, 24 May 2024 12:55:48 -0400 Subject: [PATCH 069/107] play: use `u3_Host.wrk_c` --- pkg/vere/main.c | 10 ++++------ pkg/vere/vere.h | 3 +-- 2 files changed, 5 insertions(+), 8 deletions(-) diff --git a/pkg/vere/main.c b/pkg/vere/main.c index 645ce38a1a..c9ca613204 100644 --- a/pkg/vere/main.c +++ b/pkg/vere/main.c @@ -343,7 +343,7 @@ _main_getopt(c3_i argc, c3_c** argv) break; } case 11: { // serf-bin - u3_Host.sef_c = strdup(optarg); + u3_Host.wrk_c = strdup(optarg); break; } // special args @@ -2330,7 +2330,9 @@ _cw_play_fork() c3_i sat_i; c3_c *argv[] = { u3_Host.wrk_c, "play", u3_Host.dir_c }; // XX parameterize args - if ( 0 != posix_spawn(&pid, u3_Host.sef_c, 0, 0, argv, 0) ) { + fprintf(stderr, "wrk_c: %s\r\n", u3_Host.wrk_c); + + if ( 0 != posix_spawn(&pid, u3_Host.wrk_c, 0, 0, argv, 0) ) { fprintf(stderr, "play: posix_spawn: %d\r\n", errno); return 1; } @@ -3001,10 +3003,6 @@ main(c3_i argc, c3_free(bin_c); } - if ( !u3_Host.sef_c ) { - u3_Host.sef_c = u3_Host.dem_c; - } - if ( c3y == u3_Host.ops_u.dem ) { // In daemon mode, run the urbit as a background process, but don't // exit from the parent process until the ship is finished booting. diff --git a/pkg/vere/vere.h b/pkg/vere/vere.h index b28679ee7b..f97a76f142 100644 --- a/pkg/vere/vere.h +++ b/pkg/vere/vere.h @@ -329,8 +329,7 @@ c3_c* dir_c; // pier path (no trailing /) c3_d eve_d; // initial current snapshot c3_c* dem_c; // daemon executable path - c3_c* wrk_c; // worker executable name - c3_c* sef_c; // worker executable path + c3_c* wrk_c; // worker executable path c3_d now_d; // event tick uv_loop_t* lup_u; // libuv event loop u3_usig* sig_u; // signal list From 798aa3fa532c9a625cfe9c291107ed406ede741a Mon Sep 17 00:00:00 2001 From: Matthew LeVan Date: Fri, 24 May 2024 12:57:36 -0400 Subject: [PATCH 070/107] play: remove print --- pkg/vere/main.c | 2 -- 1 file changed, 2 deletions(-) diff --git a/pkg/vere/main.c b/pkg/vere/main.c index c9ca613204..3173ef7d5a 100644 --- a/pkg/vere/main.c +++ b/pkg/vere/main.c @@ -2330,8 +2330,6 @@ _cw_play_fork() c3_i sat_i; c3_c *argv[] = { u3_Host.wrk_c, "play", u3_Host.dir_c }; // XX parameterize args - fprintf(stderr, "wrk_c: %s\r\n", u3_Host.wrk_c); - if ( 0 != posix_spawn(&pid, u3_Host.wrk_c, 0, 0, argv, 0) ) { fprintf(stderr, "play: posix_spawn: %d\r\n", errno); return 1; From d67090b6e59397782def36d0e281c85483aa20e4 Mon Sep 17 00:00:00 2001 From: Matthew LeVan Date: Fri, 24 May 2024 13:32:16 -0400 Subject: [PATCH 071/107] disk: parameterize lmdb map size --- pkg/vere/disk.c | 21 ++++++--------------- pkg/vere/main.c | 17 +++++++++++++++++ pkg/vere/vere.h | 1 + 3 files changed, 24 insertions(+), 15 deletions(-) diff --git a/pkg/vere/disk.c b/pkg/vere/disk.c index b60253fb1c..58cb2f822f 100644 --- a/pkg/vere/disk.c +++ b/pkg/vere/disk.c @@ -31,15 +31,6 @@ struct _u3_disk_walk { c3_o liv_o; }; -// for u3_lmdb_init() calls -static const size_t siz_i = -#if (defined(U3_CPU_aarch64) && defined(U3_OS_linux)) - // 500 GiB is as large as musl on aarch64 wants to allow - 0x7d00000000; -#else - 0x10000000000; -#endif - #undef VERBOSE_DISK #undef DISK_TRACE_JAM #undef DISK_TRACE_CUE @@ -680,7 +671,7 @@ u3_disk_save_meta_meta(c3_c* log_c, { MDB_env* dbm_u; - if ( 0 == (dbm_u = u3_lmdb_init(log_c, siz_i)) ) { + if ( 0 == (dbm_u = u3_lmdb_init(log_c, u3_Host.ops_u.siz_i)) ) { fprintf(stderr, "disk: failed to initialize meta-lmdb\r\n"); return c3n; } @@ -1240,7 +1231,7 @@ _disk_epoc_roll(u3_disk* log_u, c3_d epo_d) log_u->mdb_u = 0; // initialize db of new epoch - if ( 0 == (log_u->mdb_u = u3_lmdb_init(epo_c, siz_i)) ) { + if ( 0 == (log_u->mdb_u = u3_lmdb_init(epo_c, u3_Host.ops_u.siz_i)) ) { fprintf(stderr, "disk: failed to initialize database\r\n"); c3_free(log_u); goto fail3; @@ -1499,7 +1490,7 @@ _disk_migrate(u3_disk* log_u, c3_d eve_d) return c3n; } - if ( 0 == (log_u->mdb_u = u3_lmdb_init(tmp_c, siz_i)) ) { + if ( 0 == (log_u->mdb_u = u3_lmdb_init(tmp_c, u3_Host.ops_u.siz_i)) ) { fprintf(stderr, "disk: failed to initialize database at %s\r\n", tmp_c); return c3n; @@ -1533,7 +1524,7 @@ _disk_migrate(u3_disk* log_u, c3_d eve_d) strerror(errno)); } - if ( 0 == (log_u->mdb_u = u3_lmdb_init(epo_c, siz_i)) ) { + if ( 0 == (log_u->mdb_u = u3_lmdb_init(epo_c, u3_Host.ops_u.siz_i)) ) { fprintf(stderr, "disk: failed to initialize database at %s\r\n", epo_c); return c3n; @@ -1736,7 +1727,7 @@ _disk_epoc_load(u3_disk* log_u, c3_d lat_d) snprintf(epo_c, 8192, "%s/0i%" PRIc3_d, log_u->com_u->pax_c, lat_d); // initialize latest epoch's db - if ( 0 == (log_u->mdb_u = u3_lmdb_init(epo_c, siz_i)) ) { + if ( 0 == (log_u->mdb_u = u3_lmdb_init(epo_c, u3_Host.ops_u.siz_i)) ) { fprintf(stderr, "disk: failed to initialize database at %s\r\n", epo_c); return _epoc_fail; @@ -1880,7 +1871,7 @@ u3_disk_init(c3_c* pax_c, u3_disk_cb cb_u) if ( c3y == exs_o ) { // load the old data.mdb file - if ( 0 == (log_u->mdb_u = u3_lmdb_init(log_c, siz_i)) ) { + if ( 0 == (log_u->mdb_u = u3_lmdb_init(log_c, u3_Host.ops_u.siz_i)) ) { fprintf(stderr, "disk: failed to initialize lmdb\r\n"); c3_free(log_u); return 0; diff --git a/pkg/vere/main.c b/pkg/vere/main.c index 2222f87dd1..beac31ddde 100644 --- a/pkg/vere/main.c +++ b/pkg/vere/main.c @@ -297,6 +297,7 @@ _main_getopt(c3_i argc, c3_c** argv) { "swap-to", required_argument, NULL, 8 }, { "toss", required_argument, NULL, 9 }, { "behn-allow-blocked", no_argument, NULL, 10 }, + { "lmdb-map-size", required_argument, NULL, 12 }, // { NULL, 0, NULL, 0 }, }; @@ -340,6 +341,12 @@ _main_getopt(c3_i argc, c3_c** argv) u3_Host.ops_u.beb = c3y; break; } + case 12: { // lmdb-map-size + if ( 1 != sscanf(optarg, "%" SCNu32, &u3_Host.ops_u.siz_i) ) { + return c3n; + } + break; + } // special args // case c3__loom: { @@ -2991,6 +2998,16 @@ main(c3_i argc, u3_Host.bot_f = _stop_on_boot_completed_cb; } + if ( !u3_Host.ops_u.siz_i ) { + u3_Host.ops_u.siz_i = +#if (defined(U3_CPU_aarch64) && defined(U3_OS_linux)) + // 500 GiB is as large as musl on aarch64 wants to allow + 0x7d00000000; +#else + 0x10000000000; +#endif + } + #if 0 if ( 0 == getuid() ) { chroot(u3_Host.dir_c); diff --git a/pkg/vere/vere.h b/pkg/vere/vere.h index f97a76f142..b26ad4b27e 100644 --- a/pkg/vere/vere.h +++ b/pkg/vere/vere.h @@ -320,6 +320,7 @@ u3_even* vex_u; // --prop-*, boot enhancements c3_o beb; // --behn-allow-blocked + c3_z siz_i; // --lmdb-map-size } u3_opts; /* u3_host: entire host. From 41fa70ec38f36b2ccfe3d5ebd5d9304683640f6f Mon Sep 17 00:00:00 2001 From: Matthew LeVan Date: Fri, 24 May 2024 15:15:22 -0400 Subject: [PATCH 072/107] cli: add `--lmdb-map-size` to subcommands; always init `siz_i`; use correct format specifier --- pkg/vere/main.c | 132 ++++++++++++++++++++++++++++++++++-------------- 1 file changed, 93 insertions(+), 39 deletions(-) diff --git a/pkg/vere/main.c b/pkg/vere/main.c index beac31ddde..24cf65746d 100644 --- a/pkg/vere/main.c +++ b/pkg/vere/main.c @@ -193,6 +193,14 @@ _main_init(void) u3_Host.ops_u.lut_y = 31; /* aka 2G */ u3_Host.ops_u.lom_y = 31; + u3_Host.ops_u.siz_i = +#if (defined(U3_CPU_aarch64) && defined(U3_OS_linux)) + // 500 GiB is as large as musl on aarch64 wants to allow + 0x7d00000000; +#else + 0x10000000000; +#endif + u3C.eph_c = 0; u3C.tos_w = 0; } @@ -342,7 +350,7 @@ _main_getopt(c3_i argc, c3_c** argv) break; } case 12: { // lmdb-map-size - if ( 1 != sscanf(optarg, "%" SCNu32, &u3_Host.ops_u.siz_i) ) { + if ( 1 != sscanf(optarg, "%" SCNuMAX, &u3_Host.ops_u.siz_i) ) { return c3n; } break; @@ -1576,10 +1584,11 @@ _cw_info(c3_i argc, c3_c* argv[]) c3_w arg_w; static struct option lop_u[] = { - { "loom", required_argument, NULL, c3__loom }, - { "no-demand", no_argument, NULL, 6 }, - { "swap", no_argument, NULL, 7 }, - { "swap-to", required_argument, NULL, 8 }, + { "loom", required_argument, NULL, c3__loom }, + { "no-demand", no_argument, NULL, 6 }, + { "swap", no_argument, NULL, 7 }, + { "swap-to", required_argument, NULL, 8 }, + { "lmdb-map-size", required_argument, NULL, 9 }, { NULL, 0, NULL, 0 } }; @@ -1610,6 +1619,13 @@ _cw_info(c3_i argc, c3_c* argv[]) break; } + case 9: { // lmdb-map-size + if ( 1 != sscanf(optarg, "%" SCNuMAX, &u3_Host.ops_u.siz_i) ) { + exit(1); + } + break; + } + case '?': { fprintf(stderr, "invalid argument\r\n"); exit(1); @@ -1751,10 +1767,11 @@ _cw_cram(c3_i argc, c3_c* argv[]) c3_w arg_w; static struct option lop_u[] = { - { "loom", required_argument, NULL, c3__loom }, - { "no-demand", no_argument, NULL, 6 }, - { "swap", no_argument, NULL, 7 }, - { "swap-to", required_argument, NULL, 8 }, + { "loom", required_argument, NULL, c3__loom }, + { "no-demand", no_argument, NULL, 6 }, + { "swap", no_argument, NULL, 7 }, + { "swap-to", required_argument, NULL, 8 }, + { "lmdb-map-size", required_argument, NULL, 9 }, { NULL, 0, NULL, 0 } }; @@ -1785,6 +1802,13 @@ _cw_cram(c3_i argc, c3_c* argv[]) break; } + case 9: { // lmdb-map-size + if ( 1 != sscanf(optarg, "%" SCNuMAX, &u3_Host.ops_u.siz_i) ) { + exit(1); + } + break; + } + case '?': { fprintf(stderr, "invalid argument\r\n"); exit(1); @@ -1847,11 +1871,12 @@ _cw_queu(c3_i argc, c3_c* argv[]) c3_c* roc_c = 0; static struct option lop_u[] = { - { "loom", required_argument, NULL, c3__loom }, - { "no-demand", no_argument, NULL, 6 }, - { "swap", no_argument, NULL, 7 }, - { "swap-to", required_argument, NULL, 8 }, - { "replay-from", required_argument, NULL, 'r' }, + { "loom", required_argument, NULL, c3__loom }, + { "no-demand", no_argument, NULL, 6 }, + { "swap", no_argument, NULL, 7 }, + { "swap-to", required_argument, NULL, 8 }, + { "lmdb-map-size", required_argument, NULL, 9 }, + { "replay-from", required_argument, NULL, 'r' }, { NULL, 0, NULL, 0 } }; @@ -1882,6 +1907,13 @@ _cw_queu(c3_i argc, c3_c* argv[]) break; } + case 9: { // lmdb-map-size + if ( 1 != sscanf(optarg, "%" SCNuMAX, &u3_Host.ops_u.siz_i) ) { + exit(1); + } + break; + } + case 'r': { roc_c = strdup(optarg); } break; @@ -1955,11 +1987,12 @@ _cw_meld(c3_i argc, c3_c* argv[]) c3_w arg_w; static struct option lop_u[] = { - { "loom", required_argument, NULL, c3__loom }, - { "no-demand", no_argument, NULL, 6 }, - { "swap", no_argument, NULL, 7 }, - { "swap-to", required_argument, NULL, 8 }, - { "gc-early", no_argument, NULL, 9 }, + { "loom", required_argument, NULL, c3__loom }, + { "no-demand", no_argument, NULL, 6 }, + { "swap", no_argument, NULL, 7 }, + { "swap-to", required_argument, NULL, 8 }, + { "gc-early", no_argument, NULL, 9 }, + { "lmdb-map-size", required_argument, NULL, 10 }, { NULL, 0, NULL, 0 } }; @@ -1995,6 +2028,13 @@ _cw_meld(c3_i argc, c3_c* argv[]) break; } + case 10: { // lmdb-map-size + if ( 1 != sscanf(optarg, "%" SCNuMAX, &u3_Host.ops_u.siz_i) ) { + exit(1); + } + break; + } + case '?': { fprintf(stderr, "invalid argument\r\n"); exit(1); @@ -2123,11 +2163,12 @@ _cw_pack(c3_i argc, c3_c* argv[]) c3_w arg_w; static struct option lop_u[] = { - { "loom", required_argument, NULL, c3__loom }, - { "no-demand", no_argument, NULL, 6 }, - { "swap", no_argument, NULL, 7 }, - { "swap-to", required_argument, NULL, 8 }, - { "gc-early", no_argument, NULL, 9 }, + { "loom", required_argument, NULL, c3__loom }, + { "no-demand", no_argument, NULL, 6 }, + { "swap", no_argument, NULL, 7 }, + { "swap-to", required_argument, NULL, 8 }, + { "gc-early", no_argument, NULL, 9 }, + { "lmdb-map-size", required_argument, NULL, 10 }, { NULL, 0, NULL, 0 } }; @@ -2163,6 +2204,13 @@ _cw_pack(c3_i argc, c3_c* argv[]) break; } + case 10: { // lmdb-map-size + if ( 1 != sscanf(optarg, "%" SCNuMAX, &u3_Host.ops_u.siz_i) ) { + exit(1); + } + break; + } + case '?': { fprintf(stderr, "invalid argument\r\n"); exit(1); @@ -2504,10 +2552,11 @@ _cw_chop(c3_i argc, c3_c* argv[]) c3_w arg_w; static struct option lop_u[] = { - { "loom", required_argument, NULL, c3__loom }, - { "no-demand", no_argument, NULL, 6 }, - { "swap", no_argument, NULL, 7 }, - { "swap-to", required_argument, NULL, 8 }, + { "loom", required_argument, NULL, c3__loom }, + { "no-demand", no_argument, NULL, 6 }, + { "swap", no_argument, NULL, 7 }, + { "swap-to", required_argument, NULL, 8 }, + { "lmdb-map-size", required_argument, NULL, 9 }, { NULL, 0, NULL, 0 } }; @@ -2538,6 +2587,13 @@ _cw_chop(c3_i argc, c3_c* argv[]) break; } + case 9: { // lmdb-map-size + if ( 1 != sscanf(optarg, "%" SCNuMAX, &u3_Host.ops_u.siz_i) ) { + exit(1); + } + break; + } + case '?': { fprintf(stderr, "invalid argument\r\n"); exit(1); @@ -2585,7 +2641,8 @@ _cw_roll(c3_i argc, c3_c* argv[]) c3_w arg_w; static struct option lop_u[] = { - { "loom", required_argument, NULL, c3__loom }, + { "loom", required_argument, NULL, c3__loom }, + { "lmdb-map-size", required_argument, NULL, 6 }, { NULL, 0, NULL, 0 } }; @@ -2593,6 +2650,13 @@ _cw_roll(c3_i argc, c3_c* argv[]) while ( -1 != (ch_i=getopt_long(argc, argv, "", lop_u, &lid_i)) ) { switch ( ch_i ) { + case 6: { // lmdb-map-size + if ( 1 != sscanf(optarg, "%" SCNuMAX, &u3_Host.ops_u.siz_i) ) { + exit(1); + } + break; + } + case c3__loom: { if (_main_readw_loom("loom", &u3_Host.ops_u.lom_y)) { exit(1); @@ -2998,16 +3062,6 @@ main(c3_i argc, u3_Host.bot_f = _stop_on_boot_completed_cb; } - if ( !u3_Host.ops_u.siz_i ) { - u3_Host.ops_u.siz_i = -#if (defined(U3_CPU_aarch64) && defined(U3_OS_linux)) - // 500 GiB is as large as musl on aarch64 wants to allow - 0x7d00000000; -#else - 0x10000000000; -#endif - } - #if 0 if ( 0 == getuid() ) { chroot(u3_Host.dir_c); From b0de3aac5511ef8f1a7ea7706ab08653b7b9c225 Mon Sep 17 00:00:00 2001 From: Matthew LeVan Date: Sat, 25 May 2024 21:11:47 -0400 Subject: [PATCH 073/107] play: add args to `_cw_play_fork` --- pkg/vere/main.c | 23 ++++++++++++++++++++--- 1 file changed, 20 insertions(+), 3 deletions(-) diff --git a/pkg/vere/main.c b/pkg/vere/main.c index 3173ef7d5a..0bef9e4b1e 100644 --- a/pkg/vere/main.c +++ b/pkg/vere/main.c @@ -2324,11 +2324,28 @@ _cw_play_impl(c3_d eve_d, c3_d sap_d, c3_o mel_o, c3_o sof_o, c3_o ful_o) /* _cw_play_fork(): spawn a subprocess for event replay. */ static c3_i -_cw_play_fork() +_cw_play_fork(c3_d eve_d, c3_d sap_d, c3_o mel_o, c3_o sof_o, c3_o ful_o) { pid_t pid; c3_i sat_i; - c3_c *argv[] = { u3_Host.wrk_c, "play", u3_Host.dir_c }; // XX parameterize args + c3_c eve_c[21], sap_c[21] = { 0 }; + sprintf(eve_c, "%" PRIu64, eve_d); + sprintf(sap_c, "%" PRIu64, sap_d); + // if ( 0 < sprintf(eve_c, "%" PRIu64, eve_d) || + // 0 < sprintf(sap_c, "%" PRIu64, sap_d) ) + // { + // fprintf(stderr, "play: error parsing args\r\n"); + // return 1; + // } + c3_c *argv[] = { + u3_Host.wrk_c, "play", u3_Host.dir_c, + "--replay-to", eve_c, + "--snap-at", sap_c, + _(mel_o) ? "--auto-meld" : 0, // + _(sof_o) ? "--soft-mugs" : 0, // + _(ful_o) ? "--full" : 0, // XX + 0 + }; if ( 0 != posix_spawn(&pid, u3_Host.wrk_c, 0, 0, argv, 0) ) { fprintf(stderr, "play: posix_spawn: %d\r\n", errno); @@ -3136,7 +3153,7 @@ main(c3_i argc, // we need the current snapshot's latest event number to // validate whether we can execute disk migration if ( u3_Host.ops_u.nuu == c3n ) { - c3_i sat_i = _cw_play_fork(); + c3_i sat_i = _cw_play_fork(0, 0, c3n, c3n, c3y); if ( sat_i ) { fprintf(stderr, "play: replay failed: %d\r\n", sat_i); exit(sat_i); From 5ba82e80b4c4c5ad2fc246bc3581831a2d6208ec Mon Sep 17 00:00:00 2001 From: Pyry Kovanen Date: Mon, 27 May 2024 16:02:16 +0300 Subject: [PATCH 074/107] main: restore SIGTSTP handler after replay --- pkg/vere/main.c | 1 + 1 file changed, 1 insertion(+) diff --git a/pkg/vere/main.c b/pkg/vere/main.c index 2222f87dd1..37de90f882 100644 --- a/pkg/vere/main.c +++ b/pkg/vere/main.c @@ -3111,6 +3111,7 @@ main(c3_i argc, // validate whether we can execute disk migration if ( u3_Host.ops_u.nuu == c3n ) { _cw_play_impl(0, 0, c3n, c3n, c3n); + signal(SIGTSTP, _stop_exit); // XX unmap loom, else parts of the snapshot could be left in memory } From 8dd289942cc7ff008f99b3cb1df70f65435eeada Mon Sep 17 00:00:00 2001 From: Matthew LeVan Date: Tue, 28 May 2024 10:55:29 -0400 Subject: [PATCH 075/107] play: produce args better --- pkg/vere/main.c | 53 ++++++++++++++++++++++++++++++++++--------------- 1 file changed, 37 insertions(+), 16 deletions(-) diff --git a/pkg/vere/main.c b/pkg/vere/main.c index 0bef9e4b1e..047b511290 100644 --- a/pkg/vere/main.c +++ b/pkg/vere/main.c @@ -2328,25 +2328,46 @@ _cw_play_fork(c3_d eve_d, c3_d sap_d, c3_o mel_o, c3_o sof_o, c3_o ful_o) { pid_t pid; c3_i sat_i; + c3_c eve_c[21], sap_c[21] = { 0 }; - sprintf(eve_c, "%" PRIu64, eve_d); - sprintf(sap_c, "%" PRIu64, sap_d); - // if ( 0 < sprintf(eve_c, "%" PRIu64, eve_d) || - // 0 < sprintf(sap_c, "%" PRIu64, sap_d) ) - // { - // fprintf(stderr, "play: error parsing args\r\n"); - // return 1; - // } - c3_c *argv[] = { - u3_Host.wrk_c, "play", u3_Host.dir_c, - "--replay-to", eve_c, - "--snap-at", sap_c, - _(mel_o) ? "--auto-meld" : 0, // - _(sof_o) ? "--soft-mugs" : 0, // - _(ful_o) ? "--full" : 0, // XX - 0 + if ( 0 > sprintf(eve_c, "%" PRIu64, eve_d) || + 0 > sprintf(sap_c, "%" PRIu64, sap_d) ) + { + fprintf(stderr, "play: error parsing args\r\n"); + return 1; + } + + c3_c *argv[11] = { + u3_Host.wrk_c, + "play", + u3_Host.dir_c, + "--replay-to", + eve_c, + "--snap-at", + sap_c, }; + c3_z i = 7; + + if _(mel_o) { + argv[i++] = "--auto-meld"; + } + + if _(sof_o) { + argv[i++] = "--soft-mugs"; + } + + if _(ful_o) { + argv[i++] = "--full"; + } + + argv[i] = NULL; + + for (c3_i j = 0; j < i; j++) { + fprintf(stderr, "%s ", argv[j]); + } + fprintf(stderr, "\r\n"); + if ( 0 != posix_spawn(&pid, u3_Host.wrk_c, 0, 0, argv, 0) ) { fprintf(stderr, "play: posix_spawn: %d\r\n", errno); return 1; From da773cf6b2f36b91832ffe3d91c330470c740e59 Mon Sep 17 00:00:00 2001 From: Matthew LeVan Date: Thu, 30 May 2024 12:31:37 -0400 Subject: [PATCH 076/107] play: serf commits sudoku when king dies --- pkg/vere/main.c | 65 +++++++++++++++++++++++++++++++++++++++---------- pkg/vere/mars.c | 1 + 2 files changed, 53 insertions(+), 13 deletions(-) diff --git a/pkg/vere/main.c b/pkg/vere/main.c index 047b511290..4e7ae85b45 100644 --- a/pkg/vere/main.c +++ b/pkg/vere/main.c @@ -18,6 +18,7 @@ #include "db/lmdb.h" #include "getopt.h" #include "libgen.h" +#include "pthread.h" #include "spawn.h" #include "ca_bundle.h" @@ -2321,14 +2322,31 @@ _cw_play_impl(c3_d eve_d, c3_d sap_d, c3_o mel_o, c3_o sof_o, c3_o ful_o) return pay_d; } +/* _cw_play_fork_heed(): wait for EOF on STDIN or until canceled. +*/ +void* _cw_play_fork_heed(void* arg) { + c3_c buf[1]; + c3_zs red; + + do { + pthread_testcancel(); + red = read(STDIN_FILENO, buf, sizeof(buf)); + if ( 0 == red ) { + fprintf(stderr, "play: god save the king! committing sudoku...\r\n"); + exit(1); + } + } while ( 0 < red ); + + return NULL; +} + /* _cw_play_fork(): spawn a subprocess for event replay. */ static c3_i _cw_play_fork(c3_d eve_d, c3_d sap_d, c3_o mel_o, c3_o sof_o, c3_o ful_o) { - pid_t pid; - c3_i sat_i; - + // prepare args + // c3_c eve_c[21], sap_c[21] = { 0 }; if ( 0 > sprintf(eve_c, "%" PRIu64, eve_d) || 0 > sprintf(sap_c, "%" PRIu64, sap_d) ) @@ -2348,31 +2366,47 @@ _cw_play_fork(c3_d eve_d, c3_d sap_d, c3_o mel_o, c3_o sof_o, c3_o ful_o) }; c3_z i = 7; - if _(mel_o) { argv[i++] = "--auto-meld"; } - if _(sof_o) { argv[i++] = "--soft-mugs"; } - if _(ful_o) { argv[i++] = "--full"; } - argv[i] = NULL; - for (c3_i j = 0; j < i; j++) { - fprintf(stderr, "%s ", argv[j]); + // prepare a pipe for ipc with the subprocess + // + c3_i pipefd[2]; + if ( 0 != pipe(pipefd) ) { + fprintf(stderr, "play: failed to open pipe\r\n"); + return 1; } - fprintf(stderr, "\r\n"); - if ( 0 != posix_spawn(&pid, u3_Host.wrk_c, 0, 0, argv, 0) ) { - fprintf(stderr, "play: posix_spawn: %d\r\n", errno); - return 1; + // set the child process' stdin to read from the pipe + // + posix_spawn_file_actions_t action; + posix_spawn_file_actions_init(&action); + posix_spawn_file_actions_addclose(&action, pipefd[1]); + posix_spawn_file_actions_adddup2(&action, pipefd[0], STDIN_FILENO); + + // spawn a new serf process and call its play subcommand + // + pid_t pid; + if ( 0 != posix_spawn(&pid, u3_Host.wrk_c, &action, 0, argv, 0) ) { + fprintf(stderr, "play: posix_spawn: %d\r\n", errno); + return 1; } + // close the read end of the pipe in the parent + // + close(pipefd[0]); + + // wait for the child to exit + // + c3_i sat_i; if ( -1 == waitpid(pid, &sat_i, 0) ) { fprintf(stderr, "play: waitpid: %d\r\n", errno); return 1; @@ -2473,9 +2507,14 @@ _cw_play(c3_i argc, c3_c* argv[]) exit(1); } + pthread_t ted; + pthread_create(&ted, NULL, _cw_play_fork_heed, NULL); + if ( !_cw_play_impl(eve_d, sap_d, mel_o, sof_o, ful_o) ) { fprintf(stderr, "mars: nothing to do!\r\n"); } + + pthread_cancel(ted); } /* _cw_prep(): prepare for upgrade diff --git a/pkg/vere/mars.c b/pkg/vere/mars.c index db9056a0ab..dc592093b4 100644 --- a/pkg/vere/mars.c +++ b/pkg/vere/mars.c @@ -384,5 +384,6 @@ u3_mars_play(u3_mars* mar_u, c3_d eve_d, c3_d sap_d) u3l_log("---------------- playback complete ----------------"); u3m_save(); + fprintf(stderr, "mars: play finishing\r\n"); return pay_d; } From 5f41711ce2a9f37a661df91cf32bee81414338bb Mon Sep 17 00:00:00 2001 From: Matthew LeVan Date: Thu, 30 May 2024 12:34:11 -0400 Subject: [PATCH 077/107] play: flip loob --- pkg/vere/main.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pkg/vere/main.c b/pkg/vere/main.c index 4e7ae85b45..0fd9609f57 100644 --- a/pkg/vere/main.c +++ b/pkg/vere/main.c @@ -3213,7 +3213,7 @@ main(c3_i argc, // we need the current snapshot's latest event number to // validate whether we can execute disk migration if ( u3_Host.ops_u.nuu == c3n ) { - c3_i sat_i = _cw_play_fork(0, 0, c3n, c3n, c3y); + c3_i sat_i = _cw_play_fork(0, 0, c3n, c3n, c3n); if ( sat_i ) { fprintf(stderr, "play: replay failed: %d\r\n", sat_i); exit(sat_i); From 585fad2a190d9826a39af174c003630bdd06554c Mon Sep 17 00:00:00 2001 From: Matthew LeVan Date: Thu, 30 May 2024 12:40:08 -0400 Subject: [PATCH 078/107] mars: remove printf --- pkg/.DS_Store | Bin 0 -> 6148 bytes pkg/vere/mars.c | 1 - 2 files changed, 1 deletion(-) create mode 100644 pkg/.DS_Store diff --git a/pkg/.DS_Store b/pkg/.DS_Store new file mode 100644 index 0000000000000000000000000000000000000000..a22dbf756329df139a451c4e0b25a8948eadc384 GIT binary patch literal 6148 zcmeHK%}T>S5Z<-XrW7Fug&r5Y7OYx|;w8lT0!H+pQWFw17_%j5n?ot&tS{t~_&m<+ zZp31}ir5+0{pNQ!`$6`HF~+@VbikOy7_*@va#U6bx>traOfn+JF~YJKhp7y~elxMZ z4*2aBo3WVXEdKia;W$l-Nw4?LHyYOFR?}|UZTrrDl%+ojCey+T@*A`+rA)(G55lV? zE=SJJnan0ZlEt|yNTL{0Zm*LplBFl7SyZT6UkB`#-5NQa`TY2>*A@Lif6*256Qv&w z7K@g>ySIOOF?vd$Gx?$k<-oU+ZG$zug7UeZnV)B=%pSm3=hu0J!~iis3=jkB%YZo( z?DqOnKr1H(h=Cs%!2Ll$Lv#&R8r9YT9bTU?ZXu$8j&BJ>VbC>LX@m#}*QJ2El$$38 z*X7_BCeJlkY1HM6tC?XOGjsiT;c9m93zg2etC4zQfEZY2prK6%&;N7yWmZ1&mrKYZ z28e-w#sIg5{?LO(nX~oV^6;z`(C(q3U|xj^28PNN3)CUcHCSoHQP8i- Q0qG*32%(M`_yq<&050}QVgLXD literal 0 HcmV?d00001 diff --git a/pkg/vere/mars.c b/pkg/vere/mars.c index dc592093b4..db9056a0ab 100644 --- a/pkg/vere/mars.c +++ b/pkg/vere/mars.c @@ -384,6 +384,5 @@ u3_mars_play(u3_mars* mar_u, c3_d eve_d, c3_d sap_d) u3l_log("---------------- playback complete ----------------"); u3m_save(); - fprintf(stderr, "mars: play finishing\r\n"); return pay_d; } From 5212f9a266ddd738959311444e031d7591555ccd Mon Sep 17 00:00:00 2001 From: Matthew LeVan Date: Wed, 5 Jun 2024 11:30:16 -0400 Subject: [PATCH 079/107] play: handle termination cases --- pkg/vere/main.c | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/pkg/vere/main.c b/pkg/vere/main.c index 0fd9609f57..42b344bb7e 100644 --- a/pkg/vere/main.c +++ b/pkg/vere/main.c @@ -2412,7 +2412,21 @@ _cw_play_fork(c3_d eve_d, c3_d sap_d, c3_o mel_o, c3_o sof_o, c3_o ful_o) return 1; } - return WEXITSTATUS(sat_i); + if ( WIFEXITED(sat_i) ) { + c3_i ret_i = WEXITSTATUS(sat_i); + if ( 0 != ret_i ) { + fprintf(stderr, "play: exited with %d\r\n", ret_i); + } + return ret_i; + } + else if ( WIFSIGNALED(sat_i) ) { + fprintf(stderr, "play: terminated by signal %d\r\n", WTERMSIG(sat_i)); + return 1; + } + else { + fprintf(stderr, "play: strange termination\r\n"); + return 1; + } } /* _cw_play(): replay events, but better. From 9ab5c7e6b7289fc7c0da5660798f35ae36a51ffa Mon Sep 17 00:00:00 2001 From: Joe Bryan Date: Fri, 7 Jun 2024 11:16:31 -0400 Subject: [PATCH 080/107] ames: free packet on successful galaxy dns resolution --- pkg/vere/io/ames.c | 1 + 1 file changed, 1 insertion(+) diff --git a/pkg/vere/io/ames.c b/pkg/vere/io/ames.c index 7cd462b7d6..5400c26887 100644 --- a/pkg/vere/io/ames.c +++ b/pkg/vere/io/ames.c @@ -1135,6 +1135,7 @@ _ames_czar_cb(uv_getaddrinfo_t* adr_u, if ( sas_i == 0 ) { _ames_czar_here(pac_u, now, (struct sockaddr_in *)rai_u->ai_addr); + _ames_pact_free(pac_u); } else { _ames_czar_gone(pac_u->sam_u, now, pac_u->rut_u.imp_y, From 55eb4852cc608785ea71f6ddd5baadaac10caf2b Mon Sep 17 00:00:00 2001 From: ~dozreg-toplud Date: Thu, 20 Jun 2024 16:23:37 -0400 Subject: [PATCH 081/107] fix nock 9 crashing pier if axis is cell --- pkg/noun/nock.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pkg/noun/nock.c b/pkg/noun/nock.c index 452b04c309..eb459d17f7 100644 --- a/pkg/noun/nock.c +++ b/pkg/noun/nock.c @@ -1400,7 +1400,7 @@ _n_comp(u3_noun* ops, u3_noun fol, c3_o los_o, c3_o tel_o) case 9: u3x_cell(arg, &hed, &tel); - if ( (1 == hed) || (3 == u3qc_cap(hed)) ) { + if ( (1 == hed) || (3 == u3qc_cap(u3x_atom(hed))) ) { u3_noun mac = u3nq(7, u3k(tel), 2, u3nt(u3nc(0, 1), 0, u3k(hed))); tot_w += _n_comp(ops, mac, los_o, tel_o); u3z(mac); From 00f659e77b48d3d40cd0053351f119e410abf0aa Mon Sep 17 00:00:00 2001 From: pkova Date: Mon, 24 Jun 2024 15:54:58 +0300 Subject: [PATCH 082/107] ames: more accurate condition for cleaning up libnatpmp handle --- pkg/vere/io/ames.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/pkg/vere/io/ames.c b/pkg/vere/io/ames.c index 2da7f743c8..12466e0054 100644 --- a/pkg/vere/io/ames.c +++ b/pkg/vere/io/ames.c @@ -3202,7 +3202,8 @@ _ames_io_exit(u3_auto* car_u) uv_close((uv_handle_t*)&sam_u->sun_u.tim_u, 0); uv_close((uv_handle_t*)&sam_u->nat_u.tim_u, 0); - if (uv_is_active((uv_handle_t*)&sam_u->nat_u.pol_u)) { + uv_handle_type handle = uv_handle_get_type((uv_handle_t *)&sam_u->nat_u.pol_u); + if ( UV_UNKNOWN_HANDLE != handle) { uv_close((uv_handle_t*)&sam_u->nat_u.pol_u, 0); } } From 34809cd534b52156fd848d634e888b6bde7cffea Mon Sep 17 00:00:00 2001 From: Matthew LeVan Date: Mon, 24 Jun 2024 11:26:26 -0400 Subject: [PATCH 083/107] remove `pkg/.DS_Store` --- pkg/.DS_Store | Bin 6148 -> 0 bytes 1 file changed, 0 insertions(+), 0 deletions(-) delete mode 100644 pkg/.DS_Store diff --git a/pkg/.DS_Store b/pkg/.DS_Store deleted file mode 100644 index a22dbf756329df139a451c4e0b25a8948eadc384..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 6148 zcmeHK%}T>S5Z<-XrW7Fug&r5Y7OYx|;w8lT0!H+pQWFw17_%j5n?ot&tS{t~_&m<+ zZp31}ir5+0{pNQ!`$6`HF~+@VbikOy7_*@va#U6bx>traOfn+JF~YJKhp7y~elxMZ z4*2aBo3WVXEdKia;W$l-Nw4?LHyYOFR?}|UZTrrDl%+ojCey+T@*A`+rA)(G55lV? zE=SJJnan0ZlEt|yNTL{0Zm*LplBFl7SyZT6UkB`#-5NQa`TY2>*A@Lif6*256Qv&w z7K@g>ySIOOF?vd$Gx?$k<-oU+ZG$zug7UeZnV)B=%pSm3=hu0J!~iis3=jkB%YZo( z?DqOnKr1H(h=Cs%!2Ll$Lv#&R8r9YT9bTU?ZXu$8j&BJ>VbC>LX@m#}*QJ2El$$38 z*X7_BCeJlkY1HM6tC?XOGjsiT;c9m93zg2etC4zQfEZY2prK6%&;N7yWmZ1&mrKYZ z28e-w#sIg5{?LO(nX~oV^6;z`(C(q3U|xj^28PNN3)CUcHCSoHQP8i- Q0qG*32%(M`_yq<&050}QVgLXD From 81d3836358d288e227fd99b965e423f36620eaad Mon Sep 17 00:00:00 2001 From: Joe Bryan Date: Wed, 26 Jun 2024 12:54:27 -0400 Subject: [PATCH 084/107] ames: fix bugs in czar batch-dns resolution --- pkg/vere/io/ames.c | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/pkg/vere/io/ames.c b/pkg/vere/io/ames.c index c88b075168..496d439b80 100644 --- a/pkg/vere/io/ames.c +++ b/pkg/vere/io/ames.c @@ -2637,7 +2637,7 @@ _ames_czar_here(u3_ames* sam_u, c3_y imp_y, c3_w pip_w) (pip_w >> 0) & 0xff); } - sam_u->zar_u.pip_w[imp_y] = imp_y; + sam_u->zar_u.pip_w[imp_y] = pip_w; { c3_w blk_w = imp_y >> 5; @@ -2680,6 +2680,8 @@ _ames_czar_cb(uv_getaddrinfo_t* adr_u, _ames_czar_gone(sam_u, imp_y); } + sam_u->zar_u.pen_s--; + uv_freeaddrinfo(aif_u); c3_free(res_u); } @@ -2724,7 +2726,7 @@ _ames_czar_all(uv_timer_t* tim_u) sam_u->zar_u.pen_s = 256; - for ( c3_w i_w; i_w < 256; i_w++ ) { + for ( c3_w i_w = 0; i_w < 256; i_w++ ) { _ames_czar(sam_u, sam_u->zar_u.dom_c, (c3_y)i_w); } From 54137ad5e4b017671cc8e8be667bb88e0271dc64 Mon Sep 17 00:00:00 2001 From: Joe Bryan Date: Tue, 23 Apr 2024 09:58:32 -0400 Subject: [PATCH 085/107] c3: factors out sift/etch functions for scalars --- pkg/c3/defs.c | 14 +++++++ pkg/c3/defs.h | 54 +++++++++++++++++++++++++++ pkg/vere/io/ames.c | 92 ++++++++-------------------------------------- 3 files changed, 84 insertions(+), 76 deletions(-) diff --git a/pkg/c3/defs.c b/pkg/c3/defs.c index ce968cdab3..24c1d13c9b 100644 --- a/pkg/c3/defs.c +++ b/pkg/c3/defs.c @@ -1,5 +1,19 @@ #include "defs.h" +c3_s +c3_sift_short(c3_y buf_y[2]); +c3_w +c3_sift_word(c3_y buf_y[4]); +c3_d +c3_sift_chub(c3_y byt_y[8]); + +void +c3_etch_short(c3_y buf_y[2], c3_s sot_s); +void +c3_etch_word(c3_y buf_y[4], c3_w wod_w); +void +c3_etch_chub(c3_y byt_y[8], c3_d num_d); + c3_w c3_align_w(c3_w x, c3_w al, align_dir hilo); c3_d c3_align_d(c3_d x, c3_d al, align_dir hilo); void *c3_align_p(void const * p, size_t al, align_dir hilo); diff --git a/pkg/c3/defs.h b/pkg/c3/defs.h index bbf8d91ef7..aef4b6f93c 100644 --- a/pkg/c3/defs.h +++ b/pkg/c3/defs.h @@ -101,6 +101,60 @@ | (((w) >> 8) & 0xff) << 16 \ | ( (w) & 0xff) << 24 ) + inline c3_s + c3_sift_short(c3_y buf_y[2]) + { + return (buf_y[1] << 8 | buf_y[0]); + } + + inline c3_w + c3_sift_word(c3_y buf_y[4]) + { + return (buf_y[3] << 24 | buf_y[2] << 16 | buf_y[1] << 8 | buf_y[0]); + } + + inline c3_d + c3_sift_chub(c3_y byt_y[8]) + { + return (c3_d)byt_y[0] + | (c3_d)byt_y[1] << 8 + | (c3_d)byt_y[2] << 16 + | (c3_d)byt_y[3] << 24 + | (c3_d)byt_y[4] << 32 + | (c3_d)byt_y[5] << 40 + | (c3_d)byt_y[6] << 48 + | (c3_d)byt_y[7] << 56; + } + + inline void + c3_etch_short(c3_y buf_y[2], c3_s sot_s) + { + buf_y[0] = sot_s & 0xff; + buf_y[1] = (sot_s >> 8) & 0xff; + } + + inline void + c3_etch_word(c3_y buf_y[4], c3_w wod_w) + { + buf_y[0] = wod_w & 0xff; + buf_y[1] = (wod_w >> 8) & 0xff; + buf_y[2] = (wod_w >> 16) & 0xff; + buf_y[3] = (wod_w >> 24) & 0xff; + } + + inline void + c3_etch_chub(c3_y byt_y[8], c3_d num_d) + { + byt_y[0] = num_d & 0xff; + byt_y[1] = (num_d >> 8) & 0xff; + byt_y[2] = (num_d >> 16) & 0xff; + byt_y[3] = (num_d >> 24) & 0xff; + byt_y[4] = (num_d >> 32) & 0xff; + byt_y[5] = (num_d >> 40) & 0xff; + byt_y[6] = (num_d >> 48) & 0xff; + byt_y[7] = (num_d >> 56) & 0xff; + } + /* Asserting allocators. */ # define c3_free(s) free(s) diff --git a/pkg/vere/io/ames.c b/pkg/vere/io/ames.c index 66589f6aa5..3787c51242 100644 --- a/pkg/vere/io/ames.c +++ b/pkg/vere/io/ames.c @@ -434,34 +434,6 @@ _ames_check_mug(u3_pact* pac_u) ? c3y : c3n); } -static inline c3_s -_ames_sift_short(c3_y buf_y[2]) -{ - return (buf_y[1] << 8 | buf_y[0]); -} - -static inline c3_w -_ames_sift_word(c3_y buf_y[4]) -{ - return (buf_y[3] << 24 | buf_y[2] << 16 | buf_y[1] << 8 | buf_y[0]); -} - -/* _ames_chub_bytes(): c3_y[8] to c3_d -** XX factor out, deduplicate with other conversions -*/ -static inline c3_d -_ames_chub_bytes(c3_y byt_y[8]) -{ - return (c3_d)byt_y[0] - | (c3_d)byt_y[1] << 8 - | (c3_d)byt_y[2] << 16 - | (c3_d)byt_y[3] << 24 - | (c3_d)byt_y[4] << 32 - | (c3_d)byt_y[5] << 40 - | (c3_d)byt_y[6] << 48 - | (c3_d)byt_y[7] << 56; -} - /* _ames_ship_to_chubs(): pack [len_y] bytes into c3_d[2] */ static inline void @@ -470,24 +442,8 @@ _ames_ship_to_chubs(c3_d sip_d[2], c3_y len_y, c3_y* buf_y) c3_y sip_y[16] = {0}; memcpy(sip_y, buf_y, c3_min(16, len_y)); - sip_d[0] = _ames_chub_bytes(sip_y); - sip_d[1] = _ames_chub_bytes(sip_y + 8); -} - -/* _ames_chub_bytes(): c3_d to c3_y[8] -** XX factor out, deduplicate with other conversions -*/ -static inline void -_ames_bytes_chub(c3_y byt_y[8], c3_d num_d) -{ - byt_y[0] = num_d & 0xff; - byt_y[1] = (num_d >> 8) & 0xff; - byt_y[2] = (num_d >> 16) & 0xff; - byt_y[3] = (num_d >> 24) & 0xff; - byt_y[4] = (num_d >> 32) & 0xff; - byt_y[5] = (num_d >> 40) & 0xff; - byt_y[6] = (num_d >> 48) & 0xff; - byt_y[7] = (num_d >> 56) & 0xff; + sip_d[0] = c3_sift_chub(sip_y); + sip_d[1] = c3_sift_chub(sip_y + 8); } /* _ames_ship_of_chubs(): unpack c3_d[2] into [len_y] bytes. @@ -497,8 +453,8 @@ _ames_ship_of_chubs(c3_d sip_d[2], c3_y len_y, c3_y* buf_y) { c3_y sip_y[16] = {0}; - _ames_bytes_chub(sip_y, sip_d[0]); - _ames_bytes_chub(sip_y + 8, sip_d[1]); + c3_etch_chub(sip_y, sip_d[0]); + c3_etch_chub(sip_y + 8, sip_d[1]); memcpy(buf_y, sip_y, c3_min(16, len_y)); } @@ -508,7 +464,7 @@ _ames_ship_of_chubs(c3_d sip_d[2], c3_y len_y, c3_y* buf_y) static void _ames_sift_head(u3_head* hed_u, c3_y buf_y[4]) { - c3_w hed_w = _ames_sift_word(buf_y); + c3_w hed_w = c3_sift_word(buf_y); // first two bits are reserved // @@ -536,7 +492,7 @@ _ames_sift_prel(u3_head* hed_u, if ( c3y == hed_u->rel_o ) { c3_y rag_y[8] = {0}; memcpy(rag_y, buf_y + cur_w, 6); - pre_u->rog_d = _ames_chub_bytes(rag_y); + pre_u->rog_d = c3_sift_chub(rag_y); cur_w += 6; } else { @@ -589,12 +545,12 @@ _fine_sift_wail(u3_pact* pac_u, c3_w cur_w) // parse fragment number // - pac_u->wal_u.pep_u.fra_w = _ames_sift_word(pac_u->hun_y + cur_w); + pac_u->wal_u.pep_u.fra_w = c3_sift_word(pac_u->hun_y + cur_w); cur_w += fra_w; // parse path length field // - len_s = _ames_sift_short(pac_u->hun_y + cur_w); + len_s = c3_sift_short(pac_u->hun_y + cur_w); pac_u->wal_u.pep_u.len_s = len_s; cur_w += len_w; @@ -670,22 +626,6 @@ _fine_sift_meow(u3_meow* mew_u, u3_noun mew) return ret_o; } -static void -_ames_etch_short(c3_y buf_y[2], c3_s sot_s) -{ - buf_y[0] = sot_s & 0xff; - buf_y[1] = (sot_s >> 8) & 0xff; -} - -static void -_ames_etch_word(c3_y buf_y[4], c3_w wod_w) -{ - buf_y[0] = wod_w & 0xff; - buf_y[1] = (wod_w >> 8) & 0xff; - buf_y[2] = (wod_w >> 16) & 0xff; - buf_y[3] = (wod_w >> 24) & 0xff; -} - /* _ames_etch_head(): serialize packet header. */ static void @@ -703,14 +643,14 @@ _ames_etch_head(u3_head* hed_u, c3_y buf_y[4]) ^ ((hed_u->mug_l & 0xfffff) << 11) ^ ((hed_u->rel_o & 0x1) << 31); - _ames_etch_word(buf_y, hed_w); + c3_etch_word(buf_y, hed_w); } static void _ames_etch_origin(c3_d rog_d, c3_y* buf_y) { c3_y rag_y[8] = {0}; - _ames_bytes_chub(rag_y, rog_d); + c3_etch_chub(rag_y, rog_d); memcpy(buf_y, rag_y, 6); } @@ -755,12 +695,12 @@ _fine_etch_peep(u3_peep* pep_u, c3_y* buf_y) // write fragment number // - _ames_etch_word(buf_y + cur_w, pep_u->fra_w); + c3_etch_word(buf_y + cur_w, pep_u->fra_w); cur_w += sizeof(pep_u->fra_w); // write path length // - _ames_etch_short(buf_y + cur_w, pep_u->len_s); + c3_etch_short(buf_y + cur_w, pep_u->len_s); cur_w += sizeof(pep_u->len_s); // write request path @@ -787,7 +727,7 @@ _fine_etch_meow(u3_meow* mew_u, c3_y* buf_y) // write number of fragments // - _ames_etch_word(num_y, mew_u->num_w); + c3_etch_word(num_y, mew_u->num_w); memcpy(buf_y + cur_w, num_y, len_y); if (mew_u->siz_s != 0) { @@ -1435,8 +1375,8 @@ _stun_find_xor_mapped_address(c3_y* buf_y, c3_w buf_len, u3_lane* lan_u) cur += 2; - lan_u->por_s = ntohs(_ames_sift_short(buf_y + cur)) ^ (cookie >> 16); - lan_u->pip_w = ntohl(_ames_sift_word(buf_y + cur + 2)) ^ cookie; + lan_u->por_s = ntohs(c3_sift_short(buf_y + cur)) ^ (cookie >> 16); + lan_u->pip_w = ntohl(c3_sift_word(buf_y + cur + 2)) ^ cookie; if ( u3C.wag_w & u3o_verbose ) { c3_w nip_w = htonl(lan_u->pip_w); @@ -1465,7 +1405,7 @@ _stun_has_fingerprint(c3_y* buf_y, c3_w buf_len) if ( fin_y != 0 ) { c3_w len_w = fin_y - buf_y; // Skip attribute type and length - c3_w fingerprint = _ames_sift_word(fin_y + sizeof(ned_y)); + c3_w fingerprint = c3_sift_word(fin_y + sizeof(ned_y)); c3_w init = crc32(0L, Z_NULL, 0); c3_w crc = htonl(crc32(init, buf_y, len_w) ^ 0x5354554e); if ((fingerprint == crc) && (fin_y - buf_y + 8) == buf_len) { From 8480a3c97d7b9dcb9925cd40dbe2d0c19f578c6e Mon Sep 17 00:00:00 2001 From: Joe Bryan Date: Tue, 23 Apr 2024 10:22:37 -0400 Subject: [PATCH 086/107] ames: factors out stun protocol library --- pkg/vere/BUILD.bazel | 2 + pkg/vere/ames_tests.c | 4 +- pkg/vere/io/ames.c | 363 ++++++++++++---------------------------- pkg/vere/io/ames/stun.c | 187 +++++++++++++++++++++ pkg/vere/io/ames/stun.h | 35 ++++ 5 files changed, 329 insertions(+), 262 deletions(-) create mode 100644 pkg/vere/io/ames/stun.c create mode 100644 pkg/vere/io/ames/stun.h diff --git a/pkg/vere/BUILD.bazel b/pkg/vere/BUILD.bazel index 6303b87201..33882e9244 100644 --- a/pkg/vere/BUILD.bazel +++ b/pkg/vere/BUILD.bazel @@ -96,6 +96,8 @@ vere_library( "*.h", "db/*.c", "io/*.c", + "io/*/*.h", + "io/*/*.c", ], exclude = [ "main.c", diff --git a/pkg/vere/ames_tests.c b/pkg/vere/ames_tests.c index cb19fd4c5e..e76622fb78 100644 --- a/pkg/vere/ames_tests.c +++ b/pkg/vere/ames_tests.c @@ -39,11 +39,11 @@ _test_stun_addr_roundtrip(u3_lane* inn_u) c3_y req_y[20] = {0}; c3_i ret_i = 0; - _stun_make_response(req_y, inn_u, rep_y); + u3_stun_make_response(req_y, inn_u, rep_y); u3_lane lan_u; - if ( c3n == _stun_find_xor_mapped_address(rep_y, sizeof(rep_y), &lan_u) ) { + if ( c3n == u3_stun_find_xor_mapped_address(rep_y, sizeof(rep_y), &lan_u) ) { fprintf(stderr, "stun: failed to find addr in response\r\n"); ret_i = 1; } diff --git a/pkg/vere/io/ames.c b/pkg/vere/io/ames.c index 3787c51242..6a58761402 100644 --- a/pkg/vere/io/ames.c +++ b/pkg/vere/io/ames.c @@ -2,6 +2,7 @@ #include "vere.h" #include "mdns.h" +#include "io/ames/stun.h" #include "noun.h" #include "ur.h" @@ -1072,89 +1073,14 @@ _fine_put_cache(u3_ames* sam_u, u3_noun pax, c3_w lop_w, u3_noun lis) } } -// XX (code reordering?) forward declarations -static void _stun_send_request(u3_ames*); -static void _stun_on_lost(u3_ames* sam_u); -static void _stun_start(u3_ames* sam_u, c3_w tim_w); -static c3_y* _stun_add_fingerprint(c3_y *message, c3_w index); -static c3_o _stun_find_xor_mapped_address(c3_y* buf_y, c3_w buf_len, u3_lane* lan_u); - -static c3_d -_stun_time_gap(struct timeval start) -{ - struct timeval tim_tv; - gettimeofday(&tim_tv, 0); - u3_noun now = u3_time_in_tv(&tim_tv); - u3_noun den = u3_time_in_tv(&start); - return u3_time_gap_ms(den, now); -} - -/* _stun_reset(): stun failed. start again using max backoff - */ -static void -_stun_reset(uv_timer_t* tim_u) -{ - u3_ames* sam_u = (u3_ames*)(tim_u->data); - - _stun_start(sam_u, 39000); -} - -static void -_stun_timer_cb(uv_timer_t* tim_u) -{ - u3_ames* sam_u = (u3_ames*)(tim_u->data); - c3_w rto_w = 500; - - switch ( sam_u->sun_u.sat_y ) { - case STUN_OFF: { - // ignore; stray timer (although this shouldn't happen) - u3l_log("stun: stray timer STUN_OFF"); - } break; - - case STUN_KEEPALIVE: { - u3_lane* lan_u = &(sam_u->sun_u.lan_u); - c3_y imp_y = sam_u->sun_u.dad_y; - - if ( c3n == _ames_czar_lane(sam_u, imp_y, lan_u) ) { - uv_timer_start(&sam_u->sun_u.tim_u, _stun_timer_cb, 25*1000, 0); - } - else { - sam_u->sun_u.sat_y = STUN_TRYING; - gettimeofday(&sam_u->sun_u.sar_u, 0); // set start time to now - uv_timer_start(&sam_u->sun_u.tim_u, _stun_timer_cb, rto_w, 0); - _stun_send_request(sam_u); - } - } break; - - case STUN_TRYING: { - c3_d gap_d = _stun_time_gap(sam_u->sun_u.sar_u); - c3_d nex_d = (gap_d * 2) + rto_w - gap_d; - - if ( gap_d >= 39500 ) { - _stun_on_lost(sam_u); - } - else { - // wait ~s8 for the last STUN request - // - // https://datatracker.ietf.org/doc/html/rfc5389#section-7.2.1 - // - c3_w tim_w = (gap_d >= 31500) ? 8000 : c3_max(nex_d, 31500); - - uv_timer_start(&sam_u->sun_u.tim_u, _stun_timer_cb, tim_w, 0); - _stun_send_request(sam_u); - } - } break; - - default: u3_assert(!"programmer error"); - } -} - typedef struct _stun_send { uv_udp_send_t req_u; // uv udp request handle u3_ames* sam_u; // backpointer to driver state c3_y hun_y[0]; // buffer } _stun_send; +/* _stun_send_cb(): stun udp send callback. + */ static void _stun_send_cb(uv_udp_send_t *rep_u, c3_i sas_i) { @@ -1172,38 +1098,8 @@ _stun_send_cb(uv_udp_send_t *rep_u, c3_i sas_i) c3_free(snd_u); } -static void -_stun_make_response(const c3_y req_y[20], - u3_lane* lan_u, - c3_y buf_y[40]) -{ - c3_w cok_w = 0x2112A442; - c3_w cur_w = 20; - - // XX hardcoded to match the requests we produce - // - memcpy(buf_y, req_y, cur_w); - - memset(buf_y + cur_w, 0, cur_w); - - // XOR-MAPPED-ADDRESS - buf_y[cur_w + 0] = 0x00; // - buf_y[cur_w + 1] = 0x20; // attribute type 0x00020 - buf_y[cur_w + 2] = 0x00; // - buf_y[cur_w + 3] = 0x08; // STUN attribute length - buf_y[cur_w + 4] = 0x00; // extra reserved 0x0 byte - buf_y[cur_w + 5] = 0x01; // family 0x01:IPv4 - - c3_s por_s = htons(lan_u->por_s ^ (cok_w >> 16)); - c3_w pip_w = htonl(lan_u->pip_w ^ cok_w); - - memcpy(buf_y + cur_w + 6, &por_s, 2); // X-Port - memcpy(buf_y + cur_w + 8, &pip_w, 4); // X-IP Addres - - // FINGERPRINT - _stun_add_fingerprint(buf_y, cur_w + 12); -} - +/* _stun_on_request(): hear stun request, send response. + */ static void _stun_on_request(u3_ames* sam_u, const c3_y* req_y, @@ -1217,7 +1113,7 @@ _stun_on_request(u3_ames* sam_u, .por_s = ntohs(add_u->sin_port), .pip_w = ntohl(add_u->sin_addr.s_addr) }; - _stun_make_response(req_y, &lan_u, snd_u->hun_y); + u3_stun_make_response(req_y, &lan_u, snd_u->hun_y); uv_buf_t buf_u = uv_buf_init((c3_c*)snd_u->hun_y, 40); c3_i sas_i = uv_udp_send(&snd_u->req_u, &sam_u->wax_u, @@ -1228,13 +1124,18 @@ _stun_on_request(u3_ames* sam_u, } } +static void +_stun_start(u3_ames* sam_u, c3_w tim_w); + +/* _stun_on_response(): hear stun response from galaxy. + */ static void _stun_on_response(u3_ames* sam_u, c3_y* buf_y, c3_w buf_len) { u3_lane lan_u; // Ignore STUN responses that dont' have the XOR-MAPPED-ADDRESS attribute - if ( c3n == _stun_find_xor_mapped_address(buf_y, buf_len, &lan_u) ) { + if ( c3n == u3_stun_find_xor_mapped_address(buf_y, buf_len, &lan_u) ) { return; } @@ -1274,48 +1175,8 @@ _stun_on_response(u3_ames* sam_u, c3_y* buf_y, c3_w buf_len) } } -static void -_stun_on_lost(u3_ames* sam_u) -{ - sam_u->sun_u.sat_y = STUN_OFF; - - // only inject event into arvo to %kick ping app on first failure - // - if ( c3y == sam_u->sun_u.wok_o ) { - u3_noun wir = u3nc(c3__ames, u3_nul); - u3_noun cad = u3nq(c3__stun, c3__fail, sam_u->sun_u.dad_y, - u3nc(c3n, u3_ames_encode_lane(sam_u->sun_u.sef_u))); - u3_auto_plan(&sam_u->car_u, - u3_ovum_init(0, c3__ames, wir, cad)); - sam_u->sun_u.wok_o = c3n; - } - - uv_timer_start(&sam_u->sun_u.tim_u, _stun_reset, 5*1000, 0); -} - -static void -_stun_make_request(c3_y buf_y[28], c3_y tid_y[12]) -{ - // see STUN RFC 8489 - // https://datatracker.ietf.org/doc/html/rfc8489#section-5 - memset(buf_y, 0, 28); - - // STUN message type: "binding request" - buf_y[1] = 0x01; - - // STUN message length: 8 (header and 32-bit FINGERPRINT) - buf_y[2] = 0x00; buf_y[3] = 0x08; - - // STUN "magic cookie" 0x2112A442 in network byte order - buf_y[4] = 0x21; buf_y[5] = 0x12; buf_y[6] = 0xa4; buf_y[7] = 0x42; - - // STUN "transaction id" - memcpy(buf_y + 8, tid_y, 12); - - // FINGERPRINT - _stun_add_fingerprint(buf_y, 20); -} - +/* _stun_send_request(): send stun request to galaxy lane. + */ static void _stun_send_request(u3_ames* sam_u) { @@ -1324,7 +1185,7 @@ _stun_send_request(u3_ames* sam_u) _stun_send* snd_u = c3_malloc(sizeof(*snd_u) + 28); snd_u->sam_u = sam_u; - _stun_make_request(snd_u->hun_y, sam_u->sun_u.tid_y); + u3_stun_make_request(snd_u->hun_y, sam_u->sun_u.tid_y); struct sockaddr_in add_u; memset(&add_u, 0, sizeof(add_u)); @@ -1341,136 +1202,117 @@ _stun_send_request(u3_ames* sam_u) } } +/* _stun_reset(): stun failed. start again using max backoff + */ static void -_stun_start(u3_ames* sam_u, c3_w tim_w) +_stun_reset(uv_timer_t* tim_u) { - if ( ent_getentropy(sam_u->sun_u.tid_y, 12) ) { - u3l_log("stun: getentropy fail: %s", strerror(errno)); - u3_king_bail(); - } + u3_ames* sam_u = (u3_ames*)(tim_u->data); - sam_u->sun_u.sat_y = STUN_KEEPALIVE; - uv_timer_start(&sam_u->sun_u.tim_u, _stun_timer_cb, tim_w, 0); + _stun_start(sam_u, 39000); } -static c3_o -_stun_find_xor_mapped_address(c3_y* buf_y, c3_w buf_len, u3_lane* lan_u) +/* _stun_on_lost(): stun failed (timeout); capture and reset. + */ +static void +_stun_on_lost(u3_ames* sam_u) { - c3_y xor_y[4] = {0x00, 0x20, 0x00, 0x08}; - c3_w cookie = 0x2112A442; + sam_u->sun_u.sat_y = STUN_OFF; - if (buf_len < 40) { // At least STUN header, XOR-MAPPED-ADDRESS & FINGERPRINT - return c3n; + // only inject event into arvo to %kick ping app on first failure + // + if ( c3y == sam_u->sun_u.wok_o ) { + u3_noun wir = u3nc(c3__ames, u3_nul); + u3_noun cad = u3nq(c3__stun, c3__fail, sam_u->sun_u.dad_y, + u3nc(c3n, u3_ames_encode_lane(sam_u->sun_u.sef_u))); + u3_auto_plan(&sam_u->car_u, + u3_ovum_init(0, c3__ames, wir, cad)); + sam_u->sun_u.wok_o = c3n; } - c3_w i = 20; // start after header - - c3_y* fin_y = memmem(buf_y + i, buf_len - i, xor_y, sizeof(xor_y)); - if ( fin_y != 0 ) { - c3_w cur = (c3_w)(fin_y - buf_y) + sizeof(xor_y); - - if ( (buf_y[cur] != 0x0) && (buf_y[cur+1] != 0x1) ) { - return c3n; - } - - cur += 2; - - lan_u->por_s = ntohs(c3_sift_short(buf_y + cur)) ^ (cookie >> 16); - lan_u->pip_w = ntohl(c3_sift_word(buf_y + cur + 2)) ^ cookie; - - if ( u3C.wag_w & u3o_verbose ) { - c3_w nip_w = htonl(lan_u->pip_w); - c3_c nip_c[INET_ADDRSTRLEN]; - inet_ntop(AF_INET, &nip_w, nip_c, INET_ADDRSTRLEN); - u3l_log("stun: hear ip:port %s:%u", nip_c, lan_u->por_s); - } - return c3y; - } - return c3n; + uv_timer_start(&sam_u->sun_u.tim_u, _stun_reset, 5*1000, 0); } -static c3_o -_stun_has_fingerprint(c3_y* buf_y, c3_w buf_len) +/* _stun_time_gap(): elapsed milliseconds. + */ +static c3_d +_stun_time_gap(struct timeval sar_tv) { - c3_y ned_y[4] = {0x80, 0x28, 0x00, 0x04}; - if ( buf_len < 28 ) { // At least STUN header and FINGERPRINT - return c3n; - } - - { - c3_y* fin_y = 0; - c3_w i = 20; // start after the header - - fin_y = memmem(buf_y + i, buf_len - i, ned_y, sizeof(ned_y)); - if ( fin_y != 0 ) { - c3_w len_w = fin_y - buf_y; - // Skip attribute type and length - c3_w fingerprint = c3_sift_word(fin_y + sizeof(ned_y)); - c3_w init = crc32(0L, Z_NULL, 0); - c3_w crc = htonl(crc32(init, buf_y, len_w) ^ 0x5354554e); - if ((fingerprint == crc) && (fin_y - buf_y + 8) == buf_len) { - return c3y; - } - } - - return c3n; - } + struct timeval tim_tv; + gettimeofday(&tim_tv, 0); + u3_noun now = u3_time_in_tv(&tim_tv); + u3_noun den = u3_time_in_tv(&sar_tv); + return u3_time_gap_ms(den, now); } -static c3_y* -_stun_add_fingerprint(c3_y *message, c3_w index) +/* _stun_timer_cb(): advance stun state machine. + */ +static void +_stun_timer_cb(uv_timer_t* tim_u) { - // Compute FINGERPRINT value as CRC-32 of the STUN message - // up to (but excluding) the FINGERPRINT attribute itself, - // XOR'ed with the 32-bit value 0x5354554e - c3_w init = crc32(0L, Z_NULL, 0); - c3_w crc = htonl(crc32(init, message, index) ^ 0x5354554e); + u3_ames* sam_u = (u3_ames*)(tim_u->data); + c3_w rto_w = 500; - // STUN attribute type: "FINGERPRINT" - message[index] = 0x80; message[index + 1] = 0x28; - // STUN attribute length: 4 bytes - message[index + 2] = 0x00; message[index + 3] = 0x04; + switch ( sam_u->sun_u.sat_y ) { + case STUN_OFF: { + // ignore; stray timer (although this shouldn't happen) + u3l_log("stun: stray timer STUN_OFF"); + } break; - memcpy(message + index + 4, &crc, 4); + case STUN_KEEPALIVE: { + u3_lane* lan_u = &(sam_u->sun_u.lan_u); + c3_y imp_y = sam_u->sun_u.dad_y; - return message; -} + if ( c3n == _ames_czar_lane(sam_u, imp_y, lan_u) ) { + uv_timer_start(&sam_u->sun_u.tim_u, _stun_timer_cb, 25*1000, 0); + } + else { + sam_u->sun_u.sat_y = STUN_TRYING; + gettimeofday(&sam_u->sun_u.sar_u, 0); // set start time to now + uv_timer_start(&sam_u->sun_u.tim_u, _stun_timer_cb, rto_w, 0); + _stun_send_request(sam_u); + } + } break; -static c3_o -_stun_is_our_response(c3_y* buf_y, c3_y tid_y[12], c3_w buf_len) -{ - c3_w cookie = htonl(0x2112A442); + case STUN_TRYING: { + c3_d gap_d = _stun_time_gap(sam_u->sun_u.sar_u); + c3_d nex_d = (gap_d * 2) + rto_w - gap_d; - // Expects at least: - // STUN header, 12 byte XOR-MAPPED-ADDRESS and 8 byte FINGERPRINT - if ( (buf_len == 40) && - (buf_y[0] == 0x01 && buf_y[1] == 0x01) && - (memcmp(&cookie, buf_y + 4, 4) == 0) && - (memcmp(tid_y, buf_y + 8, 12) == 0) && - (c3y == _stun_has_fingerprint(buf_y, buf_len)) ) - { - return c3y; + if ( gap_d >= 39500 ) { + _stun_on_lost(sam_u); + } + else { + // wait ~s8 for the last STUN request + // + // https://datatracker.ietf.org/doc/html/rfc5389#section-7.2.1 + // + c3_w tim_w = (gap_d >= 31500) ? 8000 : c3_max(nex_d, 31500); + + uv_timer_start(&sam_u->sun_u.tim_u, _stun_timer_cb, tim_w, 0); + _stun_send_request(sam_u); + } + } break; + + default: u3_assert(!"programmer error"); } - return c3n; } -static c3_o -_stun_is_request(c3_y* buf_y, c3_w buf_len) +/* _stun_start(): begin/restart STUN state machine. +*/ +static void +_stun_start(u3_ames* sam_u, c3_w tim_w) { - c3_w cookie = htonl(0x2112A442); - - // Expects at least: - // STUN header and 8 byte FINGERPRINT - if ( (buf_len >= 28) && - (buf_y[0] == 0x0 && buf_y[1] == 0x01) && - (memcmp(&cookie, buf_y + 4, 4) == 0) && - (c3y == _stun_has_fingerprint(buf_y, buf_len)) ) - { - return c3y; + if ( ent_getentropy(sam_u->sun_u.tid_y, 12) ) { + u3l_log("stun: getentropy fail: %s", strerror(errno)); + u3_king_bail(); } - return c3n; + + sam_u->sun_u.sat_y = STUN_KEEPALIVE; + uv_timer_start(&sam_u->sun_u.tim_u, _stun_timer_cb, tim_w, 0); } +/* _ames_is_czar(): [who] is galaxy. +*/ static c3_o _ames_is_czar(u3_noun who) { @@ -2397,12 +2239,13 @@ _ames_recv_cb(uv_udp_t* wax_u, // check ames first, assume that STUN could maybe (not likely) overlap with ames // for next protocol version, have an urbit cookie // - else if (_stun_is_request((c3_y*)buf_u->base, nrd_i) == c3y) { + else if ( c3y == u3_stun_is_request((c3_y*)buf_u->base, nrd_i) ) { _stun_on_request(sam_u, (c3_y *)buf_u->base, adr_u); c3_free(buf_u->base); } - else if (_stun_is_our_response((c3_y*)buf_u->base, sam_u->sun_u.tid_y, nrd_i) - == c3y) { + else if ( c3y == u3_stun_is_our_response((c3_y*)buf_u->base, + sam_u->sun_u.tid_y, nrd_i) ) + { _stun_on_response(sam_u, (c3_y*)buf_u->base, nrd_i); c3_free(buf_u->base); } diff --git a/pkg/vere/io/ames/stun.c b/pkg/vere/io/ames/stun.c new file mode 100644 index 0000000000..e1f314ef07 --- /dev/null +++ b/pkg/vere/io/ames/stun.c @@ -0,0 +1,187 @@ +#include "vere.h" +#include "zlib.h" + +static c3_y* +_stun_add_fingerprint(c3_y *message, c3_w index) +{ + // Compute FINGERPRINT value as CRC-32 of the STUN message + // up to (but excluding) the FINGERPRINT attribute itself, + // XOR'ed with the 32-bit value 0x5354554e + c3_w init = crc32(0L, Z_NULL, 0); + c3_w crc = htonl(crc32(init, message, index) ^ 0x5354554e); + + // STUN attribute type: "FINGERPRINT" + message[index] = 0x80; message[index + 1] = 0x28; + // STUN attribute length: 4 bytes + message[index + 2] = 0x00; message[index + 3] = 0x04; + + memcpy(message + index + 4, &crc, 4); + + return message; +} + +static c3_o +_stun_has_fingerprint(c3_y* buf_y, c3_w len_w) +{ + c3_y ned_y[4] = {0x80, 0x28, 0x00, 0x04}; + if ( len_w < 28 ) { // At least STUN header and FINGERPRINT + return c3n; + } + + { + c3_y* fin_y = 0; + c3_w i = 20; // start after the header + + fin_y = memmem(buf_y + i, len_w - i, ned_y, sizeof(ned_y)); + if ( fin_y != 0 ) { + c3_w len_w = fin_y - buf_y; + // Skip attribute type and length + c3_w fingerprint = c3_sift_word(fin_y + sizeof(ned_y)); + c3_w init = crc32(0L, Z_NULL, 0); + c3_w crc = htonl(crc32(init, buf_y, len_w) ^ 0x5354554e); + if ((fingerprint == crc) && (fin_y - buf_y + 8) == len_w) { + return c3y; + } + } + + return c3n; + } +} + +/* u3_stun_is_request(): buffer is a stun request. +*/ +c3_o +u3_stun_is_request(c3_y* buf_y, c3_w len_w) +{ + c3_w cookie = htonl(0x2112A442); + + // Expects at least: + // STUN header and 8 byte FINGERPRINT + if ( (len_w >= 28) && + (buf_y[0] == 0x0 && buf_y[1] == 0x01) && + (memcmp(&cookie, buf_y + 4, 4) == 0) && + (c3y == _stun_has_fingerprint(buf_y, len_w)) ) + { + return c3y; + } + return c3n; +} + +/* u3_stun_is_our_response(): buffer is a response to our request. +*/ +c3_o +u3_stun_is_our_response(c3_y* buf_y, c3_y tid_y[12], c3_w len_w) +{ + c3_w cookie = htonl(0x2112A442); + + // Expects at least: + // STUN header, 12 byte XOR-MAPPED-ADDRESS and 8 byte FINGERPRINT + if ( (len_w == 40) && + (buf_y[0] == 0x01 && buf_y[1] == 0x01) && + (memcmp(&cookie, buf_y + 4, 4) == 0) && + (memcmp(tid_y, buf_y + 8, 12) == 0) && + (c3y == _stun_has_fingerprint(buf_y, len_w)) ) + { + return c3y; + } + return c3n; +} + +/* u3_stun_make_request(): serialize stun request. +*/ +void +u3_stun_make_request(c3_y buf_y[28], c3_y tid_y[12]) +{ + // see STUN RFC 8489 + // https://datatracker.ietf.org/doc/html/rfc8489#section-5 + memset(buf_y, 0, 28); + + // STUN message type: "binding request" + buf_y[1] = 0x01; + + // STUN message length: 8 (header and 32-bit FINGERPRINT) + buf_y[2] = 0x00; buf_y[3] = 0x08; + + // STUN "magic cookie" 0x2112A442 in network byte order + buf_y[4] = 0x21; buf_y[5] = 0x12; buf_y[6] = 0xa4; buf_y[7] = 0x42; + + // STUN "transaction id" + memcpy(buf_y + 8, tid_y, 12); + + // FINGERPRINT + _stun_add_fingerprint(buf_y, 20); +} + +/* u3_stun_make_response(): serialize stun response from request. +*/ +void +u3_stun_make_response(const c3_y req_y[20], + u3_lane* lan_u, + c3_y buf_y[40]) +{ + c3_w cok_w = 0x2112A442; + c3_w cur_w = 20; + + // XX hardcoded to match the requests we produce + // + memcpy(buf_y, req_y, cur_w); + + memset(buf_y + cur_w, 0, cur_w); + + // XOR-MAPPED-ADDRESS + buf_y[cur_w + 0] = 0x00; // + buf_y[cur_w + 1] = 0x20; // attribute type 0x00020 + buf_y[cur_w + 2] = 0x00; // + buf_y[cur_w + 3] = 0x08; // STUN attribute length + buf_y[cur_w + 4] = 0x00; // extra reserved 0x0 byte + buf_y[cur_w + 5] = 0x01; // family 0x01:IPv4 + + c3_s por_s = htons(lan_u->por_s ^ (cok_w >> 16)); + c3_w pip_w = htonl(lan_u->pip_w ^ cok_w); + + memcpy(buf_y + cur_w + 6, &por_s, 2); // X-Port + memcpy(buf_y + cur_w + 8, &pip_w, 4); // X-IP Addres + + // FINGERPRINT + _stun_add_fingerprint(buf_y, cur_w + 12); +} + +/* u3_stun_find_xor_mapped_address(): extract lane from response. +*/ +c3_o +u3_stun_find_xor_mapped_address(c3_y* buf_y, + c3_w len_w, + u3_lane* lan_u) +{ + c3_y xor_y[4] = {0x00, 0x20, 0x00, 0x08}; + c3_w cookie = 0x2112A442; + + if ( len_w < 40 ) { // At least STUN header, XOR-MAPPED-ADDRESS & FINGERPRINT + return c3n; + } + + c3_w i = 20; // start after header + + c3_y* fin_y = memmem(buf_y + i, len_w - i, xor_y, sizeof(xor_y)); + if ( fin_y != 0 ) { + c3_w cur = (c3_w)(fin_y - buf_y) + sizeof(xor_y); + + if ( (buf_y[cur] != 0x0) && (buf_y[cur+1] != 0x1) ) { + return c3n; + } + + cur += 2; + + lan_u->por_s = ntohs(c3_sift_short(buf_y + cur)) ^ (cookie >> 16); + lan_u->pip_w = ntohl(c3_sift_word(buf_y + cur + 2)) ^ cookie; + + if ( u3C.wag_w & u3o_verbose ) { + c3_w nip_w = htonl(lan_u->pip_w); + c3_c nip_c[INET_ADDRSTRLEN]; + inet_ntop(AF_INET, &nip_w, nip_c, INET_ADDRSTRLEN); + u3l_log("stun: hear ip:port %s:%u", nip_c, lan_u->por_s); + } + return c3y; + } + return c3n; +} diff --git a/pkg/vere/io/ames/stun.h b/pkg/vere/io/ames/stun.h new file mode 100644 index 0000000000..0fcaff6466 --- /dev/null +++ b/pkg/vere/io/ames/stun.h @@ -0,0 +1,35 @@ +#include "vere.h" + +#ifndef U3_STUN_H +#define U3_STUN_H + + /* u3_stun_is_request(): buffer is a stun request. + */ + c3_o + u3_stun_is_request(c3_y* buf_y, c3_w len_w); + + /* u3_stun_is_our_response(): buffer is a response to our request. + */ + c3_o + u3_stun_is_our_response(c3_y* buf_y, c3_y tid_y[12], c3_w len_w); + + /* u3_stun_make_request(): serialize stun request. + */ + void + u3_stun_make_request(c3_y buf_y[28], c3_y tid_y[12]); + + /* u3_stun_make_response(): serialize stun response from request. + */ + void + u3_stun_make_response(const c3_y req_y[20], + u3_lane* lan_u, + c3_y buf_y[40]); + + /* u3_stun_find_xor_mapped_address(): extract lane from response. + */ + c3_o + u3_stun_find_xor_mapped_address(c3_y* buf_y, + c3_w len_w, + u3_lane* lan_u); + +#endif /* ifndef U3_STUN_H */ From 34e43e1179ed800e9600d2339c03ecd5858ec6da Mon Sep 17 00:00:00 2001 From: Joe Bryan Date: Tue, 23 Apr 2024 12:15:30 -0400 Subject: [PATCH 087/107] ames: removes old logging code --- pkg/vere/io/ames.c | 75 ---------------------------------------------- 1 file changed, 75 deletions(-) diff --git a/pkg/vere/io/ames.c b/pkg/vere/io/ames.c index 6a58761402..1881764bf8 100644 --- a/pkg/vere/io/ames.c +++ b/pkg/vere/io/ames.c @@ -205,7 +205,6 @@ typedef enum u3_stun_state { c3_o for_o; // are we forwarding this? } u3_panc; -#define _str_o(lob_o) ( ( c3y == lob_o ) ? "yes" : "no" ) #define _str_typ(typ_y) ( \ ( PACT_AMES == typ_y ) ? "ames" \ : ( PACT_WAIL == typ_y ) ? "wail" \ @@ -214,80 +213,6 @@ typedef enum u3_stun_state { const c3_c* PATH_PARSER = ";~(pfix fas (most fas (cook crip (star ;~(less fas prn)))))"; -static void -_log_head(u3_head* hed_u) -{ - u3l_log("-- HEADER --"); - u3l_log("is request: %s", _str_o(hed_u->req_o)); - u3l_log("is ames: %s", _str_o(hed_u->sim_o)); - u3l_log("mug: 0x%05x", (hed_u->mug_l &0xfffff)); - u3l_log("protocol version: %u", hed_u->ver_y); - u3l_log("sender class: %u", hed_u->sac_y); - u3l_log("recevr class: %u", hed_u->rac_y); - u3l_log("is relayed: %s", _str_o(hed_u->rel_o)); - u3l_log(""); -} - -static void -_log_prel(u3_prel* pre_u) -{ - u3l_log("-- PRELUDE --"); - u3l_log("sender life: %u", pre_u->sic_y); - u3l_log("receiver life: %u", pre_u->ric_y); - u3l_log("sender: %" PRIu64 "", pre_u->sen_d[0]); - u3l_log("receiver: %" PRIu64" ", pre_u->rec_d[0]); - u3l_log(""); -} - -static void -_log_peep(u3_peep* req_u) -{ - u3l_log("--- REQUEST ---"); - u3l_log("strlen: %u", req_u->len_s); - u3l_log("path: %s", req_u->pat_c); - u3l_log("frag: %u", req_u->fra_w); - u3l_log(""); -} - -static c3_c* -_show_mug_buf(c3_y* buf_y, c3_w len_w) -{ - u3_noun mug = u3r_mug_bytes(buf_y, len_w); - u3_noun cot = u3dc("scot", 'q', mug); - return u3r_string(cot); -} - -static void -_log_meow(u3_meow* mew_u) -{ - c3_c* sig_c = _show_mug_buf(mew_u->sig_y, sizeof(mew_u->sig_y)); - c3_c* dat_c = _show_mug_buf(mew_u->dat_y, mew_u->siz_s); - - u3l_log(" sig=%s" - " num=%u" - " siz=%u" - " dat=%s", - sig_c, - mew_u->num_w, - mew_u->siz_s, - dat_c - ); - - c3_free(sig_c); - c3_free(dat_c); -} - -static void -_log_bytes(c3_y* byt_y, c3_w len_w) -{ - int i; - u3l_log("-- BYTES (%u) --", len_w); - for(i = 0; i < len_w; i++) { - u3l_log("%x", byt_y[i]); - } - u3l_log(""); -} - /* _ames_alloc(): libuv buffer allocator. */ static void From 0e8ca703d804fd963db4a0274afda10078f46395 Mon Sep 17 00:00:00 2001 From: Joe Bryan Date: Tue, 23 Apr 2024 14:58:57 -0400 Subject: [PATCH 088/107] ames: move "network-on" flag to a static variable --- pkg/vere/io/ames.c | 24 ++++++++++-------------- 1 file changed, 10 insertions(+), 14 deletions(-) diff --git a/pkg/vere/io/ames.c b/pkg/vere/io/ames.c index 1881764bf8..02649934b2 100644 --- a/pkg/vere/io/ames.c +++ b/pkg/vere/io/ames.c @@ -83,7 +83,6 @@ typedef enum u3_stun_state { } nat_u; // libnatpmp stuff for port forwarding c3_o nal_o; // lane cache backcompat flag struct { // config: - c3_o net_o; // can send c3_o see_o; // can scry c3_o fit_o; // filtering active } fig_u; // @@ -213,6 +212,8 @@ typedef enum u3_stun_state { const c3_c* PATH_PARSER = ";~(pfix fas (most fas (cook crip (star ;~(less fas prn)))))"; +static c3_o net_o = c3y; // online heuristic to limit verbosity + /* _ames_alloc(): libuv buffer allocator. */ static void @@ -740,11 +741,11 @@ _ames_send_cb(uv_udp_send_t* req_u, c3_i sas_i) u3_ames* sam_u = pac_u->sam_u; if ( !sas_i ) { - sam_u->fig_u.net_o = c3y; + net_o = c3y; } - else if ( c3y == sam_u->fig_u.net_o ) { + else if ( c3y == net_o ) { u3l_log("ames: send fail: %s", uv_strerror(sas_i)); - sam_u->fig_u.net_o = c3n; + net_o = c3n; } _ames_pact_free(pac_u); @@ -1000,7 +1001,6 @@ _fine_put_cache(u3_ames* sam_u, u3_noun pax, c3_w lop_w, u3_noun lis) typedef struct _stun_send { uv_udp_send_t req_u; // uv udp request handle - u3_ames* sam_u; // backpointer to driver state c3_y hun_y[0]; // buffer } _stun_send; @@ -1010,14 +1010,13 @@ static void _stun_send_cb(uv_udp_send_t *rep_u, c3_i sas_i) { _stun_send* snd_u = (_stun_send*)rep_u; - u3_ames* sam_u = snd_u->sam_u; if ( !sas_i ) { - sam_u->fig_u.net_o = c3y; + net_o = c3y; } - else if ( c3y == sam_u->fig_u.net_o ) { + else if ( c3y == net_o ) { u3l_log("stun: send response fail: %s", uv_strerror(sas_i)); - sam_u->fig_u.net_o = c3n; + net_o = c3n; } c3_free(snd_u); @@ -1031,7 +1030,6 @@ _stun_on_request(u3_ames* sam_u, const struct sockaddr* adr_u) { _stun_send* snd_u = c3_malloc(sizeof(*snd_u) + 40); - snd_u->sam_u = sam_u; struct sockaddr_in* add_u = (struct sockaddr_in*)adr_u; u3_lane lan_u = { @@ -1108,7 +1106,6 @@ _stun_send_request(u3_ames* sam_u) u3_assert( STUN_OFF != sam_u->sun_u.sat_y ); _stun_send* snd_u = c3_malloc(sizeof(*snd_u) + 28); - snd_u->sam_u = sam_u; u3_stun_make_request(snd_u->hun_y, sam_u->sun_u.tid_y); @@ -2783,7 +2780,7 @@ _ames_io_info(u3_auto* car_u) return u3i_list( u3_pier_mase("filtering", sam_u->fig_u.fit_o), - u3_pier_mase("can-send", sam_u->fig_u.net_o), + u3_pier_mase("can-send", net_o), u3_pier_mase("can-scry", sam_u->fig_u.see_o), u3_pier_mase("stun-working", sam_u->sun_u.wok_o), u3_pier_mase("scry-cache", u3i_word(u3h_wyt(sam_u->fin_s.sac_p))), @@ -2828,7 +2825,7 @@ _ames_io_slog(u3_auto* car_u) // u3l_log(" config:"); u3l_log(" filtering: %s", FLAG(sam_u->fig_u.fit_o)); - u3l_log(" can send: %s", FLAG(sam_u->fig_u.net_o)); + u3l_log(" can send: %s", FLAG(net_o)); u3l_log(" can scry: %s", FLAG(sam_u->fig_u.see_o)); u3l_log(" stun:"); u3l_log(" working: %s", FLAG(sam_u->sun_u.wok_o)); @@ -2860,7 +2857,6 @@ u3_ames_io_init(u3_pier* pir_u) u3_ames* sam_u = c3_calloc(sizeof(*sam_u)); sam_u->pir_u = pir_u; sam_u->nal_o = c3n; - sam_u->fig_u.net_o = c3y; sam_u->fig_u.see_o = c3y; sam_u->fig_u.fit_o = c3n; sam_u->sun_u.wok_o = c3n; From 053ef2b3b28b78922f033acc7bf008d229c5910c Mon Sep 17 00:00:00 2001 From: Joe Bryan Date: Thu, 27 Jun 2024 12:42:42 -0400 Subject: [PATCH 089/107] vere: support .run in subprocess replay --- pkg/vere/main.c | 65 +++++++++++++++++++++++++++++++------------------ 1 file changed, 41 insertions(+), 24 deletions(-) diff --git a/pkg/vere/main.c b/pkg/vere/main.c index 33fd36290a..e27c249b34 100644 --- a/pkg/vere/main.c +++ b/pkg/vere/main.c @@ -2403,35 +2403,52 @@ _cw_play_fork(c3_d eve_d, c3_d sap_d, c3_o mel_o, c3_o sof_o, c3_o ful_o) { // prepare args // - c3_c eve_c[21], sap_c[21] = { 0 }; - if ( 0 > sprintf(eve_c, "%" PRIu64, eve_d) || - 0 > sprintf(sap_c, "%" PRIu64, sap_d) ) + c3_c eve_c[21]; + c3_c sap_c[21] = { 0 }; + c3_i run_i = 0; + c3_i ret_i; + + ret_i = snprintf(eve_c, sizeof(eve_c), "%" PRIu64, eve_d); + u3_assert( ret_i && ret_i < sizeof(eve_c) ); + ret_i = snprintf(sap_c, sizeof(sap_c), "%" PRIu64, sap_d); + u3_assert( ret_i && ret_i < sizeof(sap_c) ); + { - fprintf(stderr, "play: error parsing args\r\n"); - return 1; + c3_c* run_c = _main_pier_run(u3_Host.wrk_c); + if ( run_c ) { + c3_free(run_c); + run_i = 1; + } } c3_c *argv[11] = { u3_Host.wrk_c, "play", - u3_Host.dir_c, "--replay-to", eve_c, "--snap-at", sap_c, }; - c3_z i = 7; - if _(mel_o) { - argv[i++] = "--auto-meld"; - } - if _(sof_o) { - argv[i++] = "--soft-mugs"; - } - if _(ful_o) { - argv[i++] = "--full"; + { + c3_z i_z = 6; + + if _(mel_o) { + argv[i_z++] = "--auto-meld"; + } + if _(sof_o) { + argv[i_z++] = "--soft-mugs"; + } + if _(ful_o) { + argv[i_z++] = "--full"; + } + + if ( !run_i ) { + argv[i_z++] = u3_Host.dir_c; + } + + argv[i_z] = NULL; } - argv[i] = NULL; // prepare a pipe for ipc with the subprocess // @@ -2450,9 +2467,10 @@ _cw_play_fork(c3_d eve_d, c3_d sap_d, c3_o mel_o, c3_o sof_o, c3_o ful_o) // spawn a new serf process and call its play subcommand // - pid_t pid; - if ( 0 != posix_spawn(&pid, u3_Host.wrk_c, &action, 0, argv, 0) ) { - fprintf(stderr, "play: posix_spawn: %d\r\n", errno); + pid_t pid_i; + c3_i sat_i; + if ( 0 != (sat_i = posix_spawn(&pid_i, u3_Host.wrk_c, &action, 0, argv, 0)) ) { + fprintf(stderr, "play: posix_spawn: %s\r\n", strerror(sat_i)); return 1; } @@ -2462,14 +2480,13 @@ _cw_play_fork(c3_d eve_d, c3_d sap_d, c3_o mel_o, c3_o sof_o, c3_o ful_o) // wait for the child to exit // - c3_i sat_i; - if ( -1 == waitpid(pid, &sat_i, 0) ) { - fprintf(stderr, "play: waitpid: %d\r\n", errno); + if ( -1 == waitpid(pid_i, &sat_i, 0) ) { + fprintf(stderr, "play: waitpid: %s\r\n", strerror(errno)); return 1; } if ( WIFEXITED(sat_i) ) { - c3_i ret_i = WEXITSTATUS(sat_i); + ret_i = WEXITSTATUS(sat_i); if ( 0 != ret_i ) { fprintf(stderr, "play: exited with %d\r\n", ret_i); } @@ -3301,7 +3318,7 @@ main(c3_i argc, if ( u3_Host.ops_u.nuu == c3n ) { c3_i sat_i = _cw_play_fork(0, 0, c3n, c3n, c3n); if ( sat_i ) { - fprintf(stderr, "play: replay failed: %d\r\n", sat_i); + fprintf(stderr, "play: replay failed\r\n"); exit(sat_i); } signal(SIGTSTP, _stop_exit); From d6b2fb7f797b7e775b95ae9f44896612bbd3822c Mon Sep 17 00:00:00 2001 From: Joe Bryan Date: Thu, 27 Jun 2024 12:42:57 -0400 Subject: [PATCH 090/107] vere: fix argument order in binary copying error msg --- pkg/vere/king.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pkg/vere/king.c b/pkg/vere/king.c index 66181c6f6a..915ce7262f 100644 --- a/pkg/vere/king.c +++ b/pkg/vere/king.c @@ -1498,7 +1498,7 @@ _king_copy_vere(c3_c* pac_c, c3_c* ver_c, c3_c* arc_c, c3_t lin_t) if ( ret_i ) { fprintf(stderr, "vere: copy %s -> %s failed: %s\r\n", - bin_c, u3_Host.dem_c, strerror(errno)); + u3_Host.dem_c, bin_c, strerror(errno)); c3_free(bin_c); return -1; } From c5e93ca799a149f5137783149490321f9c4fcc6c Mon Sep 17 00:00:00 2001 From: Joe Bryan Date: Thu, 27 Jun 2024 14:40:29 -0400 Subject: [PATCH 091/107] vere: refactors replay subprocess argument construction --- pkg/vere/main.c | 34 +++++++++++++++------------------- 1 file changed, 15 insertions(+), 19 deletions(-) diff --git a/pkg/vere/main.c b/pkg/vere/main.c index e27c249b34..09a94f52ba 100644 --- a/pkg/vere/main.c +++ b/pkg/vere/main.c @@ -2380,7 +2380,8 @@ _cw_play_impl(c3_d eve_d, c3_d sap_d, c3_o mel_o, c3_o sof_o, c3_o ful_o) /* _cw_play_fork_heed(): wait for EOF on STDIN or until canceled. */ -void* _cw_play_fork_heed(void* arg) { +static void* +_cw_play_fork_heed(void* arg) { c3_c buf[1]; c3_zs red; @@ -2401,11 +2402,9 @@ void* _cw_play_fork_heed(void* arg) { static c3_i _cw_play_fork(c3_d eve_d, c3_d sap_d, c3_o mel_o, c3_o sof_o, c3_o ful_o) { - // prepare args - // - c3_c eve_c[21]; - c3_c sap_c[21] = { 0 }; - c3_i run_i = 0; + c3_c *argv[11] = {0}; + c3_c eve_c[21] = {0}; + c3_c sap_c[21] = {0}; c3_i ret_i; ret_i = snprintf(eve_c, sizeof(eve_c), "%" PRIu64, eve_d); @@ -2414,24 +2413,21 @@ _cw_play_fork(c3_d eve_d, c3_d sap_d, c3_o mel_o, c3_o sof_o, c3_o ful_o) u3_assert( ret_i && ret_i < sizeof(sap_c) ); { + c3_z i_z = 0; + c3_i run_i = 0; + c3_c* run_c = _main_pier_run(u3_Host.wrk_c); if ( run_c ) { c3_free(run_c); run_i = 1; } - } - c3_c *argv[11] = { - u3_Host.wrk_c, - "play", - "--replay-to", - eve_c, - "--snap-at", - sap_c, - }; - - { - c3_z i_z = 6; + argv[i_z++] = u3_Host.wrk_c; + argv[i_z++] = "play"; + argv[i_z++] = "--replay-to"; + argv[i_z++] = eve_c; + argv[i_z++] = "--snap-at"; + argv[i_z++] = sap_c; if _(mel_o) { argv[i_z++] = "--auto-meld"; @@ -2442,12 +2438,12 @@ _cw_play_fork(c3_d eve_d, c3_d sap_d, c3_o mel_o, c3_o sof_o, c3_o ful_o) if _(ful_o) { argv[i_z++] = "--full"; } - if ( !run_i ) { argv[i_z++] = u3_Host.dir_c; } argv[i_z] = NULL; + u3_assert( i_z < sizeof(argv) ); } // prepare a pipe for ipc with the subprocess From 14f02291c211929fa029107571ba134d84bcd305 Mon Sep 17 00:00:00 2001 From: Joe Bryan Date: Thu, 27 Jun 2024 14:41:38 -0400 Subject: [PATCH 092/107] vere: quiet replay when no-op --- pkg/vere/main.c | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/pkg/vere/main.c b/pkg/vere/main.c index 09a94f52ba..40efe5e12b 100644 --- a/pkg/vere/main.c +++ b/pkg/vere/main.c @@ -2593,9 +2593,7 @@ _cw_play(c3_i argc, c3_c* argv[]) pthread_t ted; pthread_create(&ted, NULL, _cw_play_fork_heed, NULL); - if ( !_cw_play_impl(eve_d, sap_d, mel_o, sof_o, ful_o) ) { - fprintf(stderr, "mars: nothing to do!\r\n"); - } + _cw_play_impl(eve_d, sap_d, mel_o, sof_o, ful_o); pthread_cancel(ted); } From 837e515756a24407f6055bd143c347ad93b8307b Mon Sep 17 00:00:00 2001 From: Joe Bryan Date: Fri, 28 Jun 2024 11:34:17 -0400 Subject: [PATCH 093/107] vere: respect --loom in subprocess replay --- pkg/vere/main.c | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/pkg/vere/main.c b/pkg/vere/main.c index 40efe5e12b..361f8ad304 100644 --- a/pkg/vere/main.c +++ b/pkg/vere/main.c @@ -2402,15 +2402,18 @@ _cw_play_fork_heed(void* arg) { static c3_i _cw_play_fork(c3_d eve_d, c3_d sap_d, c3_o mel_o, c3_o sof_o, c3_o ful_o) { - c3_c *argv[11] = {0}; + c3_c *argv[12] = {0}; c3_c eve_c[21] = {0}; c3_c sap_c[21] = {0}; + c3_c lom_c[3] = {0}; c3_i ret_i; ret_i = snprintf(eve_c, sizeof(eve_c), "%" PRIu64, eve_d); u3_assert( ret_i && ret_i < sizeof(eve_c) ); ret_i = snprintf(sap_c, sizeof(sap_c), "%" PRIu64, sap_d); u3_assert( ret_i && ret_i < sizeof(sap_c) ); + ret_i = snprintf(lom_c, sizeof(lom_c), "%u", u3_Host.ops_u.lom_y); + u3_assert( ret_i && ret_i < sizeof(lom_c) ); { c3_z i_z = 0; @@ -2424,6 +2427,8 @@ _cw_play_fork(c3_d eve_d, c3_d sap_d, c3_o mel_o, c3_o sof_o, c3_o ful_o) argv[i_z++] = u3_Host.wrk_c; argv[i_z++] = "play"; + argv[i_z++] = "--loom"; + argv[i_z++] = lom_c; argv[i_z++] = "--replay-to"; argv[i_z++] = eve_c; argv[i_z++] = "--snap-at"; From 32fa09d2968b47beccb35227d97ec71b8cbe0820 Mon Sep 17 00:00:00 2001 From: Joe Bryan Date: Mon, 1 Jul 2024 10:43:35 -0400 Subject: [PATCH 094/107] ames: only print packet drop-count on drop --- pkg/vere/io/ames.c | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/pkg/vere/io/ames.c b/pkg/vere/io/ames.c index 66589f6aa5..5825bac0b8 100644 --- a/pkg/vere/io/ames.c +++ b/pkg/vere/io/ames.c @@ -1659,6 +1659,7 @@ static void _ames_cap_queue(u3_ames* sam_u) { u3_ovum* egg_u = sam_u->car_u.ext_u; + c3_d old_d = sam_u->sat_u.dop_d; while ( egg_u && (QUEUE_MAX < sam_u->car_u.dep_w) ) { u3_ovum* nex_u = egg_u->nex_u; @@ -1675,8 +1676,9 @@ _ames_cap_queue(u3_ames* sam_u) egg_u = nex_u; } - if ( (sam_u->sat_u.dop_d && (0 == (sam_u->sat_u.dop_d % 1000))) - && !(u3C.wag_w & u3o_verbose) ) + if ( !(u3C.wag_w & u3o_verbose) + && (old_d != sam_u->sat_u.dop_d) + && !(sam_u->sat_u.dop_d % 1000) ) { u3l_log("ames: packet dropped (%" PRIu64 " total)", sam_u->sat_u.dop_d); } From a04a2ed3c1a3a79341007b8fa8b9022b72de59ee Mon Sep 17 00:00:00 2001 From: Pyry Kovanen Date: Thu, 4 Jul 2024 15:47:38 +0300 Subject: [PATCH 095/107] disk: update epoc.txt and vere.txt atomically --- pkg/vere/disk.c | 34 +++++++++++++++++++++++++--------- 1 file changed, 25 insertions(+), 9 deletions(-) diff --git a/pkg/vere/disk.c b/pkg/vere/disk.c index 58cb2f822f..62d04fa19b 100644 --- a/pkg/vere/disk.c +++ b/pkg/vere/disk.c @@ -1123,18 +1123,26 @@ u3_disk_epoc_zero(u3_disk* log_u) } // create epoch version file, overwriting any existing file + c3_c epi_c[8193]; c3_c epv_c[8193]; - snprintf(epv_c, sizeof(epv_c), "%s/epoc.txt", epo_c); - FILE* epv_f = fopen(epv_c, "w"); // XX errors + snprintf(epi_c, sizeof(epv_c), "%s/epoc.tmp", epo_c); + FILE* epv_f = fopen(epi_c, "w"); // XX errors fprintf(epv_f, "%d", U3E_VERLAT); fclose(epv_f); + snprintf(epv_c, sizeof(epv_c), "%s/epoc.txt", epo_c); + rename(epi_c, epv_c); + c3_unlink(epi_c); // create binary version file, overwriting any existing file + c3_c bii_c[8193]; c3_c biv_c[8193]; - snprintf(biv_c, sizeof(biv_c), "%s/vere.txt", epo_c); - FILE* biv_f = fopen(biv_c, "w"); // XX errors + snprintf(bii_c, sizeof(biv_c), "%s/vere.tmp", epo_c); + FILE* biv_f = fopen(bii_c, "w"); // XX errors fprintf(biv_f, URBIT_VERSION); // XX append a sentinel for auto-rollover? fclose(biv_f); + snprintf(biv_c, sizeof(epv_c), "%s/vere.txt", epo_c); + rename(bii_c, biv_c); + c3_unlink(bii_c); if ( -1 == c3_sync(epo_i) ) { // XX fdatasync on linux? fprintf(stderr, "disk: sync epoch dir 0i0 failed: %s\r\n", strerror(errno)); @@ -1205,18 +1213,26 @@ _disk_epoc_roll(u3_disk* log_u, c3_d epo_d) } // create epoch version file, overwriting any existing file + c3_c epi_c[8193]; c3_c epv_c[8193]; - snprintf(epv_c, sizeof(epv_c), "%s/epoc.txt", epo_c); - FILE* epv_f = fopen(epv_c, "w"); // XX errors + snprintf(epi_c, sizeof(epv_c), "%s/epoc.tmp", epo_c); + FILE* epv_f = fopen(epi_c, "w"); // XX errors fprintf(epv_f, "%d", U3E_VERLAT); fclose(epv_f); + snprintf(epv_c, sizeof(epv_c), "%s/epoc.txt", epo_c); + rename(epi_c, epv_c); + c3_unlink(epi_c); // create binary version file, overwriting any existing file + c3_c bii_c[8193]; c3_c biv_c[8193]; - snprintf(biv_c, sizeof(biv_c), "%s/vere.txt", epo_c); - FILE* biv_f = fopen(biv_c, "w"); // XX errors - fprintf(biv_f, URBIT_VERSION); + snprintf(bii_c, sizeof(biv_c), "%s/vere.tmp", epo_c); + FILE* biv_f = fopen(bii_c, "w"); // XX errors + fprintf(biv_f, URBIT_VERSION); // XX append a sentinel for auto-rollover? fclose(biv_f); + snprintf(biv_c, sizeof(epv_c), "%s/vere.txt", epo_c); + rename(bii_c, biv_c); + c3_unlink(bii_c); // get metadata from old log c3_d who_d[2]; From a63cb27e80d94e76b8f29eafe4116e6f9b16ae62 Mon Sep 17 00:00:00 2001 From: Joe Bryan Date: Fri, 5 Jul 2024 12:12:36 -0400 Subject: [PATCH 096/107] disk: fix epoc.txt version parsing code --- pkg/vere/disk.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/pkg/vere/disk.c b/pkg/vere/disk.c index 62d04fa19b..c80f4f2b71 100644 --- a/pkg/vere/disk.c +++ b/pkg/vere/disk.c @@ -1723,9 +1723,9 @@ _disk_epoc_load(u3_disk* log_u, c3_d lat_d) return _epoc_gone; } - if ( (1 != sscanf(ver_c, "%" SCNu32 "%n", &ver_w, &car_i)) - && (0 < car_i) - && ('\0' == *(ver_c + car_i)) ) + if ( !( (1 == sscanf(ver_c, "%" SCNu32 "%n", &ver_w, &car_i)) + && (0 < car_i) + && ('\0' == *(ver_c + car_i)) ) ) { fprintf(stderr, "disk: failed to parse epoch version: '%s'\r\n", ver_c); return _epoc_fail; From a7fd52a19077fcff9331fff523b10f8d6e2376ea Mon Sep 17 00:00:00 2001 From: Matthew LeVan Date: Tue, 9 Jul 2024 11:35:22 -0500 Subject: [PATCH 097/107] docs: lsp integration --- INSTALL.md | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/INSTALL.md b/INSTALL.md index 6c4d4e8d1d..5f55e076a9 100644 --- a/INSTALL.md +++ b/INSTALL.md @@ -113,6 +113,17 @@ you can pass them with [`--per_file_copt`][per_file_copt] like so: bazel build --per_file_copt='pkg/.*@-DMACRO' ``` +## LSP Integration + +```console +bazel run //bazel:refresh_compile_commands +``` + +Running this command will generate a `compile_commands.json` file in the root +of the repository, which `clangd` (or other language server processors) will +use automatically to provide modern editor features like syntax highlighting, +go-to definitions, call hierarchies, symbol manipulation, etc. + ## Test Commands You can build and run unit tests only on native builds. If you have a native From ecf3c55ca32621e9d33fd0dae6a64ec5510b542c Mon Sep 17 00:00:00 2001 From: Joe Bryan Date: Mon, 15 Jul 2024 11:25:39 -0400 Subject: [PATCH 098/107] u3: actually distinguish incorrect image size from other stat failures --- pkg/noun/events.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pkg/noun/events.c b/pkg/noun/events.c index 51d911eebe..67f2352d4a 100644 --- a/pkg/noun/events.c +++ b/pkg/noun/events.c @@ -352,7 +352,7 @@ _ce_image_stat(u3e_image* img_u, c3_w* pgs_w) } else if ( siz_z != _ce_len(pgs_z) ) { fprintf(stderr, "loom: %s corrupt size %zu\r\n", img_u->nam_c, siz_z); - return _ce_img_good; + return _ce_img_size; } else if ( pgs_z > UINT32_MAX ) { fprintf(stderr, "loom: %s overflow %zu\r\n", img_u->nam_c, siz_z); From 8f1bf10a98fc10fd76e8ab697fd1327d73403b75 Mon Sep 17 00:00:00 2001 From: pkova Date: Fri, 19 Jul 2024 19:15:05 +0300 Subject: [PATCH 099/107] disk: refactor epoc/vere.txt generation and handle errors --- pkg/vere/disk.c | 71 +++++++++++++++++++++++++++++++++++-------------- 1 file changed, 51 insertions(+), 20 deletions(-) diff --git a/pkg/vere/disk.c b/pkg/vere/disk.c index c80f4f2b71..1a55abbef8 100644 --- a/pkg/vere/disk.c +++ b/pkg/vere/disk.c @@ -1126,23 +1126,36 @@ u3_disk_epoc_zero(u3_disk* log_u) c3_c epi_c[8193]; c3_c epv_c[8193]; snprintf(epi_c, sizeof(epv_c), "%s/epoc.tmp", epo_c); - FILE* epv_f = fopen(epi_c, "w"); // XX errors - fprintf(epv_f, "%d", U3E_VERLAT); - fclose(epv_f); snprintf(epv_c, sizeof(epv_c), "%s/epoc.txt", epo_c); - rename(epi_c, epv_c); - c3_unlink(epi_c); + FILE* epv_f = fopen(epi_c, "w"); // XX errors + + if ( !epv_f + || (0 > fprintf(epv_f, "%d", U3E_VERLAT)) + || fflush(epv_f) + || (-1 == c3_sync(fileno(epv_f))) + || fclose(epv_f) + || (-1 == rename(epi_c, epv_c)) ) + { + fprintf(stderr, "disk: write epoc.txt failed %s\r\n", strerror(errno)); + goto fail3; + } // create binary version file, overwriting any existing file c3_c bii_c[8193]; c3_c biv_c[8193]; snprintf(bii_c, sizeof(biv_c), "%s/vere.tmp", epo_c); - FILE* biv_f = fopen(bii_c, "w"); // XX errors - fprintf(biv_f, URBIT_VERSION); // XX append a sentinel for auto-rollover? - fclose(biv_f); snprintf(biv_c, sizeof(epv_c), "%s/vere.txt", epo_c); - rename(bii_c, biv_c); - c3_unlink(bii_c); + FILE* biv_f = fopen(bii_c, "w"); + if ( !biv_f + || (0 > fprintf(biv_f, URBIT_VERSION)) + || fflush(biv_f) + || (-1 == c3_sync(fileno(biv_f))) + || fclose(biv_f) + || (-1 == rename(bii_c, biv_c)) ) + { + fprintf(stderr, "disk: write vere.txt failed %s\r\n", strerror(errno)); + goto fail3; + } if ( -1 == c3_sync(epo_i) ) { // XX fdatasync on linux? fprintf(stderr, "disk: sync epoch dir 0i0 failed: %s\r\n", strerror(errno)); @@ -1216,23 +1229,41 @@ _disk_epoc_roll(u3_disk* log_u, c3_d epo_d) c3_c epi_c[8193]; c3_c epv_c[8193]; snprintf(epi_c, sizeof(epv_c), "%s/epoc.tmp", epo_c); - FILE* epv_f = fopen(epi_c, "w"); // XX errors - fprintf(epv_f, "%d", U3E_VERLAT); - fclose(epv_f); snprintf(epv_c, sizeof(epv_c), "%s/epoc.txt", epo_c); - rename(epi_c, epv_c); - c3_unlink(epi_c); + FILE* epv_f = fopen(epi_c, "w"); + + if ( !epv_f + || (0 > fprintf(epv_f, "%d", U3E_VERLAT)) + || fflush(epv_f) + || (-1 == c3_sync(fileno(epv_f))) + || fclose(epv_f) + || (-1 == rename(epi_c, epv_c)) ) + { + fprintf(stderr, "disk: write epoc.txt failed %s\r\n", strerror(errno)); + goto fail3; + } // create binary version file, overwriting any existing file c3_c bii_c[8193]; c3_c biv_c[8193]; snprintf(bii_c, sizeof(biv_c), "%s/vere.tmp", epo_c); - FILE* biv_f = fopen(bii_c, "w"); // XX errors - fprintf(biv_f, URBIT_VERSION); // XX append a sentinel for auto-rollover? - fclose(biv_f); snprintf(biv_c, sizeof(epv_c), "%s/vere.txt", epo_c); - rename(bii_c, biv_c); - c3_unlink(bii_c); + FILE* biv_f = fopen(bii_c, "w"); + if ( !biv_f + || (0 > fprintf(biv_f, URBIT_VERSION)) + || fflush(biv_f) + || (-1 == c3_sync(fileno(biv_f))) + || fclose(biv_f) + || (-1 == rename(bii_c, biv_c)) ) + { + fprintf(stderr, "disk: write vere.txt failed %s\r\n", strerror(errno)); + goto fail3; + } + + if ( -1 == c3_sync(epo_i) ) { // XX fdatasync on linux? + fprintf(stderr, "disk: sync epoch dir 0i0 failed: %s\r\n", strerror(errno)); + goto fail3; + } // get metadata from old log c3_d who_d[2]; From 1b4e776906f9ae06e7d90e8ec3107ef2f9cf3434 Mon Sep 17 00:00:00 2001 From: Ted Blackman Date: Fri, 19 Jul 2024 16:27:43 -0400 Subject: [PATCH 100/107] gitignore MODULE.bazel and lock --- .gitignore | 2 ++ 1 file changed, 2 insertions(+) diff --git a/.gitignore b/.gitignore index da5bf8e6c8..fe7281c2cc 100644 --- a/.gitignore +++ b/.gitignore @@ -6,6 +6,8 @@ /.user.bazelrc /bazel-* /urbit +MODULE.bazel +MODULE.bazel.lock # Swap files. *.swo From ca5f6a0348f8636537985478d7ffd4d983ee19a5 Mon Sep 17 00:00:00 2001 From: pkova Date: Mon, 22 Jul 2024 16:47:13 +0300 Subject: [PATCH 101/107] bazel: change mirror for libnatpmp because it's always down --- WORKSPACE.bazel | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/WORKSPACE.bazel b/WORKSPACE.bazel index dca6750619..30c4f600c5 100644 --- a/WORKSPACE.bazel +++ b/WORKSPACE.bazel @@ -222,7 +222,7 @@ versioned_http_archive( build_file = "//bazel/third_party/natpmp:natpmp.BUILD", sha256 = "0684ed2c8406437e7519a1bd20ea83780db871b3a3a5d752311ba3e889dbfc70", strip_prefix = "libnatpmp-{version}", - url = "http://miniupnp.free.fr/files/libnatpmp-{version}.tar.gz", + url = "http://download.openpkg.org/components/cache/libnatpmp/libnatpmp-{version}.tar.gz", version = "20230423", ) From 2d0eaf024b35479e53442981355bb2ac69a12831 Mon Sep 17 00:00:00 2001 From: Matthew LeVan Date: Mon, 22 Jul 2024 16:15:35 -0400 Subject: [PATCH 102/107] play: start using `libuv` for replay --- pkg/vere/main.c | 75 ++++++++++++++++++------------------------------- 1 file changed, 27 insertions(+), 48 deletions(-) diff --git a/pkg/vere/main.c b/pkg/vere/main.c index 361f8ad304..25ef4eaea9 100644 --- a/pkg/vere/main.c +++ b/pkg/vere/main.c @@ -2382,6 +2382,7 @@ _cw_play_impl(c3_d eve_d, c3_d sap_d, c3_o mel_o, c3_o sof_o, c3_o ful_o) */ static void* _cw_play_fork_heed(void* arg) { + // XX c3_c buf[1]; c3_zs red; @@ -2397,6 +2398,19 @@ _cw_play_fork_heed(void* arg) { return NULL; } +/* _cw_play_fork_exit(): exit callback for uv_spawn. +*/ +void +_cw_play_fork_exit(uv_process_t* req_u, c3_d sat_d, c3_i tem_i) { + // XX write better errors + if ( 0 != sat_d ) { + fprintf(stderr, "play: failed: %s\r\n", uv_strerror(tem_i)); + uv_close((uv_handle_t*)req_u, NULL); + exit(1); + } + uv_close((uv_handle_t*)req_u, NULL); +} + /* _cw_play_fork(): spawn a subprocess for event replay. */ static c3_i @@ -2451,56 +2465,23 @@ _cw_play_fork(c3_d eve_d, c3_d sap_d, c3_o mel_o, c3_o sof_o, c3_o ful_o) u3_assert( i_z < sizeof(argv) ); } - // prepare a pipe for ipc with the subprocess + // use uv_spawn to fork a new serf process and call its play subcommand // - c3_i pipefd[2]; - if ( 0 != pipe(pipefd) ) { - fprintf(stderr, "play: failed to open pipe\r\n"); - return 1; - } + uv_process_t child_req; + uv_process_options_t options = {0}; + options.file = u3_Host.wrk_c; + options.args = argv; + options.exit_cb = (uv_exit_cb)_cw_play_fork_exit; - // set the child process' stdin to read from the pipe - // - posix_spawn_file_actions_t action; - posix_spawn_file_actions_init(&action); - posix_spawn_file_actions_addclose(&action, pipefd[1]); - posix_spawn_file_actions_adddup2(&action, pipefd[0], STDIN_FILENO); + u3L = uv_default_loop(); - // spawn a new serf process and call its play subcommand - // - pid_t pid_i; - c3_i sat_i; - if ( 0 != (sat_i = posix_spawn(&pid_i, u3_Host.wrk_c, &action, 0, argv, 0)) ) { - fprintf(stderr, "play: posix_spawn: %s\r\n", strerror(sat_i)); + c3_i sat_i; + if ( 0 != (sat_i = uv_spawn(u3L, &child_req, &options)) ) { + fprintf(stderr, "play: uv_spawn: %s\r\n", uv_strerror(sat_i)); return 1; } - // close the read end of the pipe in the parent - // - close(pipefd[0]); - - // wait for the child to exit - // - if ( -1 == waitpid(pid_i, &sat_i, 0) ) { - fprintf(stderr, "play: waitpid: %s\r\n", strerror(errno)); - return 1; - } - - if ( WIFEXITED(sat_i) ) { - ret_i = WEXITSTATUS(sat_i); - if ( 0 != ret_i ) { - fprintf(stderr, "play: exited with %d\r\n", ret_i); - } - return ret_i; - } - else if ( WIFSIGNALED(sat_i) ) { - fprintf(stderr, "play: terminated by signal %d\r\n", WTERMSIG(sat_i)); - return 1; - } - else { - fprintf(stderr, "play: strange termination\r\n"); - return 1; - } + return uv_run(u3L, UV_RUN_DEFAULT); } /* _cw_play(): replay events, but better. @@ -2595,12 +2576,10 @@ _cw_play(c3_i argc, c3_c* argv[]) exit(1); } - pthread_t ted; - pthread_create(&ted, NULL, _cw_play_fork_heed, NULL); + // uv_thread_t ted; + // uv_thread_create(&ted, (uv_thread_cb)_cw_play_fork_heed, NULL); _cw_play_impl(eve_d, sap_d, mel_o, sof_o, ful_o); - - pthread_cancel(ted); } /* _cw_prep(): prepare for upgrade From f56c1af8b8869f0d531fdb65462ef8edfeee1b95 Mon Sep 17 00:00:00 2001 From: Matthew LeVan Date: Tue, 23 Jul 2024 12:35:27 -0400 Subject: [PATCH 103/107] play: `libuv` replay wip --- pkg/vere/main.c | 64 ++++++++++++++++++++++++++++++++++++++----------- 1 file changed, 50 insertions(+), 14 deletions(-) diff --git a/pkg/vere/main.c b/pkg/vere/main.c index 25ef4eaea9..75fdddf8a2 100644 --- a/pkg/vere/main.c +++ b/pkg/vere/main.c @@ -936,6 +936,17 @@ report(void) LIBCURL_VERSION_PATCH); } +/* _stop_exit(): exit immediately. +*/ +static void +_stop_exit_fore(c3_i int_i) +{ + // explicit fprintf to avoid allocation in u3l_log + // + fprintf(stderr, "\r\n[received keyboard stop signal, exiting] 3 %d\r\n", getpid()); + // raise(SIGTERM); +} + /* _stop_exit(): exit immediately. */ static void @@ -943,7 +954,7 @@ _stop_exit(c3_i int_i) { // explicit fprintf to avoid allocation in u3l_log // - fprintf(stderr, "\r\n[received keyboard stop signal, exiting]\r\n"); + fprintf(stderr, "\r\n[received keyboard stop signal, exiting] 1 %d\r\n", getpid()); u3_king_bail(); } @@ -2304,7 +2315,7 @@ _cw_play_exit(c3_i int_i) { // explicit fprintf to avoid allocation in u3l_log // - fprintf(stderr, "\r\n[received keyboard stop signal, exiting]\r\n"); + fprintf(stderr, "\r\n[received keyboard stop signal, exiting] 2 %d\r\n", getpid()); raise(SIGINT); } @@ -2401,14 +2412,14 @@ _cw_play_fork_heed(void* arg) { /* _cw_play_fork_exit(): exit callback for uv_spawn. */ void -_cw_play_fork_exit(uv_process_t* req_u, c3_d sat_d, c3_i tem_i) { - // XX write better errors - if ( 0 != sat_d ) { - fprintf(stderr, "play: failed: %s\r\n", uv_strerror(tem_i)); - uv_close((uv_handle_t*)req_u, NULL); +_cw_play_fork_exit(uv_process_t* req_u, c3_ds sat_d, c3_i tem_i) { + if ( sat_d || tem_i ) { + fprintf(stderr, "play: failed: %" PRId64 " signal: %d\r\n", sat_d, tem_i); exit(1); } + fprintf(stderr, "play: fork exit\r\n"); uv_close((uv_handle_t*)req_u, NULL); + fprintf(stderr, "play: fork exit2\r\n"); } /* _cw_play_fork(): spawn a subprocess for event replay. @@ -2467,20 +2478,34 @@ _cw_play_fork(c3_d eve_d, c3_d sap_d, c3_o mel_o, c3_o sof_o, c3_o ful_o) // use uv_spawn to fork a new serf process and call its play subcommand // - uv_process_t child_req; + u3L = uv_default_loop(); + + uv_pipe_t stdin_pipe; + uv_pipe_init(u3L, &stdin_pipe, 0); + + uv_process_t child_req = {0}; uv_process_options_t options = {0}; - options.file = u3_Host.wrk_c; + uv_stdio_container_t stdio[3]; + stdio[0].data.stream = (uv_stream_t*) &stdin_pipe; + stdio[1].data.fd = STDOUT_FILENO; + stdio[2].data.fd = STDERR_FILENO; + stdio[0].flags = UV_CREATE_PIPE | UV_READABLE_PIPE; // stdin + stdio[1].flags = UV_INHERIT_FD; // stdout + stdio[2].flags = UV_INHERIT_FD; // stderr + options.stdio_count = 3; + options.stdio = stdio; + options.file = argv[0]; options.args = argv; options.exit_cb = (uv_exit_cb)_cw_play_fork_exit; - u3L = uv_default_loop(); - c3_i sat_i; if ( 0 != (sat_i = uv_spawn(u3L, &child_req, &options)) ) { fprintf(stderr, "play: uv_spawn: %s\r\n", uv_strerror(sat_i)); return 1; } + // signal(SIGINT, SIG_IGN); + return uv_run(u3L, UV_RUN_DEFAULT); } @@ -2576,10 +2601,21 @@ _cw_play(c3_i argc, c3_c* argv[]) exit(1); } - // uv_thread_t ted; - // uv_thread_create(&ted, (uv_thread_cb)_cw_play_fork_heed, NULL); + pthread_t ted; + pthread_create(&ted, NULL, _cw_play_fork_heed, NULL); + + // sigset_t set; + + // sigemptyset(&set); + // sigaddset(&set, SIGINT); + // if ( 0 != pthread_sigmask(SIG_BLOCK, &set, NULL) ) { + // fprintf(stderr, "play: thread mask SIGINT: %s", strerror(errno)); + // exit(1); + // } _cw_play_impl(eve_d, sap_d, mel_o, sof_o, ful_o); + + pthread_cancel(ted); } /* _cw_prep(): prepare for upgrade @@ -3203,7 +3239,7 @@ main(c3_i argc, // // Configured here using signal() so as to be immediately available. // - signal(SIGTSTP, _stop_exit); + signal(SIGTSTP, _stop_exit_fore); printf("~\n"); // printf("welcome.\n"); From 17658d5df93a8c24b3646efce99cac2e0e29ed96 Mon Sep 17 00:00:00 2001 From: pkova Date: Wed, 24 Jul 2024 17:33:52 +0300 Subject: [PATCH 104/107] bazel: change openssl mirror to github since openssl.org is down --- WORKSPACE.bazel | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/WORKSPACE.bazel b/WORKSPACE.bazel index dca6750619..c3965f3807 100644 --- a/WORKSPACE.bazel +++ b/WORKSPACE.bazel @@ -264,7 +264,7 @@ versioned_http_archive( build_file = "//bazel/third_party/openssl:openssl.BUILD", sha256 = "cf3098950cb4d853ad95c0841f1f9c6d3dc102dccfcacd521d93925208b76ac8", strip_prefix = "openssl-{version}", - url = "https://www.openssl.org/source/openssl-{version}.tar.gz", + url = "https://github.com/openssl/openssl/releases/download/OpenSSL_1_1_1w/openssl-{version}.tar.gz", version = "1.1.1w", ) From 53f703a27e35a0c17d3caec42edd6d05d2c5e8c0 Mon Sep 17 00:00:00 2001 From: Matthew LeVan Date: Wed, 24 Jul 2024 17:09:12 -0400 Subject: [PATCH 105/107] play: all processes killed on all exit signals --- pkg/vere/main.c | 16 ++++------------ 1 file changed, 4 insertions(+), 12 deletions(-) diff --git a/pkg/vere/main.c b/pkg/vere/main.c index 75fdddf8a2..b084bcfdde 100644 --- a/pkg/vere/main.c +++ b/pkg/vere/main.c @@ -936,15 +936,12 @@ report(void) LIBCURL_VERSION_PATCH); } -/* _stop_exit(): exit immediately. +/* _stop_exit_fore(): exit before. */ static void _stop_exit_fore(c3_i int_i) { - // explicit fprintf to avoid allocation in u3l_log - // - fprintf(stderr, "\r\n[received keyboard stop signal, exiting] 3 %d\r\n", getpid()); - // raise(SIGTERM); + raise(SIGTERM); } /* _stop_exit(): exit immediately. @@ -954,7 +951,7 @@ _stop_exit(c3_i int_i) { // explicit fprintf to avoid allocation in u3l_log // - fprintf(stderr, "\r\n[received keyboard stop signal, exiting] 1 %d\r\n", getpid()); + fprintf(stderr, "\r\n[received keyboard stop signal, exiting]\r\n"); u3_king_bail(); } @@ -2313,9 +2310,6 @@ _cw_play_snap(u3_disk* log_u) static void _cw_play_exit(c3_i int_i) { - // explicit fprintf to avoid allocation in u3l_log - // - fprintf(stderr, "\r\n[received keyboard stop signal, exiting] 2 %d\r\n", getpid()); raise(SIGINT); } @@ -2417,9 +2411,7 @@ _cw_play_fork_exit(uv_process_t* req_u, c3_ds sat_d, c3_i tem_i) { fprintf(stderr, "play: failed: %" PRId64 " signal: %d\r\n", sat_d, tem_i); exit(1); } - fprintf(stderr, "play: fork exit\r\n"); uv_close((uv_handle_t*)req_u, NULL); - fprintf(stderr, "play: fork exit2\r\n"); } /* _cw_play_fork(): spawn a subprocess for event replay. @@ -2504,7 +2496,7 @@ _cw_play_fork(c3_d eve_d, c3_d sap_d, c3_o mel_o, c3_o sof_o, c3_o ful_o) return 1; } - // signal(SIGINT, SIG_IGN); + signal(SIGINT, SIG_IGN); return uv_run(u3L, UV_RUN_DEFAULT); } From 56932e5e23f6e1aaf5fa628101f5d1295fece9b1 Mon Sep 17 00:00:00 2001 From: Matthew LeVan Date: Fri, 26 Jul 2024 12:02:42 -0400 Subject: [PATCH 106/107] play: replay with `libuv` complete --- pkg/vere/main.c | 27 +++++++++++++++------------ 1 file changed, 15 insertions(+), 12 deletions(-) diff --git a/pkg/vere/main.c b/pkg/vere/main.c index b084bcfdde..12c64d8ecd 100644 --- a/pkg/vere/main.c +++ b/pkg/vere/main.c @@ -941,7 +941,7 @@ report(void) static void _stop_exit_fore(c3_i int_i) { - raise(SIGTERM); + kill(getpid(), SIGTERM); } /* _stop_exit(): exit immediately. @@ -2310,7 +2310,7 @@ _cw_play_snap(u3_disk* log_u) static void _cw_play_exit(c3_i int_i) { - raise(SIGINT); + kill(getpid(), SIGINT); } /* _cw_play_impl(): replay events, but better. @@ -2391,12 +2391,24 @@ _cw_play_fork_heed(void* arg) { c3_c buf[1]; c3_zs red; + sigset_t set; + + sigemptyset(&set); + sigaddset(&set, SIGINT); + sigaddset(&set, SIGTERM); + sigaddset(&set, SIGTSTP); + if ( 0 != pthread_sigmask(SIG_BLOCK, &set, NULL) ) { + fprintf(stderr, "play: watcher failed to block sigs: %s\r\n", strerror(errno)); + exit(1); + } + do { pthread_testcancel(); red = read(STDIN_FILENO, buf, sizeof(buf)); if ( 0 == red ) { fprintf(stderr, "play: god save the king! committing sudoku...\r\n"); - exit(1); + kill(getpid(), SIGINT); + return NULL; } } while ( 0 < red ); @@ -2596,15 +2608,6 @@ _cw_play(c3_i argc, c3_c* argv[]) pthread_t ted; pthread_create(&ted, NULL, _cw_play_fork_heed, NULL); - // sigset_t set; - - // sigemptyset(&set); - // sigaddset(&set, SIGINT); - // if ( 0 != pthread_sigmask(SIG_BLOCK, &set, NULL) ) { - // fprintf(stderr, "play: thread mask SIGINT: %s", strerror(errno)); - // exit(1); - // } - _cw_play_impl(eve_d, sap_d, mel_o, sof_o, ful_o); pthread_cancel(ted); From 229f6a7c55702cd35e2368cfd70932c5314e033d Mon Sep 17 00:00:00 2001 From: Pyry Kovanen Date: Mon, 29 Jul 2024 15:35:26 +0300 Subject: [PATCH 107/107] Update VERSION to 3.1 --- VERSION | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/VERSION b/VERSION index 9f55b2ccb5..8c50098d8a 100644 --- a/VERSION +++ b/VERSION @@ -1 +1 @@ -3.0 +3.1