Skip to content

Commit 727982d

Browse files
committed
fix: Fix bootstrap on emscripten/wasm.
Also added a whole bunch of logging that I needed while debugging the issue. The solution in the end is that bootstrap needs to resolve IPs, and getaddrinfo fails in the browser. Most of the time we bootstrap against IPs anyway, so trying to parse as IP address first will shortcut that.
1 parent da4ef64 commit 727982d

15 files changed

Lines changed: 369 additions & 267 deletions

File tree

auto_tests/TCP_test.c

Lines changed: 72 additions & 59 deletions
Large diffs are not rendered by default.

other/bootstrap_daemon/docker/Dockerfile

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ COPY CMakeLists.txt so.version ./
3232

3333
RUN cmake -B_build -H. \
3434
-GNinja \
35+
-DCMAKE_C_FLAGS=-DTCP_SERVER_USE_EPOLL \
36+
-DMIN_LOGGER_LEVEL=DEBUG \
3537
-DCMAKE_BUILD_TYPE=Release \
3638
-DFULLY_STATIC=ON \
3739
-DBUILD_TOXAV=OFF \
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
33548aa46a757ac403a823dbd02151580ead0fe51ea4f334cfeddaefb5391150 /usr/local/bin/tox-bootstrapd
1+
374184d92170e101b1dd809d0c74062699d20be0a955f2f730ae7a8a4e45a95f /usr/local/bin/tox-bootstrapd

other/bootstrap_daemon/docker/update-sha256

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#!/bin/bash
1+
#!/bin/sh
22

33
set -eux
44

toxcore/TCP_client.c

Lines changed: 46 additions & 42 deletions
Large diffs are not rendered by default.

toxcore/TCP_client.h

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,9 @@ void tcp_con_set_custom_uint(TCP_Client_Connection *con, uint32_t value);
4949

5050
/** Create new TCP connection to ip_port/public_key
5151
*/
52-
TCP_Client_Connection *new_TCP_connection(const Mono_Time *mono_time, IP_Port ip_port, const uint8_t *public_key,
53-
const uint8_t *self_public_key, const uint8_t *self_secret_key, const TCP_Proxy_Info *proxy_info);
52+
TCP_Client_Connection *new_TCP_connection(const Logger *logger, const Mono_Time *mono_time, IP_Port ip_port,
53+
const uint8_t *public_key, const uint8_t *self_public_key, const uint8_t *self_secret_key,
54+
const TCP_Proxy_Info *proxy_info);
5455

