Skip to content

Commit

Permalink
cleanup: Move test-only functions into tests.
Browse files Browse the repository at this point in the history
Also inlined `new_networking` in most places.
  • Loading branch information
iphydf committed Mar 3, 2022
1 parent 2cc8daf commit 0649b7f
Show file tree
Hide file tree
Showing 8 changed files with 43 additions and 60 deletions.
32 changes: 32 additions & 0 deletions auto_tests/onion_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,38 @@ static int handle_test_4(void *object, const IP_Port *source, const uint8_t *pac
return 0;
}

/** Create and send a onion packet.
*
* Use Onion_Path path to send data of length to dest.
* Maximum length of data is ONION_MAX_DATA_SIZE.
*
* return -1 on failure.
* return 0 on success.
*/
static int send_onion_packet(const Networking_Core *net, const Onion_Path *path, const IP_Port *dest, const uint8_t *data, uint16_t length)
{
uint8_t packet[ONION_MAX_PACKET_SIZE];
const int len = create_onion_packet(packet, sizeof(packet), path, dest, data, length);

if (len == -1) {
return -1;
}

if (sendpacket(net, &path->ip_port1, packet, len) != len) {
return -1;
}

return 0;
}

/** Initialize networking.
* Added for reverse compatibility with old new_networking calls.
*/
static Networking_Core *new_networking(const Logger *log, const IP *ip, uint16_t port)
{
return new_networking_ex(log, ip, port, port + (TOX_PORTRANGE_TO - TOX_PORTRANGE_FROM), nullptr);
}

static void test_basic(void)
{
uint32_t index[] = { 1, 2, 3 };
Expand Down
2 changes: 1 addition & 1 deletion other/DHT_bootstrap.c
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ int main(int argc, char *argv[])
}

Mono_Time *mono_time = mono_time_new();
DHT *dht = new_dht(logger, mono_time, new_networking(logger, &ip, PORT), true);
DHT *dht = new_dht(logger, mono_time, new_networking_ex(logger, &ip, PORT, PORT, nullptr), true);
Onion *onion = new_onion(logger, mono_time, dht);
const Onion_Announce *onion_a = new_onion_announce(logger, mono_time, dht);

Expand Down
2 changes: 1 addition & 1 deletion other/bootstrap_daemon/docker/tox-bootstrapd.sha256
Original file line number Diff line number Diff line change
@@ -1 +1 @@
ac88db3ee0c9ca621846897bb508c469eba97c263637966d74483e266b5564b0 /usr/local/bin/tox-bootstrapd
1f686b142e173522393e1c9722fe939ac751cec3df2a3f7a0513994aea9cf2aa /usr/local/bin/tox-bootstrapd
15 changes: 8 additions & 7 deletions other/bootstrap_daemon/src/tox-bootstrapd.c
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,7 @@ int main(int argc, char *argv[])

char *pid_file_path = nullptr;
char *keys_file_path = nullptr;
int port;
int start_port;
int enable_ipv6;
int enable_ipv4_fallback;
int enable_lan_discovery;
Expand All @@ -244,16 +244,16 @@ int main(int argc, char *argv[])
int enable_motd;
char *motd = nullptr;

