From aff98110e0ba3e68c657e6a39fd157d1c6192304 Mon Sep 17 00:00:00 2001 From: Cory McWilliams Date: Sat, 4 Jan 2025 21:37:39 -0500 Subject: [PATCH] ssb: Tidy up some of the more common reasons for disconnect. --- src/ssb.c | 27 +++++++++++++++++++++++---- src/ssb.js.c | 2 +- src/ssb.rpc.c | 22 +++++++++++++++++++++- 3 files changed, 45 insertions(+), 6 deletions(-) diff --git a/src/ssb.c b/src/ssb.c index 4c5bd740..3bd062a3 100644 --- a/src/ssb.c +++ b/src/ssb.c @@ -1573,7 +1573,7 @@ static bool _tf_ssb_connection_recv_pop(tf_ssb_connection_t* connection, uint8_t if (size >= sizeof(connection->recv_buffer)) { char message[256]; - snprintf(message, sizeof(message), "Trying to pop a message (%zd) larger than the connection's receive buffer (%zd).", size, sizeof(connection->recv_buffer)); + snprintf(message, sizeof(message), "Message (%zd) larger than the connection's receive buffer (%zd)", size, sizeof(connection->recv_buffer)); tf_ssb_connection_close(connection, message); } if (connection->recv_size < size) @@ -1944,7 +1944,7 @@ static void _tf_ssb_connection_destroy(tf_ssb_connection_t* connection, const ch if (it->tunnel_connection == connection) { it->tunnel_connection = NULL; - tf_ssb_connection_close(it, "tunnel closed"); + tf_ssb_connection_close(it, "Tunnel closed"); again = true; break; } @@ -2626,7 +2626,7 @@ void tf_ssb_destroy(tf_ssb_t* ssb) while (connection) { tf_ssb_connection_t* next = connection->next; - tf_ssb_connection_close(connection, "Shutting down."); + tf_ssb_connection_close(connection, "Shutting down"); connection = next; } uv_run(ssb->loop, UV_RUN_NOWAIT); @@ -2841,8 +2841,27 @@ static void _tf_ssb_connection_tunnel_callback( tf_ssb_connection_remove_request(connection, -request_number); tf_ssb_connection_rpc_send(connection, flags, -request_number, NULL, (const uint8_t*)"false", strlen("false"), NULL, NULL, NULL); + JSContext* context = tf_ssb_connection_get_context(connection); + JSValue message_val = JS_GetPropertyStr(context, args, "message"); + JSValue stack_val = JS_GetPropertyStr(context, args, "stack"); + char buffer[1024]; - snprintf(buffer, sizeof(buffer), "tunnel error: %.*s", (int)size, message); + if (!JS_IsUndefined(message_val)) + { + const char* message_string = JS_ToCString(context, message_val); + const char* stack_string = JS_ToCString(context, stack_val); + snprintf(buffer, sizeof(buffer), "Tunnel error: %s\n%s", message_string, stack_string); + JS_FreeCString(context, message_string); + JS_FreeCString(context, stack_string); + } + else + { + snprintf(buffer, sizeof(buffer), "Tunnel error: %.*s", (int)size, message); + } + + JS_FreeValue(context, stack_val); + JS_FreeValue(context, message_val); + tf_ssb_connection_close(tunnel, buffer); } else diff --git a/src/ssb.js.c b/src/ssb.js.c index ffc46ef7..6ad92e7b 100644 --- a/src/ssb.js.c +++ b/src/ssb.js.c @@ -1198,7 +1198,7 @@ static JSValue _tf_ssb_closeConnection(JSContext* context, JSValueConst this_val tf_ssb_connection_t* connection = tf_ssb_connection_get(ssb, id); if (connection) { - tf_ssb_connection_close(connection, "Close requested by user."); + tf_ssb_connection_close(connection, "Closed by user"); } JS_FreeCString(context, id); return connection ? JS_TRUE : JS_FALSE; diff --git a/src/ssb.rpc.c b/src/ssb.rpc.c index f450f26a..e30879d1 100644 --- a/src/ssb.rpc.c +++ b/src/ssb.rpc.c @@ -293,8 +293,28 @@ static void _tf_ssb_rpc_tunnel_callback(tf_ssb_connection_t* connection, uint8_t if (flags & k_ssb_rpc_flag_end_error) { tf_ssb_connection_remove_request(connection, request_number); + + JSContext* context = tf_ssb_connection_get_context(connection); + JSValue message_val = JS_GetPropertyStr(context, args, "message"); + JSValue stack_val = JS_GetPropertyStr(context, args, "stack"); + char buffer[1024]; - snprintf(buffer, sizeof(buffer), "error from tunnel: %.*s", (int)size, message); + if (!JS_IsUndefined(message_val)) + { + const char* message_string = JS_ToCString(context, message_val); + const char* stack_string = JS_ToCString(context, stack_val); + snprintf(buffer, sizeof(buffer), "Error from tunnel: %s\n%s", message_string, stack_string); + JS_FreeCString(context, message_string); + JS_FreeCString(context, stack_string); + } + else + { + snprintf(buffer, sizeof(buffer), "Error from tunnel: %.*s", (int)size, message); + } + + JS_FreeValue(context, stack_val); + JS_FreeValue(context, message_val); + tf_ssb_connection_close(tun->connection, buffer); } else