From cb94ed6a2a644c670741b552c7b498c21794e8a3 Mon Sep 17 00:00:00 2001 From: Cory McWilliams Date: Thu, 7 Mar 2024 21:03:14 -0500 Subject: [PATCH] Some plumbing to expose the actual bound SHS port so that I can make a dynamic room app. --- core/core.js | 1 + src/ssb.c | 16 +++++++++++----- src/ssb.h | 3 ++- src/task.c | 11 +++++++---- 4 files changed, 21 insertions(+), 10 deletions(-) diff --git a/core/core.js b/core/core.js index d827a842..c92d6bf8 100644 --- a/core/core.js +++ b/core/core.js @@ -578,6 +578,7 @@ async function getProcessBlob(blobId, key, options) { imports.ssb = Object.fromEntries( Object.keys(ssb).map((key) => [key, ssb[key].bind(ssb)]) ); + imports.ssb.port = tildefriends.ssb_port; imports.ssb.createIdentity = function () { if ( process.credentials && diff --git a/src/ssb.c b/src/ssb.c index 9a25885d..4fd2d044 100644 --- a/src/ssb.c +++ b/src/ssb.c @@ -2824,19 +2824,19 @@ static void _tf_ssb_broadcast_timer(uv_timer_t* timer) } } -void tf_ssb_server_open(tf_ssb_t* ssb, int port) +int tf_ssb_server_open(tf_ssb_t* ssb, int port) { if (ssb->server.data) { tf_printf("Already listening.\n"); - return; + return 0; } ssb->server.data = ssb; if (uv_tcp_init(ssb->loop, &ssb->server) != 0) { tf_printf("uv_tcp_init failed\n"); - return; + return 0; } struct sockaddr_in addr = { 0 }; @@ -2847,7 +2847,7 @@ void tf_ssb_server_open(tf_ssb_t* ssb, int port) if (status != 0) { tf_printf("%s:%d: uv_tcp_bind failed: %s\n", __FILE__, __LINE__, uv_strerror(status)); - return; + return 0; } status = uv_listen((uv_stream_t*)&ssb->server, SOMAXCONN, _tf_ssb_on_connection); @@ -2855,8 +2855,14 @@ void tf_ssb_server_open(tf_ssb_t* ssb, int port) { tf_printf("uv_listen failed: %s\n", uv_strerror(status)); /* TODO: cleanup */ - return; + return 0; } + + struct sockaddr_storage name = { 0 }; + int size = (int)sizeof(name); + status = uv_tcp_getsockname(&ssb->server, (struct sockaddr*)&name, &size); + int assigned_port = ntohs(((struct sockaddr_in*)&name)->sin_port); + return status == 0 ? assigned_port : 0; } void tf_ssb_server_close(tf_ssb_t* ssb) diff --git a/src/ssb.h b/src/ssb.h index 08470a7a..4ee6c6bf 100644 --- a/src/ssb.h +++ b/src/ssb.h @@ -316,8 +316,9 @@ void tf_ssb_connect_str(tf_ssb_t* ssb, const char* address); ** Begin listening for SHS connections on the given port. ** @param ssb The SSB instance. ** @param port The port number. +** @return The assigned port on success or 0 on failure. */ -void tf_ssb_server_open(tf_ssb_t* ssb, int port); +int tf_ssb_server_open(tf_ssb_t* ssb, int port); /** ** Stop listening for SHS connections. diff --git a/src/task.c b/src/task.c index 4eeaf37b..0e76a712 100644 --- a/src/task.c +++ b/src/task.c @@ -1694,9 +1694,6 @@ void tf_task_activate(tf_task_t* task) JS_FreeAtom(context, atom); JSValue tildefriends = JS_NewObject(context); - JS_SetPropertyStr(context, tildefriends, "ssb_port", JS_NewInt32(context, task->_ssb_port)); - JS_SetPropertyStr(context, tildefriends, "http_port", JS_NewInt32(context, task->_http_port)); - JS_SetPropertyStr(context, tildefriends, "https_port", JS_NewInt32(context, task->_https_port)); JSValue args = JS_NewObject(context); JS_SetPropertyStr(context, tildefriends, "args", args); if (task->_args) @@ -1749,13 +1746,19 @@ void tf_task_activate(tf_task_t* task) tf_ssb_register(context, task->_ssb); tf_ssb_set_hitch_callback(task->_ssb, _tf_task_record_hitch, task); + int actual_ssb_port = task->_ssb_port; + if (task->_ssb_port) { tf_ssb_broadcast_listener_start(task->_ssb, false); tf_ssb_broadcast_sender_start(task->_ssb); - tf_ssb_server_open(task->_ssb, task->_ssb_port); + actual_ssb_port = tf_ssb_server_open(task->_ssb, task->_ssb_port); } + JS_SetPropertyStr(context, tildefriends, "ssb_port", JS_NewInt32(context, actual_ssb_port)); + JS_SetPropertyStr(context, tildefriends, "http_port", JS_NewInt32(context, task->_http_port)); + JS_SetPropertyStr(context, tildefriends, "https_port", JS_NewInt32(context, task->_https_port)); + JS_SetPropertyStr(context, global, "getStats", JS_NewCFunction(context, _tf_task_getStats, "getStats", 0)); } else