From 0cf3a2dec2bd1f6e8caf35f9b273f7508bb37164 Mon Sep 17 00:00:00 2001 From: ziffee Date: Thu, 12 Dec 2024 06:44:47 +0700 Subject: [PATCH 01/34] gw/secp: add priv-to-pub jets --- ext/urcrypt/build.zig.zon | 5 +++-- pkg/noun/jets.c | 26 ++++++++++++++++---------- pkg/noun/jets/137/tree.c | 2 ++ pkg/noun/jets/e/secp.c | 32 ++++++++++++++++++++++++++++++++ pkg/noun/jets/w.h | 2 ++ 5 files changed, 55 insertions(+), 12 deletions(-) diff --git a/ext/urcrypt/build.zig.zon b/ext/urcrypt/build.zig.zon index 9de2a9ad72..b9fece161b 100644 --- a/ext/urcrypt/build.zig.zon +++ b/ext/urcrypt/build.zig.zon @@ -14,8 +14,9 @@ .hash = "1220bb683a6df744e618f58a008eaae3eb62b70a78334cec676bd82b1b9e8e944eeb", }, .urcrypt = .{ - .url = "https://github.com/urbit/urcrypt/archive/e77a0998809f7552c455cf7bf8c1b68c141cce18.tar.gz", - .hash = "12202efcf40dcfed9a9b59bba4743cfd0c31364a6d7504724bd4d643b474ff69245b", + .path = "../../../urcrypt", + //.url = "https://github.com/urbit/urcrypt/archive/e77a0998809f7552c455cf7bf8c1b68c141cce18.tar.gz", + //.hash = "12202efcf40dcfed9a9b59bba4743cfd0c31364a6d7504724bd4d643b474ff69245b", }, }, .paths = .{ diff --git a/pkg/noun/jets.c b/pkg/noun/jets.c index 200f7d46ef..eba80f5c5f 100644 --- a/pkg/noun/jets.c +++ b/pkg/noun/jets.c @@ -882,9 +882,9 @@ _cj_kick_z(u3_noun cor, u3j_core* cop_u, u3j_harm* ham_u, u3_atom axe) else { u3_weak pro, ame; - ham_u->ice = c3y; + //ham_u->ice = c3y; pro = ham_u->fun_f(cor); - ham_u->ice = c3n; + //ham_u->ice = c3n; #ifdef U3_MEMORY_DEBUG u3a_lop(cod_w); @@ -893,23 +893,29 @@ _cj_kick_z(u3_noun cor, u3j_core* cop_u, u3j_harm* ham_u, u3_atom axe) u3z(cor); return pro; } - ham_u->liv = c3n; + //ham_u->liv = c3n; ame = _cj_soft(cor, axe); - ham_u->liv = c3y; + //ham_u->liv = c3y; if ( c3n == u3r_sing(ame, pro) ) { - u3l_log("test: %s %s: mismatch: good %x, bad %x", + //u3l_log("test: %s %s: mismatch: good %x, bad %x", + // cop_u->cos_c, + // (!strcmp(".2", ham_u->fcs_c)) ? "$" : ham_u->fcs_c, + // u3r_mug(ame), + // u3r_mug(pro)); + //ham_u->liv = c3n; + u3l_log("test: %s %s: mismatch", cop_u->cos_c, - (!strcmp(".2", ham_u->fcs_c)) ? "$" : ham_u->fcs_c, - u3r_mug(ame), - u3r_mug(pro)); - ham_u->liv = c3n; + (!strcmp(".2", ham_u->fcs_c)) ? "$" : ham_u->fcs_c + ); + u3m_p("good", ame); + u3m_p("baad", pro); return u3m_bail(c3__fail); } else { -#if 0 +#if 1 u3l_log("test: %s %s", cop_u->cos_c, (!strcmp(".2", ham_u->fcs_c)) ? "$" : ham_u->fcs_c); diff --git a/pkg/noun/jets/137/tree.c b/pkg/noun/jets/137/tree.c index fec03edf1f..60a25a6743 100644 --- a/pkg/noun/jets/137/tree.c +++ b/pkg/noun/jets/137/tree.c @@ -183,6 +183,7 @@ static u3j_core _137_hex_scr_d[] = static u3j_harm _137_hex_secp_secp256k1_make_a[] = {{".2", u3we_make, c3y}, {}}; static u3j_harm _137_hex_secp_secp256k1_sign_a[] = {{".2", u3we_sign, c3y}, {}}; static u3j_harm _137_hex_secp_secp256k1_reco_a[] = {{".2", u3we_reco, c3y}, {}}; +static u3j_harm _137_hex_secp_secp256k1_priv_to_pub_a[] = {{".2", u3we_priv_to_pub, c3n}, {}}; static u3j_harm _137_hex_secp_secp256k1_schnorr_sosi_a[] = {{".2", u3we_sosi}, {}}; @@ -202,6 +203,7 @@ static u3j_core _137_hex_secp_secp256k1_d[] = { { "make", 7, _137_hex_secp_secp256k1_make_a, 0, no_hashes }, { "sign", 7, _137_hex_secp_secp256k1_sign_a, 0, no_hashes }, { "reco", 7, _137_hex_secp_secp256k1_reco_a, 0, no_hashes }, + { "priv-to-pub", 7, _137_hex_secp_secp256k1_priv_to_pub_a, 0, no_hashes }, { "schnorr", 7, 0, _137_hex_secp_secp256k1_schnorr_d, no_hashes }, diff --git a/pkg/noun/jets/e/secp.c b/pkg/noun/jets/e/secp.c index fa0fbd20c4..995935e570 100644 --- a/pkg/noun/jets/e/secp.c +++ b/pkg/noun/jets/e/secp.c @@ -296,3 +296,35 @@ u3we_sove(u3_noun cor) return _cqes_sove(pub, mes, sig); } } + +static u3_atom +_cqes_priv_to_pub(u3_atom sek) { + c3_y scalar[32], point[65]; + + if (0 != u3r_bytes_fit(32, scalar, sek)) { + return u3m_bail(c3__exit); + } + + if (0 != urcrypt_secp_point_from_scalar(sec_u, scalar, point)) { + return u3m_bail(c3__exit); + } + + return u3nc( + u3i_bytes(32, point + 1), + u3i_bytes(32, point + 33) + ); +} + +u3_noun +u3we_priv_to_pub(u3_noun cor) +{ + + u3_noun a = u3r_at(u3x_sam, cor); + + if ( (u3_none == a) || (c3n == u3ud(a)) ) { + return u3m_bail(c3__exit); + } + else { + return _cqes_priv_to_pub(a); + } +} diff --git a/pkg/noun/jets/w.h b/pkg/noun/jets/w.h index 78494314fe..ef4c4f8b54 100644 --- a/pkg/noun/jets/w.h +++ b/pkg/noun/jets/w.h @@ -231,6 +231,8 @@ u3_noun u3we_sosi(u3_noun); u3_noun u3we_sove(u3_noun); + u3_noun u3we_priv_to_pub(u3_noun); + u3_noun u3we_en_base16(u3_noun); u3_noun u3we_de_base16(u3_noun); From ad533b297c64a94a75703782b060bab8068c09c7 Mon Sep 17 00:00:00 2001 From: cyclomancer Date: Tue, 14 Jan 2025 09:38:03 -0600 Subject: [PATCH 02/34] Add point tweak, compressed points --- pkg/noun/jets/137/tree.c | 4 ++ pkg/noun/jets/e/secp.c | 83 +++++++++++++++++++++++++++++++++++++--- pkg/noun/jets/w.h | 2 + 3 files changed, 83 insertions(+), 6 deletions(-) diff --git a/pkg/noun/jets/137/tree.c b/pkg/noun/jets/137/tree.c index 60a25a6743..bcdaa37b76 100644 --- a/pkg/noun/jets/137/tree.c +++ b/pkg/noun/jets/137/tree.c @@ -184,6 +184,8 @@ static u3j_harm _137_hex_secp_secp256k1_make_a[] = {{".2", u3we_make, c3y}, {}}; static u3j_harm _137_hex_secp_secp256k1_sign_a[] = {{".2", u3we_sign, c3y}, {}}; static u3j_harm _137_hex_secp_secp256k1_reco_a[] = {{".2", u3we_reco, c3y}, {}}; static u3j_harm _137_hex_secp_secp256k1_priv_to_pub_a[] = {{".2", u3we_priv_to_pub, c3n}, {}}; +static u3j_harm _137_hex_secp_secp256k1_tweak_sec_a[] = {{".2", u3we_tweak_sec, c3n}, {}}; +static u3j_harm _137_hex_secp_secp256k1_tweak_pub_a[] = {{".2", u3we_tweak_pub, c3n}, {}}; static u3j_harm _137_hex_secp_secp256k1_schnorr_sosi_a[] = {{".2", u3we_sosi}, {}}; @@ -204,6 +206,8 @@ static u3j_core _137_hex_secp_secp256k1_d[] = { "sign", 7, _137_hex_secp_secp256k1_sign_a, 0, no_hashes }, { "reco", 7, _137_hex_secp_secp256k1_reco_a, 0, no_hashes }, { "priv-to-pub", 7, _137_hex_secp_secp256k1_priv_to_pub_a, 0, no_hashes }, + { "tweak-sec", 7, _137_hex_secp_secp256k1_tweak_sec_a, 0, no_hashes }, + { "tweak-pub", 7, _137_hex_secp_secp256k1_tweak_pub_a, 0, no_hashes }, { "schnorr", 7, 0, _137_hex_secp_secp256k1_schnorr_d, no_hashes }, diff --git a/pkg/noun/jets/e/secp.c b/pkg/noun/jets/e/secp.c index 995935e570..7a3ca706a5 100644 --- a/pkg/noun/jets/e/secp.c +++ b/pkg/noun/jets/e/secp.c @@ -298,21 +298,92 @@ u3we_sove(u3_noun cor) } static u3_atom +_cqes_tweak_sec(u3_atom sek, u3_atom tek) { + c3_y scalar[32], tweak[32]; + + if (0 != u3r_bytes_fit(32, scalar, sek)) { + return u3m_bail(c3__exit); + } + + if (0 != u3r_bytes_fit(32, tweak, tek)) { + return u3m_bail(c3__exit); + } + + if (0 != urcrypt_secp_scalar_tweak_add(sec_u, scalar, tweak)) { + return u3m_bail(c3__exit); + } + + return u3i_bytes(32, scalar); +} + +u3_noun +u3we_tweak_sec(u3_noun cor) +{ + u3_noun sek, tek; + + if ( (c3n == u3r_mean(cor, + u3x_sam_2, &sek, + u3x_sam_3, &tek, + 0)) || + (c3n == u3ud(sek)) || + (c3n == u3ud(tek))) { + return u3m_bail(c3__exit); + } + else { + return _cqes_tweak_sec(sek, tek); + } +} + +static u3_atom +_cqes_tweak_pub(u3_atom pub, u3_atom tek) { + c3_y cmp_point[33], tweak[32]; + + if (0 != u3r_bytes_fit(33, cmp_point, pub)) { + return u3m_bail(c3__exit); + } + + if (0 != u3r_bytes_fit(32, tweak, tek)) { + return u3m_bail(c3__exit); + } + + if (0 != urcrypt_secp_cmp_point_tweak_add(sec_u, cmp_point, tweak)) { + return u3m_bail(c3__exit); + } + + return u3i_bytes(33, cmp_point); +} + +u3_noun +u3we_tweak_pub(u3_noun cor) +{ + u3_noun pub, tek; + + if ( (c3n == u3r_mean(cor, + u3x_sam_2, &pub, + u3x_sam_3, &tek, + 0)) || + (c3n == u3ud(pub)) || + (c3n == u3ud(tek))) { + return u3m_bail(c3__exit); + } + else { + return _cqes_tweak_pub(pub, tek); + } +} + +static u3_noun _cqes_priv_to_pub(u3_atom sek) { - c3_y scalar[32], point[65]; + c3_y scalar[32], cmp_point[33]; if (0 != u3r_bytes_fit(32, scalar, sek)) { return u3m_bail(c3__exit); } - if (0 != urcrypt_secp_point_from_scalar(sec_u, scalar, point)) { + if (0 != urcrypt_secp_cmp_point_from_scalar(sec_u, scalar, cmp_point)) { return u3m_bail(c3__exit); } - return u3nc( - u3i_bytes(32, point + 1), - u3i_bytes(32, point + 33) - ); + return u3i_bytes(33, cmp_point); } u3_noun diff --git a/pkg/noun/jets/w.h b/pkg/noun/jets/w.h index ef4c4f8b54..6c2caf4f72 100644 --- a/pkg/noun/jets/w.h +++ b/pkg/noun/jets/w.h @@ -232,6 +232,8 @@ u3_noun u3we_sove(u3_noun); u3_noun u3we_priv_to_pub(u3_noun); + u3_noun u3we_tweak_sec(u3_noun); + u3_noun u3we_tweak_pub(u3_noun); u3_noun u3we_en_base16(u3_noun); u3_noun u3we_de_base16(u3_noun); From 61d6fa79ff851110fbc4f748423691b6097ffe5a Mon Sep 17 00:00:00 2001 From: ziffee Date: Mon, 24 Mar 2025 18:17:14 -0600 Subject: [PATCH 03/34] king: use struct u3_ship --- pkg/noun/noun.h | 1 + pkg/noun/ship.c | 72 +++++++++++--------------------- pkg/noun/ship.h | 18 ++++---- pkg/vere/io/ames.c | 92 ++++++++++++++--------------------------- pkg/vere/io/conn.c | 4 +- pkg/vere/io/http.c | 6 +-- pkg/vere/io/mesa.c | 83 ++++++++++++++----------------------- pkg/vere/io/mesa/pact.c | 8 ++-- pkg/vere/pier.c | 16 +++---- pkg/vere/vere.h | 2 +- 10 files changed, 115 insertions(+), 187 deletions(-) diff --git a/pkg/noun/noun.h b/pkg/noun/noun.h index 965330a519..565c6eedd3 100644 --- a/pkg/noun/noun.h +++ b/pkg/noun/noun.h @@ -19,6 +19,7 @@ #include "log.h" #include "nock.h" #include "retrieve.h" +#include "ship.h" #include "trace.h" #include "urth.h" #include "xtract.h" diff --git a/pkg/noun/ship.c b/pkg/noun/ship.c index 6aebff71aa..95c5414c9b 100644 --- a/pkg/noun/ship.c +++ b/pkg/noun/ship.c @@ -5,57 +5,33 @@ #include "vortex.h" #include "ship.h" -static inline void -_s_chub_to_bytes(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; -} - -static inline c3_d -_s_bytes_to_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; -} - void u3_ship_to_bytes(u3_ship who_u, c3_y len_y, c3_y* buf_y) { c3_y sip_y[16] = {0}; - _s_chub_to_bytes(sip_y, who_u[0]); - _s_chub_to_bytes(sip_y + 8, who_u[1]); + c3_etch_chub(sip_y, who_u.hed_d); + c3_etch_chub(sip_y + 8, who_u.tel_d); memcpy(buf_y, sip_y, c3_min(16, len_y)); } -void -u3_ship_of_bytes(u3_ship who_u, c3_y len_y, c3_y* buf_y) +u3_ship +u3_ship_of_bytes(c3_y len_y, c3_y* buf_y) { + u3_ship who_u; c3_y sip_y[16] = {0}; memcpy(sip_y, buf_y, c3_min(16, len_y)); - who_u[0] = _s_bytes_to_chub(sip_y); - who_u[1] = _s_bytes_to_chub(sip_y + 8); + who_u.hed_d = c3_sift_chub(sip_y); + who_u.tel_d = c3_sift_chub(sip_y + 8); + return who_u; } u3_atom u3_ship_to_noun(u3_ship who_u) { - return u3i_chubs(2, who_u); + return u3i_chubs(2, &who_u.hed_d); } c3_c* @@ -67,43 +43,45 @@ u3_ship_to_string(u3_ship who_u) return who_c; } -void -u3_ship_of_noun(u3_ship who_u, u3_noun who) +u3_ship +u3_ship_of_noun(u3_noun who) { - u3r_chubs(0, 2, who_u, who); + u3_ship who_u; + u3r_chubs(0, 2, &who_u.hed_d, who); + return who_u; } c3_o u3_ships_equal(u3_ship sip_u, u3_ship sap_u) { - return __((sip_u[0] == sap_u[0]) && (sip_u[1] == sap_u[1])); + return __((sip_u.hed_d == sap_u.hed_d) && (sip_u.tel_d == sap_u.tel_d)); } void u3_ship_copy(u3_ship des_u, u3_ship src_u) { - des_u[0] = src_u[0]; - des_u[1] = src_u[1]; + des_u.hed_d = src_u.hed_d; + des_u.tel_d = src_u.tel_d; } c3_l u3_ship_rank(u3_ship who_u) { - if ( who_u[1] ) return c3__pawn; - else if ( who_u[0] >> 32 ) return c3__earl; - else if ( who_u[0] >> 16 ) return c3__duke; - else if ( who_u[0] >> 8 ) return c3__king; + if ( who_u.tel_d ) return c3__pawn; + else if ( who_u.hed_d >> 32 ) return c3__earl; + else if ( who_u.hed_d >> 16 ) return c3__duke; + else if ( who_u.hed_d >> 8 ) return c3__king; else return c3__czar; } c3_y -u3_ship_czar(u3_ship who_u) { return who_u[0] & 0xFF; } +u3_ship_czar(u3_ship who_u) { return who_u.hed_d & 0xFF; } c3_s -u3_ship_king(u3_ship who_u) { return who_u[0] & 0xffff; } +u3_ship_king(u3_ship who_u) { return who_u.hed_d & 0xffff; } c3_w -u3_ship_duke(u3_ship who_u) { return who_u[0] & 0xffffffff; } +u3_ship_duke(u3_ship who_u) { return who_u.hed_d & 0xffffffff; } c3_d -u3_ship_earl(u3_ship who_u) { return who_u[0]; } +u3_ship_earl(u3_ship who_u) { return who_u.hed_d; } diff --git a/pkg/noun/ship.h b/pkg/noun/ship.h index 8104c42d5b..e23620992a 100644 --- a/pkg/noun/ship.h +++ b/pkg/noun/ship.h @@ -4,19 +4,22 @@ #include "c3/c3.h" #include "types.h" -typedef c3_d u3_ship[2]; +typedef struct _u3_ship { + c3_d hed_d; + c3_d tel_d; +} u3_ship; void -u3_ship_to_bytes(c3_d sip_d[2], c3_y len_y, c3_y* buf_y); +u3_ship_to_bytes(u3_ship who_u, c3_y len_y, c3_y* buf_y); -void -u3_ship_of_bytes(u3_ship who_u, c3_y len_y, c3_y* buf_y); +u3_ship +u3_ship_of_bytes(c3_y len_y, c3_y* buf_y); u3_atom u3_ship_to_noun(u3_ship who_u); -void -u3_ship_of_noun(u3_ship who_u, u3_noun who); +u3_ship +u3_ship_of_noun(u3_noun who); c3_c* u3_ship_to_string(u3_ship who_u); @@ -27,9 +30,6 @@ u3_ships_equal(u3_ship sip_u, u3_ship sap_u); c3_l u3_ship_rank(u3_ship who_u); -void -u3_ship_copy(u3_ship des_u, u3_ship src_u); - /** * Returns a ship's galaxy byte prefix. */ diff --git a/pkg/vere/io/ames.c b/pkg/vere/io/ames.c index d6d03df010..8940407e77 100644 --- a/pkg/vere/io/ames.c +++ b/pkg/vere/io/ames.c @@ -119,11 +119,11 @@ typedef enum u3_stun_state { /* u3_prel: ames/fine packet prelude */ typedef struct _u3_prel { - c3_y sic_y; // sender life tick - c3_y ric_y; // receiver life tick - c3_d sen_d[2]; // sender/requester - c3_d rec_d[2]; // receiver/responder - c3_d rog_d; // origin lane (optional) + c3_y sic_y; // sender life tick + c3_y ric_y; // receiver life tick + u3_ship sen_u; // sender/requester + u3_ship rec_u; // receiver/responder + c3_d rog_d; // origin lane (optional) } u3_prel; /* u3_peep: unsigned fine request body @@ -361,31 +361,6 @@ _ames_check_mug(u3_pact* pac_u) ? c3y : c3n); } -/* _ames_ship_to_chubs(): pack [len_y] bytes into c3_d[2] -*/ -static inline void -_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] = 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. -*/ -static inline void -_ames_ship_of_chubs(c3_d sip_d[2], c3_y len_y, c3_y* buf_y) -{ - c3_y sip_y[16] = {0}; - - 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)); -} - /* _ames_sift_head(): parse packet header. */ static void @@ -435,13 +410,13 @@ _ames_sift_prel(u3_head* hed_u, // parse sender ship // sen_y = 2 << hed_u->sac_y; - _ames_ship_to_chubs(pre_u->sen_d, sen_y, buf_y + cur_w); + u3_ship_to_bytes(pre_u->sen_u, sen_y, buf_y + cur_w); cur_w += sen_y; // parse receiver ship // rec_y = 2 << hed_u->rac_y; - _ames_ship_to_chubs(pre_u->rec_d, rec_y, buf_y + cur_w); + u3_ship_to_bytes(pre_u->rec_u, rec_y, buf_y + cur_w); cur_w += rec_y; } @@ -603,13 +578,13 @@ _ames_etch_prel(u3_head* hed_u, u3_prel* pre_u, c3_y* buf_y) // write sender ship // c3_y sen_y = 2 << hed_u->sac_y; - _ames_ship_of_chubs(pre_u->sen_d, sen_y, buf_y + cur_w); + pre_u->sen_u = u3_ship_of_bytes(sen_y, buf_y + cur_w); cur_w += sen_y; // write receiver ship // c3_y rec_y = 2 << hed_u->rac_y; - _ames_ship_of_chubs(pre_u->rec_d, rec_y, buf_y + cur_w); + pre_u->rec_u = u3_ship_of_bytes(rec_y, buf_y + cur_w); cur_w += rec_y; } @@ -1259,7 +1234,7 @@ _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); + u3_noun our = u3_ship_to_noun(sam_u->pir_u->who_u); // if we are a galaxy, don't STUN // @@ -1470,8 +1445,8 @@ _ames_send_many(u3_pact* pac_u, u3_noun las, c3_o for_o) } if ( u3C.wag_w & u3o_verbose ) { - u3_noun sen = u3dc("scot", 'p', u3i_chubs(2, pac_u->pre_u.sen_d)); - u3_noun rec = u3dc("scot", 'p', u3i_chubs(2, pac_u->pre_u.rec_d)); + u3_noun sen = u3dc("scot", 'p', u3_ship_to_noun(pac_u->pre_u.sen_u)); + u3_noun rec = u3dc("scot", 'p', u3_ship_to_noun(pac_u->pre_u.rec_u)); c3_c* sen_c = u3r_string(sen); c3_c* rec_c = u3r_string(rec); c3_y* pip_y = (c3_y*)&pac_u->lan_u.pip_w; @@ -1497,13 +1472,11 @@ _ames_send_many(u3_pact* pac_u, u3_noun las, c3_o for_o) } else { c3_o sen_o = c3y; - c3_d who_d[2]; if ( c3y == tag ) { - u3r_chubs(0, 2, who_d, dat); + u3_ship who_u = u3_ship_of_noun(dat); - if ( (who_d[0] == sam_u->pir_u->who_d[0]) - && (who_d[1] == sam_u->pir_u->who_d[1]) ) + if ( u3_ships_equal(who_u, sam_u->pir_u->who_u ) ) { sen_o = c3n; if ( u3C.wag_w & u3o_verbose ) { @@ -1548,7 +1521,7 @@ _ames_lane_scry_cb(void* vod_p, u3_noun nun) // cache the scry result for later use // _ames_lane_into_cache(sam_u->lax_p, - u3i_chubs(2, pac_u->pre_u.rec_d), + u3_ship_to_noun(pac_u->pre_u.rec_u), u3k(las)); // if there are lanes, send the packet on them; otherwise drop it @@ -1584,15 +1557,14 @@ _ames_try_send(u3_pact* pac_u, c3_o for_o) // if the recipient is a galaxy, their lane is always &+~gax // - if ( (256 > pac_u->pre_u.rec_d[0]) - && (0 == pac_u->pre_u.rec_d[1]) ) + if ( c3__czar == u3_ship_rank(pac_u->pre_u.rec_u) ) { - lac = u3nc(u3nc(c3y, (c3_y)pac_u->pre_u.rec_d[0]), u3_nul); + lac = u3nc(u3nc(c3y, (c3_y)pac_u->pre_u.rec_u.hed_d), u3_nul); } // otherwise, try to get the lane from cache // else { - u3_noun key = u3i_chubs(2, pac_u->pre_u.rec_d); + u3_noun key = u3_ship_to_noun(pac_u->pre_u.rec_u); lac = _ames_lane_from_cache(sam_u->lax_p, key, sam_u->nal_o); } @@ -1638,7 +1610,7 @@ _ames_try_send(u3_pact* pac_u, c3_o for_o) pan_u->pac_u = pac_u; pan_u->for_o = for_o; - u3_noun pax = _lane_scry_path(u3i_chubs(2, pac_u->pre_u.rec_d)); + u3_noun pax = _lane_scry_path(u3_ship_to_noun(pac_u->pre_u.rec_u)); // if forwarding, enqueue the packet and scry for the lane // @@ -1670,10 +1642,10 @@ _ames_try_send(u3_pact* pac_u, c3_o for_o) static c3_o _ames_skip(u3_prel* pre_u) { - if ( pre_u->sen_d[1] == 0 && - ( pre_u->sen_d[0] == 0x743a17a6 - || pre_u->sen_d[0] == 0xea99acb6 - || pre_u->sen_d[0] == 0x10100 + if ( pre_u->sen_u[1] == 0 && + ( pre_u->sen_u[0] == 0x743a17a6 + || pre_u->sen_u[0] == 0xea99acb6 + || pre_u->sen_u[0] == 0x10100 ) ) { return c3n; } @@ -1836,8 +1808,8 @@ _fine_hear_request(u3_pact* req_u, c3_w cur_w) res_u->pre_u = (u3_prel) { .sic_y = req_u->pre_u.ric_y, .ric_y = req_u->pre_u.sic_y, - .sen_d = { req_u->pre_u.rec_d[0], req_u->pre_u.rec_d[1] }, - .rec_d = { req_u->pre_u.sen_d[0], req_u->pre_u.sen_d[1] }, + .sen_u = req_u->pre_u.rec_u, + .rec_u = req_u->pre_u.sen_u, .rog_d = 0 }; @@ -1964,8 +1936,7 @@ _ames_try_forward(u3_pact* pac_u) // insert origin lane if needed // if ( c3n == pac_u->hed_u.rel_o - && !( ( 256 > pac_u->pre_u.sen_d[0] ) - && ( 0 == pac_u->pre_u.sen_d[1] ) ) ) + && ( c3__czar == u3_ship_rank(pac_u->pre_u.sen_u) ) ) { c3_y* old_y; c3_w old_w, cur_w; @@ -2124,8 +2095,7 @@ _ames_hear(u3_ames* sam_u, // we might want to forward statelessly // if ( (c3y == sam_u->fig_u.see_o) - && ( (pac_u->pre_u.rec_d[0] != sam_u->pir_u->who_d[0]) - || (pac_u->pre_u.rec_d[1] != sam_u->pir_u->who_d[1]) ) ) + && ( u3_ships_equal(pac_u->pre_u.rec_u, sam_u->pir_u->who_u) ) ) { if ( c3y == sam_u->sat_u.for_o ) { _ames_try_forward(pac_u); @@ -2282,7 +2252,7 @@ _ames_put_dear(c3_c* ship, bool fake, c3_w s_addr, c3_s port, void* context) return; } - u3_noun our = u3i_chubs(2, sam_u->pir_u->who_d); + u3_noun our = u3_ship_to_noun(sam_u->pir_u->who_u); if (our == u3t(whu)) { u3z(whu); u3z(our); @@ -2307,13 +2277,13 @@ static void _ames_io_start(u3_ames* sam_u) { c3_s por_s = sam_u->pir_u->por_s; - u3_noun who = u3i_chubs(2, sam_u->pir_u->who_d); + u3_noun who = u3_ship_to_noun(sam_u->pir_u->who_u); c3_o zar_o = _ames_is_czar(who); c3_i ret_i; if ( c3y == zar_o ) { - c3_y num_y = (c3_y)sam_u->pir_u->who_d[0]; + c3_y num_y = (c3_y)sam_u->pir_u->who_u.hed_d; c3_s zar_s = _ames_czar_port(num_y); if ( 0 == por_s ) { @@ -2887,7 +2857,7 @@ u3_ames_io_init(u3_pier* pir_u) uv_timer_init(u3L, &sam_u->nat_u.tim_u); // enable forwarding on galaxies only - u3_noun who = u3i_chubs(2, sam_u->pir_u->who_d); + u3_noun who = u3_ship_to_noun(sam_u->pir_u->who_u); u3_noun rac = u3do("clan:title", who); sam_u->sat_u.for_o = ( c3__czar == rac ) ? c3y : c3n; diff --git a/pkg/vere/io/conn.c b/pkg/vere/io/conn.c index 85966f06f5..d740c339a4 100644 --- a/pkg/vere/io/conn.c +++ b/pkg/vere/io/conn.c @@ -545,7 +545,7 @@ _conn_read_peel(u3_conn* con_u, u3_noun dat) // current ship. // case c3__who: { - res = u3nc(u3_nul, u3i_chubs(2, pir_u->who_d)); + res = u3nc(u3_nul, u3_ship_to_noun(pir_u->who_u)); } } } @@ -721,7 +721,7 @@ _conn_init_sock(u3_shan* san_u) { #ifdef _WIN32 u3_pier* pir_u = san_u->con_u->car_u.pir_u; - u3_atom who = u3dc("scot", c3__p, u3i_chubs(2, pir_u->who_d)); + u3_atom who = u3dc("scot", c3__p, u3_ship_to_noun(pir_u->who_u)); c3_c* who_c = u3r_string(who); c3_c pip_c[256]; c3_i ret_i; diff --git a/pkg/vere/io/http.c b/pkg/vere/io/http.c index e2f8990452..f17deac045 100644 --- a/pkg/vere/io/http.c +++ b/pkg/vere/io/http.c @@ -822,7 +822,7 @@ _get_beam(u3_hreq* req_u, c3_c* txt_c, c3_w len_w) if ( 0 == i_w ) { u3_http* htp_u = req_u->hon_u->htp_u; u3_httd* htd_u = htp_u->htd_u; - *wer = u3dc("scot", 'p', u3i_chubs(2, htd_u->car_u.pir_u->who_d)); + *wer = u3dc("scot", 'p', u3_ship_to_noun(htd_u->car_u.pir_u->who_u)); } else if ( 1 == i_w ) { *wer = c3__base; @@ -963,7 +963,7 @@ _http_req_dispatch(u3_hreq* req_u, u3_noun req) // peek or respond from cache // if ( c3y == req_u->peq_u->las_o ) { - u3_noun our = u3dc("scot", 'p', u3i_chubs(2, htd_u->car_u.pir_u->who_d)); + u3_noun our = u3dc("scot", 'p', u3_ship_to_noun(htd_u->car_u.pir_u->who_u)); if ( c3y == u3r_sing(our, bem.who) ) { u3_pier_peek_last(htd_u->car_u.pir_u, gang, c3__ex, u3k(bem.des), spur, req_u->peq_u, _http_scry_cb); @@ -2881,7 +2881,7 @@ u3_http_io_init(u3_pier* pir_u) { u3_noun key = u3dt("cat", 3, u3i_string("urbauth-"), - u3dc("scot", 'p', u3i_chubs(2, pir_u->who_d))); + u3dc("scot", 'p', u3_ship_to_noun(pir_u->who_u))); htd_u->fig_u.ses = u3_nul; htd_u->fig_u.key_c = u3r_string(key); u3z(key); diff --git a/pkg/vere/io/mesa.c b/pkg/vere/io/mesa.c index 20041121aa..488a9615bc 100644 --- a/pkg/vere/io/mesa.c +++ b/pkg/vere/io/mesa.c @@ -144,23 +144,8 @@ typedef struct _u3_pit_entry { arena are_u; } u3_pit_entry; -typedef struct _u3_shap { - c3_d hed_d; - c3_d tel_d; -} u3_shap; -static u3_shap u3_ship_to_shap( u3_ship ship ) -{ - return (u3_shap){ship[0], ship[1]}; -} - -static void u3_shap_to_ship( u3_ship ship, u3_shap shap ) -{ - ship[0] = shap.hed_d; - ship[1] = shap.tel_d; -} - static void u3_free_pit( u3_pit_entry* pit_u ) { arena_free(&pit_u->are_u); @@ -185,12 +170,12 @@ static uint64_t u3_cmpr_str( u3_str key1, u3_str key2 ) return key1.len_w == key2.len_w && memcmp( key1.str_c, key2.str_c, key1.len_w ) == 0; } -static uint64_t u3_cmpr_shap( u3_shap ship1, u3_shap ship2 ) +static uint64_t u3_cmpr_ship( u3_ship ship1, u3_ship ship2 ) { return ship1.hed_d == ship2.hed_d && ship1.tel_d == ship2.tel_d; } -static uint64_t u3_hash_shap(u3_shap ship) +static uint64_t u3_hash_ship(u3_ship ship) { uint64_t combined = ship.hed_d ^ (ship.tel_d * 0x9e3779b97f4a7c15ull); combined ^= combined >> 23; @@ -217,9 +202,9 @@ typedef struct _u3_pend_req u3_pend_req; #include "verstable.h" #define NAME gag_map -#define KEY_TY u3_shap -#define HASH_FN u3_hash_shap -#define CMPR_FN u3_cmpr_shap +#define KEY_TY u3_ship +#define HASH_FN u3_hash_ship +#define CMPR_FN u3_cmpr_ship #define VAL_TY u3_gage* #include "verstable.h" @@ -269,9 +254,9 @@ static void u3_free_line( u3_mesa_line* lin_u ) typedef struct _u3_peer u3_peer; #define NAME per_map -#define KEY_TY u3_shap -#define HASH_FN u3_hash_shap -#define CMPR_FN u3_cmpr_shap +#define KEY_TY u3_ship +#define HASH_FN u3_hash_ship +#define CMPR_FN u3_cmpr_ship #define VAL_TY u3_peer* #include "verstable.h" @@ -543,7 +528,6 @@ static void _mesa_copy_name(u3_mesa_name* des_u, u3_mesa_name* src_u, arena* are_u) { memcpy(des_u, src_u, sizeof(u3_mesa_name)); - u3_ship_copy(des_u->her_u, src_u->her_u); des_u->str_u.str_c = new(are_u, c3_c, src_u->str_u.len_w); memcpy(des_u->str_u.str_c, src_u->str_u.str_c, src_u->str_u.len_w); des_u->pat_c = des_u->str_u.str_c + (src_u->pat_c - src_u->str_u.str_c); @@ -603,7 +587,7 @@ u3_mesa_encode_lane(sockaddr_in lan_u) { static u3_peer* _mesa_get_peer(u3_mesa* sam_u, u3_ship her) { - per_map_itr itr_u = vt_get(&sam_u->per_u, u3_ship_to_shap(her)); + per_map_itr itr_u = vt_get(&sam_u->per_u, her); if ( vt_is_end(itr_u) ) { return NULL; } @@ -613,11 +597,10 @@ _mesa_get_peer(u3_mesa* sam_u, u3_ship her) static void _mesa_put_peer(u3_mesa* sam_u, u3_ship her_u, u3_peer* per_u) { - u3_shap him_u = u3_ship_to_shap(her_u); - per_map_itr itr_u = vt_get(&sam_u->per_u, him_u); + per_map_itr itr_u = vt_get(&sam_u->per_u, her_u); if ( vt_is_end(itr_u) ) { - itr_u = vt_insert(&sam_u->per_u, him_u, per_u); + itr_u = vt_insert(&sam_u->per_u, her_u, per_u); if ( vt_is_end(itr_u) ) { fprintf(stderr, "mesa: cannot allocate memory for peer, dying"); @@ -700,13 +683,13 @@ _mesa_get_direct_lane(u3_mesa* sam_u, u3_ship her_u) adr_u.sin_family = AF_INET; if ( c3__czar == u3_ship_rank(her_u) ) { - c3_s por_s = _ames_czar_port(her_u[0]); - adr_u.sin_addr.s_addr = htonl(u3_Host.imp_u[her_u[0]]); + c3_s por_s = _ames_czar_port(her_u.hed_d); + adr_u.sin_addr.s_addr = htonl(u3_Host.imp_u[her_u.hed_d]); adr_u.sin_port = htons(por_s); return adr_u; } - per_map_itr itr_u = vt_get(&sam_u->per_u, u3_ship_to_shap(her_u)); + per_map_itr itr_u = vt_get(&sam_u->per_u, her_u); if ( vt_is_end(itr_u) ) { return adr_u; } @@ -738,7 +721,7 @@ _mesa_get_czar_lane(u3_mesa* sam_u, c3_y imp_y) */ static u3_gage* _mesa_get_gage(u3_mesa* sam_u, u3_ship her_u) { - gag_map_itr itr_u = vt_get(&sam_u->gag_u, u3_ship_to_shap(her_u)); + gag_map_itr itr_u = vt_get(&sam_u->gag_u, her_u); if ( vt_is_end(itr_u) ) { return NULL; } @@ -751,7 +734,7 @@ _mesa_get_gage(u3_mesa* sam_u, u3_ship her_u) { static void _mesa_put_gage(u3_mesa* sam_u, u3_ship her_u, u3_gage* gag_u) { - gag_map_itr itr_u = vt_insert(&sam_u->gag_u, u3_ship_to_shap(her_u), gag_u); + gag_map_itr itr_u = vt_insert(&sam_u->gag_u, her_u, gag_u); if ( vt_is_end(itr_u) ) { fprintf(stderr, "mesa: cannot allocate memory for gage, dying"); u3_king_bail(); @@ -1088,8 +1071,8 @@ _mesa_send_modal(u3_peer* per_u, uv_buf_t buf_u, u3_pit_addr* las_u) memcpy(sen_y, buf_u.base, len_w); u3_ship gal_u = {0}; - gal_u[0] = per_u->imp_y; - c3_o our_o = u3_ships_equal(gal_u, sam_u->pir_u->who_d); + gal_u.hed_d = per_u->imp_y; + c3_o our_o = u3_ships_equal(gal_u, sam_u->pir_u->who_u); if ( ( c3y == _mesa_is_direct_mode(per_u) ) || // if we are the sponsor of the ship, don't send to ourselves @@ -1664,7 +1647,7 @@ _mesa_ef_send(u3_mesa* sam_u, u3_noun las, u3_noun pac) u3_mesa_request_data* dat_u = &res_u->dat_u; { dat_u->sam_u = sam_u; - u3_ship_copy(dat_u->her_u, nam_u->her_u); + dat_u->her_u = nam_u->her_u; dat_u->nam_u = nam_u; dat_u->las_u = _mesa_lanes_to_addrs(las, &res_u->are_u); dat_u->buf_y = buf_y; @@ -1727,8 +1710,7 @@ static c3_o _mesa_kick(u3_mesa* sam_u, u3_noun tag, u3_noun dat) ret_o = _ames_kick_newt(u3_Host.sam_u, u3k(tag), u3k(dat)); } break; case c3__nail: { - u3_ship who_u; - u3_ship_of_noun(who_u, u3h(dat)); + u3_ship who_u = u3_ship_of_noun(u3h(dat)); u3_peer* per_u = _mesa_get_peer(sam_u, who_u); if ( NULL == per_u ) { @@ -2132,8 +2114,7 @@ _saxo_cb(void* vod_p, u3_noun nun) if ( sax != u3_none ) { u3_noun her = u3h(sax); - u3_ship her_u; - u3_ship_of_noun(her_u, her); + u3_ship her_u = u3_ship_of_noun(her); u3_peer* new_u = _mesa_get_peer(per_u->sam_u, her_u); if ( new_u != NULL ) { per_u = new_u; @@ -2196,8 +2177,7 @@ _meet_peer(u3_mesa* sam_u, u3_peer* per_u, u3_ship her_u) u3_noun her = u3_ship_to_noun(her_u); u3_noun gan = u3nc(u3_nul, u3_nul); - per_u->her_u[0] = her_u[0]; - per_u->her_u[1] = her_u[1]; + per_u->her_u = her_u; u3_noun pax = u3nc(u3dc("scot", c3__p, her), u3_nul); u3_pier_peek_last(sam_u->pir_u, gan, c3__j, c3__saxo, pax, per_u, _saxo_cb); @@ -2474,13 +2454,12 @@ _mesa_forward_request(u3_mesa* sam_u, u3_mesa_pict* pic_u, sockaddr_in lan_u) #endif per_u = new(&sam_u->par_u, u3_peer, 1); _init_peer(sam_u, per_u); - per_u->her_u[0] = pac_u->pek_u.nam_u.her_u[0]; - per_u->her_u[1] = pac_u->pek_u.nam_u.her_u[1]; + per_u->her_u = pac_u->pek_u.nam_u.her_u; _get_peer_lanes(sam_u, per_u); // forward-lanes return; } - if ( c3y == sam_u->for_o && sam_u->pir_u->who_d[0] == per_u->imp_y ) { + if ( c3y == sam_u->for_o && sam_u->pir_u->who_u.hed_d == per_u->imp_y ) { // if ( c3y == sam_u->for_o ) { sockaddr_in lin_u = _mesa_get_direct_lane(sam_u, pac_u->pek_u.nam_u.her_u); if ( _mesa_is_lane_zero(lin_u) == c3y) { @@ -2518,7 +2497,7 @@ _mesa_hear_page(u3_mesa_pict* pic_u, sockaddr_in lan_u) u3_mesa_pact* pac_u = &pic_u->pac_u; u3_mesa_name* nam_u = &pac_u->pag_u.nam_u; - c3_o our_o = u3_ships_equal(nam_u->her_u, sam_u->pir_u->who_d); + c3_o our_o = u3_ships_equal(nam_u->her_u, sam_u->pir_u->who_u); u3_peer* per_u = _mesa_get_peer(sam_u, nam_u->her_u); c3_o new_o = c3n; @@ -2689,7 +2668,7 @@ _mesa_hear_peek(u3_mesa_pict* pic_u, sockaddr_in lan_u) u3_mesa_pact* pac_u = &pic_u->pac_u; u3_mesa* sam_u = pic_u->sam_u; - c3_o our_o = u3_ships_equal(pac_u->pek_u.nam_u.her_u, sam_u->pir_u->who_d); + c3_o our_o = u3_ships_equal(pac_u->pek_u.nam_u.her_u, sam_u->pir_u->who_u); if ( c3n == our_o ) { _mesa_forward_request(sam_u, pic_u, lan_u); @@ -2725,7 +2704,7 @@ _mesa_hear_peek(u3_mesa_pict* pic_u, sockaddr_in lan_u) _mesa_put_jumbo_cache(sam_u, &pac_u->pek_u.nam_u, lin_u); u3_noun sky = _name_to_jumbo_scry(&pac_u->pek_u.nam_u); - u3_noun our = u3i_chubs(2, sam_u->car_u.pir_u->who_d); + u3_noun our = u3_ship_to_noun(sam_u->car_u.pir_u->who_u); u3_noun bem = u3nc(u3nt(our, u3_nul, u3nc(c3__ud, 1)), sky); arena are_u = arena_create(sizeof(u3_mesa_cb_data) + 1024); @@ -2757,7 +2736,7 @@ _mesa_hear_poke(u3_mesa_pict* pic_u, sockaddr_in lan_u) u3_assert( PACT_POKE == pac_u->hed_u.typ_y ); #endif - c3_o our_o = u3_ships_equal(pac_u->pek_u.nam_u.her_u, sam_u->pir_u->who_d); + c3_o our_o = u3_ships_equal(pac_u->pek_u.nam_u.her_u, sam_u->pir_u->who_u); if ( c3n == our_o ) { _mesa_forward_request(sam_u, pic_u, lan_u); @@ -2918,12 +2897,12 @@ _mesa_io_talk(u3_auto* car_u) u3_auto_plan(car_u, u3_ovum_init(0, c3__a, wir, cad)); } - u3_noun who = u3i_chubs(2, sam_u->pir_u->who_d); + u3_noun who = u3_ship_to_noun(sam_u->pir_u->who_u); u3_noun rac = u3do("clan:title", u3k(who)); c3_s por_s = sam_u->pir_u->por_s; c3_i ret_i; if ( c3__czar == rac ) { - c3_y num_y = (c3_y)sam_u->pir_u->who_d[0]; + c3_y num_y = (c3_y)sam_u->pir_u->who_u.hed_d; c3_s zar_s = _ames_czar_port(num_y); if ( 0 == por_s ) { @@ -3045,7 +3024,7 @@ u3_mesa_io_init(u3_pier* pir_u) sam_u->for_o = c3n; { - u3_noun her = u3i_chubs(2, pir_u->who_d); + u3_noun her = u3_ship_to_noun(pir_u->who_u); if ( c3y == u3a_is_cat(her) && her < 256 ) { u3l_log("mesa: forwarding enabled"); sam_u->for_o = c3y; diff --git a/pkg/vere/io/mesa/pact.c b/pkg/vere/io/mesa/pact.c index e5c93c9a4c..f51af5831e 100644 --- a/pkg/vere/io/mesa/pact.c +++ b/pkg/vere/io/mesa/pact.c @@ -544,15 +544,15 @@ _etch_ship(u3_etcher* ech_u, u3_ship who_u, c3_y len_y) } static void -_sift_ship(u3_sifter* sif_u, u3_ship who_u, c3_y len_y) +_sift_ship(u3_sifter* sif_u, u3_ship* who_u, c3_y len_y) { assert ( len_y <= 16 ); c3_y *res_y = _sift_next(sif_u, len_y); if ( NULL == res_y ) { - who_u[0] = who_u[1] = 0; + who_u->hed_d = who_u->tel_d = 0; return; } - u3_ship_of_bytes(who_u, len_y, res_y); + *who_u = u3_ship_of_bytes(len_y, res_y); } static void @@ -648,7 +648,7 @@ _mesa_sift_name(u3_sifter* sif_u, u3_mesa_name* nam_u) nam_u->nit_o = __( met_u.nit_y == 1 ); nam_u->aut_o = __( met_u.tau_y == 1 ); - _sift_ship(sif_u, nam_u->her_u, 2 << met_u.ran_y); + _sift_ship(sif_u, &nam_u->her_u, 2 << met_u.ran_y); nam_u->rif_w = _sift_var_word(sif_u, met_u.rif_y + 1); nam_u->boq_y = _sift_byte(sif_u); diff --git a/pkg/vere/pier.c b/pkg/vere/pier.c index 7df40b6391..e93dc856e9 100644 --- a/pkg/vere/pier.c +++ b/pkg/vere/pier.c @@ -533,7 +533,7 @@ _pier_on_scry_done(void* ptr_v, u3_noun nun) static c3_c* _resolve_czar(u3_work* wok_u, c3_c* who_c) { - u3_noun czar = u3dc("scot", 'p', wok_u->pir_u->who_d[0] & ((1 << 8) - 1)); + u3_noun czar = u3dc("scot", 'p', wok_u->pir_u->who_u.hed_d & ((1 << 8) - 1)); c3_c* czar_c = u3r_string(czar); c3_c url[256]; @@ -604,7 +604,7 @@ _boot_scry_cb(void* vod_p, u3_noun nun) { u3_work* wok_u = (u3_work*)vod_p; - u3_atom who = u3dc("scot", c3__p, u3i_chubs(2, wok_u->pir_u->who_d)); + u3_atom who = u3dc("scot", c3__p, u3_ship_to_noun(wok_u->pir_u->who_u)); c3_c* who_c = u3r_string(who); u3_noun rem, glx, ryf, bon, cur, nex; @@ -791,8 +791,8 @@ _pier_work_init(u3_pier* pir_u) u3_auto_talk(wok_u->car_u); } - c3_d pi_d = wok_u->pir_u->who_d[0]; - c3_d pt_d = wok_u->pir_u->who_d[1]; + c3_d pi_d = wok_u->pir_u->who_u.hed_d; + c3_d pt_d = wok_u->pir_u->who_u.tel_d; if ( (pi_d < 256 && pt_d == 0) || (c3n == u3_Host.ops_u.net) ) { // Skip double boot protection for galaxies and local mode ships @@ -1890,7 +1890,7 @@ u3_pier_stay(c3_w wag_w, u3_noun pax) } if ( c3n == u3_disk_read_meta(pir_u->log_u->mdb_u, - &pir_u->log_u->ver_w, pir_u->who_d, + &pir_u->log_u->ver_w, &pir_u->who_u.hed_d, &pir_u->fak_o, &pir_u->lif_w) ) { fprintf(stderr, "pier: disk read meta fail\r\n"); @@ -2128,14 +2128,14 @@ _pier_boot_plan(u3_pier* pir_u, { pir_u->sat_e = u3_psat_boot; pir_u->fak_o = ( c3__fake == u3h(ven) ) ? c3y : c3n; - u3r_chubs(0, 2, pir_u->who_d, who); + pir_u->who_u = u3_ship_of_noun(who); bot_u = _pier_boot_make(who, _pier_wyrd_card(pir_u), ven, pil, fed, mor); pir_u->lif_w = u3qb_lent(bot_u.bot); } if ( c3n == u3_disk_save_meta(pir_u->log_u->mdb_u, - pir_u->log_u->ver_w, pir_u->who_d, + pir_u->log_u->ver_w, &pir_u->who_u.hed_d, pir_u->fak_o, pir_u->lif_w) ) { // XX dispose bot_u @@ -2144,7 +2144,7 @@ _pier_boot_plan(u3_pier* pir_u, } if ( c3n == u3_disk_save_meta_meta(pir_u->log_u->com_u->pax_c, - pir_u->who_d, pir_u->fak_o, pir_u->lif_w) ) + &pir_u->who_u.hed_d, pir_u->fak_o, pir_u->lif_w) ) { fprintf(stderr, "disk: failed to save top-level metadata\r\n"); return c3n; diff --git a/pkg/vere/vere.h b/pkg/vere/vere.h index c9f458260e..06fdbf36db 100644 --- a/pkg/vere/vere.h +++ b/pkg/vere/vere.h @@ -667,7 +667,7 @@ typedef struct _u3_pier { c3_c* pax_c; // pier directory c3_w lif_w; // lifecycle barrier - c3_d who_d[2]; // identity + u3_ship who_u; // identity c3_o fak_o; // yes iff fake security c3_o liv_o; // fully live u3_disk* log_u; // event log From 0b274a3055f0e4657abfc9c6f5386aec2ece0fd4 Mon Sep 17 00:00:00 2001 From: ziffee Date: Tue, 25 Mar 2025 10:21:10 -0600 Subject: [PATCH 04/34] mesa: move u3_peer and per_map to mesa.h --- pkg/vere/io/ames.c | 1 + pkg/vere/io/mesa.c | 22 +--------------------- pkg/vere/io/mesa/mesa.h | 29 +++++++++++++++++++++++++++++ 3 files changed, 31 insertions(+), 21 deletions(-) diff --git a/pkg/vere/io/ames.c b/pkg/vere/io/ames.c index 8940407e77..a8a95e85a8 100644 --- a/pkg/vere/io/ames.c +++ b/pkg/vere/io/ames.c @@ -3,6 +3,7 @@ #include "vere.h" #include "mdns.h" #include "io/ames/stun.h" +#include "io/mesa/mesa.h" #include "noun.h" #include "ur/ur.h" diff --git a/pkg/vere/io/mesa.c b/pkg/vere/io/mesa.c index 488a9615bc..ff631423a8 100644 --- a/pkg/vere/io/mesa.c +++ b/pkg/vere/io/mesa.c @@ -119,20 +119,11 @@ typedef struct _u3_mesa_pict { u3_mesa_pact pac_u; } u3_mesa_pict; -typedef struct _u3_lane_state { - c3_d sen_d; // last sent date - c3_d her_d; // last heard date - c3_w rtt_w; // round-trip time - c3_w rtv_w; // round-trip time variance -} u3_lane_state; - /* _u3_mesa: next generation networking */ typedef struct _u3_pit_addr u3_pit_addr; -typedef struct sockaddr_in sockaddr_in; - typedef struct _u3_pit_addr { sockaddr_in sdr_u; u3_pit_addr* nex_p; @@ -251,13 +242,12 @@ static void u3_free_line( u3_mesa_line* lin_u ) #define VAL_TY u3_mesa_line* #include "verstable.h" -typedef struct _u3_peer u3_peer; - #define NAME per_map #define KEY_TY u3_ship #define HASH_FN u3_hash_ship #define CMPR_FN u3_cmpr_ship #define VAL_TY u3_peer* +#define IMPLEMENTATION_MODE #include "verstable.h" typedef struct _u3_mesa { @@ -283,16 +273,6 @@ typedef struct _u3_mesa { uv_timer_t tim_u; // pit clear timer } u3_mesa; -typedef struct _u3_peer { - u3_mesa* sam_u; // backpointer - u3_ship her_u; // who is this peer - c3_o ful_o; // has this been initialized? - sockaddr_in dan_u; // direct lane (nullable) - u3_lane_state dir_u; // direct lane state - c3_y imp_y; // galaxy @p - u3_lane_state ind_u; // indirect lane state -} u3_peer; - typedef struct _u3_pend_req { u3_peer* per_u; // backpointer c3_d nex_d; // number of the next fragment to be sent diff --git a/pkg/vere/io/mesa/mesa.h b/pkg/vere/io/mesa/mesa.h index 6faab632e5..6f2519c397 100644 --- a/pkg/vere/io/mesa/mesa.h +++ b/pkg/vere/io/mesa/mesa.h @@ -1,6 +1,7 @@ #ifndef VERE_MESA_H #define VERE_MESA_H +#include "vere.h" #include "c3/c3.h" #include "ship.h" @@ -172,4 +173,32 @@ void inc_hopcount(u3_mesa_head*); void log_pact(u3_mesa_pact* pac_u); void log_name(u3_mesa_name* nam_u); +typedef struct sockaddr_in sockaddr_in; + +typedef struct _u3_lane_state { + c3_d sen_d; // last sent date + c3_d her_d; // last heard date + c3_w rtt_w; // round-trip time + c3_w rtv_w; // round-trip time variance +} u3_lane_state; + +typedef struct _u3_peer { + void* sam_u; // backpointer + u3_ship her_u; // who is this peer + c3_o ful_o; // has this been initialized? + sockaddr_in dan_u; // direct lane (nullable) + u3_lane_state dir_u; // direct lane state + c3_y imp_y; // galaxy @p + u3_lane_state ind_u; // indirect lane state +} u3_peer; + +#define NAME per_map +#define KEY_TY u3_ship +#define HASH_FN u3_hash_ship +#define CMPR_FN u3_cmpr_ship +#define VAL_TY u3_peer* +#define HEADER_MODE +#include "verstable.h" + + #endif From b06961e5eb879b06841b37f62a15c2e6f582ed6e Mon Sep 17 00:00:00 2001 From: ziffee Date: Tue, 25 Mar 2025 12:32:48 -0600 Subject: [PATCH 05/34] mesa: move sun state into u3_mesa, extract into ames/stun --- pkg/vere/io/ames.c | 304 +--------------------------------------- pkg/vere/io/ames/stun.c | 269 +++++++++++++++++++++++++++++++++++ pkg/vere/io/ames/stun.h | 36 ++++- pkg/vere/io/mesa.c | 60 +++++++- pkg/vere/vere.h | 1 + 5 files changed, 364 insertions(+), 306 deletions(-) diff --git a/pkg/vere/io/ames.c b/pkg/vere/io/ames.c index a8a95e85a8..87c4b90f57 100644 --- a/pkg/vere/io/ames.c +++ b/pkg/vere/io/ames.c @@ -29,12 +29,6 @@ #define _CZAR_GONE UINT32_MAX -typedef enum u3_stun_state { - STUN_OFF = 0, - STUN_TRYING = 1, - STUN_KEEPALIVE = 2, -} u3_stun_state; - /* u3_fine: fine networking */ typedef struct _u3_fine { @@ -67,16 +61,6 @@ typedef enum u3_stun_state { 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 - c3_y dad_y; // sponsoring galaxy - u3_lane lan_u; // sponsoring galaxy IP and port - uv_timer_t tim_u; // keepalive timer handle - 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 - } sun_u; // struct { natpmp_t req_u; // libnatpmp struct for mapping request uv_poll_t pol_u; // handle waits on libnatpmp socket @@ -902,7 +886,7 @@ _ames_etch_czar(c3_c dns_c[256], const c3_c* dom_c, c3_y imp_y) /* _ames_czar_lane: retrieve lane for galaxy if stored. */ -static c3_o +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); @@ -975,240 +959,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 - 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) -{ - _stun_send* snd_u = (_stun_send*)rep_u; - - if ( !sas_i ) { - net_o = c3y; - } - else if ( c3y == net_o ) { - u3l_log("stun: send response fail: %s", uv_strerror(sas_i)); - net_o = c3n; - } - - c3_free(snd_u); -} - -/* _stun_on_request(): hear stun request, send response. - */ -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); - - 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) - }; - 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, &u3_Host.wax_u, - &buf_u, 1, adr_u, _stun_send_cb); - - if ( sas_i ) { - _stun_send_cb(&snd_u->req_u, sas_i); - } -} - -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 == u3_stun_find_xor_mapped_address(buf_y, buf_len, &lan_u) ) { - return; - } - - 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_auto_plan(&sam_u->car_u, - u3_ovum_init(0, c3__ames, wir, cad)); - } - 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; - - // 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: { - _stun_start(sam_u, 25000); - } break; - - default: u3_assert(!"programmer error"); - } -} - -/* _stun_send_request(): send stun request to galaxy lane. - */ -static void -_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); - - 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)); - 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, &u3_Host.wax_u, &buf_u, 1, - (const struct sockaddr*)&add_u, _stun_send_cb); - - if ( sas_i ) { - _stun_send_cb(&snd_u->req_u, sas_i); - } -} - -/* _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); -} - -/* _stun_on_lost(): stun failed (timeout); capture and reset. - */ -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); -} - -/* _stun_time_gap(): elapsed milliseconds. - */ -static c3_d -_stun_time_gap(struct timeval sar_tv) -{ - 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); -} - -/* _stun_timer_cb(): advance stun state machine. - */ -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"); - } -} - -/* _stun_start(): begin/restart STUN state machine. -*/ -static void -_stun_start(u3_ames* sam_u, c3_w tim_w) -{ - if ( ent_getentropy(sam_u->sun_u.tid_y, 12) ) { - u3l_log("stun: getentropy fail: %s", strerror(errno)); - u3_king_bail(); - } - - 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 @@ -1220,33 +970,6 @@ _ames_is_czar(u3_noun who) return zar; } -/* _ames_ef_saxo(): handle sponsorship chain notification -*/ -static void -_ames_ef_saxo(u3_ames* sam_u, u3_noun zad) -{ - u3_noun daz, dad; - - daz = u3qb_flop(zad); - if ( u3_nul == daz ) { - u3l_log("ames: empty sponsorship chain"); - u3z(zad); u3z(daz); - return; - } - - dad = u3h(daz); - u3_noun our = u3_ship_to_noun(sam_u->pir_u->who_u); - - // if we are a galaxy, don't STUN - // - if ( c3y == _ames_is_czar(dad) && c3n == _ames_is_czar(our)) { - sam_u->sun_u.dad_y = (c3_y)dad; - _stun_start(sam_u, 0); - } - - u3z(zad); u3z(daz); u3z(our); -} - /* _ames_send_lane(): resolve/decode lane. RETAIN */ static c3_o @@ -2000,16 +1723,8 @@ _ames_hear(u3_ames* sam_u, // check ames first, assume that STUN could maybe (not likely) overlap with ames // for next protocol version, have an urbit cookie // - if ( c3y == u3_stun_is_request(hun_y, len_w) ) { - _stun_on_request(sam_u, hun_y, adr_u); - c3_free(hun_y); - } - else if ( c3y == u3_stun_is_our_response(hun_y, - sam_u->sun_u.tid_y, len_w) ) - { - _stun_on_response(sam_u, hun_y, len_w); - c3_free(hun_y); - } + if ( c3y == u3_stun_hear(u3_Host.sun_u, adr_u, len_w, hun_y) ) + return; else { struct sockaddr_in* add_u = (struct sockaddr_in*)adr_u; u3_lane lan_u = { @@ -2637,11 +2352,6 @@ _ames_kick_newt(u3_ames* sam_u, u3_noun tag, u3_noun dat) ret_o = c3y; } break; - case c3__saxo: { - _ames_ef_saxo(sam_u, u3k(dat)); - ret_o = c3y; - } - case c3__nail: { u3_noun who = u3k(u3h(dat)); u3_noun las = u3k(u3t(dat)); @@ -2740,7 +2450,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.tim_u, 0); uv_close((uv_handle_t*)&sam_u->nat_u.tim_u, 0); uv_handle_type handle = uv_handle_get_type((uv_handle_t *)&sam_u->nat_u.pol_u); @@ -2767,7 +2476,6 @@ _ames_io_info(u3_auto* car_u) u3_pier_mase("filtering", sam_u->fig_u.fit_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))), u3_pier_mase("scry-cache-size", u3i_word(sac_w)), u3_pier_mase("lane-cache", u3i_word(u3h_wyt(sam_u->lax_p))), @@ -2812,8 +2520,6 @@ _ames_io_slog(u3_auto* car_u) u3l_log(" filtering: %s", FLAG(sam_u->fig_u.fit_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)); 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); @@ -2844,15 +2550,11 @@ u3_ames_io_init(u3_pier* pir_u) sam_u->nal_o = c3n; 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; - uv_timer_init(u3L, &sam_u->sun_u.tim_u); - sam_u->sun_u.tim_u.data = sam_u; - // initialize libnatpmp sam_u->nat_u.tim_u.data = sam_u; uv_timer_init(u3L, &sam_u->nat_u.tim_u); diff --git a/pkg/vere/io/ames/stun.c b/pkg/vere/io/ames/stun.c index 0f830c1f4a..e334c372f7 100644 --- a/pkg/vere/io/ames/stun.c +++ b/pkg/vere/io/ames/stun.c @@ -1,5 +1,7 @@ #include "vere.h" #include "zlib.h" +#include "ent/ent.h" +#include "io/ames/stun.h" static c3_y* _stun_add_fingerprint(c3_y *message, c3_w index) @@ -190,3 +192,270 @@ u3_stun_find_xor_mapped_address(c3_y* buf_y, } return c3n; } + +typedef struct _stun_send { + uv_udp_send_t req_u; // uv udp request handle + c3_y hun_y[0]; // buffer + u3_stun_client* sun_u; // backpointer +} _stun_send; + +/* _stun_send_cb(): stun udp send callback. + */ +static void +_stun_send_cb(uv_udp_send_t *rep_u, c3_i sas_i) +{ + _stun_send* snd_u = (_stun_send*)rep_u; + + if ( !sas_i ) { + snd_u->sun_u->net_o = c3y; + } + else if ( c3y == snd_u->sun_u->net_o ) { + u3l_log("stun: send response fail: %s", uv_strerror(sas_i)); + snd_u->sun_u->net_o = c3n; + } + + c3_free(snd_u); +} + +/* _stun_on_request(): hear stun request, send response. + */ +static void +_stun_on_request(u3_stun_client* sun_u, + const c3_y* req_y, + const struct sockaddr* adr_u) +{ + _stun_send* snd_u = c3_malloc(sizeof(*snd_u) + 40); + snd_u->sun_u = sun_u; + + 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) + }; + 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, &u3_Host.wax_u, + &buf_u, 1, adr_u, _stun_send_cb); + + if ( sas_i ) { + _stun_send_cb(&snd_u->req_u, sas_i); + } +} + +void +u3_stun_start(u3_stun_client* sun_u, c3_w tim_w); + +/* _stun_on_response(): hear stun response from galaxy. + */ +static void +_stun_on_response(u3_stun_client* sun_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 == u3_stun_find_xor_mapped_address(buf_y, buf_len, &lan_u) ) { + return; + } + + if ( (sun_u->sef_u.por_s != lan_u.por_s) || + (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, sun_u->dad_y, + u3nc(c3n, u3_ames_encode_lane(lan_u))); + u3_auto_plan(sun_u->car_u, + u3_ovum_init(0, c3__ames, wir, cad)); + } + else if ( c3n == sun_u->wok_o ) { + // stop %ping app + u3_noun wir = u3nc(c3__ames, u3_nul); + u3_noun cad = u3nq(c3__stun, c3__stop, sun_u->dad_y, + u3nc(c3n, u3_ames_encode_lane(lan_u))); + u3_auto_plan(sun_u->car_u, + u3_ovum_init(0, c3__ames, wir, cad)); + sun_u->wok_o = c3y; + } + + sun_u->sef_u = lan_u; + + // XX should no-op early + // + switch ( sun_u->sat_y ) { + case STUN_OFF: break; // ignore; stray response + case STUN_KEEPALIVE: break; // ignore; duplicate response + + case STUN_TRYING: { + u3_stun_start(sun_u, 25000); + } break; + + default: u3_assert(!"programmer error"); + } +} + +/* _stun_send_request(): send stun request to galaxy lane. + */ +static void +_stun_send_request(u3_stun_client* sun_u) +{ + u3_assert( STUN_OFF != sun_u->sat_y ); + + _stun_send* snd_u = c3_malloc(sizeof(*snd_u) + 28); + snd_u->sun_u = sun_u; + + u3_stun_make_request(snd_u->hun_y, 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(sun_u->lan_u.pip_w); + add_u.sin_port = htons(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, &u3_Host.wax_u, &buf_u, 1, + (const struct sockaddr*)&add_u, _stun_send_cb); + + if ( sas_i ) { + _stun_send_cb(&snd_u->req_u, sas_i); + } +} + +/* _stun_reset(): stun failed. start again using max backoff + */ +static void +_stun_reset(uv_timer_t* tim_u) +{ + u3_stun_client* sun_u = (u3_stun_client*)(tim_u->data); + + u3_stun_start(sun_u, 39000); +} + +/* _stun_on_lost(): stun failed (timeout); capture and reset. + */ +static void +_stun_on_lost(u3_stun_client* sun_u) +{ + sun_u->sat_y = STUN_OFF; + + // only inject event into arvo to %kick ping app on first failure + // + if ( c3y == sun_u->wok_o ) { + u3_noun wir = u3nc(c3__ames, u3_nul); + u3_noun cad = u3nq(c3__stun, c3__fail, sun_u->dad_y, + u3nc(c3n, u3_ames_encode_lane(sun_u->sef_u))); + u3_auto_plan(sun_u->car_u, + u3_ovum_init(0, c3__ames, wir, cad)); + sun_u->wok_o = c3n; + } + + uv_timer_start(&sun_u->tim_u, _stun_reset, 5*1000, 0); +} + +/* _stun_time_gap(): elapsed milliseconds. + */ +static c3_d +_stun_time_gap(struct timeval sar_tv) +{ + 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); +} + +c3_o _ames_czar_lane(u3_auto*, c3_y, u3_lane*); + +/* _stun_timer_cb(): advance stun state machine. + */ +static void +_stun_timer_cb(uv_timer_t* tim_u) +{ + u3_stun_client* sun_u = (u3_stun_client*)(tim_u->data); + c3_w rto_w = 500; + + switch ( 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 = &(sun_u->lan_u); + c3_y imp_y = sun_u->dad_y; + + if ( c3n == _ames_czar_lane(sun_u->car_u, imp_y, lan_u) ) { + uv_timer_start(&sun_u->tim_u, _stun_timer_cb, 25*1000, 0); + } + else { + sun_u->sat_y = STUN_TRYING; + gettimeofday(&sun_u->sar_u, 0); // set start time to now + uv_timer_start(&sun_u->tim_u, _stun_timer_cb, rto_w, 0); + _stun_send_request(sun_u); + } + } break; + + case STUN_TRYING: { + c3_d gap_d = _stun_time_gap(sun_u->sar_u); + c3_d nex_d = (gap_d * 2) + rto_w - gap_d; + + if ( gap_d >= 39500 ) { + _stun_on_lost(sun_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(&sun_u->tim_u, _stun_timer_cb, tim_w, 0); + _stun_send_request(sun_u); + } + } break; + + default: u3_assert(!"programmer error"); + } +} + +/* _stun_start(): begin/restart STUN state machine. +*/ +void +u3_stun_start(u3_stun_client* sun_u, c3_w tim_w) +{ + if ( ent_getentropy(sun_u->tid_y, 12) ) { + u3l_log("stun: getentropy fail: %s", strerror(errno)); + u3_king_bail(); + } + + sun_u->sat_y = STUN_KEEPALIVE; + uv_timer_start(&sun_u->tim_u, _stun_timer_cb, tim_w, 0); +} + +c3_o +u3_stun_hear(u3_stun_client* sun_u, + const struct sockaddr* adr_u, + c3_w len_w, + c3_y* hun_y) +{ + // XX reorg, check if a STUN req/resp can look like an ames packet + // check the mug hash of the body of the packet, if not check if STUN + // otherwise , invalid packet, log failure + // check ames first, assume that STUN could maybe (not likely) overlap with ames + // for next protocol version, have an urbit cookie + // + if ( c3y == u3_stun_is_request(hun_y, len_w) ) { + _stun_on_request(sun_u, hun_y, adr_u); + c3_free(hun_y); + return c3y; + } + else if ( c3y == u3_stun_is_our_response(hun_y, + sun_u->tid_y, len_w) ) + { + _stun_on_response(sun_u, hun_y, len_w); + c3_free(hun_y); + return c3y; + } + return c3n; +} + diff --git a/pkg/vere/io/ames/stun.h b/pkg/vere/io/ames/stun.h index 0fcaff6466..0699bcfeed 100644 --- a/pkg/vere/io/ames/stun.h +++ b/pkg/vere/io/ames/stun.h @@ -1,8 +1,28 @@ #include "vere.h" +#include "c3/types.h" #ifndef U3_STUN_H #define U3_STUN_H + typedef enum _u3_stun_state { + STUN_OFF = 0, + STUN_TRYING = 1, + STUN_KEEPALIVE = 2, + } u3_stun_state; + + typedef struct _u3_stun_client { // stun client state: + u3_auto* car_u; // driver backpointer + u3_stun_state sat_y; // formal state + c3_y tid_y[12]; // last transaction id + c3_y dad_y; // sponsoring galaxy + u3_lane lan_u; // sponsoring galaxy IP and port + uv_timer_t tim_u; // keepalive timer handle + 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 + c3_o net_o; // online heuristic to limit verbosity + } u3_stun_client; // + /* u3_stun_is_request(): buffer is a stun request. */ c3_o @@ -20,7 +40,7 @@ /* u3_stun_make_response(): serialize stun response from request. */ - void + void u3_stun_make_response(const c3_y req_y[20], u3_lane* lan_u, c3_y buf_y[40]); @@ -32,4 +52,18 @@ c3_w len_w, u3_lane* lan_u); + /* u3_stun_start(): begin/restart STUN state machine. + */ + void + u3_stun_start(u3_stun_client* sam_u, c3_w tim_w); + + /* u3_stun_shear(): maybe hear stun packet + */ + c3_o + u3_stun_hear(u3_stun_client* sun_u, + const struct sockaddr* adr_u, + c3_w len_w, + c3_y* hun_y); + + #endif /* ifndef U3_STUN_H */ diff --git a/pkg/vere/io/mesa.c b/pkg/vere/io/mesa.c index ff631423a8..cecfbbce7b 100644 --- a/pkg/vere/io/mesa.c +++ b/pkg/vere/io/mesa.c @@ -16,6 +16,7 @@ #include #include #include +#include #include #include #include @@ -271,8 +272,14 @@ typedef struct _u3_mesa { arena are_u; // per packet arena arena par_u; // permanent arena uv_timer_t tim_u; // pit clear timer + u3_stun_client sun_u; // stun client state } u3_mesa; +STATIC_ASSERT( + ( ((void*)(u3_mesa*)(void*)0) == + ((void*)(u3_mesa*)(void*)&(((u3_mesa*)(void*)0)->car_u)) ), + "u3_mesa struct alignment" ); + typedef struct _u3_pend_req { u3_peer* per_u; // backpointer c3_d nex_d; // number of the next fragment to be sent @@ -1660,6 +1667,7 @@ u3_ames_encode_lane(u3_lane lan); static void _meet_peer(u3_mesa* sam_u, u3_peer* per_u, u3_ship her_u); static void _init_peer(u3_mesa* sam_u, u3_peer* per_u); +static void _mesa_ef_saxo(u3_mesa* sam_u, u3_noun zad); static c3_o _mesa_kick(u3_mesa* sam_u, u3_noun tag, u3_noun dat) { @@ -1679,9 +1687,13 @@ static c3_o _mesa_kick(u3_mesa* sam_u, u3_noun tag, u3_noun dat) ret_o = c3y; } } break; - case c3__send: - case c3__turf: case c3__saxo: { + _mesa_ef_saxo(sam_u, u3k(dat)); + ret_o = c3y; + break; + } + case c3__send: + case c3__turf: { #ifdef MESA_DEBUG c3_c* tag_c = u3r_string(tag); /* u3l_log("mesa: send old %s", tag_c); */ @@ -1752,13 +1764,21 @@ static c3_o _mesa_io_kick(u3_auto* car_u, u3_noun wir, u3_noun cad) static u3_noun _mesa_io_info(u3_auto* car_u) { - - return u3_nul; + u3_mesa* sam_u = (u3_mesa*)car_u; + return u3i_list( + u3_pier_mase("stun-can-send", sam_u->sun_u.net_o), + u3_pier_mase("stun-working", sam_u->sun_u.wok_o), + u3_none); } static void _mesa_io_slog(u3_auto* car_u) { + u3_mesa* sam_u = (u3_mesa*)car_u; +# define FLAG(a) ( (c3y == a) ? "&" : "|" ) u3l_log("mesa is online"); + u3l_log(" stun:"); + u3l_log(" working: %s", FLAG(sam_u->sun_u.wok_o)); + u3l_log(" can send: %s", FLAG(sam_u->sun_u.net_o)); } static void @@ -1774,6 +1794,7 @@ _mesa_io_exit(u3_auto* car_u) uv_timer_stop(&sam_u->tim_u); sam_u->tim_u.data = sam_u; uv_close((uv_handle_t*)&sam_u->tim_u, _mesa_exit_cb); + uv_close((uv_handle_t*)&sam_u->sun_u.tim_u, 0); } static void @@ -2964,6 +2985,32 @@ static void _mesa_clear_pit(uv_timer_t *tim_u) } } +/* _mesa_ef_saxo(): handle sponsorship chain notification +*/ +static void +_mesa_ef_saxo(u3_mesa* sam_u, u3_noun zad) +{ + u3_noun daz; + + daz = u3qb_flop(zad); + if ( u3_nul == daz ) { + u3l_log("ames: empty sponsorship chain"); + u3z(zad); u3z(daz); + return; + } + + u3_ship dad = u3_ship_of_noun(u3h(daz)); + + // if we are a galaxy, don't STUN + // + if ( (c3__czar == u3_ship_rank(dad)) && + (c3__czar != u3_ship_rank(sam_u->pir_u->who_u)) ) { + sam_u->sun_u.dad_y = (c3_y)dad.hed_d; + u3_stun_start(&sam_u->sun_u, 0); + } + + u3z(zad); u3z(daz); +} /* _mesa_io_init(): initialize ames I/O. */ u3_auto* @@ -3023,6 +3070,11 @@ u3_mesa_io_init(u3_pier* pir_u) car_u->io.kick_f = _mesa_io_kick; car_u->io.exit_f = _mesa_io_exit; + sam_u->sun_u.net_o = c3y; + sam_u->sun_u.wok_o = c3n; + uv_timer_init(u3L, &sam_u->sun_u.tim_u); + sam_u->sun_u.tim_u.data = sam_u; + u3_Host.sun_u = &sam_u->sun_u; /*{ diff --git a/pkg/vere/vere.h b/pkg/vere/vere.h index 06fdbf36db..40136a4772 100644 --- a/pkg/vere/vere.h +++ b/pkg/vere/vere.h @@ -343,6 +343,7 @@ c3_i xit_i; // exit code for shutdown void (*bot_f)(); // call when chis is up void* sam_u; // old ames, "unified driver" hack + void* sun_u; // old ames, "unified driver" hack uv_udp_t wax_u; // "unified driver" udp send handle c3_w* imp_u; // "unified driver" galaxy IP:s } u3_host; // host == computer == process From 3fbb67d7c3ff1d41932a5f13acbacc292650cbc0 Mon Sep 17 00:00:00 2001 From: ziffee Date: Tue, 25 Mar 2025 13:12:57 -0600 Subject: [PATCH 06/34] disk: change who_d -> who_u --- pkg/vere/disk.c | 58 +++++++++++++++++-------------------------------- pkg/vere/pier.c | 12 +++++----- pkg/vere/vere.h | 6 ++--- 3 files changed, 28 insertions(+), 48 deletions(-) diff --git a/pkg/vere/disk.c b/pkg/vere/disk.c index 173a4a8089..4c35dc1a12 100644 --- a/pkg/vere/disk.c +++ b/pkg/vere/disk.c @@ -640,7 +640,7 @@ _disk_save_meta(MDB_env* mdb_u, const c3_c* key_c, c3_w len_w, c3_y* byt_y) c3_o u3_disk_save_meta(MDB_env* mdb_u, c3_w ver_w, - c3_d who_d[2], + u3_ship who_u, c3_o fak_o, c3_w lif_w) { @@ -650,7 +650,7 @@ u3_disk_save_meta(MDB_env* mdb_u, // XX assumes little-endian // if ( (c3n == _disk_save_meta(mdb_u, "version", 4, (c3_y*)&ver_w)) - || (c3n == _disk_save_meta(mdb_u, "who", 16, (c3_y*)who_d)) + || (c3n == _disk_save_meta(mdb_u, "who", 16, (c3_y*)&who_u)) || (c3n == _disk_save_meta(mdb_u, "fake", 1, (c3_y*)&fak_o)) || (c3n == _disk_save_meta(mdb_u, "life", 4, (c3_y*)&lif_w)) ) { @@ -665,7 +665,7 @@ u3_disk_save_meta(MDB_env* mdb_u, */ c3_o u3_disk_save_meta_meta(c3_c* log_c, - c3_d who_d[2], + u3_ship who_u, c3_o fak_o, c3_w lif_w) { @@ -676,7 +676,7 @@ u3_disk_save_meta_meta(c3_c* log_c, return c3n; } - if ( c3n == u3_disk_save_meta(dbm_u, U3D_VERLAT, who_d, fak_o, lif_w) ) { + if ( c3n == u3_disk_save_meta(dbm_u, U3D_VERLAT, who_u, fak_o, lif_w) ) { fprintf(stderr, "disk: failed to save metadata\r\n"); return c3n; } @@ -712,11 +712,11 @@ _disk_meta_read_cb(void* ptr_v, ssize_t val_i, void* val_v) /* u3_disk_read_meta(): read metadata. */ c3_o -u3_disk_read_meta(MDB_env* mdb_u, - c3_w* ver_w, - c3_d* who_d, - c3_o* fak_o, - c3_w* lif_w) +u3_disk_read_meta(MDB_env* mdb_u, + c3_w* ver_w, + u3_ship* who_u, + c3_o* fak_o, + c3_w* lif_w) { _mdb_val val_u; @@ -747,27 +747,9 @@ u3_disk_read_meta(MDB_env* mdb_u, fprintf(stderr, "disk: read meta: strange identity\r\n"); } - if ( who_d ) { - c3_y* byt_y = val_u.buf_y; - who_d[0] = (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; - - byt_y += 8; - who_d[1] = (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; + if ( who_u ) { + *who_u = u3_ship_of_bytes(16, val_u.buf_y); } // fake bit @@ -1314,10 +1296,10 @@ _disk_epoc_roll(u3_disk* log_u, c3_d epo_d) } // get metadata from old log - c3_d who_d[2]; + u3_ship who_u; c3_o fak_o; c3_w lif_w; - if ( c3y != u3_disk_read_meta(log_u->mdb_u, 0, who_d, &fak_o, &lif_w) ) { + if ( c3y != u3_disk_read_meta(log_u->mdb_u, 0, &who_u, &fak_o, &lif_w) ) { fprintf(stderr, "disk: failed to read metadata\r\n"); goto fail3; } @@ -1333,7 +1315,7 @@ _disk_epoc_roll(u3_disk* log_u, c3_d epo_d) } // write the metadata to the database - if ( c3n == u3_disk_save_meta(log_u->mdb_u, U3D_VERLAT, who_d, fak_o, lif_w) ) { + if ( c3n == u3_disk_save_meta(log_u->mdb_u, U3D_VERLAT, who_u, fak_o, lif_w) ) { fprintf(stderr, "disk: failed to save metadata\r\n"); goto fail3; } @@ -1507,11 +1489,11 @@ _disk_migrate(u3_disk* log_u, c3_d eve_d) // XX: put old log in separate pointer (old_u?)? // get metadata from old log - c3_d who_d[2]; + u3_ship who_u; c3_o fak_o; c3_w lif_w; - if ( c3y != u3_disk_read_meta(log_u->mdb_u, 0, who_d, &fak_o, &lif_w) ) { + if ( c3y != u3_disk_read_meta(log_u->mdb_u, 0, &who_u, &fak_o, &lif_w) ) { fprintf(stderr, "disk: failed to read metadata\r\n"); return c3n; } @@ -1591,7 +1573,7 @@ _disk_migrate(u3_disk* log_u, c3_d eve_d) return c3n; } - if ( c3n == u3_disk_save_meta(log_u->mdb_u, U3D_VERLAT, who_d, fak_o, lif_w) ) { + if ( c3n == u3_disk_save_meta(log_u->mdb_u, U3D_VERLAT, who_u, fak_o, lif_w) ) { fprintf(stderr, "disk: failed to save metadata\r\n"); return c3n; } @@ -2055,17 +2037,17 @@ u3_disk_init(c3_c* pax_c, u3_disk_cb cb_u) fprintf(stderr, "disk: repairing pre-release pier metadata\r\n"); // read metadata from epoch's log - c3_d who_d[2]; + u3_ship who_u; c3_o fak_o; c3_w lif_w; - if ( c3n == u3_disk_read_meta(log_u->mdb_u, 0, who_d, &fak_o, &lif_w) ) + if ( c3n == u3_disk_read_meta(log_u->mdb_u, 0, &who_u, &fak_o, &lif_w) ) { fprintf(stderr, "disk: failed to read metadata\r\n"); c3_free(log_u); return 0; } - if ( c3n == u3_disk_save_meta_meta(log_c, who_d, fak_o, lif_w) ) { + if ( c3n == u3_disk_save_meta_meta(log_c, who_u, fak_o, lif_w) ) { fprintf(stderr, "disk: failed to save top-level metadata\r\n"); c3_free(log_u); return 0; diff --git a/pkg/vere/pier.c b/pkg/vere/pier.c index e93dc856e9..171dc929b3 100644 --- a/pkg/vere/pier.c +++ b/pkg/vere/pier.c @@ -791,10 +791,8 @@ _pier_work_init(u3_pier* pir_u) u3_auto_talk(wok_u->car_u); } - c3_d pi_d = wok_u->pir_u->who_u.hed_d; - c3_d pt_d = wok_u->pir_u->who_u.tel_d; - - if ( (pi_d < 256 && pt_d == 0) || (c3n == u3_Host.ops_u.net) ) { + if ( (c3__czar == u3_ship_rank(wok_u->pir_u->who_u)) || + (c3n == u3_Host.ops_u.net) ) { // Skip double boot protection for galaxies and local mode ships // _pier_work(wok_u); @@ -1890,7 +1888,7 @@ u3_pier_stay(c3_w wag_w, u3_noun pax) } if ( c3n == u3_disk_read_meta(pir_u->log_u->mdb_u, - &pir_u->log_u->ver_w, &pir_u->who_u.hed_d, + &pir_u->log_u->ver_w, &pir_u->who_u, &pir_u->fak_o, &pir_u->lif_w) ) { fprintf(stderr, "pier: disk read meta fail\r\n"); @@ -2135,7 +2133,7 @@ _pier_boot_plan(u3_pier* pir_u, } if ( c3n == u3_disk_save_meta(pir_u->log_u->mdb_u, - pir_u->log_u->ver_w, &pir_u->who_u.hed_d, + pir_u->log_u->ver_w, pir_u->who_u, pir_u->fak_o, pir_u->lif_w) ) { // XX dispose bot_u @@ -2144,7 +2142,7 @@ _pier_boot_plan(u3_pier* pir_u, } if ( c3n == u3_disk_save_meta_meta(pir_u->log_u->com_u->pax_c, - &pir_u->who_u.hed_d, pir_u->fak_o, pir_u->lif_w) ) + pir_u->who_u, pir_u->fak_o, pir_u->lif_w) ) { fprintf(stderr, "disk: failed to save top-level metadata\r\n"); return c3n; diff --git a/pkg/vere/vere.h b/pkg/vere/vere.h index 40136a4772..b578baf1d8 100644 --- a/pkg/vere/vere.h +++ b/pkg/vere/vere.h @@ -1002,7 +1002,7 @@ c3_o u3_disk_read_meta(MDB_env* mdb_u, c3_w* ver_w, - c3_d* who_d, + u3_ship* who_u, c3_o* fak_o, c3_w* lif_w); @@ -1011,7 +1011,7 @@ c3_o u3_disk_save_meta(MDB_env* mdb_u, c3_w ver_w, - c3_d who_d[2], + u3_ship who_u, c3_o fak_o, c3_w lif_w); @@ -1019,7 +1019,7 @@ */ c3_o u3_disk_save_meta_meta(c3_c* log_c, - c3_d who_d[2], + u3_ship who_d, c3_o fak_o, c3_w lif_w); From cd2dabee4e958f9fbdbfc4e0325ec7b8d93d82b1 Mon Sep 17 00:00:00 2001 From: ziffee Date: Thu, 27 Mar 2025 13:15:10 -0600 Subject: [PATCH 07/34] ames: remove u3_lane --- pkg/vere/io/ames.c | 351 +++++++++++++++++++--------------------- pkg/vere/io/ames/stun.c | 96 ++++++----- pkg/vere/io/ames/stun.h | 37 +---- pkg/vere/io/mesa.c | 36 +++-- pkg/vere/mdns.c | 2 +- pkg/vere/vere.h | 10 +- 6 files changed, 246 insertions(+), 286 deletions(-) diff --git a/pkg/vere/io/ames.c b/pkg/vere/io/ames.c index 87c4b90f57..a559778f79 100644 --- a/pkg/vere/io/ames.c +++ b/pkg/vere/io/ames.c @@ -27,7 +27,9 @@ #define QUEUE_MAX 30 // max number of packets in queue -#define _CZAR_GONE UINT32_MAX +#define _CZAR_GONE (htonl(UINT32_MAX)) + +#define NLOCALHOST (htonl(0x7f000001)) /* u3_fine: fine networking */ @@ -169,7 +171,7 @@ 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 + sockaddr_in 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 @@ -721,25 +723,20 @@ _ames_send(u3_pact* pac_u) if ( !pac_u->hun_y || !sam_u || !pac_u->len_w - || !pac_u->lan_u.por_s ) + || !pac_u->lan_u.sin_port ) { u3l_log("ames: _ames_send null"); _ames_pact_free(pac_u); } else { - 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(pac_u->lan_u.pip_w); - add_u.sin_port = htons(pac_u->lan_u.por_s); + struct sockaddr_in lan_u = pac_u->lan_u; { 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, &u3_Host.wax_u, &buf_u, 1, - (const struct sockaddr*)&add_u, + (const struct sockaddr*)&lan_u, _ames_send_cb); if ( sas_i ) { @@ -751,33 +748,35 @@ _ames_send(u3_pact* pac_u) /* u3_ames_decode_lane(): deserialize noun to lane; 0.0.0.0:0 if invalid */ -u3_lane +sockaddr_in u3_ames_decode_lane(u3_atom lan) { - u3_lane lan_u; c3_d lan_d; if ( c3n == u3r_safe_chub(lan, &lan_d) || (lan_d >> 48) != 0 ) { - return (u3_lane){0, 0}; + return (sockaddr_in){0}; } u3z(lan); - lan_u.pip_w = (c3_w)lan_d; - lan_u.por_s = (c3_s)(lan_d >> 32); + sockaddr_in lan_u; + lan_u.sin_family = AF_INET; + lan_u.sin_addr.s_addr = htonl((c3_w)lan_d); + lan_u.sin_port = htons((c3_s)(lan_d >> 32)); return lan_u; } /* u3_ames_lane_to_chub(): serialize lane to double-word */ c3_d -u3_ames_lane_to_chub(u3_lane lan) { - return ((c3_d)lan.por_s << 32) ^ (c3_d)lan.pip_w; +u3_ames_lane_to_chub(sockaddr_in lan) { + return ((c3_d)ntohs(lan.sin_addr.s_addr) << 32) + ^ (c3_d)ntohl(lan.sin_port); } /* u3_ames_encode_lane(): serialize lane to noun */ u3_atom -u3_ames_encode_lane(u3_lane lan) { +u3_ames_encode_lane(sockaddr_in lan) { // [%| p=@] // [%& p=@pC] return u3i_chub(u3_ames_lane_to_chub(lan)); @@ -842,10 +841,10 @@ static c3_s _ames_czar_port(c3_y imp_y) { if ( c3n == u3_Host.ops_u.net ) { - return 31337 + imp_y; + return htons(31337 + imp_y); } else { - return 13337 + imp_y; + return htons(13337 + imp_y); } } @@ -887,24 +886,25 @@ _ames_etch_czar(c3_c dns_c[256], const c3_c* dom_c, c3_y imp_y) /* _ames_czar_lane: retrieve lane for galaxy if stored. */ c3_o -_ames_czar_lane(u3_ames* sam_u, c3_y imp_y, u3_lane* lan_u) +_ames_czar_lane(u3_ames* sam_u, c3_y imp_y, sockaddr_in* lan_u) { - c3_s por_s = _ames_czar_port(imp_y); - c3_w pip_w; + lan_u->sin_family = AF_INET; + lan_u->sin_port = _ames_czar_port(imp_y); if ( c3n == u3_Host.ops_u.net ) { - pip_w = 0x7f000001; + lan_u->sin_addr.s_addr = NLOCALHOST; } else { - pip_w = sam_u->zar_u.pip_w[imp_y]; + lan_u->sin_addr.s_addr = + sam_u->zar_u.pip_w[imp_y]; - if ( !pip_w ) { + if ( !lan_u->sin_addr.s_addr ) { if ( u3C.wag_w & u3o_verbose ) { u3l_log("ames: czar not resolved"); } return c3n; } - else if ( _CZAR_GONE == pip_w ) { + else if ( _CZAR_GONE == lan_u->sin_addr.s_addr ) { // print only on first send failure // c3_w blk_w = imp_y >> 5; @@ -921,8 +921,6 @@ _ames_czar_lane(u3_ames* sam_u, c3_y imp_y, u3_lane* lan_u) } } - lan_u->por_s = por_s; - lan_u->pip_w = pip_w; return c3y; } @@ -973,7 +971,7 @@ _ames_is_czar(u3_noun who) /* _ames_send_lane(): resolve/decode lane. RETAIN */ static c3_o -_ames_send_lane(u3_ames* sam_u, u3_noun lan, u3_lane* lan_u) +_ames_send_lane(u3_ames* sam_u, u3_noun lan, sockaddr_in* lan_u) { u3_noun tag, val; @@ -988,26 +986,29 @@ _ames_send_lane(u3_ames* sam_u, u3_noun lan, u3_lane* lan_u) u3l_log("ames: bad galaxy lane: 0x%x", val); return c3n; } - return _ames_czar_lane(sam_u, (c3_y)val, lan_u); + _ames_czar_lane(sam_u, (c3_y)val, lan_u); + //return (c3y == _mesa_is_lane_zero(*lan_u)) ? c3n : c3y; + return __((lan_u->sin_addr.s_addr == 0) && (lan_u->sin_port == 0)); } case c3n: { // ip:port - u3_lane nal_u = u3_ames_decode_lane(u3k(val)); + sockaddr_in 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 (!nal_u.sin_addr.s_addr) + nal_u.sin_addr.s_addr = NLOCALHOST; // if in local-only mode, don't send remote packets // - if ( (c3n == u3_Host.ops_u.net) && (0x7f000001 != nal_u.pip_w) ) { + if ( (c3n == u3_Host.ops_u.net) && (NLOCALHOST != nal_u.sin_addr.s_addr) ) { return c3n; } // if the lane is uninterpretable, silently drop the packet // - else if ( !nal_u.por_s ) { + else if ( !nal_u.sin_port ) { if ( u3C.wag_w & u3o_verbose ) { u3l_log("ames: inscrutable lane"); } @@ -1036,7 +1037,7 @@ _ames_ef_send(u3_ames* sam_u, u3_noun lan, u3_noun pac) return; } - u3_lane lan_u; + sockaddr_in lan_u; if ( c3y == _ames_send_lane(sam_u, lan, &lan_u) ) { u3_pact* pac_u = c3_calloc(sizeof(*pac_u)); @@ -1136,7 +1137,7 @@ _ames_hear_bail(u3_ovum* egg_u, u3_noun lud) static void _ames_put_packet(u3_ames* sam_u, u3_noun msg, - u3_lane lan_u) + sockaddr_in lan_u) { u3_noun wir = u3nc(c3__ames, u3_nul); u3_noun cad = u3nt(c3__hear, u3nc(c3n, u3_ames_encode_lane(lan_u)), msg); @@ -1173,13 +1174,16 @@ _ames_send_many(u3_pact* pac_u, u3_noun las, c3_o for_o) u3_noun rec = u3dc("scot", 'p', u3_ship_to_noun(pac_u->pre_u.rec_u)); c3_c* sen_c = u3r_string(sen); c3_c* rec_c = u3r_string(rec); - c3_y* pip_y = (c3_y*)&pac_u->lan_u.pip_w; + + c3_w nip_w = pac_u->lan_u.sin_addr.s_addr; + c3_c nip_c[INET_ADDRSTRLEN]; + inet_ntop(AF_INET, &nip_w, nip_c, INET_ADDRSTRLEN); + //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->lan_u.por_s); + u3l_log("ames: forwarding for %s to %s from %s:%d", + sen_c, rec_c, nip_c, + ntohs(pac_u->lan_u.sin_port)); c3_free(sen_c); c3_free(rec_c); u3z(sen); u3z(rec); @@ -1708,7 +1712,7 @@ _ames_try_forward(u3_pact* pac_u) */ void _ames_hear(u3_ames* sam_u, - const struct sockaddr* adr_u, + const struct sockaddr_in* lan_u, c3_w len_w, c3_y* hun_y) { @@ -1716,127 +1720,110 @@ _ames_hear(u3_ames* sam_u, c3_w pre_w; c3_w cur_w = 0; // cursor: how many bytes we've read from hun_y - - // XX reorg, check if a STUN req/resp can look like an ames packet - // check the mug hash of the body of the packet, if not check if STUN - // otherwise , invalid packet, log failure - // check ames first, assume that STUN could maybe (not likely) overlap with ames - // for next protocol version, have an urbit cookie + // make sure packet is big enough to have a header // - if ( c3y == u3_stun_hear(u3_Host.sun_u, adr_u, len_w, hun_y) ) - return; - else { - 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) - }; - - // make sure packet is big enough to have a header - // - if ( HEAD_SIZE > len_w ) { - sam_u->sat_u.hed_d++; - if ( 0 == (sam_u->sat_u.hed_d % 100000) ) { - u3l_log("ames: %" PRIu64 " dropped, failed to read header", - sam_u->sat_u.hed_d); - } - - c3_free(hun_y); - return; + if ( HEAD_SIZE > len_w ) { + sam_u->sat_u.hed_d++; + if ( 0 == (sam_u->sat_u.hed_d % 100000) ) { + u3l_log("ames: %" PRIu64 " dropped, failed to read header", + sam_u->sat_u.hed_d); } - pac_u = c3_calloc(sizeof(*pac_u)); - pac_u->sam_u = sam_u; - pac_u->len_w = len_w; - pac_u->hun_y = hun_y; - pac_u->lan_u = lan_u; - cur_w = 0; + c3_free(hun_y); + return; + } - // parse the header - // - _ames_sift_head(&pac_u->hed_u, pac_u->hun_y); - cur_w += HEAD_SIZE; + pac_u = c3_calloc(sizeof(*pac_u)); + pac_u->sam_u = sam_u; + pac_u->len_w = len_w; + pac_u->hun_y = hun_y; + pac_u->lan_u = *lan_u; + cur_w = 0; - pac_u->typ_y = _ames_pact_typ(&pac_u->hed_u); + // parse the header + // + _ames_sift_head(&pac_u->hed_u, pac_u->hun_y); + cur_w += HEAD_SIZE; - // ensure the protocol version matches ours - // - // XX rethink use of [fit_o] here and elsewhere - // - if ( (c3y == sam_u->fig_u.fit_o) - && (sam_u->ver_y != pac_u->hed_u.ver_y) ) - { - sam_u->sat_u.vet_d++; - if ( 0 == (sam_u->sat_u.vet_d % 100000) ) { - u3l_log("ames: %" PRIu64 " dropped for version mismatch", - sam_u->sat_u.vet_d); - } - _ames_pact_free(pac_u); - return; + pac_u->typ_y = _ames_pact_typ(&pac_u->hed_u); + + // ensure the protocol version matches ours + // + // XX rethink use of [fit_o] here and elsewhere + // + if ( (c3y == sam_u->fig_u.fit_o) + && (sam_u->ver_y != pac_u->hed_u.ver_y) ) + { + sam_u->sat_u.vet_d++; + if ( 0 == (sam_u->sat_u.vet_d % 100000) ) { + u3l_log("ames: %" PRIu64 " dropped for version mismatch", + sam_u->sat_u.vet_d); } + _ames_pact_free(pac_u); + return; + } - // check contents match mug in header - // - if ( c3n == _ames_check_mug(pac_u) ) { - // _log_head(&pac_u->hed_u); - sam_u->sat_u.mut_d++; - if ( 0 == (sam_u->sat_u.mut_d % 100000) ) { - u3l_log("ames: %" PRIu64 " dropped for invalid mug", - sam_u->sat_u.mut_d); - } - _ames_pact_free(pac_u); - return; + // check contents match mug in header + // + if ( c3n == _ames_check_mug(pac_u) ) { + // _log_head(&pac_u->hed_u); + sam_u->sat_u.mut_d++; + if ( 0 == (sam_u->sat_u.mut_d % 100000) ) { + u3l_log("ames: %" PRIu64 " dropped for invalid mug", + sam_u->sat_u.mut_d); } + _ames_pact_free(pac_u); + return; + } - // check that packet is big enough for prelude - // - pre_w = _ames_prel_size(&pac_u->hed_u); - if ( len_w < cur_w + pre_w ) { - sam_u->sat_u.pre_d++; - if ( 0 == (sam_u->sat_u.pre_d % 100000) ) { - u3l_log("ames: %" PRIu64 " dropped, failed to read prelude", - sam_u->sat_u.pre_d); - } - _ames_pact_free(pac_u); - return; + // check that packet is big enough for prelude + // + pre_w = _ames_prel_size(&pac_u->hed_u); + if ( len_w < cur_w + pre_w ) { + sam_u->sat_u.pre_d++; + if ( 0 == (sam_u->sat_u.pre_d % 100000) ) { + u3l_log("ames: %" PRIu64 " dropped, failed to read prelude", + sam_u->sat_u.pre_d); } + _ames_pact_free(pac_u); + return; + } - // parse prelude - // - _ames_sift_prel(&pac_u->hed_u, &pac_u->pre_u, pac_u->hun_y + cur_w); - cur_w += pre_w; + // parse prelude + // + _ames_sift_prel(&pac_u->hed_u, &pac_u->pre_u, pac_u->hun_y + cur_w); + cur_w += pre_w; - // if we can scry for lanes, - // and we are not the recipient, - // we might want to forward statelessly - // - if ( (c3y == sam_u->fig_u.see_o) - && ( u3_ships_equal(pac_u->pre_u.rec_u, sam_u->pir_u->who_u) ) ) - { - if ( c3y == sam_u->sat_u.for_o ) { - _ames_try_forward(pac_u); - } + // if we can scry for lanes, + // and we are not the recipient, + // we might want to forward statelessly + // + if ( (c3y == sam_u->fig_u.see_o) + && ( u3_ships_equal(pac_u->pre_u.rec_u, sam_u->pir_u->who_u) ) ) + { + if ( c3y == sam_u->sat_u.for_o ) { + _ames_try_forward(pac_u); } - else { - // enter protocol-specific packet handling - // - switch ( pac_u->typ_y ) { - case PACT_WAIL: { - _fine_hear_request(pac_u, cur_w); - } break; - - case PACT_PURR: { - _fine_hear_response(pac_u, cur_w); - } break; - - case PACT_AMES: { - _ames_hear_ames(pac_u, cur_w); - } break; - - default: { - u3l_log("ames_hear: bad packet type %d", pac_u->typ_y); - u3_pier_bail(u3_king_stub()); - } + } + else { + // enter protocol-specific packet handling + // + switch ( pac_u->typ_y ) { + case PACT_WAIL: { + _fine_hear_request(pac_u, cur_w); + } break; + + case PACT_PURR: { + _fine_hear_response(pac_u, cur_w); + } break; + + case PACT_AMES: { + _ames_hear_ames(pac_u, cur_w); + } break; + + default: { + u3l_log("ames_hear: bad packet type %d", pac_u->typ_y); + u3_pier_bail(u3_king_stub()); } } } @@ -1852,6 +1839,7 @@ _ames_recv_cb(uv_udp_t* wax_u, unsigned flg_i) { u3_ames* sam_u = u3_Host.sam_u; // wax_u->data; + const struct sockaddr_in* lan_u = (const struct sockaddr_in*)adr_u; if ( 0 > nrd_i ) { if ( u3C.wag_w & u3o_verbose ) { @@ -1871,7 +1859,7 @@ _ames_recv_cb(uv_udp_t* wax_u, else { // NB: [nrd_i] will never exceed max length from _ames_alloc() // - _ames_hear(sam_u, adr_u, (c3_w)nrd_i, (c3_y*)buf_u->base); + _ames_hear(sam_u, lan_u, (c3_w)nrd_i, (c3_y*)buf_u->base); } } @@ -1957,9 +1945,10 @@ _ames_put_dear(c3_c* ship, bool fake, c3_w s_addr, c3_s port, void* context) return; } - u3_lane lan; - lan.pip_w = ntohl(s_addr); - lan.por_s = ntohs(port); + sockaddr_in lan; + lan.sin_family = AF_INET; + lan.sin_addr.s_addr = s_addr; + lan.sin_port = port; u3_noun whu = u3dc("slaw", c3__p, u3i_string(ship)); @@ -2010,22 +1999,19 @@ _ames_io_start(u3_ames* sam_u) u3l_log("ames: czar: WARNING: %d required for discoverability", zar_s); } } + struct sockaddr_in lan_u = {0}; + lan_u.sin_family = AF_INET; + lan_u.sin_addr.s_addr = _(u3_Host.ops_u.net) ? + htonl(INADDR_ANY) : + htonl(INADDR_LOOPBACK); + lan_u.sin_port = htons(por_s); + u3l_log("ames: skipping port: %u", por_s); // Bind and stuff. { - struct sockaddr_in add_u; - c3_i add_i = sizeof(add_u); - - memset(&add_u, 0, sizeof(add_u)); - add_u.sin_family = AF_INET; - add_u.sin_addr.s_addr = _(u3_Host.ops_u.net) ? - htonl(INADDR_ANY) : - htonl(INADDR_LOOPBACK); - add_u.sin_port = htons(por_s); - u3l_log("ames: skipping port: %u", por_s); /*if ( (ret_i = uv_udp_bind(&sam_u->wax_u, - (const struct sockaddr*)&add_u, 0)) != 0 ) + (const struct sockaddr*)&lan_u, 0)) != 0 ) { u3l_log("ames: port: %u", por_s); u3l_log("ames: bind: %s", uv_strerror(ret_i)); @@ -2042,10 +2028,11 @@ _ames_io_start(u3_ames* sam_u) }*/ /* - uv_udp_getsockname(&sam_u->wax_u, (struct sockaddr *)&add_u, &add_i); - u3_assert(add_u.sin_port); + c3_i add_i = sizeof(lan_u); + uv_udp_getsockname(&sam_u->wax_u, (struct sockaddr *)&lan_u, &add_i); + u3_assert(lan_u.sin_port); - sam_u->pir_u->por_s = ntohs(add_u.sin_port); + sam_u->pir_u->por_s = ntohs(lan_u.sin_port); */ } @@ -2057,11 +2044,10 @@ _ames_io_start(u3_ames* sam_u) } { - u3_noun our = u3dc("scot", 'p', u3k(who)); - char* our_s = u3r_string(our); - u3z(our); - mdns_init(por_s, !sam_u->pir_u->fak_o, our_s, _ames_put_dear, (void *)sam_u); + c3_c* our_s = u3_ship_to_string(sam_u->pir_u->who_u); + + mdns_init(lan_u.sin_port, !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); @@ -2099,21 +2085,21 @@ _ames_czar_gone(u3_ames* sam_u, c3_y imp_y) /* _ames_czar_here(): galaxy address resolution succeeded. */ static void -_ames_czar_here(u3_ames* sam_u, c3_y imp_y, c3_w pip_w) +_ames_czar_here(u3_ames* sam_u, c3_y imp_y, sockaddr_in lan_u) { c3_w old_w = sam_u->zar_u.pip_w[imp_y]; - if ( pip_w != old_w ) { + if ( lan_u.sin_addr.s_addr != old_w ) { c3_c dns_c[256]; + c3_w nip_w = lan_u.sin_addr.s_addr; + c3_c nip_c[INET_ADDRSTRLEN]; + inet_ntop(AF_INET, &nip_w, nip_c, INET_ADDRSTRLEN); + 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); + u3l_log("ames: czar %s ip .%s", dns_c, nip_c); } - sam_u->zar_u.pip_w[imp_y] = pip_w; + sam_u->zar_u.pip_w[imp_y] = lan_u.sin_addr.s_addr; { c3_w blk_w = imp_y >> 5; @@ -2140,9 +2126,8 @@ _ames_czar_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_czar_here(sam_u, imp_y, pip_w); + struct sockaddr_in* lan_u = (void*)rai_u->ai_addr; + _ames_czar_here(sam_u, imp_y, *lan_u); } else { if ( !sas_i ) { diff --git a/pkg/vere/io/ames/stun.c b/pkg/vere/io/ames/stun.c index e334c372f7..ff0ca02028 100644 --- a/pkg/vere/io/ames/stun.c +++ b/pkg/vere/io/ames/stun.c @@ -50,10 +50,10 @@ _stun_has_fingerprint(c3_y* buf_y, c3_w buf_len_w) } } -/* u3_stun_is_request(): buffer is a stun request. +/* _stun_is_request(): buffer is a stun request. */ c3_o -u3_stun_is_request(c3_y* buf_y, c3_w len_w) +_stun_is_request(c3_y* buf_y, c3_w len_w) { c3_w cookie = htonl(0x2112A442); @@ -69,10 +69,10 @@ u3_stun_is_request(c3_y* buf_y, c3_w len_w) return c3n; } -/* u3_stun_is_our_response(): buffer is a response to our request. +/* _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) +_stun_is_our_response(c3_y* buf_y, c3_y tid_y[12], c3_w len_w) { c3_w cookie = htonl(0x2112A442); @@ -89,10 +89,10 @@ u3_stun_is_our_response(c3_y* buf_y, c3_y tid_y[12], c3_w len_w) return c3n; } -/* u3_stun_make_request(): serialize stun request. +/* _stun_make_request(): serialize stun request. */ void -u3_stun_make_request(c3_y buf_y[28], c3_y tid_y[12]) +_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 @@ -114,12 +114,12 @@ u3_stun_make_request(c3_y buf_y[28], c3_y tid_y[12]) _stun_add_fingerprint(buf_y, 20); } -/* u3_stun_make_response(): serialize stun response from request. +/* _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]) +_stun_make_response(const c3_y req_y[20], + const sockaddr_in* lan_u, + c3_y buf_y[40]) { c3_w cok_w = 0x2112A442; c3_w cur_w = 20; @@ -143,8 +143,10 @@ u3_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(lan_u->por_s ^ (cok_w >> 16)); - c3_w pip_w = htonl(lan_u->pip_w ^ cok_w); + c3_s por_s = ntohs(lan_u->sin_port); + c3_w pip_w = ntohl(lan_u->sin_addr.s_addr); + por_s = htons(por_s ^ (cok_w >> 16)); + pip_w = htonl(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 @@ -153,12 +155,12 @@ u3_stun_make_response(const c3_y req_y[20], _stun_add_fingerprint(buf_y, cur_w + 12); } -/* u3_stun_find_xor_mapped_address(): extract lane from response. +/* _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) +_stun_find_xor_mapped_address(c3_y* buf_y, + c3_w len_w, + sockaddr_in* lan_u) { c3_y xor_y[4] = {0x00, 0x20, 0x00, 0x08}; c3_w cookie = 0x2112A442; @@ -179,14 +181,18 @@ u3_stun_find_xor_mapped_address(c3_y* buf_y, 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; + c3_s por_s = ntohs(c3_sift_short(buf_y + cur)) ^ (cookie >> 16); + c3_w pip_w = ntohl(c3_sift_word(buf_y + cur + 2)) ^ cookie; + + lan_u->sin_family = AF_INET; + lan_u->sin_port = htons(por_s); + lan_u->sin_addr.s_addr = htonl(pip_w); if ( u3C.wag_w & u3o_verbose ) { - c3_w nip_w = htonl(lan_u->pip_w); + c3_w nip_w = lan_u->sin_addr.s_addr; 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); + u3l_log("stun: hear ip:port %s:%u", nip_c, ntohs(lan_u->sin_port)); } return c3y; } @@ -220,23 +226,18 @@ _stun_send_cb(uv_udp_send_t *rep_u, c3_i sas_i) /* _stun_on_request(): hear stun request, send response. */ static void -_stun_on_request(u3_stun_client* sun_u, - const c3_y* req_y, - const struct sockaddr* adr_u) +_stun_on_request(u3_stun_client* sun_u, + const c3_y* req_y, + const sockaddr_in* lan_u) { _stun_send* snd_u = c3_malloc(sizeof(*snd_u) + 40); snd_u->sun_u = sun_u; - 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) - }; - u3_stun_make_response(req_y, &lan_u, snd_u->hun_y); + _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, &u3_Host.wax_u, - &buf_u, 1, adr_u, _stun_send_cb); + &buf_u, 1, (const struct sockaddr*)lan_u, _stun_send_cb); if ( sas_i ) { _stun_send_cb(&snd_u->req_u, sas_i); @@ -251,15 +252,15 @@ u3_stun_start(u3_stun_client* sun_u, c3_w tim_w); static void _stun_on_response(u3_stun_client* sun_u, c3_y* buf_y, c3_w buf_len) { - u3_lane lan_u; + sockaddr_in lan_u; // Ignore STUN responses that dont' have the XOR-MAPPED-ADDRESS attribute - if ( c3n == u3_stun_find_xor_mapped_address(buf_y, buf_len, &lan_u) ) { + if ( c3n == _stun_find_xor_mapped_address(buf_y, buf_len, &lan_u) ) { return; } - if ( (sun_u->sef_u.por_s != lan_u.por_s) || - (sun_u->sef_u.pip_w != lan_u.pip_w) ) + if ( (sun_u->sef_u.sin_addr.s_addr != lan_u.sin_addr.s_addr) || + (sun_u->sef_u.sin_port != lan_u.sin_port) ) { // lane changed u3_noun wir = u3nc(c3__ames, u3_nul); @@ -304,17 +305,11 @@ _stun_send_request(u3_stun_client* sun_u) _stun_send* snd_u = c3_malloc(sizeof(*snd_u) + 28); snd_u->sun_u = sun_u; - u3_stun_make_request(snd_u->hun_y, 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(sun_u->lan_u.pip_w); - add_u.sin_port = htons(sun_u->lan_u.por_s); + _stun_make_request(snd_u->hun_y, sun_u->tid_y); 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, &u3_Host.wax_u, &buf_u, 1, - (const struct sockaddr*)&add_u, _stun_send_cb); + (const struct sockaddr*)&sun_u->lan_u, _stun_send_cb); if ( sas_i ) { _stun_send_cb(&snd_u->req_u, sas_i); @@ -364,7 +359,7 @@ _stun_time_gap(struct timeval sar_tv) return u3_time_gap_ms(den, now); } -c3_o _ames_czar_lane(u3_auto*, c3_y, u3_lane*); +c3_o _ames_czar_lane(u3_auto*, c3_y, sockaddr_in*); /* _stun_timer_cb(): advance stun state machine. */ @@ -381,7 +376,7 @@ _stun_timer_cb(uv_timer_t* tim_u) } break; case STUN_KEEPALIVE: { - u3_lane* lan_u = &(sun_u->lan_u); + sockaddr_in* lan_u = &(sun_u->lan_u); c3_y imp_y = sun_u->dad_y; if ( c3n == _ames_czar_lane(sun_u->car_u, imp_y, lan_u) ) { @@ -418,7 +413,7 @@ _stun_timer_cb(uv_timer_t* tim_u) } } -/* _stun_start(): begin/restart STUN state machine. +/* u3_stun_start(): begin/restart STUN state machine. */ void u3_stun_start(u3_stun_client* sun_u, c3_w tim_w) @@ -432,9 +427,12 @@ u3_stun_start(u3_stun_client* sun_u, c3_w tim_w) uv_timer_start(&sun_u->tim_u, _stun_timer_cb, tim_w, 0); } + +/* u3_stun_hear(): maybe hear stun packet + */ c3_o u3_stun_hear(u3_stun_client* sun_u, - const struct sockaddr* adr_u, + const struct sockaddr_in* lan_u, c3_w len_w, c3_y* hun_y) { @@ -444,12 +442,12 @@ u3_stun_hear(u3_stun_client* sun_u, // check ames first, assume that STUN could maybe (not likely) overlap with ames // for next protocol version, have an urbit cookie // - if ( c3y == u3_stun_is_request(hun_y, len_w) ) { - _stun_on_request(sun_u, hun_y, adr_u); + if ( c3y == _stun_is_request(hun_y, len_w) ) { + _stun_on_request(sun_u, hun_y, lan_u); c3_free(hun_y); return c3y; } - else if ( c3y == u3_stun_is_our_response(hun_y, + else if ( c3y == _stun_is_our_response(hun_y, sun_u->tid_y, len_w) ) { _stun_on_response(sun_u, hun_y, len_w); diff --git a/pkg/vere/io/ames/stun.h b/pkg/vere/io/ames/stun.h index 0699bcfeed..d78545bde0 100644 --- a/pkg/vere/io/ames/stun.h +++ b/pkg/vere/io/ames/stun.h @@ -15,53 +15,24 @@ u3_stun_state sat_y; // formal state c3_y tid_y[12]; // last transaction id c3_y dad_y; // sponsoring galaxy - u3_lane lan_u; // sponsoring galaxy IP and port + sockaddr_in lan_u; // sponsoring galaxy IP and port uv_timer_t tim_u; // keepalive timer handle struct timeval sar_u; // date we started trying to send - u3_lane sef_u; // our lane, if we know it + sockaddr_in sef_u; // our lane, if we know it c3_o wok_o; // STUN worked, set on first success c3_o net_o; // online heuristic to limit verbosity } u3_stun_client; // - /* 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); - /* u3_stun_start(): begin/restart STUN state machine. */ void u3_stun_start(u3_stun_client* sam_u, c3_w tim_w); - /* u3_stun_shear(): maybe hear stun packet + /* u3_stun_hear(): maybe hear stun packet */ c3_o u3_stun_hear(u3_stun_client* sun_u, - const struct sockaddr* adr_u, + const struct sockaddr_in* adr_u, c3_w len_w, c3_y* hun_y); diff --git a/pkg/vere/io/mesa.c b/pkg/vere/io/mesa.c index cecfbbce7b..63d89b4a36 100644 --- a/pkg/vere/io/mesa.c +++ b/pkg/vere/io/mesa.c @@ -372,9 +372,9 @@ _log_gage(u3_gage* gag_u) } static void -_log_lane(u3_lane* lan_u) +_log_lane(sockaddr_in* lan_u) { - u3l_log("mesa: lane (%s,%u)", u3r_string(u3dc("scot", c3__if, u3i_word(lan_u->pip_w))), lan_u->por_s); + u3l_log("mesa: lane (%s,%u)", u3r_string(u3dc("scot", c3__if, u3i_word(ntohl(lan_u->sin_addr.s_addr)))), ntohs(lan_u->sin_port)); } static void _log_peer(u3_peer* per_u) @@ -1531,7 +1531,7 @@ _mesa_lanes_to_addrs(u3_noun las, arena* are_u) { static void _mesa_hear(u3_mesa* sam_u, - const struct sockaddr* adr_u, + const struct sockaddr_in* adr_u, c3_w len_w, c3_y* hun_y); @@ -1570,7 +1570,7 @@ packet_test(u3_mesa* sam_u, c3_c* fil_c) { c3_w len_w = *(c3_w*)(packets+i); /* u3l_log("len_w %u i %"PRIu64, len_w, i); */ i += 4; - _mesa_hear(sam_u, &adr_u, len_w, packets + i); + _mesa_hear(sam_u, (const sockaddr_in*)&adr_u, len_w, packets + i); /* tim_y[tidx] = _get_now_micros(); */ sam_u->are_u.beg = (char*)are_y; i += len_w; @@ -1663,7 +1663,7 @@ c3_o _ames_kick_newt(void* sam_u, u3_noun tag, u3_noun dat); u3_atom -u3_ames_encode_lane(u3_lane lan); +u3_ames_encode_lane(sockaddr_in lan); static void _meet_peer(u3_mesa* sam_u, u3_peer* per_u, u3_ship her_u); static void _init_peer(u3_mesa* sam_u, u3_peer* per_u); @@ -2393,7 +2393,7 @@ _mesa_req_pact_init(u3_mesa* sam_u, u3_mesa_pict* pic_u, sockaddr_in lan_u, u3_p } typedef struct _u3_mesa_lane_cb_data { - u3_lane lan_u; + sockaddr_in lan_u; u3_peer* per_u; } u3_mesa_lane_cb_data; @@ -2806,13 +2806,13 @@ _mesa_hear_poke(u3_mesa_pict* pic_u, sockaddr_in lan_u) void _ames_hear(void* sam_u, - const struct sockaddr* adr_u, + const struct sockaddr_in* adr_u, c3_w len_w, c3_y* hun_y); static void _mesa_hear(u3_mesa* sam_u, - const struct sockaddr* adr_u, + const struct sockaddr_in* lan_u, c3_w len_w, c3_y* hun_y) { @@ -2820,9 +2820,18 @@ _mesa_hear(u3_mesa* sam_u, /* fwrite(hun_y, 1, len_w, packs); */ // c3_d now_d = _get_now_micros(); if ( c3n == mesa_is_new_pact(hun_y, len_w) ) { + // XX reorg, check if a STUN req/resp can look like an ames packet + // check the mug hash of the body of the packet, if not check if STUN + // otherwise , invalid packet, log failure + // check ames first, assume that STUN could maybe (not likely) overlap with ames + // for next protocol version, have an urbit cookie + // + if ( c3y == u3_stun_hear(&sam_u->sun_u, lan_u, len_w, hun_y) ) + return; + c3_y* han_y = c3_malloc(len_w); memcpy(han_y, hun_y, len_w); - _ames_hear(u3_Host.sam_u, adr_u, len_w, han_y); + _ames_hear(u3_Host.sam_u, lan_u, len_w, han_y); return; } @@ -2834,7 +2843,7 @@ _mesa_hear(u3_mesa* sam_u, return; } - sockaddr_in sdr_u = *((sockaddr_in*)adr_u); + sockaddr_in sdr_u = *((sockaddr_in*)lan_u); switch ( pic_u->pac_u.hed_u.typ_y ) { case PACT_PEEK: { @@ -2868,6 +2877,7 @@ static void _mesa_recv_cb(uv_udp_t* wax_u, unsigned flg_i) { u3_mesa* sam_u = (u3_mesa*)wax_u->data; + const struct sockaddr_in* lan_u = (const struct sockaddr_in*)adr_u; if ( 0 > nrd_i ) { if ( u3C.wag_w & u3o_verbose ) { u3l_log("mesa: recv: fail: %s", uv_strerror(nrd_i)); @@ -2881,7 +2891,7 @@ static void _mesa_recv_cb(uv_udp_t* wax_u, } } else { - _mesa_hear(wax_u->data, adr_u, (c3_w)nrd_i, (c3_y*)buf_u->base); + _mesa_hear(wax_u->data, lan_u, (c3_w)nrd_i, (c3_y*)buf_u->base); } } @@ -3070,11 +3080,11 @@ u3_mesa_io_init(u3_pier* pir_u) car_u->io.kick_f = _mesa_io_kick; car_u->io.exit_f = _mesa_io_exit; + sam_u->sun_u.car_u = car_u; sam_u->sun_u.net_o = c3y; sam_u->sun_u.wok_o = c3n; uv_timer_init(u3L, &sam_u->sun_u.tim_u); - sam_u->sun_u.tim_u.data = sam_u; - u3_Host.sun_u = &sam_u->sun_u; + sam_u->sun_u.tim_u.data = &sam_u->sun_u; /*{ diff --git a/pkg/vere/mdns.c b/pkg/vere/mdns.c index 8b1859905c..1d8bc9268d 100644 --- a/pkg/vere/mdns.c +++ b/pkg/vere/mdns.c @@ -197,7 +197,7 @@ void mdns_init(uint16_t port, bool fake, char* our, mdns_cb* cb, void* context) } err = DNSServiceRegister(&sref, 0, 0, domain, "_ames._udp", - NULL, NULL, htons(port), 0, NULL, register_cb, (void*)register_payload); + NULL, NULL, port, 0, NULL, register_cb, (void*)register_payload); if (err != kDNSServiceErr_NoError) { if (err == kDNSServiceErr_Unknown) { diff --git a/pkg/vere/vere.h b/pkg/vere/vere.h index b578baf1d8..85fd326b59 100644 --- a/pkg/vere/vere.h +++ b/pkg/vere/vere.h @@ -40,10 +40,7 @@ /* u3_lane: ames lane (IP address and port) */ - typedef struct _u3_lane { - c3_w pip_w; // target IPv4 address - c3_s por_s; // target port - } u3_lane; + typedef struct sockaddr_in sockaddr_in; /* u3_moor_poke: poke callback function. */ @@ -343,7 +340,6 @@ c3_i xit_i; // exit code for shutdown void (*bot_f)(); // call when chis is up void* sam_u; // old ames, "unified driver" hack - void* sun_u; // old ames, "unified driver" hack uv_udp_t wax_u; // "unified driver" udp send handle c3_w* imp_u; // "unified driver" galaxy IP:s } u3_host; // host == computer == process @@ -1250,13 +1246,13 @@ /* u3_ames_decode_lane(): destructure lane from noun */ - u3_lane + sockaddr_in u3_ames_decode_lane(u3_noun); /* u3_ames_encode_lane(): encode lane as noun */ u3_noun - u3_ames_encode_lane(u3_lane); + u3_ames_encode_lane(sockaddr_in); /** mesa **/ From 78a00cd1772741337c9634be0cc57c58d23cae53 Mon Sep 17 00:00:00 2001 From: ziffee Date: Thu, 27 Mar 2025 15:11:47 -0600 Subject: [PATCH 08/34] ames: bugfixes --- pkg/vere/io/ames.c | 14 +++++++++----- pkg/vere/io/ames/stun.c | 2 +- pkg/vere/io/mesa.c | 1 + 3 files changed, 11 insertions(+), 6 deletions(-) diff --git a/pkg/vere/io/ames.c b/pkg/vere/io/ames.c index a559778f79..518e540e5b 100644 --- a/pkg/vere/io/ames.c +++ b/pkg/vere/io/ames.c @@ -699,6 +699,7 @@ _lane_scry_path(u3_noun who) static void _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; @@ -718,6 +719,7 @@ _ames_send_cb(uv_udp_send_t* req_u, c3_i sas_i) static void _ames_send(u3_pact* pac_u) { + u3_ames* sam_u = pac_u->sam_u; if ( !pac_u->hun_y @@ -729,14 +731,13 @@ _ames_send(u3_pact* pac_u) _ames_pact_free(pac_u); } else { - struct sockaddr_in lan_u = pac_u->lan_u; { 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, &u3_Host.wax_u, &buf_u, 1, - (const struct sockaddr*)&lan_u, + (const struct sockaddr*)&pac_u->lan_u, _ames_send_cb); if ( sas_i ) { @@ -798,6 +799,7 @@ _ames_lane_into_cache(u3p(u3h_root) lax_p, u3_noun who, u3_noun las) { */ static u3_weak _ames_lane_from_cache(u3p(u3h_root) lax_p, u3_noun who, c3_o nal_o) { + u3_weak lac = u3h_git(lax_p, who); if ( u3_none == lac ) { @@ -888,6 +890,7 @@ _ames_etch_czar(c3_c dns_c[256], const c3_c* dom_c, c3_y imp_y) c3_o _ames_czar_lane(u3_ames* sam_u, c3_y imp_y, sockaddr_in* lan_u) { + lan_u->sin_family = AF_INET; lan_u->sin_port = _ames_czar_port(imp_y); @@ -973,6 +976,7 @@ _ames_is_czar(u3_noun who) static c3_o _ames_send_lane(u3_ames* sam_u, u3_noun lan, sockaddr_in* lan_u) { + u3_noun tag, val; if ( c3n == u3r_cell(lan, &tag, &val) ) { @@ -986,9 +990,7 @@ _ames_send_lane(u3_ames* sam_u, u3_noun lan, sockaddr_in* lan_u) u3l_log("ames: bad galaxy lane: 0x%x", val); return c3n; } - _ames_czar_lane(sam_u, (c3_y)val, lan_u); - //return (c3y == _mesa_is_lane_zero(*lan_u)) ? c3n : c3y; - return __((lan_u->sin_addr.s_addr == 0) && (lan_u->sin_port == 0)); + return _ames_czar_lane(sam_u, (c3_y)val, lan_u); } case c3n: { // ip:port @@ -1031,6 +1033,7 @@ _ames_send_lane(u3_ames* sam_u, u3_noun lan, sockaddr_in* lan_u) static void _ames_ef_send(u3_ames* sam_u, u3_noun lan, u3_noun pac) { + if ( c3n == sam_u->car_u.liv_o ) { u3l_log("ames: not yet live, dropping outbound\r"); u3z(lan); u3z(pac); @@ -1642,6 +1645,7 @@ _fine_hear_response(u3_pact* pac_u, c3_w cur_w) static void _ames_hear_ames(u3_pact* pac_u, c3_w cur_w) { + #ifdef AMES_SKIP if ( c3_y == _ames_skip(&pac_u->pre_u) ) { _ames_pact_free(pac_u); diff --git a/pkg/vere/io/ames/stun.c b/pkg/vere/io/ames/stun.c index ff0ca02028..b1ef012e18 100644 --- a/pkg/vere/io/ames/stun.c +++ b/pkg/vere/io/ames/stun.c @@ -201,8 +201,8 @@ _stun_find_xor_mapped_address(c3_y* buf_y, typedef struct _stun_send { uv_udp_send_t req_u; // uv udp request handle - c3_y hun_y[0]; // buffer u3_stun_client* sun_u; // backpointer + c3_y hun_y[0]; // buffer } _stun_send; /* _stun_send_cb(): stun udp send callback. diff --git a/pkg/vere/io/mesa.c b/pkg/vere/io/mesa.c index 63d89b4a36..ab36689586 100644 --- a/pkg/vere/io/mesa.c +++ b/pkg/vere/io/mesa.c @@ -1671,6 +1671,7 @@ static void _mesa_ef_saxo(u3_mesa* sam_u, u3_noun zad); static c3_o _mesa_kick(u3_mesa* sam_u, u3_noun tag, u3_noun dat) { + u3l_log("%s", __func__); c3_o ret_o; switch ( tag ) { default: { From 3ccf6f4508ebd0f0a650b7ffcefed87296beff13 Mon Sep 17 00:00:00 2001 From: ziffee Date: Thu, 27 Mar 2025 16:45:15 -0600 Subject: [PATCH 09/34] ames: more bug fixes --- pkg/vere/io/ames.c | 14 ++++++-------- pkg/vere/io/mesa.c | 1 - 2 files changed, 6 insertions(+), 9 deletions(-) diff --git a/pkg/vere/io/ames.c b/pkg/vere/io/ames.c index 518e540e5b..75114156a1 100644 --- a/pkg/vere/io/ames.c +++ b/pkg/vere/io/ames.c @@ -397,13 +397,13 @@ _ames_sift_prel(u3_head* hed_u, // parse sender ship // sen_y = 2 << hed_u->sac_y; - u3_ship_to_bytes(pre_u->sen_u, sen_y, buf_y + cur_w); + pre_u->sen_u = u3_ship_of_bytes(sen_y, buf_y + cur_w); cur_w += sen_y; // parse receiver ship // rec_y = 2 << hed_u->rac_y; - u3_ship_to_bytes(pre_u->rec_u, rec_y, buf_y + cur_w); + pre_u->rec_u = u3_ship_of_bytes(rec_y, buf_y + cur_w); cur_w += rec_y; } @@ -565,13 +565,13 @@ _ames_etch_prel(u3_head* hed_u, u3_prel* pre_u, c3_y* buf_y) // write sender ship // c3_y sen_y = 2 << hed_u->sac_y; - pre_u->sen_u = u3_ship_of_bytes(sen_y, buf_y + cur_w); + u3_ship_to_bytes(pre_u->sen_u, sen_y, buf_y + cur_w); cur_w += sen_y; // write receiver ship // c3_y rec_y = 2 << hed_u->rac_y; - pre_u->rec_u = u3_ship_of_bytes(rec_y, buf_y + cur_w); + u3_ship_to_bytes(pre_u->rec_u, sen_y, buf_y + cur_w); cur_w += rec_y; } @@ -719,7 +719,6 @@ _ames_send_cb(uv_udp_send_t* req_u, c3_i sas_i) static void _ames_send(u3_pact* pac_u) { - u3_ames* sam_u = pac_u->sam_u; if ( !pac_u->hun_y @@ -770,8 +769,8 @@ u3_ames_decode_lane(u3_atom lan) { */ c3_d u3_ames_lane_to_chub(sockaddr_in lan) { - return ((c3_d)ntohs(lan.sin_addr.s_addr) << 32) - ^ (c3_d)ntohl(lan.sin_port); + return ((c3_d)ntohs(lan.sin_port) << 32) + ^ (c3_d)ntohl(lan.sin_addr.s_addr); } /* u3_ames_encode_lane(): serialize lane to noun @@ -1033,7 +1032,6 @@ _ames_send_lane(u3_ames* sam_u, u3_noun lan, sockaddr_in* lan_u) static void _ames_ef_send(u3_ames* sam_u, u3_noun lan, u3_noun pac) { - if ( c3n == sam_u->car_u.liv_o ) { u3l_log("ames: not yet live, dropping outbound\r"); u3z(lan); u3z(pac); diff --git a/pkg/vere/io/mesa.c b/pkg/vere/io/mesa.c index ab36689586..63d89b4a36 100644 --- a/pkg/vere/io/mesa.c +++ b/pkg/vere/io/mesa.c @@ -1671,7 +1671,6 @@ static void _mesa_ef_saxo(u3_mesa* sam_u, u3_noun zad); static c3_o _mesa_kick(u3_mesa* sam_u, u3_noun tag, u3_noun dat) { - u3l_log("%s", __func__); c3_o ret_o; switch ( tag ) { default: { From b82a7de0d3207d6b8472eb6477fe38639b383dba Mon Sep 17 00:00:00 2001 From: ziffee Date: Fri, 28 Mar 2025 18:16:14 -0600 Subject: [PATCH 10/34] ames: move ames to subdriver --- pkg/vere/io/{ => ames}/ames.c | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename pkg/vere/io/{ => ames}/ames.c (100%) diff --git a/pkg/vere/io/ames.c b/pkg/vere/io/ames/ames.c similarity index 100% rename from pkg/vere/io/ames.c rename to pkg/vere/io/ames/ames.c From bfde35d8345f9013696279608d8b1b8dbd661495 Mon Sep 17 00:00:00 2001 From: ziffee Date: Sat, 29 Mar 2025 13:15:11 -0600 Subject: [PATCH 11/34] zif/ames-reform: driver hax improvement --- pkg/vere/auto.c | 53 +++++--- pkg/vere/build.zig | 2 +- pkg/vere/io/ames/ames.c | 134 ++++++++++--------- pkg/vere/io/ames/lamp.c | 282 ++++++++++++++++++++++++++++++++++++++++ pkg/vere/io/ames/stun.c | 4 +- pkg/vere/io/ames/stun.h | 1 + pkg/vere/io/mesa.c | 88 +++++++------ pkg/vere/io/mesa/mesa.h | 1 + pkg/vere/vere.h | 13 +- 9 files changed, 452 insertions(+), 126 deletions(-) create mode 100644 pkg/vere/io/ames/lamp.c diff --git a/pkg/vere/auto.c b/pkg/vere/auto.c index b2da91688e..414b02231a 100644 --- a/pkg/vere/auto.c +++ b/pkg/vere/auto.c @@ -400,10 +400,10 @@ u3_auto_slog(u3_auto* car_u) } } -/* _auto_link(): validate and link initalized [car_u] +/* u3_auto_link(): validate and link initalized [car_u] */ -static u3_auto* -_auto_link(u3_auto* car_u, u3_pier* pir_u, u3_auto* nex_u) +u3_auto* +u3_auto_link(u3_auto* car_u, u3_pier* pir_u, u3_auto* nex_u) { // skip null drivers // @@ -418,7 +418,23 @@ _auto_link(u3_auto* car_u, u3_pier* pir_u, u3_auto* nex_u) u3_assert( car_u->io.exit_f ); car_u->pir_u = pir_u; - car_u->nex_u = nex_u; + u3_auto* rac_u = car_u; + while (rac_u->nex_u != NULL) { + rac_u = rac_u->nex_u; + rac_u->pir_u = pir_u; + u3_assert( rac_u->io.talk_f ); + u3_assert( rac_u->io.kick_f ); + u3_assert( rac_u->io.exit_f ); + } + + if (nex_u) { + nex_u->pir_u = pir_u; + u3_assert( nex_u->io.talk_f ); + u3_assert( nex_u->io.kick_f ); + u3_assert( nex_u->io.exit_f ); + } + + rac_u->nex_u = nex_u; return car_u; } @@ -429,17 +445,24 @@ u3_auto_init(u3_pier* pir_u) { u3_auto* car_u = 0; - car_u = _auto_link(u3_hind_io_init(pir_u), pir_u, car_u); - car_u = _auto_link(u3_behn_io_init(pir_u), pir_u, car_u); - car_u = _auto_link(u3_conn_io_init(pir_u), pir_u, car_u); - car_u = _auto_link(u3_ames_io_init(pir_u), pir_u, car_u); - car_u = _auto_link(u3_http_io_init(pir_u), pir_u, car_u); - car_u = _auto_link(u3_cttp_io_init(pir_u), pir_u, car_u); - car_u = _auto_link(u3_unix_io_init(pir_u), pir_u, car_u); - car_u = _auto_link(u3_term_io_init(pir_u), pir_u, car_u); - car_u = _auto_link(u3_fore_io_init(pir_u), pir_u, car_u); - car_u = _auto_link(u3_lick_io_init(pir_u), pir_u, car_u); - car_u = _auto_link(u3_mesa_io_init(pir_u), pir_u, car_u); + car_u = u3_auto_link(u3_hind_io_init(pir_u), pir_u, car_u); + car_u = u3_auto_link(u3_behn_io_init(pir_u), pir_u, car_u); + car_u = u3_auto_link(u3_conn_io_init(pir_u), pir_u, car_u); + car_u = u3_auto_link(u3_mesa_io_init(pir_u), pir_u, car_u); + car_u = u3_auto_link(u3_http_io_init(pir_u), pir_u, car_u); + car_u = u3_auto_link(u3_cttp_io_init(pir_u), pir_u, car_u); + car_u = u3_auto_link(u3_unix_io_init(pir_u), pir_u, car_u); + car_u = u3_auto_link(u3_term_io_init(pir_u), pir_u, car_u); + car_u = u3_auto_link(u3_fore_io_init(pir_u), pir_u, car_u); + car_u = u3_auto_link(u3_lick_io_init(pir_u), pir_u, car_u); + + u3_auto* rac_u = car_u; + while (rac_u != NULL) { + c3_c* nam_c = u3r_string(rac_u->nam_m); + u3l_log("car %s", nam_c); + c3_free(nam_c); + rac_u = rac_u->nex_u; + } return car_u; } diff --git a/pkg/vere/build.zig b/pkg/vere/build.zig index cb1668e175..044e0c7b6d 100644 --- a/pkg/vere/build.zig +++ b/pkg/vere/build.zig @@ -204,7 +204,7 @@ const c_source_files = [_][]const u8{ "db/lmdb.c", "disk.c", "foil.c", - "io/ames.c", + "io/ames/ames.c", "io/ames/stun.c", "io/behn.c", "io/conn.c", diff --git a/pkg/vere/io/ames/ames.c b/pkg/vere/io/ames/ames.c index 75114156a1..a4daa73441 100644 --- a/pkg/vere/io/ames/ames.c +++ b/pkg/vere/io/ames/ames.c @@ -46,8 +46,8 @@ u3_fine fin_s; // fine networking u3_pier* pir_u; // pier union { // uv udp handle - uv_udp_t wax_u; // - uv_handle_t had_u; // + uv_udp_t* wax_u; // + uv_handle_t* had_u; // }; // c3_l sev_l; // instance number ur_cue_test_t* tes_u; // cue-test handle @@ -89,8 +89,14 @@ c3_d vil_d; // encryption failures c3_d saw_d; // successive scry failures } sat_u; // + u3_auto* mes_u; // mesa pointer } u3_ames; +STATIC_ASSERT( + ( ((void*)(u3_ames*)(void*)0) == + ((void*)(u3_ames*)(void*)&(((u3_ames*)(void*)0)->car_u)) ), + "u3_ames struct alignment" ); + /* u3_head: ames or fine packet header */ typedef struct _u3_head { @@ -201,20 +207,20 @@ const c3_c* PATH_PARSER = static c3_o net_o = c3y; // online heuristic to limit verbosity -/* _ames_alloc(): libuv buffer allocator. -*/ -static void -_ames_alloc(uv_handle_t* had_u, - size_t len_i, - uv_buf_t* buf - ) -{ - // we allocate 2K, which gives us plenty of space - // for a single ames packet (max size 1060 bytes) - // - void* ptr_v = c3_malloc(2048); - *buf = uv_buf_init(ptr_v, 2048); -} +///* _ames_alloc(): libuv buffer allocator. +//*/ +//static void +//_ames_alloc(uv_handle_t* had_u, +// size_t len_i, +// uv_buf_t* buf +// ) +//{ +// // we allocate 2K, which gives us plenty of space +// // for a single ames packet (max size 1060 bytes) +// // +// void* ptr_v = c3_malloc(2048); +// *buf = uv_buf_init(ptr_v, 2048); +//} static void _ames_pact_free(u3_pact* pac_u) @@ -734,7 +740,7 @@ _ames_send(u3_pact* pac_u) { 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, - &u3_Host.wax_u, + sam_u->wax_u, &buf_u, 1, (const struct sockaddr*)&pac_u->lan_u, _ames_send_cb); @@ -838,7 +844,7 @@ _ames_pact_to_noun(u3_pact* pac_u) /* _ames_czar_port(): udp port for galaxy. */ -static c3_s +c3_s _ames_czar_port(c3_y imp_y) { if ( c3n == u3_Host.ops_u.net ) { @@ -1831,39 +1837,39 @@ _ames_hear(u3_ames* sam_u, } } -/* _ames_recv_cb(): udp message receive callback. -*/ -static void -_ames_recv_cb(uv_udp_t* wax_u, - ssize_t nrd_i, - const uv_buf_t * buf_u, - const struct sockaddr* adr_u, - unsigned flg_i) -{ - u3_ames* sam_u = u3_Host.sam_u; // wax_u->data; - const struct sockaddr_in* lan_u = (const struct sockaddr_in*)adr_u; - - if ( 0 > nrd_i ) { - if ( u3C.wag_w & u3o_verbose ) { - u3l_log("ames: recv: fail: %s", uv_strerror(nrd_i)); - } - c3_free(buf_u->base); - } - else if ( 0 == nrd_i ) { - c3_free(buf_u->base); - } - else if ( flg_i & UV_UDP_PARTIAL ) { - if ( u3C.wag_w & u3o_verbose ) { - u3l_log("ames: recv: fail: message truncated"); - } - c3_free(buf_u->base); - } - else { - // NB: [nrd_i] will never exceed max length from _ames_alloc() - // - _ames_hear(sam_u, lan_u, (c3_w)nrd_i, (c3_y*)buf_u->base); - } -} +///* _ames_recv_cb(): udp message receive callback. +//*/ +//static void +//_ames_recv_cb(uv_udp_t* wax_u, +// ssize_t nrd_i, +// const uv_buf_t * buf_u, +// const struct sockaddr* adr_u, +// unsigned flg_i) +//{ +// u3_ames* sam_u = u3_Host.sam_u; // wax_u->data; +// const struct sockaddr_in* lan_u = (const struct sockaddr_in*)adr_u; +// +// if ( 0 > nrd_i ) { +// if ( u3C.wag_w & u3o_verbose ) { +// u3l_log("ames: recv: fail: %s", uv_strerror(nrd_i)); +// } +// c3_free(buf_u->base); +// } +// else if ( 0 == nrd_i ) { +// c3_free(buf_u->base); +// } +// else if ( flg_i & UV_UDP_PARTIAL ) { +// if ( u3C.wag_w & u3o_verbose ) { +// u3l_log("ames: recv: fail: message truncated"); +// } +// c3_free(buf_u->base); +// } +// else { +// // NB: [nrd_i] will never exceed max length from _ames_alloc() +// // +// _ames_hear(sam_u, lan_u, (c3_w)nrd_i, (c3_y*)buf_u->base); +// } +//} static void natpmp_init(uv_timer_t* handle); @@ -2410,11 +2416,9 @@ _ames_io_kick(u3_auto* car_u, u3_noun wir, u3_noun cad) /* _ames_exit_cb(): dispose resources aftr close. */ -static void -_ames_exit_cb(uv_handle_t* had_u) +void +_ames_exit_cb(u3_ames* sam_u) { - u3_ames* sam_u = had_u->data; - u3_panc* pan_u = sam_u->pan_u; while (0 != pan_u) { u3_panc* nex_u = pan_u->nex_u; @@ -2436,7 +2440,6 @@ static void _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->nat_u.tim_u, 0); uv_handle_type handle = uv_handle_get_type((uv_handle_t *)&sam_u->nat_u.pol_u); @@ -2530,9 +2533,14 @@ _ames_io_slog(u3_auto* car_u) /* u3_ames_io_init(): initialize ames I/O. */ u3_auto* -u3_ames_io_init(u3_pier* pir_u) +u3_ames_io_init(u3_pier* pir_u, + uv_udp_t* wax_u, + u3_auto* mes_u, + c3_w** imp_u) { u3_ames* sam_u = c3_calloc(sizeof(*sam_u)); + sam_u->wax_u = wax_u; + sam_u->mes_u = mes_u; sam_u->pir_u = pir_u; sam_u->nal_o = c3n; sam_u->fig_u.see_o = c3y; @@ -2574,9 +2582,10 @@ u3_ames_io_init(u3_pier* pir_u) // sam_u->lax_p = u3h_new_cache(500000); - u3_assert( !uv_udp_init(u3L, &sam_u->wax_u) ); - u3_assert( !uv_udp_init_ex(u3L, &u3_Host.wax_u, UV_UDP_RECVMMSG) ); - sam_u->wax_u.data = sam_u; + //XX: zif what? + //u3_assert( !uv_udp_init(u3L, &sam_u->wax_u) ); + //u3_assert( !uv_udp_init_ex(u3L, sam_u->wax_u, UV_UDP_RECVMMSG) ); + //sam_u->wax_u->data = sam_u; sam_u->sil_u = u3s_cue_xeno_init(); sam_u->tes_u = ur_cue_test_init(); @@ -2608,8 +2617,7 @@ u3_ames_io_init(u3_pier* pir_u) u3z(now); } - // XX declare void pointer to u3_host and add sam_u in it - u3_Host.sam_u = sam_u; - u3_Host.imp_u = sam_u->zar_u.pip_w; + *imp_u = sam_u->zar_u.pip_w; + return car_u; } diff --git a/pkg/vere/io/ames/lamp.c b/pkg/vere/io/ames/lamp.c new file mode 100644 index 0000000000..a08aa5d528 --- /dev/null +++ b/pkg/vere/io/ames/lamp.c @@ -0,0 +1,282 @@ +#include "vere.h" +#include "io/ames/lamp.h" + +#define NAME tuf_map +#define KEY_TY u3_ship +#define HASH_FN u3_hash_ship +#define CMPR_FN u3_cmpr_ship +#define VAL_TY c3_y** +#define IMPLEMENTATION_MODE +#include "verstable.h" + +/* _ames_czar_port(): udp port for galaxy. +*/ +static c3_s +_ames_czar_port(c3_y imp_y) +{ + if ( c3n == u3_Host.ops_u.net ) { + return htons(31337 + imp_y); + } + else { + return htons(13337 + 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; +} + +/* _ames_czar_lane: retrieve lane for galaxy if stored. +*/ +c3_o +_ames_czar_lane(u3_ames* sam_u, c3_y imp_y, sockaddr_in* lan_u) +{ + + lan_u->sin_family = AF_INET; + lan_u->sin_port = _ames_czar_port(imp_y); + + if ( c3n == u3_Host.ops_u.net ) { + lan_u->sin_addr.s_addr = NLOCALHOST; + } + else { + lan_u->sin_addr.s_addr = + sam_u->zar_u.pip_w[imp_y]; + + if ( !lan_u->sin_addr.s_addr ) { + if ( u3C.wag_w & u3o_verbose ) { + u3l_log("ames: czar not resolved"); + } + return c3n; + } + else if ( _CZAR_GONE == lan_u->sin_addr.s_addr ) { + // 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; + } + } + + return c3y; +} + +typedef struct _czar_resv { + uv_getaddrinfo_t adr_u; + u3_ames* sam_u; + c3_y imp_y; +} _czar_resv; + +/* _ames_czar_gone(): galaxy address resolution failed. +*/ +static void +_ames_czar_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_czar_here(): galaxy address resolution succeeded. +*/ +static void +_ames_czar_here(u3_ames* sam_u, c3_y imp_y, sockaddr_in lan_u) +{ + c3_w old_w = sam_u->zar_u.pip_w[imp_y]; + + if ( lan_u.sin_addr.s_addr != old_w ) { + c3_c dns_c[256]; + c3_w nip_w = lan_u.sin_addr.s_addr; + c3_c nip_c[INET_ADDRSTRLEN]; + inet_ntop(AF_INET, &nip_w, nip_c, INET_ADDRSTRLEN); + + u3_assert ( !_ames_etch_czar(dns_c, sam_u->zar_u.dom_c, imp_y) ); + u3l_log("ames: czar %s ip .%s", dns_c, nip_c); + } + + sam_u->zar_u.pip_w[imp_y] = lan_u.sin_addr.s_addr; + + { + 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_czar_cb(): galaxy address resolution callback. +*/ +static void +_ames_czar_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* lan_u = (void*)rai_u->ai_addr; + _ames_czar_here(sam_u, imp_y, *lan_u); + } + 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_czar_gone(sam_u, imp_y); + } + + sam_u->zar_u.pen_s--; + + uv_freeaddrinfo(aif_u); + c3_free(res_u); +} + +/* _ames_czar(): single galaxy address resolution. +*/ +static void +_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; + _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_czar_cb, dns_c, 0, &hin_u); + + if ( sas_i ) { + _ames_czar_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 = 0; i_w < 256; 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); +} + +/* _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 ) { + 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)); + + 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 (len=%u)", len_w); + u3m_p("hot", hot); + u3_pier_bail(u3_king_stub()); + } + + 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)) ) { + 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) && (c3n == sam_u->zar_u.dom_o) ) { + u3l_log("ames: turf: no domains"); + } + + // XX is this ever necessary? + // + if ( c3n == sam_u->car_u.liv_o ) { + _ames_io_start(sam_u); + } +} + diff --git a/pkg/vere/io/ames/stun.c b/pkg/vere/io/ames/stun.c index b1ef012e18..d55fbc4ada 100644 --- a/pkg/vere/io/ames/stun.c +++ b/pkg/vere/io/ames/stun.c @@ -236,7 +236,7 @@ _stun_on_request(u3_stun_client* sun_u, _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, &u3_Host.wax_u, + c3_i sas_i = uv_udp_send(&snd_u->req_u, sun_u->wax_u, &buf_u, 1, (const struct sockaddr*)lan_u, _stun_send_cb); if ( sas_i ) { @@ -308,7 +308,7 @@ _stun_send_request(u3_stun_client* sun_u) _stun_make_request(snd_u->hun_y, sun_u->tid_y); 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, &u3_Host.wax_u, &buf_u, 1, + c3_i sas_i = uv_udp_send(&snd_u->req_u, sun_u->wax_u, &buf_u, 1, (const struct sockaddr*)&sun_u->lan_u, _stun_send_cb); if ( sas_i ) { diff --git a/pkg/vere/io/ames/stun.h b/pkg/vere/io/ames/stun.h index d78545bde0..bb0b8df692 100644 --- a/pkg/vere/io/ames/stun.h +++ b/pkg/vere/io/ames/stun.h @@ -12,6 +12,7 @@ typedef struct _u3_stun_client { // stun client state: u3_auto* car_u; // driver backpointer + uv_udp_t* wax_u; u3_stun_state sat_y; // formal state c3_y tid_y[12]; // last transaction id c3_y dad_y; // sponsoring galaxy diff --git a/pkg/vere/io/mesa.c b/pkg/vere/io/mesa.c index 63d89b4a36..e254c97175 100644 --- a/pkg/vere/io/mesa.c +++ b/pkg/vere/io/mesa.c @@ -254,10 +254,10 @@ static void u3_free_line( u3_mesa_line* lin_u ) typedef struct _u3_mesa { u3_auto car_u; u3_pier* pir_u; - union { - uv_udp_t wax_u; - uv_handle_t had_u; - }; + union { // uv udp handle + uv_udp_t wax_u; // + uv_handle_t had_u; // + }; // u3_mesa_stat sat_u; // statistics c3_l sev_l; // XX: ?? c3_o for_o; // is forwarding @@ -273,6 +273,8 @@ typedef struct _u3_mesa { arena par_u; // permanent arena uv_timer_t tim_u; // pit clear timer u3_stun_client sun_u; // stun client state + u3_auto* ame_u; // ames pointer + c3_w* imp_u; } u3_mesa; STATIC_ASSERT( @@ -650,18 +652,9 @@ _mesa_put_request(u3_mesa* sam_u, u3_mesa_name* nam_u, u3_pend_req* req_u) { } /* _ames_czar_port(): udp port for galaxy. - XX copied from io/ames.c */ -static c3_s -_ames_czar_port(c3_y imp_y) -{ - if ( c3n == u3_Host.ops_u.net ) { - return 31337 + imp_y; - } - else { - return 13337 + imp_y; - } -} +c3_s +_ames_czar_port(c3_y imp_y); static sockaddr_in _mesa_get_direct_lane(u3_mesa* sam_u, u3_ship her_u) @@ -671,7 +664,7 @@ _mesa_get_direct_lane(u3_mesa* sam_u, u3_ship her_u) if ( c3__czar == u3_ship_rank(her_u) ) { c3_s por_s = _ames_czar_port(her_u.hed_d); - adr_u.sin_addr.s_addr = htonl(u3_Host.imp_u[her_u.hed_d]); + adr_u.sin_addr.s_addr = htonl(sam_u->imp_u[her_u.hed_d]); adr_u.sin_port = htons(por_s); return adr_u; } @@ -699,7 +692,7 @@ _mesa_get_czar_lane(u3_mesa* sam_u, c3_y imp_y) c3_s por_s = _ames_czar_port(imp_y); sockaddr_in adr_u = {0}; adr_u.sin_family = AF_INET; - adr_u.sin_addr.s_addr = htonl(u3_Host.imp_u[imp_y]); + adr_u.sin_addr.s_addr = htonl(sam_u->imp_u[imp_y]); adr_u.sin_port = htons(por_s); return adr_u; } @@ -901,7 +894,7 @@ _mesa_send_cb3(uv_udp_send_t* snt_u, c3_i sas_i) c3_free(snd_u); } -static c3_i _mesa_send_buf2(struct sockaddr** ads_u, uv_buf_t** bfs_u, c3_w* int_u, c3_w num_w) +static c3_i _mesa_send_buf2(u3_mesa* sam_u, struct sockaddr** ads_u, uv_buf_t** bfs_u, c3_w* int_u, c3_w num_w) { /* add_u.sin_family = AF_INET; */ @@ -914,7 +907,7 @@ static c3_i _mesa_send_buf2(struct sockaddr** ads_u, uv_buf_t** bfs_u, c3_w* int /* c3_c* sip_c = inet_ntoa(add_u.sin_addr); */ // u3l_log("mesa: sending packet to %s:%u", sip_c, por_s); #endif - c3_i sas_i = uv_udp_try_send2(&u3_Host.wax_u, num_w, bfs_u, int_u, ads_u, 0); + c3_i sas_i = uv_udp_try_send2(&sam_u->wax_u, num_w, bfs_u, int_u, ads_u, 0); if ( sas_i < 0 ) { u3l_log("ames: send fail_sync: %s", uv_strerror(sas_i)); return 0; @@ -923,11 +916,11 @@ static c3_i _mesa_send_buf2(struct sockaddr** ads_u, uv_buf_t** bfs_u, c3_w* int // else if ( sas_i < (c3_i)num_w) { // for ( c3_i i = sas_i; i < (c3_i)num_w; i++) { // uv_udp_send_t* req_u = c3_malloc(sizeof(*req_u)); -// uv_udp_send(req_u, &u3_Host.wax_u, bfs_u[i], 1, ads_u[0], _mesa_send_cb2); +// uv_udp_send(req_u, &sam_u->wax_u, bfs_u[i], 1, ads_u[0], _mesa_send_cb2); // } // } } -static void _mesa_send_buf3(sockaddr_in add_u, uv_buf_t buf_u) +static void _mesa_send_buf3(u3_mesa* sam_u, sockaddr_in add_u, uv_buf_t buf_u) { add_u.sin_addr.s_addr = ( u3_Host.ops_u.net == c3y ) ? add_u.sin_addr.s_addr : htonl(0x7f000001); @@ -946,7 +939,7 @@ static void _mesa_send_buf3(sockaddr_in add_u, uv_buf_t buf_u) uv_udp_send_t* snd_u = c3_malloc(sizeof(*snd_u)); c3_i sas_i = uv_udp_send((uv_udp_send_t*)snd_u, - &u3_Host.wax_u, + &sam_u->wax_u, &buf_u, 1, (const struct sockaddr*)&add_u, _mesa_send_cb3); @@ -985,7 +978,7 @@ static void _mesa_send_buf(u3_mesa* sam_u, sockaddr_in add_u, c3_y* buf_y, c3_w uv_buf_t buf_u = uv_buf_init((c3_c*)buf_y, len_w); c3_i sas_i = uv_udp_send(&sel_u->snd_u, - &u3_Host.wax_u, + &sam_u->wax_u, &buf_u, 1, (const struct sockaddr*)&add_u, _mesa_send_cb); @@ -1151,7 +1144,7 @@ _try_resend(u3_pend_req* req_u, c3_d nex_d) /* new(&scr_u, uv_buf_t, 1); */ /* bfs_u[i_w] = buf_u; */ /* c3_w len_w = mesa_etch_pact_to_buf(buf_y, PACT_SIZE, pac_u); */ - /* _mesa_send_buf3(req_u->per_u->dan_u, buf_u, req_u, i_d); */ + /* _mesa_send_buf3(req_u->per_u->sam_u, req_u->per_u->dan_u, buf_u, req_u, i_d); */ _mesa_send_modal(req_u->per_u, buf_u, NULL); _mesa_req_pact_resent(req_u, &pac_u->pek_u.nam_u, now_d); // i_w++; @@ -1358,7 +1351,7 @@ _mesa_req_pact_done(u3_pend_req* req_u, } static sockaddr_in -_realise_lane(u3_noun lan) { +_realise_lane(u3_mesa* sam_u, u3_noun lan) { sockaddr_in lan_u = {0}; lan_u.sin_family = AF_INET; @@ -1368,7 +1361,7 @@ _realise_lane(u3_noun lan) { lan_u.sin_addr.s_addr = htonl(0x7f000001); lan_u.sin_port = htons(_ames_czar_port(lan)); } else { - lan_u.sin_addr.s_addr = htonl(u3_Host.imp_u[lan]); + lan_u.sin_addr.s_addr = htonl(sam_u->imp_u[lan]); lan_u.sin_port = htons(_ames_czar_port(lan)); } } else { @@ -1516,13 +1509,13 @@ _mesa_resend_timer_cb(uv_timer_t* tim_u) } static u3_pit_addr* -_mesa_lanes_to_addrs(u3_noun las, arena* are_u) { +_mesa_lanes_to_addrs(u3_mesa* sam_u, u3_noun las, arena* are_u) { u3_pit_addr* adr_u = NULL; u3_noun lan, t = las; while ( t != u3_nul ) { u3x_cell(t, &lan, &t); u3_pit_addr* new_u = new(are_u, u3_pit_addr, 1); - new_u->sdr_u = _realise_lane(u3k(lan)); + new_u->sdr_u = _realise_lane(sam_u, u3k(lan)); new_u->nex_p = adr_u; adr_u = new_u; } @@ -1636,7 +1629,7 @@ _mesa_ef_send(u3_mesa* sam_u, u3_noun las, u3_noun pac) dat_u->sam_u = sam_u; dat_u->her_u = nam_u->her_u; dat_u->nam_u = nam_u; - dat_u->las_u = _mesa_lanes_to_addrs(las, &res_u->are_u); + dat_u->las_u = _mesa_lanes_to_addrs(sam_u, las, &res_u->are_u); dat_u->buf_y = buf_y; dat_u->len_w = len_w; } @@ -1699,7 +1692,7 @@ static c3_o _mesa_kick(u3_mesa* sam_u, u3_noun tag, u3_noun dat) /* u3l_log("mesa: send old %s", tag_c); */ c3_free(tag_c); #endif - ret_o = _ames_kick_newt(u3_Host.sam_u, u3k(tag), u3k(dat)); + ret_o = _ames_kick_newt(sam_u->ame_u, u3k(tag), u3k(dat)); } break; case c3__nail: { u3_ship who_u = u3_ship_of_noun(u3h(dat)); @@ -1731,7 +1724,7 @@ static c3_o _mesa_kick(u3_mesa* sam_u, u3_noun tag, u3_noun dat) _meet_peer(sam_u, per_u, who_u); - ret_o = _ames_kick_newt(u3_Host.sam_u, u3k(tag), u3k(dat)); + ret_o = _ames_kick_newt(sam_u->ame_u, u3k(tag), u3k(dat)); } break; } @@ -1781,10 +1774,14 @@ _mesa_io_slog(u3_auto* car_u) { u3l_log(" can send: %s", FLAG(sam_u->sun_u.net_o)); } +void +_ames_exit_cb(u3_auto* sam_u); + static void _mesa_exit_cb(uv_handle_t* han_u) { u3_mesa* sam_u = (u3_mesa*)han_u->data; arena_free(&sam_u->par_u); + _ames_exit_cb(sam_u->ame_u); } static void @@ -2160,7 +2157,7 @@ _forward_lanes_cb(void* vod_p, u3_noun nun) if ( c3n == u3a_is_cat(lan) ) { // there should be only one lane that is not a direct atom // - sockaddr_in lan_u = _realise_lane(u3k(lan)); + sockaddr_in lan_u = _realise_lane(sam_u, u3k(lan)); per_u->dan_u = lan_u; } } @@ -2250,7 +2247,7 @@ _mesa_request_next_fragments(u3_mesa* sam_u, /* _mesa_send(nex_u, lan_u); */ } /* if ( i > 0 ) { */ - /* c3_i sen_i = _mesa_send_buf2(ads_u, bus_u, int_u, i); */ + /* c3_i sen_i = _mesa_send_buf2(req_u->per_u->sam_u, ads_u, bus_u, int_u, i); */ /* for (c3_w i = 0; i < sen_i; i++) { */ /* c3_w fra_w = nex_d + i; */ /* _mesa_req_pact_sent(req_u, fra_w, now_d); */ @@ -2831,7 +2828,7 @@ _mesa_hear(u3_mesa* sam_u, c3_y* han_y = c3_malloc(len_w); memcpy(han_y, hun_y, len_w); - _ames_hear(u3_Host.sam_u, lan_u, len_w, han_y); + _ames_hear(sam_u->ame_u, lan_u, len_w, han_y); return; } @@ -2938,7 +2935,7 @@ _mesa_io_talk(u3_auto* car_u) htonl(INADDR_LOOPBACK); add_u.sin_port = htons(por_s); - if ( (ret_i = uv_udp_bind(&u3_Host.wax_u, + if ( (ret_i = uv_udp_bind(&sam_u->wax_u, (const struct sockaddr*)&add_u, 0)) != 0 ) { u3l_log("mesa: bind: %s", uv_strerror(ret_i)); @@ -2954,7 +2951,7 @@ _mesa_io_talk(u3_auto* car_u) u3_pier_bail(u3_king_stub()); } - uv_udp_getsockname(&u3_Host.wax_u, (struct sockaddr *)&add_u, &add_i); + uv_udp_getsockname(&sam_u->wax_u, (struct sockaddr *)&add_u, &add_i); u3_assert(add_u.sin_port); sam_u->pir_u->por_s = ntohs(add_u.sin_port); @@ -2966,13 +2963,13 @@ _mesa_io_talk(u3_auto* car_u) u3l_log("mesa: live on %d (localhost only)", sam_u->pir_u->por_s); } - u3_Host.wax_u.data = sam_u; - uv_udp_recv_start(&u3_Host.wax_u, _mesa_alloc, _mesa_recv_cb); + sam_u->wax_u.data = sam_u; + uv_udp_recv_start(&sam_u->wax_u, _mesa_alloc, _mesa_recv_cb); c3_i rec_i = 2 * 1024 * 1024; - uv_recv_buffer_size((uv_handle_t*)&u3_Host.wax_u, &rec_i); + uv_recv_buffer_size((uv_handle_t*)&sam_u->wax_u, &rec_i); - uv_send_buffer_size((uv_handle_t*)&u3_Host.wax_u, &rec_i); + uv_send_buffer_size((uv_handle_t*)&sam_u->wax_u, &rec_i); sam_u->car_u.liv_o = c3y; //u3z(rac); u3z(who); @@ -3047,6 +3044,9 @@ u3_mesa_io_init(u3_pier* pir_u) sam_u->tim_u.data = sam_u; uv_timer_start(&sam_u->tim_u, _mesa_clear_pit, 30000, 30000); + u3_assert( !uv_udp_init_ex(u3L, &sam_u->wax_u, UV_UDP_RECVMMSG) ); + sam_u->wax_u.data = sam_u; + vt_init(&sam_u->pit_u); vt_init(&sam_u->per_u); vt_init(&sam_u->gag_u); @@ -3072,7 +3072,7 @@ u3_mesa_io_init(u3_pier* pir_u) u3_auto* car_u = &sam_u->car_u; memset(car_u, 0, sizeof(*car_u)); - car_u->nam_m = c3__ames; + car_u->nam_m = c3__mesa; car_u->liv_o = c3y; car_u->io.talk_f = _mesa_io_talk; car_u->io.info_f = _mesa_io_info; @@ -3081,12 +3081,17 @@ u3_mesa_io_init(u3_pier* pir_u) car_u->io.exit_f = _mesa_io_exit; sam_u->sun_u.car_u = car_u; + sam_u->sun_u.wax_u = &sam_u->wax_u; sam_u->sun_u.net_o = c3y; sam_u->sun_u.wok_o = c3n; uv_timer_init(u3L, &sam_u->sun_u.tim_u); sam_u->sun_u.tim_u.data = &sam_u->sun_u; + sam_u->ame_u = u3_ames_io_init(pir_u, + &sam_u->wax_u, car_u, &sam_u->imp_u); + + u3_auto_link(car_u, pir_u, sam_u->ame_u); /*{ u3_noun now; struct timeval tim_u; @@ -3097,5 +3102,6 @@ u3_mesa_io_init(u3_pier* pir_u) u3z(now); }*/ + return car_u; } diff --git a/pkg/vere/io/mesa/mesa.h b/pkg/vere/io/mesa/mesa.h index 6f2519c397..4af05e640f 100644 --- a/pkg/vere/io/mesa/mesa.h +++ b/pkg/vere/io/mesa/mesa.h @@ -188,6 +188,7 @@ typedef struct _u3_peer { c3_o ful_o; // has this been initialized? sockaddr_in dan_u; // direct lane (nullable) u3_lane_state dir_u; // direct lane state + c3_o fef_o; c3_y imp_y; // galaxy @p u3_lane_state ind_u; // indirect lane state } u3_peer; diff --git a/pkg/vere/vere.h b/pkg/vere/vere.h index 85fd326b59..74fa4a3f7c 100644 --- a/pkg/vere/vere.h +++ b/pkg/vere/vere.h @@ -339,9 +339,6 @@ c3_o pep_o; // prep for upgrade c3_i xit_i; // exit code for shutdown void (*bot_f)(); // call when chis is up - void* sam_u; // old ames, "unified driver" hack - uv_udp_t wax_u; // "unified driver" udp send handle - c3_w* imp_u; // "unified driver" galaxy IP:s } u3_host; // host == computer == process /** Pier system. @@ -883,6 +880,11 @@ u3_noun u3_auto_info(u3_auto* car_u); + /* u3_auto_link(): validate and link initalized [car_u] + */ + u3_auto* + u3_auto_link(u3_auto* car_u, u3_pier* pir_u, u3_auto* nex_u); + /* u3_auto_slog(): print status info. */ void @@ -1242,7 +1244,10 @@ /* u3_ames_io_init(): initialize ames I/O. */ u3_auto* - u3_ames_io_init(u3_pier* pir_u); + u3_ames_io_init(u3_pier* pir_u, + uv_udp_t* wax_u, + u3_auto* mes_u, + c3_w** imp_u); /* u3_ames_decode_lane(): destructure lane from noun */ From 47cf475d566e0e303ac14de1819a9c3513ea3fea Mon Sep 17 00:00:00 2001 From: ziffee Date: Wed, 2 Apr 2025 14:00:08 -0600 Subject: [PATCH 12/34] ames: shared peer state between ames and mesa, dns in peer state --- pkg/vere/auto.c | 8 - pkg/vere/build.zig | 1 + pkg/vere/io/ames/ames.c | 649 ++++++++++------------------------------ pkg/vere/io/ames/lamp.c | 314 +++++++++---------- pkg/vere/io/ames/stun.c | 12 +- pkg/vere/io/ames/stun.h | 2 +- pkg/vere/io/lamp.h | 15 + pkg/vere/io/mesa.c | 257 ++++++++-------- pkg/vere/io/mesa/mesa.h | 7 +- pkg/vere/vere.h | 13 +- pkg/vere/ward.c | 81 +++++ 11 files changed, 567 insertions(+), 792 deletions(-) create mode 100644 pkg/vere/io/lamp.h diff --git a/pkg/vere/auto.c b/pkg/vere/auto.c index 414b02231a..a9fb208965 100644 --- a/pkg/vere/auto.c +++ b/pkg/vere/auto.c @@ -456,13 +456,5 @@ u3_auto_init(u3_pier* pir_u) car_u = u3_auto_link(u3_fore_io_init(pir_u), pir_u, car_u); car_u = u3_auto_link(u3_lick_io_init(pir_u), pir_u, car_u); - u3_auto* rac_u = car_u; - while (rac_u != NULL) { - c3_c* nam_c = u3r_string(rac_u->nam_m); - u3l_log("car %s", nam_c); - c3_free(nam_c); - rac_u = rac_u->nex_u; - } - return car_u; } diff --git a/pkg/vere/build.zig b/pkg/vere/build.zig index 044e0c7b6d..f6dff31c34 100644 --- a/pkg/vere/build.zig +++ b/pkg/vere/build.zig @@ -206,6 +206,7 @@ const c_source_files = [_][]const u8{ "foil.c", "io/ames/ames.c", "io/ames/stun.c", + "io/ames/lamp.c", "io/behn.c", "io/conn.c", "io/cttp.c", diff --git a/pkg/vere/io/ames/ames.c b/pkg/vere/io/ames/ames.c index a4daa73441..c9e5c74106 100644 --- a/pkg/vere/io/ames/ames.c +++ b/pkg/vere/io/ames/ames.c @@ -3,6 +3,7 @@ #include "vere.h" #include "mdns.h" #include "io/ames/stun.h" +#include "io/lamp.h" #include "io/mesa/mesa.h" #include "noun.h" @@ -27,9 +28,7 @@ #define QUEUE_MAX 30 // max number of packets in queue -#define _CZAR_GONE (htonl(UINT32_MAX)) - -#define NLOCALHOST (htonl(0x7f000001)) +#define DIRECT_ROUTE_TIMEOUT_MICROS 120000000 /* u3_fine: fine networking */ @@ -53,22 +52,12 @@ ur_cue_test_t* tes_u; // cue-test handle u3_cue_xeno* sil_u; // cue handle c3_y ver_y; // protocol version - u3p(u3h_root) lax_p; // lane scry cache struct _u3_panc* pan_u; // outbound packet queue, backward - 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 { 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 see_o; // can scry c3_o fit_o; // filtering active @@ -720,6 +709,12 @@ _ames_send_cb(uv_udp_send_t* req_u, c3_i sas_i) _ames_pact_free(pac_u); } +u3_peer* +_mesa_get_peer(u3_auto* sam_u, u3_ship her_u); + +u3_peer* +_mesa_gut_peer(u3_auto* sam_u, u3_ship her_u); + /* _ames_send(): send buffer to address on port. */ static void @@ -752,6 +747,15 @@ _ames_send(u3_pact* pac_u) } } +sockaddr_in +u3_ames_chub_to_lane(c3_d lan_d) { + sockaddr_in lan_u; + lan_u.sin_family = AF_INET; + lan_u.sin_addr.s_addr = htonl((c3_w)lan_d); + lan_u.sin_port = htons((c3_s)(lan_d >> 32)); + return lan_u; +} + /* u3_ames_decode_lane(): deserialize noun to lane; 0.0.0.0:0 if invalid */ sockaddr_in @@ -764,11 +768,7 @@ u3_ames_decode_lane(u3_atom lan) { u3z(lan); - sockaddr_in lan_u; - lan_u.sin_family = AF_INET; - lan_u.sin_addr.s_addr = htonl((c3_w)lan_d); - lan_u.sin_port = htons((c3_s)(lan_d >> 32)); - return lan_u; + return u3_ames_chub_to_lane(lan_d); } /* u3_ames_lane_to_chub(): serialize lane to double-word @@ -790,146 +790,103 @@ u3_ames_encode_lane(sockaddr_in lan) { /* _ames_lane_into_cache(): put las for who into cache, including timestamp */ -static void -_ames_lane_into_cache(u3p(u3h_root) lax_p, u3_noun who, u3_noun las) { - struct timeval tim_tv; - gettimeofday(&tim_tv, 0); - u3_noun now = u3_time_in_tv(&tim_tv); - u3_noun val = u3nc(las, now); - u3h_put(lax_p, who, val); - u3z(who); -} - -/* _ames_lane_from_cache(): retrieve lane for who from cache, if any -*/ -static u3_weak -_ames_lane_from_cache(u3p(u3h_root) lax_p, u3_noun who, c3_o nal_o) { - - u3_weak lac = u3h_git(lax_p, who); +u3_peer* +_ames_lane_into_cache(u3_ames* sam_u, u3_noun who, u3_noun las) +{ + u3_ship who_u = u3_ship_of_noun(who); + u3_peer* per_u = _mesa_gut_peer(sam_u->mes_u, who_u); - if ( u3_none == lac ) { - u3z(who); - return lac; - } + // XX the format of the lane %nail gives is (list (each @p address)) - if ( nal_o == c3y ) { - lac = u3k(u3h(lac)); + if ( las == u3_nul ) { + per_u->dan_u = (sockaddr_in){0}; } - else { - struct timeval tim_tv; - gettimeofday(&tim_tv, 0); - u3_noun now = u3_time_in_tv(&tim_tv); - u3_noun den = u3t(lac); - - // consider entries older than 2 minutes stale, ignore them - // - if ( 120000 > u3_time_gap_ms(u3k(den), now) ) { - lac = u3k(u3h(lac)); + u3_noun lan = u3h(las); + // we either have a direct route, and a galaxy, or just one lane + if ( c3n == u3h(lan) ) { + per_u->dan_u = u3_ames_decode_lane(u3k(u3t(lan))); + if ( c3y == u3du(u3t(las)) ) + u3_noun lan = u3h(u3t(las)); + assert(c3y == u3h(lan)); + per_u->lam_u = u3_ship_of_noun(u3t(lan)); } else { - lac = u3_none; + u3_ship who_u = u3_ship_of_noun(u3t(lan)); + if ( c3n == u3_ships_equal(sam_u->car_u.pir_u->who_u, who_u) ) { + // delete direct lane if galaxy + per_u->dan_u = (sockaddr_in){0}; + } else { + per_u->lam_o = c3y; + } + per_u->lam_u = who_u; } } - - u3z(who); - return lac; + u3z(who); u3z(las); + return per_u; } -static u3_noun -_ames_pact_to_noun(u3_pact* pac_u) -{ - u3_noun pac = u3i_bytes(pac_u->len_w, pac_u->hun_y); - return pac; -} - -/* _ames_czar_port(): udp port for galaxy. -*/ -c3_s -_ames_czar_port(c3_y imp_y) -{ - if ( c3n == u3_Host.ops_u.net ) { - return htons(31337 + imp_y); - } - else { - return htons(13337 + imp_y); - } -} +c3_d +_get_now_micros(); -/* _ames_czar_str: galaxy name as c3_c[3] -*/ -static void -_ames_czar_str(c3_c zar_c[3], c3_y imp_y) +c3_o +_ames_is_direct_mode(u3_peer* per_u) { - u3_po_to_suffix(imp_y, (c3_y*)zar_c, (c3_y*)zar_c + 1, (c3_y*)zar_c + 2); + c3_d now_d = _get_now_micros(); + return __( (c3y == per_u->lam_o) || + (per_u->dir_u.her_d + DIRECT_ROUTE_TIMEOUT_MICROS > now_d) ); } -/* _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); +c3_o +_mesa_is_lane_zero(sockaddr_in lan_u); - // name 3, '.' 2, trailing null - // - if ( 250 <= len_w ) { - return -1; +static c3_o +_ames_lane_from_peer(u3_ames* sam_u, + u3_peer* per_u, + sockaddr_in lan_u[2]) { + if ( c3y == u3_ships_equal(per_u->her_u, sam_u->pir_u->who_u) ) + return c3y; + if ( NULL == per_u ) return c3n; + if ( c3y == _mesa_is_lane_zero(per_u->dan_u) ) { + if ( c3y == u3_ships_equal(per_u->lam_u, sam_u->pir_u->who_u) ) + return c3y; + u3_peer* lam_u = _mesa_get_peer(sam_u->mes_u, per_u->lam_u); + if ( NULL == lam_u || + (c3y == _mesa_is_lane_zero(lam_u->dan_u)) ) + return c3y; + lan_u[0] = lam_u->dan_u; + return c3y; + } else if ( c3y == _ames_is_direct_mode(per_u) ) { + lan_u[0] = per_u->dan_u; + return c3y; + } else { + lan_u[0] = per_u->dan_u; + if ( c3y == u3_ships_equal(per_u->lam_u, sam_u->pir_u->who_u) ) + return c3y; + u3_peer* lam_u = _mesa_get_peer(sam_u->mes_u, per_u->lam_u); + if ( NULL == lam_u || (c3y == _mesa_is_lane_zero(lam_u->dan_u)) ) + return c3y; + lan_u[1] = lam_u->dan_u; + return c3y; } - - _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; } -/* _ames_czar_lane: retrieve lane for galaxy if stored. +/* _ames_lane_from_cache(): retrieve lane for who from cache, if any */ -c3_o -_ames_czar_lane(u3_ames* sam_u, c3_y imp_y, sockaddr_in* lan_u) -{ - - lan_u->sin_family = AF_INET; - lan_u->sin_port = _ames_czar_port(imp_y); - - if ( c3n == u3_Host.ops_u.net ) { - lan_u->sin_addr.s_addr = NLOCALHOST; - } - else { - lan_u->sin_addr.s_addr = - sam_u->zar_u.pip_w[imp_y]; - - if ( !lan_u->sin_addr.s_addr ) { - if ( u3C.wag_w & u3o_verbose ) { - u3l_log("ames: czar not resolved"); - } - return c3n; - } - else if ( _CZAR_GONE == lan_u->sin_addr.s_addr ) { - // 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; - } - } +static c3_o +_ames_lane_from_cache(u3_ames* sam_u, + u3_noun who, + sockaddr_in lan_u[2]) { + memset(lan_u, 0, sizeof(sockaddr_in) * 2); + u3_ship who_u = u3_ship_of_noun(who); + u3_peer* per_u = _mesa_get_peer(sam_u->mes_u, who_u); + return _ames_lane_from_peer(sam_u, per_u, lan_u); +} - return c3y; +static u3_noun +_ames_pact_to_noun(u3_pact* pac_u) +{ + u3_noun pac = u3i_bytes(pac_u->len_w, pac_u->hun_y); + return pac; } /* _fine_get_cache(): get packet list or status from cache. RETAIN @@ -976,6 +933,9 @@ _ames_is_czar(u3_noun who) return zar; } +c3_o +_ames_lamp_lane(u3_auto* car_u, u3_ship her_u, sockaddr_in* lan_u); + /* _ames_send_lane(): resolve/decode lane. RETAIN */ static c3_o @@ -991,11 +951,7 @@ _ames_send_lane(u3_ames* sam_u, u3_noun lan, sockaddr_in* lan_u) 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); + return _ames_lamp_lane(sam_u->mes_u, u3_ship_of_noun(val), lan_u); } case c3n: { // ip:port @@ -1036,32 +992,28 @@ _ames_send_lane(u3_ames* sam_u, u3_noun lan, sockaddr_in* lan_u) /* _ames_ef_send(): send packet to network (v4). */ static void -_ames_ef_send(u3_ames* sam_u, u3_noun lan, u3_noun pac) +_ames_ef_send(u3_ames* sam_u, sockaddr_in lan_u, u3_noun pac) { if ( c3n == sam_u->car_u.liv_o ) { u3l_log("ames: not yet live, dropping outbound\r"); - u3z(lan); u3z(pac); + u3z(pac); return; } - sockaddr_in lan_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->lan_u = lan_u; - pac_u->len_w = u3r_met(3, pac); - pac_u->hun_y = c3_malloc(pac_u->len_w); + u3_pact* pac_u = c3_calloc(sizeof(*pac_u)); + pac_u->sam_u = sam_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); - u3r_bytes(0, pac_u->len_w, pac_u->hun_y, pac); + u3r_bytes(0, pac_u->len_w, pac_u->hun_y, pac); - _ames_sift_head(&pac_u->hed_u, pac_u->hun_y); - pac_u->typ_y = _ames_pact_typ(&pac_u->hed_u); + _ames_sift_head(&pac_u->hed_u, pac_u->hun_y); + pac_u->typ_y = _ames_pact_typ(&pac_u->hed_u); - _ames_send(pac_u); - } + _ames_send(pac_u); - u3z(lan); u3z(pac); + u3z(pac); } /* _ames_cap_queue(): cap ovum queue at QUEUE_MAX, dropping oldest packets. @@ -1160,10 +1112,9 @@ _ames_put_packet(u3_ames* sam_u, /* _ames_send_many(): send pac_u on the (list lane) las; retains pac_u */ static void -_ames_send_many(u3_pact* pac_u, u3_noun las, c3_o for_o) +_ames_send_many(u3_pact* pac_u, sockaddr_in lan_u[2], c3_o for_o) { u3_ames* sam_u = pac_u->sam_u; - u3_noun tag, dat, lan, t = las; u3_noun pac = _ames_pact_to_noun(pac_u); // if forwarding, track metrics @@ -1197,36 +1148,12 @@ _ames_send_many(u3_pact* pac_u, u3_noun las, c3_o for_o) } } - while ( u3_nul != t ) { - u3x_cell(t, &lan, &t); - - // validate lane and skip self if galaxy - // - if ( c3n == u3r_cell(lan, &tag, &dat) ) { - u3l_log("ames: bogus lane"); - } - else { - c3_o sen_o = c3y; - - if ( c3y == tag ) { - u3_ship who_u = u3_ship_of_noun(dat); - - if ( u3_ships_equal(who_u, sam_u->pir_u->who_u ) ) - { - sen_o = c3n; - if ( u3C.wag_w & u3o_verbose ) { - u3l_log("ames: forward skipping self"); - } - } - } - - if ( c3y == sen_o ) { - _ames_ef_send(sam_u, u3k(lan), u3k(pac)); - } - } + for ( c3_w i = 0; + ( (i < 2) && _mesa_is_lane_zero(lan_u[i]) ); + i++) { + _ames_ef_send(sam_u, lan_u[i], u3k(pac)); } u3z(pac); - u3z(las); } /* _ames_lane_scry_cb(): learn lanes to send packet on @@ -1253,16 +1180,20 @@ _ames_lane_scry_cb(void* vod_p, u3_noun nun) else { sam_u->sat_u.saw_d = 0; + u3_ship who_u = pac_u->pre_u.rec_u; + u3_noun who = u3_ship_to_noun(who_u); // cache the scry result for later use // - _ames_lane_into_cache(sam_u->lax_p, - u3_ship_to_noun(pac_u->pre_u.rec_u), + u3_peer* per_u = _ames_lane_into_cache(sam_u, + who, u3k(las)); - + sockaddr_in lan_u[2]; + _ames_lane_from_peer(sam_u, per_u, lan_u); + // if there are lanes, send the packet on them; otherwise drop it // - if ( u3_nul != las ) { - _ames_send_many(pac_u, u3k(las), pan_u->for_o); + if ( c3n == _mesa_is_lane_zero(lan_u[0]) ) { + _ames_send_many(pac_u, lan_u, pan_u->for_o); } } _ames_panc_free(pan_u); @@ -1287,25 +1218,15 @@ _ames_lane_scry_forward_cb(void *vod_p, u3_noun nun) static void _ames_try_send(u3_pact* pac_u, c3_o for_o) { - u3_weak lac; u3_ames* sam_u = pac_u->sam_u; - // if the recipient is a galaxy, their lane is always &+~gax - // - if ( c3__czar == u3_ship_rank(pac_u->pre_u.rec_u) ) - { - lac = u3nc(u3nc(c3y, (c3_y)pac_u->pre_u.rec_u.hed_d), u3_nul); - } - // otherwise, try to get the lane from cache - // - else { - u3_noun key = u3_ship_to_noun(pac_u->pre_u.rec_u); - lac = _ames_lane_from_cache(sam_u->lax_p, key, sam_u->nal_o); - } + u3_noun key = u3_ship_to_noun(pac_u->pre_u.rec_u); + sockaddr_in lan_u[2]; + c3_o lan_o = _ames_lane_from_cache(sam_u, key, lan_u); // if we know there's no lane, drop the packet // - if ( u3_nul == lac ) { + if ( (c3y == lan_o) && ( c3y == _mesa_is_lane_zero(lan_u[0]) ) ) { _ames_pact_free(pac_u); return; } @@ -1319,7 +1240,7 @@ _ames_try_send(u3_pact* pac_u, c3_o for_o) // _ames_cap_queue. as such, blocked on u3_lord_peek_cancel or w/e. // if ( (c3y == for_o) - && (u3_none == lac) + && (c3n == lan_o) && (1000 < sam_u->sat_u.foq_d) ) { sam_u->sat_u.fod_d++; @@ -1334,8 +1255,8 @@ _ames_try_send(u3_pact* pac_u, c3_o for_o) // if we already know the lane, just send // - if ( u3_none != lac ) { - _ames_send_many(pac_u, lac, for_o); + if ( c3y == lan_o ) { + _ames_send_many(pac_u, lan_u, for_o); _ames_pact_free(pac_u); } // store the packet to be sent later when the lane scry completes @@ -1671,8 +1592,8 @@ _ames_try_forward(u3_pact* pac_u) { // insert origin lane if needed // - if ( c3n == pac_u->hed_u.rel_o - && ( c3__czar == u3_ship_rank(pac_u->pre_u.sen_u) ) ) + if ( c3n == pac_u->hed_u.rel_o ) + //&& ( c3__czar == u3_ship_rank(pac_u->pre_u.sen_u) ) ) { c3_y* old_y; c3_w old_w, cur_w; @@ -1802,6 +1723,16 @@ _ames_hear(u3_ames* sam_u, _ames_sift_prel(&pac_u->hed_u, &pac_u->pre_u, pac_u->hun_y + cur_w); cur_w += pre_w; + c3_o dir_o = (pac_u->pre_u.rog_d == 0) ? c3y : c3n; + u3_peer* per_u = _mesa_gut_peer(sam_u->mes_u, pac_u->pre_u.sen_u); + if (c3n == dir_o) { + if ( c3n == per_u->lam_o ) + per_u->dan_u = u3_ames_chub_to_lane(pac_u->pre_u.rog_d); + per_u->ind_u.her_d = _get_now_micros(); + } else { + per_u->dir_u.her_d = _get_now_micros(); + } + // if we can scry for lanes, // and we are not the recipient, // we might want to forward statelessly @@ -1837,40 +1768,6 @@ _ames_hear(u3_ames* sam_u, } } -///* _ames_recv_cb(): udp message receive callback. -//*/ -//static void -//_ames_recv_cb(uv_udp_t* wax_u, -// ssize_t nrd_i, -// const uv_buf_t * buf_u, -// const struct sockaddr* adr_u, -// unsigned flg_i) -//{ -// u3_ames* sam_u = u3_Host.sam_u; // wax_u->data; -// const struct sockaddr_in* lan_u = (const struct sockaddr_in*)adr_u; -// -// if ( 0 > nrd_i ) { -// if ( u3C.wag_w & u3o_verbose ) { -// u3l_log("ames: recv: fail: %s", uv_strerror(nrd_i)); -// } -// c3_free(buf_u->base); -// } -// else if ( 0 == nrd_i ) { -// c3_free(buf_u->base); -// } -// else if ( flg_i & UV_UDP_PARTIAL ) { -// if ( u3C.wag_w & u3o_verbose ) { -// u3l_log("ames: recv: fail: message truncated"); -// } -// c3_free(buf_u->base); -// } -// else { -// // NB: [nrd_i] will never exceed max length from _ames_alloc() -// // -// _ames_hear(sam_u, lan_u, (c3_w)nrd_i, (c3_y*)buf_u->base); -// } -//} - static void natpmp_init(uv_timer_t* handle); static void @@ -1991,22 +1888,22 @@ _ames_io_start(u3_ames* sam_u) { c3_s por_s = sam_u->pir_u->por_s; u3_noun who = u3_ship_to_noun(sam_u->pir_u->who_u); - c3_o zar_o = _ames_is_czar(who); + //c3_o zar_o = _ames_is_czar(who); c3_i ret_i; - if ( c3y == zar_o ) { - c3_y num_y = (c3_y)sam_u->pir_u->who_u.hed_d; - c3_s zar_s = _ames_czar_port(num_y); + //if ( c3y == zar_o ) { + // c3_y num_y = (c3_y)sam_u->pir_u->who_u.hed_d; + // c3_s zar_s = _ames_czar_port(num_y); - if ( 0 == por_s ) { - por_s = zar_s; - } - else if ( por_s != zar_s ) { - u3l_log("ames: czar: overriding port %d with -p %d", zar_s, por_s); - u3l_log("ames: czar: WARNING: %d required for discoverability", zar_s); - } - } + // if ( 0 == por_s ) { + // por_s = zar_s; + // } + // else if ( por_s != zar_s ) { + // u3l_log("ames: czar: overriding port %d with -p %d", zar_s, por_s); + // u3l_log("ames: czar: WARNING: %d required for discoverability", zar_s); + // } + //} struct sockaddr_in lan_u = {0}; lan_u.sin_family = AF_INET; lan_u.sin_addr.s_addr = _(u3_Host.ops_u.net) ? @@ -2072,188 +1969,6 @@ _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_czar_gone(): galaxy address resolution failed. -*/ -static void -_ames_czar_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_czar_here(): galaxy address resolution succeeded. -*/ -static void -_ames_czar_here(u3_ames* sam_u, c3_y imp_y, sockaddr_in lan_u) -{ - c3_w old_w = sam_u->zar_u.pip_w[imp_y]; - - if ( lan_u.sin_addr.s_addr != old_w ) { - c3_c dns_c[256]; - c3_w nip_w = lan_u.sin_addr.s_addr; - c3_c nip_c[INET_ADDRSTRLEN]; - inet_ntop(AF_INET, &nip_w, nip_c, INET_ADDRSTRLEN); - - u3_assert ( !_ames_etch_czar(dns_c, sam_u->zar_u.dom_c, imp_y) ); - u3l_log("ames: czar %s ip .%s", dns_c, nip_c); - } - - sam_u->zar_u.pip_w[imp_y] = lan_u.sin_addr.s_addr; - - { - 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_czar_cb(): galaxy address resolution callback. -*/ -static void -_ames_czar_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* lan_u = (void*)rai_u->ai_addr; - _ames_czar_here(sam_u, imp_y, *lan_u); - } - 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_czar_gone(sam_u, imp_y); - } - - sam_u->zar_u.pen_s--; - - uv_freeaddrinfo(aif_u); - c3_free(res_u); -} - -/* _ames_czar(): single galaxy address resolution. -*/ -static void -_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; - _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_czar_cb, dns_c, 0, &hin_u); - - if ( sas_i ) { - _ames_czar_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 = 0; i_w < 256; 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); -} - -/* _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 ) { - 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)); - - 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 (len=%u)", len_w); - u3m_p("hot", hot); - u3_pier_bail(u3_king_stub()); - } - - 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)) ) { - 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) && (c3n == sam_u->zar_u.dom_o) ) { - u3l_log("ames: turf: no domains"); - } - - // XX is this ever necessary? - // - if ( c3n == sam_u->car_u.liv_o ) { - _ames_io_start(sam_u); - } -} - - /* _ames_prot_scry_cb(): receive ames protocol version */ static void @@ -2334,25 +2049,14 @@ _ames_kick_newt(u3_ames* sam_u, u3_noun tag, u3_noun dat) } break; case c3__send: { - u3_noun lan = u3k(u3h(dat)); u3_noun pac = u3k(u3t(dat)); - _ames_ef_send(sam_u, lan, pac); - ret_o = c3y; - } break; - - case c3__turf: { - _ames_ef_turf(sam_u, u3k(dat)); - ret_o = c3y; - } break; - - case c3__nail: { - u3_noun who = u3k(u3h(dat)); - u3_noun las = u3k(u3t(dat)); - _ames_lane_into_cache(sam_u->lax_p, who, las); - sam_u->nal_o = c3y; + u3_noun lan = u3k(u3h(dat)); + sockaddr_in lan_u; + if ( c3y == _ames_send_lane(sam_u, lan, &lan_u) ) { + _ames_ef_send(sam_u, lan_u, pac); + } ret_o = c3y; } break; - } u3z(tag); u3z(dat); @@ -2426,8 +2130,6 @@ _ames_exit_cb(u3_ames* sam_u) pan_u = nex_u; } - u3h_free(sam_u->lax_p); - u3s_cue_xeno_done(sam_u->sil_u); ur_cue_test_done(sam_u->tes_u); @@ -2454,22 +2156,17 @@ static u3_noun _ames_io_info(u3_auto* car_u) { u3_ames* sam_u = (u3_ames*)car_u; - c3_w sac_w, lax_w; + c3_w sac_w; sac_w = u3h_count(sam_u->fin_s.sac_p) * 4; u3h_discount(sam_u->fin_s.sac_p); - lax_w = u3h_count(sam_u->lax_p) * 4; - u3h_discount(sam_u->lax_p); - return u3i_list( u3_pier_mase("filtering", sam_u->fig_u.fit_o), u3_pier_mase("can-send", net_o), u3_pier_mase("can-scry", sam_u->fig_u.see_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))), - u3_pier_mase("lane-cache-size", u3i_word(lax_w)), u3_pier_mase("dropped", u3i_chub(sam_u->sat_u.dop_d)), u3_pier_mase("forwards-dropped", u3i_chub(sam_u->sat_u.fod_d)), u3_pier_mase("forwards-pending", u3i_chub(sam_u->sat_u.foq_d)), @@ -2483,7 +2180,6 @@ _ames_io_info(u3_auto* car_u) u3_pier_mase("crashed", u3i_chub(sam_u->sat_u.fal_d)), u3_pier_mase("evil", u3i_chub(sam_u->sat_u.vil_d)), u3_pier_mase("lane-scry-fails", u3i_chub(sam_u->sat_u.saw_d)), - u3_pier_mase("cached-lanes", u3i_word(u3h_wyt(sam_u->lax_p))), u3_none); } @@ -2493,15 +2189,11 @@ static void _ames_io_slog(u3_auto* car_u) { u3_ames* sam_u = (u3_ames*)car_u; - c3_w sac_w, lax_w; + c3_w sac_w; sac_w = u3h_count(sam_u->fin_s.sac_p) * 4; u3h_discount(sam_u->fin_s.sac_p); - lax_w = u3h_count(sam_u->lax_p) * 4; - u3h_discount(sam_u->lax_p); - - # define FLAG(a) ( (c3y == a) ? "&" : "|" ) // TODO rewrite in terms of info_f @@ -2511,7 +2203,6 @@ _ames_io_slog(u3_auto* car_u) u3l_log(" can send: %s", FLAG(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(" counters:"); u3l_log(" dropped: %" PRIu64, sam_u->sat_u.dop_d); @@ -2527,7 +2218,6 @@ _ames_io_slog(u3_auto* car_u) u3l_log(" crashed: %" PRIu64, sam_u->sat_u.fal_d); u3l_log(" evil: %" PRIu64, sam_u->sat_u.vil_d); u3l_log(" lane scry fails: %" PRIu64, sam_u->sat_u.saw_d); - u3l_log(" cached lanes: %u", u3h_wyt(sam_u->lax_p)); } /* u3_ames_io_init(): initialize ames I/O. @@ -2535,20 +2225,14 @@ _ames_io_slog(u3_auto* car_u) u3_auto* u3_ames_io_init(u3_pier* pir_u, uv_udp_t* wax_u, - u3_auto* mes_u, - c3_w** imp_u) + u3_auto* mes_u) { u3_ames* sam_u = c3_calloc(sizeof(*sam_u)); sam_u->wax_u = wax_u; sam_u->mes_u = mes_u; sam_u->pir_u = pir_u; - sam_u->nal_o = c3n; sam_u->fig_u.see_o = c3y; sam_u->fig_u.fit_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; // initialize libnatpmp sam_u->nat_u.tim_u.data = sam_u; @@ -2580,7 +2264,6 @@ u3_ames_io_init(u3_pier* pir_u, // we could afford more, but 500k entries is more than we'll likely use // in the near future. // - sam_u->lax_p = u3h_new_cache(500000); //XX: zif what? //u3_assert( !uv_udp_init(u3L, &sam_u->wax_u) ); @@ -2617,7 +2300,5 @@ u3_ames_io_init(u3_pier* pir_u, u3z(now); } - *imp_u = sam_u->zar_u.pip_w; - return car_u; } diff --git a/pkg/vere/io/ames/lamp.c b/pkg/vere/io/ames/lamp.c index a08aa5d528..f2731dd16f 100644 --- a/pkg/vere/io/ames/lamp.c +++ b/pkg/vere/io/ames/lamp.c @@ -1,17 +1,12 @@ #include "vere.h" -#include "io/ames/lamp.h" +#include "io/lamp.h" -#define NAME tuf_map -#define KEY_TY u3_ship -#define HASH_FN u3_hash_ship -#define CMPR_FN u3_cmpr_ship -#define VAL_TY c3_y** -#define IMPLEMENTATION_MODE -#include "verstable.h" +c3_o +_mesa_is_lane_zero(sockaddr_in lan_u); /* _ames_czar_port(): udp port for galaxy. */ -static c3_s +c3_s _ames_czar_port(c3_y imp_y) { if ( c3n == u3_Host.ops_u.net ) { @@ -30,66 +25,46 @@ _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++ = '.'; +u3_peer* +_mesa_get_peer(void* sam_u, u3_ship her_u); - memset(dns_c, 0, 256 - (dns_c - bas_c)); +u3_peer* +_mesa_gut_peer(void* sam_u, u3_ship her_u); - return 0; -} - -/* _ames_czar_lane: retrieve lane for galaxy if stored. +/* _ames: retrieve lane for galaxy if stored. */ c3_o -_ames_czar_lane(u3_ames* sam_u, c3_y imp_y, sockaddr_in* lan_u) +_ames_lamp_lane(u3_auto* car_u, u3_ship her_u, sockaddr_in* lan_u) { - - lan_u->sin_family = AF_INET; - lan_u->sin_port = _ames_czar_port(imp_y); + c3_c* her_c = u3_ship_to_string(her_u); + c3_free(her_c); + u3_peer* per_u = _mesa_get_peer(car_u, her_u); + if (NULL == per_u) return c3n; + + if (c3n == per_u->lam_o) { + u3l_log("fatal: peer is not lamp"); + u3_king_bail(); + } if ( c3n == u3_Host.ops_u.net ) { lan_u->sin_addr.s_addr = NLOCALHOST; } else { - lan_u->sin_addr.s_addr = - sam_u->zar_u.pip_w[imp_y]; - - if ( !lan_u->sin_addr.s_addr ) { + *lan_u = per_u->dan_u; + if ( c3y == _mesa_is_lane_zero(*lan_u) ) { if ( u3C.wag_w & u3o_verbose ) { - u3l_log("ames: czar not resolved"); + u3l_log("ames: lamp not resolved"); } return c3n; } else if ( _CZAR_GONE == lan_u->sin_addr.s_addr ) { // 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; + + if ( c3n == per_u->log_o ) { + // XX + //u3l_log("ames: lamp at %s: not found (b)", *dns_c); + per_u->log_o = c3y; } return c3n; @@ -99,62 +74,58 @@ _ames_czar_lane(u3_ames* sam_u, c3_y imp_y, sockaddr_in* lan_u) return c3y; } -typedef struct _czar_resv { +typedef struct _lamp_resv { uv_getaddrinfo_t adr_u; - u3_ames* sam_u; - c3_y imp_y; -} _czar_resv; + u3_ames_lamp_state* lam_u; + u3_peer* per_u; + c3_c** dns_c; +} _lamp_resv; -/* _ames_czar_gone(): galaxy address resolution failed. +/* _ames_lamp_gone(): galaxy address resolution failed. */ static void -_ames_czar_gone(u3_ames* sam_u, c3_y imp_y) +_ames_lamp_gone(u3_ames_lamp_state* lam_u, u3_peer* per_u) { - c3_w old_w = sam_u->zar_u.pip_w[imp_y]; + c3_w old_w = per_u->dan_u.sin_addr.s_addr; if ( !old_w ) { - sam_u->zar_u.pip_w[imp_y] = _CZAR_GONE; + per_u->dan_u.sin_addr.s_addr = _CZAR_GONE; } } -/* _ames_czar_here(): galaxy address resolution succeeded. +/* _ames_lamp_here(): galaxy address resolution succeeded. */ static void -_ames_czar_here(u3_ames* sam_u, c3_y imp_y, sockaddr_in lan_u) +_ames_lamp_here(u3_ames_lamp_state* lam_u, u3_peer* per_u, sockaddr_in lan_u, c3_c* dns_c) { - c3_w old_w = sam_u->zar_u.pip_w[imp_y]; + c3_w old_w = per_u->dan_u.sin_addr.s_addr; if ( lan_u.sin_addr.s_addr != old_w ) { - c3_c dns_c[256]; c3_w nip_w = lan_u.sin_addr.s_addr; c3_c nip_c[INET_ADDRSTRLEN]; inet_ntop(AF_INET, &nip_w, nip_c, INET_ADDRSTRLEN); - u3_assert ( !_ames_etch_czar(dns_c, sam_u->zar_u.dom_c, imp_y) ); - u3l_log("ames: czar %s ip .%s", dns_c, nip_c); + u3l_log("ames: lamp %s ip .%s", dns_c, nip_c); } - sam_u->zar_u.pip_w[imp_y] = lan_u.sin_addr.s_addr; - - { - 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; - } + per_u->dan_u.sin_addr.s_addr = lan_u.sin_addr.s_addr; + per_u->log_o = c3n; } -/* _ames_czar_cb(): galaxy address resolution callback. +static void +_ames_lamp(u3_ames_lamp_state* lam_u, u3_peer* per_u, c3_c** dns_c); + +/* _ames_lamp_cb(): galaxy address resolution callback. */ static void -_ames_czar_cb(uv_getaddrinfo_t* adr_u, +_ames_lamp_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; + _lamp_resv* res_u = (_lamp_resv*)adr_u; + u3_ames_lamp_state* lam_u = res_u->lam_u; + u3_peer* per_u = res_u->per_u; while ( rai_u && (AF_INET != rai_u->ai_family) ) { rai_u = rai_u->ai_next; @@ -162,121 +133,160 @@ _ames_czar_cb(uv_getaddrinfo_t* adr_u, if ( rai_u && rai_u->ai_addr ) { struct sockaddr_in* lan_u = (void*)rai_u->ai_addr; - _ames_czar_here(sam_u, imp_y, *lan_u); + _ames_lamp_here(lam_u, per_u, *lan_u, *res_u->dns_c); + lam_u->pen_s--; + uv_freeaddrinfo(aif_u); + c3_free(res_u); } else { if ( !sas_i ) { // XX unpossible - u3l_log("ames: czar: strange failure, no error"); + u3l_log("ames: lamp: strange failure, no error"); + _ames_lamp_gone(lam_u, per_u); + lam_u->pen_s--; + uv_freeaddrinfo(aif_u); + c3_free(res_u); } - else if ( u3C.wag_w & u3o_verbose ) { - u3l_log("ames: czar fail: %s", uv_strerror(sas_i)); + else { + c3_c** dns_c = res_u->dns_c; + c3_o nex_o = (NULL != *(dns_c + 1)) ? c3y : c3n; + if ( u3C.wag_w & u3o_verbose ) { + u3l_log("ames: lamp fail: %s", uv_strerror(sas_i)); + //if (nex_o) u3l_log("trying next"); + } + if (c3y == nex_o) + _ames_lamp(lam_u, per_u, dns_c + 1); + else { + // XX: wait should we do this here? + _ames_lamp_gone(lam_u, per_u); + lam_u->pen_s--; + uv_freeaddrinfo(aif_u); + c3_free(res_u); + } } - - _ames_czar_gone(sam_u, imp_y); } - - sam_u->zar_u.pen_s--; - - uv_freeaddrinfo(aif_u); - c3_free(res_u); } -/* _ames_czar(): single galaxy address resolution. +/* _ames_lamp(): single galaxy address resolution. */ static void -_ames_czar(u3_ames* sam_u, const c3_c* dom_c, c3_y imp_y) +_ames_lamp(u3_ames_lamp_state* lam_u, u3_peer* per_u, c3_c** dns_c) { struct addrinfo hin_u = { .ai_family = AF_INET }; uv_getaddrinfo_t* adr_u; - _czar_resv* res_u; - c3_c dns_c[256]; + _lamp_resv* res_u; 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; + res_u->lam_u = lam_u; + res_u->per_u = per_u; + res_u->dns_c = dns_c; adr_u = &(res_u->adr_u); - sas_i = uv_getaddrinfo(u3L, adr_u, _ames_czar_cb, dns_c, 0, &hin_u); + sas_i = uv_getaddrinfo(u3L, adr_u, _ames_lamp_cb, *dns_c, 0, &hin_u); if ( sas_i ) { - _ames_czar_cb(adr_u, sas_i, NULL); + _ames_lamp_cb(adr_u, sas_i, NULL); } } -/* _ames_czar_all(): galaxy address resolution. -*/ static void -_ames_czar_all(uv_timer_t* tim_u) +_ames_etch_czars(u3_ames_lamp_state* lam_u) { + for (c3_w i = 0; i < 256; i++) { + u3_ship who_u = u3_ship_of_noun(i); + u3_peer* per_u = _mesa_gut_peer(lam_u->car_u, who_u); + per_u->lam_o = c3y; + per_u->dan_u.sin_family = AF_INET; + per_u->dan_u.sin_port = _ames_czar_port(i); + c3_c* who_c = u3_ship_to_string(who_u); + c3_w len_w = u3_mpef_turfs(NULL, 0, who_c + 1, lam_u->dns_c); + if (per_u->dns_c) c3_free(per_u->dns_c); + c3_c** dns_c = per_u->dns_c = c3_malloc(len_w); + u3_mpef_turfs((c3_c*)dns_c, 0, who_c + 1, lam_u->dns_c); + c3_free(who_c); + } +} + +/* _ames_lamp_all(): galaxy address resolution. +*/ +void +_ames_lamp_all(uv_timer_t* tim_u) { - u3_ames* sam_u = tim_u->data; + u3_ames_lamp_state* lam_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); + if ( lam_u->pen_s ) { + uv_timer_start(&lam_u->tim_u, _ames_lamp_all, 30*1000, 0); return; } - sam_u->zar_u.pen_s = 256; - - 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); + lam_u->pen_s = 0; + //c3_w i = 0; + for( + per_map_itr itr = + per_map_first( lam_u->per_u ); + !per_map_is_end( itr ); + itr = per_map_next( itr ) ) { + u3_peer* per_u = itr.data->val; + //c3_c* who_c = u3_ship_to_string(per_u->her_u); + //u3l_log("czar %u %s", i, who_c); + //c3_free(who_c); + //i++; + if (per_u->dns_c) { + lam_u->pen_s++; + _ames_lamp(lam_u, per_u, per_u->dns_c); + } } - uv_timer_start(&sam_u->zar_u.tim_u, _ames_czar_all, 300*1000, 0); + uv_timer_start(&lam_u->tim_u, _ames_lamp_all, 300*1000, 0); +} + +static c3_o +_ames_cmp_turfs(c3_c** a, c3_c** b) { + while (a != NULL || b != NULL) { + if (a == NULL) { + if (b == NULL) return c3y; + return c3n; + } + if (b == NULL) { + return c3n; + } + if (0 != strcmp(*a, *b)) return 0; + a++; + b++; + } + return c3y; } /* _ames_ef_turf(): initialize ames I/O on domain(s). */ -static void -_ames_ef_turf(u3_ames* sam_u, u3_noun tuf) +void +_ames_ef_turf(u3_ames_lamp_state* lam_u, u3_noun tuf) { - if ( u3_nul != 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)); - - 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 (len=%u)", len_w); - u3m_p("hot", hot); - u3_pier_bail(u3_king_stub()); - } - - 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)) ) { - 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)); + if ( u3_nul != tuf) { + c3_w len_w = u3_mcut_hosts(NULL, 0, u3k(tuf)); + if (len_w == 0) { + // todo: clear? + return; } - - // 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)))); - } + c3_c** dns_c = c3_malloc(len_w); + u3_mcut_hosts((c3_c*)dns_c, 0, u3k(tuf)); + _ames_cmp_turfs(dns_c, lam_u->dns_c); + if ( c3n == _ames_cmp_turfs(dns_c, lam_u->dns_c) ) { + c3_free(lam_u->dns_c); + lam_u->dns_c = dns_c; + lam_u->dom_o = c3y; + _ames_etch_czars(lam_u); + _ames_lamp_all(&(lam_u->tim_u)); } - - u3z(tuf); } - else if ( (c3n == sam_u->pir_u->fak_o) && (c3n == sam_u->zar_u.dom_o) ) { + else if ( (c3n == lam_u->car_u->pir_u->fak_o) && (c3n == lam_u->dom_o) ) { u3l_log("ames: turf: no domains"); } - // XX is this ever necessary? - // - if ( c3n == sam_u->car_u.liv_o ) { - _ames_io_start(sam_u); - } + //// XX is this ever necessary? + //// + //if ( c3n == lam_u->car_u.liv_o ) { + // _ames_io_start(lam_u); + //} } - diff --git a/pkg/vere/io/ames/stun.c b/pkg/vere/io/ames/stun.c index d55fbc4ada..e3cfecfe14 100644 --- a/pkg/vere/io/ames/stun.c +++ b/pkg/vere/io/ames/stun.c @@ -264,7 +264,7 @@ _stun_on_response(u3_stun_client* sun_u, c3_y* buf_y, c3_w buf_len) { // lane changed u3_noun wir = u3nc(c3__ames, u3_nul); - u3_noun cad = u3nq(c3__stun, c3__once, sun_u->dad_y, + u3_noun cad = u3nq(c3__stun, c3__once, u3_ship_to_noun(sun_u->dad_u), u3nc(c3n, u3_ames_encode_lane(lan_u))); u3_auto_plan(sun_u->car_u, u3_ovum_init(0, c3__ames, wir, cad)); @@ -272,7 +272,7 @@ _stun_on_response(u3_stun_client* sun_u, c3_y* buf_y, c3_w buf_len) else if ( c3n == sun_u->wok_o ) { // stop %ping app u3_noun wir = u3nc(c3__ames, u3_nul); - u3_noun cad = u3nq(c3__stun, c3__stop, sun_u->dad_y, + u3_noun cad = u3nq(c3__stun, c3__stop, u3_ship_to_noun(sun_u->dad_u), u3nc(c3n, u3_ames_encode_lane(lan_u))); u3_auto_plan(sun_u->car_u, u3_ovum_init(0, c3__ames, wir, cad)); @@ -337,7 +337,7 @@ _stun_on_lost(u3_stun_client* sun_u) // if ( c3y == sun_u->wok_o ) { u3_noun wir = u3nc(c3__ames, u3_nul); - u3_noun cad = u3nq(c3__stun, c3__fail, sun_u->dad_y, + u3_noun cad = u3nq(c3__stun, c3__fail, u3_ship_to_noun(sun_u->dad_u), u3nc(c3n, u3_ames_encode_lane(sun_u->sef_u))); u3_auto_plan(sun_u->car_u, u3_ovum_init(0, c3__ames, wir, cad)); @@ -359,7 +359,7 @@ _stun_time_gap(struct timeval sar_tv) return u3_time_gap_ms(den, now); } -c3_o _ames_czar_lane(u3_auto*, c3_y, sockaddr_in*); +c3_o _ames_lamp_lane(u3_auto*, u3_ship, sockaddr_in*); /* _stun_timer_cb(): advance stun state machine. */ @@ -377,9 +377,9 @@ _stun_timer_cb(uv_timer_t* tim_u) case STUN_KEEPALIVE: { sockaddr_in* lan_u = &(sun_u->lan_u); - c3_y imp_y = sun_u->dad_y; + u3_ship lam_u = sun_u->dad_u; - if ( c3n == _ames_czar_lane(sun_u->car_u, imp_y, lan_u) ) { + if ( c3n == _ames_lamp_lane(sun_u->car_u, lam_u, lan_u) ) { uv_timer_start(&sun_u->tim_u, _stun_timer_cb, 25*1000, 0); } else { diff --git a/pkg/vere/io/ames/stun.h b/pkg/vere/io/ames/stun.h index bb0b8df692..6ede01f798 100644 --- a/pkg/vere/io/ames/stun.h +++ b/pkg/vere/io/ames/stun.h @@ -15,7 +15,7 @@ uv_udp_t* wax_u; u3_stun_state sat_y; // formal state c3_y tid_y[12]; // last transaction id - c3_y dad_y; // sponsoring galaxy + u3_ship dad_u; // sponsoring galaxy sockaddr_in lan_u; // sponsoring galaxy IP and port uv_timer_t tim_u; // keepalive timer handle struct timeval sar_u; // date we started trying to send diff --git a/pkg/vere/io/lamp.h b/pkg/vere/io/lamp.h new file mode 100644 index 0000000000..424f08defe --- /dev/null +++ b/pkg/vere/io/lamp.h @@ -0,0 +1,15 @@ +#include "mesa/mesa.h" + +#define _CZAR_GONE (htonl(UINT32_MAX)) +#define NLOCALHOST (htonl(0x7f000001)) + + +typedef struct _u3_ames_lamp_state { // + u3_auto* car_u; + per_map* per_u; + c3_c** dns_c; // domain + c3_o dom_o; // have domain + uv_timer_t tim_u; // resolve timer + c3_s pen_s; // pending +} u3_ames_lamp_state; // + diff --git a/pkg/vere/io/mesa.c b/pkg/vere/io/mesa.c index e254c97175..60fccac670 100644 --- a/pkg/vere/io/mesa.c +++ b/pkg/vere/io/mesa.c @@ -7,6 +7,7 @@ #include "ur/ur.h" #include "ship.h" #include "io/ames/stun.h" +#include "io/lamp.h" #include "mesa/mesa.h" #include "mesa/bitset.h" #include @@ -136,8 +137,6 @@ typedef struct _u3_pit_entry { arena are_u; } u3_pit_entry; - - static void u3_free_pit( u3_pit_entry* pit_u ) { arena_free(&pit_u->are_u); @@ -273,8 +272,8 @@ typedef struct _u3_mesa { arena par_u; // permanent arena uv_timer_t tim_u; // pit clear timer u3_stun_client sun_u; // stun client state + u3_ames_lamp_state lam_u; // stun client state u3_auto* ame_u; // ames pointer - c3_w* imp_u; } u3_mesa; STATIC_ASSERT( @@ -340,6 +339,10 @@ typedef struct _u3_mesa_cb_data { sockaddr_in lan_u; } u3_mesa_cb_data; +static void _meet_peer(u3_mesa* sam_u, u3_peer* per_u); +static void _init_peer(u3_mesa* sam_u, u3_peer* per_u, u3_ship her_u); +static void _mesa_ef_saxo(u3_mesa* sam_u, u3_noun zad); + static c3_d get_millis() { struct timeval tp; @@ -386,7 +389,9 @@ static void _log_peer(u3_peer* per_u) return; } u3l_log("dir"); - u3l_log("galaxy: %s", u3r_string(u3dc("scot", 'p', per_u->imp_y))); + c3_c* lam_c = u3_ship_to_string(per_u->lam_u); + u3l_log("galaxy: %s", lam_c); + c3_free(lam_c); } static void @@ -419,7 +424,7 @@ _mesa_lop(c3_d fra_d) return fra_d & ~((1 << u3_Host.ops_u.jum_y) - 1); } -static c3_d +c3_d _get_now_micros() { struct timeval tim_u; @@ -438,17 +443,21 @@ _clamp_rto(c3_d rto_d) { return c3_min(c3_max(rto_d, 200 * 1000), 25000 * 1000); // ~s25 max backoff } -static inline c3_o +//static inline c3_o +c3_o _mesa_is_lane_zero(sockaddr_in lan_u) { - return __((lan_u.sin_addr.s_addr == 0) && (lan_u.sin_port == 0)); + return __( ( (lan_u.sin_addr.s_addr == 0) || + (lan_u.sin_addr.s_addr == _CZAR_GONE) ) + && (lan_u.sin_port == 0) ); } -static c3_o +c3_o _mesa_is_direct_mode(u3_peer* per_u) { c3_d now_d = _get_now_micros(); - return __(per_u->dir_u.her_d + DIRECT_ROUTE_TIMEOUT_MICROS > now_d); + return __( (c3y == per_u->lam_o) || + (per_u->dir_u.her_d + DIRECT_ROUTE_TIMEOUT_MICROS > now_d) ); } /* _mesa_encode_path(): produce buf_y as a parsed path @@ -573,13 +582,30 @@ u3_mesa_encode_lane(sockaddr_in lan_u) { return u3nt(c3__if, u3i_word(pip_w), por_s); } -static u3_peer* -_mesa_get_peer(u3_mesa* sam_u, u3_ship her) +u3_peer* +_mesa_get_peer(u3_mesa* sam_u, u3_ship her_u) +{ + per_map_itr itr_u = vt_get(&sam_u->per_u, her_u); + if ( vt_is_end(itr_u) ) { + return NULL; + } + return itr_u.data->val; +} + +u3_peer* +_mesa_gut_peer(u3_mesa* sam_u, u3_ship her_u) { - per_map_itr itr_u = vt_get(&sam_u->per_u, her); + per_map_itr itr_u = vt_get_or_insert(&sam_u->per_u, her_u, NULL); if ( vt_is_end(itr_u) ) { + fprintf(stderr, "mesa: cannot allocate memory for peer, dying"); return NULL; } + + if (NULL == itr_u.data->val) { + u3_peer* per_u = itr_u.data->val + = new(&sam_u->par_u, u3_peer, 1); + _init_peer(sam_u, per_u, her_u); + } return itr_u.data->val; } @@ -651,23 +677,10 @@ _mesa_put_request(u3_mesa* sam_u, u3_mesa_name* nam_u, u3_pend_req* req_u) { } } -/* _ames_czar_port(): udp port for galaxy. -*/ -c3_s -_ames_czar_port(c3_y imp_y); - -static sockaddr_in +sockaddr_in _mesa_get_direct_lane(u3_mesa* sam_u, u3_ship her_u) { sockaddr_in adr_u = {0}; - adr_u.sin_family = AF_INET; - - if ( c3__czar == u3_ship_rank(her_u) ) { - c3_s por_s = _ames_czar_port(her_u.hed_d); - adr_u.sin_addr.s_addr = htonl(sam_u->imp_u[her_u.hed_d]); - adr_u.sin_port = htons(por_s); - return adr_u; - } per_map_itr itr_u = vt_get(&sam_u->per_u, her_u); if ( vt_is_end(itr_u) ) { @@ -676,7 +689,6 @@ _mesa_get_direct_lane(u3_mesa* sam_u, u3_ship her_u) adr_u = itr_u.data->val->dan_u; - return adr_u; } @@ -686,17 +698,6 @@ _mesa_lanes_equal(sockaddr_in lan_u, sockaddr_in lon_u) return __((lan_u.sin_addr.s_addr == lon_u.sin_addr.s_addr) && (lan_u.sin_port == lon_u.sin_port)); } -static sockaddr_in -_mesa_get_czar_lane(u3_mesa* sam_u, c3_y imp_y) -{ - c3_s por_s = _ames_czar_port(imp_y); - sockaddr_in adr_u = {0}; - adr_u.sin_family = AF_INET; - adr_u.sin_addr.s_addr = htonl(sam_u->imp_u[imp_y]); - adr_u.sin_port = htons(por_s); - return adr_u; -} - /* _mesa_get_lane(): get lane */ static u3_gage* @@ -1050,9 +1051,8 @@ _mesa_send_modal(u3_peer* per_u, uv_buf_t buf_u, u3_pit_addr* las_u) c3_y* sen_y = c3_calloc(len_w); memcpy(sen_y, buf_u.base, len_w); - u3_ship gal_u = {0}; - gal_u.hed_d = per_u->imp_y; - c3_o our_o = u3_ships_equal(gal_u, sam_u->pir_u->who_u); + u3_ship lam_u = per_u->lam_u; + c3_o our_o = u3_ships_equal(lam_u, sam_u->pir_u->who_u); if ( ( c3y == _mesa_is_direct_mode(per_u) ) || // if we are the sponsor of the ship, don't send to ourselves @@ -1065,13 +1065,13 @@ _mesa_send_modal(u3_peer* per_u, uv_buf_t buf_u, u3_pit_addr* las_u) _mesa_send_bufs(sam_u, per_u, sen_y, len_w, las_u); } else { #ifdef MESA_DEBUG - /* c3_c* gal_c = u3_ship_to_string(gal_u); */ + /* c3_c* gal_c = u3_ship_to_string(lam_u); */ // u3l_log("mesa: sending to %s", gal_c); /* c3_free(gal_c); */ #endif // - sockaddr_in imp_u = _mesa_get_czar_lane(sam_u, per_u->imp_y); - _mesa_send_buf(sam_u, imp_u, sen_y, len_w); + sockaddr_in ind_u = _mesa_get_direct_lane(sam_u, per_u->lam_u); + _mesa_send_buf(sam_u, ind_u, sen_y, len_w); per_u->ind_u.sen_d = now_d; if ( c3n == _mesa_is_lane_zero(per_u->dan_u) ) { @@ -1333,7 +1333,8 @@ _mesa_req_pact_done(u3_pend_req* req_u, u3_lane_state* sat_u; if ( 0 == hop_y && (c3n == _mesa_lanes_equal(lan_u, req_u->per_u->dan_u)) ) { - req_u->per_u->dan_u = lan_u; + if ( c3n == req_u->per_u->lam_o ) + req_u->per_u->dan_u = lan_u; sat_u = &req_u->per_u->dir_u; _init_lane_state(sat_u); } @@ -1350,19 +1351,24 @@ _mesa_req_pact_done(u3_pend_req* req_u, _update_resend_timer(req_u); } +/* _ames_czar_port(): udp port for galaxy. +*/ +c3_s +_ames_czar_port(c3_y imp_y); + static sockaddr_in -_realise_lane(u3_mesa* sam_u, u3_noun lan) { +_mesa_realise_lane(u3_mesa* sam_u, u3_noun lan) { sockaddr_in lan_u = {0}; lan_u.sin_family = AF_INET; if ( c3y == u3a_is_cat(lan) ) { + lan_u = _mesa_get_direct_lane(sam_u, u3_ship_of_noun(lan)); // u3_assert( lan < 256 ); if ( (c3n == u3_Host.ops_u.net) ) { lan_u.sin_addr.s_addr = htonl(0x7f000001); - lan_u.sin_port = htons(_ames_czar_port(lan)); - } else { - lan_u.sin_addr.s_addr = htonl(sam_u->imp_u[lan]); - lan_u.sin_port = htons(_ames_czar_port(lan)); + } else if (256 <= lan) { + u3l_log("local comet sponsors no!"); + u3_king_bail(); } } else { u3_noun tag, pip, por; @@ -1515,7 +1521,7 @@ _mesa_lanes_to_addrs(u3_mesa* sam_u, u3_noun las, arena* are_u) { while ( t != u3_nul ) { u3x_cell(t, &lan, &t); u3_pit_addr* new_u = new(are_u, u3_pit_addr, 1); - new_u->sdr_u = _realise_lane(sam_u, u3k(lan)); + new_u->sdr_u = _mesa_realise_lane(sam_u, u3k(lan)); new_u->nex_p = adr_u; adr_u = new_u; } @@ -1658,9 +1664,11 @@ _ames_kick_newt(void* sam_u, u3_noun tag, u3_noun dat); u3_atom u3_ames_encode_lane(sockaddr_in lan); -static void _meet_peer(u3_mesa* sam_u, u3_peer* per_u, u3_ship her_u); -static void _init_peer(u3_mesa* sam_u, u3_peer* per_u); -static void _mesa_ef_saxo(u3_mesa* sam_u, u3_noun zad); +void +_ames_ef_turf(u3_ames_lamp_state* lam_u, u3_noun tuf); +void +_ames_lane_into_cache(u3_auto* sam_u, u3_noun who, u3_noun las); + static c3_o _mesa_kick(u3_mesa* sam_u, u3_noun tag, u3_noun dat) { @@ -1669,6 +1677,10 @@ static c3_o _mesa_kick(u3_mesa* sam_u, u3_noun tag, u3_noun dat) default: { ret_o = c3n; } break; + case c3__turf: { + _ames_ef_turf(&sam_u->lam_u, u3k(dat)); + ret_o = c3y; + } break; case c3__push: { u3_noun las, pac; if ( c3n == u3r_cell(dat, &las, &pac) ) { @@ -1680,13 +1692,7 @@ static c3_o _mesa_kick(u3_mesa* sam_u, u3_noun tag, u3_noun dat) ret_o = c3y; } } break; - case c3__saxo: { - _mesa_ef_saxo(sam_u, u3k(dat)); - ret_o = c3y; - break; - } - case c3__send: - case c3__turf: { + case c3__send: { #ifdef MESA_DEBUG c3_c* tag_c = u3r_string(tag); /* u3l_log("mesa: send old %s", tag_c); */ @@ -1694,37 +1700,16 @@ static c3_o _mesa_kick(u3_mesa* sam_u, u3_noun tag, u3_noun dat) #endif ret_o = _ames_kick_newt(sam_u->ame_u, u3k(tag), u3k(dat)); } break; + case c3__saxo: { + _mesa_ef_saxo(sam_u, u3k(dat)); + ret_o = c3y; + break; + } case c3__nail: { - u3_ship who_u = u3_ship_of_noun(u3h(dat)); - u3_peer* per_u = _mesa_get_peer(sam_u, who_u); - - if ( NULL == per_u ) { - per_u = new(&sam_u->par_u, u3_peer, 1); - _init_peer(sam_u, per_u); - } - - // XX the format of the lane %nail gives is (list (each @p address)) - // - u3_noun las = u3do("tail", u3k(dat)); - - if ( las == u3_nul ) { - per_u->dan_u = (sockaddr_in){0}; - } - else { - u3_noun lan = u3h(las); - // we either have a direct route, and a galaxy, or just one lane - if ( c3n == u3h(lan) ) { - sockaddr_in lan_u = u3_mesa_decode_lane(u3k(u3t(lan))); - per_u->dan_u = lan_u; - } else { - // delete direct lane if galaxy - per_u->dan_u = (sockaddr_in){0}; - } - } - - _meet_peer(sam_u, per_u, who_u); - - ret_o = _ames_kick_newt(sam_u->ame_u, u3k(tag), u3k(dat)); + u3_noun who = u3k(u3h(dat)); + u3_noun las = u3k(u3t(dat)); + _ames_lane_into_cache(sam_u->ame_u, who, las); + ret_o = c3y; } break; } @@ -1804,13 +1789,14 @@ _init_lane_state(u3_lane_state* sat_u) } static void -_init_peer(u3_mesa* sam_u, u3_peer* per_u) +_init_peer(u3_mesa* sam_u, u3_peer* per_u, u3_ship her_u) { + memset(per_u, 0, sizeof(*per_u)); per_u->sam_u = sam_u; per_u->ful_o = c3n; per_u->dan_u = (sockaddr_in){0}; _init_lane_state(&per_u->dir_u); - per_u->imp_y = 0; + per_u->her_u = (u3_ship){0}; _init_lane_state(&per_u->ind_u); } @@ -2120,11 +2106,11 @@ _saxo_cb(void* vod_p, u3_noun nun) _mesa_put_peer(per_u->sam_u, her_u, per_u); } u3_mesa* sam_u = per_u->sam_u; - u3_noun gal = u3do("rear", u3k(sax)); - u3_assert( c3y == u3a_is_cat(gal) && gal < 256 ); + u3_noun lam = u3do("rear", u3k(sax)); + //u3_assert( c3y == u3a_is_cat(gal) && gal < 256 ); // both atoms guaranteed to be cats, bc we don't call unless forwarding per_u->ful_o = c3y; - per_u->imp_y = gal; + per_u->lam_u = u3_ship_of_noun(lam); } u3z(nun); @@ -2145,23 +2131,21 @@ _forward_lanes_cb(void* vod_p, u3_noun nun) per_u = new_u; } u3_noun gal = u3h(las); - u3_assert( c3y == u3a_is_cat(gal) && gal < 256 ); + //u3_assert( c3y == u3a_is_cat(gal) && gal < 256 ); // both atoms guaranteed to be cats, bc we don't call unless forwarding per_u->ful_o = c3y; - per_u->imp_y = gal; - u3_noun sal = u3k(u3t(las)); - u3_noun lan; - while ( sal != u3_nul ) { - u3x_cell(sal, &lan, &sal); - - if ( c3n == u3a_is_cat(lan) ) { - // there should be only one lane that is not a direct atom - // - sockaddr_in lan_u = _realise_lane(sam_u, u3k(lan)); - per_u->dan_u = lan_u; + per_u->lam_u = u3_ship_of_noun(gal); + u3_noun sal, tal; + + if ( c3n == per_u->lam_o ) { + if ( (c3y == u3r_cell(u3t(las), &sal, &tal)) && + (c3y == u3du(sal)) ) { + per_u->dan_u = _mesa_realise_lane(sam_u, u3k(sal)); + } else { + per_u->dan_u = (sockaddr_in){0}; } } - u3z(sal); + _mesa_put_peer(per_u->sam_u, per_u->her_u, per_u); } @@ -2170,13 +2154,11 @@ _forward_lanes_cb(void* vod_p, u3_noun nun) } static void -_meet_peer(u3_mesa* sam_u, u3_peer* per_u, u3_ship her_u) +_meet_peer(u3_mesa* sam_u, u3_peer* per_u) { - u3_noun her = u3_ship_to_noun(her_u); + u3_noun her = u3_ship_to_noun(per_u->her_u); u3_noun gan = u3nc(u3_nul, u3_nul); - per_u->her_u = her_u; - u3_noun pax = u3nc(u3dc("scot", c3__p, her), u3_nul); u3_pier_peek_last(sam_u->pir_u, gan, c3__j, c3__saxo, pax, per_u, _saxo_cb); @@ -2199,7 +2181,8 @@ static void _hear_peer(u3_mesa* sam_u, u3_peer* per_u, sockaddr_in lan_u, c3_o dir_o) { if ( c3y == dir_o ) { - per_u->dan_u = lan_u; + if ( c3n == per_u->lam_o ) + per_u->dan_u = lan_u; per_u->dir_u.her_d = _get_now_micros(); } else { per_u->ind_u.her_d = _get_now_micros(); @@ -2451,13 +2434,14 @@ _mesa_forward_request(u3_mesa* sam_u, u3_mesa_pict* pic_u, sockaddr_in lan_u) c3_free(mes); #endif per_u = new(&sam_u->par_u, u3_peer, 1); - _init_peer(sam_u, per_u); + _init_peer(sam_u, per_u, pac_u->pek_u.nam_u.her_u); per_u->her_u = pac_u->pek_u.nam_u.her_u; _get_peer_lanes(sam_u, per_u); // forward-lanes return; } - if ( c3y == sam_u->for_o && sam_u->pir_u->who_u.hed_d == per_u->imp_y ) { + if ( c3y == sam_u->for_o && + c3y == u3_ships_equal(sam_u->pir_u->who_u, per_u->lam_u) ) { // if ( c3y == sam_u->for_o ) { sockaddr_in lin_u = _mesa_get_direct_lane(sam_u, pac_u->pek_u.nam_u.her_u); if ( _mesa_is_lane_zero(lin_u) == c3y) { @@ -2502,8 +2486,8 @@ _mesa_hear_page(u3_mesa_pict* pic_u, sockaddr_in lan_u) if ( NULL == per_u ) { new_o = c3y; per_u = new(&sam_u->par_u, u3_peer, 1); - _init_peer(sam_u, per_u); - _meet_peer(sam_u, per_u, nam_u->her_u); + _init_peer(sam_u, per_u, nam_u->her_u); + _meet_peer(sam_u, per_u); } c3_o dir_o = __(pac_u->hed_u.hop_y == 0); @@ -2750,8 +2734,8 @@ _mesa_hear_poke(u3_mesa_pict* pic_u, sockaddr_in lan_u) if ( NULL == per_u ) { new_o = c3y; per_u = new(&sam_u->par_u, u3_peer, 1); - _init_peer(sam_u, per_u); - _meet_peer(sam_u, per_u, pac_u->pok_u.pay_u.her_u); + _init_peer(sam_u, per_u, pac_u->pok_u.pay_u.her_u); + _meet_peer(sam_u, per_u); } c3_o dir_o = __(pac_u->hed_u.hop_y == 0); @@ -2823,11 +2807,12 @@ _mesa_hear(u3_mesa* sam_u, // check ames first, assume that STUN could maybe (not likely) overlap with ames // for next protocol version, have an urbit cookie // - if ( c3y == u3_stun_hear(&sam_u->sun_u, lan_u, len_w, hun_y) ) - return; c3_y* han_y = c3_malloc(len_w); memcpy(han_y, hun_y, len_w); + if ( c3y == u3_stun_hear(&sam_u->sun_u, lan_u, len_w, han_y) ) + return; + _ames_hear(sam_u->ame_u, lan_u, len_w, han_y); return; } @@ -3006,14 +2991,15 @@ _mesa_ef_saxo(u3_mesa* sam_u, u3_noun zad) return; } - u3_ship dad = u3_ship_of_noun(u3h(daz)); + u3_ship dad_u = u3_ship_of_noun(u3h(daz)); // if we are a galaxy, don't STUN // - if ( (c3__czar == u3_ship_rank(dad)) && - (c3__czar != u3_ship_rank(sam_u->pir_u->who_u)) ) { - sam_u->sun_u.dad_y = (c3_y)dad.hed_d; + if ( c3n == u3_ships_equal(dad_u, sam_u->pir_u->who_u) ) { + sam_u->sun_u.dad_u = dad_u; u3_stun_start(&sam_u->sun_u, 0); + } else { + sam_u->for_o = c3y; } u3z(zad); u3z(daz); @@ -3060,15 +3046,6 @@ u3_mesa_io_init(u3_pier* pir_u) } sam_u->for_o = c3n; - { - u3_noun her = u3_ship_to_noun(pir_u->who_u); - if ( c3y == u3a_is_cat(her) && her < 256 ) { - u3l_log("mesa: forwarding enabled"); - sam_u->for_o = c3y; - } - u3z(her); - } - u3_auto* car_u = &sam_u->car_u; memset(car_u, 0, sizeof(*car_u)); @@ -3086,10 +3063,18 @@ u3_mesa_io_init(u3_pier* pir_u) sam_u->sun_u.wok_o = c3n; uv_timer_init(u3L, &sam_u->sun_u.tim_u); sam_u->sun_u.tim_u.data = &sam_u->sun_u; + u3l_log("mesa start %p", &sam_u->sun_u); + + sam_u->lam_u.car_u = car_u; + sam_u->lam_u.per_u = &sam_u->per_u; + sam_u->lam_u.dom_o = c3n; + + uv_timer_init(u3L, &sam_u->lam_u.tim_u); + sam_u->lam_u.tim_u.data = &sam_u->lam_u; sam_u->ame_u = u3_ames_io_init(pir_u, - &sam_u->wax_u, car_u, &sam_u->imp_u); + &sam_u->wax_u, car_u); u3_auto_link(car_u, pir_u, sam_u->ame_u); /*{ diff --git a/pkg/vere/io/mesa/mesa.h b/pkg/vere/io/mesa/mesa.h index 4af05e640f..1747edc3d1 100644 --- a/pkg/vere/io/mesa/mesa.h +++ b/pkg/vere/io/mesa/mesa.h @@ -188,9 +188,11 @@ typedef struct _u3_peer { c3_o ful_o; // has this been initialized? sockaddr_in dan_u; // direct lane (nullable) u3_lane_state dir_u; // direct lane state - c3_o fef_o; - c3_y imp_y; // galaxy @p + c3_o lam_o; + c3_o log_o; // dns log + u3_ship lam_u; // galaxy @p u3_lane_state ind_u; // indirect lane state + c3_c** dns_c; } u3_peer; #define NAME per_map @@ -201,5 +203,4 @@ typedef struct _u3_peer { #define HEADER_MODE #include "verstable.h" - #endif diff --git a/pkg/vere/vere.h b/pkg/vere/vere.h index 74fa4a3f7c..5a409083f4 100644 --- a/pkg/vere/vere.h +++ b/pkg/vere/vere.h @@ -868,6 +868,16 @@ c3_w u3_mcut_host(c3_c* buf_c, c3_w len_w, u3_noun hot); + /* u3_mcut_hosts(): measure/cut host list. + */ + c3_w + u3_mcut_hosts(c3_c* buf_c, c3_w len_w, u3_noun hot); + + /* u3_mpef_turf(): measure/cut string list, prefixing. + */ + c3_w + u3_mpef_turfs(c3_c* buf_c, c3_w len_w, c3_c* pef_c, c3_c** tuf_c); + /** IO drivers. **/ /* u3_auto_init(): initialize all drivers. @@ -1246,8 +1256,7 @@ u3_auto* u3_ames_io_init(u3_pier* pir_u, uv_udp_t* wax_u, - u3_auto* mes_u, - c3_w** imp_u); + u3_auto* mes_u); /* u3_ames_decode_lane(): destructure lane from noun */ diff --git a/pkg/vere/ward.c b/pkg/vere/ward.c index 9d6c8cbed3..06f6511185 100644 --- a/pkg/vere/ward.c +++ b/pkg/vere/ward.c @@ -252,3 +252,84 @@ u3_mcut_host(c3_c* buf_c, c3_w len_w, u3_noun hot) u3z(hot); return len_w; } + +/* u3_mcut_hosts(): measure/cut host list. +*/ +c3_w +u3_mcut_hosts(c3_c* buf_c, c3_w len_w, u3_noun hot) +{ + c3_c** dst_c = NULL; + hot = u3kdi_tap(u3kdb_gas(u3_nul, hot)); + c3_w hots_w = u3qb_lent(hot); + if (hots_w == 0) return len_w; + if (buf_c) { + dst_c = (void*)(buf_c + len_w); + dst_c[hots_w] = 0; + } + len_w += (hots_w + 1) * sizeof(c3_c*); + + u3_noun oth = hot; + c3_w i = 0; + while ( u3_nul != oth ) { + if (dst_c) dst_c[i] = (void*)(buf_c + len_w); + u3_noun h_oth = u3h(oth); + + len_w = u3_mcut_path(buf_c, len_w, '.', u3kb_flop(u3k(h_oth))); + len_w = u3_mcut_char(buf_c, len_w, '\0'); + oth = u3t(oth); + + i++; + } + u3z(hot); + return len_w; +} + +/* u3_mcut_str(): measure/cut string. +*/ +c3_w +u3_mcut_str(c3_c* buf_c, c3_w len_w, c3_c* str) +{ + c3_w ten_w = strlen(str); + + if ( buf_c ) { + memcpy((c3_y*)(buf_c + len_w), str, ten_w); + } + return (len_w + ten_w); +} + +/* u3_mpef_turf(): measure/cut string list, prefixing. +*/ +c3_w +u3_mpef_turfs(c3_c* buf_c, c3_w len_w, c3_c* pef_c, c3_c** tuf_c) +{ + c3_c** dst_c = NULL; + + c3_w tufs_w = 0; + { + c3_c** uft_c = tuf_c; + while (NULL != *uft_c) { + tufs_w++; + uft_c++; + } + } + + if (buf_c) { + dst_c = (void*)(buf_c + len_w); + dst_c[tufs_w] = 0; + } + len_w += (tufs_w + 1) * sizeof(c3_c*); + + c3_w i = 0; + while ( NULL != tuf_c[i] ) { + if (dst_c) dst_c[i] = (void*)(buf_c + len_w); + len_w = u3_mcut_str(buf_c, len_w, pef_c); + len_w = u3_mcut_char(buf_c, len_w, '.'); + len_w = u3_mcut_str(buf_c, len_w, tuf_c[i]); + len_w = u3_mcut_char(buf_c, len_w, '\0'); + + i++; + } + return len_w; +} + + From a5e562a65de4522b05ade64a46ee1e8dd723ae6e Mon Sep 17 00:00:00 2001 From: ziffee Date: Wed, 2 Apr 2025 14:02:45 -0600 Subject: [PATCH 13/34] ames: fully qualified domains --- pkg/vere/io/ames/lamp.c | 2 -- pkg/vere/ward.c | 2 +- 2 files changed, 1 insertion(+), 3 deletions(-) diff --git a/pkg/vere/io/ames/lamp.c b/pkg/vere/io/ames/lamp.c index f2731dd16f..def3c65e4d 100644 --- a/pkg/vere/io/ames/lamp.c +++ b/pkg/vere/io/ames/lamp.c @@ -36,8 +36,6 @@ _mesa_gut_peer(void* sam_u, u3_ship her_u); c3_o _ames_lamp_lane(u3_auto* car_u, u3_ship her_u, sockaddr_in* lan_u) { - c3_c* her_c = u3_ship_to_string(her_u); - c3_free(her_c); u3_peer* per_u = _mesa_get_peer(car_u, her_u); if (NULL == per_u) return c3n; diff --git a/pkg/vere/ward.c b/pkg/vere/ward.c index 06f6511185..0e8cf5174c 100644 --- a/pkg/vere/ward.c +++ b/pkg/vere/ward.c @@ -275,6 +275,7 @@ u3_mcut_hosts(c3_c* buf_c, c3_w len_w, u3_noun hot) u3_noun h_oth = u3h(oth); len_w = u3_mcut_path(buf_c, len_w, '.', u3kb_flop(u3k(h_oth))); + len_w = u3_mcut_char(buf_c, len_w, '.'); len_w = u3_mcut_char(buf_c, len_w, '\0'); oth = u3t(oth); @@ -332,4 +333,3 @@ u3_mpef_turfs(c3_c* buf_c, c3_w len_w, c3_c* pef_c, c3_c** tuf_c) return len_w; } - From ee32953f0c6608351efde6064e3b8b2c3d0b4c67 Mon Sep 17 00:00:00 2001 From: ziffee Date: Thu, 3 Apr 2025 13:00:52 -0600 Subject: [PATCH 14/34] ames: no longer a linked io driver --- pkg/vere/auto.c | 44 +-- pkg/vere/io/ames/ames.c | 134 +++----- pkg/vere/io/ames/lamp.c | 2 +- pkg/vere/io/{ => ames}/lamp.h | 6 +- pkg/vere/io/mesa.c | 631 ++++++++++++++++++---------------- pkg/vere/io/mesa/mesa.h | 2 +- pkg/vere/vere.h | 6 - 7 files changed, 398 insertions(+), 427 deletions(-) rename pkg/vere/io/{ => ames}/lamp.h (70%) diff --git a/pkg/vere/auto.c b/pkg/vere/auto.c index a9fb208965..b8d1d0bca1 100644 --- a/pkg/vere/auto.c +++ b/pkg/vere/auto.c @@ -400,10 +400,10 @@ u3_auto_slog(u3_auto* car_u) } } -/* u3_auto_link(): validate and link initalized [car_u] +/* _auto_link(): validate and link initalized [car_u] */ -u3_auto* -u3_auto_link(u3_auto* car_u, u3_pier* pir_u, u3_auto* nex_u) +static u3_auto* +_auto_link(u3_auto* car_u, u3_pier* pir_u, u3_auto* nex_u) { // skip null drivers // @@ -418,23 +418,7 @@ u3_auto_link(u3_auto* car_u, u3_pier* pir_u, u3_auto* nex_u) u3_assert( car_u->io.exit_f ); car_u->pir_u = pir_u; - u3_auto* rac_u = car_u; - while (rac_u->nex_u != NULL) { - rac_u = rac_u->nex_u; - rac_u->pir_u = pir_u; - u3_assert( rac_u->io.talk_f ); - u3_assert( rac_u->io.kick_f ); - u3_assert( rac_u->io.exit_f ); - } - - if (nex_u) { - nex_u->pir_u = pir_u; - u3_assert( nex_u->io.talk_f ); - u3_assert( nex_u->io.kick_f ); - u3_assert( nex_u->io.exit_f ); - } - - rac_u->nex_u = nex_u; + car_u->nex_u = nex_u; return car_u; } @@ -445,16 +429,16 @@ u3_auto_init(u3_pier* pir_u) { u3_auto* car_u = 0; - car_u = u3_auto_link(u3_hind_io_init(pir_u), pir_u, car_u); - car_u = u3_auto_link(u3_behn_io_init(pir_u), pir_u, car_u); - car_u = u3_auto_link(u3_conn_io_init(pir_u), pir_u, car_u); - car_u = u3_auto_link(u3_mesa_io_init(pir_u), pir_u, car_u); - car_u = u3_auto_link(u3_http_io_init(pir_u), pir_u, car_u); - car_u = u3_auto_link(u3_cttp_io_init(pir_u), pir_u, car_u); - car_u = u3_auto_link(u3_unix_io_init(pir_u), pir_u, car_u); - car_u = u3_auto_link(u3_term_io_init(pir_u), pir_u, car_u); - car_u = u3_auto_link(u3_fore_io_init(pir_u), pir_u, car_u); - car_u = u3_auto_link(u3_lick_io_init(pir_u), pir_u, car_u); + car_u = _auto_link(u3_hind_io_init(pir_u), pir_u, car_u); + car_u = _auto_link(u3_behn_io_init(pir_u), pir_u, car_u); + car_u = _auto_link(u3_conn_io_init(pir_u), pir_u, car_u); + car_u = _auto_link(u3_http_io_init(pir_u), pir_u, car_u); + car_u = _auto_link(u3_cttp_io_init(pir_u), pir_u, car_u); + car_u = _auto_link(u3_unix_io_init(pir_u), pir_u, car_u); + car_u = _auto_link(u3_term_io_init(pir_u), pir_u, car_u); + car_u = _auto_link(u3_fore_io_init(pir_u), pir_u, car_u); + car_u = _auto_link(u3_lick_io_init(pir_u), pir_u, car_u); + car_u = _auto_link(u3_mesa_io_init(pir_u), pir_u, car_u); return car_u; } diff --git a/pkg/vere/io/ames/ames.c b/pkg/vere/io/ames/ames.c index c9e5c74106..c17ab4fb7d 100644 --- a/pkg/vere/io/ames/ames.c +++ b/pkg/vere/io/ames/ames.c @@ -3,7 +3,7 @@ #include "vere.h" #include "mdns.h" #include "io/ames/stun.h" -#include "io/lamp.h" +#include "io/ames/lamp.h" #include "io/mesa/mesa.h" #include "noun.h" @@ -29,6 +29,16 @@ #define QUEUE_MAX 30 // max number of packets in queue #define DIRECT_ROUTE_TIMEOUT_MICROS 120000000 + typedef struct _u3_ames u3_ames; + typedef struct _u3_mesa_auto { + u3_auto car_u; + u3_ames* sam_u; + u3_pier* pir_u; + union { // uv udp handle + uv_udp_t wax_u; // + uv_handle_t had_u; // + }; // + } u3_mesa_auto; /* u3_fine: fine networking */ @@ -41,9 +51,9 @@ /* u3_ames: ames networking. */ typedef struct _u3_ames { // packet network state - u3_auto car_u; // ames driver + u3_mesa_auto* mes_u; // ames driver + u3_pier* pir_u; u3_fine fin_s; // fine networking - u3_pier* pir_u; // pier union { // uv udp handle uv_udp_t* wax_u; // uv_handle_t* had_u; // @@ -78,12 +88,11 @@ c3_d vil_d; // encryption failures c3_d saw_d; // successive scry failures } sat_u; // - u3_auto* mes_u; // mesa pointer } u3_ames; STATIC_ASSERT( ( ((void*)(u3_ames*)(void*)0) == - ((void*)(u3_ames*)(void*)&(((u3_ames*)(void*)0)->car_u)) ), + ((void*)(u3_ames*)(void*)&(((u3_ames*)(void*)0)->mes_u)) ), "u3_ames struct alignment" ); /* u3_head: ames or fine packet header @@ -710,10 +719,10 @@ _ames_send_cb(uv_udp_send_t* req_u, c3_i sas_i) } u3_peer* -_mesa_get_peer(u3_auto* sam_u, u3_ship her_u); +_mesa_get_peer(u3_mesa_auto* sam_u, u3_ship her_u); u3_peer* -_mesa_gut_peer(u3_auto* sam_u, u3_ship her_u); +_mesa_gut_peer(u3_mesa_auto* sam_u, u3_ship her_u); /* _ames_send(): send buffer to address on port. */ @@ -806,13 +815,16 @@ _ames_lane_into_cache(u3_ames* sam_u, u3_noun who, u3_noun las) // we either have a direct route, and a galaxy, or just one lane if ( c3n == u3h(lan) ) { per_u->dan_u = u3_ames_decode_lane(u3k(u3t(lan))); - if ( c3y == u3du(u3t(las)) ) + if ( c3y == u3du(u3t(las)) ) { u3_noun lan = u3h(u3t(las)); - assert(c3y == u3h(lan)); - per_u->lam_u = u3_ship_of_noun(u3t(lan)); + if ( c3n == u3h(lan) ) + u3m_p("strange second lane", lan); + else + per_u->lam_u = u3_ship_of_noun(u3t(lan)); + } } else { u3_ship who_u = u3_ship_of_noun(u3t(lan)); - if ( c3n == u3_ships_equal(sam_u->car_u.pir_u->who_u, who_u) ) { + if ( c3n == u3_ships_equal(sam_u->pir_u->who_u, who_u) ) { // delete direct lane if galaxy per_u->dan_u = (sockaddr_in){0}; } else { @@ -934,7 +946,7 @@ _ames_is_czar(u3_noun who) } c3_o -_ames_lamp_lane(u3_auto* car_u, u3_ship her_u, sockaddr_in* lan_u); +_ames_lamp_lane(u3_mesa_auto* mes_u, u3_ship her_u, sockaddr_in* lan_u); /* _ames_send_lane(): resolve/decode lane. RETAIN */ @@ -994,7 +1006,7 @@ _ames_send_lane(u3_ames* sam_u, u3_noun lan, sockaddr_in* lan_u) static void _ames_ef_send(u3_ames* sam_u, sockaddr_in lan_u, u3_noun pac) { - if ( c3n == sam_u->car_u.liv_o ) { + if ( c3n == sam_u->mes_u->car_u.liv_o ) { u3l_log("ames: not yet live, dropping outbound\r"); u3z(pac); return; @@ -1021,14 +1033,14 @@ _ames_ef_send(u3_ames* sam_u, sockaddr_in lan_u, u3_noun pac) static void _ames_cap_queue(u3_ames* sam_u) { - u3_ovum* egg_u = sam_u->car_u.ext_u; + u3_ovum* egg_u = sam_u->mes_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) ) { + while ( egg_u && (QUEUE_MAX < sam_u->mes_u->car_u.dep_w) ) { u3_ovum* nex_u = egg_u->nex_u; if ( c3__hear == u3h(egg_u->cad) ) { - u3_auto_drop(&sam_u->car_u, egg_u); + u3_auto_drop(&sam_u->mes_u->car_u, egg_u); sam_u->sat_u.dop_d++; if ( u3C.wag_w & u3o_verbose ) { @@ -1052,7 +1064,7 @@ _ames_cap_queue(u3_ames* sam_u) static void _ames_hear_bail(u3_ovum* egg_u, u3_noun lud) { - u3_ames* sam_u = (u3_ames*)egg_u->car_u; + u3_ames* sam_u = ((u3_mesa_auto*)egg_u->car_u)->sam_u; c3_w len_w = u3qb_lent(lud); if ( (1 == len_w) && c3__evil == u3h(u3h(lud)) ) { @@ -1102,7 +1114,7 @@ _ames_put_packet(u3_ames* sam_u, u3_noun cad = u3nt(c3__hear, u3nc(c3n, u3_ames_encode_lane(lan_u)), msg); u3_auto_peer( - u3_auto_plan(&sam_u->car_u, + u3_auto_plan(&sam_u->mes_u->car_u, u3_ovum_init(0, c3__a, wir, cad)), 0, 0, _ames_hear_bail); @@ -1527,7 +1539,7 @@ _fine_hear_request(u3_pact* req_u, c3_w cur_w) // scry into arvo for a page of packets // - u3_pier_peek_last(res_u->sam_u->car_u.pir_u, u3_nul, c3__ax, u3_nul, + u3_pier_peek_last(res_u->sam_u->pir_u, u3_nul, c3__ax, u3_nul, pax, res_u, _fine_hunk_scry_cb); } // cache hit, fill in response meow and send @@ -1560,7 +1572,7 @@ _fine_hear_response(u3_pact* pac_u, c3_w cur_w) u3i_bytes(pac_u->len_w, pac_u->hun_y)); u3_ovum* ovo_u = u3_ovum_init(0, c3__ames, wir, cad); - u3_auto_plan(&pac_u->sam_u->car_u, ovo_u); + u3_auto_plan(&pac_u->sam_u->mes_u->car_u, ovo_u); _ames_cap_queue(pac_u->sam_u); } @@ -1875,7 +1887,7 @@ _ames_put_dear(c3_c* ship, bool fake, c3_w s_addr, c3_s port, void* context) u3_noun cad = u3nt(c3__dear, u3k(u3t(whu)), u3nc(c3n, u3_ames_encode_lane(lan))); u3_auto_peer( - u3_auto_plan(&sam_u->car_u, + u3_auto_plan(&sam_u->mes_u->car_u, u3_ovum_init(0, c3__a, wir, cad)), 0, 0, _mdns_dear_bail); u3z(whu); @@ -1912,35 +1924,6 @@ _ames_io_start(u3_ames* sam_u) lan_u.sin_port = htons(por_s); u3l_log("ames: skipping port: %u", por_s); - // Bind and stuff. - { - - /*if ( (ret_i = uv_udp_bind(&sam_u->wax_u, - (const struct sockaddr*)&lan_u, 0)) != 0 ) - { - u3l_log("ames: port: %u", por_s); - u3l_log("ames: bind: %s", uv_strerror(ret_i)); - - if ( (c3y == zar_o) && - (UV_EADDRINUSE == ret_i) ) - { - u3l_log(" ...perhaps you've got two copies of vere running?"); - } - - // XX revise - // - u3_pier_bail(u3_king_stub()); - }*/ - - /* - c3_i add_i = sizeof(lan_u); - uv_udp_getsockname(&sam_u->wax_u, (struct sockaddr *)&lan_u, &add_i); - u3_assert(lan_u.sin_port); - - sam_u->pir_u->por_s = ntohs(lan_u.sin_port); - */ - } - if ( c3y == u3_Host.ops_u.net ) { u3l_log("ames: live on %d", sam_u->pir_u->por_s); } @@ -1965,7 +1948,6 @@ _ames_io_start(u3_ames* sam_u) uv_udp_recv_start(&sam_u->wax_u, _ames_alloc, _ames_recv_cb); */ - sam_u->car_u.liv_o = c3y; u3z(who); } @@ -2003,10 +1985,9 @@ _ames_prot_scry_cb(void* vod_p, u3_noun nun) /* _ames_io_talk(): start receiving ames traffic. */ -static void -_ames_io_talk(u3_auto* car_u) +void +_ames_io_talk(u3_ames* sam_u) { - u3_ames* sam_u = (u3_ames*)car_u; _ames_io_start(sam_u); // send born event @@ -2031,7 +2012,7 @@ _ames_io_talk(u3_auto* car_u) u3_noun gang = u3nc(u3_nul, u3_nul); // XX drop this; done at another level // - u3_pier_peek_last(car_u->pir_u, gang, c3__ax, u3_nul, + u3_pier_peek_last(sam_u->pir_u, gang, c3__ax, u3_nul, u3nt(u3i_string("protocol"), u3i_string("version"), u3_nul), sam_u, _ames_prot_scry_cb); } @@ -2065,10 +2046,9 @@ _ames_kick_newt(u3_ames* sam_u, u3_noun tag, u3_noun dat) /* _ames_io_kick(): apply effects */ -static c3_o -_ames_io_kick(u3_auto* car_u, u3_noun wir, u3_noun cad) +c3_o +_ames_io_kick(u3_ames* sam_u, u3_noun wir, u3_noun cad) { - u3_ames* sam_u = (u3_ames*)car_u; u3_noun tag, dat, i_wir; c3_o ret_o; @@ -2138,10 +2118,9 @@ _ames_exit_cb(u3_ames* sam_u) /* _ames_io_exit(): terminate ames I/O. */ -static void -_ames_io_exit(u3_auto* car_u) +void +_ames_io_exit(u3_ames* sam_u) { - u3_ames* sam_u = (u3_ames*)car_u; uv_close((uv_handle_t*)&sam_u->nat_u.tim_u, 0); uv_handle_type handle = uv_handle_get_type((uv_handle_t *)&sam_u->nat_u.pol_u); @@ -2152,10 +2131,9 @@ _ames_io_exit(u3_auto* car_u) /* _ames_io_info(): produce status info. */ -static u3_noun -_ames_io_info(u3_auto* car_u) +u3_noun +_ames_io_info(u3_ames* sam_u) { - u3_ames* sam_u = (u3_ames*)car_u; c3_w sac_w; sac_w = u3h_count(sam_u->fin_s.sac_p) * 4; @@ -2185,10 +2163,9 @@ _ames_io_info(u3_auto* car_u) /* _ames_io_slog(): print status info. */ -static void -_ames_io_slog(u3_auto* car_u) +void +_ames_io_slog(u3_ames* sam_u) { - u3_ames* sam_u = (u3_ames*)car_u; c3_w sac_w; sac_w = u3h_count(sam_u->fin_s.sac_p) * 4; @@ -2222,15 +2199,14 @@ _ames_io_slog(u3_auto* car_u) /* u3_ames_io_init(): initialize ames I/O. */ -u3_auto* -u3_ames_io_init(u3_pier* pir_u, - uv_udp_t* wax_u, - u3_auto* mes_u) +u3_ames* +u3_ames_io_init(u3_mesa_auto* mes_u) { u3_ames* sam_u = c3_calloc(sizeof(*sam_u)); - sam_u->wax_u = wax_u; sam_u->mes_u = mes_u; - sam_u->pir_u = pir_u; + sam_u->wax_u = &mes_u->wax_u; + sam_u->mes_u = mes_u; + sam_u->pir_u = mes_u->pir_u; sam_u->fig_u.see_o = c3y; sam_u->fig_u.fit_o = c3n; @@ -2239,7 +2215,7 @@ u3_ames_io_init(u3_pier* pir_u, uv_timer_init(u3L, &sam_u->nat_u.tim_u); // enable forwarding on galaxies only - u3_noun who = u3_ship_to_noun(sam_u->pir_u->who_u); + u3_noun who = u3_ship_to_noun(sam_u->mes_u->pir_u->who_u); u3_noun rac = u3do("clan:title", who); sam_u->sat_u.for_o = ( c3__czar == rac ) ? c3y : c3n; @@ -2279,14 +2255,6 @@ u3_ames_io_init(u3_pier* pir_u, u3_Host.ops_u.net = c3n; } - u3_auto* car_u = &sam_u->car_u; - car_u->nam_m = c3__ames; - car_u->liv_o = c3n; - car_u->io.talk_f = _ames_io_talk; - car_u->io.info_f = _ames_io_info; - car_u->io.slog_f = _ames_io_slog; - car_u->io.kick_f = _ames_io_kick; - car_u->io.exit_f = _ames_io_exit; sam_u->fin_s.sam_u = sam_u; @@ -2300,5 +2268,5 @@ u3_ames_io_init(u3_pier* pir_u, u3z(now); } - return car_u; + return sam_u; } diff --git a/pkg/vere/io/ames/lamp.c b/pkg/vere/io/ames/lamp.c index def3c65e4d..a8f6e45af7 100644 --- a/pkg/vere/io/ames/lamp.c +++ b/pkg/vere/io/ames/lamp.c @@ -1,5 +1,5 @@ #include "vere.h" -#include "io/lamp.h" +#include "io/ames/lamp.h" c3_o _mesa_is_lane_zero(sockaddr_in lan_u); diff --git a/pkg/vere/io/lamp.h b/pkg/vere/io/ames/lamp.h similarity index 70% rename from pkg/vere/io/lamp.h rename to pkg/vere/io/ames/lamp.h index 424f08defe..b4012b4a08 100644 --- a/pkg/vere/io/lamp.h +++ b/pkg/vere/io/ames/lamp.h @@ -1,12 +1,12 @@ -#include "mesa/mesa.h" +#include "io/mesa/mesa.h" #define _CZAR_GONE (htonl(UINT32_MAX)) #define NLOCALHOST (htonl(0x7f000001)) typedef struct _u3_ames_lamp_state { // - u3_auto* car_u; - per_map* per_u; + u3_auto* car_u; + per_map* per_u; c3_c** dns_c; // domain c3_o dom_o; // have domain uv_timer_t tim_u; // resolve timer diff --git a/pkg/vere/io/mesa.c b/pkg/vere/io/mesa.c index 60fccac670..e7e99fa860 100644 --- a/pkg/vere/io/mesa.c +++ b/pkg/vere/io/mesa.c @@ -7,7 +7,7 @@ #include "ur/ur.h" #include "ship.h" #include "io/ames/stun.h" -#include "io/lamp.h" +#include "io/ames/lamp.h" #include "mesa/mesa.h" #include "mesa/bitset.h" #include @@ -94,13 +94,14 @@ typedef struct _u3_mesa_stat { struct _u3_mesa_pact; +typedef struct _u3_mesa u3_mesa; + typedef struct _u3_pact_stat { c3_y tie_y; // tries c3_d sen_d; // last sent c3_y sip_y; // skips } u3_pact_stat; -struct _u3_mesa; typedef struct _u3_gage { c3_w rtt_w; // rtt @@ -113,11 +114,9 @@ typedef struct _u3_gage { // } u3_gage; -struct _u3_mesa; - typedef struct _u3_mesa_pict { uv_udp_send_t snd_u; - struct _u3_mesa* sam_u; + struct _u3_mesa* mes_u; u3_mesa_pact pac_u; } u3_mesa_pict; @@ -252,6 +251,7 @@ static void u3_free_line( u3_mesa_line* lin_u ) typedef struct _u3_mesa { u3_auto car_u; + void* sam_u; // ames pointer u3_pier* pir_u; union { // uv udp handle uv_udp_t wax_u; // @@ -273,7 +273,6 @@ typedef struct _u3_mesa { uv_timer_t tim_u; // pit clear timer u3_stun_client sun_u; // stun client state u3_ames_lamp_state lam_u; // stun client state - u3_auto* ame_u; // ames pointer } u3_mesa; STATIC_ASSERT( @@ -327,21 +326,21 @@ typedef struct _u3_cace_enty { // instead just alloc new buffer and stick here typedef struct _u3_seal { uv_udp_send_t snd_u; // udp send request - u3_mesa* sam_u; + u3_mesa* mes_u; c3_w len_w; c3_y* buf_y; arena are_u; } u3_seal; typedef struct _u3_mesa_cb_data { - u3_mesa* sam_u; + u3_mesa* mes_u; u3_mesa_name nam_u; sockaddr_in lan_u; } u3_mesa_cb_data; -static void _meet_peer(u3_mesa* sam_u, u3_peer* per_u); -static void _init_peer(u3_mesa* sam_u, u3_peer* per_u, u3_ship her_u); -static void _mesa_ef_saxo(u3_mesa* sam_u, u3_noun zad); +static void _meet_peer(u3_mesa* mes_u, u3_peer* per_u); +static void _init_peer(u3_mesa* mes_u, u3_peer* per_u, u3_ship her_u); +static void _mesa_ef_saxo(u3_mesa* mes_u, u3_noun zad); static c3_d get_millis() { @@ -548,7 +547,7 @@ _dire_etch_ud(c3_d num_d) return u3i_bytes(26 - dif_w, buf_y); // XX known-non-null } -/* _mesa_request_key(): produce key for request hashtable sam_u->req_p from nam_u +/* _mesa_request_key(): produce key for request hashtable mes_u->req_p from nam_u */ u3_noun _mesa_request_key(u3_mesa_name* nam_u) @@ -583,9 +582,9 @@ u3_mesa_encode_lane(sockaddr_in lan_u) { } u3_peer* -_mesa_get_peer(u3_mesa* sam_u, u3_ship her_u) +_mesa_get_peer(u3_mesa* mes_u, u3_ship her_u) { - per_map_itr itr_u = vt_get(&sam_u->per_u, her_u); + per_map_itr itr_u = vt_get(&mes_u->per_u, her_u); if ( vt_is_end(itr_u) ) { return NULL; } @@ -593,9 +592,9 @@ _mesa_get_peer(u3_mesa* sam_u, u3_ship her_u) } u3_peer* -_mesa_gut_peer(u3_mesa* sam_u, u3_ship her_u) +_mesa_gut_peer(u3_mesa* mes_u, u3_ship her_u) { - per_map_itr itr_u = vt_get_or_insert(&sam_u->per_u, her_u, NULL); + per_map_itr itr_u = vt_get_or_insert(&mes_u->per_u, her_u, NULL); if ( vt_is_end(itr_u) ) { fprintf(stderr, "mesa: cannot allocate memory for peer, dying"); return NULL; @@ -603,19 +602,19 @@ _mesa_gut_peer(u3_mesa* sam_u, u3_ship her_u) if (NULL == itr_u.data->val) { u3_peer* per_u = itr_u.data->val - = new(&sam_u->par_u, u3_peer, 1); - _init_peer(sam_u, per_u, her_u); + = new(&mes_u->par_u, u3_peer, 1); + _init_peer(mes_u, per_u, her_u); } return itr_u.data->val; } static void -_mesa_put_peer(u3_mesa* sam_u, u3_ship her_u, u3_peer* per_u) +_mesa_put_peer(u3_mesa* mes_u, u3_ship her_u, u3_peer* per_u) { - per_map_itr itr_u = vt_get(&sam_u->per_u, her_u); + per_map_itr itr_u = vt_get(&mes_u->per_u, her_u); if ( vt_is_end(itr_u) ) { - itr_u = vt_insert(&sam_u->per_u, her_u, per_u); + itr_u = vt_insert(&mes_u->per_u, her_u, per_u); if ( vt_is_end(itr_u) ) { fprintf(stderr, "mesa: cannot allocate memory for peer, dying"); @@ -631,9 +630,9 @@ _mesa_put_peer(u3_mesa* sam_u, u3_ship her_u, u3_peer* per_u) * produces a NULL pointer if no pending request exists */ static u3_pend_req* -_mesa_get_request(u3_mesa* sam_u, u3_mesa_name* nam_u) { +_mesa_get_request(u3_mesa* mes_u, u3_mesa_name* nam_u) { u3_str key_u = {nam_u->pat_c, nam_u->pat_s}; - req_map_itr itr_u = vt_get(&sam_u->req_u, key_u); + req_map_itr itr_u = vt_get(&mes_u->req_u, key_u); if ( vt_is_end(itr_u) ) { return NULL; } @@ -647,16 +646,16 @@ _mesa_del_request_cb(uv_handle_t* han_u) { } static void -_mesa_del_request(u3_mesa* sam_u, u3_mesa_name* nam_u) { +_mesa_del_request(u3_mesa* mes_u, u3_mesa_name* nam_u) { u3_str key_u = {nam_u->pat_c, nam_u->pat_s}; - req_map_itr itr_u = vt_get(&sam_u->req_u, key_u); + req_map_itr itr_u = vt_get(&mes_u->req_u, key_u); if ( vt_is_end(itr_u) ) { return; } u3_pend_req* req_u = itr_u.data->val; - vt_erase(&sam_u->req_u, key_u); + vt_erase(&mes_u->req_u, key_u); if ( (u3_pend_req*)CTAG_WAIT != req_u ) { uv_timer_stop(&req_u->tim_u); req_u->tim_u.data = req_u; @@ -667,9 +666,9 @@ _mesa_del_request(u3_mesa* sam_u, u3_mesa_name* nam_u) { /* _mesa_put_request(): save new pending request state for nam_u */ static void -_mesa_put_request(u3_mesa* sam_u, u3_mesa_name* nam_u, u3_pend_req* req_u) { +_mesa_put_request(u3_mesa* mes_u, u3_mesa_name* nam_u, u3_pend_req* req_u) { u3_str key_u = {nam_u->pat_c, nam_u->pat_s}; - req_map_itr itr_u = vt_insert(&sam_u->req_u, key_u, req_u); + req_map_itr itr_u = vt_insert(&mes_u->req_u, key_u, req_u); if ( vt_is_end(itr_u) ) { fprintf(stderr, "mesa: cannot allocate memory for request, dying"); @@ -678,11 +677,11 @@ _mesa_put_request(u3_mesa* sam_u, u3_mesa_name* nam_u, u3_pend_req* req_u) { } sockaddr_in -_mesa_get_direct_lane(u3_mesa* sam_u, u3_ship her_u) +_mesa_get_direct_lane(u3_mesa* mes_u, u3_ship her_u) { sockaddr_in adr_u = {0}; - per_map_itr itr_u = vt_get(&sam_u->per_u, her_u); + per_map_itr itr_u = vt_get(&mes_u->per_u, her_u); if ( vt_is_end(itr_u) ) { return adr_u; } @@ -701,8 +700,8 @@ _mesa_lanes_equal(sockaddr_in lan_u, sockaddr_in lon_u) /* _mesa_get_lane(): get lane */ static u3_gage* -_mesa_get_gage(u3_mesa* sam_u, u3_ship her_u) { - gag_map_itr itr_u = vt_get(&sam_u->gag_u, her_u); +_mesa_get_gage(u3_mesa* mes_u, u3_ship her_u) { + gag_map_itr itr_u = vt_get(&mes_u->gag_u, her_u); if ( vt_is_end(itr_u) ) { return NULL; } @@ -713,9 +712,9 @@ _mesa_get_gage(u3_mesa* sam_u, u3_ship her_u) { * */ static void -_mesa_put_gage(u3_mesa* sam_u, u3_ship her_u, u3_gage* gag_u) +_mesa_put_gage(u3_mesa* mes_u, u3_ship her_u, u3_gage* gag_u) { - gag_map_itr itr_u = vt_insert(&sam_u->gag_u, her_u, gag_u); + gag_map_itr itr_u = vt_insert(&mes_u->gag_u, her_u, gag_u); if ( vt_is_end(itr_u) ) { fprintf(stderr, "mesa: cannot allocate memory for gage, dying"); u3_king_bail(); @@ -807,9 +806,9 @@ _mesa_req_pact_sent(u3_pend_req* req_u, c3_d fra_d, c3_d now_d) static void _mesa_alloc(uv_handle_t* had_u, size_t len_i, uv_buf_t* buf) { - u3_mesa* sam_u = (u3_mesa*)had_u->data; - sam_u->are_u.beg = (char*)are_y; - c3_c* ptr_v = new(&sam_u->are_u, c3_c, 400000); + u3_mesa* mes_u = (u3_mesa*)had_u->data; + mes_u->are_u.beg = (char*)are_y; + c3_c* ptr_v = new(&mes_u->are_u, c3_c, 400000); buf->base = ptr_v; buf->len = 400000; } @@ -853,14 +852,14 @@ static void _mesa_send_cb(uv_udp_send_t* req_u, c3_i sas_i) { u3_seal* sel_u = (u3_seal*)req_u; - u3_mesa* sam_u = sel_u->sam_u; + u3_mesa* mes_u = sel_u->mes_u; if ( sas_i ) { u3l_log("mesa: send fail_async: %s", uv_strerror(sas_i)); - //sam_u->fig_u.net_o = c3n; + //mes_u->fig_u.net_o = c3n; } else { - //sam_u->fig_u.net_o = c3y; + //mes_u->fig_u.net_o = c3y; } _mesa_free_seal(sel_u); @@ -871,7 +870,7 @@ _mesa_send_cb2(uv_udp_send_t* req_u, c3_i sas_i) { if ( sas_i ) { u3l_log("mesa: send fail_async: %s", uv_strerror(sas_i)); - //sam_u->fig_u.net_o = c3n; + //mes_u->fig_u.net_o = c3n; } c3_free(req_u); } @@ -888,14 +887,14 @@ _mesa_send_cb3(uv_udp_send_t* snt_u, c3_i sas_i) send_helper* snd_u = (send_helper*)snt_u; if ( sas_i ) { u3l_log("mesa: send fail_async: %s", uv_strerror(sas_i)); - //sam_u->fig_u.net_o = c3n; + //mes_u->fig_u.net_o = c3n; } else { /* _mesa_req_pact_sent(snd_u->req_u, snd_u->fra_d); */ } c3_free(snd_u); } -static c3_i _mesa_send_buf2(u3_mesa* sam_u, struct sockaddr** ads_u, uv_buf_t** bfs_u, c3_w* int_u, c3_w num_w) +static c3_i _mesa_send_buf2(u3_mesa* mes_u, struct sockaddr** ads_u, uv_buf_t** bfs_u, c3_w* int_u, c3_w num_w) { /* add_u.sin_family = AF_INET; */ @@ -908,7 +907,7 @@ static c3_i _mesa_send_buf2(u3_mesa* sam_u, struct sockaddr** ads_u, uv_buf_t** /* c3_c* sip_c = inet_ntoa(add_u.sin_addr); */ // u3l_log("mesa: sending packet to %s:%u", sip_c, por_s); #endif - c3_i sas_i = uv_udp_try_send2(&sam_u->wax_u, num_w, bfs_u, int_u, ads_u, 0); + c3_i sas_i = uv_udp_try_send2(&mes_u->wax_u, num_w, bfs_u, int_u, ads_u, 0); if ( sas_i < 0 ) { u3l_log("ames: send fail_sync: %s", uv_strerror(sas_i)); return 0; @@ -917,11 +916,11 @@ static c3_i _mesa_send_buf2(u3_mesa* sam_u, struct sockaddr** ads_u, uv_buf_t** // else if ( sas_i < (c3_i)num_w) { // for ( c3_i i = sas_i; i < (c3_i)num_w; i++) { // uv_udp_send_t* req_u = c3_malloc(sizeof(*req_u)); -// uv_udp_send(req_u, &sam_u->wax_u, bfs_u[i], 1, ads_u[0], _mesa_send_cb2); +// uv_udp_send(req_u, &mes_u->wax_u, bfs_u[i], 1, ads_u[0], _mesa_send_cb2); // } // } } -static void _mesa_send_buf3(u3_mesa* sam_u, sockaddr_in add_u, uv_buf_t buf_u) +static void _mesa_send_buf3(u3_mesa* mes_u, sockaddr_in add_u, uv_buf_t buf_u) { add_u.sin_addr.s_addr = ( u3_Host.ops_u.net == c3y ) ? add_u.sin_addr.s_addr : htonl(0x7f000001); @@ -940,20 +939,20 @@ static void _mesa_send_buf3(u3_mesa* sam_u, sockaddr_in add_u, uv_buf_t buf_u) uv_udp_send_t* snd_u = c3_malloc(sizeof(*snd_u)); c3_i sas_i = uv_udp_send((uv_udp_send_t*)snd_u, - &sam_u->wax_u, + &mes_u->wax_u, &buf_u, 1, (const struct sockaddr*)&add_u, _mesa_send_cb3); if ( sas_i ) { u3l_log("ames: send fail_sync: %s", uv_strerror(sas_i)); - /*if ( c3y == sam_u->fig_u.net_o ) { - //sam_u->fig_u.net_o = c3n; + /*if ( c3y == mes_u->fig_u.net_o ) { + //mes_u->fig_u.net_o = c3n; }*/ } } -static void _mesa_send_buf(u3_mesa* sam_u, sockaddr_in add_u, c3_y* buf_y, c3_w len_w) +static void _mesa_send_buf(u3_mesa* mes_u, sockaddr_in add_u, c3_y* buf_y, c3_w len_w) { add_u.sin_addr.s_addr = ( u3_Host.ops_u.net == c3y ) ? add_u.sin_addr.s_addr : htonl(0x7f000001); @@ -967,7 +966,7 @@ static void _mesa_send_buf(u3_mesa* sam_u, sockaddr_in add_u, c3_y* buf_y, c3_w u3_seal* sel_u = c3_calloc(sizeof(*sel_u)); sel_u->buf_y = buf_y; sel_u->len_w = len_w; - sel_u->sam_u = sam_u; + sel_u->mes_u = mes_u; @@ -979,15 +978,15 @@ static void _mesa_send_buf(u3_mesa* sam_u, sockaddr_in add_u, c3_y* buf_y, c3_w uv_buf_t buf_u = uv_buf_init((c3_c*)buf_y, len_w); c3_i sas_i = uv_udp_send(&sel_u->snd_u, - &sam_u->wax_u, + &mes_u->wax_u, &buf_u, 1, (const struct sockaddr*)&add_u, _mesa_send_cb); if ( sas_i ) { u3l_log("ames: send fail_sync: %s", uv_strerror(sas_i)); - /*if ( c3y == sam_u->fig_u.net_o ) { - //sam_u->fig_u.net_o = c3n; + /*if ( c3y == mes_u->fig_u.net_o ) { + //mes_u->fig_u.net_o = c3n; }*/ _mesa_free_seal(sel_u); } @@ -995,14 +994,14 @@ static void _mesa_send_buf(u3_mesa* sam_u, sockaddr_in add_u, c3_y* buf_y, c3_w static void _mesa_send(u3_mesa_pict* pic_u, sockaddr_in lan_u) { - u3_mesa* sam_u = pic_u->sam_u; + u3_mesa* mes_u = pic_u->mes_u; c3_y *buf_y = c3_calloc(PACT_SIZE); c3_w len_w = mesa_etch_pact_to_buf(buf_y, PACT_SIZE, &pic_u->pac_u); - _mesa_send_buf(sam_u, lan_u, buf_y, len_w); + _mesa_send_buf(mes_u, lan_u, buf_y, len_w); } typedef struct _u3_mesa_request_data { - u3_mesa* sam_u; + u3_mesa* mes_u; u3_ship her_u; u3_mesa_name* nam_u; c3_y* buf_y; @@ -1035,7 +1034,7 @@ _mesa_free_resend_data(u3_mesa_resend_data* res_u) } static void -_mesa_send_bufs(u3_mesa* sam_u, +_mesa_send_bufs(u3_mesa* mes_u, u3_peer* per_u, c3_y* buf_y, c3_w len_w, @@ -1044,7 +1043,7 @@ _mesa_send_bufs(u3_mesa* sam_u, static void _mesa_send_modal(u3_peer* per_u, uv_buf_t buf_u, u3_pit_addr* las_u) { - u3_mesa* sam_u = per_u->sam_u; + u3_mesa* mes_u = per_u->mes_u; c3_d now_d = _get_now_micros(); c3_w len_w = buf_u.len; @@ -1052,17 +1051,17 @@ _mesa_send_modal(u3_peer* per_u, uv_buf_t buf_u, u3_pit_addr* las_u) memcpy(sen_y, buf_u.base, len_w); u3_ship lam_u = per_u->lam_u; - c3_o our_o = u3_ships_equal(lam_u, sam_u->pir_u->who_u); + c3_o our_o = u3_ships_equal(lam_u, mes_u->pir_u->who_u); if ( ( c3y == _mesa_is_direct_mode(per_u) ) || // if we are the sponsor of the ship, don't send to ourselves (our_o == c3y) ) { // u3l_log("mesa: direct"); - _mesa_send_buf(sam_u, per_u->dan_u, sen_y, len_w); + _mesa_send_buf(mes_u, per_u->dan_u, sen_y, len_w); per_u->dir_u.sen_d = now_d; } else if ( las_u != NULL ) { - _mesa_send_bufs(sam_u, per_u, sen_y, len_w, las_u); + _mesa_send_bufs(mes_u, per_u, sen_y, len_w, las_u); } else { #ifdef MESA_DEBUG /* c3_c* gal_c = u3_ship_to_string(lam_u); */ @@ -1070,14 +1069,14 @@ _mesa_send_modal(u3_peer* per_u, uv_buf_t buf_u, u3_pit_addr* las_u) /* c3_free(gal_c); */ #endif // - sockaddr_in ind_u = _mesa_get_direct_lane(sam_u, per_u->lam_u); - _mesa_send_buf(sam_u, ind_u, sen_y, len_w); + sockaddr_in ind_u = _mesa_get_direct_lane(mes_u, per_u->lam_u); + _mesa_send_buf(mes_u, ind_u, sen_y, len_w); per_u->ind_u.sen_d = now_d; if ( c3n == _mesa_is_lane_zero(per_u->dan_u) ) { c3_y* san_y = c3_calloc(len_w); memcpy(san_y, buf_u.base, len_w); - _mesa_send_buf(sam_u, per_u->dan_u, san_y, len_w); + _mesa_send_buf(mes_u, per_u->dan_u, san_y, len_w); per_u->dir_u.sen_d = now_d; } } @@ -1144,7 +1143,7 @@ _try_resend(u3_pend_req* req_u, c3_d nex_d) /* new(&scr_u, uv_buf_t, 1); */ /* bfs_u[i_w] = buf_u; */ /* c3_w len_w = mesa_etch_pact_to_buf(buf_y, PACT_SIZE, pac_u); */ - /* _mesa_send_buf3(req_u->per_u->sam_u, req_u->per_u->dan_u, buf_u, req_u, i_d); */ + /* _mesa_send_buf3(req_u->per_u->mes_u, req_u->per_u->dan_u, buf_u, req_u, i_d); */ _mesa_send_modal(req_u->per_u, buf_u, NULL); _mesa_req_pact_resent(req_u, &pac_u->pek_u.nam_u, now_d); // i_w++; @@ -1160,8 +1159,8 @@ _try_resend(u3_pend_req* req_u, c3_d nex_d) /* } */ if ( c3y == los_o ) { - /* _mesa_send_buf2(req_u->per_u->sam_u, ads_u, bus_u, int_u, i_w); */ - /* _mesa_send_buf2(req_u->per_u->sam_u, req_u->per_u->dan_u, bfs_u, i_w); */ + /* _mesa_send_buf2(req_u->per_u->mes_u, ads_u, bus_u, int_u, i_w); */ + /* _mesa_send_buf2(req_u->per_u->mes_u, req_u->per_u->dan_u, bfs_u, i_w); */ req_u->gag_u->sst_w = c3_max(1, req_u->gag_u->wnd_w / 2); req_u->gag_u->wnd_w = req_u->gag_u->sst_w; req_u->gag_u->rto_w = _clamp_rto(req_u->gag_u->rto_w * 2); @@ -1262,20 +1261,20 @@ _mesa_req_pact_done(u3_pend_req* req_u, c3_y hop_y, sockaddr_in lan_u) { - u3_mesa* sam_u = req_u->per_u->sam_u; // needed for the MESA_LOG macro + u3_mesa* mes_u = req_u->per_u->mes_u; // needed for the MESA_LOG macro // received past the end of the message if ( mesa_num_leaves(dat_u->tob_d) <= nam_u->fra_d ) { u3l_log("strange tob_d %"PRIu64" fra_d %"PRIu64" req_u %"PRIu64, dat_u->tob_d, nam_u->fra_d, req_u->hav_d); - MESA_LOG(sam_u, STRANGE); + MESA_LOG(mes_u, STRANGE); // XX: is this sufficient to drop whole request return; } // received duplicate if ( c3y == bitset_has(&req_u->was_u, nam_u->fra_d) ) { - // MESA_LOG(sam_u, DUPE); + // MESA_LOG(mes_u, DUPE); /* _update_resend_timer(req_u); */ return; } @@ -1307,13 +1306,13 @@ _mesa_req_pact_done(u3_pend_req* req_u, else if ( c3y != lss_verifier_ingest(req_u->los_u, dat_u->fra_y, dat_u->len_w, par_u) ) { u3l_log("auth fail frag %"PRIu64, nam_u->fra_d); u3l_log("nit_o %u", nam_u->nit_o); - _mesa_del_request(sam_u, nam_u); - MESA_LOG(sam_u, AUTH); + _mesa_del_request(mes_u, nam_u); + MESA_LOG(mes_u, AUTH); return; } else if ( c3y != _mesa_burn_misorder_queue(req_u, nam_u->boq_y, req_u->los_u->counter) ) { - MESA_LOG(sam_u, AUTH) - _mesa_del_request(sam_u, nam_u); + MESA_LOG(mes_u, AUTH) + _mesa_del_request(mes_u, nam_u); return; } else { @@ -1357,12 +1356,12 @@ c3_s _ames_czar_port(c3_y imp_y); static sockaddr_in -_mesa_realise_lane(u3_mesa* sam_u, u3_noun lan) { +_mesa_realise_lane(u3_mesa* mes_u, u3_noun lan) { sockaddr_in lan_u = {0}; lan_u.sin_family = AF_INET; if ( c3y == u3a_is_cat(lan) ) { - lan_u = _mesa_get_direct_lane(sam_u, u3_ship_of_noun(lan)); + lan_u = _mesa_get_direct_lane(mes_u, u3_ship_of_noun(lan)); // u3_assert( lan < 256 ); if ( (c3n == u3_Host.ops_u.net) ) { lan_u.sin_addr.s_addr = htonl(0x7f000001); @@ -1387,7 +1386,7 @@ _mesa_realise_lane(u3_mesa* sam_u, u3_noun lan) { } static void -_mesa_send_bufs(u3_mesa* sam_u, +_mesa_send_bufs(u3_mesa* mes_u, u3_peer* per_u, // null for response packets c3_y* buf_y, c3_w len_w, @@ -1403,7 +1402,7 @@ _mesa_send_bufs(u3_mesa* sam_u, } else { c3_y* sen_y = c3_calloc(len_w); memcpy(sen_y, buf_y, len_w); - _mesa_send_buf(sam_u, lan_u, sen_y, len_w); + _mesa_send_buf(mes_u, lan_u, sen_y, len_w); if ( per_u && (c3y == _mesa_lanes_equal(lan_u, per_u->dan_u)) ) { per_u->dir_u.sen_d = _get_now_micros(); } @@ -1413,9 +1412,9 @@ _mesa_send_bufs(u3_mesa* sam_u, } static u3_pit_entry* -_mesa_get_pit(u3_mesa* sam_u, u3_mesa_name* nam_u) +_mesa_get_pit(u3_mesa* mes_u, u3_mesa_name* nam_u) { - pit_map_itr itr_u = vt_get(&sam_u->pit_u, nam_u->str_u); + pit_map_itr itr_u = vt_get(&mes_u->pit_u, nam_u->str_u); if ( vt_is_end(itr_u) ) { return NULL; } @@ -1423,16 +1422,16 @@ _mesa_get_pit(u3_mesa* sam_u, u3_mesa_name* nam_u) } static void -_mesa_del_pit(u3_mesa* sam_u, u3_mesa_name* nam_u) +_mesa_del_pit(u3_mesa* mes_u, u3_mesa_name* nam_u) { /* u3l_log("deleting %llu", nam_u->fra_d); */ - vt_erase(&sam_u->pit_u, nam_u->str_u); + vt_erase(&mes_u->pit_u, nam_u->str_u); } static void -_mesa_add_lane_to_pit(u3_mesa* sam_u, u3_mesa_name* nam_u, sockaddr_in lan_u) +_mesa_add_lane_to_pit(u3_mesa* mes_u, u3_mesa_name* nam_u, sockaddr_in lan_u) { - pit_map_itr itr_u = vt_get(&sam_u->pit_u, nam_u->str_u); + pit_map_itr itr_u = vt_get(&mes_u->pit_u, nam_u->str_u); c3_d now_d = _get_now_micros(); @@ -1450,7 +1449,7 @@ _mesa_add_lane_to_pit(u3_mesa* sam_u, u3_mesa_name* nam_u, sockaddr_in lan_u) memcpy(str_c, nam_u->str_u.str_c, nam_u->str_u.len_w); u3_str str_u = {str_c, nam_u->str_u.len_w}; - itr_u = vt_insert(&sam_u->pit_u, str_u, ent_u); + itr_u = vt_insert(&mes_u->pit_u, str_u, ent_u); if ( vt_is_end(itr_u) ) { fprintf(stderr, "mesa: cannot allocate memory for pit, dying"); @@ -1485,7 +1484,7 @@ _mesa_resend_timer_cb(uv_timer_t* tim_u) u3_mesa_request_data* dat_u = &res_u->dat_u; res_u->ret_y--; - u3_pend_req* pit_u = _mesa_get_request(dat_u->sam_u, dat_u->nam_u); + u3_pend_req* pit_u = _mesa_get_request(dat_u->mes_u, dat_u->nam_u); if ( (u3_pend_req*)CTAG_WAIT != pit_u ) { #ifdef MESA_DEBUG // u3l_log("mesa: resend PIT entry gone %u", res_u->ret_y); @@ -1499,7 +1498,7 @@ _mesa_resend_timer_cb(uv_timer_t* tim_u) #endif } - _mesa_send_bufs(dat_u->sam_u, + _mesa_send_bufs(dat_u->mes_u, NULL, dat_u->buf_y, dat_u->len_w, @@ -1509,19 +1508,19 @@ _mesa_resend_timer_cb(uv_timer_t* tim_u) uv_timer_start(&res_u->tim_u, _mesa_resend_timer_cb, 1000, 0); } else { - _mesa_del_request(res_u->dat_u.sam_u, res_u->dat_u.nam_u); + _mesa_del_request(res_u->dat_u.mes_u, res_u->dat_u.nam_u); _mesa_free_resend_data(res_u); } } static u3_pit_addr* -_mesa_lanes_to_addrs(u3_mesa* sam_u, u3_noun las, arena* are_u) { +_mesa_lanes_to_addrs(u3_mesa* mes_u, u3_noun las, arena* are_u) { u3_pit_addr* adr_u = NULL; u3_noun lan, t = las; while ( t != u3_nul ) { u3x_cell(t, &lan, &t); u3_pit_addr* new_u = new(are_u, u3_pit_addr, 1); - new_u->sdr_u = _mesa_realise_lane(sam_u, u3k(lan)); + new_u->sdr_u = _mesa_realise_lane(mes_u, u3k(lan)); new_u->nex_p = adr_u; adr_u = new_u; } @@ -1529,7 +1528,7 @@ _mesa_lanes_to_addrs(u3_mesa* sam_u, u3_noun las, arena* are_u) { } static void -_mesa_hear(u3_mesa* sam_u, +_mesa_hear(u3_mesa* mes_u, const struct sockaddr_in* adr_u, c3_w len_w, c3_y* hun_y); @@ -1542,7 +1541,7 @@ static void time_elapsed(c3_d fra_d, c3_d total, c3_d begin, c3_d end) } static void -packet_test(u3_mesa* sam_u, c3_c* fil_c) { +packet_test(u3_mesa* mes_u, c3_c* fil_c) { FILE* fd = fopen(fil_c, "rb"); fseek(fd, 0L, SEEK_END); c3_d sz = ftell(fd); @@ -1569,9 +1568,9 @@ packet_test(u3_mesa* sam_u, c3_c* fil_c) { c3_w len_w = *(c3_w*)(packets+i); /* u3l_log("len_w %u i %"PRIu64, len_w, i); */ i += 4; - _mesa_hear(sam_u, (const sockaddr_in*)&adr_u, len_w, packets + i); + _mesa_hear(mes_u, (const sockaddr_in*)&adr_u, len_w, packets + i); /* tim_y[tidx] = _get_now_micros(); */ - sam_u->are_u.beg = (char*)are_y; + mes_u->are_u.beg = (char*)are_y; i += len_w; /* tidx++; */ } @@ -1585,7 +1584,7 @@ packet_test(u3_mesa* sam_u, c3_c* fil_c) { } static void -_mesa_ef_send(u3_mesa* sam_u, u3_noun las, u3_noun pac) +_mesa_ef_send(u3_mesa* mes_u, u3_noun las, u3_noun pac) { c3_w len_w = u3r_met(3, pac); arena are_u = arena_create(len_w + 16384); @@ -1603,7 +1602,7 @@ _mesa_ef_send(u3_mesa* sam_u, u3_noun las, u3_noun pac) return; } - u3_pend_req* old_u = _mesa_get_request(sam_u, &pac_u.pek_u.nam_u); + u3_pend_req* old_u = _mesa_get_request(mes_u, &pac_u.pek_u.nam_u); if (old_u) { u3z(pac); @@ -1613,7 +1612,7 @@ _mesa_ef_send(u3_mesa* sam_u, u3_noun las, u3_noun pac) } if ( PACT_PAGE == pac_u.hed_u.typ_y ) { - u3_pit_entry* pin_u = _mesa_get_pit(sam_u, &pac_u.pek_u.nam_u); + u3_pit_entry* pin_u = _mesa_get_pit(mes_u, &pac_u.pek_u.nam_u); if ( NULL == pin_u ) { arena_free(&are_u); @@ -1622,8 +1621,8 @@ _mesa_ef_send(u3_mesa* sam_u, u3_noun las, u3_noun pac) return; } - _mesa_send_bufs(sam_u, NULL, buf_y, len_w, pin_u->adr_u); - _mesa_del_pit(sam_u, &pac_u.pek_u.nam_u); + _mesa_send_bufs(mes_u, NULL, buf_y, len_w, pin_u->adr_u); + _mesa_del_pit(mes_u, &pac_u.pek_u.nam_u); arena_free(&are_u); } else { @@ -1632,10 +1631,10 @@ _mesa_ef_send(u3_mesa* sam_u, u3_noun las, u3_noun pac) u3_mesa_name* nam_u = _mesa_copy_name_alloc(&pac_u.pek_u.nam_u, &res_u->are_u); u3_mesa_request_data* dat_u = &res_u->dat_u; { - dat_u->sam_u = sam_u; + dat_u->mes_u = mes_u; dat_u->her_u = nam_u->her_u; dat_u->nam_u = nam_u; - dat_u->las_u = _mesa_lanes_to_addrs(sam_u, las, &res_u->are_u); + dat_u->las_u = _mesa_lanes_to_addrs(mes_u, las, &res_u->are_u); dat_u->buf_y = buf_y; dat_u->len_w = len_w; } @@ -1643,13 +1642,13 @@ _mesa_ef_send(u3_mesa* sam_u, u3_noun las, u3_noun pac) res_u->ret_y = 9; uv_timer_init(u3L, &res_u->tim_u); } - _mesa_put_request(sam_u, nam_u, (u3_pend_req*)CTAG_WAIT); + _mesa_put_request(mes_u, nam_u, (u3_pend_req*)CTAG_WAIT); res_u->tim_u.data = res_u; #ifdef PACKET_TEST - packet_test(sam_u, "pages.packs"); + packet_test(mes_u, "pages.packs"); #else - _mesa_send_bufs(sam_u, NULL, buf_y, len_w, dat_u->las_u); + _mesa_send_bufs(mes_u, NULL, buf_y, len_w, dat_u->las_u); uv_timer_start(&res_u->tim_u, _mesa_resend_timer_cb, 1000, 0); #endif } @@ -1659,7 +1658,7 @@ _mesa_ef_send(u3_mesa* sam_u, u3_noun las, u3_noun pac) } c3_o -_ames_kick_newt(void* sam_u, u3_noun tag, u3_noun dat); +_ames_kick_newt(void* mes_u, u3_noun tag, u3_noun dat); u3_atom u3_ames_encode_lane(sockaddr_in lan); @@ -1667,18 +1666,21 @@ u3_ames_encode_lane(sockaddr_in lan); void _ames_ef_turf(u3_ames_lamp_state* lam_u, u3_noun tuf); void -_ames_lane_into_cache(u3_auto* sam_u, u3_noun who, u3_noun las); +_ames_lane_into_cache(void* mes_u, u3_noun who, u3_noun las); -static c3_o _mesa_kick(u3_mesa* sam_u, u3_noun tag, u3_noun dat) +static c3_o _mesa_kick(u3_mesa* mes_u, u3_noun tag, u3_noun dat) { c3_o ret_o; switch ( tag ) { default: { ret_o = c3n; } break; + case c3__init: { + ret_o = c3y; + } break; case c3__turf: { - _ames_ef_turf(&sam_u->lam_u, u3k(dat)); + _ames_ef_turf(&mes_u->lam_u, u3k(dat)); ret_o = c3y; } break; case c3__push: { @@ -1688,7 +1690,7 @@ static c3_o _mesa_kick(u3_mesa* sam_u, u3_noun tag, u3_noun dat) ret_o = c3n; } else { // u3l_log(" mesa: send new"); - _mesa_ef_send(sam_u, u3k(las), u3k(pac)); + _mesa_ef_send(mes_u, u3k(las), u3k(pac)); ret_o = c3y; } } break; @@ -1698,17 +1700,17 @@ static c3_o _mesa_kick(u3_mesa* sam_u, u3_noun tag, u3_noun dat) /* u3l_log("mesa: send old %s", tag_c); */ c3_free(tag_c); #endif - ret_o = _ames_kick_newt(sam_u->ame_u, u3k(tag), u3k(dat)); + ret_o = _ames_kick_newt(mes_u->sam_u, u3k(tag), u3k(dat)); } break; case c3__saxo: { - _mesa_ef_saxo(sam_u, u3k(dat)); + _mesa_ef_saxo(mes_u, u3k(dat)); ret_o = c3y; break; } case c3__nail: { u3_noun who = u3k(u3h(dat)); u3_noun las = u3k(u3t(dat)); - _ames_lane_into_cache(sam_u->ame_u, who, las); + _ames_lane_into_cache(mes_u->sam_u, who, las); ret_o = c3y; } break; } @@ -1719,9 +1721,12 @@ static c3_o _mesa_kick(u3_mesa* sam_u, u3_noun tag, u3_noun dat) return ret_o; } +c3_o +_ames_io_kick(void* sam_u, u3_noun wir, u3_noun cad); + static c3_o _mesa_io_kick(u3_auto* car_u, u3_noun wir, u3_noun cad) { - u3_mesa* sam_u = (u3_mesa*)car_u; + u3_mesa* mes_u = (u3_mesa*)car_u; u3_noun tag, dat, i_wir; c3_o ret_o; @@ -1729,54 +1734,67 @@ static c3_o _mesa_io_kick(u3_auto* car_u, u3_noun wir, u3_noun cad) || (c3__ames != i_wir) || (c3n == u3r_cell(cad, &tag, &dat)) ) { - ret_o = c3n; + ret_o = _ames_io_kick(mes_u->sam_u, u3k(wir), u3k(cad)); } else { - ret_o = _mesa_kick(sam_u, u3k(tag), u3k(dat)); + ret_o = _mesa_kick(mes_u, u3k(tag), u3k(dat)); } u3z(wir); u3z(cad); return ret_o; } +u3_noun +_ames_io_info(void* sam_u); + static u3_noun _mesa_io_info(u3_auto* car_u) { - u3_mesa* sam_u = (u3_mesa*)car_u; - return u3i_list( - u3_pier_mase("stun-can-send", sam_u->sun_u.net_o), - u3_pier_mase("stun-working", sam_u->sun_u.wok_o), - u3_none); + u3_mesa* mes_u = (u3_mesa*)car_u; + return u3kb_weld( + _ames_io_info(mes_u->sam_u), + u3i_list( + u3_pier_mase("stun-can-send", mes_u->sun_u.net_o), + u3_pier_mase("stun-working", mes_u->sun_u.wok_o), + u3_none) ); } +void +_ames_io_slog(void* sam_u); + static void _mesa_io_slog(u3_auto* car_u) { - u3_mesa* sam_u = (u3_mesa*)car_u; + u3_mesa* mes_u = (u3_mesa*)car_u; # define FLAG(a) ( (c3y == a) ? "&" : "|" ) u3l_log("mesa is online"); u3l_log(" stun:"); - u3l_log(" working: %s", FLAG(sam_u->sun_u.wok_o)); - u3l_log(" can send: %s", FLAG(sam_u->sun_u.net_o)); + u3l_log(" working: %s", FLAG(mes_u->sun_u.wok_o)); + u3l_log(" can send: %s", FLAG(mes_u->sun_u.net_o)); + _ames_io_slog(mes_u->sam_u); } void -_ames_exit_cb(u3_auto* sam_u); +_ames_exit_cb(void* sam_u); static void _mesa_exit_cb(uv_handle_t* han_u) { - u3_mesa* sam_u = (u3_mesa*)han_u->data; - arena_free(&sam_u->par_u); - _ames_exit_cb(sam_u->ame_u); + u3_mesa* mes_u = (u3_mesa*)han_u->data; + arena_free(&mes_u->par_u); + _ames_exit_cb(mes_u->sam_u); } +void +_ames_io_exit(void* sam_u); + static void _mesa_io_exit(u3_auto* car_u) { - u3_mesa* sam_u = (u3_mesa*)car_u; - uv_timer_stop(&sam_u->tim_u); - sam_u->tim_u.data = sam_u; - uv_close((uv_handle_t*)&sam_u->tim_u, _mesa_exit_cb); - uv_close((uv_handle_t*)&sam_u->sun_u.tim_u, 0); + u3_mesa* mes_u = (u3_mesa*)car_u; + uv_timer_stop(&mes_u->tim_u); + mes_u->tim_u.data = mes_u; + uv_close((uv_handle_t*)&mes_u->tim_u, _mesa_exit_cb); + uv_close((uv_handle_t*)&mes_u->sun_u.tim_u, 0); + _ames_io_exit(mes_u->sam_u); } static void @@ -1789,10 +1807,10 @@ _init_lane_state(u3_lane_state* sat_u) } static void -_init_peer(u3_mesa* sam_u, u3_peer* per_u, u3_ship her_u) +_init_peer(u3_mesa* mes_u, u3_peer* per_u, u3_ship her_u) { memset(per_u, 0, sizeof(*per_u)); - per_u->sam_u = sam_u; + per_u->mes_u = mes_u; per_u->ful_o = c3n; per_u->dan_u = (sockaddr_in){0}; _init_lane_state(&per_u->dir_u); @@ -1834,13 +1852,13 @@ _name_to_jumbo_str(u3_mesa_name* nam_u, c3_y* buf_y) } static u3_mesa_line* -_mesa_get_jumbo_cache(u3_mesa* sam_u, u3_mesa_name* nam_u) +_mesa_get_jumbo_cache(u3_mesa* mes_u, u3_mesa_name* nam_u) { c3_y buf_y[PACT_SIZE]; c3_w len_w = _name_to_jumbo_str(nam_u, buf_y); u3_str str_u = {(c3_c*)buf_y, len_w}; - jum_map_itr itr_u = vt_get(&sam_u->jum_u, str_u); + jum_map_itr itr_u = vt_get(&mes_u->jum_u, str_u); if ( vt_is_end(itr_u) ) { return NULL; } @@ -1848,7 +1866,7 @@ _mesa_get_jumbo_cache(u3_mesa* sam_u, u3_mesa_name* nam_u) } static void -_mesa_put_jumbo_cache(u3_mesa* sam_u, u3_mesa_name* nam_u, u3_mesa_line* lin_u) +_mesa_put_jumbo_cache(u3_mesa* mes_u, u3_mesa_name* nam_u, u3_mesa_line* lin_u) { c3_y* buf_y = c3_malloc(PACT_SIZE); @@ -1857,15 +1875,15 @@ _mesa_put_jumbo_cache(u3_mesa* sam_u, u3_mesa_name* nam_u, u3_mesa_line* lin_u) // CTAG_BLOCK, CTAG_WAIT if ( lin_u > (u3_mesa_line*)2 ) { - if (sam_u->jum_d > JUMBO_CACHE_MAX_SIZE) { - vt_cleanup(&sam_u->jum_u); - sam_u->jum_d = 0; + if (mes_u->jum_d > JUMBO_CACHE_MAX_SIZE) { + vt_cleanup(&mes_u->jum_u); + mes_u->jum_d = 0; } - sam_u->jum_d += lin_u->tob_d; + mes_u->jum_d += lin_u->tob_d; } - jum_map_itr itr_u = vt_insert(&sam_u->jum_u, str_u, lin_u); + jum_map_itr itr_u = vt_insert(&mes_u->jum_u, str_u, lin_u); if ( vt_is_end(itr_u) ) { fprintf(stderr, "mesa: cannot allocate memory for jumbo cache, dying"); @@ -1874,28 +1892,28 @@ _mesa_put_jumbo_cache(u3_mesa* sam_u, u3_mesa_name* nam_u, u3_mesa_line* lin_u) } static void -_mesa_send_pact_single(u3_mesa* sam_u, +_mesa_send_pact_single(u3_mesa* mes_u, sockaddr_in adr_u, u3_mesa_pact* pac_u) { c3_y* buf_y = c3_calloc(PACT_SIZE); c3_w len_w = mesa_etch_pact_to_buf(buf_y, PACT_SIZE, pac_u); - _mesa_send_buf(sam_u, adr_u, buf_y, len_w); + _mesa_send_buf(mes_u, adr_u, buf_y, len_w); } static void -_mesa_send_pact(u3_mesa* sam_u, +_mesa_send_pact(u3_mesa* mes_u, u3_pit_addr* las_u, u3_peer* per_u, // null for response packets u3_mesa_pact* pac_u) { c3_y* buf_y = c3_calloc(PACT_SIZE); c3_w len_w = mesa_etch_pact_to_buf(buf_y, PACT_SIZE, pac_u); - _mesa_send_bufs(sam_u, per_u, buf_y, len_w, las_u); + _mesa_send_bufs(mes_u, per_u, buf_y, len_w, las_u); } static void -_mesa_send_leaf(u3_mesa* sam_u, +_mesa_send_leaf(u3_mesa* mes_u, u3_mesa_line* lin_u, u3_mesa_pact* pac_u, // scratchpad c3_d fra_d, @@ -1925,11 +1943,11 @@ _mesa_send_leaf(u3_mesa* sam_u, // u3l_log(" sending leaf packet, fra_d: %"PRIu64, nam_u->fra_d); #endif // log_pact(pac_u); - _mesa_send_pact_single(sam_u, adr_u, pac_u); + _mesa_send_pact_single(mes_u, adr_u, pac_u); } static void -_mesa_send_piece(u3_mesa* sam_u, u3_mesa_line* lin_u, u3_mesa_name* nam_u, c3_d fra_d, sockaddr_in adr_u) { +_mesa_send_piece(u3_mesa* mes_u, u3_mesa_line* lin_u, u3_mesa_name* nam_u, c3_d fra_d, sockaddr_in adr_u) { u3_mesa_pact pac_u = {0}; u3_mesa_head* hed_u = &pac_u.hed_u; { @@ -1956,20 +1974,20 @@ _mesa_send_piece(u3_mesa* sam_u, u3_mesa_line* lin_u, u3_mesa_name* nam_u, c3_d c3_y* pro_y = c3_malloc(dat_u->len_w); memcpy(pro_y, lin_u->tip_y, dat_u->len_w); dat_u->fra_y = pro_y; - _mesa_send_pact_single(sam_u, adr_u, &pac_u); + _mesa_send_pact_single(mes_u, adr_u, &pac_u); c3_free(pro_y); } // single-fragment message; just send the one data fragment else if ( 0 == fra_d ) { - _mesa_send_leaf(sam_u, lin_u, &pac_u, 0, adr_u); + _mesa_send_leaf(mes_u, lin_u, &pac_u, 0, adr_u); } else { u3l_log("mesa: weird fragment number %"PRIu64, fra_d); } } else { - _mesa_send_leaf(sam_u, lin_u, &pac_u, fra_d, adr_u); + _mesa_send_leaf(mes_u, lin_u, &pac_u, fra_d, adr_u); } } @@ -1978,21 +1996,21 @@ _mesa_page_scry_jumbo_cb(void* vod_p, u3_noun res) { u3_scry_handle* han_u = (u3_scry_handle*)vod_p; u3_mesa_cb_data* dat_u = (u3_mesa_cb_data*)han_u->vod_p; - u3_mesa* sam_u = dat_u->sam_u; + u3_mesa* mes_u = dat_u->mes_u; u3_mesa_name* nam_u = &dat_u->nam_u; u3_weak pag = u3r_at(7, res); if ( u3_none == pag ) { // TODO: mark as dead - u3_mesa_line* lin_u = _mesa_get_jumbo_cache(sam_u, nam_u); + u3_mesa_line* lin_u = _mesa_get_jumbo_cache(mes_u, nam_u); u3z(res); if ( NULL == lin_u ) { arena_free(&han_u->are_u); return; } lin_u = (u3_mesa_line*)CTAG_BLOCK; - _mesa_put_jumbo_cache(sam_u, nam_u, lin_u); + _mesa_put_jumbo_cache(mes_u, nam_u, lin_u); arena_free(&han_u->are_u); return; } @@ -2057,20 +2075,20 @@ _mesa_page_scry_jumbo_cb(void* vod_p, u3_noun res) } - _mesa_put_jumbo_cache(sam_u, nam_u, lin_u); + _mesa_put_jumbo_cache(mes_u, nam_u, lin_u); - u3_pit_entry* ent_u = _mesa_get_pit(sam_u, nam_u); + u3_pit_entry* ent_u = _mesa_get_pit(mes_u, nam_u); if ( NULL != ent_u ) { u3_pit_addr* adr_u = ent_u->adr_u; while (adr_u) { - _mesa_send_piece(sam_u, lin_u, nam_u, 0, adr_u->sdr_u); + _mesa_send_piece(mes_u, lin_u, nam_u, 0, adr_u->sdr_u); adr_u = adr_u->nex_p; } - _mesa_del_pit(sam_u, nam_u); + _mesa_del_pit(mes_u, nam_u); } - /* _mesa_send_jumbo_pieces(sam_u, lin_u, NULL); */ + /* _mesa_send_jumbo_pieces(mes_u, lin_u, NULL); */ u3z(res); arena_free(&han_u->are_u); @@ -2099,13 +2117,13 @@ _saxo_cb(void* vod_p, u3_noun nun) if ( sax != u3_none ) { u3_noun her = u3h(sax); u3_ship her_u = u3_ship_of_noun(her); - u3_peer* new_u = _mesa_get_peer(per_u->sam_u, her_u); + u3_peer* new_u = _mesa_get_peer(per_u->mes_u, her_u); if ( new_u != NULL ) { per_u = new_u; } else { - _mesa_put_peer(per_u->sam_u, her_u, per_u); + _mesa_put_peer(per_u->mes_u, her_u, per_u); } - u3_mesa* sam_u = per_u->sam_u; + u3_mesa* mes_u = per_u->mes_u; u3_noun lam = u3do("rear", u3k(sax)); //u3_assert( c3y == u3a_is_cat(gal) && gal < 256 ); // both atoms guaranteed to be cats, bc we don't call unless forwarding @@ -2120,13 +2138,13 @@ static void _forward_lanes_cb(void* vod_p, u3_noun nun) { u3_peer* per_u = vod_p; - u3_mesa* sam_u = per_u->sam_u; + u3_mesa* mes_u = per_u->mes_u; u3_weak las = u3r_at(7, nun); // u3m_p("_forward_lanes_cb", las); if ( las != u3_none ) { - u3_peer* new_u = _mesa_get_peer(per_u->sam_u, per_u->her_u); + u3_peer* new_u = _mesa_get_peer(per_u->mes_u, per_u->her_u); if ( new_u != NULL ) { per_u = new_u; } @@ -2140,13 +2158,13 @@ _forward_lanes_cb(void* vod_p, u3_noun nun) if ( c3n == per_u->lam_o ) { if ( (c3y == u3r_cell(u3t(las), &sal, &tal)) && (c3y == u3du(sal)) ) { - per_u->dan_u = _mesa_realise_lane(sam_u, u3k(sal)); + per_u->dan_u = _mesa_realise_lane(mes_u, u3k(sal)); } else { per_u->dan_u = (sockaddr_in){0}; } } - _mesa_put_peer(per_u->sam_u, per_u->her_u, per_u); + _mesa_put_peer(per_u->mes_u, per_u->her_u, per_u); } u3z(nun); @@ -2154,18 +2172,18 @@ _forward_lanes_cb(void* vod_p, u3_noun nun) } static void -_meet_peer(u3_mesa* sam_u, u3_peer* per_u) +_meet_peer(u3_mesa* mes_u, u3_peer* per_u) { u3_noun her = u3_ship_to_noun(per_u->her_u); u3_noun gan = u3nc(u3_nul, u3_nul); u3_noun pax = u3nc(u3dc("scot", c3__p, her), u3_nul); - u3_pier_peek_last(sam_u->pir_u, gan, c3__j, c3__saxo, pax, per_u, _saxo_cb); + u3_pier_peek_last(mes_u->pir_u, gan, c3__j, c3__saxo, pax, per_u, _saxo_cb); } static void -_get_peer_lanes(u3_mesa* sam_u, u3_peer* per_u) +_get_peer_lanes(u3_mesa* mes_u, u3_peer* per_u) { u3_noun her = u3_ship_to_noun(per_u->her_u); u3_noun gan = u3nc(u3_nul, u3_nul); @@ -2174,11 +2192,11 @@ _get_peer_lanes(u3_mesa* sam_u, u3_peer* per_u) u3i_string("lanes"), u3_nul); u3m_p("pax", pax); - u3_pier_peek_last(sam_u->pir_u, gan, c3__ax, u3_nul, pax, per_u, _forward_lanes_cb); + u3_pier_peek_last(mes_u->pir_u, gan, c3__ax, u3_nul, pax, per_u, _forward_lanes_cb); } static void -_hear_peer(u3_mesa* sam_u, u3_peer* per_u, sockaddr_in lan_u, c3_o dir_o) +_hear_peer(u3_mesa* mes_u, u3_peer* per_u, sockaddr_in lan_u, c3_o dir_o) { if ( c3y == dir_o ) { if ( c3n == per_u->lam_o ) @@ -2190,7 +2208,7 @@ _hear_peer(u3_mesa* sam_u, u3_peer* per_u, sockaddr_in lan_u, c3_o dir_o) } static void -_mesa_request_next_fragments(u3_mesa* sam_u, +_mesa_request_next_fragments(u3_mesa* mes_u, u3_pend_req* req_u, sockaddr_in lan_u) { @@ -2230,7 +2248,7 @@ _mesa_request_next_fragments(u3_mesa* sam_u, /* _mesa_send(nex_u, lan_u); */ } /* if ( i > 0 ) { */ - /* c3_i sen_i = _mesa_send_buf2(req_u->per_u->sam_u, ads_u, bus_u, int_u, i); */ + /* c3_i sen_i = _mesa_send_buf2(req_u->per_u->mes_u, ads_u, bus_u, int_u, i); */ /* for (c3_w i = 0; i < sen_i; i++) { */ /* c3_w fra_w = nex_d + i; */ /* _mesa_req_pact_sent(req_u, fra_w, now_d); */ @@ -2243,12 +2261,12 @@ _mesa_veri_scry_cb(void* vod_p, u3_noun nun) { u3_mesa_cb_data* ver_u = vod_p; #ifndef PACKET_TEST - u3_pend_req* req_u = _mesa_get_request(ver_u->sam_u, &ver_u->nam_u); + u3_pend_req* req_u = _mesa_get_request(ver_u->mes_u, &ver_u->nam_u); if ( !req_u ) { return; } else if ( c3y == nun ) { // XX - _mesa_request_next_fragments(ver_u->sam_u, req_u, ver_u->lan_u); + _mesa_request_next_fragments(ver_u->mes_u, req_u, ver_u->lan_u); } else if ( c3n == nun ) { u3l_log("mesa: packet auth failed verification"); @@ -2263,18 +2281,18 @@ _mesa_veri_scry_cb(void* vod_p, u3_noun nun) } static void -_mesa_req_pact_init(u3_mesa* sam_u, u3_mesa_pict* pic_u, sockaddr_in lan_u, u3_peer* per_u) +_mesa_req_pact_init(u3_mesa* mes_u, u3_mesa_pict* pic_u, sockaddr_in lan_u, u3_peer* per_u) { - sam_u->tim_d = _get_now_micros(); + mes_u->tim_d = _get_now_micros(); u3_mesa_pact* pac_u = &pic_u->pac_u; u3_mesa_name* nam_u = &pac_u->pag_u.nam_u; u3_mesa_data* dat_u = &pac_u->pag_u.dat_u; - u3_gage* gag_u = _mesa_get_gage(sam_u, nam_u->her_u); + u3_gage* gag_u = _mesa_get_gage(mes_u, nam_u->her_u); if ( gag_u == NULL ) { - gag_u = new(&sam_u->par_u, u3_gage, 1); + gag_u = new(&mes_u->par_u, u3_gage, 1); _init_gage(gag_u); - _mesa_put_gage(sam_u, nam_u->her_u, gag_u); + _mesa_put_gage(mes_u, nam_u->her_u, gag_u); u3_assert( gag_u != NULL ); } @@ -2294,7 +2312,7 @@ _mesa_req_pact_init(u3_mesa* sam_u, u3_mesa_pict* pic_u, sockaddr_in lan_u, u3_p u3_pend_req* req_u = new(&are_u, u3_pend_req, 1); req_u->are_u = are_u; req_u->pic_u = new(&req_u->are_u, u3_mesa_pict, 1); - req_u->pic_u->sam_u = sam_u; + req_u->pic_u->mes_u = mes_u; req_u->pic_u->pac_u.hed_u.typ_y = PACT_PEEK; req_u->pic_u->pac_u.hed_u.pro_y = MESA_VER; req_u->pic_u->pac_u.pek_u.nam_u = *(_mesa_copy_name_alloc(nam_u, &req_u->are_u)); @@ -2344,7 +2362,7 @@ _mesa_req_pact_init(u3_mesa* sam_u, u3_mesa_pict* pic_u, sockaddr_in lan_u, u3_p req_u->per_u = per_u; - _mesa_put_request(sam_u, &req_u->pic_u->pac_u.pek_u.nam_u, req_u); + _mesa_put_request(mes_u, &req_u->pic_u->pac_u.pek_u.nam_u, req_u); _update_resend_timer(req_u); // scry to verify auth @@ -2366,10 +2384,10 @@ _mesa_req_pact_init(u3_mesa* sam_u, u3_mesa_pict* pic_u, sockaddr_in lan_u, u3_p u3_noun pax = _mesa_encode_path(nam_u->pat_s, (c3_y*)nam_u->pat_c); u3_noun sky = u3i_list(typ, her, aut, rut, pax, u3_none); u3_mesa_cb_data* ver_u = c3_malloc(sizeof(u3_mesa_cb_data)); - ver_u->sam_u = sam_u; + ver_u->mes_u = mes_u; ver_u->nam_u = req_u->pic_u->pac_u.pek_u.nam_u; ver_u->lan_u = lan_u; - u3_pier_peek_last(sam_u->pir_u, u3_nul, c3__a, c3__veri, sky, ver_u, _mesa_veri_scry_cb); + u3_pier_peek_last(mes_u->pir_u, u3_nul, c3__a, c3__veri, sky, ver_u, _mesa_veri_scry_cb); } typedef struct _u3_mesa_lane_cb_data { @@ -2423,27 +2441,27 @@ _mesa_add_hop(c3_y hop_y, u3_mesa_head* hed_u, u3_mesa_page_pact* pag_u, sockadd /* } */ static void -_mesa_forward_request(u3_mesa* sam_u, u3_mesa_pict* pic_u, sockaddr_in lan_u) +_mesa_forward_request(u3_mesa* mes_u, u3_mesa_pict* pic_u, sockaddr_in lan_u) { u3_mesa_pact* pac_u = &pic_u->pac_u; - u3_peer* per_u = _mesa_get_peer(sam_u, pac_u->pek_u.nam_u.her_u); + u3_peer* per_u = _mesa_get_peer(mes_u, pac_u->pek_u.nam_u.her_u); if ( !per_u ) { #ifdef MESA_DEBUG c3_c* mes = u3_ship_to_string(pac_u->pek_u.nam_u.her_u); u3l_log("mesa: alien forward for %s; meeting ship", mes); c3_free(mes); #endif - per_u = new(&sam_u->par_u, u3_peer, 1); - _init_peer(sam_u, per_u, pac_u->pek_u.nam_u.her_u); + per_u = new(&mes_u->par_u, u3_peer, 1); + _init_peer(mes_u, per_u, pac_u->pek_u.nam_u.her_u); per_u->her_u = pac_u->pek_u.nam_u.her_u; - _get_peer_lanes(sam_u, per_u); // forward-lanes + _get_peer_lanes(mes_u, per_u); // forward-lanes return; } - if ( c3y == sam_u->for_o && - c3y == u3_ships_equal(sam_u->pir_u->who_u, per_u->lam_u) ) { - // if ( c3y == sam_u->for_o ) { - sockaddr_in lin_u = _mesa_get_direct_lane(sam_u, pac_u->pek_u.nam_u.her_u); + if ( c3y == mes_u->for_o && + c3y == u3_ships_equal(mes_u->pir_u->who_u, per_u->lam_u) ) { + // if ( c3y == mes_u->for_o ) { + sockaddr_in lin_u = _mesa_get_direct_lane(mes_u, pac_u->pek_u.nam_u.her_u); if ( _mesa_is_lane_zero(lin_u) == c3y) { c3_c* shp_c = u3_ship_to_string(pac_u->pek_u.nam_u.her_u); u3l_log("zero lane for %s", shp_c); @@ -2461,7 +2479,7 @@ _mesa_forward_request(u3_mesa* sam_u, u3_mesa_pict* pic_u, sockaddr_in lan_u) u3l_log("mesa: sending packet to %s:%u", sip_c, ntohs(lin_u.sin_port)); #endif - _mesa_add_lane_to_pit(sam_u, &pac_u->pek_u.nam_u, lan_u); + _mesa_add_lane_to_pit(mes_u, &pac_u->pek_u.nam_u, lan_u); _mesa_send(pic_u, lin_u); } } @@ -2475,19 +2493,19 @@ _mesa_hear_page(u3_mesa_pict* pic_u, sockaddr_in lan_u) u3_assert( PACT_PAGE == pic_u->pac_u.hed_u.typ_y ); #endif - u3_mesa* sam_u = pic_u->sam_u; + u3_mesa* mes_u = pic_u->mes_u; u3_mesa_pact* pac_u = &pic_u->pac_u; u3_mesa_name* nam_u = &pac_u->pag_u.nam_u; - c3_o our_o = u3_ships_equal(nam_u->her_u, sam_u->pir_u->who_u); + c3_o our_o = u3_ships_equal(nam_u->her_u, mes_u->pir_u->who_u); - u3_peer* per_u = _mesa_get_peer(sam_u, nam_u->her_u); + u3_peer* per_u = _mesa_get_peer(mes_u, nam_u->her_u); c3_o new_o = c3n; if ( NULL == per_u ) { new_o = c3y; - per_u = new(&sam_u->par_u, u3_peer, 1); - _init_peer(sam_u, per_u, nam_u->her_u); - _meet_peer(sam_u, per_u); + per_u = new(&mes_u->par_u, u3_peer, 1); + _init_peer(mes_u, per_u, nam_u->her_u); + _meet_peer(mes_u, per_u); } c3_o dir_o = __(pac_u->hed_u.hop_y == 0); @@ -2496,16 +2514,16 @@ _mesa_hear_page(u3_mesa_pict* pic_u, sockaddr_in lan_u) // } else { // u3l_log(" received forwarded page"); // } - _hear_peer(sam_u, per_u, lan_u, dir_o); + _hear_peer(mes_u, per_u, lan_u, dir_o); if ( new_o == c3y ) { //u3l_log("new lane is direct %c", c3y == dir_o ? 'y' : 'n'); //_log_lane(&lan_u); } - _mesa_put_peer(sam_u, nam_u->her_u, per_u); + _mesa_put_peer(mes_u, nam_u->her_u, per_u); - u3_pit_entry* pin_u = _mesa_get_pit(sam_u, nam_u); + u3_pit_entry* pin_u = _mesa_get_pit(mes_u, nam_u); if ( NULL != pin_u ) { #ifdef MESA_DEBUG @@ -2520,13 +2538,13 @@ _mesa_hear_page(u3_mesa_pict* pic_u, sockaddr_in lan_u) _mesa_add_hop(pac_u->hed_u.hop_y, &pac_u->hed_u, &pac_u->pag_u, lan_u); - _mesa_send_pact(sam_u, pin_u->adr_u, per_u, pac_u); - _mesa_del_pit(sam_u, nam_u); + _mesa_send_pact(mes_u, pin_u->adr_u, per_u, pac_u); + _mesa_del_pit(mes_u, nam_u); return; } c3_d lev_d = mesa_num_leaves(pac_u->pag_u.dat_u.tob_d); - u3_pend_req* req_u = _mesa_get_request(sam_u, nam_u); + u3_pend_req* req_u = _mesa_get_request(mes_u, nam_u); if ( !req_u ) { return; } @@ -2552,19 +2570,19 @@ _mesa_hear_page(u3_mesa_pict* pic_u, sockaddr_in lan_u) u3_noun wir = u3nc(c3__ames, u3_nul); u3_ovum* ovo = u3_ovum_init(0, c3__ames, wir, cad); - ovo = u3_auto_plan(&sam_u->car_u, ovo); + ovo = u3_auto_plan(&mes_u->car_u, ovo); // early deletion, to avoid injecting retries, // (in case of failure, the retry timer will add it again to the PIT) // - _mesa_del_request(sam_u, nam_u); + _mesa_del_request(mes_u, nam_u); u3_auto_peer(ovo, 0, 0, _mesa_page_bail_cb); return; } c3_d now_d = _get_now_micros(); - _mesa_req_pact_init(sam_u, pic_u, lan_u, per_u); + _mesa_req_pact_init(mes_u, pic_u, lan_u, per_u); return; } @@ -2602,7 +2620,7 @@ _mesa_hear_page(u3_mesa_pict* pic_u, sockaddr_in lan_u) /* c3_d now_d = _get_now_micros(); */ /* u3l_log("%" PRIu64 " kilobytes took %f ms", */ /* req_u->tof_d, */ - /* (now_d - sam_u->tim_d)/1000.0); */ + /* (now_d - mes_u->tim_d)/1000.0); */ // u3l_log("page handling took %"PRIu64, avg_time()); //done = c3y; @@ -2628,15 +2646,15 @@ _mesa_hear_page(u3_mesa_pict* pic_u, sockaddr_in lan_u) } - _mesa_del_request(sam_u, &pac_u->pag_u.nam_u); + _mesa_del_request(mes_u, &pac_u->pag_u.nam_u); - u3_auto_plan(&sam_u->car_u, + u3_auto_plan(&mes_u->car_u, u3_ovum_init(0, c3__ames, u3nc(c3__ames, u3_nul), cad)); #else - _mesa_del_request(sam_u, &pac_u->pag_u.nam_u); + _mesa_del_request(mes_u, &pac_u->pag_u.nam_u); #endif } else if ( req_u->hav_d < lev_d ) { - _mesa_request_next_fragments(sam_u, req_u, lan_u); + _mesa_request_next_fragments(mes_u, req_u, lan_u); } } @@ -2649,11 +2667,11 @@ _mesa_hear_peek(u3_mesa_pict* pic_u, sockaddr_in lan_u) #endif u3_mesa_pact* pac_u = &pic_u->pac_u; - u3_mesa* sam_u = pic_u->sam_u; - c3_o our_o = u3_ships_equal(pac_u->pek_u.nam_u.her_u, sam_u->pir_u->who_u); + u3_mesa* mes_u = pic_u->mes_u; + c3_o our_o = u3_ships_equal(pac_u->pek_u.nam_u.her_u, mes_u->pir_u->who_u); if ( c3n == our_o ) { - _mesa_forward_request(sam_u, pic_u, lan_u); + _mesa_forward_request(mes_u, pic_u, lan_u); return; } @@ -2665,28 +2683,28 @@ _mesa_hear_peek(u3_mesa_pict* pic_u, sockaddr_in lan_u) /* u3l_log("hear peek fra %llu", fra_d); */ // if we have the page, send it - u3_mesa_line* lin_u = _mesa_get_jumbo_cache(sam_u, &pac_u->pek_u.nam_u); + u3_mesa_line* lin_u = _mesa_get_jumbo_cache(mes_u, &pac_u->pek_u.nam_u); if ( ( (u3_mesa_line*)CTAG_WAIT == lin_u )) { return; } if ( ( NULL != lin_u && (u3_mesa_line*)CTAG_BLOCK != lin_u)) { - _mesa_send_piece(sam_u, lin_u, &pac_u->pek_u.nam_u, fra_d, lan_u); + _mesa_send_piece(mes_u, lin_u, &pac_u->pek_u.nam_u, fra_d, lan_u); return; } // record interest - _mesa_add_lane_to_pit(sam_u, &pac_u->pek_u.nam_u, lan_u); + _mesa_add_lane_to_pit(mes_u, &pac_u->pek_u.nam_u, lan_u); // otherwise, if blocked or NULL scry lin_u = (u3_mesa_line*)CTAG_WAIT; /* _mesa_copy_name(&lin_u->nam_u, &pac_u->pek_u.nam_u); // XX */ - _mesa_put_jumbo_cache(sam_u, &pac_u->pek_u.nam_u, lin_u); + _mesa_put_jumbo_cache(mes_u, &pac_u->pek_u.nam_u, lin_u); u3_noun sky = _name_to_jumbo_scry(&pac_u->pek_u.nam_u); - u3_noun our = u3_ship_to_noun(sam_u->car_u.pir_u->who_u); + u3_noun our = u3_ship_to_noun(mes_u->car_u.pir_u->who_u); u3_noun bem = u3nc(u3nt(our, u3_nul, u3nc(c3__ud, 1)), sky); arena are_u = arena_create(sizeof(u3_mesa_cb_data) + 1024); @@ -2694,10 +2712,10 @@ _mesa_hear_peek(u3_mesa_pict* pic_u, sockaddr_in lan_u) han_u->are_u = are_u; u3_mesa_cb_data* dat_u = new(&han_u->are_u, u3_mesa_cb_data, 1); han_u->vod_p = dat_u; - dat_u->sam_u = sam_u; + dat_u->mes_u = mes_u; _mesa_copy_name(&dat_u->nam_u, &pac_u->pek_u.nam_u, &han_u->are_u); - u3_pier_peek(sam_u->car_u.pir_u, u3_nul, u3k(u3nq(1, c3__beam, c3__ax, bem)), han_u, _mesa_page_scry_jumbo_cb); + u3_pier_peek(mes_u->car_u.pir_u, u3_nul, u3k(u3nq(1, c3__beam, c3__ax, bem)), han_u, _mesa_page_scry_jumbo_cb); } static void @@ -2711,37 +2729,37 @@ static void _mesa_hear_poke(u3_mesa_pict* pic_u, sockaddr_in lan_u) { u3_mesa_pact* pac_u = &pic_u->pac_u; - u3_mesa* sam_u = pic_u->sam_u; + u3_mesa* mes_u = pic_u->mes_u; #ifdef MESA_DEBUG u3l_log("mesa: hear_poke()"); u3_assert( PACT_POKE == pac_u->hed_u.typ_y ); #endif - c3_o our_o = u3_ships_equal(pac_u->pek_u.nam_u.her_u, sam_u->pir_u->who_u); + c3_o our_o = u3_ships_equal(pac_u->pek_u.nam_u.her_u, mes_u->pir_u->who_u); if ( c3n == our_o ) { - _mesa_forward_request(sam_u, pic_u, lan_u); + _mesa_forward_request(mes_u, pic_u, lan_u); return; } // TODO check if lane already in pit, drop dupes - _mesa_add_lane_to_pit(sam_u, &pac_u->pek_u.nam_u, lan_u); + _mesa_add_lane_to_pit(mes_u, &pac_u->pek_u.nam_u, lan_u); // XX if this lane management stuff is necessary // it should be deferred to after successful event processing - u3_peer* per_u = _mesa_get_peer(sam_u, pac_u->pok_u.pay_u.her_u); + u3_peer* per_u = _mesa_get_peer(mes_u, pac_u->pok_u.pay_u.her_u); c3_o new_o = c3n; if ( NULL == per_u ) { new_o = c3y; - per_u = new(&sam_u->par_u, u3_peer, 1); - _init_peer(sam_u, per_u, pac_u->pok_u.pay_u.her_u); - _meet_peer(sam_u, per_u); + per_u = new(&mes_u->par_u, u3_peer, 1); + _init_peer(mes_u, per_u, pac_u->pok_u.pay_u.her_u); + _meet_peer(mes_u, per_u); } c3_o dir_o = __(pac_u->hed_u.hop_y == 0); if ( pac_u->hed_u.hop_y == 0 ) { new_o = c3y; - _hear_peer(sam_u, per_u, lan_u, dir_o); + _hear_peer(mes_u, per_u, lan_u, dir_o); // u3l_log("learnt lane"); } else { // u3l_log("received forwarded poke"); @@ -2752,9 +2770,9 @@ _mesa_hear_poke(u3_mesa_pict* pic_u, sockaddr_in lan_u) } // XX _meet_peer, in the _saxo_cb, is already putting the peer in her_p // - _mesa_put_peer(sam_u, pac_u->pok_u.pay_u.her_u, per_u); + _mesa_put_peer(mes_u, pac_u->pok_u.pay_u.her_u, per_u); - u3_pend_req* req_u = _mesa_get_request(sam_u, &pac_u->pok_u.pay_u); + u3_pend_req* req_u = _mesa_get_request(mes_u, &pac_u->pok_u.pay_u); if ( req_u != NULL) { // u3l_log("req pending"); return; @@ -2777,7 +2795,7 @@ _mesa_hear_poke(u3_mesa_pict* pic_u, sockaddr_in lan_u) } u3_ovum* ovo = u3_ovum_init(0, c3__ames, wir, cad); - ovo = u3_auto_plan(&sam_u->car_u, ovo); + ovo = u3_auto_plan(&mes_u->car_u, ovo); // XX check request state for *payload* (in-progress duplicate) assert(pac_u->pok_u.dat_u.tob_d); @@ -2792,7 +2810,7 @@ _ames_hear(void* sam_u, c3_y* hun_y); static void -_mesa_hear(u3_mesa* sam_u, +_mesa_hear(u3_mesa* mes_u, const struct sockaddr_in* lan_u, c3_w len_w, c3_y* hun_y) @@ -2810,15 +2828,15 @@ _mesa_hear(u3_mesa* sam_u, c3_y* han_y = c3_malloc(len_w); memcpy(han_y, hun_y, len_w); - if ( c3y == u3_stun_hear(&sam_u->sun_u, lan_u, len_w, han_y) ) + if ( c3y == u3_stun_hear(&mes_u->sun_u, lan_u, len_w, han_y) ) return; - _ames_hear(sam_u->ame_u, lan_u, len_w, han_y); + _ames_hear(mes_u->sam_u, lan_u, len_w, han_y); return; } - u3_mesa_pict* pic_u = new(&sam_u->are_u, u3_mesa_pict, 1); - pic_u->sam_u = sam_u; + u3_mesa_pict* pic_u = new(&mes_u->are_u, u3_mesa_pict, 1); + pic_u->mes_u = mes_u; c3_c* err_c = mesa_sift_pact_from_buf(&pic_u->pac_u, hun_y, len_w); if ( err_c ) { u3l_log("mesa: hear: sift failed: %s", err_c); @@ -2858,7 +2876,7 @@ static void _mesa_recv_cb(uv_udp_t* wax_u, const struct sockaddr* adr_u, unsigned flg_i) { - u3_mesa* sam_u = (u3_mesa*)wax_u->data; + u3_mesa* mes_u = (u3_mesa*)wax_u->data; const struct sockaddr_in* lan_u = (const struct sockaddr_in*)adr_u; if ( 0 > nrd_i ) { if ( u3C.wag_w & u3o_verbose ) { @@ -2877,11 +2895,15 @@ static void _mesa_recv_cb(uv_udp_t* wax_u, } } +void +_ames_io_talk(void* sam_u); + static void _mesa_io_talk(u3_auto* car_u) { - u3_mesa* sam_u = (u3_mesa*)car_u; - sam_u->dns_c = "urbit.org"; // TODO: receive turf + u3_mesa* mes_u = (u3_mesa*)car_u; + _ames_io_talk(mes_u->sam_u); + mes_u->dns_c = "urbit.org"; // TODO: receive turf { // XX remove [sev_l] // @@ -2890,12 +2912,12 @@ _mesa_io_talk(u3_auto* car_u) u3_auto_plan(car_u, u3_ovum_init(0, c3__a, wir, cad)); } - u3_noun who = u3_ship_to_noun(sam_u->pir_u->who_u); + u3_noun who = u3_ship_to_noun(mes_u->pir_u->who_u); u3_noun rac = u3do("clan:title", u3k(who)); - c3_s por_s = sam_u->pir_u->por_s; + c3_s por_s = mes_u->pir_u->por_s; c3_i ret_i; if ( c3__czar == rac ) { - c3_y num_y = (c3_y)sam_u->pir_u->who_u.hed_d; + c3_y num_y = (c3_y)mes_u->pir_u->who_u.hed_d; c3_s zar_s = _ames_czar_port(num_y); if ( 0 == por_s ) { @@ -2920,7 +2942,7 @@ _mesa_io_talk(u3_auto* car_u) htonl(INADDR_LOOPBACK); add_u.sin_port = htons(por_s); - if ( (ret_i = uv_udp_bind(&sam_u->wax_u, + if ( (ret_i = uv_udp_bind(&mes_u->wax_u, (const struct sockaddr*)&add_u, 0)) != 0 ) { u3l_log("mesa: bind: %s", uv_strerror(ret_i)); @@ -2936,41 +2958,41 @@ _mesa_io_talk(u3_auto* car_u) u3_pier_bail(u3_king_stub()); } - uv_udp_getsockname(&sam_u->wax_u, (struct sockaddr *)&add_u, &add_i); + uv_udp_getsockname(&mes_u->wax_u, (struct sockaddr *)&add_u, &add_i); u3_assert(add_u.sin_port); - sam_u->pir_u->por_s = ntohs(add_u.sin_port); + mes_u->pir_u->por_s = ntohs(add_u.sin_port); } if ( c3y == u3_Host.ops_u.net ) { - u3l_log("mesa: live on %d", sam_u->pir_u->por_s); + u3l_log("mesa: live on %d", mes_u->pir_u->por_s); } else { - u3l_log("mesa: live on %d (localhost only)", sam_u->pir_u->por_s); + u3l_log("mesa: live on %d (localhost only)", mes_u->pir_u->por_s); } - sam_u->wax_u.data = sam_u; - uv_udp_recv_start(&sam_u->wax_u, _mesa_alloc, _mesa_recv_cb); + mes_u->wax_u.data = mes_u; + uv_udp_recv_start(&mes_u->wax_u, _mesa_alloc, _mesa_recv_cb); c3_i rec_i = 2 * 1024 * 1024; - uv_recv_buffer_size((uv_handle_t*)&sam_u->wax_u, &rec_i); + uv_recv_buffer_size((uv_handle_t*)&mes_u->wax_u, &rec_i); - uv_send_buffer_size((uv_handle_t*)&sam_u->wax_u, &rec_i); + uv_send_buffer_size((uv_handle_t*)&mes_u->wax_u, &rec_i); - sam_u->car_u.liv_o = c3y; + mes_u->car_u.liv_o = c3y; //u3z(rac); u3z(who); } static void _mesa_clear_pit(uv_timer_t *tim_u) { - u3_mesa* sam_u = (u3_mesa*)tim_u->data; - pit_map_itr itr_u = vt_first( &sam_u->pit_u ); + u3_mesa* mes_u = (u3_mesa*)tim_u->data; + pit_map_itr itr_u = vt_first( &mes_u->pit_u ); c3_d now_d = _get_now_micros(); while (!vt_is_end(itr_u)) { u3_pit_entry* pit_u = itr_u.data->val; if ( (now_d - pit_u->tim_d) > PIT_EXPIRE_MICROS) { - itr_u = vt_erase_itr(&sam_u->pit_u, itr_u); + itr_u = vt_erase_itr(&mes_u->pit_u, itr_u); } else { itr_u = vt_next(itr_u); } @@ -2980,7 +3002,7 @@ static void _mesa_clear_pit(uv_timer_t *tim_u) /* _mesa_ef_saxo(): handle sponsorship chain notification */ static void -_mesa_ef_saxo(u3_mesa* sam_u, u3_noun zad) +_mesa_ef_saxo(u3_mesa* mes_u, u3_noun zad) { u3_noun daz; @@ -2995,15 +3017,21 @@ _mesa_ef_saxo(u3_mesa* sam_u, u3_noun zad) // if we are a galaxy, don't STUN // - if ( c3n == u3_ships_equal(dad_u, sam_u->pir_u->who_u) ) { - sam_u->sun_u.dad_u = dad_u; - u3_stun_start(&sam_u->sun_u, 0); + if ( c3n == u3_ships_equal(dad_u, mes_u->pir_u->who_u) ) { + mes_u->sun_u.dad_u = dad_u; + u3_stun_start(&mes_u->sun_u, 0); } else { - sam_u->for_o = c3y; + mes_u->for_o = c3y; } u3z(zad); u3z(daz); } + +/* u3_ames_io_init(): initialize ames I/O. +*/ +void* +u3_ames_io_init(void* mes_u); + /* _mesa_io_init(): initialize ames I/O. */ u3_auto* @@ -3012,78 +3040,75 @@ u3_mesa_io_init(u3_pier* pir_u) u3l_log("mesa: INIT"); /* packs = fopen("/home/ec2-user/pages.packs", "rb"); */ arena par_u = arena_create(67108864); - u3_mesa* sam_u = new(&par_u, u3_mesa, 1); - sam_u->par_u = par_u; - sam_u->pir_u = pir_u; + u3_mesa* mes_u = new(&par_u, u3_mesa, 1); + mes_u->par_u = par_u; + mes_u->pir_u = pir_u; arena are_u; are_u.dat = (char*)are_y; are_u.beg = (char*)are_y; are_u.end = (char*)(are_y + 524288); - sam_u->are_u = are_u; + mes_u->are_u = are_u; - sam_u->jum_d = 0; + mes_u->jum_d = 0; - uv_timer_init(u3L, &sam_u->tim_u); + uv_timer_init(u3L, &mes_u->tim_u); // clear pit every 30 seconds - sam_u->tim_u.data = sam_u; - uv_timer_start(&sam_u->tim_u, _mesa_clear_pit, 30000, 30000); + mes_u->tim_u.data = mes_u; + uv_timer_start(&mes_u->tim_u, _mesa_clear_pit, 30000, 30000); - u3_assert( !uv_udp_init_ex(u3L, &sam_u->wax_u, UV_UDP_RECVMMSG) ); - sam_u->wax_u.data = sam_u; + u3_assert( !uv_udp_init_ex(u3L, &mes_u->wax_u, UV_UDP_RECVMMSG) ); + mes_u->wax_u.data = mes_u; - vt_init(&sam_u->pit_u); - vt_init(&sam_u->per_u); - vt_init(&sam_u->gag_u); - vt_init(&sam_u->jum_u); - vt_init(&sam_u->req_u); + vt_init(&mes_u->pit_u); + vt_init(&mes_u->per_u); + vt_init(&mes_u->gag_u); + vt_init(&mes_u->jum_u); + vt_init(&mes_u->req_u); // Disable networking for fake ships // - if ( c3y == sam_u->pir_u->fak_o ) { + if ( c3y == mes_u->pir_u->fak_o ) { u3_Host.ops_u.net = c3n; } - sam_u->for_o = c3n; + mes_u->for_o = c3n; - u3_auto* car_u = &sam_u->car_u; + u3_auto* car_u = &mes_u->car_u; memset(car_u, 0, sizeof(*car_u)); car_u->nam_m = c3__mesa; - car_u->liv_o = c3y; + car_u->liv_o = c3n; car_u->io.talk_f = _mesa_io_talk; car_u->io.info_f = _mesa_io_info; car_u->io.slog_f = _mesa_io_slog; car_u->io.kick_f = _mesa_io_kick; car_u->io.exit_f = _mesa_io_exit; - sam_u->sun_u.car_u = car_u; - sam_u->sun_u.wax_u = &sam_u->wax_u; - sam_u->sun_u.net_o = c3y; - sam_u->sun_u.wok_o = c3n; - uv_timer_init(u3L, &sam_u->sun_u.tim_u); - sam_u->sun_u.tim_u.data = &sam_u->sun_u; - u3l_log("mesa start %p", &sam_u->sun_u); - - sam_u->lam_u.car_u = car_u; - sam_u->lam_u.per_u = &sam_u->per_u; - sam_u->lam_u.dom_o = c3n; + mes_u->sun_u.car_u = car_u; + mes_u->sun_u.wax_u = &mes_u->wax_u; + mes_u->sun_u.net_o = c3y; + mes_u->sun_u.wok_o = c3n; + uv_timer_init(u3L, &mes_u->sun_u.tim_u); + mes_u->sun_u.tim_u.data = &mes_u->sun_u; + u3l_log("mesa start %p", &mes_u->sun_u); - uv_timer_init(u3L, &sam_u->lam_u.tim_u); - sam_u->lam_u.tim_u.data = &sam_u->lam_u; + mes_u->lam_u.car_u = car_u; + mes_u->lam_u.per_u = &mes_u->per_u; + mes_u->lam_u.dom_o = c3n; + uv_timer_init(u3L, &mes_u->lam_u.tim_u); + mes_u->lam_u.tim_u.data = &mes_u->lam_u; - sam_u->ame_u = u3_ames_io_init(pir_u, - &sam_u->wax_u, car_u); - u3_auto_link(car_u, pir_u, sam_u->ame_u); + mes_u->sam_u = u3_ames_io_init(car_u); /*{ u3_noun now; struct timeval tim_u; gettimeofday(&tim_u, 0); now = u3_time_in_tv(&tim_u); - //sam_u->sev_l = u3r_mug(now); + //mes_u->sev_l = u3r_mug(now); u3z(now); }*/ diff --git a/pkg/vere/io/mesa/mesa.h b/pkg/vere/io/mesa/mesa.h index 1747edc3d1..626aa9cb4b 100644 --- a/pkg/vere/io/mesa/mesa.h +++ b/pkg/vere/io/mesa/mesa.h @@ -183,7 +183,7 @@ typedef struct _u3_lane_state { } u3_lane_state; typedef struct _u3_peer { - void* sam_u; // backpointer + void* mes_u; // backpointer u3_ship her_u; // who is this peer c3_o ful_o; // has this been initialized? sockaddr_in dan_u; // direct lane (nullable) diff --git a/pkg/vere/vere.h b/pkg/vere/vere.h index 5a409083f4..6e20309366 100644 --- a/pkg/vere/vere.h +++ b/pkg/vere/vere.h @@ -1251,12 +1251,6 @@ /** Ames, packet networking. **/ - /* u3_ames_io_init(): initialize ames I/O. - */ - u3_auto* - u3_ames_io_init(u3_pier* pir_u, - uv_udp_t* wax_u, - u3_auto* mes_u); /* u3_ames_decode_lane(): destructure lane from noun */ From eb3efeec3e262ddac0f4c5de81bc42998843d6e3 Mon Sep 17 00:00:00 2001 From: ziffee Date: Thu, 3 Apr 2025 13:27:05 -0600 Subject: [PATCH 15/34] ames: natpmp into subdriver --- pkg/vere/build.zig | 1 + pkg/vere/io/ames/ames.c | 144 ---------------------------------------- pkg/vere/io/ames/lamp.c | 18 ++--- pkg/vere/io/ames/lamp.h | 5 +- pkg/vere/io/ames/nat.c | 64 ++++++++++++++++++ pkg/vere/io/ames/nat.h | 11 +++ pkg/vere/io/mesa.c | 99 ++++++++++++++++++++++----- 7 files changed, 170 insertions(+), 172 deletions(-) create mode 100644 pkg/vere/io/ames/nat.c create mode 100644 pkg/vere/io/ames/nat.h diff --git a/pkg/vere/build.zig b/pkg/vere/build.zig index f6dff31c34..dab6ac7c4a 100644 --- a/pkg/vere/build.zig +++ b/pkg/vere/build.zig @@ -207,6 +207,7 @@ const c_source_files = [_][]const u8{ "io/ames/ames.c", "io/ames/stun.c", "io/ames/lamp.c", + "io/ames/nat.c", "io/behn.c", "io/conn.c", "io/cttp.c", diff --git a/pkg/vere/io/ames/ames.c b/pkg/vere/io/ames/ames.c index c17ab4fb7d..4e46e6383b 100644 --- a/pkg/vere/io/ames/ames.c +++ b/pkg/vere/io/ames/ames.c @@ -1,7 +1,6 @@ /// @file #include "vere.h" -#include "mdns.h" #include "io/ames/stun.h" #include "io/ames/lamp.h" #include "io/mesa/mesa.h" @@ -10,7 +9,6 @@ #include "ur/ur.h" #include "zlib.h" -#include "natpmp.h" #include "ent/ent.h" @@ -63,11 +61,6 @@ u3_cue_xeno* sil_u; // cue handle c3_y ver_y; // protocol version struct _u3_panc* pan_u; // outbound packet queue, backward - 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 struct { // config: c3_o see_o; // can scry c3_o fit_o; // filtering active @@ -1780,119 +1773,6 @@ _ames_hear(u3_ames* sam_u, } } -static void natpmp_init(uv_timer_t* handle); - -static void -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; - c3_i err_i = readnatpmpresponseorretry(&sam_u->nat_u.req_u, &response); - if ( NATPMP_TRYAGAIN == err_i ) { - return; - } - - uv_poll_stop(handle); - - 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; - } - - 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; - - c3_i err_i = initnatpmp(&sam_u->nat_u.req_u, 0, 0); - - if (err_i != 0) { - return; - } - - 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); - - 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) -{ - u3z(lud); - u3_ovum_free(egg_u); -} - -/* _ames_put_dear(): send lane to arvo after hearing mdns response -*/ -static void -_ames_put_dear(c3_c* ship, bool fake, c3_w s_addr, c3_s port, void* context) -{ - u3_ames* sam_u = (u3_ames*)context; - - // one is loobean one is boolean - if (fake == sam_u->pir_u->fak_o) { - return; - } - - sockaddr_in lan; - lan.sin_family = AF_INET; - lan.sin_addr.s_addr = s_addr; - lan.sin_port = port; - - u3_noun whu = u3dc("slaw", c3__p, u3i_string(ship)); - - if (u3_nul == whu) { - u3l_log("ames: strange ship from mdns: %s", ship); - return; - } - - u3_noun our = u3_ship_to_noun(sam_u->pir_u->who_u); - if (our == u3t(whu)) { - u3z(whu); - u3z(our); - return; - } - - u3z(our); - - u3_noun wir = u3nc(c3__ames, u3_nul); - u3_noun cad = u3nt(c3__dear, u3k(u3t(whu)), u3nc(c3n, u3_ames_encode_lane(lan))); - - u3_auto_peer( - u3_auto_plan(&sam_u->mes_u->car_u, - u3_ovum_init(0, c3__a, wir, cad)), - 0, 0, _mdns_dear_bail); - u3z(whu); -} - /* _ames_io_start(): initialize ames I/O. */ static void @@ -1934,13 +1814,6 @@ _ames_io_start(u3_ames* sam_u) { c3_c* our_s = u3_ship_to_string(sam_u->pir_u->who_u); - - mdns_init(lan_u.sin_port, !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); } @@ -2116,19 +1989,6 @@ _ames_exit_cb(u3_ames* sam_u) c3_free(sam_u); } -/* _ames_io_exit(): terminate ames I/O. -*/ -void -_ames_io_exit(u3_ames* sam_u) -{ - uv_close((uv_handle_t*)&sam_u->nat_u.tim_u, 0); - - 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); - } -} - /* _ames_io_info(): produce status info. */ u3_noun @@ -2210,10 +2070,6 @@ u3_ames_io_init(u3_mesa_auto* mes_u) sam_u->fig_u.see_o = c3y; sam_u->fig_u.fit_o = c3n; - // initialize libnatpmp - sam_u->nat_u.tim_u.data = sam_u; - uv_timer_init(u3L, &sam_u->nat_u.tim_u); - // enable forwarding on galaxies only u3_noun who = u3_ship_to_noun(sam_u->mes_u->pir_u->who_u); u3_noun rac = u3do("clan:title", who); diff --git a/pkg/vere/io/ames/lamp.c b/pkg/vere/io/ames/lamp.c index a8f6e45af7..7df0b1ab17 100644 --- a/pkg/vere/io/ames/lamp.c +++ b/pkg/vere/io/ames/lamp.c @@ -74,7 +74,7 @@ _ames_lamp_lane(u3_auto* car_u, u3_ship her_u, sockaddr_in* lan_u) typedef struct _lamp_resv { uv_getaddrinfo_t adr_u; - u3_ames_lamp_state* lam_u; + u3_lamp_state* lam_u; u3_peer* per_u; c3_c** dns_c; } _lamp_resv; @@ -82,7 +82,7 @@ typedef struct _lamp_resv { /* _ames_lamp_gone(): galaxy address resolution failed. */ static void -_ames_lamp_gone(u3_ames_lamp_state* lam_u, u3_peer* per_u) +_ames_lamp_gone(u3_lamp_state* lam_u, u3_peer* per_u) { c3_w old_w = per_u->dan_u.sin_addr.s_addr; @@ -94,7 +94,7 @@ _ames_lamp_gone(u3_ames_lamp_state* lam_u, u3_peer* per_u) /* _ames_lamp_here(): galaxy address resolution succeeded. */ static void -_ames_lamp_here(u3_ames_lamp_state* lam_u, u3_peer* per_u, sockaddr_in lan_u, c3_c* dns_c) +_ames_lamp_here(u3_lamp_state* lam_u, u3_peer* per_u, sockaddr_in lan_u, c3_c* dns_c) { c3_w old_w = per_u->dan_u.sin_addr.s_addr; @@ -111,7 +111,7 @@ _ames_lamp_here(u3_ames_lamp_state* lam_u, u3_peer* per_u, sockaddr_in lan_u, c3 } static void -_ames_lamp(u3_ames_lamp_state* lam_u, u3_peer* per_u, c3_c** dns_c); +_ames_lamp(u3_lamp_state* lam_u, u3_peer* per_u, c3_c** dns_c); /* _ames_lamp_cb(): galaxy address resolution callback. */ @@ -122,7 +122,7 @@ _ames_lamp_cb(uv_getaddrinfo_t* adr_u, { struct addrinfo* rai_u = aif_u; _lamp_resv* res_u = (_lamp_resv*)adr_u; - u3_ames_lamp_state* lam_u = res_u->lam_u; + u3_lamp_state* lam_u = res_u->lam_u; u3_peer* per_u = res_u->per_u; while ( rai_u && (AF_INET != rai_u->ai_family) ) { @@ -168,7 +168,7 @@ _ames_lamp_cb(uv_getaddrinfo_t* adr_u, /* _ames_lamp(): single galaxy address resolution. */ static void -_ames_lamp(u3_ames_lamp_state* lam_u, u3_peer* per_u, c3_c** dns_c) +_ames_lamp(u3_lamp_state* lam_u, u3_peer* per_u, c3_c** dns_c) { struct addrinfo hin_u = { .ai_family = AF_INET }; uv_getaddrinfo_t* adr_u; @@ -189,7 +189,7 @@ _ames_lamp(u3_ames_lamp_state* lam_u, u3_peer* per_u, c3_c** dns_c) } static void -_ames_etch_czars(u3_ames_lamp_state* lam_u) { +_ames_etch_czars(u3_lamp_state* lam_u) { for (c3_w i = 0; i < 256; i++) { u3_ship who_u = u3_ship_of_noun(i); u3_peer* per_u = _mesa_gut_peer(lam_u->car_u, who_u); @@ -210,7 +210,7 @@ _ames_etch_czars(u3_ames_lamp_state* lam_u) { void _ames_lamp_all(uv_timer_t* tim_u) { - u3_ames_lamp_state* lam_u = tim_u->data; + u3_lamp_state* lam_u = tim_u->data; // requests still pending if ( lam_u->pen_s ) { @@ -259,7 +259,7 @@ _ames_cmp_turfs(c3_c** a, c3_c** b) { /* _ames_ef_turf(): initialize ames I/O on domain(s). */ void -_ames_ef_turf(u3_ames_lamp_state* lam_u, u3_noun tuf) +_ames_ef_turf(u3_lamp_state* lam_u, u3_noun tuf) { if ( u3_nul != tuf) { c3_w len_w = u3_mcut_hosts(NULL, 0, u3k(tuf)); diff --git a/pkg/vere/io/ames/lamp.h b/pkg/vere/io/ames/lamp.h index b4012b4a08..a873708bfc 100644 --- a/pkg/vere/io/ames/lamp.h +++ b/pkg/vere/io/ames/lamp.h @@ -3,13 +3,12 @@ #define _CZAR_GONE (htonl(UINT32_MAX)) #define NLOCALHOST (htonl(0x7f000001)) - -typedef struct _u3_ames_lamp_state { // +typedef struct _u3_lamp_state { // u3_auto* car_u; per_map* per_u; c3_c** dns_c; // domain c3_o dom_o; // have domain uv_timer_t tim_u; // resolve timer c3_s pen_s; // pending -} u3_ames_lamp_state; // +} u3_lamp_state; // diff --git a/pkg/vere/io/ames/nat.c b/pkg/vere/io/ames/nat.c new file mode 100644 index 0000000000..fe25d2b8d4 --- /dev/null +++ b/pkg/vere/io/ames/nat.c @@ -0,0 +1,64 @@ +#include "nat.h" + +static void +natpmp_cb(uv_poll_t* handle, + c3_i status, + c3_i events) +{ + + if (status != 0) { + return; + } + + u3_natpmp_state* nat_u = handle->data; + + natpmpresp_t response; + c3_i err_i = readnatpmpresponseorretry(&nat_u->req_u, &response); + if ( NATPMP_TRYAGAIN == err_i ) { + return; + } + + uv_poll_stop(handle); + + if ( 0 != err_i ) { + u3l_log("ames: natpmp error %i", err_i); + uv_poll_stop(&nat_u->pol_u); + closenatpmp(&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(&nat_u->req_u); + nat_u->tim_u.data = nat_u; + uv_timer_start(&nat_u->tim_u, natpmp_init, 7200000, 0); +} + +void +natpmp_init(uv_timer_t *handle) +{ + u3_natpmp_state* nat_u = handle->data; + c3_s por_s = nat_u->car_u->pir_u->por_s; + + c3_i err_i = initnatpmp(&nat_u->req_u, 0, 0); + + if (err_i != 0) { + return; + } + + err_i = uv_poll_init(u3L, &nat_u->pol_u, nat_u->req_u.s); + + if (err_i != 0) { + return; + } + + sendnewportmappingrequest(&nat_u->req_u, NATPMP_PROTOCOL_UDP, por_s, por_s, 7200); + + nat_u->pol_u.data = nat_u; + uv_poll_start(&nat_u->pol_u, UV_READABLE, natpmp_cb); +} + + diff --git a/pkg/vere/io/ames/nat.h b/pkg/vere/io/ames/nat.h new file mode 100644 index 0000000000..e888b4d978 --- /dev/null +++ b/pkg/vere/io/ames/nat.h @@ -0,0 +1,11 @@ +#include "vere.h" +#include "natpmp.h" + +typedef struct _u3_natpmp_state { + u3_auto* car_u; + 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 + } u3_natpmp_state; // libnatpmp stuff for port forwarding + // +void natpmp_init(uv_timer_t* handle); diff --git a/pkg/vere/io/mesa.c b/pkg/vere/io/mesa.c index e7e99fa860..4934315762 100644 --- a/pkg/vere/io/mesa.c +++ b/pkg/vere/io/mesa.c @@ -7,6 +7,7 @@ #include "ur/ur.h" #include "ship.h" #include "io/ames/stun.h" +#include "io/ames/nat.h" #include "io/ames/lamp.h" #include "mesa/mesa.h" #include "mesa/bitset.h" @@ -25,6 +26,7 @@ #include #include "lss.h" #include "arena.h" +#include "mdns.h" static c3_o dop_o = c3n; @@ -272,7 +274,8 @@ typedef struct _u3_mesa { arena par_u; // permanent arena uv_timer_t tim_u; // pit clear timer u3_stun_client sun_u; // stun client state - u3_ames_lamp_state lam_u; // stun client state + u3_lamp_state lam_u; // lamp state + u3_natpmp_state nat_u; // natpmp state } u3_mesa; STATIC_ASSERT( @@ -1664,7 +1667,7 @@ u3_atom u3_ames_encode_lane(sockaddr_in lan); void -_ames_ef_turf(u3_ames_lamp_state* lam_u, u3_noun tuf); +_ames_ef_turf(u3_lamp_state* lam_u, u3_noun tuf); void _ames_lane_into_cache(void* mes_u, u3_noun who, u3_noun las); @@ -1783,9 +1786,6 @@ _mesa_exit_cb(uv_handle_t* han_u) { _ames_exit_cb(mes_u->sam_u); } -void -_ames_io_exit(void* sam_u); - static void _mesa_io_exit(u3_auto* car_u) { @@ -1794,7 +1794,12 @@ _mesa_io_exit(u3_auto* car_u) mes_u->tim_u.data = mes_u; uv_close((uv_handle_t*)&mes_u->tim_u, _mesa_exit_cb); uv_close((uv_handle_t*)&mes_u->sun_u.tim_u, 0); - _ames_io_exit(mes_u->sam_u); + + uv_close((uv_handle_t*)&mes_u->nat_u.tim_u, 0); + uv_handle_type handle = uv_handle_get_type((uv_handle_t *)&mes_u->nat_u.pol_u); + if ( UV_UNKNOWN_HANDLE != handle) { + uv_close((uv_handle_t*)&mes_u->nat_u.pol_u, 0); + } } static void @@ -2895,6 +2900,56 @@ static void _mesa_recv_cb(uv_udp_t* wax_u, } } +static void +_mdns_dear_bail(u3_ovum* egg_u, u3_noun lud) +{ + u3z(lud); + u3_ovum_free(egg_u); +} + +/* _mesa_put_dear(): send lane to arvo after hearing mdns response +*/ +static void +_mesa_put_dear(c3_c* ship, bool fake, c3_w s_addr, c3_s port, void* context) +{ + u3_mesa* mes_u = (u3_mesa*)context; + + // one is loobean one is boolean + if (fake == mes_u->pir_u->fak_o) { + return; + } + + sockaddr_in lan; + lan.sin_family = AF_INET; + lan.sin_addr.s_addr = s_addr; + lan.sin_port = port; + + u3_noun whu = u3dc("slaw", c3__p, u3i_string(ship)); + + if (u3_nul == whu) { + u3l_log("ames: strange ship from mdns: %s", ship); + return; + } + + u3_noun our = u3_ship_to_noun(mes_u->pir_u->who_u); + if (our == u3t(whu)) { + u3z(whu); + u3z(our); + return; + } + + u3z(our); + + u3_noun wir = u3nc(c3__ames, u3_nul); + u3_noun cad = u3nt(c3__dear, u3k(u3t(whu)), u3nc(c3n, u3_ames_encode_lane(lan))); + + u3_auto_peer( + u3_auto_plan(&mes_u->car_u, + u3_ovum_init(0, c3__a, wir, cad)), + 0, 0, _mdns_dear_bail); + u3z(whu); +} + void _ames_io_talk(void* sam_u); @@ -2932,18 +2987,18 @@ _mesa_io_talk(u3_auto* car_u) // Bind and stuff. { - struct sockaddr_in add_u; - c3_i add_i = sizeof(add_u); + struct sockaddr_in lan_u; + c3_i add_i = sizeof(lan_u); - memset(&add_u, 0, sizeof(add_u)); - add_u.sin_family = AF_INET; - add_u.sin_addr.s_addr = _(u3_Host.ops_u.net) ? + memset(&lan_u, 0, sizeof(lan_u)); + lan_u.sin_family = AF_INET; + lan_u.sin_addr.s_addr = _(u3_Host.ops_u.net) ? htonl(INADDR_ANY) : htonl(INADDR_LOOPBACK); - add_u.sin_port = htons(por_s); + lan_u.sin_port = htons(por_s); if ( (ret_i = uv_udp_bind(&mes_u->wax_u, - (const struct sockaddr*)&add_u, 0)) != 0 ) + (const struct sockaddr*)&lan_u, 0)) != 0 ) { u3l_log("mesa: bind: %s", uv_strerror(ret_i)); @@ -2958,10 +3013,13 @@ _mesa_io_talk(u3_auto* car_u) u3_pier_bail(u3_king_stub()); } - uv_udp_getsockname(&mes_u->wax_u, (struct sockaddr *)&add_u, &add_i); - u3_assert(add_u.sin_port); + uv_udp_getsockname(&mes_u->wax_u, (struct sockaddr *)&lan_u, &add_i); + u3_assert(lan_u.sin_port); - mes_u->pir_u->por_s = ntohs(add_u.sin_port); + mes_u->pir_u->por_s = ntohs(lan_u.sin_port); + c3_c* our_s = u3_ship_to_string(mes_u->pir_u->who_u); + mdns_init(lan_u.sin_port, !mes_u->pir_u->fak_o, our_s, _mesa_put_dear, (void *)mes_u); + c3_free(our_s); } if ( c3y == u3_Host.ops_u.net ) { u3l_log("mesa: live on %d", mes_u->pir_u->por_s); @@ -2970,6 +3028,10 @@ _mesa_io_talk(u3_auto* car_u) u3l_log("mesa: live on %d (localhost only)", mes_u->pir_u->por_s); } + if ( c3n == mes_u->pir_u->fak_o ) { + uv_timer_start(&mes_u->nat_u.tim_u, natpmp_init, 0, 0); + } + mes_u->wax_u.data = mes_u; uv_udp_recv_start(&mes_u->wax_u, _mesa_alloc, _mesa_recv_cb); @@ -3100,8 +3162,13 @@ u3_mesa_io_init(u3_pier* pir_u) uv_timer_init(u3L, &mes_u->lam_u.tim_u); mes_u->lam_u.tim_u.data = &mes_u->lam_u; + // initialize libnatpmp + mes_u->nat_u.car_u = car_u; + mes_u->nat_u.tim_u.data = &mes_u->nat_u; + uv_timer_init(u3L, &mes_u->nat_u.tim_u); mes_u->sam_u = u3_ames_io_init(car_u); + /*{ u3_noun now; struct timeval tim_u; From af82ef540008217df99cb17cf081eab00046b463 Mon Sep 17 00:00:00 2001 From: ziffee Date: Thu, 3 Apr 2025 14:54:55 -0600 Subject: [PATCH 16/34] ames: cleanup --- pkg/vere/io/ames/ames.c | 56 +++-------------------------------------- pkg/vere/io/ames/lamp.c | 21 +++++++++++++--- pkg/vere/io/mesa.c | 1 + 3 files changed, 21 insertions(+), 57 deletions(-) diff --git a/pkg/vere/io/ames/ames.c b/pkg/vere/io/ames/ames.c index 4e46e6383b..ddf9a8c2a5 100644 --- a/pkg/vere/io/ames/ames.c +++ b/pkg/vere/io/ames/ames.c @@ -27,6 +27,7 @@ #define QUEUE_MAX 30 // max number of packets in queue #define DIRECT_ROUTE_TIMEOUT_MICROS 120000000 + typedef struct _u3_ames u3_ames; typedef struct _u3_mesa_auto { u3_auto car_u; @@ -797,6 +798,7 @@ _ames_lane_into_cache(u3_ames* sam_u, u3_noun who, u3_noun las) { u3_ship who_u = u3_ship_of_noun(who); u3_peer* per_u = _mesa_gut_peer(sam_u->mes_u, who_u); + if ( c3y == per_u->lam_o ) return per_u; // XX the format of the lane %nail gives is (list (each @p address)) @@ -822,6 +824,7 @@ _ames_lane_into_cache(u3_ames* sam_u, u3_noun who, u3_noun las) per_u->dan_u = (sockaddr_in){0}; } else { per_u->lam_o = c3y; + per_u->dan_u = u3_ames_decode_lane(u3k(u3t(lan))); } per_u->lam_u = who_u; } @@ -1773,57 +1776,6 @@ _ames_hear(u3_ames* sam_u, } } -/* _ames_io_start(): initialize ames I/O. -*/ -static void -_ames_io_start(u3_ames* sam_u) -{ - c3_s por_s = sam_u->pir_u->por_s; - u3_noun who = u3_ship_to_noun(sam_u->pir_u->who_u); - //c3_o zar_o = _ames_is_czar(who); - c3_i ret_i; - - - //if ( c3y == zar_o ) { - // c3_y num_y = (c3_y)sam_u->pir_u->who_u.hed_d; - // c3_s zar_s = _ames_czar_port(num_y); - - // if ( 0 == por_s ) { - // por_s = zar_s; - // } - // else if ( por_s != zar_s ) { - // u3l_log("ames: czar: overriding port %d with -p %d", zar_s, por_s); - // u3l_log("ames: czar: WARNING: %d required for discoverability", zar_s); - // } - //} - struct sockaddr_in lan_u = {0}; - lan_u.sin_family = AF_INET; - lan_u.sin_addr.s_addr = _(u3_Host.ops_u.net) ? - htonl(INADDR_ANY) : - htonl(INADDR_LOOPBACK); - lan_u.sin_port = htons(por_s); - u3l_log("ames: skipping port: %u", por_s); - - if ( c3y == u3_Host.ops_u.net ) { - u3l_log("ames: live on %d", sam_u->pir_u->por_s); - } - else { - u3l_log("ames: live on %d (localhost only)", sam_u->pir_u->por_s); - } - - { - - c3_c* our_s = u3_ship_to_string(sam_u->pir_u->who_u); - c3_free(our_s); - } - - /* - uv_udp_recv_start(&sam_u->wax_u, _ames_alloc, _ames_recv_cb); - */ - - u3z(who); -} - /* _ames_prot_scry_cb(): receive ames protocol version */ static void @@ -1861,8 +1813,6 @@ _ames_prot_scry_cb(void* vod_p, u3_noun nun) void _ames_io_talk(u3_ames* sam_u) { - _ames_io_start(sam_u); - // send born event // // { diff --git a/pkg/vere/io/ames/lamp.c b/pkg/vere/io/ames/lamp.c index 7df0b1ab17..1c4651b4cb 100644 --- a/pkg/vere/io/ames/lamp.c +++ b/pkg/vere/io/ames/lamp.c @@ -198,16 +198,18 @@ _ames_etch_czars(u3_lamp_state* lam_u) { per_u->dan_u.sin_port = _ames_czar_port(i); c3_c* who_c = u3_ship_to_string(who_u); c3_w len_w = u3_mpef_turfs(NULL, 0, who_c + 1, lam_u->dns_c); - if (per_u->dns_c) c3_free(per_u->dns_c); - c3_c** dns_c = per_u->dns_c = c3_malloc(len_w); - u3_mpef_turfs((c3_c*)dns_c, 0, who_c + 1, lam_u->dns_c); + if (0 != len_w) { + if (per_u->dns_c) c3_free(per_u->dns_c); + c3_c** dns_c = per_u->dns_c = c3_malloc(len_w); + u3_mpef_turfs((c3_c*)dns_c, 0, who_c + 1, lam_u->dns_c); + } c3_free(who_c); } } /* _ames_lamp_all(): galaxy address resolution. */ -void +static void _ames_lamp_all(uv_timer_t* tim_u) { u3_lamp_state* lam_u = tim_u->data; @@ -256,6 +258,17 @@ _ames_cmp_turfs(c3_c** a, c3_c** b) { return c3y; } +void _ames_init_czars(u3_lamp_state* lam_u) +{ + for (c3_w i = 0; i < 256; i++) { + u3_ship who_u = u3_ship_of_noun(i); + u3_peer* per_u = _mesa_gut_peer(lam_u->car_u, who_u); + per_u->lam_o = c3y; + per_u->dan_u.sin_family = AF_INET; + per_u->dan_u.sin_port = _ames_czar_port(i); + } +} + /* _ames_ef_turf(): initialize ames I/O on domain(s). */ void diff --git a/pkg/vere/io/mesa.c b/pkg/vere/io/mesa.c index 4934315762..8b2d92ced7 100644 --- a/pkg/vere/io/mesa.c +++ b/pkg/vere/io/mesa.c @@ -3161,6 +3161,7 @@ u3_mesa_io_init(u3_pier* pir_u) uv_timer_init(u3L, &mes_u->lam_u.tim_u); mes_u->lam_u.tim_u.data = &mes_u->lam_u; + _ames_init_czars(&mes_u->lam_u); // initialize libnatpmp mes_u->nat_u.car_u = car_u; From 3a72007b3b0b56b2ee3bfc3b67dd39770d3038ad Mon Sep 17 00:00:00 2001 From: ziffee Date: Fri, 4 Apr 2025 10:07:56 -0600 Subject: [PATCH 17/34] ames: fix _ames_cmp_turfs and add %fief effect --- pkg/c3/motes.h | 1 + pkg/vere/io/ames/lamp.c | 77 +++++++++++++++++++++++++++++++++++++---- pkg/vere/io/mesa.c | 13 ++++++- 3 files changed, 84 insertions(+), 7 deletions(-) diff --git a/pkg/c3/motes.h b/pkg/c3/motes.h index 86cdccbb20..702882d9fe 100644 --- a/pkg/c3/motes.h +++ b/pkg/c3/motes.h @@ -419,6 +419,7 @@ # define c3__fent c3_s4('f','e','n','t') # define c3__ferd c3_s4('f','e','r','d') # define c3__fhex c3_s4('f','h','e','x') +# define c3__fief c3_s4('f','i','e','f') # define c3__file c3_s4('f','i','l','e') # define c3__film c3_s4('f','i','l','m') # define c3__find c3_s4('f','i','n','d') diff --git a/pkg/vere/io/ames/lamp.c b/pkg/vere/io/ames/lamp.c index 1c4651b4cb..806d52ee58 100644 --- a/pkg/vere/io/ames/lamp.c +++ b/pkg/vere/io/ames/lamp.c @@ -243,12 +243,19 @@ _ames_lamp_all(uv_timer_t* tim_u) static c3_o _ames_cmp_turfs(c3_c** a, c3_c** b) { - while (a != NULL || b != NULL) { - if (a == NULL) { - if (b == NULL) return c3y; + if (a == NULL) { + if (b == NULL) return c3y; + return c3n; + } + if (b == NULL) { + return c3n; + } + while (*a != NULL || *b != NULL) { + if (*a == NULL) { + if (*b == NULL) return c3y; return c3n; } - if (b == NULL) { + if (*b == NULL) { return c3n; } if (0 != strcmp(*a, *b)) return 0; @@ -258,7 +265,8 @@ _ames_cmp_turfs(c3_c** a, c3_c** b) { return c3y; } -void _ames_init_czars(u3_lamp_state* lam_u) +void +_ames_init_czars(u3_lamp_state* lam_u) { for (c3_w i = 0; i < 256; i++) { u3_ship who_u = u3_ship_of_noun(i); @@ -278,11 +286,11 @@ _ames_ef_turf(u3_lamp_state* lam_u, u3_noun tuf) c3_w len_w = u3_mcut_hosts(NULL, 0, u3k(tuf)); if (len_w == 0) { // todo: clear? + u3z(tuf); return; } c3_c** dns_c = c3_malloc(len_w); u3_mcut_hosts((c3_c*)dns_c, 0, u3k(tuf)); - _ames_cmp_turfs(dns_c, lam_u->dns_c); if ( c3n == _ames_cmp_turfs(dns_c, lam_u->dns_c) ) { c3_free(lam_u->dns_c); lam_u->dns_c = dns_c; @@ -300,4 +308,61 @@ _ames_ef_turf(u3_lamp_state* lam_u, u3_noun tuf) //if ( c3n == lam_u->car_u.liv_o ) { // _ames_io_start(lam_u); //} + u3z(tuf); +} + +void +_ames_fief(u3_lamp_state* lam_u, u3_noun fef) { + u3_noun who, q_fef; + u3x_cell(fef, &who, &q_fef); + u3_ship who_u = u3_ship_of_noun(who); + u3_peer* per_u = _mesa_gut_peer(lam_u->car_u, who_u); + if ( u3_nul == q_fef ) { + per_u->lam_o = c3n; + } + u3_noun tag, val; + u3x_cell(u3t(q_fef), &tag, &val); + switch ( tag ) { + default: { + u3l_log("ames: inscrutable fief"); + } break; + case c3__turf: { + u3_noun tuf = val; + if ( u3_nul == tuf ) break; + c3_w len_w = u3_mcut_hosts(NULL, 0, u3k(tuf)); + if (len_w == 0) { + // todo: clear? + break; + } + c3_c** dns_c = c3_malloc(len_w); + u3_mcut_hosts((c3_c*)dns_c, 0, u3k(tuf)); + if ( c3n == _ames_cmp_turfs(dns_c, per_u->dns_c) ) { + if ( NULL != per_u->dns_c ) + c3_free(per_u->dns_c); + per_u->dns_c = dns_c; + lam_u->pen_s++; + _ames_lamp(lam_u, per_u, per_u->dns_c); + } + } break; + case c3__if: { + u3_noun pip, por; + u3x_cell(val, &pip, &por); + per_u->dan_u.sin_family = AF_INET; + per_u->dan_u.sin_addr.s_addr = htonl(u3r_word(0, pip)); + per_u->dan_u.sin_port = htons(por); + } break; + } + u3z(fef); +} + +void +_ames_ef_fief(u3_lamp_state* lam_u, u3_noun fef) +{ + u3_noun _fef = fef = u3kdi_tap(fef); + while ( u3_nul != fef ) { + _ames_fief(lam_u, u3h(fef)); + fef = u3t(fef); + } + u3z(_fef); } + diff --git a/pkg/vere/io/mesa.c b/pkg/vere/io/mesa.c index 8b2d92ced7..49745dab27 100644 --- a/pkg/vere/io/mesa.c +++ b/pkg/vere/io/mesa.c @@ -1358,7 +1358,7 @@ _mesa_req_pact_done(u3_pend_req* req_u, c3_s _ames_czar_port(c3_y imp_y); -static sockaddr_in +sockaddr_in _mesa_realise_lane(u3_mesa* mes_u, u3_noun lan) { sockaddr_in lan_u = {0}; lan_u.sin_family = AF_INET; @@ -1668,6 +1668,10 @@ u3_ames_encode_lane(sockaddr_in lan); void _ames_ef_turf(u3_lamp_state* lam_u, u3_noun tuf); + +void +_ames_ef_fief(u3_lamp_state* lam_u, u3_noun fef); + void _ames_lane_into_cache(void* mes_u, u3_noun who, u3_noun las); @@ -1686,6 +1690,10 @@ static c3_o _mesa_kick(u3_mesa* mes_u, u3_noun tag, u3_noun dat) _ames_ef_turf(&mes_u->lam_u, u3k(dat)); ret_o = c3y; } break; + case c3__fief: { + _ames_ef_fief(&mes_u->lam_u, u3k(dat)); + ret_o = c3y; + } break; case c3__push: { u3_noun las, pac; if ( c3n == u3r_cell(dat, &las, &pac) ) { @@ -3094,6 +3102,9 @@ _mesa_ef_saxo(u3_mesa* mes_u, u3_noun zad) void* u3_ames_io_init(void* mes_u); +void +_ames_init_czars(u3_lamp_state* lam_u); + /* _mesa_io_init(): initialize ames I/O. */ u3_auto* From 4f13232e76e67c4f5d851ed887e075cb3afc3b0c Mon Sep 17 00:00:00 2001 From: ziffee Date: Fri, 4 Apr 2025 14:11:00 -0600 Subject: [PATCH 18/34] ames: non-czar turfs should come with port --- pkg/vere/io/ames/lamp.c | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/pkg/vere/io/ames/lamp.c b/pkg/vere/io/ames/lamp.c index 806d52ee58..88e616b0bf 100644 --- a/pkg/vere/io/ames/lamp.c +++ b/pkg/vere/io/ames/lamp.c @@ -327,7 +327,10 @@ _ames_fief(u3_lamp_state* lam_u, u3_noun fef) { u3l_log("ames: inscrutable fief"); } break; case c3__turf: { - u3_noun tuf = val; + u3_noun tuf, por; + u3x_cell(val, &tuf, &por); + per_u->dan_u.sin_family = AF_INET; + per_u->dan_u.sin_port = htons(por); if ( u3_nul == tuf ) break; c3_w len_w = u3_mcut_hosts(NULL, 0, u3k(tuf)); if (len_w == 0) { From ca388878700e3b4116724a8d50277335b96cf162 Mon Sep 17 00:00:00 2001 From: ziffee Date: Sun, 6 Apr 2025 16:46:21 -0600 Subject: [PATCH 19/34] ames: lamp fixes --- pkg/vere/io/ames/lamp.c | 40 +++++++++++++++++++++++++++++----------- pkg/vere/io/mesa.c | 4 ++-- 2 files changed, 31 insertions(+), 13 deletions(-) diff --git a/pkg/vere/io/ames/lamp.c b/pkg/vere/io/ames/lamp.c index 88e616b0bf..5e584edffd 100644 --- a/pkg/vere/io/ames/lamp.c +++ b/pkg/vere/io/ames/lamp.c @@ -4,6 +4,10 @@ c3_o _mesa_is_lane_zero(sockaddr_in lan_u); +c3_o +_mesa_lanes_equal(sockaddr_in lan_u, sockaddr_in lon_u); + + /* _ames_czar_port(): udp port for galaxy. */ c3_s @@ -96,17 +100,25 @@ _ames_lamp_gone(u3_lamp_state* lam_u, u3_peer* per_u) static void _ames_lamp_here(u3_lamp_state* lam_u, u3_peer* per_u, sockaddr_in lan_u, c3_c* dns_c) { - c3_w old_w = per_u->dan_u.sin_addr.s_addr; - - if ( lan_u.sin_addr.s_addr != old_w ) { + if ( c3n == _mesa_lanes_equal(lan_u, per_u->dan_u) ) { c3_w nip_w = lan_u.sin_addr.s_addr; c3_c nip_c[INET_ADDRSTRLEN]; inet_ntop(AF_INET, &nip_w, nip_c, INET_ADDRSTRLEN); - u3l_log("ames: lamp %s ip .%s", dns_c, nip_c); + c3_c* who_c = u3_ship_to_string(per_u->her_u); + if ( dns_c ) + u3l_log("ames: lamp %s domain %s ip .%s port %u", + who_c, dns_c, + nip_c, ntohs(lan_u.sin_port)); + else + u3l_log("ames: lamp %s static ip .%s port %u", + who_c, + nip_c, ntohs(lan_u.sin_port)); + + c3_free(who_c); } - per_u->dan_u.sin_addr.s_addr = lan_u.sin_addr.s_addr; + per_u->dan_u = lan_u; per_u->log_o = c3n; } @@ -130,8 +142,12 @@ _ames_lamp_cb(uv_getaddrinfo_t* adr_u, } if ( rai_u && rai_u->ai_addr ) { - struct sockaddr_in* lan_u = (void*)rai_u->ai_addr; - _ames_lamp_here(lam_u, per_u, *lan_u, *res_u->dns_c); + struct sockaddr_in* adr_u = (void*)rai_u->ai_addr; + sockaddr_in lan_u = {0}; + lan_u.sin_family = AF_INET; + lan_u.sin_port = per_u->dan_u.sin_port; + lan_u.sin_addr.s_addr = adr_u->sin_addr.s_addr; + _ames_lamp_here(lam_u, per_u, lan_u, *res_u->dns_c); lam_u->pen_s--; uv_freeaddrinfo(aif_u); c3_free(res_u); @@ -350,9 +366,11 @@ _ames_fief(u3_lamp_state* lam_u, u3_noun fef) { case c3__if: { u3_noun pip, por; u3x_cell(val, &pip, &por); - per_u->dan_u.sin_family = AF_INET; - per_u->dan_u.sin_addr.s_addr = htonl(u3r_word(0, pip)); - per_u->dan_u.sin_port = htons(por); + sockaddr_in lan_u; + lan_u.sin_family = AF_INET; + lan_u.sin_addr.s_addr = htonl(u3r_word(0, pip)); + lan_u.sin_port = htons(por); + _ames_lamp_here(lam_u, per_u, lan_u, NULL); } break; } u3z(fef); @@ -363,7 +381,7 @@ _ames_ef_fief(u3_lamp_state* lam_u, u3_noun fef) { u3_noun _fef = fef = u3kdi_tap(fef); while ( u3_nul != fef ) { - _ames_fief(lam_u, u3h(fef)); + _ames_fief(lam_u, u3k(u3h(fef))); fef = u3t(fef); } u3z(_fef); diff --git a/pkg/vere/io/mesa.c b/pkg/vere/io/mesa.c index 49745dab27..1b7d4b9896 100644 --- a/pkg/vere/io/mesa.c +++ b/pkg/vere/io/mesa.c @@ -694,7 +694,7 @@ _mesa_get_direct_lane(u3_mesa* mes_u, u3_ship her_u) return adr_u; } -static c3_o +c3_o _mesa_lanes_equal(sockaddr_in lan_u, sockaddr_in lon_u) { return __((lan_u.sin_addr.s_addr == lon_u.sin_addr.s_addr) && (lan_u.sin_port == lon_u.sin_port)); @@ -1827,7 +1827,7 @@ _init_peer(u3_mesa* mes_u, u3_peer* per_u, u3_ship her_u) per_u->ful_o = c3n; per_u->dan_u = (sockaddr_in){0}; _init_lane_state(&per_u->dir_u); - per_u->her_u = (u3_ship){0}; + per_u->her_u = her_u; _init_lane_state(&per_u->ind_u); } From bad6e92690a298537b54bcdc10eaffc621ffd0c9 Mon Sep 17 00:00:00 2001 From: ziffee Date: Mon, 7 Apr 2025 13:56:21 -0600 Subject: [PATCH 20/34] ames: fix _realise_lane for indirect atoms --- pkg/vere/io/mesa.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pkg/vere/io/mesa.c b/pkg/vere/io/mesa.c index 1b7d4b9896..23c4825a84 100644 --- a/pkg/vere/io/mesa.c +++ b/pkg/vere/io/mesa.c @@ -1363,7 +1363,7 @@ _mesa_realise_lane(u3_mesa* mes_u, u3_noun lan) { sockaddr_in lan_u = {0}; lan_u.sin_family = AF_INET; - if ( c3y == u3a_is_cat(lan) ) { + if ( c3y == u3a_is_atom(lan) ) { lan_u = _mesa_get_direct_lane(mes_u, u3_ship_of_noun(lan)); // u3_assert( lan < 256 ); if ( (c3n == u3_Host.ops_u.net) ) { From a6921af4b19d90037b6d81b20ebfba29fa908d24 Mon Sep 17 00:00:00 2001 From: ziffee Date: Mon, 7 Apr 2025 14:05:26 -0600 Subject: [PATCH 21/34] ames: more comet fixes --- pkg/vere/io/mesa.c | 3 --- 1 file changed, 3 deletions(-) diff --git a/pkg/vere/io/mesa.c b/pkg/vere/io/mesa.c index 23c4825a84..9ea0a26195 100644 --- a/pkg/vere/io/mesa.c +++ b/pkg/vere/io/mesa.c @@ -1368,9 +1368,6 @@ _mesa_realise_lane(u3_mesa* mes_u, u3_noun lan) { // u3_assert( lan < 256 ); if ( (c3n == u3_Host.ops_u.net) ) { lan_u.sin_addr.s_addr = htonl(0x7f000001); - } else if (256 <= lan) { - u3l_log("local comet sponsors no!"); - u3_king_bail(); } } else { u3_noun tag, pip, por; From 36a823bc5bcce126bd4f6aee43b017041c9d6b74 Mon Sep 17 00:00:00 2001 From: ziffee Date: Mon, 7 Apr 2025 17:10:39 -0600 Subject: [PATCH 22/34] mesa: c3n initialize lam_o --- pkg/vere/io/mesa.c | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/pkg/vere/io/mesa.c b/pkg/vere/io/mesa.c index 9ea0a26195..18bf3c648d 100644 --- a/pkg/vere/io/mesa.c +++ b/pkg/vere/io/mesa.c @@ -1822,6 +1822,7 @@ _init_peer(u3_mesa* mes_u, u3_peer* per_u, u3_ship her_u) memset(per_u, 0, sizeof(*per_u)); per_u->mes_u = mes_u; per_u->ful_o = c3n; + per_u->lam_o = c3n; per_u->dan_u = (sockaddr_in){0}; _init_lane_state(&per_u->dir_u); per_u->her_u = her_u; @@ -3087,8 +3088,14 @@ _mesa_ef_saxo(u3_mesa* mes_u, u3_noun zad) if ( c3n == u3_ships_equal(dad_u, mes_u->pir_u->who_u) ) { mes_u->sun_u.dad_u = dad_u; u3_stun_start(&mes_u->sun_u, 0); +#ifdef MESA_DEBUG + u3l_log("mesa: starting stun"); +#endif } else { mes_u->for_o = c3y; +#ifdef MESA_DEBUG + u3l_log("mesa: enabling forwarding"); +#endif } u3z(zad); u3z(daz); From bf313ed4f95dcf5ea2534c1bb7569c8b581b9464 Mon Sep 17 00:00:00 2001 From: ziffee Date: Mon, 7 Apr 2025 20:14:02 -0600 Subject: [PATCH 23/34] ames: fix uv_close cleanup for mesa, set lam_o on %fief --- pkg/vere/io/ames/lamp.c | 2 ++ pkg/vere/io/mesa.c | 4 ++-- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/pkg/vere/io/ames/lamp.c b/pkg/vere/io/ames/lamp.c index 5e584edffd..46b59f41c8 100644 --- a/pkg/vere/io/ames/lamp.c +++ b/pkg/vere/io/ames/lamp.c @@ -335,6 +335,8 @@ _ames_fief(u3_lamp_state* lam_u, u3_noun fef) { u3_peer* per_u = _mesa_gut_peer(lam_u->car_u, who_u); if ( u3_nul == q_fef ) { per_u->lam_o = c3n; + } else { + per_u->lam_o = c3y; } u3_noun tag, val; u3x_cell(u3t(q_fef), &tag, &val); diff --git a/pkg/vere/io/mesa.c b/pkg/vere/io/mesa.c index 18bf3c648d..f47f1e1be7 100644 --- a/pkg/vere/io/mesa.c +++ b/pkg/vere/io/mesa.c @@ -1787,8 +1787,8 @@ _ames_exit_cb(void* sam_u); static void _mesa_exit_cb(uv_handle_t* han_u) { u3_mesa* mes_u = (u3_mesa*)han_u->data; - arena_free(&mes_u->par_u); _ames_exit_cb(mes_u->sam_u); + arena_free(&mes_u->par_u); } static void @@ -1797,7 +1797,6 @@ _mesa_io_exit(u3_auto* car_u) u3_mesa* mes_u = (u3_mesa*)car_u; uv_timer_stop(&mes_u->tim_u); mes_u->tim_u.data = mes_u; - uv_close((uv_handle_t*)&mes_u->tim_u, _mesa_exit_cb); uv_close((uv_handle_t*)&mes_u->sun_u.tim_u, 0); uv_close((uv_handle_t*)&mes_u->nat_u.tim_u, 0); @@ -1805,6 +1804,7 @@ _mesa_io_exit(u3_auto* car_u) if ( UV_UNKNOWN_HANDLE != handle) { uv_close((uv_handle_t*)&mes_u->nat_u.pol_u, 0); } + uv_close((uv_handle_t*)&mes_u->tim_u, _mesa_exit_cb); } static void From 2f473a6777c578643a8fdfdb4bb348ebbd0c25ca Mon Sep 17 00:00:00 2001 From: ziffee Date: Wed, 9 Apr 2025 13:37:13 -0600 Subject: [PATCH 24/34] ames: refcount packet buffers --- pkg/vere/io/ames/ames.c | 200 ++++++++++++++++++++++------------------ pkg/vere/io/ames/stun.c | 4 +- pkg/vere/io/mesa.c | 18 ++-- 3 files changed, 122 insertions(+), 100 deletions(-) diff --git a/pkg/vere/io/ames/ames.c b/pkg/vere/io/ames/ames.c index ddf9a8c2a5..512f4b86f7 100644 --- a/pkg/vere/io/ames/ames.c +++ b/pkg/vere/io/ames/ames.c @@ -160,16 +160,25 @@ STATIC_ASSERT( PACT_PURR = 3 // fine response packet } u3_ptag; + typedef struct _u3_ref_hun { + c3_w ref_w; + c3_w len_w; // length in bytes + c3_y hun_y[0]; // packet buffer + } u3_ref_hun; + + typedef struct _u3_send_handle { + uv_udp_send_t snd_u; // udp send request + u3_ref_hun *hun_u; + } u3_send_handle; + /* u3_pact: ames packet * * Filled in piece by piece as we parse or construct it. */ typedef struct _u3_pact { - uv_udp_send_t snd_u; // udp send request struct _u3_ames* sam_u; // ames backpointer - c3_w len_w; // length in bytes - c3_y* hun_y; // packet buffer sockaddr_in lan_u; // destination/origin lane + u3_ref_hun* hun_u; u3_head hed_u; // head of packet u3_prel pre_u; // packet prelude u3_ptag typ_y; // packet type tag @@ -214,6 +223,30 @@ static c3_o net_o = c3y; // online heuristic to limit verbosity // *buf = uv_buf_init(ptr_v, 2048); //} +static void +_ames_ref_hun_lose(u3_ref_hun* hun_u) { + assert(0 != hun_u->ref_w); + hun_u->ref_w--; + if (0 == hun_u->ref_w) c3_free(hun_u); +} + +static u3_ref_hun* +_ames_ref_hun_gain(u3_ref_hun* hun_u) { + assert(0 != hun_u->ref_w); + assert(UINT32_MAX != hun_u->ref_w); + hun_u->ref_w++; + return hun_u; +} + +static u3_ref_hun* +_ames_ref_hun_new(c3_w len_w) { + u3_ref_hun* hun_u = c3_malloc(sizeof(hun_u) + len_w); + hun_u->ref_w = 1; + hun_u->len_w = len_w; + return hun_u; +} + + static void _ames_pact_free(u3_pact* pac_u) { @@ -237,7 +270,7 @@ _ames_pact_free(u3_pact* pac_u) u3_pier_bail(u3_king_stub()); } - c3_free(pac_u->hun_y); + _ames_ref_hun_lose(pac_u->hun_u); c3_free(pac_u); } @@ -335,10 +368,10 @@ static c3_o _ames_check_mug(u3_pact* pac_u) { c3_w rog_w = HEAD_SIZE + _ames_origin_size(&pac_u->hed_u); - c3_l mug_l = u3r_mug_bytes(pac_u->hun_y + rog_w, - pac_u->len_w - rog_w); + c3_l mug_l = u3r_mug_bytes(pac_u->hun_u->hun_y + rog_w, + pac_u->hun_u->len_w - rog_w); // u3l_log("len_w: %u, rog_w: %u, bod_l 0x%05x, hed_l 0x%05x", - // pac_u->len_w, rog_w, + // pac_u->hun_u->len_w, rog_w, // (mug_l & 0xfffff), // (pac_u->hed_u.mug_l & 0xfffff)); return ( @@ -415,14 +448,14 @@ _fine_sift_wail(u3_pact* pac_u, c3_w cur_w) c3_w exp_w = fra_w + len_w; c3_s len_s; - if ( cur_w + exp_w > pac_u->len_w ) { + if ( cur_w + exp_w > pac_u->hun_u->len_w ) { u3l_log("fine: wail not big enough"); return c3n; } // parse tag // - pac_u->wal_u.tag_y = *(pac_u->hun_y + cur_w); + pac_u->wal_u.tag_y = *(pac_u->hun_u->hun_y + cur_w); cur_w++; if ( 0 != pac_u->wal_u.tag_y ) { @@ -432,12 +465,12 @@ _fine_sift_wail(u3_pact* pac_u, c3_w cur_w) // parse fragment number // - pac_u->wal_u.pep_u.fra_w = c3_sift_word(pac_u->hun_y + cur_w); + pac_u->wal_u.pep_u.fra_w = c3_sift_word(pac_u->hun_u->hun_y + cur_w); cur_w += fra_w; // parse path length field // - len_s = c3_sift_short(pac_u->hun_y + cur_w); + len_s = c3_sift_short(pac_u->hun_u->hun_y + cur_w); pac_u->wal_u.pep_u.len_s = len_s; cur_w += len_w; @@ -448,9 +481,9 @@ _fine_sift_wail(u3_pact* pac_u, c3_w cur_w) { c3_w tot_w = cur_w + len_s; - if ( tot_w != pac_u->len_w ) { + if ( tot_w != pac_u->hun_u->len_w ) { u3l_log("fine: wail expected total len: %u, actual %u", - tot_w, pac_u->len_w); + tot_w, pac_u->hun_u->len_w); return c3n; } } @@ -458,7 +491,7 @@ _fine_sift_wail(u3_pact* pac_u, c3_w cur_w) // parse request path // pac_u->wal_u.pep_u.pat_c = c3_calloc(len_s + 1); - memcpy(pac_u->wal_u.pep_u.pat_c, pac_u->hun_y + cur_w, len_s); + memcpy(pac_u->wal_u.pep_u.pat_c, pac_u->hun_u->hun_y + cur_w, len_s); pac_u->wal_u.pep_u.pat_c[len_s] = '\0'; return c3y; } @@ -655,8 +688,7 @@ _fine_etch_response(u3_pact* pac_u) pre_w = _ames_prel_size(&pac_u->hed_u); pur_w = _fine_purr_size(&pac_u->pur_u); - pac_u->len_w = HEAD_SIZE + pre_w + pur_w; - pac_u->hun_y = c3_calloc(pac_u->len_w); + pac_u->hun_u = _ames_ref_hun_new(HEAD_SIZE + pre_w + pur_w); // skip the header until we know what the mug should be // @@ -664,19 +696,19 @@ _fine_etch_response(u3_pact* pac_u) // write prelude // - _ames_etch_prel(&pac_u->hed_u, &pac_u->pre_u, pac_u->hun_y + cur_w); + _ames_etch_prel(&pac_u->hed_u, &pac_u->pre_u, pac_u->hun_u->hun_y + cur_w); cur_w += pre_w; // write body // - _fine_etch_purr(&pac_u->pur_u, pac_u->hun_y + cur_w); + _fine_etch_purr(&pac_u->pur_u, pac_u->hun_u->hun_y + cur_w); // calculate mug and write header // rog_w = HEAD_SIZE + _ames_origin_size(&pac_u->hed_u); - pac_u->hed_u.mug_l = u3r_mug_bytes(pac_u->hun_y + rog_w, - pac_u->len_w - rog_w); - _ames_etch_head(&pac_u->hed_u, pac_u->hun_y); + pac_u->hed_u.mug_l = u3r_mug_bytes(pac_u->hun_u->hun_y + rog_w, + pac_u->hun_u->len_w - rog_w); + _ames_etch_head(&pac_u->hed_u, pac_u->hun_u->hun_y); u3_assert( c3y == _ames_check_mug(pac_u) ); } @@ -698,8 +730,7 @@ static void _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; + u3_send_handle* snd_u = (u3_send_handle*)req_u; if ( !sas_i ) { net_o = c3y; @@ -709,7 +740,8 @@ _ames_send_cb(uv_udp_send_t* req_u, c3_i sas_i) net_o = c3n; } - _ames_pact_free(pac_u); + _ames_ref_hun_lose(snd_u->hun_u); + c3_free(req_u); } u3_peer* @@ -718,34 +750,42 @@ _mesa_get_peer(u3_mesa_auto* sam_u, u3_ship her_u); u3_peer* _mesa_gut_peer(u3_mesa_auto* sam_u, u3_ship her_u); +c3_o +_mesa_is_lane_zero(sockaddr_in lan_u); + /* _ames_send(): send buffer to address on port. */ static void -_ames_send(u3_pact* pac_u) +_ames_send(u3_ames* sam_u, sockaddr_in lan_u, u3_ref_hun* hun_u) { - u3_ames* sam_u = pac_u->sam_u; + if ( c3n == sam_u->mes_u->car_u.liv_o ) { + u3l_log("ames: not yet live, dropping outbound\r"); + if ( NULL != hun_u) + _ames_ref_hun_lose(hun_u); + return; + } - if ( !pac_u->hun_y - || !sam_u - || !pac_u->len_w - || !pac_u->lan_u.sin_port ) + assert( (NULL == hun_u) || (0 != hun_u->ref_w) ); + if ( !hun_u->hun_y + || !hun_u->len_w + || c3y == _mesa_is_lane_zero(lan_u) ) { u3l_log("ames: _ames_send null"); - _ames_pact_free(pac_u); + if ( NULL != hun_u) + _ames_ref_hun_lose(hun_u); } else { - - { - 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, - (const struct sockaddr*)&pac_u->lan_u, - _ames_send_cb); - - if ( sas_i ) { - _ames_send_cb(&pac_u->snd_u, sas_i); - } + u3_send_handle* snd_u = c3_calloc(sizeof(*snd_u)); + snd_u->hun_u = hun_u; + uv_buf_t buf_u = uv_buf_init((c3_c*)hun_u->hun_y, hun_u->len_w); + c3_i sas_i = uv_udp_send(&snd_u->snd_u, + sam_u->wax_u, + &buf_u, 1, + (const struct sockaddr*)&lan_u, + _ames_send_cb); + + if ( sas_i ) { + _ames_send_cb(&snd_u->snd_u, sas_i); } } } @@ -844,9 +884,6 @@ _ames_is_direct_mode(u3_peer* per_u) (per_u->dir_u.her_d + DIRECT_ROUTE_TIMEOUT_MICROS > now_d) ); } -c3_o -_mesa_is_lane_zero(sockaddr_in lan_u); - static c3_o _ames_lane_from_peer(u3_ames* sam_u, u3_peer* per_u, @@ -893,7 +930,7 @@ _ames_lane_from_cache(u3_ames* sam_u, static u3_noun _ames_pact_to_noun(u3_pact* pac_u) { - u3_noun pac = u3i_bytes(pac_u->len_w, pac_u->hun_y); + u3_noun pac = u3i_bytes(pac_u->hun_u->len_w, pac_u->hun_u->hun_y); return pac; } @@ -1008,18 +1045,10 @@ _ames_ef_send(u3_ames* sam_u, sockaddr_in lan_u, u3_noun pac) return; } - u3_pact* pac_u = c3_calloc(sizeof(*pac_u)); - pac_u->sam_u = sam_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); - - u3r_bytes(0, pac_u->len_w, pac_u->hun_y, pac); - - _ames_sift_head(&pac_u->hed_u, pac_u->hun_y); - pac_u->typ_y = _ames_pact_typ(&pac_u->hed_u); + u3_ref_hun* hun_u = _ames_ref_hun_new(u3r_met(3, pac)); + u3r_bytes(0, hun_u->len_w, hun_u->hun_y, pac); - _ames_send(pac_u); + _ames_send(sam_u, lan_u, hun_u); u3z(pac); } @@ -1123,8 +1152,6 @@ static void _ames_send_many(u3_pact* pac_u, sockaddr_in lan_u[2], c3_o for_o) { u3_ames* sam_u = pac_u->sam_u; - u3_noun pac = _ames_pact_to_noun(pac_u); - // if forwarding, track metrics // if ( c3y == for_o ) { @@ -1159,9 +1186,8 @@ _ames_send_many(u3_pact* pac_u, sockaddr_in lan_u[2], c3_o for_o) for ( c3_w i = 0; ( (i < 2) && _mesa_is_lane_zero(lan_u[i]) ); i++) { - _ames_ef_send(sam_u, lan_u[i], u3k(pac)); + _ames_send(sam_u, lan_u[i], _ames_ref_hun_gain(pac_u->hun_u)); } - u3z(pac); } /* _ames_lane_scry_cb(): learn lanes to send packet on @@ -1227,6 +1253,11 @@ static void _ames_try_send(u3_pact* pac_u, c3_o for_o) { u3_ames* sam_u = pac_u->sam_u; + if ( c3n == sam_u->mes_u->car_u.liv_o ) { + u3l_log("ames: not yet live, dropping outbound\r"); + _ames_pact_free(pac_u); + return; + } u3_noun key = u3_ship_to_noun(pac_u->pre_u.rec_u); sockaddr_in lan_u[2]; @@ -1565,7 +1596,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->lan_u)), - u3i_bytes(pac_u->len_w, pac_u->hun_y)); + u3i_bytes(pac_u->hun_u->len_w, pac_u->hun_u->hun_y)); u3_ovum* ovo_u = u3_ovum_init(0, c3__ames, wir, cad); u3_auto_plan(&pac_u->sam_u->mes_u->car_u, ovo_u); @@ -1587,7 +1618,7 @@ _ames_hear_ames(u3_pact* pac_u, c3_w cur_w) #endif { - u3_noun msg = u3i_bytes(pac_u->len_w, pac_u->hun_y); + u3_noun msg = u3i_bytes(pac_u->hun_u->len_w, pac_u->hun_u->hun_y); _ames_put_packet(pac_u->sam_u, msg, pac_u->lan_u); _ames_pact_free(pac_u); } @@ -1603,31 +1634,29 @@ _ames_try_forward(u3_pact* pac_u) if ( c3n == pac_u->hed_u.rel_o ) //&& ( c3__czar == u3_ship_rank(pac_u->pre_u.sen_u) ) ) { - c3_y* old_y; - c3_w old_w, cur_w; + c3_w cur_w; + u3_ref_hun* old_u; pac_u->hed_u.rel_o = c3y; 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; + old_u = pac_u->hun_u; - pac_u->len_w += 6; - pac_u->hun_y = c3_calloc(pac_u->len_w); + pac_u->hun_u = _ames_ref_hun_new(pac_u->hun_u->len_w + 6); cur_w = 0; - _ames_etch_head(&pac_u->hed_u, pac_u->hun_y); + _ames_etch_head(&pac_u->hed_u, pac_u->hun_u->hun_y); cur_w += HEAD_SIZE; - _ames_etch_origin(pac_u->pre_u.rog_d, pac_u->hun_y + cur_w); + _ames_etch_origin(pac_u->pre_u.rog_d, pac_u->hun_u->hun_y + cur_w); cur_w += 6; - memcpy(pac_u->hun_y + cur_w, - old_y + HEAD_SIZE, - old_w - HEAD_SIZE); + memcpy(pac_u->hun_u->hun_y + cur_w, + old_u->hun_y + HEAD_SIZE, + old_u->len_w - HEAD_SIZE); - c3_free(old_y); + _ames_ref_hun_lose(old_u); } _ames_try_send(pac_u, c3y); @@ -1672,14 +1701,15 @@ _ames_hear(u3_ames* sam_u, pac_u = c3_calloc(sizeof(*pac_u)); pac_u->sam_u = sam_u; - pac_u->len_w = len_w; - pac_u->hun_y = hun_y; + pac_u->hun_u = _ames_ref_hun_new(len_w); + memcpy(pac_u->hun_u->hun_y, hun_y, len_w); + pac_u->lan_u = *lan_u; cur_w = 0; // parse the header // - _ames_sift_head(&pac_u->hed_u, pac_u->hun_y); + _ames_sift_head(&pac_u->hed_u, pac_u->hun_u->hun_y); cur_w += HEAD_SIZE; pac_u->typ_y = _ames_pact_typ(&pac_u->hed_u); @@ -1728,19 +1758,9 @@ _ames_hear(u3_ames* sam_u, // parse prelude // - _ames_sift_prel(&pac_u->hed_u, &pac_u->pre_u, pac_u->hun_y + cur_w); + _ames_sift_prel(&pac_u->hed_u, &pac_u->pre_u, pac_u->hun_u->hun_y + cur_w); cur_w += pre_w; - c3_o dir_o = (pac_u->pre_u.rog_d == 0) ? c3y : c3n; - u3_peer* per_u = _mesa_gut_peer(sam_u->mes_u, pac_u->pre_u.sen_u); - if (c3n == dir_o) { - if ( c3n == per_u->lam_o ) - per_u->dan_u = u3_ames_chub_to_lane(pac_u->pre_u.rog_d); - per_u->ind_u.her_d = _get_now_micros(); - } else { - per_u->dir_u.her_d = _get_now_micros(); - } - // if we can scry for lanes, // and we are not the recipient, // we might want to forward statelessly diff --git a/pkg/vere/io/ames/stun.c b/pkg/vere/io/ames/stun.c index e3cfecfe14..821ed07221 100644 --- a/pkg/vere/io/ames/stun.c +++ b/pkg/vere/io/ames/stun.c @@ -444,14 +444,14 @@ u3_stun_hear(u3_stun_client* sun_u, // if ( c3y == _stun_is_request(hun_y, len_w) ) { _stun_on_request(sun_u, hun_y, lan_u); - c3_free(hun_y); + //c3_free(hun_y); return c3y; } else if ( c3y == _stun_is_our_response(hun_y, sun_u->tid_y, len_w) ) { _stun_on_response(sun_u, hun_y, len_w); - c3_free(hun_y); + //c3_free(hun_y); return c3y; } return c3n; diff --git a/pkg/vere/io/mesa.c b/pkg/vere/io/mesa.c index f47f1e1be7..e4ff7c3e25 100644 --- a/pkg/vere/io/mesa.c +++ b/pkg/vere/io/mesa.c @@ -38,7 +38,7 @@ static c3_y are_y[524288]; /* #define PACKET_TEST c3y */ -// #define MESA_DEBUG c3y +//#define MESA_DEBUG c3y //#define MESA_TEST #define RED_TEXT "\033[0;31m" #define DEF_TEXT "\033[0m" @@ -449,9 +449,9 @@ _clamp_rto(c3_d rto_d) { c3_o _mesa_is_lane_zero(sockaddr_in lan_u) { - return __( ( (lan_u.sin_addr.s_addr == 0) || - (lan_u.sin_addr.s_addr == _CZAR_GONE) ) - && (lan_u.sin_port == 0) ); + return __( (lan_u.sin_addr.s_addr == _CZAR_GONE) + || ((lan_u.sin_addr.s_addr == 0) + && (lan_u.sin_port == 0)) ); } c3_o @@ -2525,6 +2525,10 @@ _mesa_hear_page(u3_mesa_pict* pic_u, sockaddr_in lan_u) // } else { // u3l_log(" received forwarded page"); // } + // XX: we haven't validated the packet yet + // maybe what we should do is move away from nails (joe says it's bad io overhead) + // and only scry lanes if peer is NULL, and do all of our updating in our callbacks + // (what the nail would be, by looking at peer state and the hops data) _hear_peer(mes_u, per_u, lan_u, dir_o); if ( new_o == c3y ) { @@ -2837,12 +2841,10 @@ _mesa_hear(u3_mesa* mes_u, // for next protocol version, have an urbit cookie // - c3_y* han_y = c3_malloc(len_w); - memcpy(han_y, hun_y, len_w); - if ( c3y == u3_stun_hear(&mes_u->sun_u, lan_u, len_w, han_y) ) + if ( c3y == u3_stun_hear(&mes_u->sun_u, lan_u, len_w, hun_y) ) return; - _ames_hear(mes_u->sam_u, lan_u, len_w, han_y); + _ames_hear(mes_u->sam_u, lan_u, len_w, hun_y); return; } From 782b742dec92dc211f1addf49a54f9393ae36879 Mon Sep 17 00:00:00 2001 From: ziffee Date: Wed, 9 Apr 2025 17:16:32 -0600 Subject: [PATCH 25/34] ames: update lanes on hear callbacks, auto: add u3_ovum_exit news --- pkg/vere/auto.c | 1 + pkg/vere/io/ames/ames.c | 170 ++++++++++++++++++++++------------------ pkg/vere/io/conn.c | 6 +- pkg/vere/io/mesa.c | 7 +- pkg/vere/vere.h | 3 +- 5 files changed, 105 insertions(+), 82 deletions(-) diff --git a/pkg/vere/auto.c b/pkg/vere/auto.c index b8d1d0bca1..2a176de2b2 100644 --- a/pkg/vere/auto.c +++ b/pkg/vere/auto.c @@ -341,6 +341,7 @@ u3_auto_exit(u3_auto* car_u) while ( egg_u ) { xen_u = egg_u->nex_u; + _auto_news(egg_u, u3_ovum_exit); u3_ovum_free(egg_u); egg_u = xen_u; } diff --git a/pkg/vere/io/ames/ames.c b/pkg/vere/io/ames/ames.c index 512f4b86f7..bdcb22809c 100644 --- a/pkg/vere/io/ames/ames.c +++ b/pkg/vere/io/ames/ames.c @@ -61,7 +61,6 @@ ur_cue_test_t* tes_u; // cue-test handle u3_cue_xeno* sil_u; // cue handle c3_y ver_y; // protocol version - struct _u3_panc* pan_u; // outbound packet queue, backward struct { // config: c3_o see_o; // can scry c3_o fit_o; // filtering active @@ -163,7 +162,7 @@ STATIC_ASSERT( typedef struct _u3_ref_hun { c3_w ref_w; c3_w len_w; // length in bytes - c3_y hun_y[0]; // packet buffer + c3_y hun_y[0]; // packet buffer } u3_ref_hun; typedef struct _u3_send_handle { @@ -182,6 +181,8 @@ STATIC_ASSERT( u3_head hed_u; // head of packet u3_prel pre_u; // packet prelude u3_ptag typ_y; // packet type tag + c3_d her_d; // time packet was heard + c3_o for_o; // are we forwarding union { u3_body bod_u; // tagged by PACT_AMES u3_wail wal_u; // tagged by PACT_WAIL @@ -189,15 +190,6 @@ STATIC_ASSERT( }; } u3_pact; -/* u3_panc: packet queue -*/ - typedef struct _u3_panc { - struct _u3_panc* pre_u; // previous packet - struct _u3_panc* nex_u; // next packet - u3_pact* pac_u; // this packet - c3_o for_o; // are we forwarding this? - } u3_panc; - #define _str_typ(typ_y) ( \ ( PACT_AMES == typ_y ) ? "ames" \ : ( PACT_WAIL == typ_y ) ? "wail" \ @@ -274,28 +266,6 @@ _ames_pact_free(u3_pact* pac_u) c3_free(pac_u); } -/* _ames_panc_free(): remove references, lose refcounts and free struct -*/ -static void -_ames_panc_free(u3_panc* pan_u) -{ - if ( c3y == pan_u->for_o ) { - if ( 0 != pan_u->nex_u ) { - pan_u->nex_u->pre_u = pan_u->pre_u; - } - - if ( 0 != pan_u->pre_u ) { - pan_u->pre_u->nex_u = pan_u->nex_u; - } - else { - u3_assert(pan_u == pan_u->pac_u->sam_u->pan_u); - pan_u->pac_u->sam_u->pan_u = pan_u->nex_u; - } - } - _ames_pact_free(pan_u->pac_u); - c3_free(pan_u); -} - static inline u3_ptag _ames_pact_typ(u3_head* hed_u) { @@ -766,7 +736,8 @@ _ames_send(u3_ames* sam_u, sockaddr_in lan_u, u3_ref_hun* hun_u) } assert( (NULL == hun_u) || (0 != hun_u->ref_w) ); - if ( !hun_u->hun_y + if ( !hun_u + || !hun_u->hun_y || !hun_u->len_w || c3y == _mesa_is_lane_zero(lan_u) ) { @@ -832,6 +803,7 @@ u3_ames_encode_lane(sockaddr_in lan) { } /* _ames_lane_into_cache(): put las for who into cache, including timestamp + * TODO: remove? use callbacks */ u3_peer* _ames_lane_into_cache(u3_ames* sam_u, u3_noun who, u3_noun las) @@ -1084,12 +1056,38 @@ _ames_cap_queue(u3_ames* sam_u) } } +static void +_ames_hear_news(u3_ovum* egg_u, u3_ovum_news new_e) +{ + u3_pact* pac_u = egg_u->ptr_v; + u3_ames* sam_u = pac_u->sam_u; + if ( u3_ovum_exit == new_e ) { + _ames_pact_free(pac_u); + return; + } + else if ( u3_ovum_done == new_e ) { + if ( c3n == pac_u->for_o) { + c3_o dir_o = (pac_u->pre_u.rog_d == 0) ? c3y : c3n; + u3_peer* per_u = _mesa_gut_peer(sam_u->mes_u, pac_u->pre_u.sen_u); + if (c3n == dir_o) { + if ( c3n == per_u->lam_o ) + per_u->dan_u = u3_ames_chub_to_lane(pac_u->pre_u.rog_d); + per_u->ind_u.her_d = pac_u->her_d; + } else { + per_u->dir_u.her_d = pac_u->her_d; + } + } + } + _ames_pact_free(pac_u); +} + /* _ames_hear_bail(): handle packet failure. */ static void _ames_hear_bail(u3_ovum* egg_u, u3_noun lud) { - u3_ames* sam_u = ((u3_mesa_auto*)egg_u->car_u)->sam_u; + u3_pact* pac_u = egg_u->ptr_v; + u3_ames* sam_u = pac_u->sam_u; c3_w len_w = u3qb_lent(lud); if ( (1 == len_w) && c3__evil == u3h(u3h(lud)) ) { @@ -1124,6 +1122,7 @@ _ames_hear_bail(u3_ovum* egg_u, u3_noun lud) } } + _ames_pact_free(pac_u); u3z(lud); u3_ovum_free(egg_u); } @@ -1132,16 +1131,17 @@ _ames_hear_bail(u3_ovum* egg_u, u3_noun lud) */ static void _ames_put_packet(u3_ames* sam_u, - u3_noun msg, + u3_pact* pac_u, sockaddr_in lan_u) { u3_noun wir = u3nc(c3__ames, u3_nul); - u3_noun cad = u3nt(c3__hear, u3nc(c3n, u3_ames_encode_lane(lan_u)), msg); + u3_noun cad = u3nt(c3__hear, u3nc(c3n, u3_ames_encode_lane(lan_u)), + _ames_pact_to_noun(pac_u)); u3_auto_peer( u3_auto_plan(&sam_u->mes_u->car_u, u3_ovum_init(0, c3__a, wir, cad)), - 0, 0, _ames_hear_bail); + pac_u, _ames_hear_news, _ames_hear_bail); _ames_cap_queue(sam_u); } @@ -1195,8 +1195,7 @@ _ames_send_many(u3_pact* pac_u, sockaddr_in lan_u[2], c3_o for_o) static void _ames_lane_scry_cb(void* vod_p, u3_noun nun) { - u3_panc* pan_u = vod_p; - u3_pact* pac_u = pan_u->pac_u; + u3_pact* pac_u = vod_p; u3_ames* sam_u = pac_u->sam_u; u3_weak las = u3r_at(7, nun); @@ -1208,7 +1207,7 @@ _ames_lane_scry_cb(void* vod_p, u3_noun nun) sam_u->fig_u.see_o = c3n; } _ames_put_packet(sam_u, - _ames_pact_to_noun(pac_u), + pac_u, pac_u->lan_u); } else { @@ -1227,10 +1226,10 @@ _ames_lane_scry_cb(void* vod_p, u3_noun nun) // if there are lanes, send the packet on them; otherwise drop it // if ( c3n == _mesa_is_lane_zero(lan_u[0]) ) { - _ames_send_many(pac_u, lan_u, pan_u->for_o); + _ames_send_many(pac_u, lan_u, pac_u->for_o); } + _ames_pact_free(pac_u); } - _ames_panc_free(pan_u); u3z(nun); } @@ -1239,8 +1238,8 @@ _ames_lane_scry_cb(void* vod_p, u3_noun nun) static void _ames_lane_scry_forward_cb(void *vod_p, u3_noun nun) { - u3_panc *pan_u = vod_p; - u3_ames *sam_u = pan_u->pac_u->sam_u; + u3_pact* pac_u = vod_p; + u3_ames *sam_u = pac_u->sam_u; sam_u->sat_u.foq_d--; @@ -1250,7 +1249,7 @@ _ames_lane_scry_forward_cb(void *vod_p, u3_noun nun) /* _ames_try_send(): try to send a packet to a ship and its sponsors */ static void -_ames_try_send(u3_pact* pac_u, c3_o for_o) +_ames_try_send(u3_pact* pac_u) { u3_ames* sam_u = pac_u->sam_u; if ( c3n == sam_u->mes_u->car_u.liv_o ) { @@ -1278,7 +1277,7 @@ _ames_try_send(u3_pact* pac_u, c3_o for_o) // so can't pluck these out of the event queue like it does in // _ames_cap_queue. as such, blocked on u3_lord_peek_cancel or w/e. // - if ( (c3y == for_o) + if ( (c3y == pac_u->for_o) && (c3n == lan_o) && (1000 < sam_u->sat_u.foq_d) ) { @@ -1295,37 +1294,28 @@ _ames_try_send(u3_pact* pac_u, c3_o for_o) // if we already know the lane, just send // if ( c3y == lan_o ) { - _ames_send_many(pac_u, lan_u, for_o); + _ames_send_many(pac_u, lan_u, pac_u->for_o); _ames_pact_free(pac_u); } // store the packet to be sent later when the lane scry completes // else { - u3_panc* pan_u = c3_calloc(sizeof(*pan_u)); - pan_u->pac_u = pac_u; - pan_u->for_o = for_o; - u3_noun pax = _lane_scry_path(u3_ship_to_noun(pac_u->pre_u.rec_u)); // if forwarding, enqueue the packet and scry for the lane // u3_noun gang = u3nc(u3_nul, u3_nul); - if ( c3y == for_o ) { - if ( 0 != sam_u->pan_u ) { - pan_u->nex_u = sam_u->pan_u; - sam_u->pan_u->pre_u = pan_u; - } - sam_u->pan_u = pan_u; + if ( c3y == pac_u->for_o ) { sam_u->sat_u.foq_d++; u3_pier_peek_last(sam_u->pir_u, gang, c3__ax, - u3_nul, pax, pan_u, _ames_lane_scry_forward_cb); + u3_nul, pax, pac_u, _ames_lane_scry_forward_cb); } // otherwise, just scry for the lane // else { u3_pier_peek_last(sam_u->pir_u, gang, c3__ax, - u3_nul, pax, pan_u, _ames_lane_scry_cb); + u3_nul, pax, pac_u, _ames_lane_scry_cb); } } } @@ -1433,7 +1423,7 @@ _fine_hunk_scry_cb(void* vod_p, u3_noun nun) } _fine_etch_response(pac_u); - _ames_try_send(pac_u, c3n); + _ames_try_send(pac_u); } else { u3l_log("fine: bad meow"); @@ -1577,7 +1567,7 @@ _fine_hear_request(u3_pact* req_u, c3_w cur_w) res_u->pur_u.pep_u.pat_c); } _fine_etch_response(res_u); - _ames_try_send(res_u, c3n); + _ames_try_send(res_u); } else { u3l_log("fine: _fine_hear_request meow bad"); @@ -1599,7 +1589,9 @@ _fine_hear_response(u3_pact* pac_u, c3_w cur_w) u3i_bytes(pac_u->hun_u->len_w, pac_u->hun_u->hun_y)); u3_ovum* ovo_u = u3_ovum_init(0, c3__ames, wir, cad); - u3_auto_plan(&pac_u->sam_u->mes_u->car_u, ovo_u); + u3_auto_peer( + u3_auto_plan(&pac_u->sam_u->mes_u->car_u, ovo_u), + pac_u, _ames_hear_news, _ames_hear_bail); _ames_cap_queue(pac_u->sam_u); } @@ -1617,11 +1609,7 @@ _ames_hear_ames(u3_pact* pac_u, c3_w cur_w) } #endif - { - u3_noun msg = u3i_bytes(pac_u->hun_u->len_w, pac_u->hun_u->hun_y); - _ames_put_packet(pac_u->sam_u, msg, pac_u->lan_u); - _ames_pact_free(pac_u); - } + _ames_put_packet(pac_u->sam_u, pac_u, pac_u->lan_u); } /* _ames_try_forward(): forward packet, updating lane if needed. @@ -1659,7 +1647,7 @@ _ames_try_forward(u3_pact* pac_u) _ames_ref_hun_lose(old_u); } - _ames_try_send(pac_u, c3y); + _ames_try_send(pac_u); } /* _ames_hear(): parse a (potential) packet, dispatch appropriately. @@ -1761,12 +1749,17 @@ _ames_hear(u3_ames* sam_u, _ames_sift_prel(&pac_u->hed_u, &pac_u->pre_u, pac_u->hun_u->hun_y + cur_w); cur_w += pre_w; + pac_u->her_d = _get_now_micros(); + pac_u->for_o = (c3y == u3_ships_equal(pac_u->pre_u.rec_u, + sam_u->pir_u->who_u)) + ? c3n : c3y; + // if we can scry for lanes, // and we are not the recipient, // we might want to forward statelessly // if ( (c3y == sam_u->fig_u.see_o) - && ( u3_ships_equal(pac_u->pre_u.rec_u, sam_u->pir_u->who_u) ) ) + && ( c3y == pac_u->for_o ) ) { if ( c3y == sam_u->sat_u.for_o ) { _ames_try_forward(pac_u); @@ -1828,6 +1821,33 @@ _ames_prot_scry_cb(void* vod_p, u3_noun nun) u3z(nun); } +static void +_saxo_self_cb(void* vod_p, u3_noun nun) +{ + u3_ames* sam_u = vod_p; + u3_weak sax = u3r_at(7, nun); + + if ( sax != u3_none ) { + u3_noun lam = u3do("rear", u3k(sax)); + u3_ship lam_u = u3_ship_of_noun(lam); + if ( c3y == u3_ships_equal(lam_u, sam_u->mes_u->pir_u->who_u) ) + sam_u->sat_u.for_o = c3y; + u3z(lam); + } + + u3z(nun); +} + +static void +_meet_self(u3_ames* sam_u) +{ + u3_noun her = u3_ship_to_noun(sam_u->mes_u->pir_u->who_u); + u3_noun gan = u3nc(u3_nul, u3_nul); + + u3_noun pax = u3nc(u3dc("scot", c3__p, her), u3_nul); + u3_pier_peek_last(sam_u->mes_u->pir_u, gan, c3__j, c3__saxo, pax, sam_u, _saxo_self_cb); +} + /* _ames_io_talk(): start receiving ames traffic. */ void @@ -1858,6 +1878,8 @@ _ames_io_talk(u3_ames* sam_u) u3_pier_peek_last(sam_u->pir_u, gang, c3__ax, u3_nul, u3nt(u3i_string("protocol"), u3i_string("version"), u3_nul), sam_u, _ames_prot_scry_cb); + if ( c3n == sam_u->sat_u.for_o ) + _meet_self(sam_u); } /* _ames_kick_newt(): apply packet network outputs. @@ -1946,13 +1968,6 @@ _ames_io_kick(u3_ames* sam_u, u3_noun wir, u3_noun cad) void _ames_exit_cb(u3_ames* sam_u) { - u3_panc* pan_u = sam_u->pan_u; - while (0 != pan_u) { - u3_panc* nex_u = pan_u->nex_u; - _ames_panc_free(pan_u); - pan_u = nex_u; - } - u3s_cue_xeno_done(sam_u->sil_u); ur_cue_test_done(sam_u->tes_u); @@ -2043,6 +2058,7 @@ u3_ames_io_init(u3_mesa_auto* mes_u) // enable forwarding on galaxies only u3_noun who = u3_ship_to_noun(sam_u->mes_u->pir_u->who_u); u3_noun rac = u3do("clan:title", who); + // XX: fix for groundwire sam_u->sat_u.for_o = ( c3__czar == rac ) ? c3y : c3n; // hashtable for scry cache diff --git a/pkg/vere/io/conn.c b/pkg/vere/io/conn.c index d740c339a4..3ecbb9731a 100644 --- a/pkg/vere/io/conn.c +++ b/pkg/vere/io/conn.c @@ -413,10 +413,12 @@ _conn_ovum_news(u3_ovum* egg_u, u3_ovum_news new_e) u3_chan* can_u = ran_u->can_u; if ( u3_ovum_done == new_e || - u3_ovum_drop == new_e ) + u3_ovum_drop == new_e || + u3_ovum_exit == new_e ) { if ( can_u ) { - _conn_send_noun(can_u, + if ( u3_ovum_exit != new_e ) + _conn_send_noun(can_u, u3nt(ran_u->rid, c3__news, ( u3_ovum_done == new_e ? c3__done diff --git a/pkg/vere/io/mesa.c b/pkg/vere/io/mesa.c index e4ff7c3e25..f8bb45c382 100644 --- a/pkg/vere/io/mesa.c +++ b/pkg/vere/io/mesa.c @@ -341,7 +341,7 @@ typedef struct _u3_mesa_cb_data { sockaddr_in lan_u; } u3_mesa_cb_data; -static void _meet_peer(u3_mesa* mes_u, u3_peer* per_u); +void _meet_peer(u3_mesa* mes_u, u3_peer* per_u); static void _init_peer(u3_mesa* mes_u, u3_peer* per_u, u3_ship her_u); static void _mesa_ef_saxo(u3_mesa* mes_u, u3_noun zad); @@ -1716,6 +1716,7 @@ static c3_o _mesa_kick(u3_mesa* mes_u, u3_noun tag, u3_noun dat) break; } case c3__nail: { + // TODO: remove? use callbacks u3_noun who = u3k(u3h(dat)); u3_noun las = u3k(u3t(dat)); _ames_lane_into_cache(mes_u->sam_u, who, las); @@ -2140,6 +2141,7 @@ _saxo_cb(void* vod_p, u3_noun nun) // both atoms guaranteed to be cats, bc we don't call unless forwarding per_u->ful_o = c3y; per_u->lam_u = u3_ship_of_noun(lam); + u3z(lam); } u3z(nun); @@ -2407,7 +2409,7 @@ typedef struct _u3_mesa_lane_cb_data { } u3_mesa_lane_cb_data; static void -_mesa_page_bail_cb(u3_ovum* egg_u, u3_ovum_news new_e) +_mesa_page_bail_cb(u3_ovum* egg_u, u3_noun why) { u3l_log("mesa: arvo page event failed"); } @@ -2425,6 +2427,7 @@ _mesa_add_hop(c3_y hop_y, u3_mesa_head* hed_u, u3_mesa_page_pact* pag_u, sockadd } + // XX: leak u3_mesa_hop_once* lan_y = c3_calloc(sizeof(u3_mesa_hop_once)); c3_etch_word(lan_y->dat_y, pip_w); diff --git a/pkg/vere/vere.h b/pkg/vere/vere.h index 6e20309366..c7a3d00972 100644 --- a/pkg/vere/vere.h +++ b/pkg/vere/vere.h @@ -348,7 +348,8 @@ typedef enum { u3_ovum_drop = 0, // unplanned u3_ovum_work = 1, // begun - u3_ovum_done = 2 // complete + u3_ovum_done = 2, // complete + u3_ovum_exit = 3 // exit } u3_ovum_news; struct _u3_ovum; From 391f0b48b42a62653c8008ea09663e22fe95f1f3 Mon Sep 17 00:00:00 2001 From: ziffee Date: Wed, 9 Apr 2025 17:24:35 -0600 Subject: [PATCH 26/34] mesa: move gauge to peer struct --- pkg/vere/io/ames/ames.c | 4 +- pkg/vere/io/mesa.c | 108 ++++++++++------------------------------ pkg/vere/io/mesa/mesa.h | 12 +++++ 3 files changed, 40 insertions(+), 84 deletions(-) diff --git a/pkg/vere/io/ames/ames.c b/pkg/vere/io/ames/ames.c index bdcb22809c..719c902709 100644 --- a/pkg/vere/io/ames/ames.c +++ b/pkg/vere/io/ames/ames.c @@ -238,7 +238,6 @@ _ames_ref_hun_new(c3_w len_w) { return hun_u; } - static void _ames_pact_free(u3_pact* pac_u) { @@ -1063,7 +1062,6 @@ _ames_hear_news(u3_ovum* egg_u, u3_ovum_news new_e) u3_ames* sam_u = pac_u->sam_u; if ( u3_ovum_exit == new_e ) { _ames_pact_free(pac_u); - return; } else if ( u3_ovum_done == new_e ) { if ( c3n == pac_u->for_o) { @@ -1077,8 +1075,8 @@ _ames_hear_news(u3_ovum* egg_u, u3_ovum_news new_e) per_u->dir_u.her_d = pac_u->her_d; } } + _ames_pact_free(pac_u); } - _ames_pact_free(pac_u); } /* _ames_hear_bail(): handle packet failure. diff --git a/pkg/vere/io/mesa.c b/pkg/vere/io/mesa.c index f8bb45c382..2fe4aa50f8 100644 --- a/pkg/vere/io/mesa.c +++ b/pkg/vere/io/mesa.c @@ -104,18 +104,6 @@ typedef struct _u3_pact_stat { c3_y sip_y; // skips } u3_pact_stat; - -typedef struct _u3_gage { - c3_w rtt_w; // rtt - c3_w rto_w; // rto - c3_w rtv_w; // rttvar - c3_w wnd_w; // cwnd - c3_w wnf_w; // cwnd fraction - c3_w sst_w; // ssthresh - c3_w con_w; // counter - // -} u3_gage; - typedef struct _u3_mesa_pict { uv_udp_send_t snd_u; struct _u3_mesa* mes_u; @@ -193,13 +181,6 @@ typedef struct _u3_pend_req u3_pend_req; #define VAL_DTOR_FN u3_free_pit #include "verstable.h" -#define NAME gag_map -#define KEY_TY u3_ship -#define HASH_FN u3_hash_ship -#define CMPR_FN u3_cmpr_ship -#define VAL_TY u3_gage* -#include "verstable.h" - typedef enum _u3_mesa_ctag { CTAG_WAIT = 1, CTAG_BLOCK = 2, @@ -265,7 +246,6 @@ typedef struct _u3_mesa { per_map per_u; // (map ship u3_peer) c3_d jum_d; // bytes in jumbo cache jum_map jum_u; // jumbo cache - gag_map gag_u; // lane cache pit_map pit_u; // (map path [our=? las=(set lane)]) req_map req_u; // (map [rift path] u3_pend_req) c3_c* dns_c; // turf (urb.otrg) @@ -296,7 +276,6 @@ typedef struct _u3_pend_req { c3_d out_d; // outstanding fragments in flight c3_d old_d; // frag num of oldest packet sent c3_d ack_d; // highest acked fragment number - u3_gage* gag_u; // congestion control lss_pair* mis_u; // misordered packets lss_verifier* los_u; // Lockstep verifier u3_mesa_pict* pic_u; // preallocated request packet @@ -406,7 +385,7 @@ _log_pend_req(u3_pend_req* req_u) u3l_log("have: %"PRIu64, req_u->hav_d); u3l_log("next: %"PRIu64, req_u->nex_d); u3l_log("total: %" PRIu64, req_u->tof_d); - u3l_log("gage: %c", req_u->gag_u == NULL ? 'n' : 'y'); + //u3l_log("gage: %c", req_u->gag_u == NULL ? 'n' : 'y'); //u3l_log("timer in: %" PRIu64 " ms", uv_timer_get_due_in(&req_u->tim_u)); } @@ -560,20 +539,6 @@ _mesa_request_key(u3_mesa_name* nam_u) return res; } -static void -_init_gage(u3_gage* gag_u) // microseconds -{ - gag_u->rto_w = 200 * 1000; // ~s1 - gag_u->rtt_w = 1000 * 1000; // ~s1 - gag_u->rtv_w = 1000 * 1000; // ~s1 - /* gag_u->rto_w = 200 * 1000; // ~s1 */ - /* gag_u->rtt_w = 82 * 1000; // ~s1 */ - /* gag_u->rtv_w = 100 * 1000; // ~s1 */ - gag_u->con_w = 0; - gag_u->wnd_w = 1; - gag_u->sst_w = 10000; -} - /* u3_mesa_encode_lane(): serialize lane to noun */ static u3_noun @@ -700,29 +665,6 @@ _mesa_lanes_equal(sockaddr_in lan_u, sockaddr_in lon_u) return __((lan_u.sin_addr.s_addr == lon_u.sin_addr.s_addr) && (lan_u.sin_port == lon_u.sin_port)); } -/* _mesa_get_lane(): get lane -*/ -static u3_gage* -_mesa_get_gage(u3_mesa* mes_u, u3_ship her_u) { - gag_map_itr itr_u = vt_get(&mes_u->gag_u, her_u); - if ( vt_is_end(itr_u) ) { - return NULL; - } - return itr_u.data->val; -} - -/* _mesa_put_gage(): put gage state in state - * -*/ -static void -_mesa_put_gage(u3_mesa* mes_u, u3_ship her_u, u3_gage* gag_u) -{ - gag_map_itr itr_u = vt_insert(&mes_u->gag_u, her_u, gag_u); - if ( vt_is_end(itr_u) ) { - fprintf(stderr, "mesa: cannot allocate memory for gage, dying"); - u3_king_bail(); - } -} // congestion control update static void _mesa_handle_ack(u3_gage* gag_u, u3_pact_stat* pat_u) { @@ -768,13 +710,13 @@ _mesa_req_get_cwnd(u3_pend_req* req_u) { /* c3_w liv_w = bitset_wyt(&req_u->was_u); */ c3_w rem_w = _mesa_req_get_remaining(req_u); - /* u3l_log("rem_w %u wnd_w %u", rem_w, req_u->gag_u->wnd_w); */ + /* u3l_log("rem_w %u wnd_w %u", rem_w, req_u->per_u->gag_u.wnd_w); */ /* u3l_log("rem_w %u", rem_w); */ - /* u3l_log("wnd_w %u", req_u->gag_u->wnd_w); */ + /* u3l_log("wnd_w %u", req_u->per_u->gag_u.wnd_w); */ /* u3l_log("out_d %"PRIu64, req_u->out_d); */ /* return c3_min(rem_w, _safe_sub(3500, req_u->out_d)); */ - return c3_min(rem_w, _safe_sub((c3_d)req_u->gag_u->wnd_w, req_u->out_d)); + return c3_min(rem_w, _safe_sub((c3_d)req_u->per_u->gag_u.wnd_w, req_u->out_d)); /* return c3_min(rem_w, 5000 - req_u->out_d); */ } @@ -1131,10 +1073,10 @@ _try_resend(u3_pend_req* req_u, c3_d nex_d) // TODO: make fast recovery different from slow // TODO: track skip count but not dupes, since dupes are meaningless if ( (c3n == bitset_has(&req_u->was_u, i_d)) && - (now_d - req_u->wat_u[i_d].sen_d > req_u->gag_u->rto_w) ) { + (now_d - req_u->wat_u[i_d].sen_d > req_u->per_u->gag_u.rto_w) ) { // u3l_log("now_d %"PRIu64, now_d); // u3l_log("sen_d %"PRIu64, req_u->wat_u[i_d].sen_d); - // u3l_log("rto_w %u", req_u->gag_u->rto_w); + // u3l_log("rto_w %u", req_u->per_u->gag_u.rto_w); los_o = c3y; /* u3l_log("resend fra_w: %llu", i_d); */ @@ -1164,13 +1106,13 @@ _try_resend(u3_pend_req* req_u, c3_d nex_d) if ( c3y == los_o ) { /* _mesa_send_buf2(req_u->per_u->mes_u, ads_u, bus_u, int_u, i_w); */ /* _mesa_send_buf2(req_u->per_u->mes_u, req_u->per_u->dan_u, bfs_u, i_w); */ - req_u->gag_u->sst_w = c3_max(1, req_u->gag_u->wnd_w / 2); - req_u->gag_u->wnd_w = req_u->gag_u->sst_w; - req_u->gag_u->rto_w = _clamp_rto(req_u->gag_u->rto_w * 2); + req_u->per_u->gag_u.sst_w = c3_max(1, req_u->per_u->gag_u.wnd_w / 2); + req_u->per_u->gag_u.wnd_w = req_u->per_u->gag_u.sst_w; + req_u->per_u->gag_u.rto_w = _clamp_rto(req_u->per_u->gag_u.rto_w * 2); // u3l_log("loss"); // u3l_log("resent %u", i_w); // u3l_log("counter %u hav_d %"PRIu64 " nex_d %"PRIu64 " ack_d %"PRIu64 " lef_d %"PRIu64 " old_d %"PRIu64, req_u->los_u->counter, req_u->hav_d, req_u->nex_d, req_u->ack_d, req_u->lef_d, req_u->old_d); - // _log_gage(req_u->gag_u); + // _log_gage(&req_u->per_u->gag_u); } } @@ -1204,7 +1146,7 @@ _update_resend_timer(u3_pend_req *req_u) // c3_d gap_d = req_u->wat_u[idx_d].sen_d == 0 ? // 0 : // now_d - req_u->wat_u[idx_d].sen_d; - c3_d next_expiry = req_u->gag_u->rto_w; + c3_d next_expiry = req_u->per_u->gag_u.rto_w; // u3l_log("next_expiry %llu", next_expiry / 1000); // u3l_log("DUE %llu", uv_timer_get_due_in(&req_u->tim_u)); uv_timer_start(&req_u->tim_u, _mesa_packet_timeout, next_expiry / 1000, 0); @@ -1301,7 +1243,7 @@ _mesa_req_pact_done(u3_pend_req* req_u, req_u->out_d--; bitset_put(&req_u->was_u, nam_u->fra_d); - _mesa_handle_ack(req_u->gag_u, &req_u->wat_u[nam_u->fra_d]); + _mesa_handle_ack(&req_u->per_u->gag_u, &req_u->wat_u[nam_u->fra_d]); /* _try_resend(req_u, nam_u->fra_d); */ /* _update_resend_timer(req_u); */ return; @@ -1346,7 +1288,7 @@ _mesa_req_pact_done(u3_pend_req* req_u, sat_u->her_d = _get_now_micros(); // handle gauge update - _mesa_handle_ack(req_u->gag_u, &req_u->wat_u[nam_u->fra_d]); + _mesa_handle_ack(&req_u->per_u->gag_u, &req_u->wat_u[nam_u->fra_d]); // XX FIXME? /* _try_resend(req_u, nam_u->fra_d); */ @@ -1817,6 +1759,19 @@ _init_lane_state(u3_lane_state* sat_u) sat_u->rtv_w = 1000000; } +static void +_init_gage(u3_gage* gag_u) // microseconds +{ + gag_u->rto_w = 200 * 1000; // ~s1 + gag_u->rtt_w = 1000 * 1000; // ~s1 + gag_u->rtv_w = 1000 * 1000; // ~s1 + /* gag_u->rto_w = 200 * 1000; // ~s1 */ + /* gag_u->rtt_w = 82 * 1000; // ~s1 */ + /* gag_u->rtv_w = 100 * 1000; // ~s1 */ + gag_u->con_w = 0; + gag_u->wnd_w = 1; + gag_u->sst_w = 10000; +} static void _init_peer(u3_mesa* mes_u, u3_peer* per_u, u3_ship her_u) { @@ -1828,6 +1783,7 @@ _init_peer(u3_mesa* mes_u, u3_peer* per_u, u3_ship her_u) _init_lane_state(&per_u->dir_u); per_u->her_u = her_u; _init_lane_state(&per_u->ind_u); + _init_gage(&per_u->gag_u); } static u3_noun @@ -2301,14 +2257,6 @@ _mesa_req_pact_init(u3_mesa* mes_u, u3_mesa_pict* pic_u, sockaddr_in lan_u, u3_p u3_mesa_name* nam_u = &pac_u->pag_u.nam_u; u3_mesa_data* dat_u = &pac_u->pag_u.dat_u; - u3_gage* gag_u = _mesa_get_gage(mes_u, nam_u->her_u); - if ( gag_u == NULL ) { - gag_u = new(&mes_u->par_u, u3_gage, 1); - _init_gage(gag_u); - _mesa_put_gage(mes_u, nam_u->her_u, gag_u); - u3_assert( gag_u != NULL ); - } - // _log_gage(gag_u); u3_mesa_pact exa_u; exa_u.hed_u.typ_y = PACT_PEEK; @@ -2336,7 +2284,6 @@ _mesa_req_pact_init(u3_mesa* mes_u, u3_mesa_pict* pic_u, sockaddr_in lan_u, u3_p uv_timer_init(u3L, &req_u->tim_u); u3_assert( pac_u->pag_u.nam_u.boq_y == 13 ); - req_u->gag_u = gag_u; req_u->tob_d = dat_u->tob_d; /* req_u->out_d = 4000; */ req_u->out_d = 0; @@ -3145,7 +3092,6 @@ u3_mesa_io_init(u3_pier* pir_u) vt_init(&mes_u->pit_u); vt_init(&mes_u->per_u); - vt_init(&mes_u->gag_u); vt_init(&mes_u->jum_u); vt_init(&mes_u->req_u); diff --git a/pkg/vere/io/mesa/mesa.h b/pkg/vere/io/mesa/mesa.h index aefcd1c976..c8a77cc2f6 100644 --- a/pkg/vere/io/mesa/mesa.h +++ b/pkg/vere/io/mesa/mesa.h @@ -182,6 +182,17 @@ typedef struct _u3_lane_state { c3_w rtv_w; // round-trip time variance } u3_lane_state; +typedef struct _u3_gage { + c3_w rtt_w; // rtt + c3_w rto_w; // rto + c3_w rtv_w; // rttvar + c3_w wnd_w; // cwnd + c3_w wnf_w; // cwnd fraction + c3_w sst_w; // ssthresh + c3_w con_w; // counter + // +} u3_gage; + typedef struct _u3_peer { void* mes_u; // backpointer u3_ship her_u; // who is this peer @@ -193,6 +204,7 @@ typedef struct _u3_peer { u3_ship lam_u; // galaxy @p u3_lane_state ind_u; // indirect lane state c3_c** dns_c; + u3_gage gag_u; } u3_peer; #define NAME per_map From 58ee752e28d2ba7afac620ac08cfb49ba2be74a5 Mon Sep 17 00:00:00 2001 From: ziffee Date: Fri, 11 Apr 2025 15:05:12 -0600 Subject: [PATCH 27/34] mesa: fix local networking --- pkg/vere/io/ames/lamp.c | 2 ++ pkg/vere/io/mesa.c | 10 +++++----- 2 files changed, 7 insertions(+), 5 deletions(-) diff --git a/pkg/vere/io/ames/lamp.c b/pkg/vere/io/ames/lamp.c index 46b59f41c8..5d4a48b9e8 100644 --- a/pkg/vere/io/ames/lamp.c +++ b/pkg/vere/io/ames/lamp.c @@ -49,7 +49,9 @@ _ames_lamp_lane(u3_auto* car_u, u3_ship her_u, sockaddr_in* lan_u) } if ( c3n == u3_Host.ops_u.net ) { + lan_u->sin_family = AF_INET; lan_u->sin_addr.s_addr = NLOCALHOST; + lan_u->sin_port = per_u->dan_u.sin_port; } else { *lan_u = per_u->dan_u; diff --git a/pkg/vere/io/mesa.c b/pkg/vere/io/mesa.c index 2fe4aa50f8..e3ecf512f7 100644 --- a/pkg/vere/io/mesa.c +++ b/pkg/vere/io/mesa.c @@ -2931,14 +2931,14 @@ _mesa_io_talk(u3_auto* car_u) c3_i ret_i; if ( c3__czar == rac ) { c3_y num_y = (c3_y)mes_u->pir_u->who_u.hed_d; - c3_s zar_s = _ames_czar_port(num_y); + c3_s zar_s = ntohs(_ames_czar_port(num_y)); if ( 0 == por_s ) { por_s = zar_s; } else if ( por_s != zar_s ) { - u3l_log("ames: czar: overriding port %d with -p %d", zar_s, por_s); - u3l_log("ames: czar: WARNING: %d required for discoverability", zar_s); + u3l_log("ames: czar: overriding port %u with -p %u", zar_s, por_s); + u3l_log("ames: czar: WARNING: %u required for discoverability", zar_s); } } @@ -2980,10 +2980,10 @@ _mesa_io_talk(u3_auto* car_u) c3_free(our_s); } if ( c3y == u3_Host.ops_u.net ) { - u3l_log("mesa: live on %d", mes_u->pir_u->por_s); + u3l_log("mesa: live on %u", mes_u->pir_u->por_s); } else { - u3l_log("mesa: live on %d (localhost only)", mes_u->pir_u->por_s); + u3l_log("mesa: live on %u (localhost only)", mes_u->pir_u->por_s); } if ( c3n == mes_u->pir_u->fak_o ) { From 31bf1cb45d6b5e41c2a93740d27943dd3b77eb40 Mon Sep 17 00:00:00 2001 From: ziffee Date: Sun, 13 Apr 2025 14:16:48 -0600 Subject: [PATCH 28/34] mesa: always fetch lanes and sponsors in _meet_peer --- pkg/vere/io/mesa.c | 48 ++++++++++++++++++++++++----------------- pkg/vere/io/mesa/mesa.h | 8 ++++++- 2 files changed, 35 insertions(+), 21 deletions(-) diff --git a/pkg/vere/io/mesa.c b/pkg/vere/io/mesa.c index e3ecf512f7..8c2d7c8fd8 100644 --- a/pkg/vere/io/mesa.c +++ b/pkg/vere/io/mesa.c @@ -1340,6 +1340,7 @@ _mesa_send_bufs(u3_mesa* mes_u, sockaddr_in lan_u = t->sdr_u; if ( !lan_u.sin_port ) { + // XX: noisy u3l_log("mesa: failed to realise lane"); } else { c3_y* sen_y = c3_calloc(len_w); @@ -1777,7 +1778,7 @@ _init_peer(u3_mesa* mes_u, u3_peer* per_u, u3_ship her_u) { memset(per_u, 0, sizeof(*per_u)); per_u->mes_u = mes_u; - per_u->ful_o = c3n; + per_u->liv_e = 0; per_u->lam_o = c3n; per_u->dan_u = (sockaddr_in){0}; _init_lane_state(&per_u->dir_u); @@ -2095,8 +2096,8 @@ _saxo_cb(void* vod_p, u3_noun nun) u3_noun lam = u3do("rear", u3k(sax)); //u3_assert( c3y == u3a_is_cat(gal) && gal < 256 ); // both atoms guaranteed to be cats, bc we don't call unless forwarding - per_u->ful_o = c3y; per_u->lam_u = u3_ship_of_noun(lam); + per_u->liv_e |= u3_peer_lamp; u3z(lam); } @@ -2120,7 +2121,7 @@ _forward_lanes_cb(void* vod_p, u3_noun nun) u3_noun gal = u3h(las); //u3_assert( c3y == u3a_is_cat(gal) && gal < 256 ); // both atoms guaranteed to be cats, bc we don't call unless forwarding - per_u->ful_o = c3y; + per_u->liv_e |= u3_peer_lane; per_u->lam_u = u3_ship_of_noun(gal); u3_noun sal, tal; @@ -2146,22 +2147,18 @@ _meet_peer(u3_mesa* mes_u, u3_peer* per_u) u3_noun her = u3_ship_to_noun(per_u->her_u); u3_noun gan = u3nc(u3_nul, u3_nul); - u3_noun pax = u3nc(u3dc("scot", c3__p, her), u3_nul); - u3_pier_peek_last(mes_u->pir_u, gan, c3__j, c3__saxo, pax, per_u, _saxo_cb); - -} + if ( !(u3_peer_lamp & per_u->liv_e) ) { + u3_noun pax = u3nc(u3dc("scot", c3__p, her), u3_nul); + u3_pier_peek_last(mes_u->pir_u, gan, c3__j, c3__saxo, pax, per_u, _saxo_cb); + } -static void -_get_peer_lanes(u3_mesa* mes_u, u3_peer* per_u) -{ - u3_noun her = u3_ship_to_noun(per_u->her_u); - u3_noun gan = u3nc(u3_nul, u3_nul); - u3_noun pax = u3nq(u3i_string("chums"), - u3dc("scot", 'p', her), - u3i_string("lanes"), - u3_nul); - u3m_p("pax", pax); - u3_pier_peek_last(mes_u->pir_u, gan, c3__ax, u3_nul, pax, per_u, _forward_lanes_cb); + if ( !(u3_peer_lane & per_u->liv_e) ) { + u3_noun pax = u3nq(u3i_string("chums"), + u3dc("scot", 'p', her), + u3i_string("lanes"), + u3_nul); + u3_pier_peek_last(mes_u->pir_u, gan, c3__ax, u3_nul, pax, per_u, _forward_lanes_cb); + } } static void @@ -2416,9 +2413,13 @@ _mesa_forward_request(u3_mesa* mes_u, u3_mesa_pict* pic_u, sockaddr_in lan_u) _init_peer(mes_u, per_u, pac_u->pek_u.nam_u.her_u); per_u->her_u = pac_u->pek_u.nam_u.her_u; - _get_peer_lanes(mes_u, per_u); // forward-lanes + _meet_peer(mes_u, per_u); return; + } else if ( u3_peer_full != per_u->liv_e ) { + _meet_peer(mes_u, per_u); + return; } + if ( c3y == mes_u->for_o && c3y == u3_ships_equal(mes_u->pir_u->who_u, per_u->lam_u) ) { // if ( c3y == mes_u->for_o ) { @@ -2467,6 +2468,10 @@ _mesa_hear_page(u3_mesa_pict* pic_u, sockaddr_in lan_u) per_u = new(&mes_u->par_u, u3_peer, 1); _init_peer(mes_u, per_u, nam_u->her_u); _meet_peer(mes_u, per_u); + return; + } else if ( u3_peer_full != per_u->liv_e ) { + _meet_peer(mes_u, per_u); + return; } c3_o dir_o = __(pac_u->hed_u.hop_y == 0); @@ -2634,7 +2639,6 @@ _mesa_hear_peek(u3_mesa_pict* pic_u, sockaddr_in lan_u) u3_mesa_pact* pac_u = &pic_u->pac_u; u3_mesa* mes_u = pic_u->mes_u; c3_o our_o = u3_ships_equal(pac_u->pek_u.nam_u.her_u, mes_u->pir_u->who_u); - if ( c3n == our_o ) { _mesa_forward_request(mes_u, pic_u, lan_u); return; @@ -2719,6 +2723,10 @@ _mesa_hear_poke(u3_mesa_pict* pic_u, sockaddr_in lan_u) per_u = new(&mes_u->par_u, u3_peer, 1); _init_peer(mes_u, per_u, pac_u->pok_u.pay_u.her_u); _meet_peer(mes_u, per_u); + return; + } else if ( u3_peer_full != per_u->liv_e ) { + _meet_peer(mes_u, per_u); + return; } c3_o dir_o = __(pac_u->hed_u.hop_y == 0); diff --git a/pkg/vere/io/mesa/mesa.h b/pkg/vere/io/mesa/mesa.h index c8a77cc2f6..f229e48d75 100644 --- a/pkg/vere/io/mesa/mesa.h +++ b/pkg/vere/io/mesa/mesa.h @@ -193,10 +193,16 @@ typedef struct _u3_gage { // } u3_gage; +typedef enum _u3_peer_live { + u3_peer_lane = 1 << 0, + u3_peer_lamp = 1 << 1, + u3_peer_full = (1 << 0) | (1 << 1) +} u3_peer_live; + typedef struct _u3_peer { void* mes_u; // backpointer u3_ship her_u; // who is this peer - c3_o ful_o; // has this been initialized? + u3_peer_live liv_e; // has this been initialized? sockaddr_in dan_u; // direct lane (nullable) u3_lane_state dir_u; // direct lane state c3_o lam_o; From 9bce35234c00a24dc1f3c82354a37238ed4ddb36 Mon Sep 17 00:00:00 2001 From: ziffee Date: Mon, 14 Apr 2025 13:00:26 -0600 Subject: [PATCH 29/34] mesa: fix fetching saxo and lane state --- pkg/vere/io/ames/ames.c | 6 +++++ pkg/vere/io/mesa.c | 57 ++++++++--------------------------------- 2 files changed, 17 insertions(+), 46 deletions(-) diff --git a/pkg/vere/io/ames/ames.c b/pkg/vere/io/ames/ames.c index 719c902709..e42b060318 100644 --- a/pkg/vere/io/ames/ames.c +++ b/pkg/vere/io/ames/ames.c @@ -748,6 +748,12 @@ _ames_send(u3_ames* sam_u, sockaddr_in lan_u, u3_ref_hun* hun_u) u3_send_handle* snd_u = c3_calloc(sizeof(*snd_u)); snd_u->hun_u = hun_u; uv_buf_t buf_u = uv_buf_init((c3_c*)hun_u->hun_y, hun_u->len_w); + c3_w nip_w = lan_u.sin_addr.s_addr; + c3_c nip_c[INET_ADDRSTRLEN]; + inet_ntop(AF_INET, &nip_w, nip_c, INET_ADDRSTRLEN); + u3l_log("send ip .%s port %u", + nip_c, ntohs(lan_u.sin_port)); + c3_i sas_i = uv_udp_send(&snd_u->snd_u, sam_u->wax_u, &buf_u, 1, diff --git a/pkg/vere/io/mesa.c b/pkg/vere/io/mesa.c index 8c2d7c8fd8..26f972247f 100644 --- a/pkg/vere/io/mesa.c +++ b/pkg/vere/io/mesa.c @@ -2086,12 +2086,6 @@ _saxo_cb(void* vod_p, u3_noun nun) if ( sax != u3_none ) { u3_noun her = u3h(sax); u3_ship her_u = u3_ship_of_noun(her); - u3_peer* new_u = _mesa_get_peer(per_u->mes_u, her_u); - if ( new_u != NULL ) { - per_u = new_u; - } else { - _mesa_put_peer(per_u->mes_u, her_u, per_u); - } u3_mesa* mes_u = per_u->mes_u; u3_noun lam = u3do("rear", u3k(sax)); //u3_assert( c3y == u3a_is_cat(gal) && gal < 256 ); @@ -2114,10 +2108,6 @@ _forward_lanes_cb(void* vod_p, u3_noun nun) // u3m_p("_forward_lanes_cb", las); if ( las != u3_none ) { - u3_peer* new_u = _mesa_get_peer(per_u->mes_u, per_u->her_u); - if ( new_u != NULL ) { - per_u = new_u; - } u3_noun gal = u3h(las); //u3_assert( c3y == u3a_is_cat(gal) && gal < 256 ); // both atoms guaranteed to be cats, bc we don't call unless forwarding @@ -2133,8 +2123,6 @@ _forward_lanes_cb(void* vod_p, u3_noun nun) per_u->dan_u = (sockaddr_in){0}; } } - - _mesa_put_peer(per_u->mes_u, per_u->her_u, per_u); } u3z(nun); @@ -2148,17 +2136,18 @@ _meet_peer(u3_mesa* mes_u, u3_peer* per_u) u3_noun gan = u3nc(u3_nul, u3_nul); if ( !(u3_peer_lamp & per_u->liv_e) ) { - u3_noun pax = u3nc(u3dc("scot", c3__p, her), u3_nul); - u3_pier_peek_last(mes_u->pir_u, gan, c3__j, c3__saxo, pax, per_u, _saxo_cb); + u3_noun pax = u3nc(u3dc("scot", c3__p, u3k(her)), u3_nul); + u3_pier_peek_last(mes_u->pir_u, u3k(gan), c3__j, c3__saxo, pax, per_u, _saxo_cb); } if ( !(u3_peer_lane & per_u->liv_e) ) { u3_noun pax = u3nq(u3i_string("chums"), - u3dc("scot", 'p', her), + u3dc("scot", 'p', u3k(her)), u3i_string("lanes"), u3_nul); - u3_pier_peek_last(mes_u->pir_u, gan, c3__ax, u3_nul, pax, per_u, _forward_lanes_cb); + u3_pier_peek_last(mes_u->pir_u, u3k(gan), c3__ax, u3_nul, pax, per_u, _forward_lanes_cb); } + u3z(her); u3z(gan); } static void @@ -2402,20 +2391,8 @@ static void _mesa_forward_request(u3_mesa* mes_u, u3_mesa_pict* pic_u, sockaddr_in lan_u) { u3_mesa_pact* pac_u = &pic_u->pac_u; - u3_peer* per_u = _mesa_get_peer(mes_u, pac_u->pek_u.nam_u.her_u); - if ( !per_u ) { - #ifdef MESA_DEBUG - c3_c* mes = u3_ship_to_string(pac_u->pek_u.nam_u.her_u); - u3l_log("mesa: alien forward for %s; meeting ship", mes); - c3_free(mes); - #endif - per_u = new(&mes_u->par_u, u3_peer, 1); - _init_peer(mes_u, per_u, pac_u->pek_u.nam_u.her_u); - per_u->her_u = pac_u->pek_u.nam_u.her_u; - - _meet_peer(mes_u, per_u); - return; - } else if ( u3_peer_full != per_u->liv_e ) { + u3_peer* per_u = _mesa_gut_peer(mes_u, pac_u->pek_u.nam_u.her_u); + if ( u3_peer_full != per_u->liv_e ) { _meet_peer(mes_u, per_u); return; } @@ -2461,15 +2438,9 @@ _mesa_hear_page(u3_mesa_pict* pic_u, sockaddr_in lan_u) c3_o our_o = u3_ships_equal(nam_u->her_u, mes_u->pir_u->who_u); - u3_peer* per_u = _mesa_get_peer(mes_u, nam_u->her_u); + u3_peer* per_u = _mesa_gut_peer(mes_u, nam_u->her_u); c3_o new_o = c3n; - if ( NULL == per_u ) { - new_o = c3y; - per_u = new(&mes_u->par_u, u3_peer, 1); - _init_peer(mes_u, per_u, nam_u->her_u); - _meet_peer(mes_u, per_u); - return; - } else if ( u3_peer_full != per_u->liv_e ) { + if ( u3_peer_full != per_u->liv_e ) { _meet_peer(mes_u, per_u); return; } @@ -2716,15 +2687,9 @@ _mesa_hear_poke(u3_mesa_pict* pic_u, sockaddr_in lan_u) // XX if this lane management stuff is necessary // it should be deferred to after successful event processing - u3_peer* per_u = _mesa_get_peer(mes_u, pac_u->pok_u.pay_u.her_u); + u3_peer* per_u = _mesa_gut_peer(mes_u, pac_u->pok_u.pay_u.her_u); c3_o new_o = c3n; - if ( NULL == per_u ) { - new_o = c3y; - per_u = new(&mes_u->par_u, u3_peer, 1); - _init_peer(mes_u, per_u, pac_u->pok_u.pay_u.her_u); - _meet_peer(mes_u, per_u); - return; - } else if ( u3_peer_full != per_u->liv_e ) { + if ( u3_peer_full != per_u->liv_e ) { _meet_peer(mes_u, per_u); return; } From 67dea083b0c391db9aea3fdee4199fd616522f3c Mon Sep 17 00:00:00 2001 From: ziffee Date: Sun, 20 Apr 2025 16:55:18 -0600 Subject: [PATCH 30/34] ames: fix forward lane discovery --- pkg/vere/io/ames/ames.c | 141 ++++++++++++++++++++++++++++------------ 1 file changed, 98 insertions(+), 43 deletions(-) diff --git a/pkg/vere/io/ames/ames.c b/pkg/vere/io/ames/ames.c index e42b060318..32943578be 100644 --- a/pkg/vere/io/ames/ames.c +++ b/pkg/vere/io/ames/ames.c @@ -865,14 +865,15 @@ static c3_o _ames_lane_from_peer(u3_ames* sam_u, u3_peer* per_u, sockaddr_in lan_u[2]) { + if ( NULL == per_u ) return c3n; + if ( u3_peer_full != per_u->liv_e ) return c3n; if ( c3y == u3_ships_equal(per_u->her_u, sam_u->pir_u->who_u) ) return c3y; - if ( NULL == per_u ) return c3n; if ( c3y == _mesa_is_lane_zero(per_u->dan_u) ) { if ( c3y == u3_ships_equal(per_u->lam_u, sam_u->pir_u->who_u) ) return c3y; u3_peer* lam_u = _mesa_get_peer(sam_u->mes_u, per_u->lam_u); - if ( NULL == lam_u || + if ( (NULL == lam_u) || (c3y == _mesa_is_lane_zero(lam_u->dan_u)) ) return c3y; lan_u[0] = lam_u->dan_u; @@ -885,7 +886,8 @@ _ames_lane_from_peer(u3_ames* sam_u, if ( c3y == u3_ships_equal(per_u->lam_u, sam_u->pir_u->who_u) ) return c3y; u3_peer* lam_u = _mesa_get_peer(sam_u->mes_u, per_u->lam_u); - if ( NULL == lam_u || (c3y == _mesa_is_lane_zero(lam_u->dan_u)) ) + if ( (NULL == lam_u) || + (c3y == _mesa_is_lane_zero(lam_u->dan_u)) ) return c3y; lan_u[1] = lam_u->dan_u; return c3y; @@ -958,6 +960,92 @@ _ames_is_czar(u3_noun who) c3_o _ames_lamp_lane(u3_mesa_auto* mes_u, u3_ship her_u, sockaddr_in* lan_u); +static void +_saxo_cb(void* vod_p, u3_noun nun) +{ + u3_pact* pac_u = vod_p; + u3_ames* sam_u = pac_u->sam_u; + + u3_weak sax = u3r_at(7, nun); + + u3_peer* per_u = NULL; + if ( sax != u3_none ) { + per_u = _mesa_gut_peer(sam_u->mes_u, pac_u->pre_u.rec_u); + u3_noun her = u3h(sax); + u3_ship her_u = u3_ship_of_noun(her); + u3_noun lam = u3do("rear", u3k(sax)); + //u3_assert( c3y == u3a_is_cat(gal) && gal < 256 ); + // both atoms guaranteed to be cats, bc we don't call unless forwarding + per_u->lam_u = u3_ship_of_noun(lam); + per_u->liv_e |= u3_peer_lamp; + u3z(lam); + } + + u3z(nun); +} + +sockaddr_in +_mesa_realise_lane(u3_mesa_auto* mes_u, u3_noun lan); + +static void +_ames_lane_scry_cb(u3_pact* pac_u, u3_peer* per_u); + +static void +_forward_lanes_cb(void* vod_p, u3_noun nun) +{ + u3_pact* pac_u = vod_p; + u3_ames* sam_u = pac_u->sam_u; + + u3_weak las = u3r_at(7, nun); + // u3m_p("_forward_lanes_cb", las); + + u3_peer* per_u = NULL; + if ( las != u3_none ) { + per_u = _mesa_gut_peer(sam_u->mes_u, pac_u->pre_u.rec_u); + u3_noun gal = u3h(las); + //u3_assert( c3y == u3a_is_cat(gal) && gal < 256 ); + // both atoms guaranteed to be cats, bc we don't call unless forwarding + per_u->liv_e |= u3_peer_lane; + per_u->lam_u = u3_ship_of_noun(gal); + u3_noun sal, tal; + + if ( c3n == per_u->lam_o ) { + if ( (c3y == u3r_cell(u3t(las), &sal, &tal)) && + (c3y == u3du(sal)) ) { + per_u->dan_u = _mesa_realise_lane(sam_u->mes_u, u3k(sal)); + } else { + per_u->dan_u = (sockaddr_in){0}; + } + } + } + _ames_lane_scry_cb(pac_u, per_u); + + u3z(nun); + +} + +static void +_meet_peer(u3_ames* sam_u, u3_pact* pac_u) +{ + u3_peer* per_u = _mesa_get_peer(sam_u->mes_u, pac_u->pre_u.rec_u); + u3_noun her = u3_ship_to_noun(pac_u->pre_u.rec_u); + u3_noun gan = u3nc(u3_nul, u3_nul); + + if ( (NULL == per_u) || !(u3_peer_lamp & per_u->liv_e) ) { + u3_noun pax = u3nc(u3dc("scot", c3__p, u3k(her)), u3_nul); + u3_pier_peek_last(sam_u->mes_u->pir_u, u3k(gan), c3__j, c3__saxo, pax, pac_u, _saxo_cb); + } + + if ( (NULL == per_u) || !(u3_peer_lane & per_u->liv_e) ) { + u3_noun pax = u3nq(u3i_string("chums"), + u3dc("scot", 'p', u3k(her)), + u3i_string("lanes"), + u3_nul); + u3_pier_peek_last(sam_u->mes_u->pir_u, u3k(gan), c3__ax, u3_nul, pax, pac_u, _forward_lanes_cb); + } + u3z(her); u3z(gan); +} + /* _ames_send_lane(): resolve/decode lane. RETAIN */ static c3_o @@ -1194,18 +1282,20 @@ _ames_send_many(u3_pact* pac_u, sockaddr_in lan_u[2], c3_o for_o) } } + /* _ames_lane_scry_cb(): learn lanes to send packet on */ static void -_ames_lane_scry_cb(void* vod_p, u3_noun nun) +_ames_lane_scry_cb(u3_pact* pac_u, u3_peer* per_u) { - u3_pact* pac_u = vod_p; u3_ames* sam_u = pac_u->sam_u; - u3_weak las = u3r_at(7, nun); + + if ( c3y == pac_u->for_o ) + sam_u->sat_u.foq_d--; // if scry fails, remember we can't scry, and just inject the packet // - if ( u3_none == las ) { + if ( (NULL == per_u) || (u3_peer_full != per_u->liv_e) ) { if ( 5 < ++sam_u->sat_u.saw_d ) { u3l_log("ames: giving up scry"); sam_u->fig_u.see_o = c3n; @@ -1216,14 +1306,6 @@ _ames_lane_scry_cb(void* vod_p, u3_noun nun) } else { sam_u->sat_u.saw_d = 0; - - u3_ship who_u = pac_u->pre_u.rec_u; - u3_noun who = u3_ship_to_noun(who_u); - // cache the scry result for later use - // - u3_peer* per_u = _ames_lane_into_cache(sam_u, - who, - u3k(las)); sockaddr_in lan_u[2]; _ames_lane_from_peer(sam_u, per_u, lan_u); @@ -1234,20 +1316,6 @@ _ames_lane_scry_cb(void* vod_p, u3_noun nun) } _ames_pact_free(pac_u); } - u3z(nun); -} - -/* _ames_lane_scry_forward_cb(): learn lanes to forward packet on - */ -static void -_ames_lane_scry_forward_cb(void *vod_p, u3_noun nun) -{ - u3_pact* pac_u = vod_p; - u3_ames *sam_u = pac_u->sam_u; - - sam_u->sat_u.foq_d--; - - _ames_lane_scry_cb(vod_p, nun); } /* _ames_try_send(): try to send a packet to a ship and its sponsors @@ -1304,23 +1372,10 @@ _ames_try_send(u3_pact* pac_u) // store the packet to be sent later when the lane scry completes // else { - u3_noun pax = _lane_scry_path(u3_ship_to_noun(pac_u->pre_u.rec_u)); - - // if forwarding, enqueue the packet and scry for the lane - // - u3_noun gang = u3nc(u3_nul, u3_nul); if ( c3y == pac_u->for_o ) { sam_u->sat_u.foq_d++; - - u3_pier_peek_last(sam_u->pir_u, gang, c3__ax, - u3_nul, pax, pac_u, _ames_lane_scry_forward_cb); - } - // otherwise, just scry for the lane - // - else { - u3_pier_peek_last(sam_u->pir_u, gang, c3__ax, - u3_nul, pax, pac_u, _ames_lane_scry_cb); } + _meet_peer(sam_u, pac_u); } } From 101bcc8f35e041f4b14b6b8857ed5f2bc6851c1f Mon Sep 17 00:00:00 2001 From: novord Date: Thu, 9 Oct 2025 11:29:47 -0400 Subject: [PATCH 31/34] otw to fake comets --- pkg/noun/manage.c | 5 ++++ pkg/vere/dawn.c | 52 ++++++++++++++++++----------------------- pkg/vere/io/ames/ames.c | 2 +- pkg/vere/king.c | 42 ++++++++++++++++++++++++--------- pkg/vere/main.c | 12 +++++----- pkg/vere/pier.c | 43 ++++++++++++++++++---------------- pkg/vere/vere.h | 9 ++++++- 7 files changed, 97 insertions(+), 68 deletions(-) diff --git a/pkg/noun/manage.c b/pkg/noun/manage.c index fef1866c84..f9078e2785 100644 --- a/pkg/noun/manage.c +++ b/pkg/noun/manage.c @@ -1490,6 +1490,11 @@ u3m_soft_esc(u3_noun ref, u3_noun sam) /* Assert preconditions. */ { + if ( 0 == u3R->ski.gul ) { + u3t_slog_hela(0); + fflush(stderr); + fflush(stdout); + } u3_assert(0 != u3R->ski.gul); gul = u3h(u3R->ski.gul); } diff --git a/pkg/vere/dawn.c b/pkg/vere/dawn.c index fb8457c2af..e340b671ca 100644 --- a/pkg/vere/dawn.c +++ b/pkg/vere/dawn.c @@ -143,10 +143,10 @@ _dawn_get_jam(c3_c* url_c) return u3ke_cue(jammed); } -/* _dawn_eth_rpc(): ethereum JSON RPC with request/response as +octs +/* _dawn_gat_rpc(): ethereum JSON RPC with request/response as +octs */ static u3_noun -_dawn_eth_rpc(c3_c* url_c, u3_noun oct) +_dawn_gat_rpc_old(c3_c* url_c, u3_noun oct) { uv_buf_t buf_u = _dawn_post_json(url_c, _dawn_oct_to_buf(oct)); u3_noun pro = _dawn_buf_to_oct(buf_u); @@ -273,9 +273,7 @@ u3_dawn_vent(u3_noun ship, u3_noun feed) u3_noun rank = u3do("clan:title", u3k(ship)); - c3_c* url_c = ( 0 != u3_Host.ops_u.eth_c ) ? - u3_Host.ops_u.eth_c : - "https://roller.urbit.org/v1/azimuth"; + c3_c url_c[4096]; { // +point:azimuth: on-chain state @@ -295,12 +293,11 @@ u3_dawn_vent(u3_noun ship, u3_noun feed) u3_Host.ops_u.who_c); { - u3_noun oct = u3do("point:give:dawn", u3k(ship)); - u3_noun luh = _dawn_eth_rpc(url_c, u3k(oct)); + sprintf(url_c, "%s/_~_/=pynt=/j/%s", + u3_Host.ops_u.gat_c, u3_Host.ops_u.who_c); + u3_noun top = u3_king_get_noun(url_c); - pot = _dawn_need_unit(u3dc("point:take:dawn", u3k(ship), u3k(luh)), - "boot: failed to retrieve public keys"); - u3z(oct); u3z(luh); + pot = _dawn_need_unit(top, "boot: failed to retrieve public keys"); } } @@ -342,12 +339,9 @@ u3_dawn_vent(u3_noun ship, u3_noun feed) { u3l_log("boot: retrieving galaxy table"); - u3_noun oct = u3v_wish("czar:give:dawn"); - u3_noun raz = _dawn_eth_rpc(url_c, u3k(oct)); - - zar = _dawn_need_unit(u3do("czar:take:dawn", u3k(raz)), - "boot: failed to retrieve galaxy table"); - u3z(oct); u3z(raz); + sprintf(url_c, "%s/_~_/=lamp=/j", + u3_Host.ops_u.gat_c); + zar = u3_king_get_noun(url_c); } // (list turf): ames domains @@ -358,12 +352,9 @@ u3_dawn_vent(u3_noun ship, u3_noun feed) else { u3l_log("boot: retrieving network domains"); - u3_noun oct = u3v_wish("turf:give:dawn"); - u3_noun fut = _dawn_eth_rpc(url_c, u3k(oct)); - - tuf = _dawn_need_unit(u3do("turf:take:dawn", u3k(fut)), - "boot: failed to retrieve network domains"); - u3z(oct); u3z(fut); + sprintf(url_c, "%s/_~_/=turf=/j", + u3_Host.ops_u.gat_c); + u3_noun tuf = u3_king_get_noun(url_c); } pon = u3_nul; @@ -382,15 +373,18 @@ u3_dawn_vent(u3_noun ship, u3_noun feed) // retrieve +point:azimuth of pos (sponsor of ship) // { - u3_noun oct = u3do("point:give:dawn", u3k(pos)); - u3_noun luh = _dawn_eth_rpc(url_c, u3k(oct)); - - son = _dawn_need_unit(u3dc("point:take:dawn", u3k(pos), u3k(luh)), - "boot: failed to retrieve sponsor keys"); + u3_noun top = u3dc("scot", c3__p, u3k(pos)); + c3_c* pot_c = u3r_string(top); + u3z(top); + sprintf(url_c, "%s/_~_/=pynt=/j/%s", + u3_Host.ops_u.gat_c, pot_c); + u3_noun nos = u3_king_get_noun(url_c); + c3_free(pot_c); + + son = _dawn_need_unit(top, "boot: failed to retrieve public keys"); // append to sponsor chain list // - pon = u3nc(u3nc(u3k(pos), u3k(son)), pon); - u3z(oct); u3z(luh); + pon = u3nc(u3nc(u3k(pos), son), pon); } // find next sponsor diff --git a/pkg/vere/io/ames/ames.c b/pkg/vere/io/ames/ames.c index 9dcc9b4236..1b9963ceeb 100644 --- a/pkg/vere/io/ames/ames.c +++ b/pkg/vere/io/ames/ames.c @@ -1059,7 +1059,7 @@ _ames_send_lane(u3_ames* sam_u, u3_noun lan, sockaddr_in* lan_u) } switch ( tag ) { - case c3y: { // galaxy + case c3y: { // galaxy/lamp return _ames_lamp_lane(sam_u->mes_u, u3_ship_of_noun(val), lan_u); } diff --git a/pkg/vere/king.c b/pkg/vere/king.c index 36a24dbe41..88badb59aa 100644 --- a/pkg/vere/king.c +++ b/pkg/vere/king.c @@ -90,8 +90,6 @@ 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 @@ -177,7 +175,7 @@ _king_prop() case 2: { // url u3l_log("boot: downloading prop %s", vex_u->loc_c); - u3_atom jam = _king_get_atom(vex_u->loc_c); + u3_atom jam = u3_king_get_atom(vex_u->loc_c); mor = u3nc(u3ke_cue(jam), mor); } break; @@ -189,7 +187,7 @@ _king_prop() "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); + u3_atom jam = u3_king_get_atom(url_c); mor = u3nc(u3ke_cue(jam), mor); } break; @@ -344,10 +342,10 @@ king_curl_bytes(c3_c* url_c, c3_w* len_w, c3_y** hun_y, c3_t veb_t, c3_y tri_y) return ret_i; } -/* _king_get_atom(): HTTP GET url_c, produce response body as atom. +/* u3_king_get_atom(): HTTP GET url_c, produce response body as atom. */ -static u3_noun -_king_get_atom(c3_c* url_c) +u3_atom +u3_king_get_atom(c3_c* url_c) { c3_w len_w; c3_y* hun_y; @@ -363,6 +361,28 @@ _king_get_atom(c3_c* url_c) return pro; } +u3_noun +u3_king_get_noun(c3_c* url_c) { + c3_w len_w; + c3_y* byt_y; + + if ( king_curl_bytes(url_c, &len_w, &byt_y, 1, 5) ) { + u3_king_bail(); + exit(1); + } + + u3_cue_xeno* sil_u = u3s_cue_xeno_init_with(ur_fib27, ur_fib28); + u3_weak pro; + + if ( u3_none == (pro = u3s_cue_xeno_with(sil_u, len_w, byt_y)) ) { + u3l_log("u3_king_get_noun: unable to cue noun"); + exit(1); + } + + u3s_cue_xeno_done(sil_u); + return pro; +} + /* _king_get_pace(): get "pace" (release channel name). */ static c3_c* @@ -476,10 +496,10 @@ u3_king_next(c3_c* pac_c, c3_c** out_c) /* _get_cmd_output(): Run a shell command and capture its output. Exits with an error if the command fails or produces no output. The 'out_c' parameter should be an array of sufficient length to hold - the command's output, up to a max of len_c characters. + the command's output, up to a max of len_w characters. */ static void -_get_cmd_output(c3_c *cmd_c, c3_c *out_c, c3_w len_c) +_get_cmd_output(c3_c *cmd_c, c3_c *out_c, c3_w len_w) { FILE *fp = popen(cmd_c, "r"); if ( NULL == fp ) { @@ -487,7 +507,7 @@ _get_cmd_output(c3_c *cmd_c, c3_c *out_c, c3_w len_c) exit(1); } - if ( NULL == fgets(out_c, len_c, fp) ) { + if ( NULL == fgets(out_c, len_w, fp) ) { u3l_log("'%s' produced no output", cmd_c); exit(1); } @@ -555,7 +575,7 @@ _boothack_pill(void) } u3l_log("boot: downloading pill %s", url_c); - pil = _king_get_atom(url_c); + pil = u3_king_get_atom(url_c); } if ( 0 != u3_Host.ops_u.arv_c ) { diff --git a/pkg/vere/main.c b/pkg/vere/main.c index 4eb827235d..5e34f66ac9 100644 --- a/pkg/vere/main.c +++ b/pkg/vere/main.c @@ -261,7 +261,7 @@ _main_getopt(c3_i argc, c3_c** argv) { "pier", required_argument, NULL, 'c' }, { "replay", no_argument, NULL, 'D' }, { "daemon", no_argument, NULL, 'd' }, - { "ethereum", required_argument, NULL, 'e' }, + { "gateway", required_argument, NULL, 'g' }, { "fake", required_argument, NULL, 'F' }, { "key-string", required_argument, NULL, 'G' }, { "gc", no_argument, NULL, 'g' }, @@ -438,10 +438,6 @@ _main_getopt(c3_i argc, c3_c** argv) u3_Host.ops_u.nuu = c3y; break; } - case 'e': { - u3_Host.ops_u.eth_c = strdup(optarg); - break; - } case 'F': { u3_Host.ops_u.fak_c = _main_presig(optarg); u3_Host.ops_u.net = c3n; @@ -512,6 +508,10 @@ _main_getopt(c3_i argc, c3_c** argv) u3_Host.ops_u.nuu = c3y; break; } + case 'W': { + u3_Host.ops_u.gat_c = strdup(optarg); + break; + } case 'X': { u3_Host.ops_u.pek_c = strdup(optarg); break; @@ -855,7 +855,6 @@ u3_ve_usage(c3_i argc, c3_c** argv) "-c, --pier PIER Create a new urbit in /\n", "-D, --replay Recompute from events\n", "-d, --daemon Daemon mode; implies -t\n", - "-e, --ethereum URL Ethereum gateway\n", "-F, --fake SHIP Boot fake urbit; also disables networking\n", "-G, --key-string STRING Private key string (@uw, see also -k)\n" "-g, --gc Set GC flag\n", @@ -886,6 +885,7 @@ u3_ve_usage(c3_i argc, c3_c** argv) "-u, --bootstrap-url URL URL from which to download pill\n", "-v, --verbose Verbose\n", "-w, --name NAME Initial boot as ~name (with ticket)\n", + "-W, --cards CARDS PKI init cards\n", "-X, --scry PATH Scry, write to file, then exit\n", "-x, --exit Exit immediately\n", "-Y, --scry-into FILE Optional name of file (for -X)\n", diff --git a/pkg/vere/pier.c b/pkg/vere/pier.c index 297ee732cd..8e38e1d1b0 100644 --- a/pkg/vere/pier.c +++ b/pkg/vere/pier.c @@ -553,14 +553,14 @@ _resolve_czar(u3_ship who_u, c3_c* who_c) } static c3_o -_czar_boot_data(c3_c* czar_c, - c3_c* who_c, - c3_w* bone_w, - c3_w* czar_glx_w, - c3_w* czar_ryf_w, - c3_w* czar_lyf_w, - c3_w* czar_bon_w, - c3_w* czar_ack_w) +_czar_boot_data(c3_c* czar_c, + c3_c* who_c, + c3_w* bone_w, + u3_ship* czar_glx_u, + c3_w* czar_ryf_w, + c3_w* czar_lyf_w, + c3_w* czar_bon_w, + c3_w* czar_ack_w) { c3_c url[256]; c3_w len_w; @@ -583,9 +583,9 @@ _czar_boot_data(c3_c* czar_c, if ( (c3y == u3r_hext(cued, 0, &czar_glx, &czar_ryf, &czar_lyf, &czar_bon, &czar_ack)) && - (c3y == u3r_safe_word(czar_glx, czar_glx_w)) && (c3y == u3r_safe_word(czar_ryf, czar_ryf_w)) && (c3y == u3r_safe_word(czar_lyf, czar_lyf_w)) ) { + *czar_glx_u = u3_ship_of_noun(czar_glx); if ( c3y == u3du(czar_bon) ) u3r_safe_word(u3t(czar_bon), czar_bon_w); if ( c3y == u3du(czar_ack) ) u3r_safe_word(u3t(czar_ack), czar_ack_w); ret_o = c3y; @@ -698,10 +698,12 @@ _boot_scry_cb(void* vod_p, u3_noun nun) c3_c* who_c = u3r_string(who); u3_noun rem, glx, ryf, bon, cur, nex; - c3_w glx_w, ryf_w, bon_w, cur_w, nex_w; + c3_w ryf_w, bon_w, cur_w, nex_w; + u3_ship glx_u; - c3_w czar_glx_w, czar_ryf_w, czar_lyf_w, czar_bon_w, czar_ack_w; - czar_glx_w = czar_ryf_w = czar_lyf_w = czar_bon_w = czar_ack_w = u3_none; + c3_w czar_ryf_w, czar_lyf_w, czar_bon_w, czar_ack_w; + czar_ryf_w = czar_lyf_w = czar_bon_w = czar_ack_w = u3_none; + u3_ship czar_glx_u; if ( (c3y == u3r_qual(nun, 0, 0, 0, &rem)) && (c3y == u3r_hext(rem, &glx, &ryf, 0, &bon, &cur, &nex)) ) { @@ -709,15 +711,14 @@ _boot_scry_cb(void* vod_p, u3_noun nun) * Boot scry succeeded. Proceed to cross reference networking state against * sponsoring galaxy. */ - glx_w = u3r_word(0, glx); ryf_w = u3r_word(0, ryf); + glx_u = u3_ship_of_noun(glx); ryf_w = u3r_word(0, ryf); bon_w = u3r_word(0, bon); cur_w = u3r_word(0, cur); nex_w = u3r_word(0, nex); - u3_atom czar = u3dc("scot", c3__p, glx_w); - c3_c* czar_c = u3r_string(czar); + c3_c* czar_c = u3_ship_to_string(glx_u); if ( c3n == _czar_boot_data(czar_c, who_c, &bon_w, - &czar_glx_w, &czar_ryf_w, + &czar_glx_u, &czar_ryf_w, &czar_lyf_w, &czar_bon_w, &czar_ack_w) ) { u3l_log("boot: peer-state unvailable on czar, cannot protect from double-boot"); @@ -750,18 +751,20 @@ _boot_scry_cb(void* vod_p, u3_noun nun) } } - u3z(czar); c3_free(czar_c); - } else if ( c3y == u3r_trel(nun, 0, 0, &rem) && rem == 0 ) { + } else if ( c3y == u3r_trel(nun, 0, 0, &rem) && + c3y == u3r_cell(rem, 0, &glx) ) { /* * Data not available for boot scry. Check against sponsoring galaxy. * If peer state exists exit(1) unless ship has breached, * otherwise continue boot. */ - c3_c* czar_c = _resolve_czar(pir_u->who_u, who_c); + + glx_u = u3_ship_of_noun(glx); ryf_w = u3r_word(0, ryf); + c3_c* czar_c = u3_ship_to_string(glx_u); if ( c3n == _czar_boot_data(czar_c, who_c, 0, - &czar_glx_w, &czar_ryf_w, + &czar_glx_u, &czar_ryf_w, &czar_lyf_w, &czar_bon_w, 0) ) { c3_free(czar_c); _pier_wyrd_init(pir_u); diff --git a/pkg/vere/vere.h b/pkg/vere/vere.h index cc52308c74..8b1f126c6a 100644 --- a/pkg/vere/vere.h +++ b/pkg/vere/vere.h @@ -273,7 +273,7 @@ c3_w hap_w; // -C, cap transient memo cache c3_o dry; // -D, dry compute, no checkpoint c3_o dem; // -d, daemon - c3_c* eth_c; // -e, ethereum node url + c3_c* gat_c; // -g, PKI gateway node url c3_c* fak_c; // -F, fake ship c3_c* gen_c; // -G, czar generator c3_o gab; // -g, test garbage collection @@ -1583,6 +1583,13 @@ c3_c* dir_c, // output directory c3_t lin_t); // link to $pier/.run + + u3_atom + u3_king_get_atom(c3_c* url_c); + + u3_noun + u3_king_get_noun(c3_c* url_c); + /* u3_daemon_init(): platform-specific daemon mode initialization. */ void From 08e8e3a09074be5cc936a94b0728b35109c51770 Mon Sep 17 00:00:00 2001 From: Tinnus Napbus Date: Fri, 10 Oct 2025 21:00:09 +1300 Subject: [PATCH 32/34] gw: fake comets --- pkg/vere/king.c | 67 ++++++++++++++++++++++++++++++++++++------------- 1 file changed, 50 insertions(+), 17 deletions(-) diff --git a/pkg/vere/king.c b/pkg/vere/king.c index 88badb59aa..b630f4b36e 100644 --- a/pkg/vere/king.c +++ b/pkg/vere/king.c @@ -70,9 +70,9 @@ :: Should require auth to the daemon itself [%dawn p=seed] :: boot with fake keys :: - :: p: identity + :: seed for fake comets, @p for others :: - [%fake p=ship] + [%fake $@(=ship [~ =feed])] == :: +pill: boot-sequence ingredients :: @@ -87,7 +87,7 @@ void _king_doom(u3_noun doom); void _king_boot(u3_noun boot); void _king_come(u3_noun star, u3_noun pill, u3_noun path); void _king_dawn(u3_noun seed, u3_noun pill, u3_noun path); - void _king_fake(u3_noun ship, u3_noun pill, u3_noun path); + void _king_fake(u3_noun farg, u3_noun pill, u3_noun path); void _king_pier(u3_noun pier); /* _king_defy_fate(): invalid fate @@ -205,12 +205,22 @@ _king_prop() /* _king_fake(): boot with fake keys */ void -_king_fake(u3_noun ship, u3_noun pill, u3_noun path) +_king_fake(u3_noun farg, u3_noun pill, u3_noun path) { + u3_noun ship; + + if ( c3y == u3a_is_atom(farg) ) + ship = farg; + else { + ship = ( c3y == u3a_is_cell(u3h(u3t(farg))) ) + ? u3h(u3t(u3t(farg))) + : u3h(u3t(farg)); + } // XX link properly // - u3_noun vent = u3nc(c3__fake, u3k(ship)); - u3K.pir_u = u3_pier_boot(sag_w, ship, vent, pill, path, + + u3_noun vent = u3nc(c3__fake, u3k(farg)); + u3K.pir_u = u3_pier_boot(sag_w, u3k(ship), vent, pill, path, u3_none, _king_prop()); } @@ -627,23 +637,39 @@ _boothack_key(u3_noun kef) u3z(kef); } - if ( 0 != u3_Host.ops_u.who_c ) { - u3_noun woh = u3i_string(u3_Host.ops_u.who_c); + if ( 0 != u3_Host.ops_u.who_c || + ( 0 != u3_Host.ops_u.fak_c && + 28 < strlen(u3_Host.ops_u.fak_c) ) ) { + u3_noun woh; + + if ( 0 != u3_Host.ops_u.fak_c ) + woh = u3i_string(u3_Host.ops_u.fak_c); + else + woh = u3i_string(u3_Host.ops_u.who_c); + u3_noun whu = u3dc("slaw", 'p', u3k(woh)); if ( u3_nul == whu ) { - u3l_log("dawn: invalid ship specified with -w %s", - u3_Host.ops_u.who_c); + if ( 0 != u3_Host.ops_u.fak_c ) + u3l_log("boot: malformed -F ship %s", + u3_Host.ops_u.fak_c); + else + u3l_log("dawn: invalid ship specified with -w %s", + u3_Host.ops_u.who_c); exit(1); } if ( (u3_none != ship) && - (c3n == u3r_sing(ship, u3t(whu))) ) - { + (c3n == u3r_sing(ship, u3t(whu))) ) { u3_noun how = u3dc("scot", 'p', u3k(ship)); c3_c* how_c = u3r_string(u3k(how)); - u3l_log("dawn: mismatch between -w %s and -K %s", - u3_Host.ops_u.who_c, how_c); + + if ( 0 != u3_Host.ops_u.fak_c ) + u3l_log("boot: mismatch between -F %s and -K %s", + u3_Host.ops_u.fak_c, how_c); + else + u3l_log("dawn: mismatch between -w %s and -K %s", + u3_Host.ops_u.who_c, how_c); u3z(how); c3_free(how_c); @@ -668,7 +694,8 @@ _boothack_doom(void) if ( c3n == u3_Host.ops_u.nuu ) { return u3nt(c3__pier, u3_nul, pax); } - else if ( 0 != u3_Host.ops_u.fak_c ) { + else if ( 0 != u3_Host.ops_u.fak_c && + 28 > strlen(u3_Host.ops_u.fak_c) ) { u3_noun fak = u3i_string(u3_Host.ops_u.fak_c); u3_noun whu = u3dc("slaw", 'p', u3k(fak)); @@ -682,7 +709,9 @@ _boothack_doom(void) u3z(whu); u3z(fak); } - else if ( 0 != u3_Host.ops_u.who_c ) { + else if ( 0 != u3_Host.ops_u.who_c || + ( 0 != u3_Host.ops_u.fak_c && + 28 < strlen(u3_Host.ops_u.fak_c) ) ) { u3_noun kef; if ( 0 != u3_Host.ops_u.key_c ) { @@ -711,7 +740,11 @@ _boothack_doom(void) exit(1); } - bot = u3nc(c3__dawn, _boothack_key(kef)); + if ( 0 != u3_Host.ops_u.fak_c ) { + bot = u3nc(c3__fake, u3nc(u3_nul, _boothack_key(kef))); + } + else + bot = u3nc(c3__dawn, _boothack_key(kef)); } else { // XX allow parent star to be specified? From 1b0ba585a76adb919c17fd67813bfae7b3f95e6a Mon Sep 17 00:00:00 2001 From: Tinnus Napbus Date: Wed, 29 Oct 2025 21:39:33 +1300 Subject: [PATCH 33/34] gw: fix merge errors --- pkg/noun/jets.c | 4 ++-- pkg/vere/disk.c | 2 +- pkg/vere/io/ames/ames.c | 21 ++++++--------------- pkg/vere/io/mesa.c | 4 ++++ pkg/vere/mars.c | 5 ++--- pkg/vere/pier.c | 2 +- 6 files changed, 16 insertions(+), 22 deletions(-) diff --git a/pkg/noun/jets.c b/pkg/noun/jets.c index d45e0ba4c2..cca0d6449f 100644 --- a/pkg/noun/jets.c +++ b/pkg/noun/jets.c @@ -882,9 +882,9 @@ _cj_kick_z(u3_noun cor, u3j_core* cop_u, u3j_harm* ham_u, u3_atom axe) else { u3_weak pro, ame; - //ham_u->ice = c3y; + ham_u->ice = c3y; pro = ham_u->fun_f(cor); - //ham_u->ice = c3n; + ham_u->ice = c3n; #ifdef U3_MEMORY_DEBUG u3a_lop(cod_w); diff --git a/pkg/vere/disk.c b/pkg/vere/disk.c index 3dd476b6e8..121526cc82 100644 --- a/pkg/vere/disk.c +++ b/pkg/vere/disk.c @@ -480,7 +480,7 @@ u3_disk_save_meta(MDB_env* mdb_u, const u3_meta* met_u) { u3_assert( c3y == u3a_is_cat(met_u->lif_w) ); - u3_noun who = u3i_chubs(2, (c3_d*)&met_u->who_u); + u3_noun who = u3_ship_to_noun(met_u->who_u); if ( (c3n == _disk_save_meta(mdb_u, "version", sizeof(c3_w), (c3_y*)&met_u->ver_w)) || (c3n == _disk_save_meta(mdb_u, "who", sizeof(u3_ship), (c3_y*)&met_u->who_u)) diff --git a/pkg/vere/io/ames/ames.c b/pkg/vere/io/ames/ames.c index 49a5bee052..5488dbd981 100644 --- a/pkg/vere/io/ames/ames.c +++ b/pkg/vere/io/ames/ames.c @@ -260,7 +260,8 @@ _ames_pact_free(u3_pact* pac_u) default: u3l_log("ames_pact_free: bad packet type %s", _str_typ(pac_u->typ_y)); - u3_pier_bail(u3_king_stub()); + u3_king_bail(); + exit(1); } _ames_ref_hun_lose(pac_u->hun_u); @@ -1844,7 +1845,8 @@ _ames_hear(u3_ames* sam_u, default: { u3l_log("ames_hear: bad packet type %d", pac_u->typ_y); - u3_pier_bail(u3_king_stub()); + u3_king_bail(); + exit(1); } } } @@ -2103,8 +2105,8 @@ _ames_io_slog(u3_ames* sam_u) u3l_log(" lane scry fails: %" PRIu64, sam_u->sat_u.saw_d); } -static u3m_quac** -_ames_io_mark(u3_auto* car_u, c3_w *out_w) +u3m_quac** +_ames_io_mark(u3_auto* car_u, c3_w* out_w) { u3m_quac** all_u = c3_malloc(2 * sizeof(*all_u)); u3_ames *sam_u = (u3_ames*)car_u; @@ -2114,17 +2116,6 @@ _ames_io_mark(u3_auto* car_u, c3_w *out_w) all_u[0]->siz_w = 4 * u3h_mark(sam_u->fin_s.sac_p); all_u[0]->qua_u = 0; -// XX tinnus -// -// all_u[1] = c3_malloc(sizeof(**all_u)); -// all_u[1]->nam_c = strdup("lane cache"); -// all_u[1]->siz_w = 4 * u3h_mark(sam_u->lax_p); -// all_u[1]->qua_u = 0; -// -// all_u[2] = 0; -// -// *out_w = all_u[0]->siz_w + all_u[1]->siz_w; - all_u[1] = 0; *out_w = all_u[0]->siz_w; diff --git a/pkg/vere/io/mesa.c b/pkg/vere/io/mesa.c index d49d2b0a02..8e7d426e31 100644 --- a/pkg/vere/io/mesa.c +++ b/pkg/vere/io/mesa.c @@ -3011,6 +3011,9 @@ u3_ames_io_init(void* mes_u); void _ames_init_czars(u3_lamp_state* lam_u); +u3m_quac** +_ames_io_mark(u3_auto*, c3_w*); + /* _mesa_io_init(): initialize ames I/O. */ u3_auto* @@ -3060,6 +3063,7 @@ u3_mesa_io_init(u3_pier* pir_u) car_u->io.talk_f = _mesa_io_talk; car_u->io.info_f = _mesa_io_info; car_u->io.slog_f = _mesa_io_slog; + car_u->io.mark_f = _ames_io_mark; car_u->io.kick_f = _mesa_io_kick; car_u->io.exit_f = _mesa_io_exit; diff --git a/pkg/vere/mars.c b/pkg/vere/mars.c index 63b435b296..1f22362cee 100644 --- a/pkg/vere/mars.c +++ b/pkg/vere/mars.c @@ -1496,7 +1496,7 @@ u3_mars_work(u3_mars* mar_u) u3_noun wyn = u3_nul; u3_noun msg = u3nq(c3__ripe, u3nc(2, wyn), - u3nc(u3i_chubs(2, (c3_d*)&mar_u->met_u.who_u), + u3nc(u3_ship_to_noun(mar_u->met_u.who_u), mar_u->met_u.fak_o), u3nc(u3i_chub(mar_u->dun_d), mar_u->mug_l)); @@ -1721,8 +1721,7 @@ _mars_boot_make(u3_boot_opts* inp_u, return c3n; } - u3r_chubs(0, 2, (c3_d*)&met_u->who_u, who); - + met_u->who_u = u3_ship_of_noun(who); { u3_noun bot, mod, use, cax; diff --git a/pkg/vere/pier.c b/pkg/vere/pier.c index a7796c04f1..05512e4538 100644 --- a/pkg/vere/pier.c +++ b/pkg/vere/pier.c @@ -955,7 +955,7 @@ _pier_on_lord_live(void* ptr_v, u3_atom who, c3_o fak_o) // XX validate // - u3r_chubs(0, 2, (c3_d*)&pir_u->who_u, who); + pir_u->who_u = u3_ship_of_noun(who); pir_u->fak_o = fak_o; // early exit, preparing for upgrade From 31acef0358727a20f97b1e8d824515e2ef94387d Mon Sep 17 00:00:00 2001 From: Tinnus Napbus Date: Mon, 10 Nov 2025 21:47:24 +1300 Subject: [PATCH 34/34] gw: add --jael-sources --- pkg/vere/dawn.c | 13 ++++++++++--- pkg/vere/main.c | 11 ++++++++++- pkg/vere/vere.h | 1 + 3 files changed, 21 insertions(+), 4 deletions(-) diff --git a/pkg/vere/dawn.c b/pkg/vere/dawn.c index ae6ab1e0c2..e586daac01 100644 --- a/pkg/vere/dawn.c +++ b/pkg/vere/dawn.c @@ -269,7 +269,7 @@ _dawn_sponsor(u3_noun who, u3_noun rac, u3_noun pot) u3_noun u3_dawn_vent(u3_noun ship, u3_noun feed, u3_noun* rift) { - u3_noun fed, pos, pon, zar, tuf; + u3_noun fed, pos, pon, zar, tuf, src; u3_noun rank = u3do("clan:title", u3k(ship)); @@ -399,13 +399,20 @@ u3_dawn_vent(u3_noun ship, u3_noun feed, u3_noun* rift) u3z(son); } + + if ( 0 != u3_Host.ops_u.src_c ) { + src = u3v_wish(u3_Host.ops_u.src_c); + } + else { + src = u3_nul; + } - // [%dawn seed sponsors galaxies domains block eth-url snap] + // [%dawn seed sponsors galaxies domains block eth-url sources] // //NOTE blocknum of 0 is fine because jael ignores it. // should probably be removed from dawn event. u3_noun ven = u3nc(c3__dawn, - u3nq(u3k(u3t(fed)), pon, zar, u3nt(tuf, 0, u3_nul))); + u3nq(u3k(u3t(fed)), pon, zar, u3nq(tuf, 0, u3_nul, src))); u3z(fed); u3z(rank); u3z(pos); u3z(ship); u3z(feed); diff --git a/pkg/vere/main.c b/pkg/vere/main.c index f9a33792ba..65c94a6ceb 100644 --- a/pkg/vere/main.c +++ b/pkg/vere/main.c @@ -308,6 +308,7 @@ _main_getopt(c3_i argc, c3_c** argv) { "behn-allow-blocked", no_argument, NULL, 10 }, { "serf-bin", required_argument, NULL, 11 }, { "lmdb-map-size", required_argument, NULL, 12 }, + { "jael-sources", required_argument, NULL, 13 }, // { NULL, 0, NULL, 0 }, }; @@ -359,7 +360,10 @@ _main_getopt(c3_i argc, c3_c** argv) if ( 1 != sscanf(optarg, "%" SCNuMAX, &u3_Host.ops_u.siz_i) ) { return c3n; } - + break; + } + case 13: { // jael-sources + u3_Host.ops_u.src_c = strdup(optarg); break; } // special args @@ -651,6 +655,11 @@ _main_getopt(c3_i argc, c3_c** argv) fprintf(stderr, "-B and -u cannot be used together\n"); return c3n; } + + if (u3_Host.ops_u.nuu != c3y && u3_Host.ops_u.src_c != 0 ) { + fprintf(stderr, "--jael-sources only makes sense when creating a new ship\n"); + return c3n; + } else if ( u3_Host.ops_u.nuu == c3y && u3_Host.ops_u.url_c == 0 && u3_Host.ops_u.git == c3n ) { diff --git a/pkg/vere/vere.h b/pkg/vere/vere.h index bd59355b62..1a227e976a 100644 --- a/pkg/vere/vere.h +++ b/pkg/vere/vere.h @@ -309,6 +309,7 @@ c3_o eph; // --swap, use ephemeral file c3_o tos; // --toss, discard ephemeral u3_even* vex_u; // --prop-*, boot enhancements + c3_c* src_c; // --jael-sources c3_o beb; // --behn-allow-blocked c3_z siz_i; // --lmdb-map-size