From 011670c70baf83bce22d3b633f776c131432f278 Mon Sep 17 00:00:00 2001 From: Cory McWilliams Date: Sun, 25 Aug 2024 09:50:28 -0400 Subject: [PATCH] Pass along and use the actual port we're listening on for peers.exchange. --- src/ssb.c | 15 +++++++++++++++ src/ssb.h | 7 +++++++ src/ssb.rpc.c | 8 +++++++- 3 files changed, 29 insertions(+), 1 deletion(-) diff --git a/src/ssb.c b/src/ssb.c index 1fc416b7..51c6e89b 100644 --- a/src/ssb.c +++ b/src/ssb.c @@ -3031,6 +3031,21 @@ int tf_ssb_server_open(tf_ssb_t* ssb, int port) return status == 0 ? assigned_port : 0; } +int tf_ssb_server_get_port(tf_ssb_t* ssb) +{ + int port = 0; + if (ssb && ssb->server.data) + { + struct sockaddr_storage name = { 0 }; + int size = (int)sizeof(name); + if (uv_tcp_getsockname(&ssb->server, (struct sockaddr*)&name, &size) == 0) + { + port = ntohs(((struct sockaddr_in*)&name)->sin_port); + } + } + return port; +} + void tf_ssb_server_close(tf_ssb_t* ssb) { if (ssb->server.data && !uv_is_closing((uv_handle_t*)&ssb->server)) diff --git a/src/ssb.h b/src/ssb.h index 883ac616..da4621e7 100644 --- a/src/ssb.h +++ b/src/ssb.h @@ -364,6 +364,13 @@ void tf_ssb_connect_str(tf_ssb_t* ssb, const char* address); */ int tf_ssb_server_open(tf_ssb_t* ssb, int port); +/** +** Determine the port that a server is listening on. +** @param ssb The SSB instance. +** @return The port number, or 0 if not bound. +*/ +int tf_ssb_server_get_port(tf_ssb_t* ssb); + /** ** Stop listening for SHS connections. ** @param ssb The SSB instance. diff --git a/src/ssb.rpc.c b/src/ssb.rpc.c index dcd0cdf4..99b56822 100644 --- a/src/ssb.rpc.c +++ b/src/ssb.rpc.c @@ -1429,8 +1429,13 @@ static void _tf_ssb_rpc_peers_exchange_internal( *dot = '\0'; } + int port = tf_ssb_connection_get_port(connection); + JSValue port_value = JS_GetPropertyStr(context, args, "port"); + JS_ToInt32(context, &port, port_value); + JS_FreeValue(context, port_value); + char connection_string[1024] = { 0 }; - snprintf(connection_string, sizeof(connection_string), "net:%s:%d~shs:%s", tf_ssb_connection_get_host(connection), tf_ssb_connection_get_port(connection), fullid + 1); + snprintf(connection_string, sizeof(connection_string), "net:%s:%d~shs:%s", tf_ssb_connection_get_host(connection), port, fullid + 1); tf_ssb_add_broadcast(ssb, connection_string, k_tf_ssb_broadcast_origin_peer_exchange, k_ssb_peer_exchange_expires_seconds); } @@ -1478,6 +1483,7 @@ static void _tf_ssb_rpc_send_peers_exchange(tf_ssb_connection_t* connection) JS_SetPropertyUint32(context, name, 1, JS_NewString(context, "exchange")); JS_SetPropertyStr(context, message, "name", name); tf_ssb_t* ssb = tf_ssb_connection_get_ssb(connection); + JS_SetPropertyStr(context, message, "port", JS_NewInt32(context, tf_ssb_server_get_port(ssb))); JS_SetPropertyStr(context, message, "peers", _tf_ssb_get_peers_exchange(ssb)); tf_ssb_connection_rpc_send_json(connection, k_ssb_rpc_flag_new_request, request_number, "peers.exchange", message, _tf_ssb_rpc_peers_exchange_internal, NULL, NULL); JS_FreeValue(context, message);