Skip to content

Commit

Permalink
cleanup: Make Networking_Core pointer-to-const where possible.
Browse files Browse the repository at this point in the history
  • Loading branch information
iphydf committed Jan 16, 2022
1 parent c081a50 commit 6c081e9
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 13 deletions.
21 changes: 11 additions & 10 deletions toxcore/network.c
Original file line number Diff line number Diff line change
Expand Up @@ -485,7 +485,7 @@ static uint32_t data_1(uint16_t buflen, const uint8_t *buffer)
}

static void loglogdata(const Logger *log, const char *message, const uint8_t *buffer,
uint16_t buflen, IP_Port ip_port, int res)
uint16_t buflen, IP_Port ip_port, long res)
{
char ip_str[IP_NTOA_LEN];

Expand All @@ -503,7 +503,7 @@ static void loglogdata(const Logger *log, const char *message, const uint8_t *bu
ip_ntoa(&ip_port.ip, ip_str, sizeof(ip_str)), net_ntohs(ip_port.port), 0, "OK",
data_0(buflen, buffer), data_1(buflen, buffer));
} else { /* empty or overwrite */
LOGGER_TRACE(log, "[%2u] %s %u%c%u %s:%u (%u: %s) | %04x%04x",
LOGGER_TRACE(log, "[%2u] %s %lu%c%u %s:%u (%u: %s) | %04x%04x",
buffer[0], message, res, !res ? '!' : '>', buflen,
ip_ntoa(&ip_port.ip, ip_str, sizeof(ip_str)), net_ntohs(ip_port.port), 0, "OK",
data_0(buflen, buffer), data_1(buflen, buffer));
Expand Down Expand Up @@ -538,7 +538,7 @@ uint16_t net_port(const Networking_Core *net)
/* Basic network functions:
*/

int send_packet(Networking_Core *net, IP_Port ip_port, Packet packet)
int send_packet(const Networking_Core *net, IP_Port ip_port, Packet packet)
{
if (net_family_is_unspec(net->family)) { /* Socket not initialized */
// TODO(iphydf): Make this an error. Currently, the onion client calls
Expand Down Expand Up @@ -600,24 +600,25 @@ int send_packet(Networking_Core *net, IP_Port ip_port, Packet packet)
}

#ifdef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION
const int res = fuzz_sendto(net->sock.socket, (const char *)packet.data, packet.length, 0,
(struct sockaddr *)&addr, addrsize);
const long res = fuzz_sendto(net->sock.socket, (const char *)packet.data, packet.length, 0,
(struct sockaddr *)&addr, addrsize);
#else
const int res = sendto(net->sock.socket, (const char *)packet.data, packet.length, 0,
(struct sockaddr *)&addr, addrsize);
const long res = sendto(net->sock.socket, (const char *)packet.data, packet.length, 0,
(struct sockaddr *)&addr, addrsize);
#endif

loglogdata(net->log, "O=>", packet.data, packet.length, ip_port, res);

return res;
assert(res <= INT_MAX);
return (int)res;
}

/**
* Function to send packet(data) of length length to ip_port.
*
* @deprecated Use send_packet instead.
*/
int sendpacket(Networking_Core *net, IP_Port ip_port, const uint8_t *data, uint16_t length)
int sendpacket(const Networking_Core *net, IP_Port ip_port, const uint8_t *data, uint16_t length)
{
const Packet packet = {data, length};
return send_packet(net, ip_port, packet);
Expand Down Expand Up @@ -704,7 +705,7 @@ void networking_registerhandler(Networking_Core *net, uint8_t byte, packet_handl
net->packethandlers[byte].object = object;
}

void networking_poll(Networking_Core *net, void *userdata)
void networking_poll(const Networking_Core *net, void *userdata)
{
if (net_family_is_unspec(net->family)) {
/* Socket not initialized */
Expand Down
6 changes: 3 additions & 3 deletions toxcore/network.h
Original file line number Diff line number Diff line change
Expand Up @@ -383,20 +383,20 @@ typedef struct Packet {
/**
* Function to send a network packet to a given IP/port.
*/
int send_packet(Networking_Core *net, IP_Port ip_port, Packet packet);
int send_packet(const Networking_Core *net, IP_Port ip_port, Packet packet);

/**
* Function to send packet(data) of length length to ip_port.
*
* @deprecated Use send_packet instead.
*/
int sendpacket(Networking_Core *net, IP_Port ip_port, const uint8_t *data, uint16_t length);
int sendpacket(const Networking_Core *net, IP_Port ip_port, const uint8_t *data, uint16_t length);

/** Function to call when packet beginning with byte is received. */
void networking_registerhandler(Networking_Core *net, uint8_t byte, packet_handler_cb *cb, void *object);

/** Call this several times a second. */
void networking_poll(Networking_Core *net, void *userdata);
void networking_poll(const Networking_Core *net, void *userdata);

/** Connect a socket to the address specified by the ip_port.
*
Expand Down

0 comments on commit 6c081e9

Please sign in to comment.