summaryrefslogtreecommitdiff
path: root/toxcore
diff options
context:
space:
mode:
Diffstat (limited to 'toxcore')
-rw-r--r--toxcore/Lossless_UDP.c38
-rw-r--r--toxcore/Lossless_UDP.h12
-rw-r--r--toxcore/net_crypto.c4
3 files changed, 51 insertions, 3 deletions
diff --git a/toxcore/Lossless_UDP.c b/toxcore/Lossless_UDP.c
index 6b2c83a5..fe22f869 100644
--- a/toxcore/Lossless_UDP.c
+++ b/toxcore/Lossless_UDP.c
@@ -185,8 +185,10 @@ int new_connection(Lossless_UDP *ludp, IP_Port ip_port)
185{ 185{
186 int connection_id = getconnection_id(ludp, ip_port); 186 int connection_id = getconnection_id(ludp, ip_port);
187 187
188 if (connection_id != -1) 188 if (connection_id != -1) {
189 confirm_connection(ludp, connection_id);
189 return connection_id; 190 return connection_id;
191 }
190 192
191 tox_array_for_each(&ludp->connections, Connection, tmp) { 193 tox_array_for_each(&ludp->connections, Connection, tmp) {
192 if (tmp->status == 0) { 194 if (tmp->status == 0) {
@@ -486,7 +488,41 @@ int read_packet(Lossless_UDP *ludp, int connection_id, uint8_t *data)
486 ++connection->successful_read; 488 ++connection->successful_read;
487 connection->recvbuffer[index].size = 0; 489 connection->recvbuffer[index].size = 0;
488 return size; 490 return size;
491}
492
493/* Like read_packet() but does leaves the queue as is.
494 * return 0 if there is no received data in the buffer.
495 * return length of received packet if successful.
496 */
497int read_packet_silent(Lossless_UDP *ludp, int connection_id, uint8_t *data)
498{
499 if (recvqueue(ludp, connection_id) == 0)
500 return 0;
501
502 Connection *connection = &tox_array_get(&ludp->connections, connection_id, Connection);
503
504 if (connection->status == 0)
505 return 0;
506
507 uint16_t index = connection->successful_read % connection->recvbuffer_length;
508 uint16_t size = connection->recvbuffer[index].size;
509 memcpy(data, connection->recvbuffer[index].data, size);
510 return size;
511}
512/* Discard the next packet to be read from the queue
513 * return 0 if success.
514 * return -1 if failure.
515 */
516int discard_packet(Lossless_UDP *ludp, int connection_id)
517{
518 if (recvqueue(ludp, connection_id) == 0)
519 return -1;
489 520
521 Connection *connection = &tox_array_get(&ludp->connections, connection_id, Connection);
522 uint16_t index = connection->successful_read % connection->recvbuffer_length;
523 ++connection->successful_read;
524 connection->recvbuffer[index].size = 0;
525 return 0;
490} 526}
491 527
492/* return 0 if data could not be put in packet queue. 528/* return 0 if data could not be put in packet queue.
diff --git a/toxcore/Lossless_UDP.h b/toxcore/Lossless_UDP.h
index aa6e344f..9b5c2406 100644
--- a/toxcore/Lossless_UDP.h
+++ b/toxcore/Lossless_UDP.h
@@ -196,6 +196,18 @@ char id_packet(Lossless_UDP *ludp, int connection_id);
196 */ 196 */
197int read_packet(Lossless_UDP *ludp, int connection_id, uint8_t *data); 197int read_packet(Lossless_UDP *ludp, int connection_id, uint8_t *data);
198 198
199/* Like read_packet() but does leaves the queue as is.
200 * return 0 if there is no received data in the buffer.
201 * return length of received packet if successful.
202 */
203int read_packet_silent(Lossless_UDP *ludp, int connection_id, uint8_t *data);
204
205/* Discard the next packet to be read from the queue
206 * return 0 if success.
207 * return -1 if failure.
208 */
209int discard_packet(Lossless_UDP *ludp, int connection_id);
210
199/* return 0 if data could not be put in packet queue. 211/* return 0 if data could not be put in packet queue.
200 * return 1 if data was put into the queue. 212 * return 1 if data was put into the queue.
201 */ 213 */
diff --git a/toxcore/net_crypto.c b/toxcore/net_crypto.c
index ca23957d..41c8c45c 100644
--- a/toxcore/net_crypto.c
+++ b/toxcore/net_crypto.c
@@ -516,7 +516,7 @@ int crypto_inbound(Net_Crypto *c, uint8_t *public_key, uint8_t *secret_nonce, ui
516 516
517 if (id_packet(c->lossless_udp, c->incoming_connections[i]) == 2) { 517 if (id_packet(c->lossless_udp, c->incoming_connections[i]) == 2) {
518 uint8_t temp_data[MAX_DATA_SIZE]; 518 uint8_t temp_data[MAX_DATA_SIZE];
519 uint16_t len = read_packet(c->lossless_udp, c->incoming_connections[i], temp_data); 519 uint16_t len = read_packet_silent(c->lossless_udp, c->incoming_connections[i], temp_data);
520 520
521 if (handle_cryptohandshake(c, public_key, secret_nonce, session_key, temp_data, len)) { 521 if (handle_cryptohandshake(c, public_key, secret_nonce, session_key, temp_data, len)) {
522 int connection_id = c->incoming_connections[i]; 522 int connection_id = c->incoming_connections[i];
@@ -570,7 +570,7 @@ int accept_crypto_inbound(Net_Crypto *c, int connection_id, uint8_t *public_key,
570{ 570{
571 uint32_t i; 571 uint32_t i;
572 572
573 if (connection_id == -1) 573 if (discard_packet(c->lossless_udp, connection_id) == -1)
574 return -1; 574 return -1;
575 575
576 /* 576 /*