summaryrefslogtreecommitdiff
path: root/toxcore
diff options
context:
space:
mode:
Diffstat (limited to 'toxcore')
-rw-r--r--toxcore/Lossless_UDP.c376
-rw-r--r--toxcore/Lossless_UDP.h9
2 files changed, 228 insertions, 157 deletions
diff --git a/toxcore/Lossless_UDP.c b/toxcore/Lossless_UDP.c
index c30eb903..717882c1 100644
--- a/toxcore/Lossless_UDP.c
+++ b/toxcore/Lossless_UDP.c
@@ -38,13 +38,22 @@
38 */ 38 */
39int getconnection_id(Lossless_UDP *ludp, IP_Port ip_port) 39int getconnection_id(Lossless_UDP *ludp, IP_Port ip_port)
40{ 40{
41 uint32_t i; 41 /*vx-t uint32_t i;
42 42
43 for (i = 0; i < ludp->connections_length; ++i) { 43 for (i = 0; i < ludp->connections_length; ++i) {
44 if (ludp->connections[i].ip_port.ip.i == ip_port.ip.i && 44 if (ludp->connections[i].ip_port.ip.i == ip_port.ip.i &&
45 ludp->connections[i].ip_port.port == ip_port.port && 45 ludp->connections[i].ip_port.port == ip_port.port &&
46 ludp->connections[i].status > 0) 46 ludp->connections[i].status > 0)
47 return i; 47 return i;
48 }*/
49
50 tox_array_for_each(&ludp->connections, Connection, tmp) {
51 if (tmp.ip_port.ip.i == ip_port.ip.i &&
52 tmp.ip_port.port == ip_port.port &&
53 tmp.status > 0)
54 {
55 return tmp_i;
56 }
48 } 57 }
49 58
50 return -1; 59 return -1;
@@ -98,7 +107,12 @@ int new_connection(Lossless_UDP *ludp, IP_Port ip_port)
98 if (connect != -1) 107 if (connect != -1)
99 return connect; 108 return connect;
100 109
101 if (ludp->connections_number == ludp->connections_length) { 110 /* TODO: this allocates space regardless of whether it's needed or not
111 * Rewrite so it will first scan for empty, and if not found make a new one.
112 */
113 tox_array_push_ptr(&ludp->connections, 0);
114 memset(&tox_array_get(&ludp->connections, ludp->connections.len, Connection), 0, sizeof(Connection));
115 /*vx-t if (ludp->connections_number == ludp->connections_length) {
102 Connection *temp; 116 Connection *temp;
103 temp = realloc(ludp->connections, sizeof(Connection) * (ludp->connections_length + 1)); 117 temp = realloc(ludp->connections, sizeof(Connection) * (ludp->connections_length + 1));
104 118
@@ -108,9 +122,34 @@ int new_connection(Lossless_UDP *ludp, IP_Port ip_port)
108 memset(&temp[ludp->connections_length], 0, sizeof(Connection)); 122 memset(&temp[ludp->connections_length], 0, sizeof(Connection));
109 ++ludp->connections_length; 123 ++ludp->connections_length;
110 ludp->connections = temp; 124 ludp->connections = temp;
111 } 125 }*/
112 126
113 uint32_t i; 127 tox_array_for_each(&ludp->connections, Connection, tmp) {
128 if (tmp.status == 0) {
129 memset(&tox_array_get(&ludp->connections, tmp_i, Connection), 0, sizeof(Connection));
130 uint32_t handshake_id1 = handshake_id(ludp, ip_port);
131
132 tox_array_get(&ludp->connections, tmp_i, Connection) = (Connection) {
133 .ip_port = ip_port,
134 .status = 1,
135 .inbound = 0,
136 .handshake_id1 = handshake_id1,
137 .sent_packetnum = handshake_id1,
138 .sendbuff_packetnum = handshake_id1,
139 .successful_sent = handshake_id1,
140 .SYNC_rate = SYNC_RATE,
141 .data_rate = DATA_SYNC_RATE,
142 .last_recvSYNC = current_time(),
143 .last_sent = current_time(),
144 .killat = ~0,
145 .send_counter = 0,
146 /* add randomness to timeout to prevent connections getting stuck in a loop. */
147 .timeout = CONNEXION_TIMEOUT + rand() % CONNEXION_TIMEOUT
148 };
149 return tmp_i;
150 }
151 }
152 /*vx-t uint32_t i;
114 153
115 for (i = 0; i < ludp->connections_length; ++i) { 154 for (i = 0; i < ludp->connections_length; ++i) {
116 if (ludp->connections[i].status == 0) { 155 if (ludp->connections[i].status == 0) {
@@ -131,14 +170,14 @@ int new_connection(Lossless_UDP *ludp, IP_Port ip_port)
131 .last_sent = current_time(), 170 .last_sent = current_time(),
132 .killat = ~0, 171 .killat = ~0,
133 .send_counter = 0, 172 .send_counter = 0,
134 /* add randomness to timeout to prevent connections getting stuck in a loop. */ 173 /* add randomness to timeout to prevent connections getting stuck in a loop. /
135 .timeout = CONNEXION_TIMEOUT + rand() % CONNEXION_TIMEOUT 174 .timeout = CONNEXION_TIMEOUT + rand() % CONNEXION_TIMEOUT
136 }; 175 };
137 ++ludp->connections_number; 176 ++ludp->connections_number;
138 177
139 return i; 178 return i;
140 } 179 }
141 } 180 }*/
142 181
143 return -1; 182 return -1;
144} 183}
@@ -153,7 +192,11 @@ static int new_inconnection(Lossless_UDP *ludp, IP_Port ip_port)
153 if (getconnection_id(ludp, ip_port) != -1) 192 if (getconnection_id(ludp, ip_port) != -1)
154 return -1; 193 return -1;
155 194
156 if (ludp->connections_number == ludp->connections_length) { 195 /* TODO: See comment in int new_connection(Lossless_UDP *ludp, IP_Port ip_port) */
196 tox_array_push_ptr(&ludp->connections, 0);
197 memset(&tox_array_get(&ludp->connections, ludp->connections.len, Connection), 0, sizeof(Connection));
198
199 /*vx-t if (ludp->connections_number == ludp->connections_length) {
157 Connection *temp; 200 Connection *temp;
158 temp = realloc(ludp->connections, sizeof(Connection) * (ludp->connections_length + 1)); 201 temp = realloc(ludp->connections, sizeof(Connection) * (ludp->connections_length + 1));
159 202
@@ -163,8 +206,8 @@ static int new_inconnection(Lossless_UDP *ludp, IP_Port ip_port)
163 memset(&temp[ludp->connections_length], 0, sizeof(Connection)); 206 memset(&temp[ludp->connections_length], 0, sizeof(Connection));
164 ++ludp->connections_length; 207 ++ludp->connections_length;
165 ludp->connections = temp; 208 ludp->connections = temp;
166 } 209 }*/
167 210/* vx-k: IMPORTANT!!!!!!!!! DO THIS
168 uint32_t i; 211 uint32_t i;
169 212
170 for (i = 0; i < ludp->connections_length; ++i) { 213 for (i = 0; i < ludp->connections_length; ++i) {
@@ -182,17 +225,17 @@ static int new_inconnection(Lossless_UDP *ludp, IP_Port ip_port)
182 .last_sent = current_time(), 225 .last_sent = current_time(),
183 .send_counter = 127, 226 .send_counter = 127,
184 227
185 /* add randomness to timeout to prevent connections getting stuck in a loop. */ 228 /* add randomness to timeout to prevent connections getting stuck in a loop. /
186 .timeout = timeout, 229 .timeout = timeout,
187 230
188 /* if this connection isn't handled within the timeout kill it. */ 231 /* if this connection isn't handled within the timeout kill it. /
189 .killat = current_time() + 1000000UL * timeout 232 .killat = current_time() + 1000000UL * timeout
190 }; 233 };
191 ++ludp->connections_number; 234 ++ludp->connections_number;
192 return i; 235 return i;
193 } 236 }
194 } 237 }
195 238*/
196 return -1; 239 return -1;
197} 240}
198 241
@@ -202,14 +245,21 @@ static int new_inconnection(Lossless_UDP *ludp, IP_Port ip_port)
202 */ 245 */
203int incoming_connection(Lossless_UDP *ludp) 246int incoming_connection(Lossless_UDP *ludp)
204{ 247{
205 uint32_t i; 248 tox_array_for_each(&ludp->connections, Connection, tmp) {
249 if (tmp.inbound == 2) {
250 tmp.inbound = 1;
251 return tmp_i;
252 }
253 }
254
255 /*vx-k uint32_t i;
206 256
207 for (i = 0; i < ludp->connections_length; ++i) { 257 for (i = 0; i < ludp->connections_length; ++i) {
208 if (ludp->connections[i].inbound == 2) { 258 if (ludp->connections[i].inbound == 2) {
209 ludp->connections[i].inbound = 1; 259 ludp->connections[i].inbound = 1;
210 return i; 260 return i;
211 } 261 }
212 } 262 }*/
213 263
214 return -1; 264 return -1;
215} 265}
@@ -217,6 +267,7 @@ int incoming_connection(Lossless_UDP *ludp)
217/* Try to free some memory from the connections array. */ 267/* Try to free some memory from the connections array. */
218static void free_connections(Lossless_UDP *ludp) 268static void free_connections(Lossless_UDP *ludp)
219{ 269{
270 /*vx-k: fix this to work with new tox_array
220 uint32_t i; 271 uint32_t i;
221 272
222 for (i = ludp->connections_length; i != 0; --i) 273 for (i = ludp->connections_length; i != 0; --i)
@@ -241,6 +292,7 @@ static void free_connections(Lossless_UDP *ludp)
241 292
242 ludp->connections = temp; 293 ludp->connections = temp;
243 ludp->connections_length = i; 294 ludp->connections_length = i;
295 */
244} 296}
245 297
246/* 298/*
@@ -249,11 +301,11 @@ static void free_connections(Lossless_UDP *ludp)
249 */ 301 */
250int kill_connection(Lossless_UDP *ludp, int connection_id) 302int kill_connection(Lossless_UDP *ludp, int connection_id)
251{ 303{
252 if (connection_id >= 0 && connection_id < ludp->connections_length) { 304 if (connection_id >= 0 && connection_id < ludp->connections.len) {
253 if (ludp->connections[connection_id].status > 0) { 305 if (tox_array_get(&ludp->connections, connection_id, Connection).status > 0) {
254 ludp->connections[connection_id].status = 0; 306 tox_array_get(&ludp->connections, connection_id, Connection).status = 0;
255 change_handshake(ludp, ludp->connections[connection_id].ip_port); 307 change_handshake(ludp, tox_array_get(&ludp->connections, connection_id, Connection).ip_port);
256 --ludp->connections_number; 308 tox_array_pop(&ludp->connections, 0);
257 free_connections(ludp); 309 free_connections(ludp);
258 return 0; 310 return 0;
259 } 311 }
@@ -269,9 +321,9 @@ int kill_connection(Lossless_UDP *ludp, int connection_id)
269 */ 321 */
270int kill_connection_in(Lossless_UDP *ludp, int connection_id, uint32_t seconds) 322int kill_connection_in(Lossless_UDP *ludp, int connection_id, uint32_t seconds)
271{ 323{
272 if (connection_id >= 0 && connection_id < ludp->connections_length) { 324 if (connection_id >= 0 && connection_id < ludp->connections.len) {
273 if (ludp->connections[connection_id].status > 0) { 325 if (tox_array_get(&ludp->connections, connection_id, Connection).status > 0) {
274 ludp->connections[connection_id].killat = current_time() + 1000000UL * seconds; 326 tox_array_get(&ludp->connections, connection_id, Connection).killat = current_time() + 1000000UL * seconds;
275 return 0; 327 return 0;
276 } 328 }
277 } 329 }
@@ -289,8 +341,8 @@ int kill_connection_in(Lossless_UDP *ludp, int connection_id, uint32_t seconds)
289 */ 341 */
290int is_connected(Lossless_UDP *ludp, int connection_id) 342int is_connected(Lossless_UDP *ludp, int connection_id)
291{ 343{
292 if (connection_id >= 0 && connection_id < ludp->connections_length) 344 if (connection_id >= 0 && connection_id < ludp->connections.len)
293 return ludp->connections[connection_id].status; 345 return tox_array_get(&ludp->connections, connection_id, Connection).status;
294 346
295 return 0; 347 return 0;
296} 348}
@@ -298,8 +350,8 @@ int is_connected(Lossless_UDP *ludp, int connection_id)
298/* returns the ip_port of the corresponding connection. */ 350/* returns the ip_port of the corresponding connection. */
299IP_Port connection_ip(Lossless_UDP *ludp, int connection_id) 351IP_Port connection_ip(Lossless_UDP *ludp, int connection_id)
300{ 352{
301 if (connection_id >= 0 && connection_id < ludp->connections_length) 353 if (connection_id >= 0 && connection_id < ludp->connections.len)
302 return ludp->connections[connection_id].ip_port; 354 return tox_array_get(&ludp->connections, connection_id, Connection).ip_port;
303 355
304 IP_Port zero = {{{0}}, 0}; 356 IP_Port zero = {{{0}}, 0};
305 return zero; 357 return zero;
@@ -308,30 +360,31 @@ IP_Port connection_ip(Lossless_UDP *ludp, int connection_id)
308/* returns the number of packets in the queue waiting to be successfully sent. */ 360/* returns the number of packets in the queue waiting to be successfully sent. */
309uint32_t sendqueue(Lossless_UDP *ludp, int connection_id) 361uint32_t sendqueue(Lossless_UDP *ludp, int connection_id)
310{ 362{
311 if (connection_id < 0 || connection_id >= ludp->connections_length) 363 if (connection_id < 0 || connection_id >= ludp->connections.len)
312 return 0; 364 return 0;
313 365
314 return ludp->connections[connection_id].sendbuff_packetnum - ludp->connections[connection_id].successful_sent; 366 return tox_array_get(&ludp->connections, connection_id, Connection).sendbuff_packetnum -
367 tox_array_get(&ludp->connections, connection_id, Connection).successful_sent;
315} 368}
316 369
317/* returns the number of packets in the queue waiting to be successfully read with read_packet(...) */ 370/* returns the number of packets in the queue waiting to be successfully read with read_packet(...) */
318uint32_t recvqueue(Lossless_UDP *ludp, int connection_id) 371uint32_t recvqueue(Lossless_UDP *ludp, int connection_id)
319{ 372{
320 if (connection_id < 0 || connection_id >= ludp->connections_length) 373 if (connection_id < 0 || connection_id >= ludp->connections.len)
321 return 0; 374 return 0;
322 375
323 return ludp->connections[connection_id].recv_packetnum - ludp->connections[connection_id].successful_read; 376 return tox_array_get(&ludp->connections, connection_id, Connection).recv_packetnum - tox_array_get(&ludp->connections, connection_id, Connection).successful_read;
324} 377}
325 378
326/* returns the id of the next packet in the queue 379/* returns the id of the next packet in the queue
327 return -1 if no packet in queue */ 380 return -1 if no packet in queue */
328char id_packet(Lossless_UDP *ludp, int connection_id) 381char id_packet(Lossless_UDP *ludp, int connection_id)
329{ 382{
330 if (connection_id < 0 || connection_id >= ludp->connections_length) 383 if (connection_id < 0 || connection_id >= ludp->connections.len)
331 return -1; 384 return -1;
332 385
333 if (recvqueue(ludp, connection_id) != 0 && ludp->connections[connection_id].status != 0) 386 if (recvqueue(ludp, connection_id) != 0 && tox_array_get(&ludp->connections, connection_id, Connection).status != 0)
334 return ludp->connections[connection_id].recvbuffer[ludp->connections[connection_id].successful_read % 387 return tox_array_get(&ludp->connections, connection_id, Connection).recvbuffer[tox_array_get(&ludp->connections, connection_id, Connection).successful_read %
335 MAX_QUEUE_NUM].data[0]; 388 MAX_QUEUE_NUM].data[0];
336 389
337 return -1; 390 return -1;
@@ -342,11 +395,11 @@ char id_packet(Lossless_UDP *ludp, int connection_id)
342int read_packet(Lossless_UDP *ludp, int connection_id, uint8_t *data) 395int read_packet(Lossless_UDP *ludp, int connection_id, uint8_t *data)
343{ 396{
344 if (recvqueue(ludp, connection_id) != 0) { 397 if (recvqueue(ludp, connection_id) != 0) {
345 uint16_t index = ludp->connections[connection_id].successful_read % MAX_QUEUE_NUM; 398 uint16_t index = tox_array_get(&ludp->connections, connection_id, Connection).successful_read % MAX_QUEUE_NUM;
346 uint16_t size = ludp->connections[connection_id].recvbuffer[index].size; 399 uint16_t size = tox_array_get(&ludp->connections, connection_id, Connection).recvbuffer[index].size;
347 memcpy(data, ludp->connections[connection_id].recvbuffer[index].data, size); 400 memcpy(data, tox_array_get(&ludp->connections, connection_id, Connection).recvbuffer[index].data, size);
348 ++ludp->connections[connection_id].successful_read; 401 ++tox_array_get(&ludp->connections, connection_id, Connection).successful_read;
349 ludp->connections[connection_id].recvbuffer[index].size = 0; 402 tox_array_get(&ludp->connections, connection_id, Connection).recvbuffer[index].size = 0;
350 return size; 403 return size;
351 } 404 }
352 405
@@ -363,10 +416,10 @@ int write_packet(Lossless_UDP *ludp, int connection_id, uint8_t *data, uint32_t
363 return 0; 416 return 0;
364 417
365 if (sendqueue(ludp, connection_id) < BUFFER_PACKET_NUM) { 418 if (sendqueue(ludp, connection_id) < BUFFER_PACKET_NUM) {
366 uint32_t index = ludp->connections[connection_id].sendbuff_packetnum % MAX_QUEUE_NUM; 419 uint32_t index = tox_array_get(&ludp->connections, connection_id, Connection).sendbuff_packetnum % MAX_QUEUE_NUM;
367 memcpy(ludp->connections[connection_id].sendbuffer[index].data, data, length); 420 memcpy(tox_array_get(&ludp->connections, connection_id, Connection).sendbuffer[index].data, data, length);
368 ludp->connections[connection_id].sendbuffer[index].size = length; 421 tox_array_get(&ludp->connections, connection_id, Connection).sendbuffer[index].size = length;
369 ludp->connections[connection_id].sendbuff_packetnum++; 422 tox_array_get(&ludp->connections, connection_id, Connection).sendbuff_packetnum++;
370 return 1; 423 return 1;
371 } 424 }
372 425
@@ -384,8 +437,11 @@ uint32_t missing_packets(Lossless_UDP *ludp, int connection_id, uint32_t *reques
384 if (recvqueue(ludp, connection_id) >= (BUFFER_PACKET_NUM - 1)) 437 if (recvqueue(ludp, connection_id) >= (BUFFER_PACKET_NUM - 1))
385 return 0; 438 return 0;
386 439
387 for (i = ludp->connections[connection_id].recv_packetnum; i != ludp->connections[connection_id].osent_packetnum; i++) { 440 for (i = tox_array_get(&ludp->connections, connection_id, Connection).recv_packetnum;
388 if (ludp->connections[connection_id].recvbuffer[i % MAX_QUEUE_NUM].size == 0) { 441 i != tox_array_get(&ludp->connections, connection_id, Connection).osent_packetnum;
442 i++)
443 {
444 if (tox_array_get(&ludp->connections, connection_id, Connection).recvbuffer[i % MAX_QUEUE_NUM].size == 0) {
389 temp = htonl(i); 445 temp = htonl(i);
390 memcpy(requested + number, &temp, 4); 446 memcpy(requested + number, &temp, 4);
391 ++number; 447 ++number;
@@ -393,7 +449,7 @@ uint32_t missing_packets(Lossless_UDP *ludp, int connection_id, uint32_t *reques
393 } 449 }
394 450
395 if (number == 0) 451 if (number == 0)
396 ludp->connections[connection_id].recv_packetnum = ludp->connections[connection_id].osent_packetnum; 452 tox_array_get(&ludp->connections, connection_id, Connection).recv_packetnum = tox_array_get(&ludp->connections, connection_id, Connection).osent_packetnum;
397 453
398 return number; 454 return number;
399} 455}
@@ -423,10 +479,10 @@ static int send_SYNC(Lossless_UDP *ludp, uint32_t connection_id)
423 uint8_t packet[(BUFFER_PACKET_NUM * 4 + 4 + 4 + 2)]; 479 uint8_t packet[(BUFFER_PACKET_NUM * 4 + 4 + 4 + 2)];
424 uint16_t index = 0; 480 uint16_t index = 0;
425 481
426 IP_Port ip_port = ludp->connections[connection_id].ip_port; 482 IP_Port ip_port = tox_array_get(&ludp->connections, connection_id, Connection).ip_port;
427 uint8_t counter = ludp->connections[connection_id].send_counter; 483 uint8_t counter = tox_array_get(&ludp->connections, connection_id, Connection).send_counter;
428 uint32_t recv_packetnum = htonl(ludp->connections[connection_id].recv_packetnum); 484 uint32_t recv_packetnum = htonl(tox_array_get(&ludp->connections, connection_id, Connection).recv_packetnum);
429 uint32_t sent_packetnum = htonl(ludp->connections[connection_id].sent_packetnum); 485 uint32_t sent_packetnum = htonl(tox_array_get(&ludp->connections, connection_id, Connection).sent_packetnum);
430 486
431 uint32_t requested[BUFFER_PACKET_NUM]; 487 uint32_t requested[BUFFER_PACKET_NUM];
432 uint32_t number = missing_packets(ludp, connection_id, requested); 488 uint32_t number = missing_packets(ludp, connection_id, requested);
@@ -453,29 +509,30 @@ static int send_data_packet(Lossless_UDP *ludp, uint32_t connection_id, uint32_t
453 packet[0] = NET_PACKET_DATA; 509 packet[0] = NET_PACKET_DATA;
454 temp = htonl(packet_num); 510 temp = htonl(packet_num);
455 memcpy(packet + 1, &temp, 4); 511 memcpy(packet + 1, &temp, 4);
456 memcpy(packet + 5, ludp->connections[connection_id].sendbuffer[index].data, 512 memcpy(packet + 5, tox_array_get(&ludp->connections, connection_id, Connection).sendbuffer[index].data,
457 ludp->connections[connection_id].sendbuffer[index].size); 513 tox_array_get(&ludp->connections, connection_id, Connection).sendbuffer[index].size);
458 return sendpacket(ludp->net->sock, ludp->connections[connection_id].ip_port, packet, 514 return sendpacket(ludp->net->sock, tox_array_get(&ludp->connections, connection_id, Connection).ip_port, packet,
459 1 + 4 + ludp->connections[connection_id].sendbuffer[index].size); 515 1 + 4 + tox_array_get(&ludp->connections, connection_id, Connection).sendbuffer[index].size);
460} 516}
461 517
462/* sends 1 data packet */ 518/* sends 1 data packet */
463static int send_DATA(Lossless_UDP *ludp, uint32_t connection_id) 519static int send_DATA(Lossless_UDP *ludp, uint32_t connection_id)
464{ 520{
521 Connection *connection = &tox_array_get(&ludp->connections, connection_id, Connection);
465 int ret; 522 int ret;
466 uint32_t buffer[BUFFER_PACKET_NUM]; 523 uint32_t buffer[BUFFER_PACKET_NUM];
467 524
468 if (ludp->connections[connection_id].num_req_paquets > 0) { 525 if (connection->num_req_paquets > 0) {
469 ret = send_data_packet(ludp, connection_id, ludp->connections[connection_id].req_packets[0]); 526 ret = send_data_packet(ludp, connection_id, connection->req_packets[0]);
470 ludp->connections[connection_id].num_req_paquets--; 527 connection->num_req_paquets--;
471 memcpy(buffer, ludp->connections[connection_id].req_packets + 1, ludp->connections[connection_id].num_req_paquets * 4); 528 memcpy(buffer, connection->req_packets + 1, connection->num_req_paquets * 4);
472 memcpy(ludp->connections[connection_id].req_packets, buffer, ludp->connections[connection_id].num_req_paquets * 4); 529 memcpy(connection->req_packets, buffer, connection->num_req_paquets * 4);
473 return ret; 530 return ret;
474 } 531 }
475 532
476 if (ludp->connections[connection_id].sendbuff_packetnum != ludp->connections[connection_id].sent_packetnum) { 533 if (connection->sendbuff_packetnum != connection->sent_packetnum) {
477 ret = send_data_packet(ludp, connection_id, ludp->connections[connection_id].sent_packetnum); 534 ret = send_data_packet(ludp, connection_id, connection->sent_packetnum);
478 ludp->connections[connection_id].sent_packetnum++; 535 connection->sent_packetnum++;
479 return ret; 536 return ret;
480 } 537 }
481 538
@@ -502,29 +559,31 @@ static int handle_handshake(void *object, IP_Port source, uint8_t *packet, uint3
502 uint32_t temp; 559 uint32_t temp;
503 uint32_t handshake_id1, handshake_id2; 560 uint32_t handshake_id1, handshake_id2;
504 561
505 int connection = getconnection_id(ludp, source); 562 int connection_id = getconnection_id(ludp, source);
563 Connection *connection = &tox_array_get(&ludp->connections, connection_id, Connection);
564
506 memcpy(&temp, packet + 1, 4); 565 memcpy(&temp, packet + 1, 4);
507 handshake_id1 = ntohl(temp); 566 handshake_id1 = ntohl(temp);
508 memcpy(&temp, packet + 5, 4); 567 memcpy(&temp, packet + 5, 4);
509 handshake_id2 = ntohl(temp); 568 handshake_id2 = ntohl(temp);
510 569
511 if (handshake_id2 == 0 && is_connected(ludp, connection) < 3) { 570 if (handshake_id2 == 0 && is_connected(ludp, connection_id) < 3) {
512 send_handshake(ludp, source, handshake_id(ludp, source), handshake_id1); 571 send_handshake(ludp, source, handshake_id(ludp, source), handshake_id1);
513 return 0; 572 return 0;
514 } 573 }
515 574
516 if (is_connected(ludp, connection) != 1) 575 if (is_connected(ludp, connection_id) != 1)
517 return 1; 576 return 1;
518 577
519 /* if handshake_id2 is what we sent previously as handshake_id1 */ 578 /* if handshake_id2 is what we sent previously as handshake_id1 */
520 if (handshake_id2 == ludp->connections[connection].handshake_id1) { 579 if (handshake_id2 == connection->handshake_id1) {
521 ludp->connections[connection].status = 2; 580 connection->status = 2;
522 /* NOTE: is this necessary? 581 /* NOTE: is this necessary?
523 ludp->connections[connection].handshake_id2 = handshake_id1; */ 582 connection->handshake_id2 = handshake_id1; */
524 ludp->connections[connection].orecv_packetnum = handshake_id2; 583 connection->orecv_packetnum = handshake_id2;
525 ludp->connections[connection].osent_packetnum = handshake_id1; 584 connection->osent_packetnum = handshake_id1;
526 ludp->connections[connection].recv_packetnum = handshake_id1; 585 connection->recv_packetnum = handshake_id1;
527 ludp->connections[connection].successful_read = handshake_id1; 586 connection->successful_read = handshake_id1;
528 } 587 }
529 588
530 return 0; 589 return 0;
@@ -547,18 +606,19 @@ static int SYNC_valid(uint32_t length)
547static int handle_SYNC1(Lossless_UDP *ludp, IP_Port source, uint32_t recv_packetnum, uint32_t sent_packetnum) 606static int handle_SYNC1(Lossless_UDP *ludp, IP_Port source, uint32_t recv_packetnum, uint32_t sent_packetnum)
548{ 607{
549 if (handshake_id(ludp, source) == recv_packetnum) { 608 if (handshake_id(ludp, source) == recv_packetnum) {
550 int x = new_inconnection(ludp, source); 609 int connection_id = new_inconnection(ludp, source);
551 610 Connection *connection = &tox_array_get(&ludp->connections, connection_id, Connection);
552 if (x != -1) { 611
553 ludp->connections[x].orecv_packetnum = recv_packetnum; 612 if (connection_id != -1) {
554 ludp->connections[x].sent_packetnum = recv_packetnum; 613 connection->orecv_packetnum = recv_packetnum;
555 ludp->connections[x].sendbuff_packetnum = recv_packetnum; 614 connection->sent_packetnum = recv_packetnum;
556 ludp->connections[x].successful_sent = recv_packetnum; 615 connection->sendbuff_packetnum = recv_packetnum;
557 ludp->connections[x].osent_packetnum = sent_packetnum; 616 connection->successful_sent = recv_packetnum;
558 ludp->connections[x].recv_packetnum = sent_packetnum; 617 connection->osent_packetnum = sent_packetnum;
559 ludp->connections[x].successful_read = sent_packetnum; 618 connection->recv_packetnum = sent_packetnum;
560 619 connection->successful_read = sent_packetnum;
561 return x; 620
621 return connection_id;
562 } 622 }
563 } 623 }
564 624
@@ -569,11 +629,13 @@ static int handle_SYNC1(Lossless_UDP *ludp, IP_Port source, uint32_t recv_packet
569static int handle_SYNC2(Lossless_UDP *ludp, int connection_id, uint8_t counter, uint32_t recv_packetnum, 629static int handle_SYNC2(Lossless_UDP *ludp, int connection_id, uint8_t counter, uint32_t recv_packetnum,
570 uint32_t sent_packetnum) 630 uint32_t sent_packetnum)
571{ 631{
572 if (recv_packetnum == ludp->connections[connection_id].orecv_packetnum) { 632 Connection *connection = &tox_array_get(&ludp->connections, connection_id, Connection);
573 /* && sent_packetnum == ludp->connections[connection_id].osent_packetnum) */ 633
574 ludp->connections[connection_id].status = 3; 634 if (recv_packetnum == connection->orecv_packetnum) {
575 ludp->connections[connection_id].recv_counter = counter; 635 /* && sent_packetnum == connection->osent_packetnum) */
576 ++ludp->connections[connection_id].send_counter; 636 connection->status = 3;
637 connection->recv_counter = counter;
638 ++connection->send_counter;
577 send_SYNC(ludp, connection_id); 639 send_SYNC(ludp, connection_id);
578 return 0; 640 return 0;
579 } 641 }
@@ -583,35 +645,37 @@ static int handle_SYNC2(Lossless_UDP *ludp, int connection_id, uint8_t counter,
583/* case 3 in handle_SYNC: */ 645/* case 3 in handle_SYNC: */
584static int handle_SYNC3(Lossless_UDP *ludp, int connection_id, uint8_t counter, uint32_t recv_packetnum, 646static int handle_SYNC3(Lossless_UDP *ludp, int connection_id, uint8_t counter, uint32_t recv_packetnum,
585 uint32_t sent_packetnum, 647 uint32_t sent_packetnum,
586 uint32_t *req_packets, 648 uint32_t *req_packets,
587 uint16_t number) 649 uint16_t number)
588{ 650{
589 uint8_t comp_counter = (counter - ludp->connections[connection_id].recv_counter ); 651 Connection *connection = &tox_array_get(&ludp->connections, connection_id, Connection);
652
653 uint8_t comp_counter = (counter - connection->recv_counter);
590 uint32_t i, temp; 654 uint32_t i, temp;
591 /* uint32_t comp_1 = (recv_packetnum - ludp->connections[connection_id].successful_sent); 655 /* uint32_t comp_1 = (recv_packetnum - connection->successful_sent);
592 uint32_t comp_2 = (sent_packetnum - ludp->connections[connection_id].successful_read); */ 656 uint32_t comp_2 = (sent_packetnum - connection->successful_read); */
593 uint32_t comp_1 = (recv_packetnum - ludp->connections[connection_id].orecv_packetnum); 657 uint32_t comp_1 = (recv_packetnum - connection->orecv_packetnum);
594 uint32_t comp_2 = (sent_packetnum - ludp->connections[connection_id].osent_packetnum); 658 uint32_t comp_2 = (sent_packetnum - connection->osent_packetnum);
595 659
596 /* packet valid */ 660 /* packet valid */
597 if (comp_1 <= BUFFER_PACKET_NUM && 661 if (comp_1 <= BUFFER_PACKET_NUM &&
598 comp_2 <= BUFFER_PACKET_NUM && 662 comp_2 <= BUFFER_PACKET_NUM &&
599 comp_counter < 10 && comp_counter != 0) { 663 comp_counter < 10 && comp_counter != 0) {
600 664
601 ludp->connections[connection_id].orecv_packetnum = recv_packetnum; 665 connection->orecv_packetnum = recv_packetnum;
602 ludp->connections[connection_id].osent_packetnum = sent_packetnum; 666 connection->osent_packetnum = sent_packetnum;
603 ludp->connections[connection_id].successful_sent = recv_packetnum; 667 connection->successful_sent = recv_packetnum;
604 ludp->connections[connection_id].last_recvSYNC = current_time(); 668 connection->last_recvSYNC = current_time();
605 ludp->connections[connection_id].recv_counter = counter; 669 connection->recv_counter = counter;
606 670
607 ++ludp->connections[connection_id].send_counter; 671 ++connection->send_counter;
608 672
609 for (i = 0; i < number; ++i) { 673 for (i = 0; i < number; ++i) {
610 temp = ntohl(req_packets[i]); 674 temp = ntohl(req_packets[i]);
611 memcpy(ludp->connections[connection_id].req_packets + i, &temp, 4 * number); 675 memcpy(connection->req_packets + i, &temp, 4 * number);
612 } 676 }
613 677
614 ludp->connections[connection_id].num_req_paquets = number; 678 connection->num_req_paquets = number;
615 return 0; 679 return 0;
616 } 680 }
617 681
@@ -625,7 +689,6 @@ static int handle_SYNC(void *object, IP_Port source, uint8_t *packet, uint32_t l
625 if (!SYNC_valid(length)) 689 if (!SYNC_valid(length))
626 return 1; 690 return 1;
627 691
628 int connection = getconnection_id(ludp, source);
629 uint8_t counter; 692 uint8_t counter;
630 uint32_t temp; 693 uint32_t temp;
631 uint32_t recv_packetnum, sent_packetnum; 694 uint32_t recv_packetnum, sent_packetnum;
@@ -641,15 +704,17 @@ static int handle_SYNC(void *object, IP_Port source, uint8_t *packet, uint32_t l
641 if (number != 0) 704 if (number != 0)
642 memcpy(req_packets, packet + 10, 4 * number); 705 memcpy(req_packets, packet + 10, 4 * number);
643 706
644 if (connection == -1) 707 int connection_id = getconnection_id(ludp, source);
708 if (connection_id == -1)
645 return handle_SYNC1(ludp, source, recv_packetnum, sent_packetnum); 709 return handle_SYNC1(ludp, source, recv_packetnum, sent_packetnum);
710 Connection *connection = &tox_array_get(&ludp->connections, connection_id, Connection);
646 711
647 if (ludp->connections[connection].status == 2) 712 if (connection->status == 2)
648 return handle_SYNC2(ludp, connection, counter, 713 return handle_SYNC2(ludp, connection_id, counter,
649 recv_packetnum, sent_packetnum); 714 recv_packetnum, sent_packetnum);
650 715
651 if (ludp->connections[connection].status == 3) 716 if (connection->status == 3)
652 return handle_SYNC3(ludp, connection, counter, recv_packetnum, 717 return handle_SYNC3(ludp, connection_id, counter, recv_packetnum,
653 sent_packetnum, req_packets, number); 718 sent_packetnum, req_packets, number);
654 719
655 return 0; 720 return 0;
@@ -661,31 +726,33 @@ static int handle_SYNC(void *object, IP_Port source, uint8_t *packet, uint32_t l
661 */ 726 */
662static int add_recv(Lossless_UDP *ludp, int connection_id, uint32_t data_num, uint8_t *data, uint16_t size) 727static int add_recv(Lossless_UDP *ludp, int connection_id, uint32_t data_num, uint8_t *data, uint16_t size)
663{ 728{
729 Connection *connection = &tox_array_get(&ludp->connections, connection_id, Connection);
730
664 if (size > MAX_DATA_SIZE) 731 if (size > MAX_DATA_SIZE)
665 return 1; 732 return 1;
666 733
667 uint32_t i; 734 uint32_t i;
668 uint32_t maxnum = ludp->connections[connection_id].successful_read + BUFFER_PACKET_NUM; 735 uint32_t maxnum = connection->successful_read + BUFFER_PACKET_NUM;
669 uint32_t sent_packet = data_num - ludp->connections[connection_id].osent_packetnum; 736 uint32_t sent_packet = data_num - connection->osent_packetnum;
670 737
671 for (i = ludp->connections[connection_id].recv_packetnum; i != maxnum; ++i) { 738 for (i = connection->recv_packetnum; i != maxnum; ++i) {
672 if (i == data_num) { 739 if (i == data_num) {
673 memcpy(ludp->connections[connection_id].recvbuffer[i % MAX_QUEUE_NUM].data, data, size); 740 memcpy(connection->recvbuffer[i % MAX_QUEUE_NUM].data, data, size);
674 741
675 ludp->connections[connection_id].recvbuffer[i % MAX_QUEUE_NUM].size = size; 742 connection->recvbuffer[i % MAX_QUEUE_NUM].size = size;
676 ludp->connections[connection_id].last_recvdata = current_time(); 743 connection->last_recvdata = current_time();
677 744
678 if (sent_packet < BUFFER_PACKET_NUM) { 745 if (sent_packet < BUFFER_PACKET_NUM) {
679 ludp->connections[connection_id].osent_packetnum = data_num; 746 connection->osent_packetnum = data_num;
680 } 747 }
681 748
682 break; 749 break;
683 } 750 }
684 } 751 }
685 752
686 for (i = ludp->connections[connection_id].recv_packetnum; i != maxnum; ++i) { 753 for (i = connection->recv_packetnum; i != maxnum; ++i) {
687 if (ludp->connections[connection_id].recvbuffer[i % MAX_QUEUE_NUM].size != 0) 754 if (connection->recvbuffer[i % MAX_QUEUE_NUM].size != 0)
688 ludp->connections[connection_id].recv_packetnum = i; 755 connection->recv_packetnum = i;
689 else 756 else
690 break; 757 break;
691 } 758 }
@@ -696,13 +763,13 @@ static int add_recv(Lossless_UDP *ludp, int connection_id, uint32_t data_num, ui
696static int handle_data(void *object, IP_Port source, uint8_t *packet, uint32_t length) 763static int handle_data(void *object, IP_Port source, uint8_t *packet, uint32_t length)
697{ 764{
698 Lossless_UDP *ludp = object; 765 Lossless_UDP *ludp = object;
699 int connection = getconnection_id(ludp, source); 766 int connection_id = getconnection_id(ludp, source);
700 767 if (connection_id == -1)
701 if (connection == -1)
702 return 1; 768 return 1;
769 Connection *connection = &tox_array_get(&ludp->connections, connection_id, Connection);
703 770
704 /* Drop the data packet if connection is not connected. */ 771 /* Drop the data packet if connection is not connected. */
705 if (ludp->connections[connection].status != 3) 772 if (connection->status != 3)
706 return 1; 773 return 1;
707 774
708 if (length > 1 + 4 + MAX_DATA_SIZE || length < 1 + 4 + 1) 775 if (length > 1 + 4 + MAX_DATA_SIZE || length < 1 + 4 + 1)
@@ -715,7 +782,7 @@ static int handle_data(void *object, IP_Port source, uint8_t *packet, uint32_t l
715 memcpy(&temp, packet + 1, 4); 782 memcpy(&temp, packet + 1, 4);
716 number = ntohl(temp); 783 number = ntohl(temp);
717 784
718 return add_recv(ludp, connection, number, packet + 5, size); 785 return add_recv(ludp, connection_id, number, packet + 5, size);
719} 786}
720 787
721/* 788/*
@@ -748,22 +815,20 @@ static void do_new(Lossless_UDP *ludp)
748 uint32_t i; 815 uint32_t i;
749 uint64_t temp_time = current_time(); 816 uint64_t temp_time = current_time();
750 817
751 for (i = 0; i < ludp->connections_length; ++i) { 818 tox_array_for_each(&ludp->connections, Connection, tmp) {
752 if (ludp->connections[i].status == 1) 819 if (tmp.status == 1)
753 if ((ludp->connections[i].last_sent + (1000000UL / ludp->connections[i].SYNC_rate)) <= temp_time) { 820 if ((tmp.last_sent + (1000000UL / tmp.SYNC_rate)) <= temp_time) {
754 send_handshake(ludp, ludp->connections[i].ip_port, ludp->connections[i].handshake_id1, 0); 821 send_handshake(ludp, tmp.ip_port, tmp.handshake_id1, 0);
755 ludp->connections[i].last_sent = temp_time; 822 tmp.last_sent = temp_time;
756 } 823 }
757 824
758 /* kill all timed out connections */ 825 /* kill all timed out connections */
759 if (ludp->connections[i].status > 0 && 826 if (tmp.status > 0 && (tmp.last_recvSYNC + tmp.timeout * 1000000UL) < temp_time && tmp.status != 4) {
760 (ludp->connections[i].last_recvSYNC + ludp->connections[i].timeout * 1000000UL) < temp_time && 827 tmp.status = 4;
761 ludp->connections[i].status != 4) {
762 ludp->connections[i].status = 4;
763 /* kill_connection(i); */ 828 /* kill_connection(i); */
764 } 829 }
765 830
766 if (ludp->connections[i].status > 0 && ludp->connections[i].killat < temp_time) 831 if (tmp.status > 0 && tmp.killat < temp_time)
767 kill_connection(ludp, i); 832 kill_connection(ludp, i);
768 } 833 }
769} 834}
@@ -773,11 +838,11 @@ static void do_SYNC(Lossless_UDP *ludp)
773 uint32_t i; 838 uint32_t i;
774 uint64_t temp_time = current_time(); 839 uint64_t temp_time = current_time();
775 840
776 for (i = 0; i < ludp->connections_length; ++i) { 841 tox_array_for_each(&ludp->connections, Connection, tmp) {
777 if (ludp->connections[i].status == 2 || ludp->connections[i].status == 3) 842 if (tmp.status == 2 || tmp.status == 3)
778 if ((ludp->connections[i].last_SYNC + (1000000UL / ludp->connections[i].SYNC_rate)) <= temp_time) { 843 if ((tmp.last_SYNC + (1000000UL / tmp.SYNC_rate)) <= temp_time) {
779 send_SYNC(ludp, i); 844 send_SYNC(ludp, i);
780 ludp->connections[i].last_SYNC = temp_time; 845 tmp.last_SYNC = temp_time;
781 } 846 }
782 } 847 }
783} 848}
@@ -788,14 +853,17 @@ static void do_data(Lossless_UDP *ludp)
788 uint64_t j; 853 uint64_t j;
789 uint64_t temp_time = current_time(); 854 uint64_t temp_time = current_time();
790 855
791 for (i = 0; i < ludp->connections_length; ++i) 856 tox_array_for_each(&ludp->connections, Connection, tmp) {
792 if (ludp->connections[i].status == 3 && sendqueue(ludp, i) != 0) 857 if (tmp.status == 3 && sendqueue(ludp, i) != 0 &&
793 if ((ludp->connections[i].last_sent + (1000000UL / ludp->connections[i].data_rate)) <= temp_time) { 858 (tmp.last_sent + (1000000UL / tmp.data_rate)) <= temp_time)
794 for (j = ludp->connections[i].last_sent; j < temp_time; j += (1000000UL / ludp->connections[i].data_rate)) 859 {
795 send_DATA(ludp, i); 860 for (j = tmp.last_sent; j < temp_time; j += (1000000UL / tmp.data_rate))
861 send_DATA(ludp, i);
796 862
797 ludp->connections[i].last_sent = temp_time; 863 tmp.last_sent = temp_time;
798 } 864
865 }
866 }
799} 867}
800 868
801#define MAX_SYNC_RATE 10 869#define MAX_SYNC_RATE 10
@@ -810,18 +878,18 @@ static void adjust_rates(Lossless_UDP *ludp)
810 uint32_t i; 878 uint32_t i;
811 uint64_t temp_time = current_time(); 879 uint64_t temp_time = current_time();
812 880
813 for (i = 0; i < ludp->connections_length; ++i) { 881 tox_array_for_each(&ludp->connections, Connection, tmp) {
814 if (ludp->connections[i].status == 1 || ludp->connections[i].status == 2) 882 if (tmp.status == 1 || tmp.status == 2)
815 ludp->connections[i].SYNC_rate = MAX_SYNC_RATE; 883 tmp.SYNC_rate = MAX_SYNC_RATE;
816 884
817 if (ludp->connections[i].status == 3) { 885 if (tmp.status == 3) {
818 if (sendqueue(ludp, i) != 0) { 886 if (sendqueue(ludp, i) != 0) {
819 ludp->connections[i].data_rate = (BUFFER_PACKET_NUM - ludp->connections[i].num_req_paquets) * MAX_SYNC_RATE; 887 tmp.data_rate = (BUFFER_PACKET_NUM - tmp.num_req_paquets) * MAX_SYNC_RATE;
820 ludp->connections[i].SYNC_rate = MAX_SYNC_RATE; 888 tmp.SYNC_rate = MAX_SYNC_RATE;
821 } else if (ludp->connections[i].last_recvdata + 1000000UL > temp_time) 889 } else if (tmp.last_recvdata + 1000000UL > temp_time)
822 ludp->connections[i].SYNC_rate = MAX_SYNC_RATE; 890 tmp.SYNC_rate = MAX_SYNC_RATE;
823 else 891 else
824 ludp->connections[i].SYNC_rate = SYNC_RATE; 892 tmp.SYNC_rate = SYNC_RATE;
825 } 893 }
826 } 894 }
827} 895}
@@ -837,6 +905,6 @@ void do_lossless_udp(Lossless_UDP *ludp)
837 905
838void kill_lossless_udp(Lossless_UDP *ludp) 906void kill_lossless_udp(Lossless_UDP *ludp)
839{ 907{
840 free(ludp->connections); 908 tox_array_delete(&ludp->connections);
841 free(ludp); 909 free(ludp);
842} 910}
diff --git a/toxcore/Lossless_UDP.h b/toxcore/Lossless_UDP.h
index 176e86ce..bd43cb67 100644
--- a/toxcore/Lossless_UDP.h
+++ b/toxcore/Lossless_UDP.h
@@ -25,6 +25,7 @@
25#define LOSSLESS_UDP_H 25#define LOSSLESS_UDP_H
26 26
27#include "network.h" 27#include "network.h"
28#include "../testing/misc_tools.h"
28 29
29#ifdef __cplusplus 30#ifdef __cplusplus
30extern "C" { 31extern "C" {
@@ -119,10 +120,12 @@ typedef struct {
119 120
120typedef struct { 121typedef struct {
121 Networking_Core *net; 122 Networking_Core *net;
122 Connection *connections;
123 123
124 uint32_t connections_length; /* Length of connections array */ 124 tox_array connections;
125 uint32_t connections_number; /* Number of connections in connections array */ 125 //kk Connection *connections;
126
127 //kk uint32_t connections_length; /* Length of connections array */
128 //kk uint32_t connections_number; /* Number of connections in connections array */
126 129
127 /* table of random numbers used in handshake_id. */ 130 /* table of random numbers used in handshake_id. */
128 uint32_t randtable[6][256]; 131 uint32_t randtable[6][256];