Skip to content

Commit

Permalink
give correct self connection status when only connected to LAN nodes
Browse files Browse the repository at this point in the history
  • Loading branch information
zoff99 committed Jul 10, 2023
1 parent 12b7273 commit 6e1c0a2
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 8 deletions.
2 changes: 1 addition & 1 deletion toxcore/Messenger.c
Original file line number Diff line number Diff line change
Expand Up @@ -2806,7 +2806,7 @@ static void do_friends(Messenger *m, void *userdata)
non_null(1) nullable(2)
static void m_connection_status_callback(Messenger *m, void *userdata)
{
const Onion_Connection_Status conn_status = onion_connection_status(m->onion_c);
const Onion_Connection_Status conn_status = onion_connection_status(m->onion_c, true);

if (conn_status != m->last_connection_status) {
if (m->core_connection_change != nullptr) {
Expand Down
17 changes: 12 additions & 5 deletions toxcore/onion_client.c
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,7 @@ struct Onion_Client {

unsigned int onion_connected;
bool udp_connected;
bool udp_connected_lan_only_is_ok;

onion_group_announce_cb *group_announce_response;
void *group_announce_response_user_data;
Expand Down Expand Up @@ -2015,13 +2016,18 @@ static void reset_friend_run_counts(Onion_Client *onion_c)
#define ONION_CONNECTION_SECONDS 3
#define ONION_CONNECTED_TIMEOUT 10

Onion_Connection_Status onion_connection_status(const Onion_Client *onion_c)
Onion_Connection_Status onion_connection_status(const Onion_Client *onion_c, bool lan_only_is_online)
{
if (onion_c->onion_connected >= ONION_CONNECTION_SECONDS) {
if (onion_c->udp_connected) {
return ONION_CONNECTION_STATUS_UDP;
if (lan_only_is_online) {
if (onion_c->udp_connected_lan_only_is_ok) {
return ONION_CONNECTION_STATUS_UDP;
}
} else {
if (onion_c->udp_connected) {
return ONION_CONNECTION_STATUS_UDP;
}
}

return ONION_CONNECTION_STATUS_TCP;
}

Expand Down Expand Up @@ -2056,12 +2062,13 @@ void do_onion_client(Onion_Client *onion_c)
}

onion_c->udp_connected = dht_non_lan_connected(onion_c->dht);
onion_c->udp_connected_lan_only_is_ok = dht_isconnected(onion_c->dht);

if (mono_time_is_timeout(onion_c->mono_time, onion_c->first_run, ONION_CONNECTION_SECONDS * 2)) {
set_tcp_onion_status(nc_get_tcp_c(onion_c->c), !onion_c->udp_connected);
}

if (onion_connection_status(onion_c) != ONION_CONNECTION_STATUS_NONE) {
if (onion_connection_status(onion_c, false) != ONION_CONNECTION_STATUS_NONE) {
for (unsigned i = 0; i < onion_c->num_friends; ++i) {
do_friend(onion_c, i);
}
Expand Down
2 changes: 1 addition & 1 deletion toxcore/onion_client.h
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,7 @@ typedef enum Onion_Connection_Status {
} Onion_Connection_Status;

non_null()
Onion_Connection_Status onion_connection_status(const Onion_Client *onion_c);
Onion_Connection_Status onion_connection_status(const Onion_Client *onion_c, bool lan_only_is_online);

typedef struct Onion_Friend Onion_Friend;

Expand Down
2 changes: 1 addition & 1 deletion toxcore/tox.c
Original file line number Diff line number Diff line change
Expand Up @@ -1218,7 +1218,7 @@ Tox_Connection tox_self_get_connection_status(const Tox *tox)
{
assert(tox != nullptr);
tox_lock(tox);
const Onion_Connection_Status ret = onion_connection_status(tox->m->onion_c);
const Onion_Connection_Status ret = onion_connection_status(tox->m->onion_c, true);
tox_unlock(tox);

switch (ret) {
Expand Down

0 comments on commit 6e1c0a2

Please sign in to comment.