Skip to content

Commit 5536cb7

Browse files
committed
cleanup: Move test-only functions into tests.
Also inlined `new_networking` in most places.
1 parent 2cc8daf commit 5536cb7

File tree

9 files changed

+46
-64
lines changed

9 files changed

+46
-64
lines changed

auto_tests/onion_test.c

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,38 @@ static int handle_test_4(void *object, const IP_Port *source, const uint8_t *pac
164164
return 0;
165165
}
166166

167+
/** Create and send a onion packet.
168+
*
169+
* Use Onion_Path path to send data of length to dest.
170+
* Maximum length of data is ONION_MAX_DATA_SIZE.
171+
*
172+
* return -1 on failure.
173+
* return 0 on success.
174+
*/
175+
static int send_onion_packet(const Networking_Core *net, const Onion_Path *path, const IP_Port *dest, const uint8_t *data, uint16_t length)
176+
{
177+
uint8_t packet[ONION_MAX_PACKET_SIZE];
178+
const int len = create_onion_packet(packet, sizeof(packet), path, dest, data, length);
179+
180+
if (len == -1) {
181+
return -1;
182+
}
183+
184+
if (sendpacket(net, &path->ip_port1, packet, len) != len) {
185+
return -1;
186+
}
187+
188+
return 0;
189+
}
190+
191+
/** Initialize networking.
192+
* Added for reverse compatibility with old new_networking calls.
193+
*/
194+
static Networking_Core *new_networking(const Logger *log, const IP *ip, uint16_t port)
195+
{
196+
return new_networking_ex(log, ip, port, port + (TOX_PORTRANGE_TO - TOX_PORTRANGE_FROM), nullptr);
197+
}
198+
167199
static void test_basic(void)
168200
{
169201
uint32_t index[] = { 1, 2, 3 };

other/DHT_bootstrap.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ int main(int argc, char *argv[])
142142
}
143143

144144
Mono_Time *mono_time = mono_time_new();
145-
DHT *dht = new_dht(logger, mono_time, new_networking(logger, &ip, PORT), true);
145+
DHT *dht = new_dht(logger, mono_time, new_networking_ex(logger, &ip, PORT, PORT, nullptr), true);
146146
Onion *onion = new_onion(logger, mono_time, dht);
147147
const Onion_Announce *onion_a = new_onion_announce(logger, mono_time, dht);
148148

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
ac88db3ee0c9ca621846897bb508c469eba97c263637966d74483e266b5564b0 /usr/local/bin/tox-bootstrapd
1+
1f686b142e173522393e1c9722fe939ac751cec3df2a3f7a0513994aea9cf2aa /usr/local/bin/tox-bootstrapd

other/bootstrap_daemon/src/tox-bootstrapd.c

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -234,7 +234,7 @@ int main(int argc, char *argv[])
234234

235235
char *pid_file_path = nullptr;
236236
char *keys_file_path = nullptr;
237-
int port;
237+
int start_port;
238238
int enable_ipv6;
239239
int enable_ipv4_fallback;
240240
int enable_lan_discovery;
@@ -244,16 +244,16 @@ int main(int argc, char *argv[])
244244
int enable_motd;
245245
char *motd = nullptr;
246246

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

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

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

