ffi-tox 0.1.1 → 0.1.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/ProjectTox-Core/AUTHORS +0 -0
- data/ProjectTox-Core/ChangeLog +0 -0
- data/ProjectTox-Core/INSTALL +370 -0
- data/ProjectTox-Core/INSTALL.md +455 -56
- data/ProjectTox-Core/Makefile.am +35 -0
- data/ProjectTox-Core/NEWS +0 -0
- data/ProjectTox-Core/README +43 -0
- data/ProjectTox-Core/README.md +34 -44
- data/ProjectTox-Core/auto_tests/Makefile.inc +110 -0
- data/ProjectTox-Core/auto_tests/TCP_test.c +519 -0
- data/ProjectTox-Core/auto_tests/assoc_test.c +160 -0
- data/ProjectTox-Core/auto_tests/crypto_test.c +302 -0
- data/ProjectTox-Core/auto_tests/dht_test.c +362 -0
- data/ProjectTox-Core/auto_tests/encryptsave_test.c +104 -0
- data/ProjectTox-Core/auto_tests/friends_test.c +238 -0
- data/ProjectTox-Core/auto_tests/helpers.h +15 -0
- data/ProjectTox-Core/auto_tests/messenger_test.c +365 -0
- data/ProjectTox-Core/auto_tests/network_test.c +171 -0
- data/ProjectTox-Core/auto_tests/onion_test.c +363 -0
- data/ProjectTox-Core/auto_tests/skeleton_test.c +49 -0
- data/ProjectTox-Core/auto_tests/tox_test.c +454 -0
- data/ProjectTox-Core/auto_tests/toxav_basic_test.c +597 -0
- data/ProjectTox-Core/auto_tests/toxav_many_test.c +402 -0
- data/ProjectTox-Core/autogen.sh +6 -0
- data/ProjectTox-Core/build/Makefile.am +14 -0
- data/ProjectTox-Core/configure.ac +694 -0
- data/ProjectTox-Core/dist-build/android-arm.sh +3 -0
- data/ProjectTox-Core/dist-build/android-armv7.sh +3 -0
- data/ProjectTox-Core/dist-build/android-build.sh +59 -0
- data/ProjectTox-Core/dist-build/android-mips.sh +3 -0
- data/ProjectTox-Core/dist-build/android-x86.sh +3 -0
- data/ProjectTox-Core/docs/Group-Chats.md +71 -0
- data/ProjectTox-Core/docs/Hardening.txt +60 -0
- data/ProjectTox-Core/docs/Hardening_docs.txt +30 -0
- data/ProjectTox-Core/docs/Prevent_Tracking.txt +160 -0
- data/ProjectTox-Core/docs/TCP_Network.txt +154 -0
- data/ProjectTox-Core/docs/TODO +62 -0
- data/ProjectTox-Core/docs/Tox_middle_level_network_protocol.txt +120 -0
- data/ProjectTox-Core/docs/av_api.md +194 -0
- data/ProjectTox-Core/libtoxav.pc.in +11 -0
- data/ProjectTox-Core/libtoxcore.pc.in +11 -0
- data/ProjectTox-Core/m4/ax_have_epoll.m4 +104 -0
- data/ProjectTox-Core/m4/ax_pthread.m4 +317 -0
- data/ProjectTox-Core/m4/pkg.m4 +199 -0
- data/ProjectTox-Core/other/DHT_bootstrap.c +121 -58
- data/ProjectTox-Core/other/DHTnodes +3 -0
- data/ProjectTox-Core/other/Makefile.inc +20 -0
- data/ProjectTox-Core/other/bootstrap_node_packets.c +65 -0
- data/ProjectTox-Core/other/tox.png +0 -0
- data/ProjectTox-Core/testing/DHT_test.c +170 -98
- data/ProjectTox-Core/testing/Makefile.inc +112 -0
- data/ProjectTox-Core/testing/Messenger_test.c +133 -69
- data/ProjectTox-Core/testing/dns3_test.c +115 -0
- data/ProjectTox-Core/testing/misc_tools.c +59 -13
- data/ProjectTox-Core/testing/nTox.c +1127 -264
- data/ProjectTox-Core/testing/nTox.h +10 -19
- data/ProjectTox-Core/testing/tox_shell.c +159 -0
- data/ProjectTox-Core/testing/tox_sync.c +299 -0
- data/ProjectTox-Core/tools/README +11 -0
- data/ProjectTox-Core/tools/astylerc +11 -0
- data/ProjectTox-Core/tools/pre-commit +17 -0
- data/ProjectTox-Core/toxav/Makefile.inc +36 -0
- data/ProjectTox-Core/toxav/codec.c +357 -0
- data/ProjectTox-Core/toxav/codec.h +116 -0
- data/ProjectTox-Core/toxav/msi.c +1949 -0
- data/ProjectTox-Core/toxav/msi.h +267 -0
- data/ProjectTox-Core/toxav/rtp.c +600 -0
- data/ProjectTox-Core/toxav/rtp.h +196 -0
- data/ProjectTox-Core/toxav/toxav.c +1148 -0
- data/ProjectTox-Core/toxav/toxav.h +389 -0
- data/ProjectTox-Core/toxcore/DHT.c +2521 -0
- data/ProjectTox-Core/toxcore/DHT.h +412 -0
- data/ProjectTox-Core/toxcore/LAN_discovery.c +322 -0
- data/ProjectTox-Core/{core → toxcore}/LAN_discovery.h +17 -12
- data/ProjectTox-Core/toxcore/Makefile.inc +67 -0
- data/ProjectTox-Core/toxcore/Messenger.c +3006 -0
- data/ProjectTox-Core/toxcore/Messenger.h +818 -0
- data/ProjectTox-Core/toxcore/TCP_client.c +858 -0
- data/ProjectTox-Core/toxcore/TCP_client.h +156 -0
- data/ProjectTox-Core/toxcore/TCP_server.c +1332 -0
- data/ProjectTox-Core/toxcore/TCP_server.h +181 -0
- data/ProjectTox-Core/toxcore/assoc.c +1033 -0
- data/ProjectTox-Core/toxcore/assoc.h +104 -0
- data/ProjectTox-Core/toxcore/crypto_core.c +278 -0
- data/ProjectTox-Core/toxcore/crypto_core.h +151 -0
- data/ProjectTox-Core/toxcore/friend_requests.c +175 -0
- data/ProjectTox-Core/toxcore/friend_requests.h +83 -0
- data/ProjectTox-Core/toxcore/group_chats.c +837 -0
- data/ProjectTox-Core/toxcore/group_chats.h +199 -0
- data/ProjectTox-Core/toxcore/list.c +256 -0
- data/ProjectTox-Core/toxcore/list.h +85 -0
- data/ProjectTox-Core/toxcore/logger.c +153 -0
- data/ProjectTox-Core/toxcore/logger.h +84 -0
- data/ProjectTox-Core/toxcore/misc_tools.h +70 -0
- data/ProjectTox-Core/toxcore/net_crypto.c +2753 -0
- data/ProjectTox-Core/toxcore/net_crypto.h +410 -0
- data/ProjectTox-Core/toxcore/network.c +979 -0
- data/ProjectTox-Core/toxcore/network.h +367 -0
- data/ProjectTox-Core/toxcore/onion.c +540 -0
- data/ProjectTox-Core/toxcore/onion.h +150 -0
- data/ProjectTox-Core/toxcore/onion_announce.c +433 -0
- data/ProjectTox-Core/toxcore/onion_announce.h +139 -0
- data/ProjectTox-Core/toxcore/onion_client.c +1347 -0
- data/ProjectTox-Core/toxcore/onion_client.h +253 -0
- data/ProjectTox-Core/toxcore/ping.c +346 -0
- data/ProjectTox-Core/toxcore/ping.h +47 -0
- data/ProjectTox-Core/toxcore/ping_array.c +162 -0
- data/ProjectTox-Core/toxcore/ping_array.h +75 -0
- data/ProjectTox-Core/toxcore/tox.c +940 -0
- data/ProjectTox-Core/toxcore/tox.h +734 -0
- data/ProjectTox-Core/toxcore/util.c +193 -0
- data/ProjectTox-Core/toxcore/util.h +63 -0
- data/ProjectTox-Core/toxdns/Makefile.inc +29 -0
- data/ProjectTox-Core/toxdns/toxdns.c +238 -0
- data/ProjectTox-Core/toxdns/toxdns.h +88 -0
- data/ProjectTox-Core/toxencryptsave/Makefile.inc +45 -0
- data/ProjectTox-Core/toxencryptsave/toxencryptsave.c +179 -0
- data/ProjectTox-Core/toxencryptsave/toxencryptsave.h +74 -0
- data/interfaces/libtox.i +2 -6
- data/lib/ffi-tox/libtox.rb +406 -28
- metadata +124 -46
- data/ProjectTox-Core/CMakeLists.txt +0 -50
- data/ProjectTox-Core/cmake/FindLIBCONFIG.cmake +0 -15
- data/ProjectTox-Core/cmake/FindNaCl.cmake +0 -17
- data/ProjectTox-Core/cmake/FindSODIUM.cmake +0 -15
- data/ProjectTox-Core/core/CMakeLists.txt +0 -19
- data/ProjectTox-Core/core/DHT.c +0 -1104
- data/ProjectTox-Core/core/DHT.h +0 -111
- data/ProjectTox-Core/core/LAN_discovery.c +0 -79
- data/ProjectTox-Core/core/Lossless_UDP.c +0 -755
- data/ProjectTox-Core/core/Lossless_UDP.h +0 -106
- data/ProjectTox-Core/core/Messenger.c +0 -596
- data/ProjectTox-Core/core/Messenger.h +0 -165
- data/ProjectTox-Core/core/friend_requests.c +0 -131
- data/ProjectTox-Core/core/friend_requests.h +0 -51
- data/ProjectTox-Core/core/net_crypto.c +0 -575
- data/ProjectTox-Core/core/net_crypto.h +0 -134
- data/ProjectTox-Core/core/network.c +0 -205
- data/ProjectTox-Core/core/network.h +0 -134
- data/ProjectTox-Core/docs/commands.md +0 -25
- data/ProjectTox-Core/docs/start_guide.de.md +0 -40
- data/ProjectTox-Core/docs/start_guide.md +0 -38
- data/ProjectTox-Core/other/CMakeLists.txt +0 -9
- data/ProjectTox-Core/testing/CMakeLists.txt +0 -18
- data/ProjectTox-Core/testing/DHT_cryptosendfiletest.c +0 -228
- data/ProjectTox-Core/testing/DHT_sendfiletest.c +0 -176
- data/ProjectTox-Core/testing/Lossless_UDP_testclient.c +0 -214
- data/ProjectTox-Core/testing/Lossless_UDP_testserver.c +0 -201
- data/ProjectTox-Core/testing/misc_tools.h +0 -29
- data/ProjectTox-Core/testing/nTox_win32.c +0 -387
- data/ProjectTox-Core/testing/nTox_win32.h +0 -40
- data/ProjectTox-Core/testing/rect.py +0 -45
data/ProjectTox-Core/core/DHT.h
DELETED
@@ -1,111 +0,0 @@
|
|
1
|
-
/* DHT.h
|
2
|
-
*
|
3
|
-
* An implementation of the DHT as seen in docs/DHT.txt
|
4
|
-
*
|
5
|
-
* Copyright (C) 2013 Tox project All Rights Reserved.
|
6
|
-
*
|
7
|
-
* This file is part of Tox.
|
8
|
-
*
|
9
|
-
* 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
|
-
* the Free Software Foundation, either version 3 of the License, or
|
12
|
-
* (at your option) any later version.
|
13
|
-
*
|
14
|
-
* Tox is distributed in the hope that it will be useful,
|
15
|
-
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
16
|
-
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
17
|
-
* GNU General Public License for more details.
|
18
|
-
*
|
19
|
-
* 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
|
-
*
|
22
|
-
*/
|
23
|
-
|
24
|
-
#ifndef DHT_H
|
25
|
-
#define DHT_H
|
26
|
-
|
27
|
-
#include "net_crypto.h"
|
28
|
-
|
29
|
-
#ifdef __cplusplus
|
30
|
-
extern "C" {
|
31
|
-
#endif
|
32
|
-
|
33
|
-
/* Current time, unix format */
|
34
|
-
#define unix_time() ((uint32_t)time(NULL))
|
35
|
-
|
36
|
-
/* size of the client_id in bytes */
|
37
|
-
#define CLIENT_ID_SIZE crypto_box_PUBLICKEYBYTES
|
38
|
-
|
39
|
-
/* Add a new friend to the friends list
|
40
|
-
client_id must be CLIENT_ID_SIZE bytes long.
|
41
|
-
returns 0 if success
|
42
|
-
returns 1 if failure (friends list is full) */
|
43
|
-
int DHT_addfriend(uint8_t *client_id);
|
44
|
-
|
45
|
-
/* Delete a friend from the friends list
|
46
|
-
client_id must be CLIENT_ID_SIZE bytes long.
|
47
|
-
returns 0 if success
|
48
|
-
returns 1 if failure (client_id not in friends list) */
|
49
|
-
int DHT_delfriend(uint8_t *client_id);
|
50
|
-
|
51
|
-
/* Get ip of friend
|
52
|
-
client_id must be CLIENT_ID_SIZE bytes long.
|
53
|
-
ip must be 4 bytes long.
|
54
|
-
port must be 2 bytes long.
|
55
|
-
returns ip if success
|
56
|
-
returns ip of 0 if failure (This means the friend is either offline or we have not found him yet.)
|
57
|
-
returns ip of 1 if friend is not in list. */
|
58
|
-
IP_Port DHT_getfriendip(uint8_t *client_id);
|
59
|
-
|
60
|
-
/* Run this function at least a couple times per second (It's the main loop) */
|
61
|
-
void doDHT();
|
62
|
-
|
63
|
-
/* if we receive a DHT packet we call this function so it can be handled.
|
64
|
-
return 0 if packet is handled correctly.
|
65
|
-
return 1 if it didn't handle the packet or if the packet was shit. */
|
66
|
-
int DHT_handlepacket(uint8_t *packet, uint32_t length, IP_Port source);
|
67
|
-
|
68
|
-
/* Use this function to bootstrap the client
|
69
|
-
Sends a get nodes request to the given node with ip port and public_key */
|
70
|
-
void DHT_bootstrap(IP_Port ip_port, uint8_t *public_key);
|
71
|
-
|
72
|
-
/* ROUTING FUNCTIONS */
|
73
|
-
|
74
|
-
/* send the given packet to node with client_id
|
75
|
-
returns -1 if failure */
|
76
|
-
int route_packet(uint8_t *client_id, uint8_t *packet, uint32_t length);
|
77
|
-
|
78
|
-
/* Send the following packet to everyone who tells us they are connected to friend_id
|
79
|
-
returns the number of nodes it sent the packet to */
|
80
|
-
int route_tofriend(uint8_t *friend_id, uint8_t *packet, uint32_t length);
|
81
|
-
|
82
|
-
/* NAT PUNCHING FUNCTIONS */
|
83
|
-
|
84
|
-
/* Puts all the different ips returned by the nodes for a friend_id into array ip_portlist
|
85
|
-
ip_portlist must be at least MAX_FRIEND_CLIENTS big
|
86
|
-
returns the number of ips returned
|
87
|
-
returns -1 if no such friend*/
|
88
|
-
int friend_ips(IP_Port *ip_portlist, uint8_t *friend_id);
|
89
|
-
|
90
|
-
/* SAVE/LOAD functions */
|
91
|
-
|
92
|
-
/* get the size of the DHT (for saving) */
|
93
|
-
uint32_t DHT_size();
|
94
|
-
|
95
|
-
/* save the DHT in data where data is an array of size DHT_size() */
|
96
|
-
void DHT_save(uint8_t *data);
|
97
|
-
|
98
|
-
/* load the DHT from data of size size;
|
99
|
-
return -1 if failure
|
100
|
-
return 0 if success */
|
101
|
-
int DHT_load(uint8_t *data, uint32_t size);
|
102
|
-
|
103
|
-
/* returns 0 if we are not connected to the DHT
|
104
|
-
returns 1 if we are */
|
105
|
-
int DHT_isconnected();
|
106
|
-
|
107
|
-
#ifdef __cplusplus
|
108
|
-
}
|
109
|
-
#endif
|
110
|
-
|
111
|
-
#endif
|
@@ -1,79 +0,0 @@
|
|
1
|
-
/* LAN_discovery.c
|
2
|
-
*
|
3
|
-
* LAN discovery implementation.
|
4
|
-
*
|
5
|
-
* Copyright (C) 2013 Tox project All Rights Reserved.
|
6
|
-
*
|
7
|
-
* This file is part of Tox.
|
8
|
-
*
|
9
|
-
* 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
|
-
* the Free Software Foundation, either version 3 of the License, or
|
12
|
-
* (at your option) any later version.
|
13
|
-
*
|
14
|
-
* Tox is distributed in the hope that it will be useful,
|
15
|
-
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
16
|
-
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
17
|
-
* GNU General Public License for more details.
|
18
|
-
*
|
19
|
-
* 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
|
-
*
|
22
|
-
*/
|
23
|
-
|
24
|
-
#include "LAN_discovery.h"
|
25
|
-
|
26
|
-
|
27
|
-
/*Return the broadcast ip
|
28
|
-
TODO: make it return the real one, not the 255.255.255.255 one.*/
|
29
|
-
IP broadcast_ip()
|
30
|
-
{
|
31
|
-
IP ip;
|
32
|
-
ip.i = ~0;
|
33
|
-
return ip;
|
34
|
-
}
|
35
|
-
|
36
|
-
/*return 0 if ip is a LAN ip
|
37
|
-
return -1 if it is not */
|
38
|
-
int LAN_ip(IP ip)
|
39
|
-
{
|
40
|
-
if (ip.c[0] == 127)/* Loopback */
|
41
|
-
return 0;
|
42
|
-
if (ip.c[0] == 10)/* 10.0.0.0 to 10.255.255.255 range */
|
43
|
-
return 0;
|
44
|
-
if (ip.c[0] == 172 && ip.c[1] >= 16 && ip.c[1] <= 31)/* 172.16.0.0 to 172.31.255.255 range */
|
45
|
-
return 0;
|
46
|
-
if (ip.c[0] == 192 && ip.c[1] == 168) /* 192.168.0.0 to 192.168.255.255 range */
|
47
|
-
return 0;
|
48
|
-
if (ip.c[0] == 169 && ip.c[1] == 254 && ip.c[2] != 0 && ip.c[2] != 255)/* 169.254.1.0 to 169.254.254.255 range */
|
49
|
-
return 0;
|
50
|
-
return -1;
|
51
|
-
}
|
52
|
-
|
53
|
-
int handle_LANdiscovery(uint8_t *packet, uint32_t length, IP_Port source)
|
54
|
-
{
|
55
|
-
if (LAN_ip(source.ip) == -1)
|
56
|
-
return 1;
|
57
|
-
if (length != crypto_box_PUBLICKEYBYTES + 1)
|
58
|
-
return 1;
|
59
|
-
DHT_bootstrap(source, packet + 1);
|
60
|
-
return 0;
|
61
|
-
}
|
62
|
-
|
63
|
-
|
64
|
-
int send_LANdiscovery(uint16_t port)
|
65
|
-
{
|
66
|
-
uint8_t data[crypto_box_PUBLICKEYBYTES + 1];
|
67
|
-
data[0] = 32;
|
68
|
-
memcpy(data + 1, self_public_key, crypto_box_PUBLICKEYBYTES);
|
69
|
-
IP_Port ip_port = {broadcast_ip(), port};
|
70
|
-
return sendpacket(ip_port, data, 1 + crypto_box_PUBLICKEYBYTES);
|
71
|
-
}
|
72
|
-
|
73
|
-
|
74
|
-
int LANdiscovery_handlepacket(uint8_t *packet, uint32_t length, IP_Port source)
|
75
|
-
{
|
76
|
-
if (packet[0] == 32)
|
77
|
-
return handle_LANdiscovery(packet, length, source);
|
78
|
-
return 1;
|
79
|
-
}
|
@@ -1,755 +0,0 @@
|
|
1
|
-
/* Lossless_UDP.c
|
2
|
-
*
|
3
|
-
* An implementation of the Lossless_UDP protocol as seen in docs/Lossless_UDP.txt
|
4
|
-
*
|
5
|
-
* Copyright (C) 2013 Tox project All Rights Reserved.
|
6
|
-
*
|
7
|
-
* This file is part of Tox.
|
8
|
-
*
|
9
|
-
* 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
|
-
* the Free Software Foundation, either version 3 of the License, or
|
12
|
-
* (at your option) any later version.
|
13
|
-
*
|
14
|
-
* Tox is distributed in the hope that it will be useful,
|
15
|
-
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
16
|
-
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
17
|
-
* GNU General Public License for more details.
|
18
|
-
*
|
19
|
-
* 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
|
-
*
|
22
|
-
*/
|
23
|
-
|
24
|
-
/* TODO: clean this file a bit.
|
25
|
-
There are a couple of useless variables to get rid of. */
|
26
|
-
#include "Lossless_UDP.h"
|
27
|
-
|
28
|
-
/* maximum data packets in sent and receive queues. */
|
29
|
-
#define MAX_QUEUE_NUM 16
|
30
|
-
|
31
|
-
/* maximum length of the data in the data packets */
|
32
|
-
/* #define MAX_DATA_SIZE 1024 */ /* defined in Lossless_UDP.h */
|
33
|
-
|
34
|
-
/* maximum number of data packets in the buffer */
|
35
|
-
#define BUFFER_PACKET_NUM (16-1)
|
36
|
-
|
37
|
-
/* Lossless UDP connection timeout.
|
38
|
-
timeout per connection is randomly set between CONNEXION_TIMEOUT and 2*CONNEXION_TIMEOUT */
|
39
|
-
#define CONNEXION_TIMEOUT 5
|
40
|
-
|
41
|
-
/* initial amount of sync/hanshake packets to send per second. */
|
42
|
-
#define SYNC_RATE 2
|
43
|
-
|
44
|
-
/* initial send rate of data. */
|
45
|
-
#define DATA_SYNC_RATE 30
|
46
|
-
|
47
|
-
typedef struct {
|
48
|
-
uint8_t data[MAX_DATA_SIZE];
|
49
|
-
uint16_t size;
|
50
|
-
} Data;
|
51
|
-
|
52
|
-
typedef struct {
|
53
|
-
IP_Port ip_port;
|
54
|
-
uint8_t status; /* 0 if connection is dead, 1 if attempting handshake,
|
55
|
-
2 if handshake is done (we start sending SYNC packets)
|
56
|
-
3 if we are sending SYNC packets and can send data
|
57
|
-
4 if the connection has timed out. */
|
58
|
-
|
59
|
-
uint8_t inbound; /* 1 or 2 if connection was initiated by someone else, 0 if not.
|
60
|
-
2 if incoming_connection() has not returned it yet, 1 if it has. */
|
61
|
-
|
62
|
-
uint16_t SYNC_rate; /* current SYNC packet send rate packets per second. */
|
63
|
-
uint16_t data_rate; /* current data packet send rate packets per second. */
|
64
|
-
uint64_t last_SYNC; /* time at which our last SYNC packet was sent. */
|
65
|
-
uint64_t last_sent; /* time at which our last data or handshake packet was sent. */
|
66
|
-
uint64_t last_recvSYNC; /* time at which we last received a SYNC packet from the other */
|
67
|
-
uint64_t last_recvdata; /* time at which we last received a DATA packet from the other */
|
68
|
-
uint64_t killat; /* time at which to kill the connection */
|
69
|
-
Data sendbuffer[MAX_QUEUE_NUM]; /* packet send buffer. */
|
70
|
-
Data recvbuffer[MAX_QUEUE_NUM]; /* packet receive buffer. */
|
71
|
-
uint32_t handshake_id1;
|
72
|
-
uint32_t handshake_id2;
|
73
|
-
uint32_t recv_packetnum; /* number of data packets received (also used as handshake_id1) */
|
74
|
-
uint32_t orecv_packetnum; /* number of packets received by the other peer */
|
75
|
-
uint32_t sent_packetnum; /* number of data packets sent */
|
76
|
-
uint32_t osent_packetnum; /* number of packets sent by the other peer. */
|
77
|
-
uint32_t sendbuff_packetnum; /* number of latest packet written onto the sendbuffer */
|
78
|
-
uint32_t successful_sent; /* we know all packets before that number were successfully sent */
|
79
|
-
uint32_t successful_read; /* packet number of last packet read with the read_packet function */
|
80
|
-
uint32_t req_packets[BUFFER_PACKET_NUM]; /* list of currently requested packet numbers(by the other person) */
|
81
|
-
uint16_t num_req_paquets; /* total number of currently requested packets(by the other person) */
|
82
|
-
uint8_t recv_counter;
|
83
|
-
uint8_t send_counter;
|
84
|
-
uint8_t timeout; /* connection timeout in seconds. */
|
85
|
-
} Connection;
|
86
|
-
|
87
|
-
|
88
|
-
static Connection * connections;
|
89
|
-
|
90
|
-
static uint32_t connections_length; /* Length of connections array */
|
91
|
-
static uint32_t connections_number; /* Number of connections in connections array */
|
92
|
-
|
93
|
-
#define MAX_CONNECTIONS connections_length
|
94
|
-
|
95
|
-
/* Functions */
|
96
|
-
|
97
|
-
/* get connection id from IP_Port
|
98
|
-
return -1 if there are no connections like we are looking for
|
99
|
-
return id if it found it */
|
100
|
-
int getconnection_id(IP_Port ip_port)
|
101
|
-
{
|
102
|
-
uint32_t i;
|
103
|
-
for (i = 0; i < MAX_CONNECTIONS; ++i) {
|
104
|
-
if (connections[i].ip_port.ip.i == ip_port.ip.i &&
|
105
|
-
connections[i].ip_port.port == ip_port.port && connections[i].status > 0)
|
106
|
-
return i;
|
107
|
-
}
|
108
|
-
return -1;
|
109
|
-
}
|
110
|
-
|
111
|
-
/* table of random numbers used below. */
|
112
|
-
static uint32_t randtable[6][256];
|
113
|
-
|
114
|
-
/* generate a handshake_id which depends on the ip_port.
|
115
|
-
this function will always give one unique handshake_id per ip_port.
|
116
|
-
TODO: make this better */
|
117
|
-
uint32_t handshake_id(IP_Port source)
|
118
|
-
{
|
119
|
-
uint32_t id = 0, i;
|
120
|
-
for (i = 0; i < 6; ++i) {
|
121
|
-
if(randtable[i][((uint8_t *)&source)[i]] == 0)
|
122
|
-
randtable[i][((uint8_t *)&source)[i]] = random_int();
|
123
|
-
id ^= randtable[i][((uint8_t *)&source)[i]];
|
124
|
-
}
|
125
|
-
if (id == 0) /* id can't be zero */
|
126
|
-
id = 1;
|
127
|
-
return id;
|
128
|
-
}
|
129
|
-
|
130
|
-
/* change the hnshake id associated with that ip_port
|
131
|
-
TODO: make this better */
|
132
|
-
void change_handshake(IP_Port source)
|
133
|
-
{
|
134
|
-
uint8_t rand = random_int() % 4;
|
135
|
-
randtable[rand][((uint8_t *)&source)[rand]] = random_int();
|
136
|
-
}
|
137
|
-
|
138
|
-
/* initialize a new connection to ip_port
|
139
|
-
returns an integer corresponding to the connection id.
|
140
|
-
return -1 if it could not initialize the connection.
|
141
|
-
if there already was an existing connection to that ip_port return its number. */
|
142
|
-
int new_connection(IP_Port ip_port)
|
143
|
-
{
|
144
|
-
int connect = getconnection_id(ip_port);
|
145
|
-
if (connect != -1)
|
146
|
-
return connect;
|
147
|
-
|
148
|
-
if(connections_number == connections_length) {
|
149
|
-
Connection * temp;
|
150
|
-
temp = realloc(connections, sizeof(Connection) * (connections_length + 1));
|
151
|
-
if(temp == NULL)
|
152
|
-
return -1;
|
153
|
-
memset(&temp[connections_length], 0, sizeof(Connection));
|
154
|
-
++connections_length;
|
155
|
-
connections = temp;
|
156
|
-
}
|
157
|
-
|
158
|
-
uint32_t i;
|
159
|
-
for (i = 0; i < MAX_CONNECTIONS; ++i) {
|
160
|
-
if(connections[i].status == 0) {
|
161
|
-
memset(&connections[i], 0, sizeof(Connection));
|
162
|
-
connections[i].ip_port = ip_port;
|
163
|
-
connections[i].status = 1;
|
164
|
-
connections[i].inbound = 0;
|
165
|
-
connections[i].handshake_id1 = handshake_id(ip_port);
|
166
|
-
connections[i].sent_packetnum = connections[i].handshake_id1;
|
167
|
-
connections[i].sendbuff_packetnum = connections[i].handshake_id1;
|
168
|
-
connections[i].successful_sent = connections[i].handshake_id1;
|
169
|
-
connections[i].SYNC_rate = SYNC_RATE;
|
170
|
-
connections[i].data_rate = DATA_SYNC_RATE;
|
171
|
-
connections[i].last_recvSYNC = current_time();
|
172
|
-
connections[i].last_sent = current_time();
|
173
|
-
connections[i].killat = ~0;
|
174
|
-
connections[i].send_counter = 0;
|
175
|
-
/* add randomness to timeout to prevent connections getting stuck in a loop. */
|
176
|
-
connections[i].timeout = CONNEXION_TIMEOUT + rand() % CONNEXION_TIMEOUT;
|
177
|
-
++connections_number;
|
178
|
-
return i;
|
179
|
-
}
|
180
|
-
}
|
181
|
-
return -1;
|
182
|
-
}
|
183
|
-
|
184
|
-
/* initialize a new inbound connection from ip_port
|
185
|
-
returns an integer corresponding to the connection id.
|
186
|
-
return -1 if it could not initialize the connection. */
|
187
|
-
int new_inconnection(IP_Port ip_port)
|
188
|
-
{
|
189
|
-
if (getconnection_id(ip_port) != -1)
|
190
|
-
return -1;
|
191
|
-
|
192
|
-
if(connections_number == connections_length) {
|
193
|
-
Connection * temp;
|
194
|
-
temp = realloc(connections, sizeof(Connection) * (connections_length + 1));
|
195
|
-
if(temp == NULL)
|
196
|
-
return -1;
|
197
|
-
memset(&temp[connections_length], 0, sizeof(Connection));
|
198
|
-
++connections_length;
|
199
|
-
connections = temp;
|
200
|
-
}
|
201
|
-
|
202
|
-
uint32_t i;
|
203
|
-
for (i = 0; i < MAX_CONNECTIONS; ++i) {
|
204
|
-
if (connections[i].status == 0) {
|
205
|
-
memset(&connections[i], 0, sizeof(Connection));
|
206
|
-
connections[i].ip_port = ip_port;
|
207
|
-
connections[i].status = 2;
|
208
|
-
connections[i].inbound = 2;
|
209
|
-
connections[i].SYNC_rate = SYNC_RATE;
|
210
|
-
connections[i].data_rate = DATA_SYNC_RATE;
|
211
|
-
connections[i].last_recvSYNC = current_time();
|
212
|
-
connections[i].last_sent = current_time();
|
213
|
-
/* add randomness to timeout to prevent connections getting stuck in a loop. */
|
214
|
-
connections[i].timeout = CONNEXION_TIMEOUT + rand() % CONNEXION_TIMEOUT;
|
215
|
-
/* if this connection isn't handled within the timeout kill it. */
|
216
|
-
connections[i].killat = current_time() + 1000000UL*connections[i].timeout;
|
217
|
-
connections[i].send_counter = 127;
|
218
|
-
++connections_number;
|
219
|
-
return i;
|
220
|
-
}
|
221
|
-
}
|
222
|
-
return -1;
|
223
|
-
}
|
224
|
-
|
225
|
-
/* returns an integer corresponding to the next connection in our incoming connection list
|
226
|
-
return -1 if there are no new incoming connections in the list. */
|
227
|
-
int incoming_connection()
|
228
|
-
{
|
229
|
-
uint32_t i;
|
230
|
-
for (i = 0; i < MAX_CONNECTIONS; ++i) {
|
231
|
-
if (connections[i].inbound == 2) {
|
232
|
-
connections[i].inbound = 1;
|
233
|
-
return i;
|
234
|
-
}
|
235
|
-
}
|
236
|
-
return -1;
|
237
|
-
}
|
238
|
-
/*Try to free some memory from the connections array.*/
|
239
|
-
static void free_connections()
|
240
|
-
{
|
241
|
-
uint32_t i;
|
242
|
-
for(i = connections_length; i != 0; --i)
|
243
|
-
if (connections[i - 1].status != 0)
|
244
|
-
break;
|
245
|
-
|
246
|
-
if(connections_length == i)
|
247
|
-
return;
|
248
|
-
Connection * temp;
|
249
|
-
temp = realloc(connections, sizeof(Connection) * i);
|
250
|
-
if(temp == NULL && i != 0)
|
251
|
-
return;
|
252
|
-
connections = temp;
|
253
|
-
connections_length = i;
|
254
|
-
}
|
255
|
-
|
256
|
-
/* return -1 if it could not kill the connection.
|
257
|
-
return 0 if killed successfully */
|
258
|
-
int kill_connection(int connection_id)
|
259
|
-
{
|
260
|
-
if (connection_id >= 0 && connection_id < MAX_CONNECTIONS) {
|
261
|
-
if (connections[connection_id].status > 0) {
|
262
|
-
connections[connection_id].status = 0;
|
263
|
-
change_handshake(connections[connection_id].ip_port);
|
264
|
-
--connections_number;
|
265
|
-
free_connections();
|
266
|
-
return 0;
|
267
|
-
}
|
268
|
-
}
|
269
|
-
return -1;
|
270
|
-
}
|
271
|
-
|
272
|
-
/* kill connection in seconds seconds.
|
273
|
-
return -1 if it can not kill the connection.
|
274
|
-
return 0 if it will kill it */
|
275
|
-
int kill_connection_in(int connection_id, uint32_t seconds)
|
276
|
-
{
|
277
|
-
if (connection_id >= 0 && connection_id < MAX_CONNECTIONS) {
|
278
|
-
if (connections[connection_id].status > 0) {
|
279
|
-
connections[connection_id].killat = current_time() + 1000000UL*seconds;
|
280
|
-
return 0;
|
281
|
-
}
|
282
|
-
}
|
283
|
-
return -1;
|
284
|
-
}
|
285
|
-
|
286
|
-
/* check if connection is connected
|
287
|
-
return 0 no.
|
288
|
-
return 1 if attempting handshake
|
289
|
-
return 2 if handshake is done
|
290
|
-
return 3 if fully connected
|
291
|
-
return 4 if timed out and waiting to be killed */
|
292
|
-
int is_connected(int connection_id)
|
293
|
-
{
|
294
|
-
if (connection_id >= 0 && connection_id < MAX_CONNECTIONS)
|
295
|
-
return connections[connection_id].status;
|
296
|
-
return 0;
|
297
|
-
}
|
298
|
-
|
299
|
-
/* returns the ip_port of the corresponding connection. */
|
300
|
-
IP_Port connection_ip(int connection_id)
|
301
|
-
{
|
302
|
-
if (connection_id >= 0 && connection_id < MAX_CONNECTIONS)
|
303
|
-
return connections[connection_id].ip_port;
|
304
|
-
IP_Port zero = {{{0}}, 0};
|
305
|
-
return zero;
|
306
|
-
}
|
307
|
-
|
308
|
-
/* returns the number of packets in the queue waiting to be successfully sent. */
|
309
|
-
uint32_t sendqueue(int connection_id)
|
310
|
-
{
|
311
|
-
if (connection_id < 0 || connection_id >= MAX_CONNECTIONS)
|
312
|
-
return 0;
|
313
|
-
return connections[connection_id].sendbuff_packetnum - connections[connection_id].successful_sent;
|
314
|
-
}
|
315
|
-
|
316
|
-
/* returns the number of packets in the queue waiting to be successfully read with read_packet(...) */
|
317
|
-
uint32_t recvqueue(int connection_id)
|
318
|
-
{
|
319
|
-
if (connection_id < 0 || connection_id >= MAX_CONNECTIONS)
|
320
|
-
return 0;
|
321
|
-
return connections[connection_id].recv_packetnum - connections[connection_id].successful_read;
|
322
|
-
}
|
323
|
-
|
324
|
-
/* returns the id of the next packet in the queue
|
325
|
-
return -1 if no packet in queue */
|
326
|
-
char id_packet(int connection_id)
|
327
|
-
{
|
328
|
-
if (connection_id < 0 || connection_id >= MAX_CONNECTIONS)
|
329
|
-
return -1;
|
330
|
-
if (recvqueue(connection_id) != 0 && connections[connection_id].status != 0)
|
331
|
-
return connections[connection_id].recvbuffer[connections[connection_id].successful_read % MAX_QUEUE_NUM].data[0];
|
332
|
-
return -1;
|
333
|
-
}
|
334
|
-
|
335
|
-
/* return 0 if there is no received data in the buffer.
|
336
|
-
return length of received packet if successful */
|
337
|
-
int read_packet(int connection_id, uint8_t * data)
|
338
|
-
{
|
339
|
-
if (recvqueue(connection_id) != 0) {
|
340
|
-
uint16_t index = connections[connection_id].successful_read % MAX_QUEUE_NUM;
|
341
|
-
uint16_t size = connections[connection_id].recvbuffer[index].size;
|
342
|
-
memcpy(data, connections[connection_id].recvbuffer[index].data, size);
|
343
|
-
++connections[connection_id].successful_read;
|
344
|
-
connections[connection_id].recvbuffer[index].size = 0;
|
345
|
-
return size;
|
346
|
-
}
|
347
|
-
return 0;
|
348
|
-
}
|
349
|
-
|
350
|
-
/* return 0 if data could not be put in packet queue
|
351
|
-
return 1 if data was put into the queue */
|
352
|
-
int write_packet(int connection_id, uint8_t * data, uint32_t length)
|
353
|
-
{
|
354
|
-
if (length > MAX_DATA_SIZE)
|
355
|
-
return 0;
|
356
|
-
if (length == 0)
|
357
|
-
return 0;
|
358
|
-
if (sendqueue(connection_id) < BUFFER_PACKET_NUM) {
|
359
|
-
uint32_t index = connections[connection_id].sendbuff_packetnum % MAX_QUEUE_NUM;
|
360
|
-
memcpy(connections[connection_id].sendbuffer[index].data, data, length);
|
361
|
-
connections[connection_id].sendbuffer[index].size = length;
|
362
|
-
connections[connection_id].sendbuff_packetnum++;
|
363
|
-
return 1;
|
364
|
-
}
|
365
|
-
return 0;
|
366
|
-
}
|
367
|
-
|
368
|
-
/* put the packet numbers the we are missing in requested and return the number */
|
369
|
-
uint32_t missing_packets(int connection_id, uint32_t * requested)
|
370
|
-
{
|
371
|
-
uint32_t number = 0;
|
372
|
-
uint32_t i;
|
373
|
-
uint32_t temp;
|
374
|
-
if (recvqueue(connection_id) >= (BUFFER_PACKET_NUM - 1)) /* don't request packets if the buffer is full. */
|
375
|
-
return 0;
|
376
|
-
for (i = connections[connection_id].recv_packetnum; i != connections[connection_id].osent_packetnum; i++) {
|
377
|
-
if(connections[connection_id].recvbuffer[i % MAX_QUEUE_NUM].size == 0) {
|
378
|
-
temp = htonl(i);
|
379
|
-
memcpy(requested + number, &temp, 4);
|
380
|
-
++number;
|
381
|
-
}
|
382
|
-
}
|
383
|
-
if(number == 0)
|
384
|
-
connections[connection_id].recv_packetnum = connections[connection_id].osent_packetnum;
|
385
|
-
return number;
|
386
|
-
}
|
387
|
-
|
388
|
-
/* Packet sending functions
|
389
|
-
One per packet type.
|
390
|
-
see docs/Lossless_UDP.txt for more information. */
|
391
|
-
int send_handshake(IP_Port ip_port, uint32_t handshake_id1, uint32_t handshake_id2)
|
392
|
-
{
|
393
|
-
uint8_t packet[1 + 4 + 4];
|
394
|
-
uint32_t temp;
|
395
|
-
|
396
|
-
packet[0] = 16;
|
397
|
-
temp = htonl(handshake_id1);
|
398
|
-
memcpy(packet + 1, &temp, 4);
|
399
|
-
temp = htonl(handshake_id2);
|
400
|
-
memcpy(packet + 5, &temp, 4);
|
401
|
-
return sendpacket(ip_port, packet, sizeof(packet));
|
402
|
-
}
|
403
|
-
|
404
|
-
int send_SYNC(uint32_t connection_id)
|
405
|
-
{
|
406
|
-
|
407
|
-
uint8_t packet[(BUFFER_PACKET_NUM*4 + 4 + 4 + 2)];
|
408
|
-
uint16_t index = 0;
|
409
|
-
|
410
|
-
IP_Port ip_port = connections[connection_id].ip_port;
|
411
|
-
uint8_t counter = connections[connection_id].send_counter;
|
412
|
-
uint32_t recv_packetnum = htonl(connections[connection_id].recv_packetnum);
|
413
|
-
uint32_t sent_packetnum = htonl(connections[connection_id].sent_packetnum);
|
414
|
-
uint32_t requested[BUFFER_PACKET_NUM];
|
415
|
-
uint32_t number = missing_packets(connection_id, requested);
|
416
|
-
|
417
|
-
packet[0] = 17;
|
418
|
-
index += 1;
|
419
|
-
memcpy(packet + index, &counter, 1);
|
420
|
-
index += 1;
|
421
|
-
memcpy(packet + index, &recv_packetnum, 4);
|
422
|
-
index += 4;
|
423
|
-
memcpy(packet + index, &sent_packetnum, 4);
|
424
|
-
index += 4;
|
425
|
-
memcpy(packet + index, requested, 4 * number);
|
426
|
-
|
427
|
-
return sendpacket(ip_port, packet, (number*4 + 4 + 4 + 2));
|
428
|
-
|
429
|
-
}
|
430
|
-
|
431
|
-
int send_data_packet(uint32_t connection_id, uint32_t packet_num)
|
432
|
-
{
|
433
|
-
uint32_t index = packet_num % MAX_QUEUE_NUM;
|
434
|
-
uint32_t temp;
|
435
|
-
uint8_t packet[1 + 4 + MAX_DATA_SIZE];
|
436
|
-
packet[0] = 18;
|
437
|
-
temp = htonl(packet_num);
|
438
|
-
memcpy(packet + 1, &temp, 4);
|
439
|
-
memcpy(packet + 5, connections[connection_id].sendbuffer[index].data,
|
440
|
-
connections[connection_id].sendbuffer[index].size);
|
441
|
-
return sendpacket(connections[connection_id].ip_port, packet,
|
442
|
-
1 + 4 + connections[connection_id].sendbuffer[index].size);
|
443
|
-
}
|
444
|
-
|
445
|
-
/* sends 1 data packet */
|
446
|
-
int send_DATA(uint32_t connection_id)
|
447
|
-
{
|
448
|
-
int ret;
|
449
|
-
uint32_t buffer[BUFFER_PACKET_NUM];
|
450
|
-
if (connections[connection_id].num_req_paquets > 0) {
|
451
|
-
ret = send_data_packet(connection_id, connections[connection_id].req_packets[0]);
|
452
|
-
connections[connection_id].num_req_paquets--;
|
453
|
-
memcpy(buffer, connections[connection_id].req_packets + 1, connections[connection_id].num_req_paquets * 4);
|
454
|
-
memcpy(connections[connection_id].req_packets, buffer, connections[connection_id].num_req_paquets * 4);
|
455
|
-
return ret;
|
456
|
-
}
|
457
|
-
if (connections[connection_id].sendbuff_packetnum != connections[connection_id].sent_packetnum) {
|
458
|
-
ret = send_data_packet(connection_id, connections[connection_id].sent_packetnum);
|
459
|
-
connections[connection_id].sent_packetnum++;
|
460
|
-
return ret;
|
461
|
-
}
|
462
|
-
return 0;
|
463
|
-
}
|
464
|
-
|
465
|
-
/* END of packet sending functions */
|
466
|
-
|
467
|
-
/* Packet handling functions
|
468
|
-
One to handle each type of packets we receive
|
469
|
-
return 0 if handled correctly, 1 if packet is bad. */
|
470
|
-
int handle_handshake(uint8_t * packet, uint32_t length, IP_Port source)
|
471
|
-
{
|
472
|
-
if (length != (1 + 4 + 4))
|
473
|
-
return 1;
|
474
|
-
uint32_t temp;
|
475
|
-
uint32_t handshake_id1, handshake_id2;
|
476
|
-
int connection = getconnection_id(source);
|
477
|
-
memcpy(&temp, packet + 1, 4);
|
478
|
-
handshake_id1 = ntohl(temp);
|
479
|
-
memcpy(&temp, packet + 5, 4);
|
480
|
-
handshake_id2 = ntohl(temp);
|
481
|
-
|
482
|
-
if (handshake_id2 == 0 && is_connected(connection) < 3) {
|
483
|
-
send_handshake(source, handshake_id(source), handshake_id1);
|
484
|
-
return 0;
|
485
|
-
}
|
486
|
-
if (is_connected(connection) != 1)
|
487
|
-
return 1;
|
488
|
-
if (handshake_id2 == connections[connection].handshake_id1) { /* if handshake_id2 is what we sent previously as handshake_id1 */
|
489
|
-
connections[connection].status = 2;
|
490
|
-
/* NOTE: is this necessary?
|
491
|
-
connections[connection].handshake_id2 = handshake_id1; */
|
492
|
-
connections[connection].orecv_packetnum = handshake_id2;
|
493
|
-
connections[connection].osent_packetnum = handshake_id1;
|
494
|
-
connections[connection].recv_packetnum = handshake_id1;
|
495
|
-
connections[connection].successful_read = handshake_id1;
|
496
|
-
}
|
497
|
-
return 0;
|
498
|
-
|
499
|
-
}
|
500
|
-
|
501
|
-
/* returns 1 if sync packet is valid
|
502
|
-
0 if not. */
|
503
|
-
int SYNC_valid(uint32_t length)
|
504
|
-
{
|
505
|
-
if (length < 4 + 4 + 2)
|
506
|
-
return 0;
|
507
|
-
if (length > (BUFFER_PACKET_NUM*4 + 4 + 4 + 2) ||
|
508
|
-
((length - 4 - 4 - 2) % 4) != 0)
|
509
|
-
return 0;
|
510
|
-
return 1;
|
511
|
-
}
|
512
|
-
|
513
|
-
/* case 1: */
|
514
|
-
int handle_SYNC1(IP_Port source, uint32_t recv_packetnum, uint32_t sent_packetnum)
|
515
|
-
{
|
516
|
-
if (handshake_id(source) == recv_packetnum) {
|
517
|
-
int x = new_inconnection(source);
|
518
|
-
if (x != -1) {
|
519
|
-
connections[x].orecv_packetnum = recv_packetnum;
|
520
|
-
connections[x].sent_packetnum = recv_packetnum;
|
521
|
-
connections[x].sendbuff_packetnum = recv_packetnum;
|
522
|
-
connections[x].successful_sent = recv_packetnum;
|
523
|
-
connections[x].osent_packetnum = sent_packetnum;
|
524
|
-
connections[x].recv_packetnum = sent_packetnum;
|
525
|
-
connections[x].successful_read = sent_packetnum;
|
526
|
-
|
527
|
-
return x;
|
528
|
-
}
|
529
|
-
}
|
530
|
-
return -1;
|
531
|
-
}
|
532
|
-
|
533
|
-
/* case 2: */
|
534
|
-
int handle_SYNC2(int connection_id, uint8_t counter, uint32_t recv_packetnum, uint32_t sent_packetnum)
|
535
|
-
{
|
536
|
-
if (recv_packetnum == connections[connection_id].orecv_packetnum) {
|
537
|
-
/* && sent_packetnum == connections[connection_id].osent_packetnum) */
|
538
|
-
connections[connection_id].status = 3;
|
539
|
-
connections[connection_id].recv_counter = counter;
|
540
|
-
++connections[connection_id].send_counter;
|
541
|
-
send_SYNC(connection_id);
|
542
|
-
return 0;
|
543
|
-
}
|
544
|
-
return 1;
|
545
|
-
}
|
546
|
-
/* case 3: */
|
547
|
-
int handle_SYNC3(int connection_id, uint8_t counter, uint32_t recv_packetnum, uint32_t sent_packetnum, uint32_t * req_packets,
|
548
|
-
uint16_t number)
|
549
|
-
{
|
550
|
-
uint8_t comp_counter = (counter - connections[connection_id].recv_counter );
|
551
|
-
uint32_t i, temp;
|
552
|
-
/* uint32_t comp_1 = (recv_packetnum - connections[connection_id].successful_sent);
|
553
|
-
uint32_t comp_2 = (sent_packetnum - connections[connection_id].successful_read); */
|
554
|
-
uint32_t comp_1 = (recv_packetnum - connections[connection_id].orecv_packetnum);
|
555
|
-
uint32_t comp_2 = (sent_packetnum - connections[connection_id].osent_packetnum);
|
556
|
-
if (comp_1 <= BUFFER_PACKET_NUM && comp_2 <= BUFFER_PACKET_NUM && comp_counter < 10 && comp_counter != 0) { /* packet valid */
|
557
|
-
connections[connection_id].orecv_packetnum = recv_packetnum;
|
558
|
-
connections[connection_id].osent_packetnum = sent_packetnum;
|
559
|
-
connections[connection_id].successful_sent = recv_packetnum;
|
560
|
-
connections[connection_id].last_recvSYNC = current_time();
|
561
|
-
connections[connection_id].recv_counter = counter;
|
562
|
-
++connections[connection_id].send_counter;
|
563
|
-
for (i = 0; i < number; ++i) {
|
564
|
-
temp = ntohl(req_packets[i]);
|
565
|
-
memcpy(connections[connection_id].req_packets + i, &temp, 4 * number);
|
566
|
-
}
|
567
|
-
connections[connection_id].num_req_paquets = number;
|
568
|
-
return 0;
|
569
|
-
}
|
570
|
-
return 1;
|
571
|
-
}
|
572
|
-
|
573
|
-
int handle_SYNC(uint8_t *packet, uint32_t length, IP_Port source)
|
574
|
-
{
|
575
|
-
|
576
|
-
if (!SYNC_valid(length))
|
577
|
-
return 1;
|
578
|
-
int connection = getconnection_id(source);
|
579
|
-
uint8_t counter;
|
580
|
-
uint32_t temp;
|
581
|
-
uint32_t recv_packetnum, sent_packetnum;
|
582
|
-
uint32_t req_packets[BUFFER_PACKET_NUM];
|
583
|
-
uint16_t number = (length - 4 - 4 - 2)/ 4;
|
584
|
-
|
585
|
-
memcpy(&counter, packet + 1, 1);
|
586
|
-
memcpy(&temp, packet + 2, 4);
|
587
|
-
recv_packetnum = ntohl(temp);
|
588
|
-
memcpy(&temp,packet + 6, 4);
|
589
|
-
sent_packetnum = ntohl(temp);
|
590
|
-
if (number != 0)
|
591
|
-
memcpy(req_packets, packet + 10, 4 * number);
|
592
|
-
if (connection == -1)
|
593
|
-
return handle_SYNC1(source, recv_packetnum, sent_packetnum);
|
594
|
-
if (connections[connection].status == 2)
|
595
|
-
return handle_SYNC2(connection, counter, recv_packetnum, sent_packetnum);
|
596
|
-
if (connections[connection].status == 3)
|
597
|
-
return handle_SYNC3(connection, counter, recv_packetnum, sent_packetnum, req_packets, number);
|
598
|
-
return 0;
|
599
|
-
}
|
600
|
-
|
601
|
-
/* add a packet to the received buffer and set the recv_packetnum of the connection to its proper value.
|
602
|
-
return 1 if data was too big, 0 if not. */
|
603
|
-
int add_recv(int connection_id, uint32_t data_num, uint8_t *data, uint16_t size)
|
604
|
-
{
|
605
|
-
if (size > MAX_DATA_SIZE)
|
606
|
-
return 1;
|
607
|
-
|
608
|
-
uint32_t i;
|
609
|
-
uint32_t maxnum = connections[connection_id].successful_read + BUFFER_PACKET_NUM;
|
610
|
-
uint32_t sent_packet = data_num - connections[connection_id].osent_packetnum;
|
611
|
-
for (i = connections[connection_id].recv_packetnum; i != maxnum; ++i) {
|
612
|
-
if (i == data_num) {
|
613
|
-
memcpy(connections[connection_id].recvbuffer[i % MAX_QUEUE_NUM].data, data, size);
|
614
|
-
connections[connection_id].recvbuffer[i % MAX_QUEUE_NUM].size = size;
|
615
|
-
connections[connection_id].last_recvdata = current_time();
|
616
|
-
if (sent_packet < BUFFER_PACKET_NUM)
|
617
|
-
connections[connection_id].osent_packetnum = data_num;
|
618
|
-
break;
|
619
|
-
}
|
620
|
-
}
|
621
|
-
for (i = connections[connection_id].recv_packetnum; i != maxnum; ++i) {
|
622
|
-
if (connections[connection_id].recvbuffer[i % MAX_QUEUE_NUM].size != 0)
|
623
|
-
connections[connection_id].recv_packetnum = i;
|
624
|
-
else
|
625
|
-
break;
|
626
|
-
}
|
627
|
-
|
628
|
-
return 0;
|
629
|
-
}
|
630
|
-
|
631
|
-
int handle_data(uint8_t *packet, uint32_t length, IP_Port source)
|
632
|
-
{
|
633
|
-
int connection = getconnection_id(source);
|
634
|
-
|
635
|
-
if (connection == -1)
|
636
|
-
return 1;
|
637
|
-
|
638
|
-
if (connections[connection].status != 3) /* Drop the data packet if connection is not connected. */
|
639
|
-
return 1;
|
640
|
-
|
641
|
-
if (length > 1 + 4 + MAX_DATA_SIZE || length < 1 + 4 + 1)
|
642
|
-
return 1;
|
643
|
-
uint32_t temp;
|
644
|
-
uint32_t number;
|
645
|
-
uint16_t size = length - 1 - 4;
|
646
|
-
|
647
|
-
memcpy(&temp, packet + 1, 4);
|
648
|
-
number = ntohl(temp);
|
649
|
-
return add_recv(connection, number, packet + 5, size);
|
650
|
-
}
|
651
|
-
|
652
|
-
/* END of packet handling functions */
|
653
|
-
|
654
|
-
int LosslessUDP_handlepacket(uint8_t *packet, uint32_t length, IP_Port source)
|
655
|
-
{
|
656
|
-
switch (packet[0]) { //TODO: check if no break statement is correct???
|
657
|
-
case 16:
|
658
|
-
return handle_handshake(packet, length, source);
|
659
|
-
|
660
|
-
case 17:
|
661
|
-
return handle_SYNC(packet, length, source);
|
662
|
-
|
663
|
-
case 18:
|
664
|
-
return handle_data(packet, length, source);
|
665
|
-
|
666
|
-
default:
|
667
|
-
return 1;
|
668
|
-
}
|
669
|
-
|
670
|
-
return 0;
|
671
|
-
}
|
672
|
-
|
673
|
-
/* Send handshake requests
|
674
|
-
handshake packets are sent at the same rate as SYNC packets */
|
675
|
-
void doNew()
|
676
|
-
{
|
677
|
-
uint32_t i;
|
678
|
-
uint64_t temp_time = current_time();
|
679
|
-
for (i = 0; i < MAX_CONNECTIONS; ++i) {
|
680
|
-
if (connections[i].status == 1)
|
681
|
-
if ((connections[i].last_sent + (1000000UL/connections[i].SYNC_rate)) <= temp_time) {
|
682
|
-
send_handshake(connections[i].ip_port, connections[i].handshake_id1, 0);
|
683
|
-
connections[i].last_sent = temp_time;
|
684
|
-
}
|
685
|
-
|
686
|
-
/* kill all timed out connections */
|
687
|
-
if ( connections[i].status > 0 && (connections[i].last_recvSYNC + connections[i].timeout * 1000000UL) < temp_time &&
|
688
|
-
connections[i].status != 4)
|
689
|
-
/* kill_connection(i); */
|
690
|
-
connections[i].status = 4;
|
691
|
-
if (connections[i].status > 0 && connections[i].killat < temp_time)
|
692
|
-
kill_connection(i);
|
693
|
-
}
|
694
|
-
}
|
695
|
-
|
696
|
-
void doSYNC()
|
697
|
-
{
|
698
|
-
uint32_t i;
|
699
|
-
uint64_t temp_time = current_time();
|
700
|
-
for (i = 0; i < MAX_CONNECTIONS; ++i) {
|
701
|
-
if (connections[i].status == 2 || connections[i].status == 3)
|
702
|
-
if ((connections[i].last_SYNC + (1000000UL/connections[i].SYNC_rate)) <= temp_time) {
|
703
|
-
send_SYNC(i);
|
704
|
-
connections[i].last_SYNC = temp_time;
|
705
|
-
}
|
706
|
-
}
|
707
|
-
}
|
708
|
-
|
709
|
-
void doData()
|
710
|
-
{
|
711
|
-
uint32_t i;
|
712
|
-
uint64_t j;
|
713
|
-
uint64_t temp_time = current_time();
|
714
|
-
for (i = 0; i < MAX_CONNECTIONS; ++i)
|
715
|
-
if (connections[i].status == 3 && sendqueue(i) != 0)
|
716
|
-
if ((connections[i].last_sent + (1000000UL/connections[i].data_rate)) <= temp_time) {
|
717
|
-
for (j = connections[i].last_sent; j < temp_time; j += (1000000UL/connections[i].data_rate))
|
718
|
-
send_DATA(i);
|
719
|
-
connections[i].last_sent = temp_time;
|
720
|
-
}
|
721
|
-
}
|
722
|
-
|
723
|
-
/* TODO: flow control.
|
724
|
-
automatically adjusts send rates of packets for optimal transmission. */
|
725
|
-
|
726
|
-
#define MAX_SYNC_RATE 10
|
727
|
-
|
728
|
-
void adjustRates()
|
729
|
-
{
|
730
|
-
uint32_t i;
|
731
|
-
uint64_t temp_time = current_time();
|
732
|
-
for (i = 0; i < MAX_CONNECTIONS; ++i) {
|
733
|
-
if (connections[i].status == 1 || connections[i].status == 2)
|
734
|
-
connections[i].SYNC_rate = MAX_SYNC_RATE;
|
735
|
-
if (connections[i].status == 3) {
|
736
|
-
if (sendqueue(i) != 0) {
|
737
|
-
connections[i].data_rate = (BUFFER_PACKET_NUM - connections[i].num_req_paquets) * MAX_SYNC_RATE;
|
738
|
-
connections[i].SYNC_rate = MAX_SYNC_RATE;
|
739
|
-
} else if (connections[i].last_recvdata + 1000000UL > temp_time)
|
740
|
-
connections[i].SYNC_rate = MAX_SYNC_RATE;
|
741
|
-
else
|
742
|
-
connections[i].SYNC_rate = SYNC_RATE;
|
743
|
-
}
|
744
|
-
}
|
745
|
-
}
|
746
|
-
|
747
|
-
/* Call this function a couple times per second
|
748
|
-
It's the main loop. */
|
749
|
-
void doLossless_UDP()
|
750
|
-
{
|
751
|
-
doNew();
|
752
|
-
doSYNC();
|
753
|
-
doData();
|
754
|
-
adjustRates();
|
755
|
-
}
|