if (get_general_config(cfg_file_path, &pid_file_path, &keys_file_path, &port, &enable_ipv6, &enable_ipv4_fallback,
if (get_general_config(cfg_file_path, &pid_file_path, &keys_file_path, &start_port, &enable_ipv6, &enable_ipv4_fallback,
&enable_lan_discovery, &enable_tcp_relay, &tcp_relay_ports, &tcp_relay_port_count, &enable_motd, &motd)) {
log_write(LOG_LEVEL_INFO, "General config read successfully\n");
} else {
log_write(LOG_LEVEL_ERROR, "Couldn't read config file: %s. Exiting.\n", cfg_file_path);
return 1;
}

if (port < MIN_ALLOWED_PORT || port > MAX_ALLOWED_PORT) {
log_write(LOG_LEVEL_ERROR, "Invalid port: %d, should be in [%d, %d]. Exiting.\n", port, MIN_ALLOWED_PORT,
if (start_port < MIN_ALLOWED_PORT || start_port > MAX_ALLOWED_PORT) {
log_write(LOG_LEVEL_ERROR, "Invalid port: %d, should be in [%d, %d]. Exiting.\n", start_port, MIN_ALLOWED_PORT,
MAX_ALLOWED_PORT);
free(motd);
free(tcp_relay_ports);
Expand All @@ -277,14 +277,15 @@ int main(int argc, char *argv[])
logger_callback_log(logger, toxcore_logger_callback, nullptr, nullptr);
}

Networking_Core *net = new_networking(logger, &ip, port);
const uint16_t end_port = start_port + (TOX_PORTRANGE_TO - TOX_PORTRANGE_FROM);
Networking_Core *net = new_networking_ex(logger, &ip, start_port, end_port, nullptr);

if (net == nullptr) {
if (enable_ipv6 && enable_ipv4_fallback) {
log_write(LOG_LEVEL_WARNING, "Couldn't initialize IPv6 networking. Falling back to using IPv4.\n");
enable_ipv6 = 0;
ip_init(&ip, enable_ipv6);
net = new_networking(logger, &ip, port);
net = new_networking_ex(logger, &ip, start_port, end_port, nullptr);

if (net == nullptr) {
log_write(LOG_LEVEL_ERROR, "Couldn't fallback to IPv4. Exiting.\n");
Expand Down Expand Up @@ -470,7 +471,7 @@ int main(int argc, char *argv[])
print_public_key(dht_get_self_public_key(dht));

uint64_t last_LANdiscovery = 0;
const uint16_t net_htons_port = net_htons(port);
const uint16_t net_htons_port = net_htons(start_port);

int waiting_for_dht_connection = 1;

Expand Down
8 changes: 0 additions & 8 deletions toxcore/network.c
Original file line number Diff line number Diff line change
Expand Up @@ -813,14 +813,6 @@ static void at_shutdown(void)
}
#endif

/** Initialize networking.
* Added for reverse compatibility with old new_networking calls.
*/
Networking_Core *new_networking(const Logger *log, const IP *ip, uint16_t port)
{
return new_networking_ex(log, ip, port, port + (TOX_PORTRANGE_TO - TOX_PORTRANGE_FROM), nullptr);
}

/** Initialize networking.
* Bind to ip and port.
* ip must be in network order EX: 127.0.0.1 = (7F000001).
Expand Down
5 changes: 0 additions & 5 deletions toxcore/network.h
Original file line number Diff line number Diff line change
Expand Up @@ -487,11 +487,6 @@ char *net_new_strerror(int error);
non_null()
void net_kill_strerror(char *strerror);

/** Initialize networking.
* Added for reverse compatibility with old new_networking calls.
*/
non_null()
Networking_Core *new_networking(const Logger *log, const IP *ip, uint16_t port);
/** Initialize networking.
* Bind to ip and port.
* ip must be in network order EX: 127.0.0.1 = (7F000001).
Expand Down
25 changes: 0 additions & 25 deletions toxcore/onion.c
Original file line number Diff line number Diff line change
Expand Up @@ -268,31 +268,6 @@ int create_onion_packet_tcp(uint8_t *packet, uint16_t max_packet_length, const O
return CRYPTO_NONCE_SIZE + SIZE_IPPORT + CRYPTO_PUBLIC_KEY_SIZE + len;
}

/** Create and send a onion packet.
*
* Use Onion_Path path to send data of length to dest.
* Maximum length of data is ONION_MAX_DATA_SIZE.
*
* return -1 on failure.
* return 0 on success.
*/
int send_onion_packet(const Networking_Core *net, const Onion_Path *path, const IP_Port *dest, const uint8_t *data,
uint16_t length)
{
uint8_t packet[ONION_MAX_PACKET_SIZE];
const int len = create_onion_packet(packet, sizeof(packet), path, dest, data, length);

if (len == -1) {
return -1;
}

if (sendpacket(net, &path->ip_port1, packet, len) != len) {
return -1;
}

return 0;
}

/** Create and send a onion response sent initially to dest with.
* Maximum length of data is ONION_RESPONSE_MAX_DATA_SIZE.
*
Expand Down
14 changes: 1 addition & 13 deletions toxcore/onion.h
Original file line number Diff line number Diff line change
Expand Up @@ -115,18 +115,6 @@ non_null()
int create_onion_packet_tcp(uint8_t *packet, uint16_t max_packet_length, const Onion_Path *path, const IP_Port *dest,
const uint8_t *data, uint16_t length);

/** Create and send a onion packet.
*
* Use Onion_Path path to send data of length to dest.
* Maximum length of data is ONION_MAX_DATA_SIZE.
*
* return -1 on failure.
* return 0 on success.
*/
non_null()
int send_onion_packet(const Networking_Core *net, const Onion_Path *path, const IP_Port *dest, const uint8_t *data,
uint16_t length);

/** Create and send a onion response sent initially to dest with.
* Maximum length of data is ONION_RESPONSE_MAX_DATA_SIZE.
*
Expand All @@ -137,7 +125,7 @@ non_null()
int send_onion_response(const Networking_Core *net, const IP_Port *dest, const uint8_t *data, uint16_t length,
const uint8_t *ret);

/** Function to handle/send received decrypted versions of the packet sent with send_onion_packet.
/** Function to handle/send received decrypted versions of the packet created by create_onion_packet.
*
* return 0 on success.
* return 1 on failure.
Expand Down

0 comments on commit 0649b7f

Please sign in to comment.