Skip to content

Commit

Permalink
Merge branch 'develop' into jb/ames-dequeue
Browse files Browse the repository at this point in the history
  • Loading branch information
joemfb committed Jun 26, 2024
2 parents 81d3836 + 81b31ca commit fc2c3ab
Show file tree
Hide file tree
Showing 9 changed files with 343 additions and 53 deletions.
9 changes: 9 additions & 0 deletions WORKSPACE.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,15 @@ versioned_http_file(
version = "721fa05",
)

versioned_http_archive(
name = "natpmp",
build_file = "//bazel/third_party/natpmp:natpmp.BUILD",
sha256 = "0684ed2c8406437e7519a1bd20ea83780db871b3a3a5d752311ba3e889dbfc70",
strip_prefix = "libnatpmp-{version}",
url = "http://miniupnp.free.fr/files/libnatpmp-{version}.tar.gz",
version = "20230423",
)

versioned_http_file(
name = "solid_pill",
sha256 = "8b658fcee6978e2b19004a54233cab953e77ea0bb6c3a04d1bfda4ddc6be63c5",
Expand Down
Empty file.
8 changes: 8 additions & 0 deletions bazel/third_party/natpmp/natpmp.BUILD
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
cc_library(
name = "natpmp",
srcs = ["natpmp.c", "getgateway.c"],
hdrs = ["natpmp.h", "getgateway.h", "natpmp_declspec.h"],
copts = ["-O3"],
linkstatic = True,
visibility = ["//visibility:public"],
)
2 changes: 1 addition & 1 deletion pkg/noun/nock.c
Original file line number Diff line number Diff line change
Expand Up @@ -1400,7 +1400,7 @@ _n_comp(u3_noun* ops, u3_noun fol, c3_o los_o, c3_o tel_o)

case 9:
u3x_cell(arg, &hed, &tel);
if ( (1 == hed) || (3 == u3qc_cap(hed)) ) {
if ( (1 == hed) || (3 == u3qc_cap(u3x_atom(hed))) ) {
u3_noun mac = u3nq(7, u3k(tel), 2, u3nt(u3nc(0, 1), 0, u3k(hed)));
tot_w += _n_comp(ops, mac, los_o, tel_o);
u3z(mac);
Expand Down
1 change: 1 addition & 0 deletions pkg/vere/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,7 @@ vere_library(
"@lmdb",
"@openssl",
"@uv",
"@natpmp",
] + select({
"@platforms//os:macos": [],
"@platforms//os:linux": [
Expand Down
25 changes: 9 additions & 16 deletions pkg/vere/disk.c
Original file line number Diff line number Diff line change
Expand Up @@ -31,15 +31,6 @@ struct _u3_disk_walk {
c3_o liv_o;
};

// for u3_lmdb_init() calls
static const size_t siz_i =
#if (defined(U3_CPU_aarch64) && defined(U3_OS_linux))
// 500 GiB is as large as musl on aarch64 wants to allow
0x7d00000000;
#else
0x10000000000;
#endif

#undef VERBOSE_DISK
#undef DISK_TRACE_JAM
#undef DISK_TRACE_CUE
Expand Down Expand Up @@ -680,7 +671,7 @@ u3_disk_save_meta_meta(c3_c* log_c,
{
MDB_env* dbm_u;

if ( 0 == (dbm_u = u3_lmdb_init(log_c, siz_i)) ) {
if ( 0 == (dbm_u = u3_lmdb_init(log_c, u3_Host.ops_u.siz_i)) ) {
fprintf(stderr, "disk: failed to initialize meta-lmdb\r\n");
return c3n;
}
Expand Down Expand Up @@ -1240,7 +1231,7 @@ _disk_epoc_roll(u3_disk* log_u, c3_d epo_d)
log_u->mdb_u = 0;

// initialize db of new epoch
if ( 0 == (log_u->mdb_u = u3_lmdb_init(epo_c, siz_i)) ) {
if ( 0 == (log_u->mdb_u = u3_lmdb_init(epo_c, u3_Host.ops_u.siz_i)) ) {
fprintf(stderr, "disk: failed to initialize database\r\n");
c3_free(log_u);
goto fail3;
Expand Down Expand Up @@ -1499,7 +1490,7 @@ _disk_migrate(u3_disk* log_u, c3_d eve_d)
return c3n;
}

if ( 0 == (log_u->mdb_u = u3_lmdb_init(tmp_c, siz_i)) ) {
if ( 0 == (log_u->mdb_u = u3_lmdb_init(tmp_c, u3_Host.ops_u.siz_i)) ) {
fprintf(stderr, "disk: failed to initialize database at %s\r\n",
tmp_c);
return c3n;
Expand Down Expand Up @@ -1533,7 +1524,7 @@ _disk_migrate(u3_disk* log_u, c3_d eve_d)
strerror(errno));
}

if ( 0 == (log_u->mdb_u = u3_lmdb_init(epo_c, siz_i)) ) {
if ( 0 == (log_u->mdb_u = u3_lmdb_init(epo_c, u3_Host.ops_u.siz_i)) ) {
fprintf(stderr, "disk: failed to initialize database at %s\r\n",
epo_c);
return c3n;
Expand Down Expand Up @@ -1632,7 +1623,9 @@ u3_disk_chop(u3_disk* log_u, c3_d eve_d)
u3_disk_epoc_list(log_u, sot_d);

if ( len_z <= 2 ) {
fprintf(stderr, "chop: nothing to do, have a great day\r\n");
fprintf(stderr, "chop: nothing to do, try running roll first\r\n"
"chop: for more info see "
"https://docs.urbit.org/manual/running/vere#chop\r\n");
exit(0); // enjoy
}

Expand Down Expand Up @@ -1734,7 +1727,7 @@ _disk_epoc_load(u3_disk* log_u, c3_d lat_d)
snprintf(epo_c, 8192, "%s/0i%" PRIc3_d, log_u->com_u->pax_c, lat_d);

// initialize latest epoch's db
if ( 0 == (log_u->mdb_u = u3_lmdb_init(epo_c, siz_i)) ) {
if ( 0 == (log_u->mdb_u = u3_lmdb_init(epo_c, u3_Host.ops_u.siz_i)) ) {
fprintf(stderr, "disk: failed to initialize database at %s\r\n",
epo_c);
return _epoc_fail;
Expand Down Expand Up @@ -1878,7 +1871,7 @@ u3_disk_init(c3_c* pax_c, u3_disk_cb cb_u)

if ( c3y == exs_o ) {
// load the old data.mdb file
if ( 0 == (log_u->mdb_u = u3_lmdb_init(log_c, siz_i)) ) {
if ( 0 == (log_u->mdb_u = u3_lmdb_init(log_c, u3_Host.ops_u.siz_i)) ) {
fprintf(stderr, "disk: failed to initialize lmdb\r\n");
c3_free(log_u);
return 0;
Expand Down
84 changes: 84 additions & 0 deletions pkg/vere/io/ames.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include "ur.h"

#include "zlib.h"
#include "natpmp.h"

#include <ent.h>

Expand Down Expand Up @@ -74,6 +75,11 @@ typedef enum u3_stun_state {
u3_lane sef_u; // our lane, if we know it
c3_o wok_o; // STUN worked, set on first success
} sun_u; //
struct {
natpmp_t req_u; // libnatpmp struct for mapping request
uv_poll_t pol_u; // handle waits on libnatpmp socket
uv_timer_t tim_u; // every two hours if mapping succeeds
} nat_u; // libnatpmp stuff for port forwarding
c3_o nal_o; // lane cache backcompat flag
struct { // config:
c3_o net_o; // can send
Expand Down Expand Up @@ -2473,6 +2479,69 @@ _ames_recv_cb(uv_udp_t* wax_u,
}
}

static void natpmp_init(uv_timer_t* handle);

static void
natpmp_cb(uv_poll_t* handle,
c3_i status,
c3_i events)
{

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)
{
Expand Down Expand Up @@ -2593,6 +2662,11 @@ _ames_io_start(u3_ames* sam_u)
u3z(our);

mdns_init(por_s, !sam_u->pir_u->fak_o, our_s, _ames_put_dear, (void *)sam_u);

if ( c3n == sam_u->pir_u->fak_o ) {
uv_timer_start(&sam_u->nat_u.tim_u, natpmp_init, 0, 0);
}

c3_free(our_s);
}

Expand Down Expand Up @@ -2977,6 +3051,12 @@ _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);
if ( UV_UNKNOWN_HANDLE != handle) {
uv_close((uv_handle_t*)&sam_u->nat_u.pol_u, 0);
}
}

/* _ames_io_info(): produce status info.
Expand Down Expand Up @@ -3084,6 +3164,10 @@ u3_ames_io_init(u3_pier* pir_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);

// enable forwarding on galaxies only
u3_noun who = u3i_chubs(2, sam_u->pir_u->who_d);
u3_noun rac = u3do("clan:title", who);
Expand Down
Loading

0 comments on commit fc2c3ab

Please sign in to comment.