Pass along and use the actual port we're listening on for peers.exchange.

This commit is contained in:
Cory McWilliams 2024-08-25 09:50:28 -04:00
parent 6cebd6c769
commit 011670c70b
3 changed files with 29 additions and 1 deletions

View File

@ -3031,6 +3031,21 @@ int tf_ssb_server_open(tf_ssb_t* ssb, int port)
return status == 0 ? assigned_port : 0; 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) void tf_ssb_server_close(tf_ssb_t* ssb)
{ {
if (ssb->server.data && !uv_is_closing((uv_handle_t*)&ssb->server)) if (ssb->server.data && !uv_is_closing((uv_handle_t*)&ssb->server))

View File

@ -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); 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. ** Stop listening for SHS connections.
** @param ssb The SSB instance. ** @param ssb The SSB instance.

View File

@ -1429,8 +1429,13 @@ static void _tf_ssb_rpc_peers_exchange_internal(
*dot = '\0'; *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 }; 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); 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_SetPropertyUint32(context, name, 1, JS_NewString(context, "exchange"));
JS_SetPropertyStr(context, message, "name", name); JS_SetPropertyStr(context, message, "name", name);
tf_ssb_t* ssb = tf_ssb_connection_get_ssb(connection); 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)); 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); 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); JS_FreeValue(context, message);