Skip to content

Commit

Permalink
Address some unused return values
Browse files Browse the repository at this point in the history
We now either log errors or properly propagate them. In the case of
ping_send_request we make the function return void because it doesn't
seem to matter whether or not it succeeds.
  • Loading branch information
JFreegman committed Dec 11, 2021
1 parent 891fe60 commit 757e928
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 11 deletions.
1 change: 1 addition & 0 deletions toxcore/TCP_client.c
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ static int connect_sock_to(Socket sock, IP_Port ip_port, const TCP_Proxy_Info *p

/* nonblocking socket, connect will never return success */
net_connect(sock, ip_port);

return 1;
}

Expand Down
17 changes: 13 additions & 4 deletions toxcore/network.c
Original file line number Diff line number Diff line change
Expand Up @@ -798,12 +798,21 @@ Networking_Core *new_networking_ex(const Logger *log, IP ip, uint16_t port_from,
/* Functions to increase the size of the send and receive UDP buffers.
*/
int n = 1024 * 1024 * 2;
setsockopt(temp->sock.socket, SOL_SOCKET, SO_RCVBUF, (const char *)&n, sizeof(n));
setsockopt(temp->sock.socket, SOL_SOCKET, SO_SNDBUF, (const char *)&n, sizeof(n));

if (setsockopt(temp->sock.socket, SOL_SOCKET, SO_RCVBUF, (const char *)&n, sizeof(n)) != 0) {
LOGGER_WARNING(log, "Failed to set socket option %d", SO_RCVBUF);
}

if (setsockopt(temp->sock.socket, SOL_SOCKET, SO_SNDBUF, (const char *)&n, sizeof(n)) != 0) {
LOGGER_WARNING(log, "Failed to set socket option %d", SO_SNDBUF);
}

/* Enable broadcast on socket */
int broadcast = 1;
setsockopt(temp->sock.socket, SOL_SOCKET, SO_BROADCAST, (const char *)&broadcast, sizeof(broadcast));

if (setsockopt(temp->sock.socket, SOL_SOCKET, SO_BROADCAST, (const char *)&broadcast, sizeof(broadcast)) != 0) {
LOGGER_WARNING(log, "Failed to set socket option %d", SO_BROADCAST);
}

/* iOS UDP sockets are weird and apparently can SIGPIPE */
if (!set_socket_nosigpipe(temp->sock)) {
Expand Down Expand Up @@ -1324,7 +1333,7 @@ int net_connect(Socket sock, IP_Port ip_port)
fill_addr6(ip_port.ip.ip.v6, &addr6->sin6_addr);
addr6->sin6_port = ip_port.port;
} else {
return 0;
return -1;
}

return connect(sock.socket, (struct sockaddr *)&addr, addrsize);
Expand Down
6 changes: 5 additions & 1 deletion toxcore/network.h
Original file line number Diff line number Diff line change
Expand Up @@ -373,7 +373,11 @@ void networking_registerhandler(Networking_Core *net, uint8_t byte, packet_handl
/* Call this several times a second. */
void networking_poll(Networking_Core *net, void *userdata);

/* Connect a socket to the address specified by the ip_port. */
/* Connect a socket to the address specified by the ip_port.
*
* Return 0 on success.
* Return -1 on failure.
*/
int net_connect(Socket sock, IP_Port ip_port);

/* High-level getaddrinfo implementation.
Expand Down
11 changes: 6 additions & 5 deletions toxcore/ping.c
Original file line number Diff line number Diff line change
Expand Up @@ -45,14 +45,14 @@ struct Ping {
#define DHT_PING_SIZE (1 + CRYPTO_PUBLIC_KEY_SIZE + CRYPTO_NONCE_SIZE + PING_PLAIN_SIZE + CRYPTO_MAC_SIZE)
#define PING_DATA_SIZE (CRYPTO_PUBLIC_KEY_SIZE + sizeof(IP_Port))

int32_t ping_send_request(Ping *ping, IP_Port ipp, const uint8_t *public_key)
void ping_send_request(Ping *ping, IP_Port ipp, const uint8_t *public_key)
{
uint8_t pk[DHT_PING_SIZE];
int rc;
uint64_t ping_id;

if (id_equal(public_key, dht_get_self_public_key(ping->dht))) {
return 1;
return;
}

uint8_t shared_key[CRYPTO_SHARED_KEY_SIZE];
Expand All @@ -67,7 +67,7 @@ int32_t ping_send_request(Ping *ping, IP_Port ipp, const uint8_t *public_key)

if (ping_id == 0) {
crypto_memzero(shared_key, sizeof(shared_key));
return 1;
return;
}

uint8_t ping_plain[PING_PLAIN_SIZE];
Expand All @@ -87,10 +87,11 @@ int32_t ping_send_request(Ping *ping, IP_Port ipp, const uint8_t *public_key)
crypto_memzero(shared_key, sizeof(shared_key));

if (rc != PING_PLAIN_SIZE + CRYPTO_MAC_SIZE) {
return 1;
return;
}

return sendpacket(dht_get_net(ping->dht), ipp, pk, sizeof(pk));
// We never check this return value and failures in sendpacket are already logged
(void) sendpacket(dht_get_net(ping->dht), ipp, pk, sizeof(pk));
}

static int ping_send_response(Ping *ping, IP_Port ipp, const uint8_t *public_key, uint64_t ping_id,
Expand Down
2 changes: 1 addition & 1 deletion toxcore/ping.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,6 @@ int32_t ping_add(Ping *ping, const uint8_t *public_key, struct IP_Port ip_port);

void ping_iterate(Ping *ping);

int32_t ping_send_request(Ping *ping, struct IP_Port ipp, const uint8_t *public_key);
void ping_send_request(Ping *ping, struct IP_Port ipp, const uint8_t *public_key);

#endif // C_TOXCORE_TOXCORE_PING_H

0 comments on commit 757e928

Please sign in to comment.