diff options
author | irungentoo <irungentoo@gmail.com> | 2013-07-26 05:38:35 -0700 |
---|---|---|
committer | irungentoo <irungentoo@gmail.com> | 2013-07-26 05:38:35 -0700 |
commit | 8c70b7c11d6d5acc08e234554f187b18876fd5d3 (patch) | |
tree | 8ef350879553acd22c20887016f4249815ab1d0f /core/Lossless_UDP.c | |
parent | 63375b68b63d33c8437f313b2041b559c32c945b (diff) | |
parent | 1577a1eb61b6e2839aec962025d78e8703be3661 (diff) |
Merge pull request #103 from SilentSand/master
Licensing
Diffstat (limited to 'core/Lossless_UDP.c')
-rw-r--r-- | core/Lossless_UDP.c | 63 |
1 files changed, 26 insertions, 37 deletions
diff --git a/core/Lossless_UDP.c b/core/Lossless_UDP.c index c4c464b6..83bb1785 100644 --- a/core/Lossless_UDP.c +++ b/core/Lossless_UDP.c | |||
@@ -1,32 +1,30 @@ | |||
1 | /* Lossless_UDP.c | 1 | /* Lossless_UDP.c |
2 | * | 2 | * |
3 | * An implementation of the Lossless_UDP protocol as seen in docs/Lossless_UDP.txt | 3 | * An implementation of the Lossless_UDP protocol as seen in docs/Lossless_UDP.txt |
4 | * | 4 | * |
5 | 5 | * Copyright (C) 2013 Tox project All Rights Reserved. | |
6 | Copyright (C) 2013 Tox project All Rights Reserved. | 6 | * |
7 | 7 | * This file is part of Tox. | |
8 | This file is part of Tox. | 8 | * |
9 | 9 | * Tox is free software: you can redistribute it and/or modify | |
10 | Tox is free software: you can redistribute it and/or modify | 10 | * it under the terms of the GNU General Public License as published by |
11 | it under the terms of the GNU General Public License as published by | 11 | * the Free Software Foundation, either version 3 of the License, or |
12 | the Free Software Foundation, either version 3 of the License, or | 12 | * (at your option) any later version. |
13 | (at your option) any later version. | 13 | * |
14 | 14 | * Tox is distributed in the hope that it will be useful, | |
15 | Tox is distributed in the hope that it will be useful, | 15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
16 | but WITHOUT ANY WARRANTY; without even the implied warranty of | 16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
17 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | 17 | * GNU General Public License for more details. |
18 | GNU General Public License for more details. | 18 | * |
19 | 19 | * You should have received a copy of the GNU General Public License | |
20 | You should have received a copy of the GNU General Public License | 20 | * along with Tox. If not, see <http://www.gnu.org/licenses/>. |
21 | along with Tox. If not, see <http://www.gnu.org/licenses/>. | 21 | * |
22 | 22 | */ | |
23 | */ | 23 | |
24 | /* TODO: clean this file a bit. | 24 | /* TODO: clean this file a bit. |
25 | There are a couple of useless variables to get rid of. */ | 25 | There are a couple of useless variables to get rid of. */ |
26 | #include "Lossless_UDP.h" | 26 | #include "Lossless_UDP.h" |
27 | 27 | ||
28 | |||
29 | |||
30 | /* maximum data packets in sent and receive queues. */ | 28 | /* maximum data packets in sent and receive queues. */ |
31 | #define MAX_QUEUE_NUM 16 | 29 | #define MAX_QUEUE_NUM 16 |
32 | 30 | ||
@@ -88,7 +86,6 @@ typedef struct | |||
88 | uint8_t timeout; /* connection timeout in seconds. */ | 86 | uint8_t timeout; /* connection timeout in seconds. */ |
89 | }Connection; | 87 | }Connection; |
90 | 88 | ||
91 | |||
92 | #define MAX_CONNECTIONS 256 | 89 | #define MAX_CONNECTIONS 256 |
93 | 90 | ||
94 | static Connection connections[MAX_CONNECTIONS]; | 91 | static Connection connections[MAX_CONNECTIONS]; |
@@ -117,7 +114,6 @@ int getconnection_id(IP_Port ip_port) | |||
117 | /* table of random numbers used below. */ | 114 | /* table of random numbers used below. */ |
118 | static uint32_t randtable[6][256]; | 115 | static uint32_t randtable[6][256]; |
119 | 116 | ||
120 | |||
121 | /* generate a handshake_id which depends on the ip_port. | 117 | /* generate a handshake_id which depends on the ip_port. |
122 | this function will always give one unique handshake_id per ip_port. | 118 | this function will always give one unique handshake_id per ip_port. |
123 | TODO: make this better */ | 119 | TODO: make this better */ |
@@ -138,6 +134,7 @@ uint32_t handshake_id(IP_Port source) | |||
138 | } | 134 | } |
139 | return id; | 135 | return id; |
140 | } | 136 | } |
137 | |||
141 | /* change the hnshake id associated with that ip_port | 138 | /* change the hnshake id associated with that ip_port |
142 | TODO: make this better */ | 139 | TODO: make this better */ |
143 | void change_handshake(IP_Port source) | 140 | void change_handshake(IP_Port source) |
@@ -146,7 +143,6 @@ void change_handshake(IP_Port source) | |||
146 | randtable[rand][((uint8_t *)&source)[rand]] = random_int(); | 143 | randtable[rand][((uint8_t *)&source)[rand]] = random_int(); |
147 | } | 144 | } |
148 | 145 | ||
149 | |||
150 | /* initialize a new connection to ip_port | 146 | /* initialize a new connection to ip_port |
151 | returns an integer corresponding to the connection id. | 147 | returns an integer corresponding to the connection id. |
152 | return -1 if it could not initialize the connection. | 148 | return -1 if it could not initialize the connection. |
@@ -314,6 +310,7 @@ char id_packet(int connection_id) | |||
314 | } | 310 | } |
315 | return -1; | 311 | return -1; |
316 | } | 312 | } |
313 | |||
317 | /* return 0 if there is no received data in the buffer. | 314 | /* return 0 if there is no received data in the buffer. |
318 | return length of received packet if successful */ | 315 | return length of received packet if successful */ |
319 | int read_packet(int connection_id, uint8_t * data) | 316 | int read_packet(int connection_id, uint8_t * data) |
@@ -353,9 +350,6 @@ int write_packet(int connection_id, uint8_t * data, uint32_t length) | |||
353 | return 0; | 350 | return 0; |
354 | } | 351 | } |
355 | 352 | ||
356 | |||
357 | |||
358 | |||
359 | /* put the packet numbers the we are missing in requested and return the number */ | 353 | /* put the packet numbers the we are missing in requested and return the number */ |
360 | uint32_t missing_packets(int connection_id, uint32_t * requested) | 354 | uint32_t missing_packets(int connection_id, uint32_t * requested) |
361 | { | 355 | { |
@@ -386,8 +380,6 @@ uint32_t missing_packets(int connection_id, uint32_t * requested) | |||
386 | /* Packet sending functions | 380 | /* Packet sending functions |
387 | One per packet type. | 381 | One per packet type. |
388 | see docs/Lossless_UDP.txt for more information. */ | 382 | see docs/Lossless_UDP.txt for more information. */ |
389 | |||
390 | |||
391 | int send_handshake(IP_Port ip_port, uint32_t handshake_id1, uint32_t handshake_id2) | 383 | int send_handshake(IP_Port ip_port, uint32_t handshake_id1, uint32_t handshake_id2) |
392 | { | 384 | { |
393 | uint8_t packet[1 + 4 + 4]; | 385 | uint8_t packet[1 + 4 + 4]; |
@@ -402,7 +394,6 @@ int send_handshake(IP_Port ip_port, uint32_t handshake_id1, uint32_t handshake_i | |||
402 | 394 | ||
403 | } | 395 | } |
404 | 396 | ||
405 | |||
406 | int send_SYNC(uint32_t connection_id) | 397 | int send_SYNC(uint32_t connection_id) |
407 | { | 398 | { |
408 | 399 | ||
@@ -468,8 +459,6 @@ int send_DATA(uint32_t connection_id) | |||
468 | 459 | ||
469 | /* END of packet sending functions */ | 460 | /* END of packet sending functions */ |
470 | 461 | ||
471 | |||
472 | |||
473 | /* Packet handling functions | 462 | /* Packet handling functions |
474 | One to handle each type of packets we receive | 463 | One to handle each type of packets we receive |
475 | return 0 if handled correctly, 1 if packet is bad. */ | 464 | return 0 if handled correctly, 1 if packet is bad. */ |
@@ -700,7 +689,6 @@ int handle_data(uint8_t * packet, uint32_t length, IP_Port source) | |||
700 | 689 | ||
701 | /* END of packet handling functions */ | 690 | /* END of packet handling functions */ |
702 | 691 | ||
703 | |||
704 | int LosslessUDP_handlepacket(uint8_t * packet, uint32_t length, IP_Port source) | 692 | int LosslessUDP_handlepacket(uint8_t * packet, uint32_t length, IP_Port source) |
705 | { | 693 | { |
706 | 694 | ||
@@ -828,6 +816,7 @@ void adjustRates() | |||
828 | } | 816 | } |
829 | } | 817 | } |
830 | } | 818 | } |
819 | |||
831 | /* Call this function a couple times per second | 820 | /* Call this function a couple times per second |
832 | It's the main loop. */ | 821 | It's the main loop. */ |
833 | void doLossless_UDP() | 822 | void doLossless_UDP() |