From e92a67ed5f735df2ec4e627817b3eeb6da8861a5 Mon Sep 17 00:00:00 2001 From: jfreegman Date: Tue, 21 Dec 2021 10:51:17 -0500 Subject: [PATCH] Merge friend_connections from NGC fork --- toxcore/BUILD.bazel | 10 ++++++++++ toxcore/friend_connection.c | 18 ++++++++++++++---- toxcore/friend_connection.cc | 13 +++++++++++++ toxcore/friend_connection.h | 6 ++++++ 4 files changed, 43 insertions(+), 4 deletions(-) create mode 100644 toxcore/friend_connection.cc diff --git a/toxcore/BUILD.bazel b/toxcore/BUILD.bazel index 1073dece8..c0fc825ab 100644 --- a/toxcore/BUILD.bazel +++ b/toxcore/BUILD.bazel @@ -282,6 +282,16 @@ cc_library( ], ) +cc_test( + name = "friend_connection_test", + size = "small", + srcs = ["friend_connection.cc"], + deps = [ + ":friend_connection", + "@com_google_googletest//:gtest_main", + ], +) + cc_library( name = "friend_requests", srcs = ["friend_requests.c"], diff --git a/toxcore/friend_connection.c b/toxcore/friend_connection.c index 2eb1a01d0..e24959a16 100644 --- a/toxcore/friend_connection.c +++ b/toxcore/friend_connection.c @@ -25,7 +25,7 @@ typedef struct Friend_Conn_Callbacks { int callback_id; } Friend_Conn_Callbacks; -typedef struct Friend_Conn { +struct Friend_Conn { uint8_t status; uint8_t real_public_key[CRYPTO_PUBLIC_KEY_SIZE]; @@ -50,7 +50,7 @@ typedef struct Friend_Conn { uint16_t tcp_relay_counter; bool hosting_tcp_relay; -} Friend_Conn; +}; struct Friend_Connections { @@ -75,11 +75,21 @@ struct Friend_Connections { bool local_discovery_enabled; }; +int friend_conn_get_onion_friendnum(const Friend_Conn *fc) +{ + return fc->onion_friendnum; +} + Net_Crypto *friendconn_net_crypto(const Friend_Connections *fr_c) { return fr_c->net_crypto; } +const IP_Port *friend_conn_get_dht_ip_port(const Friend_Conn *fc) +{ + return &fc->dht_ip_port; +} + /* return true if the friendcon_id is valid. * return false if the friendcon_id is not valid. @@ -168,7 +178,7 @@ static int wipe_friend_conn(Friend_Connections *fr_c, int friendcon_id) return 0; } -static Friend_Conn *get_conn(const Friend_Connections *fr_c, int friendcon_id) +Friend_Conn *get_conn(const Friend_Connections *fr_c, int friendcon_id) { if (!friendconn_id_valid(fr_c, friendcon_id)) { return nullptr; @@ -262,7 +272,7 @@ static unsigned int send_relays(Friend_Connections *fr_c, int friendcon_id) return 0; } - Node_format nodes[MAX_SHARED_RELAYS]; + Node_format nodes[MAX_SHARED_RELAYS] = {{{0}}}; uint8_t data[1024]; const int n = copy_connected_tcp_relays(fr_c->net_crypto, nodes, MAX_SHARED_RELAYS); diff --git a/toxcore/friend_connection.cc b/toxcore/friend_connection.cc new file mode 100644 index 000000000..dd83f82f4 --- /dev/null +++ b/toxcore/friend_connection.cc @@ -0,0 +1,13 @@ +#include "friend_connection.h" + +#include + +namespace { + +// TODO(Jfreegman) make this useful or remove it after NGC is merged +TEST(friend_connection, NullTest) { + (void)friend_conn_get_onion_friendnum; + (void)friend_conn_get_dht_ip_port; +} + +} // namespace diff --git a/toxcore/friend_connection.h b/toxcore/friend_connection.h index fecd74556..c0217a821 100644 --- a/toxcore/friend_connection.h +++ b/toxcore/friend_connection.h @@ -157,4 +157,10 @@ void do_friend_connections(Friend_Connections *fr_c, void *userdata); /* Free everything related with friend_connections. */ void kill_friend_connections(Friend_Connections *fr_c); +typedef struct Friend_Conn Friend_Conn; + +Friend_Conn *get_conn(const Friend_Connections *fr_c, int friendcon_id); +int friend_conn_get_onion_friendnum(const Friend_Conn *fc); +const IP_Port *friend_conn_get_dht_ip_port(const Friend_Conn *fc); + #endif