5556
/** Run the TCP connection
5657
*/
@@ -67,7 +68,7 @@ typedef int tcp_onion_response_cb(void *object, const uint8_t *data, uint16_t le
6768
* return 0 if could not send packet.
6869
* return -1 on failure (connection must be killed).
6970
*/
70-
int send_onion_request(TCP_Client_Connection *con, const uint8_t *data, uint16_t length);
71+
int send_onion_request(const Logger *logger, TCP_Client_Connection *con, const uint8_t *data, uint16_t length);
7172
void onion_response_handler(TCP_Client_Connection *con, tcp_onion_response_cb *onion_callback, void *object);
7273

7374
typedef int tcp_routing_response_cb(void *object, uint8_t connection_id, const uint8_t *public_key);
@@ -77,15 +78,15 @@ typedef int tcp_routing_status_cb(void *object, uint32_t number, uint8_t connect
7778
* return 0 if could not send packet.
7879
* return -1 on failure (connection must be killed).
7980
*/
80-
int send_routing_request(TCP_Client_Connection *con, const uint8_t *public_key);
81+
int send_routing_request(const Logger *logger, TCP_Client_Connection *con, const uint8_t *public_key);
8182
void routing_response_handler(TCP_Client_Connection *con, tcp_routing_response_cb *response_callback, void *object);
8283
void routing_status_handler(TCP_Client_Connection *con, tcp_routing_status_cb *status_callback, void *object);
8384

8485
/** return 1 on success.
8586
* return 0 if could not send packet.
8687
* return -1 on failure (connection must be killed).
8788
*/
88-
int send_disconnect_request(TCP_Client_Connection *con, uint8_t con_id);
89+
int send_disconnect_request(const Logger *logger, TCP_Client_Connection *con, uint8_t con_id);
8990

9091
/** Set the number that will be used as an argument in the callbacks related to con_id.
9192
*
@@ -103,7 +104,7 @@ typedef int tcp_routing_data_cb(void *object, uint32_t number, uint8_t connectio
103104
* return 0 if could not send packet.
104105
* return -1 on failure.
105106
*/
106-
int send_data(TCP_Client_Connection *con, uint8_t con_id, const uint8_t *data, uint16_t length);
107+
int send_data(const Logger *logger, TCP_Client_Connection *con, uint8_t con_id, const uint8_t *data, uint16_t length);
107108
void routing_data_handler(TCP_Client_Connection *con, tcp_routing_data_cb *data_callback, void *object);
108109

109110
typedef int tcp_oob_data_cb(void *object, const uint8_t *public_key, const uint8_t *data, uint16_t length,
@@ -113,7 +114,8 @@ typedef int tcp_oob_data_cb(void *object, const uint8_t *public_key, const uint8
113114
* return 0 if could not send packet.
114115
* return -1 on failure.
115116
*/
116-
int send_oob_packet(TCP_Client_Connection *con, const uint8_t *public_key, const uint8_t *data, uint16_t length);
117+
int send_oob_packet(const Logger *logger, TCP_Client_Connection *con, const uint8_t *public_key, const uint8_t *data,
118+
uint16_t length);
117119
void oob_data_handler(TCP_Client_Connection *con, tcp_oob_data_cb *oob_data_callback, void *object);
118120

119121

toxcore/TCP_common.c

Lines changed: 33 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,14 @@ void wipe_priority_list(TCP_Priority_List *p)
2121
/** return 0 if pending data was sent completely
2222
* return -1 if it wasn't
2323
*/
24-
int send_pending_data_nonpriority(TCP_Connection *con)
24+
int send_pending_data_nonpriority(const Logger *logger, TCP_Connection *con)
2525
{
2626
if (con->last_packet_length == 0) {
2727
return 0;
2828
}
2929

3030
const uint16_t left = con->last_packet_length - con->last_packet_sent;
31-
const int len = net_send(con->sock, con->last_packet + con->last_packet_sent, left);
31+
const int len = net_send(logger, con->sock, con->last_packet + con->last_packet_sent, left, con->ip_port);
3232

3333
if (len <= 0) {
3434
return -1;
@@ -47,18 +47,18 @@ int send_pending_data_nonpriority(TCP_Connection *con)
4747
/** return 0 if pending data was sent completely
4848
* return -1 if it wasn't
4949
*/
50-
int send_pending_data(TCP_Connection *con)
50+
int send_pending_data(const Logger *logger, TCP_Connection *con)
5151
{
5252
/* finish sending current non-priority packet */
53-
if (send_pending_data_nonpriority(con) == -1) {
53+
if (send_pending_data_nonpriority(logger, con) == -1) {
5454
return -1;
5555
}
5656

5757
TCP_Priority_List *p = con->priority_queue_start;
5858

5959
while (p) {
6060
const uint16_t left = p->size - p->sent;
61-
const int len = net_send(con->sock, p->data + p->sent, left);
61+
const int len = net_send(logger, con->sock, p->data + p->sent, left, con->ip_port);
6262

6363
if (len != left) {
6464
if (len > 0) {
@@ -122,15 +122,16 @@ bool add_priority(TCP_Connection *con, const uint8_t *packet, uint16_t size, uin
122122
* return 0 if could not send packet.
123123
* return -1 on failure (connection must be killed).
124124
*/
125-
int write_packet_TCP_secure_connection(TCP_Connection *con, const uint8_t *data, uint16_t length, bool priority)
125+
int write_packet_TCP_secure_connection(const Logger *logger, TCP_Connection *con, const uint8_t *data, uint16_t length,
126+
bool priority)
126127
{
127128
if (length + CRYPTO_MAC_SIZE > MAX_PACKET_SIZE) {
128129
return -1;
129130
}
130131

131132
bool sendpriority = 1;
132133

133-
if (send_pending_data(con) == -1) {
134+
if (send_pending_data(logger, con) == -1) {
134135
if (priority) {
135136
sendpriority = 0;
136137
} else {
@@ -149,7 +150,7 @@ int write_packet_TCP_secure_connection(TCP_Connection *con, const uint8_t *data,
149150
}
150151

151152
if (priority) {
152-
len = sendpriority ? net_send(con->sock, packet, SIZEOF_VLA(packet)) : 0;
153+
len = sendpriority ? net_send(logger, con->sock, packet, SIZEOF_VLA(packet), con->ip_port) : 0;
153154

154155
if (len <= 0) {
155156
len = 0;
@@ -164,7 +165,7 @@ int write_packet_TCP_secure_connection(TCP_Connection *con, const uint8_t *data,
164165
return add_priority(con, packet, SIZEOF_VLA(packet), len);
165166
}
166167

167-
len = net_send(con->sock, packet, SIZEOF_VLA(packet));
168+
len = net_send(logger, con->sock, packet, SIZEOF_VLA(packet), con->ip_port);
168169

169170
if (len <= 0) {
170171
return 0;
@@ -187,22 +188,23 @@ int write_packet_TCP_secure_connection(TCP_Connection *con, const uint8_t *data,
187188
* return length on success
188189
* return -1 on failure/no data in buffer.
189190
*/
190-
int read_TCP_packet(const Logger *logger, Socket sock, uint8_t *data, uint16_t length)
191+
int read_TCP_packet(const Logger *logger, Socket sock, uint8_t *data, uint16_t length, IP_Port ip_port)
191192
{
192193
const uint16_t count = net_socket_data_recv_buffer(sock);
193194

194-
if (count >= length) {
195-
const int len = net_recv(sock, data, length);
195+
if (count < length) {
196+
LOGGER_INFO(logger, "recv buffer has %d bytes, but requested %d bytes", count, length);
197+
return -1;
198+
}
196199

197-
if (len != length) {
198-
LOGGER_ERROR(logger, "FAIL recv packet");
199-
return -1;
200-
}
200+
const int len = net_recv(logger, sock, data, length, ip_port);
201201

202-
return len;
202+
if (len != length) {
203+
LOGGER_ERROR(logger, "FAIL recv packet");
204+
return -1;
203205
}
204206

205-
return -1;
207+
return len;
206208
}
207209

208210
/** Read the next two bytes in TCP stream then convert them to
@@ -212,22 +214,24 @@ int read_TCP_packet(const Logger *logger, Socket sock, uint8_t *data, uint16_t l
212214
* return 0 if nothing has been read from socket.
213215
* return -1 on failure.
214216
*/
215-
static uint16_t read_TCP_length(const Logger *logger, Socket sock)
217+
static uint16_t read_TCP_length(const Logger *logger, Socket sock, IP_Port ip_port)
216218
{
217219
const uint16_t count = net_socket_data_recv_buffer(sock);
218220

219221
if (count >= sizeof(uint16_t)) {
220-
uint16_t length;
221-
const int len = net_recv(sock, &length, sizeof(uint16_t));
222+
uint8_t length_buf[sizeof(uint16_t)];
223+
const int len = net_recv(logger, sock, length_buf, sizeof(length_buf), ip_port);
222224

223225
if (len != sizeof(uint16_t)) {
224226
LOGGER_ERROR(logger, "FAIL recv packet");
225227
return 0;
226228
}
227229

228-
length = net_ntohs(length);
230+
uint16_t length;
231+
net_unpack_u16(length_buf, &length);
229232

230233
if (length > MAX_PACKET_SIZE) {
234+
LOGGER_WARNING(logger, "TCP packet too large: %d > %d", length, MAX_PACKET_SIZE);
231235
return -1;
232236
}
233237

@@ -242,10 +246,10 @@ static uint16_t read_TCP_length(const Logger *logger, Socket sock)
242246
* return -1 on failure (connection must be killed).
243247
*/
244248
int read_packet_TCP_secure_connection(const Logger *logger, Socket sock, uint16_t *next_packet_length,
245-
const uint8_t *shared_key, uint8_t *recv_nonce, uint8_t *data, uint16_t max_len)
249+
const uint8_t *shared_key, uint8_t *recv_nonce, uint8_t *data, uint16_t max_len, IP_Port ip_port)
246250
{
247251
if (*next_packet_length == 0) {
248-
uint16_t len = read_TCP_length(logger, sock);
252+
const uint16_t len = read_TCP_length(logger, sock, ip_port);
249253

250254
if (len == (uint16_t) -1) {
251255
return -1;
@@ -259,21 +263,24 @@ int read_packet_TCP_secure_connection(const Logger *logger, Socket sock, uint16_
259263
}
260264

261265
if (max_len + CRYPTO_MAC_SIZE < *next_packet_length) {
266+
LOGGER_DEBUG(logger, "packet too large");
262267
return -1;
263268
}
264269

265270
VLA(uint8_t, data_encrypted, *next_packet_length);
266-
int len_packet = read_TCP_packet(logger, sock, data_encrypted, *next_packet_length);
271+
const int len_packet = read_TCP_packet(logger, sock, data_encrypted, *next_packet_length, ip_port);
267272

268273
if (len_packet != *next_packet_length) {
274+
LOGGER_WARNING(logger, "invalid packet length: %d, expected %d", len_packet, *next_packet_length);
269275
return 0;
270276
}
271277

272278
*next_packet_length = 0;
273279

274-
int len = decrypt_data_symmetric(shared_key, recv_nonce, data_encrypted, len_packet, data);
280+
const int len = decrypt_data_symmetric(shared_key, recv_nonce, data_encrypted, len_packet, data);
275281

276282
if (len + CRYPTO_MAC_SIZE != len_packet) {
283+
LOGGER_WARNING(logger, "decrypted length %d does not match expected length %d", len + CRYPTO_MAC_SIZE, len_packet);
277284
return -1;
278285
}
279286

toxcore/TCP_common.h

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ void wipe_priority_list(TCP_Priority_List *p);
4646

4747
typedef struct TCP_Connection {
4848
Socket sock;
49+
IP_Port ip_port; // for debugging.
4950
uint8_t sent_nonce[CRYPTO_NONCE_SIZE]; /* Nonce of sent packets. */
5051
uint8_t shared_key[CRYPTO_SHARED_KEY_SIZE];
5152
uint8_t last_packet[2 + MAX_PACKET_SIZE];
@@ -59,12 +60,12 @@ typedef struct TCP_Connection {
5960
/** return 0 if pending data was sent completely
6061
* return -1 if it wasn't
6162
*/
62-
int send_pending_data_nonpriority(TCP_Connection *con);
63+
int send_pending_data_nonpriority(const Logger *logger, TCP_Connection *con);
6364

6465
/** return 0 if pending data was sent completely
6566
* return -1 if it wasn't
6667
*/
67-
int send_pending_data(TCP_Connection *con);
68+
int send_pending_data(const Logger *logger, TCP_Connection *con);
6869

6970
/** return 0 on failure (only if calloc fails)
7071
* return 1 on success
@@ -75,20 +76,21 @@ bool add_priority(TCP_Connection *con, const uint8_t *packet, uint16_t size, uin
7576
* return 0 if could not send packet.
7677
* return -1 on failure (connection must be killed).
7778
*/
78-
int write_packet_TCP_secure_connection(TCP_Connection *con, const uint8_t *data, uint16_t length, bool priority);
79+
int write_packet_TCP_secure_connection(const Logger *logger, TCP_Connection *con, const uint8_t *data, uint16_t length,
80+
bool priority);
7981

8082
/** Read length bytes from socket.
8183
*
8284
* return length on success
8385
* return -1 on failure/no data in buffer.
8486
*/
85-
int read_TCP_packet(const Logger *logger, Socket sock, uint8_t *data, uint16_t length);
87+
int read_TCP_packet(const Logger *logger, Socket sock, uint8_t *data, uint16_t length, IP_Port ip_port);
8688

8789
/** return length of received packet on success.
8890
* return 0 if could not read any packet.
8991
* return -1 on failure (connection must be killed).
9092
*/
9193
int read_packet_TCP_secure_connection(const Logger *logger, Socket sock, uint16_t *next_packet_length,
92-
const uint8_t *shared_key, uint8_t *recv_nonce, uint8_t *data, uint16_t max_len);
94+
const uint8_t *shared_key, uint8_t *recv_nonce, uint8_t *data, uint16_t max_len, IP_Port ip_port);
9395

9496
#endif

0 commit comments

Comments
 (0)