diff --git a/src/ssb.rpc.c b/src/ssb.rpc.c index 2143086b..f7345870 100644 --- a/src/ssb.rpc.c +++ b/src/ssb.rpc.c @@ -1353,6 +1353,11 @@ static void _tf_ssb_get_peers_exhange_callback( { char fullid[k_id_base64_len] = { 0 }; tf_base64_encode(pub, sizeof(pub), fullid, sizeof(fullid)); + char* dot = strchr(fullid, '.'); + if (dot) + { + *dot = '\0'; + } char connection[1024] = { 0 }; snprintf(connection, sizeof(connection), "net:%s:%d~shs:%s", host, ntohs(addr->sin_port), fullid); @@ -1385,9 +1390,14 @@ static void _tf_ssb_rpc_peers_exchange_internal( { char fullid[k_id_base64_len] = { 0 }; tf_ssb_connection_get_id(connection, fullid, sizeof(fullid)); + char* dot = strchr(fullid, '.'); + if (dot) + { + *dot = '\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); + 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); tf_ssb_add_broadcast(ssb, connection_string, k_tf_ssb_broadcast_origin_peer_exchange, k_ssb_peer_exchange_expires_seconds); } @@ -1430,9 +1440,13 @@ static void _tf_ssb_rpc_send_peers_exchange(tf_ssb_connection_t* connection) int32_t request_number = tf_ssb_connection_next_request_number(connection); JSContext* context = tf_ssb_connection_get_context(connection); JSValue message = JS_NewObject(context); + JSValue name = JS_NewArray(context); + JS_SetPropertyUint32(context, name, 0, JS_NewString(context, "peers")); + 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, "peers", _tf_ssb_get_peers_exchange(ssb)); - tf_ssb_connection_rpc_send_json(connection, k_ssb_rpc_flag_new_request, request_number, NULL, 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); } diff --git a/src/ssb.tests.c b/src/ssb.tests.c index 2e7cf28b..67b13217 100644 --- a/src/ssb.tests.c +++ b/src/ssb.tests.c @@ -870,4 +870,52 @@ void tf_ssb_test_encrypt(const tf_test_options_t* options) printf("returned %d\n", WEXITSTATUS(result)); assert(WEXITSTATUS(result) == 0); } + +void tf_ssb_test_peer_exchange(const tf_test_options_t* options) +{ + uv_loop_t loop = { 0 }; + uv_loop_init(&loop); + + unlink("out/test_db0.sqlite"); + tf_ssb_t* ssb0 = tf_ssb_create(&loop, NULL, "file:out/test_db0.sqlite", NULL); + tf_ssb_set_is_room(ssb0, false); + tf_ssb_set_is_replicator(ssb0, false); + tf_ssb_set_is_peer_exchange(ssb0, true); + tf_ssb_register(tf_ssb_get_context(ssb0), ssb0); + tf_ssb_server_open(ssb0, 12347); + + unlink("out/test_db1.sqlite"); + tf_ssb_t* ssb1 = tf_ssb_create(&loop, NULL, "file:out/test_db1.sqlite", NULL); + tf_ssb_set_is_room(ssb1, false); + tf_ssb_set_is_replicator(ssb1, false); + tf_ssb_set_is_peer_exchange(ssb1, true); + tf_ssb_register(tf_ssb_get_context(ssb1), ssb1); + tf_ssb_server_open(ssb1, 12348); + + unlink("out/test_db2.sqlite"); + tf_ssb_t* ssb2 = tf_ssb_create(&loop, NULL, "file:out/test_db2.sqlite", NULL); + tf_ssb_set_is_room(ssb2, false); + tf_ssb_set_is_replicator(ssb2, false); + tf_ssb_set_is_peer_exchange(ssb2, true); + tf_ssb_register(tf_ssb_get_context(ssb2), ssb2); + tf_ssb_server_open(ssb2, 12349); + + char id0[k_id_base64_len] = { 0 }; + tf_ssb_whoami(ssb0, id0, sizeof(id0)); + uint8_t id0bin[k_id_bin_len]; + tf_ssb_id_str_to_bin(id0bin, id0); + tf_ssb_connect(ssb1, "127.0.0.1", 12347, id0bin); + tf_ssb_connect(ssb2, "127.0.0.1", 12347, id0bin); + + uv_run(&loop, UV_RUN_DEFAULT); + + tf_ssb_destroy(ssb0); + tf_ssb_destroy(ssb1); + tf_ssb_destroy(ssb2); + + uv_run(&loop, UV_RUN_DEFAULT); + + uv_loop_close(&loop); +} + #endif diff --git a/src/ssb.tests.h b/src/ssb.tests.h index 06dc4665..192101b6 100644 --- a/src/ssb.tests.h +++ b/src/ssb.tests.h @@ -53,4 +53,10 @@ void tf_ssb_test_go_ssb_room(const tf_test_options_t* options); */ void tf_ssb_test_encrypt(const tf_test_options_t* options); +/** +** Test peer exchange. +** @param options The test options. +*/ +void tf_ssb_test_peer_exchange(const tf_test_options_t* options); + /** @} */ diff --git a/src/tests.c b/src/tests.c index c2629896..e34ac4b3 100644 --- a/src/tests.c +++ b/src/tests.c @@ -915,6 +915,7 @@ void tf_tests(const tf_test_options_t* options) _tf_test_run(options, "auto", _test_auto, false); _tf_test_run(options, "go-ssb-room", tf_ssb_test_go_ssb_room, true); _tf_test_run(options, "encrypt", tf_ssb_test_encrypt, false); + _tf_test_run(options, "peer_exchange", tf_ssb_test_peer_exchange, false); tf_printf("Tests completed.\n"); #endif }