Skip to content

Commit

Permalink
catch some null pointers
Browse files Browse the repository at this point in the history
  • Loading branch information
zoff99 committed Mar 21, 2024
1 parent b4110d3 commit 2dfbff0
Show file tree
Hide file tree
Showing 4 changed files with 270 additions and 3 deletions.
127 changes: 126 additions & 1 deletion amalgamation/toxcore_amalgamation.c
Original file line number Diff line number Diff line change
Expand Up @@ -9582,6 +9582,8 @@ typedef enum Tox_Group_Role {
******************************************************************************/


void tox_group_get_peer_connection_ip(const Tox *tox, uint32_t group_number, uint32_t peer_id, uint8_t *ip_str);


typedef enum Tox_Err_Group_New {

Expand Down Expand Up @@ -12869,6 +12871,8 @@ Group_Voice_State gc_get_voice_state(const GC_Chat *chat);
non_null()
uint16_t gc_get_max_peers(const GC_Chat *chat);

void gc_get_group_peer_connection_ip(const Messenger *m, int group_number, uint32_t peer_id, uint8_t *ip_str);

/** @brief Sets your own nick to `nick`.
*
* `length` cannot exceed MAX_GC_NICK_SIZE. if `length` is zero or `name` is a
Expand Down Expand Up @@ -32212,6 +32216,106 @@ static void handle_gc_peer_exit(const GC_Chat *chat, GC_Connection *gconn, const
gcc_mark_for_deletion(gconn, chat->tcp_conn, GC_EXIT_TYPE_QUIT, data, length);
}

void gc_get_group_peer_connection_ip(const Messenger *m, int group_number, uint32_t peer_id, uint8_t *ip_str)
{
if (ip_str == nullptr) {
return;
}
char *p = (char *)ip_str;

const GC_Session *c = m->group_handler;
const GC_Chat *chat = gc_get_group(c, group_number);
if (chat == nullptr) {
return;
}
if (chat->connection_state != CS_CONNECTED) {
return;
}

const int peer_number = get_peer_number_of_peer_id(chat, peer_id);
const GC_Connection *gconn = get_gc_connection(chat, peer_number);
if (gconn == nullptr) {
return;
}

if (gcc_direct_conn_is_possible(chat, gconn)) {
if (gcc_conn_is_direct(chat->mono_time, gconn)) {
if (!net_family_is_unspec(gconn->addr.ip_port.ip.family)) {
if (net_family_is_ipv4(gconn->addr.ip_port.ip.family)) {
char ipv4[20];
memset(ipv4, 0, 20);
snprintf(ipv4, 16, "%d.%d.%d.%d",
gconn->addr.ip_port.ip.ip.v4.uint8[0],
gconn->addr.ip_port.ip.ip.v4.uint8[1],
gconn->addr.ip_port.ip.ip.v4.uint8[2],
gconn->addr.ip_port.ip.ip.v4.uint8[3]
);
p += snprintf(p, 60, "%s %5d\n", ipv4, net_ntohs(gconn->addr.ip_port.port));
} else if (net_family_is_ipv6(gconn->addr.ip_port.ip.family)) {
char ipv6[401];
memset(ipv6, 0, 401);
bool res = ip_parse_addr(&gconn->addr.ip_port.ip, ipv6, 400);
if (!res) {
snprintf(ipv6, 16, "<error in ipv6>");
}
p += snprintf(p, 60, "%s %5d\n", ipv6, net_ntohs(gconn->addr.ip_port.port));
}
}
return;
}
}

if (gconn->tcp_relays_count > 0) {
// get tcp connections
const TCP_Connection_to *con_to = get_connection(chat->tcp_conn, gconn->tcp_connection_num);

if (con_to == nullptr) {
return;
}
for (uint32_t i = 0; i < MAX_FRIEND_TCP_CONNECTIONS; ++i) {
uint32_t tcp_con_num = con_to->connections[i].tcp_connection;
const uint8_t status = con_to->connections[i].status;

if (tcp_con_num > 0 && status == TCP_CONNECTIONS_STATUS_ONLINE) {
tcp_con_num -= 1;
TCP_con *tcp_con = get_tcp_connection(chat->tcp_conn, tcp_con_num);

if (tcp_con == nullptr) {
continue;
}

if (tcp_con->connection == nullptr) {
continue;
}

const IP_Port conn_ip_port = tcp_con_ip_port(tcp_con->connection);

if (!net_family_is_unspec(conn_ip_port.ip.family)) {
if (net_family_is_ipv4(conn_ip_port.ip.family)) {
char ipv4[20];
memset(ipv4, 0, 20);
snprintf(ipv4, 16, "%d.%d.%d.%d",
conn_ip_port.ip.ip.v4.uint8[0],
conn_ip_port.ip.ip.v4.uint8[1],
conn_ip_port.ip.ip.v4.uint8[2],
conn_ip_port.ip.ip.v4.uint8[3]
);
p += snprintf(p, 60, "%s %5d\n", ipv4, net_ntohs(conn_ip_port.port));
} else if (net_family_is_ipv6(conn_ip_port.ip.family)) {
char ipv6[401];
memset(ipv6, 0, 401);
bool res = ip_parse_addr(&conn_ip_port.ip, ipv6, 400);
if (!res) {
snprintf(ipv6, 16, "<error in ipv6>");
}
p += snprintf(p, 60, "%s %5d\n", ipv6, net_ntohs(conn_ip_port.port));
}
}
}
}
}
}

int gc_set_self_nick(const Messenger *m, int group_number, const uint8_t *nick, uint16_t length)
{
const GC_Session *c = m->group_handler;
Expand Down Expand Up @@ -47243,6 +47347,11 @@ void copy_friend_ip_port(Net_Crypto *c, const int crypt_conn_id, char *report_st
if (report_string == nullptr) {
return;
}

if (crypt_conn_id == nullptr) {
return;
}

char *p = report_string;

if (direct_connected) {
Expand Down Expand Up @@ -47273,8 +47382,14 @@ void copy_friend_ip_port(Net_Crypto *c, const int crypt_conn_id, char *report_st
} else {
// get tcp connections
Crypto_Connection *conn = get_crypto_connection(c, crypt_conn_id);
if (conn == nullptr) {
return;
}
unsigned int conn_num_tcp = conn->connection_number_tcp;
const TCP_Connection_to *con_to = get_connection(c->tcp_c, conn_num_tcp);
if (con_to == nullptr) {
return;
}

for (uint32_t i = 0; i < MAX_FRIEND_TCP_CONNECTIONS; ++i) {
uint32_t tcp_con_num = con_to->connections[i].tcp_connection;
Expand All @@ -47284,7 +47399,6 @@ void copy_friend_ip_port(Net_Crypto *c, const int crypt_conn_id, char *report_st
if (tcp_con_num > 0 && status == TCP_CONNECTIONS_STATUS_ONLINE) {
tcp_con_num -= 1;
TCP_con *tcp_con = get_tcp_connection(c->tcp_c, tcp_con_num);

if (tcp_con == nullptr) {
continue;
}
Expand Down Expand Up @@ -62416,6 +62530,17 @@ void tox_callback_group_join_fail(Tox *tox, tox_group_join_fail_cb *callback)
tox->group_join_fail_callback = callback;
}

void tox_group_get_peer_connection_ip(const Tox *tox, uint32_t group_number, uint32_t peer_id, uint8_t *ip_str)
{
if (ip_str == nullptr) {
return;
}
assert(tox != nullptr);
tox_lock(tox);
gc_get_group_peer_connection_ip(tox->m, group_number, peer_id, ip_str);
tox_unlock(tox);
}

uint32_t tox_group_new(Tox *tox, Tox_Group_Privacy_State privacy_state, const uint8_t *group_name,
size_t group_name_length, const uint8_t *name, size_t name_length, Tox_Err_Group_New *error)
{
Expand Down
127 changes: 126 additions & 1 deletion amalgamation/toxcore_amalgamation_no_toxav.c
Original file line number Diff line number Diff line change
Expand Up @@ -9582,6 +9582,8 @@ typedef enum Tox_Group_Role {
******************************************************************************/


void tox_group_get_peer_connection_ip(const Tox *tox, uint32_t group_number, uint32_t peer_id, uint8_t *ip_str);


typedef enum Tox_Err_Group_New {

Expand Down Expand Up @@ -12869,6 +12871,8 @@ Group_Voice_State gc_get_voice_state(const GC_Chat *chat);
non_null()
uint16_t gc_get_max_peers(const GC_Chat *chat);

void gc_get_group_peer_connection_ip(const Messenger *m, int group_number, uint32_t peer_id, uint8_t *ip_str);

/** @brief Sets your own nick to `nick`.
*
* `length` cannot exceed MAX_GC_NICK_SIZE. if `length` is zero or `name` is a
Expand Down Expand Up @@ -29778,6 +29782,106 @@ static void handle_gc_peer_exit(const GC_Chat *chat, GC_Connection *gconn, const
gcc_mark_for_deletion(gconn, chat->tcp_conn, GC_EXIT_TYPE_QUIT, data, length);
}

void gc_get_group_peer_connection_ip(const Messenger *m, int group_number, uint32_t peer_id, uint8_t *ip_str)
{
if (ip_str == nullptr) {
return;
}
char *p = (char *)ip_str;

const GC_Session *c = m->group_handler;
const GC_Chat *chat = gc_get_group(c, group_number);
if (chat == nullptr) {
return;
}
if (chat->connection_state != CS_CONNECTED) {
return;
}

const int peer_number = get_peer_number_of_peer_id(chat, peer_id);
const GC_Connection *gconn = get_gc_connection(chat, peer_number);
if (gconn == nullptr) {
return;
}

if (gcc_direct_conn_is_possible(chat, gconn)) {
if (gcc_conn_is_direct(chat->mono_time, gconn)) {
if (!net_family_is_unspec(gconn->addr.ip_port.ip.family)) {
if (net_family_is_ipv4(gconn->addr.ip_port.ip.family)) {
char ipv4[20];
memset(ipv4, 0, 20);
snprintf(ipv4, 16, "%d.%d.%d.%d",
gconn->addr.ip_port.ip.ip.v4.uint8[0],
gconn->addr.ip_port.ip.ip.v4.uint8[1],
gconn->addr.ip_port.ip.ip.v4.uint8[2],
gconn->addr.ip_port.ip.ip.v4.uint8[3]
);
p += snprintf(p, 60, "%s %5d\n", ipv4, net_ntohs(gconn->addr.ip_port.port));
} else if (net_family_is_ipv6(gconn->addr.ip_port.ip.family)) {
char ipv6[401];
memset(ipv6, 0, 401);
bool res = ip_parse_addr(&gconn->addr.ip_port.ip, ipv6, 400);
if (!res) {
snprintf(ipv6, 16, "<error in ipv6>");
}
p += snprintf(p, 60, "%s %5d\n", ipv6, net_ntohs(gconn->addr.ip_port.port));
}
}
return;
}
}

if (gconn->tcp_relays_count > 0) {
// get tcp connections
const TCP_Connection_to *con_to = get_connection(chat->tcp_conn, gconn->tcp_connection_num);

if (con_to == nullptr) {
return;
}
for (uint32_t i = 0; i < MAX_FRIEND_TCP_CONNECTIONS; ++i) {
uint32_t tcp_con_num = con_to->connections[i].tcp_connection;
const uint8_t status = con_to->connections[i].status;

if (tcp_con_num > 0 && status == TCP_CONNECTIONS_STATUS_ONLINE) {
tcp_con_num -= 1;
TCP_con *tcp_con = get_tcp_connection(chat->tcp_conn, tcp_con_num);

if (tcp_con == nullptr) {
continue;
}

if (tcp_con->connection == nullptr) {
continue;
}

const IP_Port conn_ip_port = tcp_con_ip_port(tcp_con->connection);

if (!net_family_is_unspec(conn_ip_port.ip.family)) {
if (net_family_is_ipv4(conn_ip_port.ip.family)) {
char ipv4[20];
memset(ipv4, 0, 20);
snprintf(ipv4, 16, "%d.%d.%d.%d",
conn_ip_port.ip.ip.v4.uint8[0],
conn_ip_port.ip.ip.v4.uint8[1],
conn_ip_port.ip.ip.v4.uint8[2],
conn_ip_port.ip.ip.v4.uint8[3]
);
p += snprintf(p, 60, "%s %5d\n", ipv4, net_ntohs(conn_ip_port.port));
} else if (net_family_is_ipv6(conn_ip_port.ip.family)) {
char ipv6[401];
memset(ipv6, 0, 401);
bool res = ip_parse_addr(&conn_ip_port.ip, ipv6, 400);
if (!res) {
snprintf(ipv6, 16, "<error in ipv6>");
}
p += snprintf(p, 60, "%s %5d\n", ipv6, net_ntohs(conn_ip_port.port));
}
}
}
}
}
}

int gc_set_self_nick(const Messenger *m, int group_number, const uint8_t *nick, uint16_t length)
{
const GC_Session *c = m->group_handler;
Expand Down Expand Up @@ -44809,6 +44913,11 @@ void copy_friend_ip_port(Net_Crypto *c, const int crypt_conn_id, char *report_st
if (report_string == nullptr) {
return;
}

if (crypt_conn_id == nullptr) {
return;
}

char *p = report_string;

if (direct_connected) {
Expand Down Expand Up @@ -44839,8 +44948,14 @@ void copy_friend_ip_port(Net_Crypto *c, const int crypt_conn_id, char *report_st
} else {
// get tcp connections
Crypto_Connection *conn = get_crypto_connection(c, crypt_conn_id);
if (conn == nullptr) {
return;
}
unsigned int conn_num_tcp = conn->connection_number_tcp;
const TCP_Connection_to *con_to = get_connection(c->tcp_c, conn_num_tcp);
if (con_to == nullptr) {
return;
}

for (uint32_t i = 0; i < MAX_FRIEND_TCP_CONNECTIONS; ++i) {
uint32_t tcp_con_num = con_to->connections[i].tcp_connection;
Expand All @@ -44850,7 +44965,6 @@ void copy_friend_ip_port(Net_Crypto *c, const int crypt_conn_id, char *report_st
if (tcp_con_num > 0 && status == TCP_CONNECTIONS_STATUS_ONLINE) {
tcp_con_num -= 1;
TCP_con *tcp_con = get_tcp_connection(c->tcp_c, tcp_con_num);

if (tcp_con == nullptr) {
continue;
}
Expand Down Expand Up @@ -59982,6 +60096,17 @@ void tox_callback_group_join_fail(Tox *tox, tox_group_join_fail_cb *callback)
tox->group_join_fail_callback = callback;
}

void tox_group_get_peer_connection_ip(const Tox *tox, uint32_t group_number, uint32_t peer_id, uint8_t *ip_str)
{
if (ip_str == nullptr) {
return;
}
assert(tox != nullptr);
tox_lock(tox);
gc_get_group_peer_connection_ip(tox->m, group_number, peer_id, ip_str);
tox_unlock(tox);
}

uint32_t tox_group_new(Tox *tox, Tox_Group_Privacy_State privacy_state, const uint8_t *group_name,
size_t group_name_length, const uint8_t *name, size_t name_length, Tox_Err_Group_New *error)
{
Expand Down
7 changes: 7 additions & 0 deletions toxcore/group_chats.c
Original file line number Diff line number Diff line change
Expand Up @@ -3419,6 +3419,9 @@ void gc_get_group_peer_connection_ip(const Messenger *m, int group_number, uint3
// get tcp connections
const TCP_Connection_to *con_to = get_connection(chat->tcp_conn, gconn->tcp_connection_num);

if (con_to == nullptr) {
return;
}
for (uint32_t i = 0; i < MAX_FRIEND_TCP_CONNECTIONS; ++i) {
uint32_t tcp_con_num = con_to->connections[i].tcp_connection;
const uint8_t status = con_to->connections[i].status;
Expand All @@ -3431,6 +3434,10 @@ void gc_get_group_peer_connection_ip(const Messenger *m, int group_number, uint3
continue;
}

if (tcp_con->connection == nullptr) {
continue;
}

const IP_Port conn_ip_port = tcp_con_ip_port(tcp_con->connection);

if (!net_family_is_unspec(conn_ip_port.ip.family)) {
Expand Down
Loading

0 comments on commit 2dfbff0

Please sign in to comment.