282283
if (net == nullptr) {
283284
if (enable_ipv6 && enable_ipv4_fallback) {
284285
log_write(LOG_LEVEL_WARNING, "Couldn't initialize IPv6 networking. Falling back to using IPv4.\n");
285286
enable_ipv6 = 0;
286287
ip_init(&ip, enable_ipv6);
287-
net = new_networking(logger, &ip, port);
288+
net = new_networking_ex(logger, &ip, start_port, end_port, nullptr);
288289

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

472473
uint64_t last_LANdiscovery = 0;
473-
const uint16_t net_htons_port = net_htons(port);
474+
const uint16_t net_htons_port = net_htons(start_port);
474475

475476
int waiting_for_dht_connection = 1;
476477

toxcore/network.c

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -813,14 +813,6 @@ static void at_shutdown(void)
813813
}
814814
#endif
815815

816-
/** Initialize networking.
817-
* Added for reverse compatibility with old new_networking calls.
818-
*/
819-
Networking_Core *new_networking(const Logger *log, const IP *ip, uint16_t port)
820-
{
821-
return new_networking_ex(log, ip, port, port + (TOX_PORTRANGE_TO - TOX_PORTRANGE_FROM), nullptr);
822-
}
823-
824816
/** Initialize networking.
825817
* Bind to ip and port.
826818
* ip must be in network order EX: 127.0.0.1 = (7F000001).
@@ -1280,17 +1272,13 @@ bool ip_parse_addr(const IP *ip, char *address, size_t length)
12801272

12811273
if (net_family_is_ipv4(ip->family)) {
12821274
struct in_addr addr;
1283-
static_assert(sizeof(addr) == sizeof(ip->ip.v4.uint32),
1284-
"assumption does not hold: in_addr should be 4 bytes");
12851275
assert(make_family(ip->family) == AF_INET);
12861276
fill_addr4(&ip->ip.v4, &addr);
12871277
return inet_ntop4(&addr, address, length) != nullptr;
12881278
}
12891279

12901280
if (net_family_is_ipv6(ip->family)) {
12911281
struct in6_addr addr;
1292-
static_assert(sizeof(addr) == sizeof(ip->ip.v6.uint8),
1293-
"assumption does not hold: in6_addr should be 16 bytes");
12941282
assert(make_family(ip->family) == AF_INET6);
12951283
fill_addr6(&ip->ip.v6, &addr);
12961284
return inet_ntop6(&addr, address, length) != nullptr;

toxcore/network.h

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -487,11 +487,6 @@ char *net_new_strerror(int error);
487487
non_null()
488488
void net_kill_strerror(char *strerror);
489489

490-
/** Initialize networking.
491-
* Added for reverse compatibility with old new_networking calls.
492-
*/
493-
non_null()
494-
Networking_Core *new_networking(const Logger *log, const IP *ip, uint16_t port);
495490
/** Initialize networking.
496491
* Bind to ip and port.
497492
* ip must be in network order EX: 127.0.0.1 = (7F000001).

toxcore/onion.c

Lines changed: 0 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -268,31 +268,6 @@ int create_onion_packet_tcp(uint8_t *packet, uint16_t max_packet_length, const O
268268
return CRYPTO_NONCE_SIZE + SIZE_IPPORT + CRYPTO_PUBLIC_KEY_SIZE + len;
269269
}
270270

271-
/** Create and send a onion packet.
272-
*
273-
* Use Onion_Path path to send data of length to dest.
274-
* Maximum length of data is ONION_MAX_DATA_SIZE.
275-
*
276-
* return -1 on failure.
277-
* return 0 on success.
278-
*/
279-
int send_onion_packet(const Networking_Core *net, const Onion_Path *path, const IP_Port *dest, const uint8_t *data,
280-
uint16_t length)
281-
{
282-
uint8_t packet[ONION_MAX_PACKET_SIZE];
283-
const int len = create_onion_packet(packet, sizeof(packet), path, dest, data, length);
284-
285-
if (len == -1) {
286-
return -1;
287-
}
288-
289-
if (sendpacket(net, &path->ip_port1, packet, len) != len) {
290-
return -1;
291-
}
292-
293-
return 0;
294-
}
295-
296271
/** Create and send a onion response sent initially to dest with.
297272
* Maximum length of data is ONION_RESPONSE_MAX_DATA_SIZE.
298273
*

toxcore/onion.h

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -115,18 +115,6 @@ non_null()
115115
int create_onion_packet_tcp(uint8_t *packet, uint16_t max_packet_length, const Onion_Path *path, const IP_Port *dest,
116116
const uint8_t *data, uint16_t length);
117117

118-
/** Create and send a onion packet.
119-
*
120-
* Use Onion_Path path to send data of length to dest.
121-
* Maximum length of data is ONION_MAX_DATA_SIZE.
122-
*
123-
* return -1 on failure.
124-
* return 0 on success.
125-
*/
126-
non_null()
127-
int send_onion_packet(const Networking_Core *net, const Onion_Path *path, const IP_Port *dest, const uint8_t *data,
128-
uint16_t length);
129-
130118
/** Create and send a onion response sent initially to dest with.
131119
* Maximum length of data is ONION_RESPONSE_MAX_DATA_SIZE.
132120
*
@@ -137,7 +125,7 @@ non_null()
137125
int send_onion_response(const Networking_Core *net, const IP_Port *dest, const uint8_t *data, uint16_t length,
138126
const uint8_t *ret);
139127

140-
/** Function to handle/send received decrypted versions of the packet sent with send_onion_packet.
128+
/** Function to handle/send received decrypted versions of the packet created by create_onion_packet.
141129
*
142130
* return 0 on success.
143131
* return 1 on failure.

toxcore/tox_dispatch.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,9 @@ Tox_Dispatch *tox_dispatch_new(Tox_Err_Dispatch_New *error)
4747
*dispatch = (Tox_Dispatch) {
4848
nullptr
4949
};
50+
if (error != nullptr) {
51+
*error = TOX_ERR_DISPATCH_NEW_OK;
52+
}
5053
return dispatch;
5154
}
5255

0 commit comments

Comments
 (0)