diff options
Diffstat (limited to 'core')
-rw-r--r-- | core/CMakeLists.txt | 1 | ||||
-rw-r--r-- | core/DHT.c | 806 | ||||
-rw-r--r-- | core/DHT.h | 54 | ||||
-rw-r--r-- | core/LAN_discovery.c | 79 | ||||
-rw-r--r-- | core/LAN_discovery.h | 50 | ||||
-rw-r--r-- | core/Lossless_UDP.c | 281 | ||||
-rw-r--r-- | core/Lossless_UDP.h | 52 | ||||
-rw-r--r-- | core/Messenger.c | 178 | ||||
-rw-r--r-- | core/Messenger.h | 55 | ||||
-rw-r--r-- | core/friend_requests.c | 87 | ||||
-rw-r--r-- | core/friend_requests.h | 22 | ||||
-rw-r--r-- | core/net_crypto.c | 203 | ||||
-rw-r--r-- | core/net_crypto.h | 50 | ||||
-rw-r--r-- | core/network.c | 83 | ||||
-rw-r--r-- | core/network.h | 47 |
15 files changed, 978 insertions, 1070 deletions
diff --git a/core/CMakeLists.txt b/core/CMakeLists.txt index ab4ff4bc..6ddd5b9b 100644 --- a/core/CMakeLists.txt +++ b/core/CMakeLists.txt | |||
@@ -11,6 +11,7 @@ set(core_sources | |||
11 | Lossless_UDP.c | 11 | Lossless_UDP.c |
12 | net_crypto.c | 12 | net_crypto.c |
13 | friend_requests.c | 13 | friend_requests.c |
14 | LAN_discovery.c | ||
14 | Messenger.c) | 15 | Messenger.c) |
15 | 16 | ||
16 | add_library(core ${core_sources}) | 17 | add_library(core ${core_sources}) |
@@ -1,33 +1,28 @@ | |||
1 | /* DHT.c | 1 | /* DHT.c |
2 | * | 2 | * |
3 | * An implementation of the DHT as seen in docs/DHT.txt | 3 | * An implementation of the DHT as seen in docs/DHT.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 | */ | ||
24 | |||
25 | |||
26 | |||
27 | 23 | ||
28 | #include "DHT.h" | 24 | #include "DHT.h" |
29 | 25 | ||
30 | |||
31 | typedef struct | 26 | typedef struct |
32 | { | 27 | { |
33 | uint8_t client_id[CLIENT_ID_SIZE]; | 28 | uint8_t client_id[CLIENT_ID_SIZE]; |
@@ -38,17 +33,24 @@ typedef struct | |||
38 | (for nodes in friends_list) or us (for nodes in close_clientlist) */ | 33 | (for nodes in friends_list) or us (for nodes in close_clientlist) */ |
39 | uint32_t ret_timestamp; | 34 | uint32_t ret_timestamp; |
40 | }Client_data; | 35 | }Client_data; |
36 | |||
41 | /* maximum number of clients stored per friend. */ | 37 | /* maximum number of clients stored per friend. */ |
42 | #define MAX_FRIEND_CLIENTS 8 | 38 | #define MAX_FRIEND_CLIENTS 8 |
39 | |||
43 | typedef struct | 40 | typedef struct |
44 | { | 41 | { |
45 | uint8_t client_id[CLIENT_ID_SIZE]; | 42 | uint8_t client_id[CLIENT_ID_SIZE]; |
46 | Client_data client_list[MAX_FRIEND_CLIENTS]; | 43 | Client_data client_list[MAX_FRIEND_CLIENTS]; |
47 | uint32_t lastgetnode; /* time at which the last get_nodes request was sent. */ | 44 | uint32_t lastgetnode; /* time at which the last get_nodes request was sent. */ |
48 | 45 | ||
46 | /*Symetric NAT hole punching stuff*/ | ||
47 | uint8_t hole_punching; /*0 if not hole punching, 1 if currently hole punching */ | ||
48 | uint32_t punching_index; | ||
49 | uint32_t punching_timestamp; | ||
50 | uint64_t NATping_id; | ||
51 | uint32_t NATping_timestamp; | ||
49 | }Friend; | 52 | }Friend; |
50 | 53 | ||
51 | |||
52 | typedef struct | 54 | typedef struct |
53 | { | 55 | { |
54 | uint8_t client_id[CLIENT_ID_SIZE]; | 56 | uint8_t client_id[CLIENT_ID_SIZE]; |
@@ -72,13 +74,11 @@ uint8_t self_secret_key[crypto_box_SECRETKEYBYTES]; | |||
72 | #define LCLIENT_LIST 32 | 74 | #define LCLIENT_LIST 32 |
73 | static Client_data close_clientlist[LCLIENT_LIST]; | 75 | static Client_data close_clientlist[LCLIENT_LIST]; |
74 | 76 | ||
75 | |||
76 | |||
77 | static Friend * friends_list; | 77 | static Friend * friends_list; |
78 | static uint16_t num_friends; | 78 | static uint16_t num_friends; |
79 | 79 | ||
80 | /* The list of ip ports along with the ping_id of what we sent them and a timestamp */ | 80 | /* The list of ip ports along with the ping_id of what we sent them and a timestamp */ |
81 | #define LPING_ARRAY 128 | 81 | #define LPING_ARRAY 256 |
82 | 82 | ||
83 | static Pinged pings[LPING_ARRAY]; | 83 | static Pinged pings[LPING_ARRAY]; |
84 | 84 | ||
@@ -86,7 +86,6 @@ static Pinged pings[LPING_ARRAY]; | |||
86 | 86 | ||
87 | static Pinged send_nodes[LSEND_NODES_ARRAY]; | 87 | static Pinged send_nodes[LSEND_NODES_ARRAY]; |
88 | 88 | ||
89 | |||
90 | /* Compares client_id1 and client_id2 with client_id | 89 | /* Compares client_id1 and client_id2 with client_id |
91 | return 0 if both are same distance | 90 | return 0 if both are same distance |
92 | return 1 if client_id1 is closer | 91 | return 1 if client_id1 is closer |
@@ -94,17 +93,12 @@ static Pinged send_nodes[LSEND_NODES_ARRAY]; | |||
94 | int id_closest(uint8_t * client_id, uint8_t * client_id1, uint8_t * client_id2) /* tested */ | 93 | int id_closest(uint8_t * client_id, uint8_t * client_id1, uint8_t * client_id2) /* tested */ |
95 | { | 94 | { |
96 | uint32_t i; | 95 | uint32_t i; |
97 | for(i = 0; i < CLIENT_ID_SIZE; ++i) | 96 | for(i = 0; i < CLIENT_ID_SIZE; ++i) { |
98 | { | 97 | if(abs(client_id[i] ^ client_id1[i]) < abs(client_id[i] ^ client_id2[i])) { |
99 | if(abs(client_id[i] ^ client_id1[i]) < abs(client_id[i] ^ client_id2[i])) | ||
100 | { | ||
101 | return 1; | 98 | return 1; |
102 | } | 99 | } else if(abs(client_id[i] ^ client_id1[i]) > abs(client_id[i] ^ client_id2[i])) { |
103 | else if(abs(client_id[i] ^ client_id1[i]) > abs(client_id[i] ^ client_id2[i])) | ||
104 | { | ||
105 | return 2; | 100 | return 2; |
106 | } | 101 | } |
107 | |||
108 | } | 102 | } |
109 | 103 | ||
110 | return 0; | 104 | return 0; |
@@ -121,17 +115,14 @@ int client_in_list(Client_data * list, uint32_t length, uint8_t * client_id, IP_ | |||
121 | uint32_t i; | 115 | uint32_t i; |
122 | uint32_t temp_time = unix_time(); | 116 | uint32_t temp_time = unix_time(); |
123 | 117 | ||
124 | for(i = 0; i < length; ++i) | 118 | for(i = 0; i < length; ++i) { |
125 | { | ||
126 | /*If ip_port is assigned to a different client_id replace it*/ | 119 | /*If ip_port is assigned to a different client_id replace it*/ |
127 | if(list[i].ip_port.ip.i == ip_port.ip.i && | 120 | if(list[i].ip_port.ip.i == ip_port.ip.i && |
128 | list[i].ip_port.port == ip_port.port) | 121 | list[i].ip_port.port == ip_port.port) { |
129 | { | ||
130 | memcpy(list[i].client_id, client_id, CLIENT_ID_SIZE); | 122 | memcpy(list[i].client_id, client_id, CLIENT_ID_SIZE); |
131 | } | 123 | } |
132 | 124 | ||
133 | if(memcmp(list[i].client_id, client_id, CLIENT_ID_SIZE) == 0) | 125 | if(memcmp(list[i].client_id, client_id, CLIENT_ID_SIZE) == 0) { |
134 | { | ||
135 | /* Refresh the client timestamp. */ | 126 | /* Refresh the client timestamp. */ |
136 | list[i].timestamp = temp_time; | 127 | list[i].timestamp = temp_time; |
137 | list[i].ip_port.ip.i = ip_port.ip.i; | 128 | list[i].ip_port.ip.i = ip_port.ip.i; |
@@ -148,10 +139,8 @@ int client_in_list(Client_data * list, uint32_t length, uint8_t * client_id, IP_ | |||
148 | int client_in_nodelist(Node_format * list, uint32_t length, uint8_t * client_id) | 139 | int client_in_nodelist(Node_format * list, uint32_t length, uint8_t * client_id) |
149 | { | 140 | { |
150 | uint32_t i; | 141 | uint32_t i; |
151 | for(i = 0; i < length; ++i) | 142 | for(i = 0; i < length; ++i) { |
152 | { | 143 | if(memcmp(list[i].client_id, client_id, CLIENT_ID_SIZE) == 0) { |
153 | if(memcmp(list[i].client_id, client_id, CLIENT_ID_SIZE) == 0) | ||
154 | { | ||
155 | 144 | ||
156 | return 1; | 145 | return 1; |
157 | } | 146 | } |
@@ -160,14 +149,25 @@ int client_in_nodelist(Node_format * list, uint32_t length, uint8_t * client_id) | |||
160 | 149 | ||
161 | } | 150 | } |
162 | 151 | ||
163 | 152 | /*Return the friend number from the client_id | |
153 | Return -1 if failure, number of friend if success*/ | ||
154 | static int friend_number(uint8_t * client_id) | ||
155 | { | ||
156 | uint32_t i; | ||
157 | for(i = 0; i < num_friends; ++i) { | ||
158 | if(memcmp(friends_list[i].client_id, client_id, CLIENT_ID_SIZE) == 0) /* Equal */ | ||
159 | { | ||
160 | return i; | ||
161 | } | ||
162 | } | ||
163 | return -1; | ||
164 | } | ||
164 | 165 | ||
165 | /* the number of seconds for a non responsive node to become bad. */ | 166 | /* the number of seconds for a non responsive node to become bad. */ |
166 | #define BAD_NODE_TIMEOUT 130 | 167 | #define BAD_NODE_TIMEOUT 70 |
167 | /* the max number of nodes to send with send nodes. */ | 168 | /* the max number of nodes to send with send nodes. */ |
168 | #define MAX_SENT_NODES 8 | 169 | #define MAX_SENT_NODES 8 |
169 | 170 | ||
170 | |||
171 | /* Find MAX_SENT_NODES nodes closest to the client_id for the send nodes request: | 171 | /* Find MAX_SENT_NODES nodes closest to the client_id for the send nodes request: |
172 | put them in the nodes_list and return how many were found. | 172 | put them in the nodes_list and return how many were found. |
173 | TODO: Make this function much more efficient. */ | 173 | TODO: Make this function much more efficient. */ |
@@ -176,48 +176,35 @@ int get_close_nodes(uint8_t * client_id, Node_format * nodes_list) | |||
176 | uint32_t i, j, k; | 176 | uint32_t i, j, k; |
177 | int num_nodes=0; | 177 | int num_nodes=0; |
178 | uint32_t temp_time = unix_time(); | 178 | uint32_t temp_time = unix_time(); |
179 | for(i = 0; i < LCLIENT_LIST; ++i) | 179 | for(i = 0; i < LCLIENT_LIST; ++i) { |
180 | { | ||
181 | if(close_clientlist[i].timestamp + BAD_NODE_TIMEOUT > temp_time && | 180 | if(close_clientlist[i].timestamp + BAD_NODE_TIMEOUT > temp_time && |
182 | !client_in_nodelist(nodes_list, MAX_SENT_NODES,close_clientlist[i].client_id)) | 181 | !client_in_nodelist(nodes_list, MAX_SENT_NODES,close_clientlist[i].client_id)) { |
183 | /* if node is good and not already in list. */ | 182 | /* if node is good and not already in list. */ |
184 | { | 183 | if(num_nodes < MAX_SENT_NODES) { |
185 | if(num_nodes < MAX_SENT_NODES) | ||
186 | { | ||
187 | memcpy(nodes_list[num_nodes].client_id, close_clientlist[i].client_id, CLIENT_ID_SIZE); | 184 | memcpy(nodes_list[num_nodes].client_id, close_clientlist[i].client_id, CLIENT_ID_SIZE); |
188 | nodes_list[num_nodes].ip_port = close_clientlist[i].ip_port; | 185 | nodes_list[num_nodes].ip_port = close_clientlist[i].ip_port; |
189 | num_nodes++; | 186 | num_nodes++; |
190 | } | 187 | } |
191 | else for(j = 0; j < MAX_SENT_NODES; ++j) | 188 | else for(j = 0; j < MAX_SENT_NODES; ++j) { |
192 | { | 189 | if(id_closest(client_id, nodes_list[j].client_id, close_clientlist[i].client_id) == 2) { |
193 | if(id_closest(client_id, nodes_list[j].client_id, close_clientlist[i].client_id) == 2) | ||
194 | { | ||
195 | memcpy(nodes_list[j].client_id, close_clientlist[i].client_id, CLIENT_ID_SIZE); | 190 | memcpy(nodes_list[j].client_id, close_clientlist[i].client_id, CLIENT_ID_SIZE); |
196 | nodes_list[j].ip_port = close_clientlist[i].ip_port; | 191 | nodes_list[j].ip_port = close_clientlist[i].ip_port; |
197 | break; | 192 | break; |
198 | } | 193 | } |
199 | } | 194 | } |
200 | } | 195 | } |
201 | |||
202 | } | 196 | } |
203 | for(i = 0; i < num_friends; ++i) | 197 | for(i = 0; i < num_friends; ++i) { |
204 | { | 198 | for(j = 0; j < MAX_FRIEND_CLIENTS; ++j) { |
205 | for(j = 0; j < MAX_FRIEND_CLIENTS; ++j) | ||
206 | { | ||
207 | if(friends_list[i].client_list[j].timestamp + BAD_NODE_TIMEOUT > temp_time && | 199 | if(friends_list[i].client_list[j].timestamp + BAD_NODE_TIMEOUT > temp_time && |
208 | !client_in_nodelist(nodes_list, MAX_SENT_NODES,friends_list[i].client_list[j].client_id)) | 200 | !client_in_nodelist(nodes_list, MAX_SENT_NODES,friends_list[i].client_list[j].client_id)) { |
209 | /* if node is good and not already in list. */ | 201 | /* if node is good and not already in list. */ |
210 | { | 202 | if(num_nodes < MAX_SENT_NODES) { |
211 | if(num_nodes < MAX_SENT_NODES) | ||
212 | { | ||
213 | memcpy(nodes_list[num_nodes].client_id, friends_list[i].client_list[j].client_id, CLIENT_ID_SIZE); | 203 | memcpy(nodes_list[num_nodes].client_id, friends_list[i].client_list[j].client_id, CLIENT_ID_SIZE); |
214 | nodes_list[num_nodes].ip_port = friends_list[i].client_list[j].ip_port; | 204 | nodes_list[num_nodes].ip_port = friends_list[i].client_list[j].ip_port; |
215 | num_nodes++; | 205 | num_nodes++; |
216 | } | 206 | } else for(k = 0; k < MAX_SENT_NODES; ++k) { |
217 | else for(k = 0; k < MAX_SENT_NODES; ++k) | 207 | if(id_closest(client_id, nodes_list[k].client_id, friends_list[i].client_list[j].client_id) == 2) { |
218 | { | ||
219 | if(id_closest(client_id, nodes_list[k].client_id, friends_list[i].client_list[j].client_id) == 2) | ||
220 | { | ||
221 | memcpy(nodes_list[k].client_id, friends_list[i].client_list[j].client_id, CLIENT_ID_SIZE); | 208 | memcpy(nodes_list[k].client_id, friends_list[i].client_list[j].client_id, CLIENT_ID_SIZE); |
222 | nodes_list[k].ip_port = friends_list[i].client_list[j].ip_port; | 209 | nodes_list[k].ip_port = friends_list[i].client_list[j].ip_port; |
223 | break; | 210 | break; |
@@ -227,12 +214,9 @@ int get_close_nodes(uint8_t * client_id, Node_format * nodes_list) | |||
227 | } | 214 | } |
228 | } | 215 | } |
229 | 216 | ||
230 | return num_nodes; | 217 | return num_nodes; |
231 | |||
232 | } | 218 | } |
233 | 219 | ||
234 | |||
235 | |||
236 | /* replace first bad (or empty) node with this one | 220 | /* replace first bad (or empty) node with this one |
237 | return 0 if successful | 221 | return 0 if successful |
238 | return 1 if not (list contains no bad nodes) */ | 222 | return 1 if not (list contains no bad nodes) */ |
@@ -240,10 +224,8 @@ int replace_bad(Client_data * list, uint32_t length, uint8_t * client_id, IP_Por | |||
240 | { | 224 | { |
241 | uint32_t i; | 225 | uint32_t i; |
242 | uint32_t temp_time = unix_time(); | 226 | uint32_t temp_time = unix_time(); |
243 | for(i = 0; i < length; ++i) | 227 | for(i = 0; i < length; ++i) { |
244 | { | 228 | if(list[i].timestamp + BAD_NODE_TIMEOUT < temp_time) /* if node is bad. */ { |
245 | if(list[i].timestamp + BAD_NODE_TIMEOUT < temp_time) /* if node is bad. */ | ||
246 | { | ||
247 | memcpy(list[i].client_id, client_id, CLIENT_ID_SIZE); | 229 | memcpy(list[i].client_id, client_id, CLIENT_ID_SIZE); |
248 | list[i].ip_port = ip_port; | 230 | list[i].ip_port = ip_port; |
249 | list[i].timestamp = temp_time; | 231 | list[i].timestamp = temp_time; |
@@ -263,10 +245,8 @@ int replace_good(Client_data * list, uint32_t length, uint8_t * client_id, IP_Po | |||
263 | uint32_t i; | 245 | uint32_t i; |
264 | uint32_t temp_time = unix_time(); | 246 | uint32_t temp_time = unix_time(); |
265 | 247 | ||
266 | for(i = 0; i < length; ++i) | 248 | for(i = 0; i < length; ++i) { |
267 | { | 249 | if(id_closest(comp_client_id, list[i].client_id, client_id) == 2) { |
268 | if(id_closest(comp_client_id, list[i].client_id, client_id) == 2) | ||
269 | { | ||
270 | memcpy(list[i].client_id, client_id, CLIENT_ID_SIZE); | 250 | memcpy(list[i].client_id, client_id, CLIENT_ID_SIZE); |
271 | list[i].ip_port = ip_port; | 251 | list[i].ip_port = ip_port; |
272 | list[i].timestamp = temp_time; | 252 | list[i].timestamp = temp_time; |
@@ -286,23 +266,16 @@ void addto_lists(IP_Port ip_port, uint8_t * client_id) | |||
286 | uint32_t i; | 266 | uint32_t i; |
287 | 267 | ||
288 | /* NOTE: current behavior if there are two clients with the same id is to replace the first ip by the second. */ | 268 | /* NOTE: current behavior if there are two clients with the same id is to replace the first ip by the second. */ |
289 | if(!client_in_list(close_clientlist, LCLIENT_LIST, client_id, ip_port)) | 269 | if(!client_in_list(close_clientlist, LCLIENT_LIST, client_id, ip_port)) { |
290 | { | ||
291 | 270 | ||
292 | if(replace_bad(close_clientlist, LCLIENT_LIST, client_id, ip_port)) | 271 | if(replace_bad(close_clientlist, LCLIENT_LIST, client_id, ip_port)) { |
293 | { | ||
294 | /* if we can't replace bad nodes we try replacing good ones */ | 272 | /* if we can't replace bad nodes we try replacing good ones */ |
295 | replace_good(close_clientlist, LCLIENT_LIST, client_id, ip_port, self_public_key); | 273 | replace_good(close_clientlist, LCLIENT_LIST, client_id, ip_port, self_public_key); |
296 | } | 274 | } |
297 | |||
298 | } | 275 | } |
299 | for(i = 0; i < num_friends; ++i) | 276 | for(i = 0; i < num_friends; ++i) { |
300 | { | 277 | if(!client_in_list(friends_list[i].client_list, MAX_FRIEND_CLIENTS, client_id, ip_port)) { |
301 | if(!client_in_list(friends_list[i].client_list, MAX_FRIEND_CLIENTS, client_id, ip_port)) | 278 | if(replace_bad(friends_list[i].client_list, MAX_FRIEND_CLIENTS, client_id, ip_port)) { |
302 | { | ||
303 | |||
304 | if(replace_bad(friends_list[i].client_list, MAX_FRIEND_CLIENTS, client_id, ip_port)) | ||
305 | { | ||
306 | /* if we can't replace bad nodes we try replacing good ones. */ | 279 | /* if we can't replace bad nodes we try replacing good ones. */ |
307 | replace_good(friends_list[i].client_list, MAX_FRIEND_CLIENTS, client_id, ip_port, friends_list[i].client_id); | 280 | replace_good(friends_list[i].client_list, MAX_FRIEND_CLIENTS, client_id, ip_port, friends_list[i].client_id); |
308 | } | 281 | } |
@@ -316,12 +289,9 @@ void returnedip_ports(IP_Port ip_port, uint8_t * client_id, uint8_t * nodeclient | |||
316 | { | 289 | { |
317 | uint32_t i, j; | 290 | uint32_t i, j; |
318 | uint32_t temp_time = unix_time(); | 291 | uint32_t temp_time = unix_time(); |
319 | if(memcmp(client_id, self_public_key, CLIENT_ID_SIZE) == 0) | 292 | if(memcmp(client_id, self_public_key, CLIENT_ID_SIZE) == 0) { |
320 | { | 293 | for(i = 0; i < LCLIENT_LIST; ++i) { |
321 | for(i = 0; i < LCLIENT_LIST; ++i) | 294 | if(memcmp(nodeclient_id, close_clientlist[i].client_id, CLIENT_ID_SIZE) == 0) { |
322 | { | ||
323 | if(memcmp(nodeclient_id, close_clientlist[i].client_id, CLIENT_ID_SIZE) == 0) | ||
324 | { | ||
325 | close_clientlist[i].ret_ip_port = ip_port; | 295 | close_clientlist[i].ret_ip_port = ip_port; |
326 | close_clientlist[i].ret_timestamp = temp_time; | 296 | close_clientlist[i].ret_timestamp = temp_time; |
327 | return; | 297 | return; |
@@ -329,14 +299,10 @@ void returnedip_ports(IP_Port ip_port, uint8_t * client_id, uint8_t * nodeclient | |||
329 | } | 299 | } |
330 | } | 300 | } |
331 | else | 301 | else |
332 | for(i = 0; i < num_friends; ++i) | 302 | for(i = 0; i < num_friends; ++i) { |
333 | { | 303 | if(memcmp(client_id, friends_list[i].client_id, CLIENT_ID_SIZE) == 0) { |
334 | if(memcmp(client_id, friends_list[i].client_id, CLIENT_ID_SIZE) == 0) | 304 | for(j = 0; j < MAX_FRIEND_CLIENTS; ++j) { |
335 | { | 305 | if(memcmp(nodeclient_id, friends_list[i].client_list[j].client_id, CLIENT_ID_SIZE) == 0) { |
336 | for(j = 0; j < MAX_FRIEND_CLIENTS; ++j) | ||
337 | { | ||
338 | if(memcmp(nodeclient_id, friends_list[i].client_list[j].client_id, CLIENT_ID_SIZE) == 0) | ||
339 | { | ||
340 | friends_list[i].client_list[j].ret_ip_port = ip_port; | 306 | friends_list[i].client_list[j].ret_ip_port = ip_port; |
341 | friends_list[i].client_list[j].ret_timestamp = temp_time; | 307 | friends_list[i].client_list[j].ret_timestamp = temp_time; |
342 | return; | 308 | return; |
@@ -348,6 +314,7 @@ void returnedip_ports(IP_Port ip_port, uint8_t * client_id, uint8_t * nodeclient | |||
348 | 314 | ||
349 | /* ping timeout in seconds */ | 315 | /* ping timeout in seconds */ |
350 | #define PING_TIMEOUT 5 | 316 | #define PING_TIMEOUT 5 |
317 | |||
351 | /* check if we are currently pinging an ip_port and/or a ping_id | 318 | /* check if we are currently pinging an ip_port and/or a ping_id |
352 | variables with values of zero will not be checked. | 319 | variables with values of zero will not be checked. |
353 | if we are already, return 1 | 320 | if we are already, return 1 |
@@ -359,28 +326,22 @@ int is_pinging(IP_Port ip_port, uint64_t ping_id) | |||
359 | uint8_t pinging; | 326 | uint8_t pinging; |
360 | uint32_t temp_time = unix_time(); | 327 | uint32_t temp_time = unix_time(); |
361 | 328 | ||
362 | for(i = 0; i < LPING_ARRAY; ++i ) | 329 | for(i = 0; i < LPING_ARRAY; ++i ) { |
363 | { | 330 | if((pings[i].timestamp + PING_TIMEOUT) > temp_time) { |
364 | if((pings[i].timestamp + PING_TIMEOUT) > temp_time) | ||
365 | { | ||
366 | pinging = 0; | 331 | pinging = 0; |
367 | if(ip_port.ip.i != 0) | 332 | if(ip_port.ip.i != 0) { |
368 | { | ||
369 | if(pings[i].ip_port.ip.i == ip_port.ip.i && | 333 | if(pings[i].ip_port.ip.i == ip_port.ip.i && |
370 | pings[i].ip_port.port == ip_port.port) | 334 | pings[i].ip_port.port == ip_port.port) { |
371 | { | ||
372 | ++pinging; | 335 | ++pinging; |
373 | } | 336 | } |
374 | } | 337 | } |
375 | if(ping_id != 0) | 338 | if(ping_id != 0) { |
376 | { | ||
377 | if(pings[i].ping_id == ping_id) | 339 | if(pings[i].ping_id == ping_id) |
378 | { | 340 | { |
379 | ++pinging; | 341 | ++pinging; |
380 | } | 342 | } |
381 | } | 343 | } |
382 | if(pinging == (ping_id != 0) + (ip_port.ip.i != 0)) | 344 | if(pinging == (ping_id != 0) + (ip_port.ip.i != 0)) { |
383 | { | ||
384 | return 1; | 345 | return 1; |
385 | } | 346 | } |
386 | 347 | ||
@@ -391,7 +352,6 @@ int is_pinging(IP_Port ip_port, uint64_t ping_id) | |||
391 | 352 | ||
392 | } | 353 | } |
393 | 354 | ||
394 | |||
395 | /* Same as last function but for get_node requests. */ | 355 | /* Same as last function but for get_node requests. */ |
396 | int is_gettingnodes(IP_Port ip_port, uint64_t ping_id) | 356 | int is_gettingnodes(IP_Port ip_port, uint64_t ping_id) |
397 | { | 357 | { |
@@ -399,28 +359,21 @@ int is_gettingnodes(IP_Port ip_port, uint64_t ping_id) | |||
399 | uint8_t pinging; | 359 | uint8_t pinging; |
400 | uint32_t temp_time = unix_time(); | 360 | uint32_t temp_time = unix_time(); |
401 | 361 | ||
402 | for(i = 0; i < LSEND_NODES_ARRAY; ++i ) | 362 | for(i = 0; i < LSEND_NODES_ARRAY; ++i ) { |
403 | { | 363 | if((send_nodes[i].timestamp + PING_TIMEOUT) > temp_time) { |
404 | if((send_nodes[i].timestamp + PING_TIMEOUT) > temp_time) | ||
405 | { | ||
406 | pinging = 0; | 364 | pinging = 0; |
407 | if(ip_port.ip.i != 0) | 365 | if(ip_port.ip.i != 0) { |
408 | { | ||
409 | if(send_nodes[i].ip_port.ip.i == ip_port.ip.i && | 366 | if(send_nodes[i].ip_port.ip.i == ip_port.ip.i && |
410 | send_nodes[i].ip_port.port == ip_port.port) | 367 | send_nodes[i].ip_port.port == ip_port.port) { |
411 | { | ||
412 | ++pinging; | 368 | ++pinging; |
413 | } | 369 | } |
414 | } | 370 | } |
415 | if(ping_id != 0) | 371 | if(ping_id != 0) { |
416 | { | 372 | if(send_nodes[i].ping_id == ping_id) { |
417 | if(send_nodes[i].ping_id == ping_id) | ||
418 | { | ||
419 | ++pinging; | 373 | ++pinging; |
420 | } | 374 | } |
421 | } | 375 | } |
422 | if(pinging == (ping_id != 0) + (ip_port.ip.i != 0)) | 376 | if(pinging == (ping_id != 0) + (ip_port.ip.i != 0)) { |
423 | { | ||
424 | return 1; | 377 | return 1; |
425 | } | 378 | } |
426 | 379 | ||
@@ -431,7 +384,6 @@ int is_gettingnodes(IP_Port ip_port, uint64_t ping_id) | |||
431 | 384 | ||
432 | } | 385 | } |
433 | 386 | ||
434 | |||
435 | /* Add a new ping request to the list of ping requests | 387 | /* Add a new ping request to the list of ping requests |
436 | returns the ping_id to put in the ping request | 388 | returns the ping_id to put in the ping request |
437 | returns 0 if problem. | 389 | returns 0 if problem. |
@@ -442,12 +394,9 @@ uint64_t add_pinging(IP_Port ip_port) | |||
442 | uint64_t ping_id = ((uint64_t)random_int() << 32) + random_int(); | 394 | uint64_t ping_id = ((uint64_t)random_int() << 32) + random_int(); |
443 | uint32_t temp_time = unix_time(); | 395 | uint32_t temp_time = unix_time(); |
444 | 396 | ||
445 | for(i = 0; i < PING_TIMEOUT; ++i ) | 397 | for(i = 0; i < PING_TIMEOUT; ++i ) { |
446 | { | 398 | for(j = 0; j < LPING_ARRAY; ++j ) { |
447 | for(j = 0; j < LPING_ARRAY; ++j ) | 399 | if((pings[j].timestamp + PING_TIMEOUT - i) < temp_time) { |
448 | { | ||
449 | if((pings[j].timestamp + PING_TIMEOUT - i) < temp_time) | ||
450 | { | ||
451 | pings[j].timestamp = temp_time; | 400 | pings[j].timestamp = temp_time; |
452 | pings[j].ip_port = ip_port; | 401 | pings[j].ip_port = ip_port; |
453 | pings[j].ping_id = ping_id; | 402 | pings[j].ping_id = ping_id; |
@@ -466,12 +415,9 @@ uint64_t add_gettingnodes(IP_Port ip_port) | |||
466 | uint64_t ping_id = ((uint64_t)random_int() << 32) + random_int(); | 415 | uint64_t ping_id = ((uint64_t)random_int() << 32) + random_int(); |
467 | uint32_t temp_time = unix_time(); | 416 | uint32_t temp_time = unix_time(); |
468 | 417 | ||
469 | for(i = 0; i < PING_TIMEOUT; ++i ) | 418 | for(i = 0; i < PING_TIMEOUT; ++i ) { |
470 | { | 419 | for(j = 0; j < LSEND_NODES_ARRAY; ++j ) { |
471 | for(j = 0; j < LSEND_NODES_ARRAY; ++j ) | 420 | if((send_nodes[j].timestamp + PING_TIMEOUT - i) < temp_time) { |
472 | { | ||
473 | if((send_nodes[j].timestamp + PING_TIMEOUT - i) < temp_time) | ||
474 | { | ||
475 | send_nodes[j].timestamp = temp_time; | 421 | send_nodes[j].timestamp = temp_time; |
476 | send_nodes[j].ip_port = ip_port; | 422 | send_nodes[j].ip_port = ip_port; |
477 | send_nodes[j].ping_id = ping_id; | 423 | send_nodes[j].ping_id = ping_id; |
@@ -483,25 +429,20 @@ uint64_t add_gettingnodes(IP_Port ip_port) | |||
483 | 429 | ||
484 | } | 430 | } |
485 | 431 | ||
486 | |||
487 | |||
488 | /* send a ping request | 432 | /* send a ping request |
489 | Ping request only works if none has been sent to that ip/port in the last 5 seconds. */ | 433 | Ping request only works if none has been sent to that ip/port in the last 5 seconds. */ |
490 | static int pingreq(IP_Port ip_port, uint8_t * public_key) | 434 | static int pingreq(IP_Port ip_port, uint8_t * public_key) |
491 | { | 435 | { |
492 | if(memcmp(public_key, self_public_key, CLIENT_ID_SIZE) == 0) /* check if packet is gonna be sent to ourself */ | 436 | if(memcmp(public_key, self_public_key, CLIENT_ID_SIZE) == 0) /* check if packet is gonna be sent to ourself */ { |
493 | { | ||
494 | return 1; | 437 | return 1; |
495 | } | 438 | } |
496 | 439 | ||
497 | if(is_pinging(ip_port, 0)) | 440 | if(is_pinging(ip_port, 0)) { |
498 | { | ||
499 | return 1; | 441 | return 1; |
500 | } | 442 | } |
501 | 443 | ||
502 | uint64_t ping_id = add_pinging(ip_port); | 444 | uint64_t ping_id = add_pinging(ip_port); |
503 | if(ping_id == 0) | 445 | if(ping_id == 0) { |
504 | { | ||
505 | return 1; | 446 | return 1; |
506 | } | 447 | } |
507 | 448 | ||
@@ -511,8 +452,7 @@ static int pingreq(IP_Port ip_port, uint8_t * public_key) | |||
511 | random_nonce(nonce); | 452 | random_nonce(nonce); |
512 | 453 | ||
513 | int len = encrypt_data(public_key, self_secret_key, nonce, (uint8_t *)&ping_id, sizeof(ping_id), encrypt); | 454 | int len = encrypt_data(public_key, self_secret_key, nonce, (uint8_t *)&ping_id, sizeof(ping_id), encrypt); |
514 | if(len != sizeof(ping_id) + ENCRYPTION_PADDING) | 455 | if(len != sizeof(ping_id) + ENCRYPTION_PADDING) { |
515 | { | ||
516 | return -1; | 456 | return -1; |
517 | } | 457 | } |
518 | data[0] = 0; | 458 | data[0] = 0; |
@@ -524,12 +464,11 @@ static int pingreq(IP_Port ip_port, uint8_t * public_key) | |||
524 | 464 | ||
525 | } | 465 | } |
526 | 466 | ||
527 | |||
528 | /* send a ping response */ | 467 | /* send a ping response */ |
529 | static int pingres(IP_Port ip_port, uint8_t * public_key, uint64_t ping_id) | 468 | static int pingres(IP_Port ip_port, uint8_t * public_key, uint64_t ping_id) |
530 | { | 469 | { |
531 | if(memcmp(public_key, self_public_key, CLIENT_ID_SIZE) == 0) /* check if packet is gonna be sent to ourself */ | 470 | /* check if packet is gonna be sent to ourself */ |
532 | { | 471 | if(memcmp(public_key, self_public_key, CLIENT_ID_SIZE) == 0) { |
533 | return 1; | 472 | return 1; |
534 | } | 473 | } |
535 | 474 | ||
@@ -539,8 +478,7 @@ static int pingres(IP_Port ip_port, uint8_t * public_key, uint64_t ping_id) | |||
539 | random_nonce(nonce); | 478 | random_nonce(nonce); |
540 | 479 | ||
541 | int len = encrypt_data(public_key, self_secret_key, nonce, (uint8_t *)&ping_id, sizeof(ping_id), encrypt); | 480 | int len = encrypt_data(public_key, self_secret_key, nonce, (uint8_t *)&ping_id, sizeof(ping_id), encrypt); |
542 | if(len != sizeof(ping_id) + ENCRYPTION_PADDING) | 481 | if(len != sizeof(ping_id) + ENCRYPTION_PADDING) { |
543 | { | ||
544 | return -1; | 482 | return -1; |
545 | } | 483 | } |
546 | data[0] = 1; | 484 | data[0] = 1; |
@@ -555,20 +493,18 @@ static int pingres(IP_Port ip_port, uint8_t * public_key, uint64_t ping_id) | |||
555 | /* send a getnodes request */ | 493 | /* send a getnodes request */ |
556 | static int getnodes(IP_Port ip_port, uint8_t * public_key, uint8_t * client_id) | 494 | static int getnodes(IP_Port ip_port, uint8_t * public_key, uint8_t * client_id) |
557 | { | 495 | { |
558 | if(memcmp(public_key, self_public_key, CLIENT_ID_SIZE) == 0) /* check if packet is gonna be sent to ourself */ | 496 | /* check if packet is gonna be sent to ourself */ |
559 | { | 497 | if(memcmp(public_key, self_public_key, CLIENT_ID_SIZE) == 0) { |
560 | return 1; | 498 | return 1; |
561 | } | 499 | } |
562 | 500 | ||
563 | if(is_gettingnodes(ip_port, 0)) | 501 | if(is_gettingnodes(ip_port, 0)) { |
564 | { | ||
565 | return 1; | 502 | return 1; |
566 | } | 503 | } |
567 | 504 | ||
568 | uint64_t ping_id = add_gettingnodes(ip_port); | 505 | uint64_t ping_id = add_gettingnodes(ip_port); |
569 | 506 | ||
570 | if(ping_id == 0) | 507 | if(ping_id == 0) { |
571 | { | ||
572 | return 1; | 508 | return 1; |
573 | } | 509 | } |
574 | 510 | ||
@@ -583,8 +519,7 @@ static int getnodes(IP_Port ip_port, uint8_t * public_key, uint8_t * client_id) | |||
583 | 519 | ||
584 | int len = encrypt_data(public_key, self_secret_key, nonce, plain, sizeof(ping_id) + CLIENT_ID_SIZE, encrypt); | 520 | int len = encrypt_data(public_key, self_secret_key, nonce, plain, sizeof(ping_id) + CLIENT_ID_SIZE, encrypt); |
585 | 521 | ||
586 | if(len != sizeof(ping_id) + CLIENT_ID_SIZE + ENCRYPTION_PADDING) | 522 | if(len != sizeof(ping_id) + CLIENT_ID_SIZE + ENCRYPTION_PADDING) { |
587 | { | ||
588 | return -1; | 523 | return -1; |
589 | } | 524 | } |
590 | data[0] = 2; | 525 | data[0] = 2; |
@@ -595,12 +530,10 @@ static int getnodes(IP_Port ip_port, uint8_t * public_key, uint8_t * client_id) | |||
595 | 530 | ||
596 | } | 531 | } |
597 | 532 | ||
598 | |||
599 | /* send a send nodes response */ | 533 | /* send a send nodes response */ |
600 | static int sendnodes(IP_Port ip_port, uint8_t * public_key, uint8_t * client_id, uint64_t ping_id) | 534 | static int sendnodes(IP_Port ip_port, uint8_t * public_key, uint8_t * client_id, uint64_t ping_id) |
601 | { | 535 | { |
602 | if(memcmp(public_key, self_public_key, CLIENT_ID_SIZE) == 0) /* check if packet is gonna be sent to ourself */ | 536 | if(memcmp(public_key, self_public_key, CLIENT_ID_SIZE) == 0) /* check if packet is gonna be sent to ourself */ { |
603 | { | ||
604 | return 1; | 537 | return 1; |
605 | } | 538 | } |
606 | 539 | ||
@@ -610,8 +543,7 @@ static int sendnodes(IP_Port ip_port, uint8_t * public_key, uint8_t * client_id, | |||
610 | Node_format nodes_list[MAX_SENT_NODES]; | 543 | Node_format nodes_list[MAX_SENT_NODES]; |
611 | int num_nodes = get_close_nodes(client_id, nodes_list); | 544 | int num_nodes = get_close_nodes(client_id, nodes_list); |
612 | 545 | ||
613 | if(num_nodes == 0) | 546 | if(num_nodes == 0) { |
614 | { | ||
615 | return 0; | 547 | return 0; |
616 | } | 548 | } |
617 | 549 | ||
@@ -626,8 +558,7 @@ static int sendnodes(IP_Port ip_port, uint8_t * public_key, uint8_t * client_id, | |||
626 | int len = encrypt_data(public_key, self_secret_key, nonce, plain, | 558 | int len = encrypt_data(public_key, self_secret_key, nonce, plain, |
627 | sizeof(ping_id) + num_nodes * sizeof(Node_format), encrypt); | 559 | sizeof(ping_id) + num_nodes * sizeof(Node_format), encrypt); |
628 | 560 | ||
629 | if(len != sizeof(ping_id) + num_nodes * sizeof(Node_format) + ENCRYPTION_PADDING) | 561 | if(len != sizeof(ping_id) + num_nodes * sizeof(Node_format) + ENCRYPTION_PADDING) { |
630 | { | ||
631 | return -1; | 562 | return -1; |
632 | } | 563 | } |
633 | 564 | ||
@@ -640,20 +571,17 @@ static int sendnodes(IP_Port ip_port, uint8_t * public_key, uint8_t * client_id, | |||
640 | 571 | ||
641 | } | 572 | } |
642 | 573 | ||
643 | |||
644 | |||
645 | /* Packet handling functions | 574 | /* Packet handling functions |
646 | One to handle each types of packets we receive | 575 | One to handle each types of packets we receive |
647 | return 0 if handled correctly, 1 if packet is bad. */ | 576 | return 0 if handled correctly, 1 if packet is bad. */ |
648 | int handle_pingreq(uint8_t * packet, uint32_t length, IP_Port source) | 577 | int handle_pingreq(uint8_t * packet, uint32_t length, IP_Port source) |
649 | { | 578 | { |
650 | uint64_t ping_id; | 579 | uint64_t ping_id; |
651 | if(length != 1 + CLIENT_ID_SIZE + crypto_box_NONCEBYTES + sizeof(ping_id) + ENCRYPTION_PADDING) | 580 | if(length != 1 + CLIENT_ID_SIZE + crypto_box_NONCEBYTES + sizeof(ping_id) + ENCRYPTION_PADDING) { |
652 | { | ||
653 | return 1; | 581 | return 1; |
654 | } | 582 | } |
655 | if(memcmp(packet + 1, self_public_key, CLIENT_ID_SIZE) == 0) /* check if packet is from ourself. */ | 583 | /* check if packet is from ourself. */ |
656 | { | 584 | if(memcmp(packet + 1, self_public_key, CLIENT_ID_SIZE) == 0) { |
657 | return 1; | 585 | return 1; |
658 | } | 586 | } |
659 | 587 | ||
@@ -662,8 +590,7 @@ int handle_pingreq(uint8_t * packet, uint32_t length, IP_Port source) | |||
662 | int len = decrypt_data(packet + 1, self_secret_key, packet + 1 + CLIENT_ID_SIZE, | 590 | int len = decrypt_data(packet + 1, self_secret_key, packet + 1 + CLIENT_ID_SIZE, |
663 | packet + 1 + CLIENT_ID_SIZE + crypto_box_NONCEBYTES, | 591 | packet + 1 + CLIENT_ID_SIZE + crypto_box_NONCEBYTES, |
664 | sizeof(ping_id) + ENCRYPTION_PADDING, (uint8_t *)&ping_id); | 592 | sizeof(ping_id) + ENCRYPTION_PADDING, (uint8_t *)&ping_id); |
665 | if(len != sizeof(ping_id)) | 593 | if(len != sizeof(ping_id)) { |
666 | { | ||
667 | return 1; | 594 | return 1; |
668 | } | 595 | } |
669 | 596 | ||
@@ -679,12 +606,10 @@ int handle_pingreq(uint8_t * packet, uint32_t length, IP_Port source) | |||
679 | int handle_pingres(uint8_t * packet, uint32_t length, IP_Port source) | 606 | int handle_pingres(uint8_t * packet, uint32_t length, IP_Port source) |
680 | { | 607 | { |
681 | uint64_t ping_id; | 608 | uint64_t ping_id; |
682 | if(length != 1 + CLIENT_ID_SIZE + crypto_box_NONCEBYTES + sizeof(ping_id) + ENCRYPTION_PADDING) | 609 | if(length != 1 + CLIENT_ID_SIZE + crypto_box_NONCEBYTES + sizeof(ping_id) + ENCRYPTION_PADDING) { |
683 | { | ||
684 | return 1; | 610 | return 1; |
685 | } | 611 | } |
686 | if(memcmp(packet + 1, self_public_key, CLIENT_ID_SIZE) == 0) /* check if packet is from ourself. */ | 612 | if(memcmp(packet + 1, self_public_key, CLIENT_ID_SIZE) == 0) /* check if packet is from ourself. */ { |
687 | { | ||
688 | return 1; | 613 | return 1; |
689 | } | 614 | } |
690 | 615 | ||
@@ -693,13 +618,11 @@ int handle_pingres(uint8_t * packet, uint32_t length, IP_Port source) | |||
693 | int len = decrypt_data(packet + 1, self_secret_key, packet + 1 + CLIENT_ID_SIZE, | 618 | int len = decrypt_data(packet + 1, self_secret_key, packet + 1 + CLIENT_ID_SIZE, |
694 | packet + 1 + CLIENT_ID_SIZE + crypto_box_NONCEBYTES, | 619 | packet + 1 + CLIENT_ID_SIZE + crypto_box_NONCEBYTES, |
695 | sizeof(ping_id) + ENCRYPTION_PADDING, (uint8_t *)&ping_id); | 620 | sizeof(ping_id) + ENCRYPTION_PADDING, (uint8_t *)&ping_id); |
696 | if(len != sizeof(ping_id)) | 621 | if(len != sizeof(ping_id)) { |
697 | { | ||
698 | return 1; | 622 | return 1; |
699 | } | 623 | } |
700 | 624 | ||
701 | if(is_pinging(source, ping_id)) | 625 | if(is_pinging(source, ping_id)) { |
702 | { | ||
703 | addto_lists(source, packet + 1); | 626 | addto_lists(source, packet + 1); |
704 | return 0; | 627 | return 0; |
705 | } | 628 | } |
@@ -710,12 +633,11 @@ int handle_pingres(uint8_t * packet, uint32_t length, IP_Port source) | |||
710 | int handle_getnodes(uint8_t * packet, uint32_t length, IP_Port source) | 633 | int handle_getnodes(uint8_t * packet, uint32_t length, IP_Port source) |
711 | { | 634 | { |
712 | uint64_t ping_id; | 635 | uint64_t ping_id; |
713 | if(length != 1 + CLIENT_ID_SIZE + crypto_box_NONCEBYTES + sizeof(ping_id) + CLIENT_ID_SIZE + ENCRYPTION_PADDING) | 636 | if(length != 1 + CLIENT_ID_SIZE + crypto_box_NONCEBYTES + sizeof(ping_id) + CLIENT_ID_SIZE + ENCRYPTION_PADDING) { |
714 | { | ||
715 | return 1; | 637 | return 1; |
716 | } | 638 | } |
717 | if(memcmp(packet + 1, self_public_key, CLIENT_ID_SIZE) == 0) /* check if packet is from ourself. */ | 639 | /* check if packet is from ourself. */ |
718 | { | 640 | if(memcmp(packet + 1, self_public_key, CLIENT_ID_SIZE) == 0) { |
719 | return 1; | 641 | return 1; |
720 | } | 642 | } |
721 | 643 | ||
@@ -725,8 +647,7 @@ int handle_getnodes(uint8_t * packet, uint32_t length, IP_Port source) | |||
725 | packet + 1 + CLIENT_ID_SIZE + crypto_box_NONCEBYTES, | 647 | packet + 1 + CLIENT_ID_SIZE + crypto_box_NONCEBYTES, |
726 | sizeof(ping_id) + CLIENT_ID_SIZE + ENCRYPTION_PADDING, plain); | 648 | sizeof(ping_id) + CLIENT_ID_SIZE + ENCRYPTION_PADDING, plain); |
727 | 649 | ||
728 | if(len != sizeof(ping_id) + CLIENT_ID_SIZE) | 650 | if(len != sizeof(ping_id) + CLIENT_ID_SIZE) { |
729 | { | ||
730 | return 1; | 651 | return 1; |
731 | } | 652 | } |
732 | 653 | ||
@@ -748,8 +669,7 @@ int handle_sendnodes(uint8_t * packet, uint32_t length, IP_Port source) | |||
748 | (length - (1 + CLIENT_ID_SIZE + crypto_box_NONCEBYTES + sizeof(ping_id) | 669 | (length - (1 + CLIENT_ID_SIZE + crypto_box_NONCEBYTES + sizeof(ping_id) |
749 | + ENCRYPTION_PADDING)) % (sizeof(Node_format)) != 0 || | 670 | + ENCRYPTION_PADDING)) % (sizeof(Node_format)) != 0 || |
750 | length < 1 + CLIENT_ID_SIZE + crypto_box_NONCEBYTES + sizeof(ping_id) | 671 | length < 1 + CLIENT_ID_SIZE + crypto_box_NONCEBYTES + sizeof(ping_id) |
751 | + sizeof(Node_format) + ENCRYPTION_PADDING) | 672 | + sizeof(Node_format) + ENCRYPTION_PADDING) { |
752 | { | ||
753 | return 1; | 673 | return 1; |
754 | } | 674 | } |
755 | uint32_t num_nodes = (length - (1 + CLIENT_ID_SIZE + crypto_box_NONCEBYTES | 675 | uint32_t num_nodes = (length - (1 + CLIENT_ID_SIZE + crypto_box_NONCEBYTES |
@@ -762,14 +682,12 @@ int handle_sendnodes(uint8_t * packet, uint32_t length, IP_Port source) | |||
762 | sizeof(ping_id) + num_nodes * sizeof(Node_format) + ENCRYPTION_PADDING, plain); | 682 | sizeof(ping_id) + num_nodes * sizeof(Node_format) + ENCRYPTION_PADDING, plain); |
763 | 683 | ||
764 | 684 | ||
765 | if(len != sizeof(ping_id) + num_nodes * sizeof(Node_format)) | 685 | if(len != sizeof(ping_id) + num_nodes * sizeof(Node_format)) { |
766 | { | ||
767 | return 1; | 686 | return 1; |
768 | } | 687 | } |
769 | 688 | ||
770 | memcpy(&ping_id, plain, sizeof(ping_id)); | 689 | memcpy(&ping_id, plain, sizeof(ping_id)); |
771 | if(!is_gettingnodes(source, ping_id)) | 690 | if(!is_gettingnodes(source, ping_id)) { |
772 | { | ||
773 | return 1; | 691 | return 1; |
774 | } | 692 | } |
775 | 693 | ||
@@ -779,8 +697,7 @@ int handle_sendnodes(uint8_t * packet, uint32_t length, IP_Port source) | |||
779 | addto_lists(source, packet + 1); | 697 | addto_lists(source, packet + 1); |
780 | 698 | ||
781 | uint32_t i; | 699 | uint32_t i; |
782 | for(i = 0; i < num_nodes; ++i) | 700 | for(i = 0; i < num_nodes; ++i) { |
783 | { | ||
784 | pingreq(nodes_list[i].ip_port, nodes_list[i].client_id); | 701 | pingreq(nodes_list[i].ip_port, nodes_list[i].client_id); |
785 | returnedip_ports(nodes_list[i].ip_port, nodes_list[i].client_id, packet + 1); | 702 | returnedip_ports(nodes_list[i].ip_port, nodes_list[i].client_id, packet + 1); |
786 | } | 703 | } |
@@ -793,44 +710,36 @@ int handle_sendnodes(uint8_t * packet, uint32_t length, IP_Port source) | |||
793 | int DHT_addfriend(uint8_t * client_id) | 710 | int DHT_addfriend(uint8_t * client_id) |
794 | { | 711 | { |
795 | Friend * temp; | 712 | Friend * temp; |
796 | if(num_friends == 0) | 713 | if(num_friends == 0) { |
797 | { | ||
798 | temp = malloc(sizeof(Friend)); | 714 | temp = malloc(sizeof(Friend)); |
799 | } | 715 | } else { |
800 | else | ||
801 | { | ||
802 | temp = realloc(friends_list, sizeof(Friend) * (num_friends + 1)); | 716 | temp = realloc(friends_list, sizeof(Friend) * (num_friends + 1)); |
803 | } | 717 | } |
804 | if(temp == NULL) | 718 | if(temp == NULL) { |
805 | { | ||
806 | return 1; | 719 | return 1; |
807 | } | 720 | } |
808 | 721 | ||
809 | friends_list = temp; | 722 | friends_list = temp; |
810 | memset(&friends_list[num_friends], 0, sizeof(Friend)); | 723 | memset(&friends_list[num_friends], 0, sizeof(Friend)); |
811 | memcpy(friends_list[num_friends].client_id, client_id, CLIENT_ID_SIZE); | 724 | memcpy(friends_list[num_friends].client_id, client_id, CLIENT_ID_SIZE); |
725 | friends_list[num_friends].NATping_id = ((uint64_t)random_int() << 32) + random_int(); | ||
812 | ++num_friends; | 726 | ++num_friends; |
813 | return 0; | 727 | return 0; |
814 | } | 728 | } |
815 | 729 | ||
816 | |||
817 | |||
818 | int DHT_delfriend(uint8_t * client_id) | 730 | int DHT_delfriend(uint8_t * client_id) |
819 | { | 731 | { |
820 | uint32_t i; | 732 | uint32_t i; |
821 | Friend * temp; | 733 | Friend * temp; |
822 | for(i = 0; i < num_friends; ++i) | 734 | for(i = 0; i < num_friends; ++i) { |
823 | { | 735 | /* Equal */ |
824 | if(memcmp(friends_list[i].client_id, client_id, CLIENT_ID_SIZE) == 0) /* Equal */ | 736 | if(memcmp(friends_list[i].client_id, client_id, CLIENT_ID_SIZE) == 0){ |
825 | { | ||
826 | --num_friends; | 737 | --num_friends; |
827 | if(num_friends != i) | 738 | if(num_friends != i) { |
828 | { | ||
829 | memcpy(friends_list[i].client_id, friends_list[num_friends].client_id, CLIENT_ID_SIZE); | 739 | memcpy(friends_list[i].client_id, friends_list[num_friends].client_id, CLIENT_ID_SIZE); |
830 | } | 740 | } |
831 | temp = realloc(friends_list, sizeof(Friend) * (num_friends)); | 741 | temp = realloc(friends_list, sizeof(Friend) * (num_friends)); |
832 | if(temp != NULL) | 742 | if(temp != NULL) { |
833 | { | ||
834 | friends_list = temp; | 743 | friends_list = temp; |
835 | } | 744 | } |
836 | return 0; | 745 | return 0; |
@@ -839,61 +748,27 @@ int DHT_delfriend(uint8_t * client_id) | |||
839 | return 1; | 748 | return 1; |
840 | } | 749 | } |
841 | 750 | ||
842 | |||
843 | |||
844 | |||
845 | /* TODO: Optimize this. */ | 751 | /* TODO: Optimize this. */ |
846 | IP_Port DHT_getfriendip(uint8_t * client_id) | 752 | IP_Port DHT_getfriendip(uint8_t * client_id) |
847 | { | 753 | { |
848 | uint32_t i, j; | 754 | uint32_t i, j; |
849 | IP_Port empty = {{{0}}, 0}; | 755 | IP_Port empty = {{{0}}, 0}; |
850 | uint32_t temp_time = unix_time(); | 756 | uint32_t temp_time = unix_time(); |
851 | for(i = 0; i < num_friends; ++i) | 757 | for(i = 0; i < num_friends; ++i) { |
852 | { | 758 | /* Equal */ |
853 | if(memcmp(friends_list[i].client_id, client_id, CLIENT_ID_SIZE) == 0) /* Equal */ | 759 | if(memcmp(friends_list[i].client_id, client_id, CLIENT_ID_SIZE) == 0) { |
854 | { | 760 | for(j = 0; j < MAX_FRIEND_CLIENTS; ++j) { |
855 | for(j = 0; j < MAX_FRIEND_CLIENTS; ++j) | ||
856 | { | ||
857 | if(memcmp(friends_list[i].client_list[j].client_id, client_id, CLIENT_ID_SIZE) == 0 && | 761 | if(memcmp(friends_list[i].client_list[j].client_id, client_id, CLIENT_ID_SIZE) == 0 && |
858 | friends_list[i].client_list[j].timestamp + BAD_NODE_TIMEOUT > temp_time) | 762 | friends_list[i].client_list[j].timestamp + BAD_NODE_TIMEOUT > temp_time) { |
859 | { | ||
860 | return friends_list[i].client_list[j].ip_port; | 763 | return friends_list[i].client_list[j].ip_port; |
861 | } | 764 | } |
862 | 765 | } | |
863 | } | ||
864 | |||
865 | return empty; | 766 | return empty; |
866 | } | 767 | } |
867 | } | 768 | } |
868 | empty.ip.i = 1; | 769 | empty.ip.i = 1; |
869 | return empty; | 770 | return empty; |
870 | 771 | ||
871 | } | ||
872 | |||
873 | |||
874 | |||
875 | int DHT_handlepacket(uint8_t * packet, uint32_t length, IP_Port source) | ||
876 | { | ||
877 | switch (packet[0]) { | ||
878 | case 0: | ||
879 | return handle_pingreq(packet, length, source); | ||
880 | |||
881 | case 1: | ||
882 | return handle_pingres(packet, length, source); | ||
883 | |||
884 | case 2: | ||
885 | return handle_getnodes(packet, length, source); | ||
886 | |||
887 | case 3: | ||
888 | return handle_sendnodes(packet, length, source); | ||
889 | |||
890 | default: | ||
891 | return 1; | ||
892 | |||
893 | } | ||
894 | |||
895 | return 0; | ||
896 | |||
897 | } | 772 | } |
898 | 773 | ||
899 | /* The timeout after which a node is discarded completely. */ | 774 | /* The timeout after which a node is discarded completely. */ |
@@ -907,8 +782,6 @@ int DHT_handlepacket(uint8_t * packet, uint32_t length, IP_Port source) | |||
907 | 782 | ||
908 | /* Ping each client in the "friends" list every 60 seconds. | 783 | /* Ping each client in the "friends" list every 60 seconds. |
909 | Send a get nodes request every 20 seconds to a random good node for each "friend" in our "friends" list. */ | 784 | Send a get nodes request every 20 seconds to a random good node for each "friend" in our "friends" list. */ |
910 | |||
911 | |||
912 | void doDHTFriends() | 785 | void doDHTFriends() |
913 | { | 786 | { |
914 | uint32_t i, j; | 787 | uint32_t i, j; |
@@ -916,27 +789,21 @@ void doDHTFriends() | |||
916 | uint32_t rand_node; | 789 | uint32_t rand_node; |
917 | uint32_t index[MAX_FRIEND_CLIENTS]; | 790 | uint32_t index[MAX_FRIEND_CLIENTS]; |
918 | 791 | ||
919 | for(i = 0; i < num_friends; ++i) | 792 | for(i = 0; i < num_friends; ++i) { |
920 | { | ||
921 | uint32_t num_nodes = 0; | 793 | uint32_t num_nodes = 0; |
922 | for(j = 0; j < MAX_FRIEND_CLIENTS; ++j) | 794 | for(j = 0; j < MAX_FRIEND_CLIENTS; ++j) { |
923 | { | 795 | if(friends_list[i].client_list[j].timestamp + Kill_NODE_TIMEOUT > temp_time) /* if node is not dead. */ { |
924 | if(friends_list[i].client_list[j].timestamp + Kill_NODE_TIMEOUT > temp_time) /* if node is not dead. */ | 796 | if((friends_list[i].client_list[j].last_pinged + PING_INTERVAL) <= temp_time) { |
925 | { | ||
926 | if((friends_list[i].client_list[j].last_pinged + PING_INTERVAL) <= temp_time) | ||
927 | { | ||
928 | pingreq(friends_list[i].client_list[j].ip_port, friends_list[i].client_list[j].client_id); | 797 | pingreq(friends_list[i].client_list[j].ip_port, friends_list[i].client_list[j].client_id); |
929 | friends_list[i].client_list[j].last_pinged = temp_time; | 798 | friends_list[i].client_list[j].last_pinged = temp_time; |
930 | } | 799 | } |
931 | if(friends_list[i].client_list[j].timestamp + BAD_NODE_TIMEOUT > temp_time) /* if node is good. */ | 800 | if(friends_list[i].client_list[j].timestamp + BAD_NODE_TIMEOUT > temp_time) /* if node is good. */ { |
932 | { | ||
933 | index[num_nodes] = j; | 801 | index[num_nodes] = j; |
934 | ++num_nodes; | 802 | ++num_nodes; |
935 | } | 803 | } |
936 | } | 804 | } |
937 | } | 805 | } |
938 | if(friends_list[i].lastgetnode + GET_NODE_INTERVAL <= temp_time && num_nodes != 0) | 806 | if(friends_list[i].lastgetnode + GET_NODE_INTERVAL <= temp_time && num_nodes != 0) { |
939 | { | ||
940 | rand_node = rand() % num_nodes; | 807 | rand_node = rand() % num_nodes; |
941 | getnodes(friends_list[i].client_list[index[rand_node]].ip_port, | 808 | getnodes(friends_list[i].client_list[index[rand_node]].ip_port, |
942 | friends_list[i].client_list[index[rand_node]].client_id, | 809 | friends_list[i].client_list[index[rand_node]].client_id, |
@@ -958,66 +825,77 @@ void doClose() /* tested */ | |||
958 | uint32_t rand_node; | 825 | uint32_t rand_node; |
959 | uint32_t index[LCLIENT_LIST]; | 826 | uint32_t index[LCLIENT_LIST]; |
960 | 827 | ||
961 | for(i = 0; i < LCLIENT_LIST; ++i) | 828 | for(i = 0; i < LCLIENT_LIST; ++i) { |
962 | { | 829 | /* if node is not dead. */ |
963 | if(close_clientlist[i].timestamp + Kill_NODE_TIMEOUT > temp_time) /* if node is not dead. */ | 830 | if(close_clientlist[i].timestamp + Kill_NODE_TIMEOUT > temp_time) { |
964 | { | ||
965 | if((close_clientlist[i].last_pinged + PING_INTERVAL) <= temp_time) | 831 | if((close_clientlist[i].last_pinged + PING_INTERVAL) <= temp_time) |
966 | { | 832 | { |
967 | pingreq(close_clientlist[i].ip_port, close_clientlist[i].client_id); | 833 | pingreq(close_clientlist[i].ip_port, close_clientlist[i].client_id); |
968 | close_clientlist[i].last_pinged = temp_time; | 834 | close_clientlist[i].last_pinged = temp_time; |
969 | } | 835 | } |
970 | if(close_clientlist[i].timestamp + BAD_NODE_TIMEOUT > temp_time) /* if node is good. */ | 836 | /* if node is good. */ |
971 | { | 837 | if(close_clientlist[i].timestamp + BAD_NODE_TIMEOUT > temp_time) { |
972 | index[num_nodes] = i; | 838 | index[num_nodes] = i; |
973 | ++num_nodes; | 839 | ++num_nodes; |
974 | } | 840 | } |
975 | } | 841 | } |
976 | } | 842 | } |
977 | 843 | ||
978 | if(close_lastgetnodes + GET_NODE_INTERVAL <= temp_time && num_nodes != 0) | 844 | if(close_lastgetnodes + GET_NODE_INTERVAL <= temp_time && num_nodes != 0) { |
979 | { | ||
980 | rand_node = rand() % num_nodes; | 845 | rand_node = rand() % num_nodes; |
981 | getnodes(close_clientlist[index[rand_node]].ip_port, | 846 | getnodes(close_clientlist[index[rand_node]].ip_port, |
982 | close_clientlist[index[rand_node]].client_id, | 847 | close_clientlist[index[rand_node]].client_id, |
983 | self_public_key); | 848 | self_public_key); |
984 | close_lastgetnodes = temp_time; | 849 | close_lastgetnodes = temp_time; |
985 | } | 850 | } |
986 | |||
987 | } | ||
988 | |||
989 | |||
990 | |||
991 | void doDHT() | ||
992 | { | ||
993 | doClose(); | ||
994 | doDHTFriends(); | ||
995 | } | 851 | } |
996 | 852 | ||
997 | |||
998 | |||
999 | void DHT_bootstrap(IP_Port ip_port, uint8_t * public_key) | 853 | void DHT_bootstrap(IP_Port ip_port, uint8_t * public_key) |
1000 | { | 854 | { |
1001 | getnodes(ip_port, public_key, self_public_key); | 855 | getnodes(ip_port, public_key, self_public_key); |
1002 | } | 856 | } |
1003 | 857 | ||
1004 | |||
1005 | |||
1006 | /* send the given packet to node with client_id | 858 | /* send the given packet to node with client_id |
1007 | returns -1 if failure */ | 859 | returns -1 if failure */ |
1008 | int route_packet(uint8_t * client_id, uint8_t * packet, uint32_t length) | 860 | int route_packet(uint8_t * client_id, uint8_t * packet, uint32_t length) |
1009 | { | 861 | { |
1010 | uint32_t i; | 862 | uint32_t i; |
1011 | for(i = 0; i < LCLIENT_LIST; ++i) | 863 | for(i = 0; i < LCLIENT_LIST; ++i) { |
1012 | { | 864 | if(memcmp(client_id, close_clientlist[i].client_id, CLIENT_ID_SIZE) == 0) { |
1013 | if(memcmp(client_id, close_clientlist[i].client_id, CLIENT_ID_SIZE) == 0) | ||
1014 | { | ||
1015 | return sendpacket(close_clientlist[i].ip_port, packet, length); | 865 | return sendpacket(close_clientlist[i].ip_port, packet, length); |
1016 | } | 866 | } |
1017 | } | 867 | } |
1018 | return -1; | 868 | return -1; |
1019 | } | 869 | } |
1020 | 870 | ||
871 | /* Puts all the different ips returned by the nodes for a friend_num into array ip_portlist | ||
872 | ip_portlist must be at least MAX_FRIEND_CLIENTS big | ||
873 | returns the number of ips returned | ||
874 | return 0 if we are connected to friend or if no ips were found. | ||
875 | returns -1 if no such friend*/ | ||
876 | static int friend_iplist(IP_Port * ip_portlist, uint16_t friend_num) | ||
877 | { | ||
878 | int num_ips = 0; | ||
879 | uint32_t i; | ||
880 | uint32_t temp_time = unix_time(); | ||
881 | if(friend_num >= num_friends) { | ||
882 | return -1; | ||
883 | } | ||
884 | for(i = 0; i < MAX_FRIEND_CLIENTS; ++i) | ||
885 | { | ||
886 | /*If ip is not zero and node is good */ | ||
887 | if(friends_list[friend_num].client_list[i].ret_ip_port.ip.i != 0 && | ||
888 | friends_list[friend_num].client_list[i].ret_timestamp + BAD_NODE_TIMEOUT > temp_time) { | ||
889 | if(memcmp(friends_list[friend_num].client_list[i].client_id, friends_list[friend_num].client_id, CLIENT_ID_SIZE) == 0 ) { | ||
890 | return 0; | ||
891 | } | ||
892 | ip_portlist[num_ips] = friends_list[friend_num].client_list[i].ret_ip_port; | ||
893 | ++num_ips; | ||
894 | } | ||
895 | } | ||
896 | return num_ips; | ||
897 | } | ||
898 | |||
1021 | /* Send the following packet to everyone who tells us they are connected to friend_id | 899 | /* Send the following packet to everyone who tells us they are connected to friend_id |
1022 | returns the number of nodes it sent the packet to */ | 900 | returns the number of nodes it sent the packet to */ |
1023 | int route_tofriend(uint8_t * friend_id, uint8_t * packet, uint32_t length) | 901 | int route_tofriend(uint8_t * friend_id, uint8_t * packet, uint32_t length) |
@@ -1025,18 +903,14 @@ int route_tofriend(uint8_t * friend_id, uint8_t * packet, uint32_t length) | |||
1025 | uint32_t i, j; | 903 | uint32_t i, j; |
1026 | uint32_t sent = 0; | 904 | uint32_t sent = 0; |
1027 | uint32_t temp_time = unix_time(); | 905 | uint32_t temp_time = unix_time(); |
1028 | for(i = 0; i < num_friends; ++i) | 906 | for(i = 0; i < num_friends; ++i) { |
1029 | { | 907 | /* Equal */ |
1030 | if(memcmp(friends_list[i].client_id, friend_id, CLIENT_ID_SIZE) == 0) /* Equal */ | 908 | if(memcmp(friends_list[i].client_id, friend_id, CLIENT_ID_SIZE) == 0) { |
1031 | { | 909 | for(j = 0; j < MAX_FRIEND_CLIENTS; ++j) { |
1032 | for(j = 0; j < MAX_FRIEND_CLIENTS; ++j) | 910 | /*If ip is not zero and node is good */ |
1033 | { | ||
1034 | if(friends_list[i].client_list[j].ret_ip_port.ip.i != 0 && | 911 | if(friends_list[i].client_list[j].ret_ip_port.ip.i != 0 && |
1035 | friends_list[i].client_list[j].ret_timestamp + BAD_NODE_TIMEOUT > temp_time) | 912 | friends_list[i].client_list[j].ret_timestamp + BAD_NODE_TIMEOUT > temp_time) { |
1036 | /*If ip is not zero and node is good */ | 913 | if(sendpacket(friends_list[i].client_list[j].ip_port, packet, length) == length) { |
1037 | { | ||
1038 | if(sendpacket(friends_list[i].client_list[j].ip_port, packet, length) == length) | ||
1039 | { | ||
1040 | ++sent; | 914 | ++sent; |
1041 | } | 915 | } |
1042 | } | 916 | } |
@@ -1047,34 +921,255 @@ int route_tofriend(uint8_t * friend_id, uint8_t * packet, uint32_t length) | |||
1047 | return 0; | 921 | return 0; |
1048 | } | 922 | } |
1049 | 923 | ||
924 | /* Send the following packet to one random person who tells us they are connected to friend_id | ||
925 | returns the number of nodes it sent the packet to */ | ||
926 | int routeone_tofriend(uint8_t * friend_id, uint8_t * packet, uint32_t length) | ||
927 | { | ||
928 | int num = friend_number(friend_id); | ||
929 | if(num == -1) { | ||
930 | return 0; | ||
931 | } | ||
932 | |||
933 | IP_Port ip_list[MAX_FRIEND_CLIENTS]; | ||
934 | int n = 0; | ||
935 | uint32_t i; | ||
936 | uint32_t temp_time = unix_time(); | ||
937 | for(i = 0; i < MAX_FRIEND_CLIENTS; ++i) { | ||
938 | /*If ip is not zero and node is good */ | ||
939 | if(friends_list[num].client_list[i].ret_ip_port.ip.i != 0 && | ||
940 | friends_list[num].client_list[i].ret_timestamp + BAD_NODE_TIMEOUT > temp_time) { | ||
941 | ip_list[n] = friends_list[num].client_list[i].ip_port; | ||
942 | ++n; | ||
943 | } | ||
944 | } | ||
945 | if(n < 1) { | ||
946 | return 0; | ||
947 | } | ||
948 | if(sendpacket(ip_list[rand() % n], packet, length) == length) { | ||
949 | return 1; | ||
950 | } | ||
951 | return 0; | ||
952 | } | ||
953 | |||
1050 | /* Puts all the different ips returned by the nodes for a friend_id into array ip_portlist | 954 | /* Puts all the different ips returned by the nodes for a friend_id into array ip_portlist |
1051 | ip_portlist must be at least MAX_FRIEND_CLIENTS big | 955 | ip_portlist must be at least MAX_FRIEND_CLIENTS big |
1052 | returns the number of ips returned | 956 | returns the number of ips returned |
957 | return 0 if we are connected to friend or if no ips were found. | ||
1053 | returns -1 if no such friend*/ | 958 | returns -1 if no such friend*/ |
1054 | int friend_ips(IP_Port * ip_portlist, uint8_t * friend_id) | 959 | int friend_ips(IP_Port * ip_portlist, uint8_t * friend_id) |
1055 | { | 960 | { |
1056 | int num_ips = 0; | 961 | |
962 | uint32_t i; | ||
963 | for(i = 0; i < num_friends; ++i) { | ||
964 | /* Equal */ | ||
965 | if(memcmp(friends_list[i].client_id, friend_id, CLIENT_ID_SIZE) == 0) { | ||
966 | return friend_iplist(ip_portlist, i); | ||
967 | } | ||
968 | } | ||
969 | return -1; | ||
970 | } | ||
971 | |||
972 | /*BEGINNING OF NAT PUNCHING FUNCTIONS*/ | ||
973 | |||
974 | int send_NATping(uint8_t * public_key, uint64_t ping_id, uint8_t type) | ||
975 | { | ||
976 | uint8_t data[sizeof(uint64_t) + 1]; | ||
977 | data[0] = type; | ||
978 | memcpy(data + 1, &ping_id, sizeof(uint64_t)); | ||
979 | |||
980 | uint8_t packet[MAX_DATA_SIZE]; | ||
981 | int len = create_request(packet, public_key, data, sizeof(uint64_t) + 1, 254); /* 254 is NAT ping request packet id */ | ||
982 | if(len == -1) { | ||
983 | return -1; | ||
984 | } | ||
985 | int num = 0; | ||
986 | if(type == 0) { | ||
987 | num = route_tofriend(public_key, packet, len);/*If packet is request use many people to route it*/ | ||
988 | } | ||
989 | else if(type == 1) { | ||
990 | num = routeone_tofriend(public_key, packet, len);/*If packet is response use only one person to route it*/ | ||
991 | } | ||
992 | if(num == 0) { | ||
993 | return -1; | ||
994 | } | ||
995 | return num; | ||
996 | } | ||
997 | |||
998 | /* Handle a recieved ping request for */ | ||
999 | int handle_NATping(uint8_t * packet, uint32_t length, IP_Port source) | ||
1000 | { | ||
1001 | if(length <= crypto_box_PUBLICKEYBYTES * 2 + crypto_box_NONCEBYTES + 1 + ENCRYPTION_PADDING && | ||
1002 | length > MAX_DATA_SIZE + ENCRYPTION_PADDING) { | ||
1003 | return 1; | ||
1004 | } | ||
1005 | /* check if request is for us. */ | ||
1006 | if(memcmp(packet + 1, self_public_key, crypto_box_PUBLICKEYBYTES) == 0) { | ||
1007 | uint8_t public_key[crypto_box_PUBLICKEYBYTES]; | ||
1008 | uint8_t data[MAX_DATA_SIZE]; | ||
1009 | int len = handle_request(public_key, data, packet, length); | ||
1010 | if(len != sizeof(uint64_t) + 1) { | ||
1011 | return 1; | ||
1012 | } | ||
1013 | uint64_t ping_id; | ||
1014 | memcpy(&ping_id, data + 1, sizeof(uint64_t)); | ||
1015 | |||
1016 | int friendnumber = friend_number(public_key); | ||
1017 | if(friendnumber == -1) { | ||
1018 | return 1; | ||
1019 | } | ||
1020 | |||
1021 | if(data[0] == 0) { | ||
1022 | send_NATping(public_key, ping_id, 1);/*1 is reply*/ | ||
1023 | return 0; | ||
1024 | } else if (data[0] == 1) { | ||
1025 | if(friends_list[friendnumber].NATping_id == ping_id) | ||
1026 | { | ||
1027 | friends_list[friendnumber].NATping_id = ((uint64_t)random_int() << 32) + random_int(); | ||
1028 | friends_list[friendnumber].hole_punching = 1; | ||
1029 | return 0; | ||
1030 | } | ||
1031 | } | ||
1032 | return 1; | ||
1033 | } | ||
1034 | /* if request is not for us, try routing it. */ | ||
1035 | else { | ||
1036 | if(route_packet(packet + 1, packet, length) == length) { | ||
1037 | return 0; | ||
1038 | } | ||
1039 | } | ||
1040 | return 0; | ||
1041 | } | ||
1042 | |||
1043 | /*Get the most common ip in the ip_portlist | ||
1044 | Only return ip if it appears in list min_num or more | ||
1045 | len must not be bigger than MAX_FRIEND_CLIENTS | ||
1046 | return ip of 0 if failure */ | ||
1047 | static IP NAT_commonip(IP_Port * ip_portlist, uint16_t len, uint16_t min_num) | ||
1048 | { | ||
1049 | IP zero = {{0}}; | ||
1050 | if(len > MAX_FRIEND_CLIENTS) { | ||
1051 | return zero; | ||
1052 | } | ||
1053 | |||
1057 | uint32_t i, j; | 1054 | uint32_t i, j; |
1055 | uint16_t numbers[MAX_FRIEND_CLIENTS] = {0}; | ||
1056 | for(i = 0; i < len; ++i) { | ||
1057 | for(j = 0; j < len; ++j) { | ||
1058 | if(ip_portlist[i].ip.i == ip_portlist[j].ip.i) { | ||
1059 | ++numbers[i]; | ||
1060 | } | ||
1061 | } | ||
1062 | if(numbers[i] >= min_num) { | ||
1063 | return ip_portlist[i].ip; | ||
1064 | } | ||
1065 | } | ||
1066 | return zero; | ||
1067 | } | ||
1068 | |||
1069 | /*Return all the ports for one ip in a list | ||
1070 | portlist must be at least len long | ||
1071 | where len is the length of ip_portlist | ||
1072 | returns the number of ports and puts the list of ports in portlist*/ | ||
1073 | static uint16_t NAT_getports(uint16_t * portlist, IP_Port * ip_portlist, uint16_t len, IP ip) | ||
1074 | { | ||
1075 | uint32_t i; | ||
1076 | uint16_t num = 0; | ||
1077 | for(i = 0; i < len; ++i) { | ||
1078 | if(ip_portlist[i].ip.i == ip.i) { | ||
1079 | portlist[num] = ntohs(ip_portlist[i].port); | ||
1080 | ++num; | ||
1081 | } | ||
1082 | } | ||
1083 | return num; | ||
1084 | } | ||
1085 | |||
1086 | #define MAX_PUNCHING_PORTS 32 | ||
1087 | |||
1088 | static void punch_holes(IP ip, uint16_t * port_list, uint16_t numports, uint16_t friend_num) | ||
1089 | { | ||
1090 | if(numports > MAX_FRIEND_CLIENTS || numports == 0) { | ||
1091 | return; | ||
1092 | } | ||
1093 | uint32_t i; | ||
1094 | uint32_t top = friends_list[friend_num].punching_index + MAX_PUNCHING_PORTS; | ||
1095 | for(i = friends_list[friend_num].punching_index; i != top; i++) { | ||
1096 | /*TODO: improve port guessing algorithm*/ | ||
1097 | uint16_t port = port_list[(i/2) % numports] + (i/(2*numports))*((i % 2) ? -1 : 1); | ||
1098 | IP_Port pinging = {ip, htons(port)}; | ||
1099 | pingreq(pinging, friends_list[friend_num].client_id); | ||
1100 | } | ||
1101 | friends_list[friend_num].punching_index = i; | ||
1102 | } | ||
1103 | |||
1104 | /*Interval in seconds between punching attempts*/ | ||
1105 | #define PUNCH_INTERVAL 10 | ||
1106 | |||
1107 | static void doNAT() | ||
1108 | { | ||
1109 | uint32_t i; | ||
1058 | uint32_t temp_time = unix_time(); | 1110 | uint32_t temp_time = unix_time(); |
1059 | for(i = 0; i < num_friends; ++i) | 1111 | for(i = 0; i < num_friends; ++i) { |
1060 | { | 1112 | IP_Port ip_list[MAX_FRIEND_CLIENTS]; |
1061 | if(memcmp(friends_list[i].client_id, friend_id, CLIENT_ID_SIZE) == 0) /* Equal */ | 1113 | int num = friend_iplist(ip_list, i); |
1062 | { | 1114 | /*If we are connected to friend or if friend is not online don't try to hole punch with him*/ |
1063 | for(j = 0; j < MAX_FRIEND_CLIENTS; ++j) | 1115 | if(num < MAX_FRIEND_CLIENTS/2) { |
1064 | { | 1116 | continue; |
1065 | if(friends_list[i].client_list[j].ret_ip_port.ip.i != 0 && | 1117 | } |
1066 | friends_list[i].client_list[j].ret_timestamp + BAD_NODE_TIMEOUT > temp_time) | 1118 | if(friends_list[i].hole_punching != 1) { |
1067 | /*If ip is not zero and node is good */ | 1119 | if(friends_list[i].NATping_timestamp + PUNCH_INTERVAL < temp_time) { |
1068 | { | 1120 | send_NATping(friends_list[i].client_id, friends_list[i].NATping_id, 0); /*0 is request*/ |
1069 | ip_portlist[num_ips] = friends_list[i].client_list[j].ret_ip_port; | 1121 | friends_list[i].NATping_timestamp = temp_time; |
1070 | ++num_ips; | ||
1071 | } | ||
1072 | } | 1122 | } |
1073 | return num_ips; | ||
1074 | } | 1123 | } |
1124 | else if(friends_list[i].punching_timestamp + PUNCH_INTERVAL < temp_time) { | ||
1125 | IP ip = NAT_commonip(ip_list, num, MAX_FRIEND_CLIENTS/2); | ||
1126 | if(ip.i == 0) { | ||
1127 | continue; | ||
1128 | } | ||
1129 | uint16_t port_list[MAX_FRIEND_CLIENTS]; | ||
1130 | uint16_t numports = NAT_getports(port_list, ip_list, num, ip); | ||
1131 | punch_holes(ip, port_list, numports, i); | ||
1132 | |||
1133 | friends_list[i].punching_timestamp = temp_time; | ||
1134 | friends_list[i].hole_punching = 0; | ||
1135 | } | ||
1136 | } | ||
1137 | } | ||
1138 | |||
1139 | /*END OF NAT PUNCHING FUNCTIONS*/ | ||
1140 | |||
1141 | int DHT_handlepacket(uint8_t * packet, uint32_t length, IP_Port source) | ||
1142 | { | ||
1143 | switch (packet[0]) { | ||
1144 | case 0: | ||
1145 | return handle_pingreq(packet, length, source); | ||
1146 | |||
1147 | case 1: | ||
1148 | return handle_pingres(packet, length, source); | ||
1149 | |||
1150 | case 2: | ||
1151 | return handle_getnodes(packet, length, source); | ||
1152 | |||
1153 | case 3: | ||
1154 | return handle_sendnodes(packet, length, source); | ||
1155 | |||
1156 | case 254: | ||
1157 | return handle_NATping(packet, length, source); | ||
1158 | |||
1159 | default: | ||
1160 | return 1; | ||
1075 | 1161 | ||
1076 | } | 1162 | } |
1163 | |||
1077 | return 0; | 1164 | return 0; |
1165 | |||
1166 | } | ||
1167 | |||
1168 | void doDHT() | ||
1169 | { | ||
1170 | doClose(); | ||
1171 | doDHTFriends(); | ||
1172 | doNAT(); | ||
1078 | } | 1173 | } |
1079 | 1174 | ||
1080 | /* get the size of the DHT (for saving) */ | 1175 | /* get the size of the DHT (for saving) */ |
@@ -1095,12 +1190,10 @@ void DHT_save(uint8_t * data) | |||
1095 | return 0 if success */ | 1190 | return 0 if success */ |
1096 | int DHT_load(uint8_t * data, uint32_t size) | 1191 | int DHT_load(uint8_t * data, uint32_t size) |
1097 | { | 1192 | { |
1098 | if(size < sizeof(close_clientlist)) | 1193 | if(size < sizeof(close_clientlist)) { |
1099 | { | ||
1100 | return -1; | 1194 | return -1; |
1101 | } | 1195 | } |
1102 | if((size - sizeof(close_clientlist)) % sizeof(Friend) != 0) | 1196 | if((size - sizeof(close_clientlist)) % sizeof(Friend) != 0) { |
1103 | { | ||
1104 | return -1; | 1197 | return -1; |
1105 | } | 1198 | } |
1106 | uint32_t i, j; | 1199 | uint32_t i, j; |
@@ -1109,17 +1202,13 @@ int DHT_load(uint8_t * data, uint32_t size) | |||
1109 | 1202 | ||
1110 | temp = (size - sizeof(close_clientlist))/sizeof(Friend); | 1203 | temp = (size - sizeof(close_clientlist))/sizeof(Friend); |
1111 | 1204 | ||
1112 | if(temp != 0) | 1205 | if(temp != 0) { |
1113 | { | ||
1114 | Friend * tempfriends_list = (Friend *)(data + sizeof(close_clientlist)); | 1206 | Friend * tempfriends_list = (Friend *)(data + sizeof(close_clientlist)); |
1115 | 1207 | ||
1116 | for(i = 0; i < temp; ++i) | 1208 | for(i = 0; i < temp; ++i) { |
1117 | { | ||
1118 | DHT_addfriend(tempfriends_list[i].client_id); | 1209 | DHT_addfriend(tempfriends_list[i].client_id); |
1119 | for(j = 0; j < MAX_FRIEND_CLIENTS; ++j) | 1210 | for(j = 0; j < MAX_FRIEND_CLIENTS; ++j) { |
1120 | { | 1211 | if(tempfriends_list[i].client_list[j].timestamp != 0) { |
1121 | if(tempfriends_list[i].client_list[j].timestamp != 0) | ||
1122 | { | ||
1123 | getnodes(tempfriends_list[i].client_list[j].ip_port, | 1212 | getnodes(tempfriends_list[i].client_list[j].ip_port, |
1124 | tempfriends_list[i].client_list[j].client_id, tempfriends_list[i].client_id); | 1213 | tempfriends_list[i].client_list[j].client_id, tempfriends_list[i].client_id); |
1125 | } | 1214 | } |
@@ -1128,10 +1217,8 @@ int DHT_load(uint8_t * data, uint32_t size) | |||
1128 | } | 1217 | } |
1129 | Client_data * tempclose_clientlist = (Client_data *)data; | 1218 | Client_data * tempclose_clientlist = (Client_data *)data; |
1130 | 1219 | ||
1131 | for(i = 0; i < LCLIENT_LIST; ++i) | 1220 | for(i = 0; i < LCLIENT_LIST; ++i) { |
1132 | { | 1221 | if(tempclose_clientlist[i].timestamp != 0) { |
1133 | if(tempclose_clientlist[i].timestamp != 0) | ||
1134 | { | ||
1135 | DHT_bootstrap(tempclose_clientlist[i].ip_port, tempclose_clientlist[i].client_id); | 1222 | DHT_bootstrap(tempclose_clientlist[i].ip_port, tempclose_clientlist[i].client_id); |
1136 | } | 1223 | } |
1137 | } | 1224 | } |
@@ -1144,13 +1231,10 @@ int DHT_isconnected() | |||
1144 | { | 1231 | { |
1145 | uint32_t i; | 1232 | uint32_t i; |
1146 | uint32_t temp_time = unix_time(); | 1233 | uint32_t temp_time = unix_time(); |
1147 | for(i = 0; i < LCLIENT_LIST; ++i) | 1234 | for(i = 0; i < LCLIENT_LIST; ++i) { |
1148 | { | 1235 | if(close_clientlist[i].timestamp + BAD_NODE_TIMEOUT > temp_time) { |
1149 | if(close_clientlist[i].timestamp + BAD_NODE_TIMEOUT > temp_time) | ||
1150 | { | ||
1151 | return 1; | 1236 | return 1; |
1152 | } | 1237 | } |
1153 | } | 1238 | } |
1154 | return 0; | 1239 | return 0; |
1155 | } | 1240 | } \ No newline at end of file |
1156 | |||
@@ -1,27 +1,25 @@ | |||
1 | /* DHT.h | 1 | /* DHT.h |
2 | * | 2 | * |
3 | * An implementation of the DHT as seen in docs/DHT.txt | 3 | * An implementation of the DHT as seen in docs/DHT.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 | */ | ||
24 | |||
25 | 23 | ||
26 | #ifndef DHT_H | 24 | #ifndef DHT_H |
27 | #define DHT_H | 25 | #define DHT_H |
@@ -38,8 +36,6 @@ extern "C" { | |||
38 | /* size of the client_id in bytes */ | 36 | /* size of the client_id in bytes */ |
39 | #define CLIENT_ID_SIZE crypto_box_PUBLICKEYBYTES | 37 | #define CLIENT_ID_SIZE crypto_box_PUBLICKEYBYTES |
40 | 38 | ||
41 | |||
42 | |||
43 | /* Add a new friend to the friends list | 39 | /* Add a new friend to the friends list |
44 | client_id must be CLIENT_ID_SIZE bytes long. | 40 | client_id must be CLIENT_ID_SIZE bytes long. |
45 | returns 0 if success | 41 | returns 0 if success |
@@ -52,7 +48,6 @@ int DHT_addfriend(uint8_t * client_id); | |||
52 | returns 1 if failure (client_id not in friends list) */ | 48 | returns 1 if failure (client_id not in friends list) */ |
53 | int DHT_delfriend(uint8_t * client_id); | 49 | int DHT_delfriend(uint8_t * client_id); |
54 | 50 | ||
55 | |||
56 | /* Get ip of friend | 51 | /* Get ip of friend |
57 | client_id must be CLIENT_ID_SIZE bytes long. | 52 | client_id must be CLIENT_ID_SIZE bytes long. |
58 | ip must be 4 bytes long. | 53 | ip must be 4 bytes long. |
@@ -62,7 +57,6 @@ int DHT_delfriend(uint8_t * client_id); | |||
62 | returns ip of 1 if friend is not in list. */ | 57 | returns ip of 1 if friend is not in list. */ |
63 | IP_Port DHT_getfriendip(uint8_t * client_id); | 58 | IP_Port DHT_getfriendip(uint8_t * client_id); |
64 | 59 | ||
65 | |||
66 | /* Run this function at least a couple times per second (It's the main loop) */ | 60 | /* Run this function at least a couple times per second (It's the main loop) */ |
67 | void doDHT(); | 61 | void doDHT(); |
68 | 62 | ||
@@ -75,8 +69,6 @@ int DHT_handlepacket(uint8_t * packet, uint32_t length, IP_Port source); | |||
75 | Sends a get nodes request to the given node with ip port and public_key */ | 69 | Sends a get nodes request to the given node with ip port and public_key */ |
76 | void DHT_bootstrap(IP_Port ip_port, uint8_t * public_key); | 70 | void DHT_bootstrap(IP_Port ip_port, uint8_t * public_key); |
77 | 71 | ||
78 | |||
79 | |||
80 | /* ROUTING FUNCTIONS */ | 72 | /* ROUTING FUNCTIONS */ |
81 | 73 | ||
82 | /* send the given packet to node with client_id | 74 | /* send the given packet to node with client_id |
@@ -87,8 +79,6 @@ int route_packet(uint8_t * client_id, uint8_t * packet, uint32_t length); | |||
87 | returns the number of nodes it sent the packet to */ | 79 | returns the number of nodes it sent the packet to */ |
88 | int route_tofriend(uint8_t * friend_id, uint8_t * packet, uint32_t length); | 80 | int route_tofriend(uint8_t * friend_id, uint8_t * packet, uint32_t length); |
89 | 81 | ||
90 | |||
91 | |||
92 | /* NAT PUNCHING FUNCTIONS */ | 82 | /* NAT PUNCHING FUNCTIONS */ |
93 | 83 | ||
94 | /* Puts all the different ips returned by the nodes for a friend_id into array ip_portlist | 84 | /* Puts all the different ips returned by the nodes for a friend_id into array ip_portlist |
@@ -97,8 +87,6 @@ int route_tofriend(uint8_t * friend_id, uint8_t * packet, uint32_t length); | |||
97 | returns -1 if no such friend*/ | 87 | returns -1 if no such friend*/ |
98 | int friend_ips(IP_Port * ip_portlist, uint8_t * friend_id); | 88 | int friend_ips(IP_Port * ip_portlist, uint8_t * friend_id); |
99 | 89 | ||
100 | |||
101 | |||
102 | /* SAVE/LOAD functions */ | 90 | /* SAVE/LOAD functions */ |
103 | 91 | ||
104 | /* get the size of the DHT (for saving) */ | 92 | /* get the size of the DHT (for saving) */ |
diff --git a/core/LAN_discovery.c b/core/LAN_discovery.c new file mode 100644 index 00000000..3cfcb067 --- /dev/null +++ b/core/LAN_discovery.c | |||
@@ -0,0 +1,79 @@ | |||
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 | } | ||
diff --git a/core/LAN_discovery.h b/core/LAN_discovery.h new file mode 100644 index 00000000..655830f9 --- /dev/null +++ b/core/LAN_discovery.h | |||
@@ -0,0 +1,50 @@ | |||
1 | /* LAN_discovery.h | ||
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 | |||
25 | #ifndef LAN_DISCOVERY_H | ||
26 | #define LAN_DISCOVERY_H | ||
27 | |||
28 | |||
29 | #include "DHT.h" | ||
30 | |||
31 | #ifdef __cplusplus | ||
32 | extern "C" { | ||
33 | #endif | ||
34 | |||
35 | /*Send a LAN discovery pcaket to the broadcast address with port port*/ | ||
36 | int send_LANdiscovery(uint16_t port); | ||
37 | |||
38 | |||
39 | /* if we receive a packet we call this function so it can be handled. | ||
40 | return 0 if packet is handled correctly. | ||
41 | return 1 if it didn't handle the packet or if the packet was shit. */ | ||
42 | int LANdiscovery_handlepacket(uint8_t * packet, uint32_t length, IP_Port source); | ||
43 | |||
44 | |||
45 | |||
46 | #ifdef __cplusplus | ||
47 | } | ||
48 | #endif | ||
49 | |||
50 | #endif | ||
diff --git a/core/Lossless_UDP.c b/core/Lossless_UDP.c index c4c464b6..43f61b5b 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]; |
@@ -104,40 +101,31 @@ int getconnection_id(IP_Port ip_port) | |||
104 | { | 101 | { |
105 | uint32_t i; | 102 | uint32_t i; |
106 | for(i = 0; i < MAX_CONNECTIONS; ++i) | 103 | for(i = 0; i < MAX_CONNECTIONS; ++i) |
107 | { | ||
108 | if(connections[i].ip_port.ip.i == ip_port.ip.i && | 104 | if(connections[i].ip_port.ip.i == ip_port.ip.i && |
109 | connections[i].ip_port.port == ip_port.port && connections[i].status > 0) | 105 | connections[i].ip_port.port == ip_port.port && connections[i].status > 0) |
110 | { | ||
111 | return i; | 106 | return i; |
112 | } | ||
113 | } | ||
114 | return -1; | 107 | return -1; |
115 | } | 108 | } |
116 | 109 | ||
117 | /* table of random numbers used below. */ | 110 | /* table of random numbers used below. */ |
118 | static uint32_t randtable[6][256]; | 111 | static uint32_t randtable[6][256]; |
119 | 112 | ||
120 | |||
121 | /* generate a handshake_id which depends on the ip_port. | 113 | /* generate a handshake_id which depends on the ip_port. |
122 | this function will always give one unique handshake_id per ip_port. | 114 | this function will always give one unique handshake_id per ip_port. |
123 | TODO: make this better */ | 115 | TODO: make this better */ |
124 | uint32_t handshake_id(IP_Port source) | 116 | uint32_t handshake_id(IP_Port source) |
125 | { | 117 | { |
126 | uint32_t id = 0, i; | 118 | uint32_t id = 0, i; |
127 | for(i = 0; i < 6; ++i) | 119 | for(i = 0; i < 6; ++i) { |
128 | { | ||
129 | if(randtable[i][((uint8_t *)&source)[i]] == 0) | 120 | if(randtable[i][((uint8_t *)&source)[i]] == 0) |
130 | { | ||
131 | randtable[i][((uint8_t *)&source)[i]] = random_int(); | 121 | randtable[i][((uint8_t *)&source)[i]] = random_int(); |
132 | } | ||
133 | id ^= randtable[i][((uint8_t *)&source)[i]]; | 122 | id ^= randtable[i][((uint8_t *)&source)[i]]; |
134 | } | 123 | } |
135 | if(id == 0) /* id can't be zero */ | 124 | if(id == 0) /* id can't be zero */ |
136 | { | ||
137 | id = 1; | 125 | id = 1; |
138 | } | ||
139 | return id; | 126 | return id; |
140 | } | 127 | } |
128 | |||
141 | /* change the hnshake id associated with that ip_port | 129 | /* change the hnshake id associated with that ip_port |
142 | TODO: make this better */ | 130 | TODO: make this better */ |
143 | void change_handshake(IP_Port source) | 131 | void change_handshake(IP_Port source) |
@@ -146,7 +134,6 @@ void change_handshake(IP_Port source) | |||
146 | randtable[rand][((uint8_t *)&source)[rand]] = random_int(); | 134 | randtable[rand][((uint8_t *)&source)[rand]] = random_int(); |
147 | } | 135 | } |
148 | 136 | ||
149 | |||
150 | /* initialize a new connection to ip_port | 137 | /* initialize a new connection to ip_port |
151 | returns an integer corresponding to the connection id. | 138 | returns an integer corresponding to the connection id. |
152 | return -1 if it could not initialize the connection. | 139 | return -1 if it could not initialize the connection. |
@@ -155,14 +142,10 @@ int new_connection(IP_Port ip_port) | |||
155 | { | 142 | { |
156 | int connect = getconnection_id(ip_port); | 143 | int connect = getconnection_id(ip_port); |
157 | if(connect != -1) | 144 | if(connect != -1) |
158 | { | ||
159 | return connect; | 145 | return connect; |
160 | } | ||
161 | uint32_t i; | 146 | uint32_t i; |
162 | for(i = 0; i < MAX_CONNECTIONS; ++i) | 147 | for(i = 0; i < MAX_CONNECTIONS; ++i) { |
163 | { | 148 | if(connections[i].status == 0) { |
164 | if(connections[i].status == 0) | ||
165 | { | ||
166 | memset(&connections[i], 0, sizeof(Connection)); | 149 | memset(&connections[i], 0, sizeof(Connection)); |
167 | connections[i].ip_port = ip_port; | 150 | connections[i].ip_port = ip_port; |
168 | connections[i].status = 1; | 151 | connections[i].status = 1; |
@@ -191,14 +174,10 @@ int new_connection(IP_Port ip_port) | |||
191 | int new_inconnection(IP_Port ip_port) | 174 | int new_inconnection(IP_Port ip_port) |
192 | { | 175 | { |
193 | if(getconnection_id(ip_port) != -1) | 176 | if(getconnection_id(ip_port) != -1) |
194 | { | ||
195 | return -1; | 177 | return -1; |
196 | } | ||
197 | uint32_t i; | 178 | uint32_t i; |
198 | for(i = 0; i < MAX_CONNECTIONS; ++i) | 179 | for(i = 0; i < MAX_CONNECTIONS; ++i) { |
199 | { | 180 | if(connections[i].status == 0) { |
200 | if(connections[i].status == 0) | ||
201 | { | ||
202 | memset(&connections[i], 0, sizeof(Connection)); | 181 | memset(&connections[i], 0, sizeof(Connection)); |
203 | connections[i].ip_port = ip_port; | 182 | connections[i].ip_port = ip_port; |
204 | connections[i].status = 2; | 183 | connections[i].status = 2; |
@@ -224,13 +203,10 @@ int incoming_connection() | |||
224 | { | 203 | { |
225 | uint32_t i; | 204 | uint32_t i; |
226 | for(i = 0; i < MAX_CONNECTIONS; ++i) | 205 | for(i = 0; i < MAX_CONNECTIONS; ++i) |
227 | { | 206 | if(connections[i].inbound == 2) { |
228 | if(connections[i].inbound == 2) | ||
229 | { | ||
230 | connections[i].inbound = 1; | 207 | connections[i].inbound = 1; |
231 | return i; | 208 | return i; |
232 | } | 209 | } |
233 | } | ||
234 | return -1; | 210 | return -1; |
235 | } | 211 | } |
236 | 212 | ||
@@ -239,14 +215,11 @@ int incoming_connection() | |||
239 | int kill_connection(int connection_id) | 215 | int kill_connection(int connection_id) |
240 | { | 216 | { |
241 | if(connection_id >= 0 && connection_id < MAX_CONNECTIONS) | 217 | if(connection_id >= 0 && connection_id < MAX_CONNECTIONS) |
242 | { | 218 | if(connections[connection_id].status > 0) { |
243 | if(connections[connection_id].status > 0) | ||
244 | { | ||
245 | connections[connection_id].status = 0; | 219 | connections[connection_id].status = 0; |
246 | change_handshake(connections[connection_id].ip_port); | 220 | change_handshake(connections[connection_id].ip_port); |
247 | return 0; | 221 | return 0; |
248 | } | 222 | } |
249 | } | ||
250 | return -1; | 223 | return -1; |
251 | } | 224 | } |
252 | 225 | ||
@@ -256,13 +229,10 @@ int kill_connection(int connection_id) | |||
256 | int kill_connection_in(int connection_id, uint32_t seconds) | 229 | int kill_connection_in(int connection_id, uint32_t seconds) |
257 | { | 230 | { |
258 | if(connection_id >= 0 && connection_id < MAX_CONNECTIONS) | 231 | if(connection_id >= 0 && connection_id < MAX_CONNECTIONS) |
259 | { | 232 | if(connections[connection_id].status > 0) { |
260 | if(connections[connection_id].status > 0) | ||
261 | { | ||
262 | connections[connection_id].killat = current_time() + 1000000UL*seconds; | 233 | connections[connection_id].killat = current_time() + 1000000UL*seconds; |
263 | return 0; | 234 | return 0; |
264 | } | 235 | } |
265 | } | ||
266 | return -1; | 236 | return -1; |
267 | } | 237 | } |
268 | 238 | ||
@@ -275,9 +245,7 @@ int kill_connection_in(int connection_id, uint32_t seconds) | |||
275 | int is_connected(int connection_id) | 245 | int is_connected(int connection_id) |
276 | { | 246 | { |
277 | if(connection_id >= 0 && connection_id < MAX_CONNECTIONS) | 247 | if(connection_id >= 0 && connection_id < MAX_CONNECTIONS) |
278 | { | ||
279 | return connections[connection_id].status; | 248 | return connections[connection_id].status; |
280 | } | ||
281 | return 0; | 249 | return 0; |
282 | } | 250 | } |
283 | 251 | ||
@@ -285,9 +253,7 @@ int is_connected(int connection_id) | |||
285 | IP_Port connection_ip(int connection_id) | 253 | IP_Port connection_ip(int connection_id) |
286 | { | 254 | { |
287 | if(connection_id >= 0 && connection_id < MAX_CONNECTIONS) | 255 | if(connection_id >= 0 && connection_id < MAX_CONNECTIONS) |
288 | { | ||
289 | return connections[connection_id].ip_port; | 256 | return connections[connection_id].ip_port; |
290 | } | ||
291 | IP_Port zero = {{{0}}, 0}; | 257 | IP_Port zero = {{{0}}, 0}; |
292 | return zero; | 258 | return zero; |
293 | } | 259 | } |
@@ -309,17 +275,15 @@ uint32_t recvqueue(int connection_id) | |||
309 | char id_packet(int connection_id) | 275 | char id_packet(int connection_id) |
310 | { | 276 | { |
311 | if(recvqueue(connection_id) != 0 && connections[connection_id].status != 0) | 277 | if(recvqueue(connection_id) != 0 && connections[connection_id].status != 0) |
312 | { | ||
313 | return connections[connection_id].recvbuffer[connections[connection_id].successful_read % MAX_QUEUE_NUM].data[0]; | 278 | return connections[connection_id].recvbuffer[connections[connection_id].successful_read % MAX_QUEUE_NUM].data[0]; |
314 | } | ||
315 | return -1; | 279 | return -1; |
316 | } | 280 | } |
281 | |||
317 | /* return 0 if there is no received data in the buffer. | 282 | /* return 0 if there is no received data in the buffer. |
318 | return length of received packet if successful */ | 283 | return length of received packet if successful */ |
319 | int read_packet(int connection_id, uint8_t * data) | 284 | int read_packet(int connection_id, uint8_t * data) |
320 | { | 285 | { |
321 | if(recvqueue(connection_id) != 0) | 286 | if(recvqueue(connection_id) != 0) { |
322 | { | ||
323 | uint16_t index = connections[connection_id].successful_read % MAX_QUEUE_NUM; | 287 | uint16_t index = connections[connection_id].successful_read % MAX_QUEUE_NUM; |
324 | uint16_t size = connections[connection_id].recvbuffer[index].size; | 288 | uint16_t size = connections[connection_id].recvbuffer[index].size; |
325 | memcpy(data, connections[connection_id].recvbuffer[index].data, size); | 289 | memcpy(data, connections[connection_id].recvbuffer[index].data, size); |
@@ -335,15 +299,10 @@ int read_packet(int connection_id, uint8_t * data) | |||
335 | int write_packet(int connection_id, uint8_t * data, uint32_t length) | 299 | int write_packet(int connection_id, uint8_t * data, uint32_t length) |
336 | { | 300 | { |
337 | if(length > MAX_DATA_SIZE) | 301 | if(length > MAX_DATA_SIZE) |
338 | { | ||
339 | return 0; | 302 | return 0; |
340 | } | ||
341 | if(length == 0) | 303 | if(length == 0) |
342 | { | ||
343 | return 0; | 304 | return 0; |
344 | } | 305 | if(sendqueue(connection_id) < BUFFER_PACKET_NUM) { |
345 | if(sendqueue(connection_id) < BUFFER_PACKET_NUM) | ||
346 | { | ||
347 | uint32_t index = connections[connection_id].sendbuff_packetnum % MAX_QUEUE_NUM; | 306 | uint32_t index = connections[connection_id].sendbuff_packetnum % MAX_QUEUE_NUM; |
348 | memcpy(connections[connection_id].sendbuffer[index].data, data, length); | 307 | memcpy(connections[connection_id].sendbuffer[index].data, data, length); |
349 | connections[connection_id].sendbuffer[index].size = length; | 308 | connections[connection_id].sendbuffer[index].size = length; |
@@ -353,9 +312,6 @@ int write_packet(int connection_id, uint8_t * data, uint32_t length) | |||
353 | return 0; | 312 | return 0; |
354 | } | 313 | } |
355 | 314 | ||
356 | |||
357 | |||
358 | |||
359 | /* put the packet numbers the we are missing in requested and return the number */ | 315 | /* 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) | 316 | uint32_t missing_packets(int connection_id, uint32_t * requested) |
361 | { | 317 | { |
@@ -363,59 +319,47 @@ uint32_t missing_packets(int connection_id, uint32_t * requested) | |||
363 | uint32_t i; | 319 | uint32_t i; |
364 | uint32_t temp; | 320 | uint32_t temp; |
365 | if(recvqueue(connection_id) >= (BUFFER_PACKET_NUM - 1)) /* don't request packets if the buffer is full. */ | 321 | if(recvqueue(connection_id) >= (BUFFER_PACKET_NUM - 1)) /* don't request packets if the buffer is full. */ |
366 | { | ||
367 | return 0; | 322 | return 0; |
368 | } | ||
369 | for(i = connections[connection_id].recv_packetnum; i != connections[connection_id].osent_packetnum; i++ ) | 323 | for(i = connections[connection_id].recv_packetnum; i != connections[connection_id].osent_packetnum; i++ ) |
370 | { | 324 | if(connections[connection_id].recvbuffer[i % MAX_QUEUE_NUM].size == 0) { |
371 | if(connections[connection_id].recvbuffer[i % MAX_QUEUE_NUM].size == 0) | ||
372 | { | ||
373 | temp = htonl(i); | 325 | temp = htonl(i); |
374 | memcpy(requested + number, &temp, 4); | 326 | memcpy(requested + number, &temp, 4); |
375 | ++number; | 327 | ++number; |
376 | } | 328 | } |
377 | } | ||
378 | if(number == 0) | 329 | if(number == 0) |
379 | { | ||
380 | connections[connection_id].recv_packetnum = connections[connection_id].osent_packetnum; | 330 | connections[connection_id].recv_packetnum = connections[connection_id].osent_packetnum; |
381 | } | ||
382 | return number; | 331 | return number; |
383 | |||
384 | } | 332 | } |
385 | 333 | ||
386 | /* Packet sending functions | 334 | /* Packet sending functions |
387 | One per packet type. | 335 | One per packet type. |
388 | see docs/Lossless_UDP.txt for more information. */ | 336 | 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) | 337 | int send_handshake(IP_Port ip_port, uint32_t handshake_id1, uint32_t handshake_id2) |
392 | { | 338 | { |
393 | uint8_t packet[1 + 4 + 4]; | 339 | uint8_t packet[1 + 4 + 4]; |
394 | uint32_t temp; | 340 | uint32_t temp; |
395 | 341 | ||
396 | packet[0] = 16; | 342 | packet[0] = 16; |
397 | temp = htonl(handshake_id1); | 343 | temp = htonl(handshake_id1); |
398 | memcpy(packet + 1, &temp, 4); | 344 | memcpy(packet + 1, &temp, 4); |
399 | temp = htonl(handshake_id2); | 345 | temp = htonl(handshake_id2); |
400 | memcpy(packet + 5, &temp, 4); | 346 | memcpy(packet + 5, &temp, 4); |
401 | return sendpacket(ip_port, packet, sizeof(packet)); | 347 | return sendpacket(ip_port, packet, sizeof(packet)); |
402 | |||
403 | } | 348 | } |
404 | 349 | ||
405 | |||
406 | int send_SYNC(uint32_t connection_id) | 350 | int send_SYNC(uint32_t connection_id) |
407 | { | 351 | { |
408 | 352 | ||
409 | uint8_t packet[(BUFFER_PACKET_NUM*4 + 4 + 4 + 2)]; | 353 | uint8_t packet[(BUFFER_PACKET_NUM*4 + 4 + 4 + 2)]; |
410 | uint16_t index = 0; | 354 | uint16_t index = 0; |
411 | 355 | ||
412 | IP_Port ip_port = connections[connection_id].ip_port; | 356 | IP_Port ip_port = connections[connection_id].ip_port; |
413 | uint8_t counter = connections[connection_id].send_counter; | 357 | uint8_t counter = connections[connection_id].send_counter; |
414 | uint32_t recv_packetnum = htonl(connections[connection_id].recv_packetnum); | 358 | uint32_t recv_packetnum = htonl(connections[connection_id].recv_packetnum); |
415 | uint32_t sent_packetnum = htonl(connections[connection_id].sent_packetnum); | 359 | uint32_t sent_packetnum = htonl(connections[connection_id].sent_packetnum); |
416 | uint32_t requested[BUFFER_PACKET_NUM]; | 360 | uint32_t requested[BUFFER_PACKET_NUM]; |
417 | uint32_t number = missing_packets(connection_id, requested); | 361 | uint32_t number = missing_packets(connection_id, requested); |
418 | 362 | ||
419 | packet[0] = 17; | 363 | packet[0] = 17; |
420 | index += 1; | 364 | index += 1; |
421 | memcpy(packet + index, &counter, 1); | 365 | memcpy(packet + index, &counter, 1); |
@@ -427,7 +371,7 @@ int send_SYNC(uint32_t connection_id) | |||
427 | memcpy(packet + index, requested, 4 * number); | 371 | memcpy(packet + index, requested, 4 * number); |
428 | 372 | ||
429 | return sendpacket(ip_port, packet, (number*4 + 4 + 4 + 2)); | 373 | return sendpacket(ip_port, packet, (number*4 + 4 + 4 + 2)); |
430 | 374 | ||
431 | } | 375 | } |
432 | 376 | ||
433 | int send_data_packet(uint32_t connection_id, uint32_t packet_num) | 377 | int send_data_packet(uint32_t connection_id, uint32_t packet_num) |
@@ -449,16 +393,14 @@ int send_DATA(uint32_t connection_id) | |||
449 | { | 393 | { |
450 | int ret; | 394 | int ret; |
451 | uint32_t buffer[BUFFER_PACKET_NUM]; | 395 | uint32_t buffer[BUFFER_PACKET_NUM]; |
452 | if(connections[connection_id].num_req_paquets > 0) | 396 | if(connections[connection_id].num_req_paquets > 0) { |
453 | { | ||
454 | ret = send_data_packet(connection_id, connections[connection_id].req_packets[0]); | 397 | ret = send_data_packet(connection_id, connections[connection_id].req_packets[0]); |
455 | connections[connection_id].num_req_paquets--; | 398 | connections[connection_id].num_req_paquets--; |
456 | memcpy(buffer, connections[connection_id].req_packets + 1, connections[connection_id].num_req_paquets * 4); | 399 | memcpy(buffer, connections[connection_id].req_packets + 1, connections[connection_id].num_req_paquets * 4); |
457 | memcpy(connections[connection_id].req_packets, buffer, connections[connection_id].num_req_paquets * 4); | 400 | memcpy(connections[connection_id].req_packets, buffer, connections[connection_id].num_req_paquets * 4); |
458 | return ret; | 401 | return ret; |
459 | } | 402 | } |
460 | if(connections[connection_id].sendbuff_packetnum != connections[connection_id].sent_packetnum) | 403 | if(connections[connection_id].sendbuff_packetnum != connections[connection_id].sent_packetnum) { |
461 | { | ||
462 | ret = send_data_packet(connection_id, connections[connection_id].sent_packetnum); | 404 | ret = send_data_packet(connection_id, connections[connection_id].sent_packetnum); |
463 | connections[connection_id].sent_packetnum++; | 405 | connections[connection_id].sent_packetnum++; |
464 | return ret; | 406 | return ret; |
@@ -468,17 +410,13 @@ int send_DATA(uint32_t connection_id) | |||
468 | 410 | ||
469 | /* END of packet sending functions */ | 411 | /* END of packet sending functions */ |
470 | 412 | ||
471 | |||
472 | |||
473 | /* Packet handling functions | 413 | /* Packet handling functions |
474 | One to handle each type of packets we receive | 414 | One to handle each type of packets we receive |
475 | return 0 if handled correctly, 1 if packet is bad. */ | 415 | return 0 if handled correctly, 1 if packet is bad. */ |
476 | int handle_handshake(uint8_t * packet, uint32_t length, IP_Port source) | 416 | int handle_handshake(uint8_t * packet, uint32_t length, IP_Port source) |
477 | { | 417 | { |
478 | if(length != (1 + 4 + 4)) | 418 | if(length != (1 + 4 + 4)) |
479 | { | ||
480 | return 1; | 419 | return 1; |
481 | } | ||
482 | uint32_t temp; | 420 | uint32_t temp; |
483 | uint32_t handshake_id1, handshake_id2; | 421 | uint32_t handshake_id1, handshake_id2; |
484 | int connection = getconnection_id(source); | 422 | int connection = getconnection_id(source); |
@@ -487,17 +425,13 @@ int handle_handshake(uint8_t * packet, uint32_t length, IP_Port source) | |||
487 | memcpy(&temp, packet + 5, 4); | 425 | memcpy(&temp, packet + 5, 4); |
488 | handshake_id2 = ntohl(temp); | 426 | handshake_id2 = ntohl(temp); |
489 | 427 | ||
490 | if(handshake_id2 == 0) | 428 | if(handshake_id2 == 0) { |
491 | { | ||
492 | send_handshake(source, handshake_id(source), handshake_id1); | 429 | send_handshake(source, handshake_id(source), handshake_id1); |
493 | return 0; | 430 | return 0; |
494 | } | 431 | } |
495 | if(is_connected(connection) != 1) | 432 | if(is_connected(connection) != 1) |
496 | { | ||
497 | return 1; | 433 | return 1; |
498 | } | 434 | if(handshake_id2 == connections[connection].handshake_id1) { /* if handshake_id2 is what we sent previously as handshake_id1 */ |
499 | if(handshake_id2 == connections[connection].handshake_id1) /* if handshake_id2 is what we sent previously as handshake_id1 */ | ||
500 | { | ||
501 | connections[connection].status = 2; | 435 | connections[connection].status = 2; |
502 | /* NOTE: is this necessary? | 436 | /* NOTE: is this necessary? |
503 | connections[connection].handshake_id2 = handshake_id1; */ | 437 | connections[connection].handshake_id2 = handshake_id1; */ |
@@ -515,25 +449,19 @@ int handle_handshake(uint8_t * packet, uint32_t length, IP_Port source) | |||
515 | int SYNC_valid(uint32_t length) | 449 | int SYNC_valid(uint32_t length) |
516 | { | 450 | { |
517 | if(length < 4 + 4 + 2) | 451 | if(length < 4 + 4 + 2) |
518 | { | ||
519 | return 0; | 452 | return 0; |
520 | } | ||
521 | if(length > (BUFFER_PACKET_NUM*4 + 4 + 4 + 2) || | 453 | if(length > (BUFFER_PACKET_NUM*4 + 4 + 4 + 2) || |
522 | ((length - 4 - 4 - 2) % 4) != 0) | 454 | ((length - 4 - 4 - 2) % 4) != 0) |
523 | { | ||
524 | return 0; | 455 | return 0; |
525 | } | ||
526 | return 1; | 456 | return 1; |
527 | } | 457 | } |
528 | 458 | ||
529 | /* case 1: */ | 459 | /* case 1: */ |
530 | int handle_SYNC1(IP_Port source, uint32_t recv_packetnum, uint32_t sent_packetnum) | 460 | int handle_SYNC1(IP_Port source, uint32_t recv_packetnum, uint32_t sent_packetnum) |
531 | { | 461 | { |
532 | if(handshake_id(source) == recv_packetnum) | 462 | if(handshake_id(source) == recv_packetnum) { |
533 | { | ||
534 | int x = new_inconnection(source); | 463 | int x = new_inconnection(source); |
535 | if(x != -1) | 464 | if(x != -1) { |
536 | { | ||
537 | connections[x].orecv_packetnum = recv_packetnum; | 465 | connections[x].orecv_packetnum = recv_packetnum; |
538 | connections[x].sent_packetnum = recv_packetnum; | 466 | connections[x].sent_packetnum = recv_packetnum; |
539 | connections[x].sendbuff_packetnum = recv_packetnum; | 467 | connections[x].sendbuff_packetnum = recv_packetnum; |
@@ -551,9 +479,8 @@ int handle_SYNC1(IP_Port source, uint32_t recv_packetnum, uint32_t sent_packetnu | |||
551 | /* case 2: */ | 479 | /* case 2: */ |
552 | int handle_SYNC2(int connection_id, uint8_t counter, uint32_t recv_packetnum, uint32_t sent_packetnum) | 480 | int handle_SYNC2(int connection_id, uint8_t counter, uint32_t recv_packetnum, uint32_t sent_packetnum) |
553 | { | 481 | { |
554 | if(recv_packetnum == connections[connection_id].orecv_packetnum) | 482 | if(recv_packetnum == connections[connection_id].orecv_packetnum) { |
555 | /* && sent_packetnum == connections[connection_id].osent_packetnum) */ | 483 | /* && sent_packetnum == connections[connection_id].osent_packetnum) */ |
556 | { | ||
557 | connections[connection_id].status = 3; | 484 | connections[connection_id].status = 3; |
558 | connections[connection_id].recv_counter = counter; | 485 | connections[connection_id].recv_counter = counter; |
559 | ++connections[connection_id].send_counter; | 486 | ++connections[connection_id].send_counter; |
@@ -572,16 +499,14 @@ int handle_SYNC3(int connection_id, uint8_t counter, uint32_t recv_packetnum, ui | |||
572 | uint32_t comp_2 = (sent_packetnum - connections[connection_id].successful_read); */ | 499 | uint32_t comp_2 = (sent_packetnum - connections[connection_id].successful_read); */ |
573 | uint32_t comp_1 = (recv_packetnum - connections[connection_id].orecv_packetnum); | 500 | uint32_t comp_1 = (recv_packetnum - connections[connection_id].orecv_packetnum); |
574 | uint32_t comp_2 = (sent_packetnum - connections[connection_id].osent_packetnum); | 501 | uint32_t comp_2 = (sent_packetnum - connections[connection_id].osent_packetnum); |
575 | if(comp_1 <= BUFFER_PACKET_NUM && comp_2 <= BUFFER_PACKET_NUM && comp_counter < 10 && comp_counter != 0) /* packet valid */ | 502 | if(comp_1 <= BUFFER_PACKET_NUM && comp_2 <= BUFFER_PACKET_NUM && comp_counter < 10 && comp_counter != 0) { /* packet valid */ |
576 | { | ||
577 | connections[connection_id].orecv_packetnum = recv_packetnum; | 503 | connections[connection_id].orecv_packetnum = recv_packetnum; |
578 | connections[connection_id].osent_packetnum = sent_packetnum; | 504 | connections[connection_id].osent_packetnum = sent_packetnum; |
579 | connections[connection_id].successful_sent = recv_packetnum; | 505 | connections[connection_id].successful_sent = recv_packetnum; |
580 | connections[connection_id].last_recvSYNC = current_time(); | 506 | connections[connection_id].last_recvSYNC = current_time(); |
581 | connections[connection_id].recv_counter = counter; | 507 | connections[connection_id].recv_counter = counter; |
582 | ++connections[connection_id].send_counter; | 508 | ++connections[connection_id].send_counter; |
583 | for(i = 0; i < number; ++i) | 509 | for(i = 0; i < number; ++i) { |
584 | { | ||
585 | temp = ntohl(req_packets[i]); | 510 | temp = ntohl(req_packets[i]); |
586 | memcpy(connections[connection_id].req_packets + i, &temp, 4 * number); | 511 | memcpy(connections[connection_id].req_packets + i, &temp, 4 * number); |
587 | } | 512 | } |
@@ -595,9 +520,7 @@ int handle_SYNC(uint8_t * packet, uint32_t length, IP_Port source) | |||
595 | { | 520 | { |
596 | 521 | ||
597 | if(!SYNC_valid(length)) | 522 | if(!SYNC_valid(length)) |
598 | { | ||
599 | return 1; | 523 | return 1; |
600 | } | ||
601 | int connection = getconnection_id(source); | 524 | int connection = getconnection_id(source); |
602 | uint8_t counter; | 525 | uint8_t counter; |
603 | uint32_t temp; | 526 | uint32_t temp; |
@@ -611,21 +534,13 @@ int handle_SYNC(uint8_t * packet, uint32_t length, IP_Port source) | |||
611 | memcpy(&temp,packet + 6, 4); | 534 | memcpy(&temp,packet + 6, 4); |
612 | sent_packetnum = ntohl(temp); | 535 | sent_packetnum = ntohl(temp); |
613 | if(number != 0) | 536 | if(number != 0) |
614 | { | ||
615 | memcpy(req_packets, packet + 10, 4 * number); | 537 | memcpy(req_packets, packet + 10, 4 * number); |
616 | } | ||
617 | if(connection == -1) | 538 | if(connection == -1) |
618 | { | ||
619 | return handle_SYNC1(source, recv_packetnum, sent_packetnum); | 539 | return handle_SYNC1(source, recv_packetnum, sent_packetnum); |
620 | } | ||
621 | if(connections[connection].status == 2) | 540 | if(connections[connection].status == 2) |
622 | { | ||
623 | return handle_SYNC2(connection, counter, recv_packetnum, sent_packetnum); | 541 | return handle_SYNC2(connection, counter, recv_packetnum, sent_packetnum); |
624 | } | ||
625 | if(connections[connection].status == 3) | 542 | if(connections[connection].status == 3) |
626 | { | ||
627 | return handle_SYNC3(connection, counter, recv_packetnum, sent_packetnum, req_packets, number); | 543 | return handle_SYNC3(connection, counter, recv_packetnum, sent_packetnum, req_packets, number); |
628 | } | ||
629 | return 0; | 544 | return 0; |
630 | } | 545 | } |
631 | 546 | ||
@@ -634,37 +549,26 @@ int handle_SYNC(uint8_t * packet, uint32_t length, IP_Port source) | |||
634 | int add_recv(int connection_id, uint32_t data_num, uint8_t * data, uint16_t size) | 549 | int add_recv(int connection_id, uint32_t data_num, uint8_t * data, uint16_t size) |
635 | { | 550 | { |
636 | if(size > MAX_DATA_SIZE) | 551 | if(size > MAX_DATA_SIZE) |
637 | { | ||
638 | return 1; | 552 | return 1; |
639 | } | ||
640 | 553 | ||
641 | uint32_t i; | 554 | uint32_t i; |
642 | uint32_t maxnum = connections[connection_id].successful_read + BUFFER_PACKET_NUM; | 555 | uint32_t maxnum = connections[connection_id].successful_read + BUFFER_PACKET_NUM; |
643 | uint32_t sent_packet = data_num - connections[connection_id].osent_packetnum; | 556 | uint32_t sent_packet = data_num - connections[connection_id].osent_packetnum; |
644 | for(i = connections[connection_id].recv_packetnum; i != maxnum; ++i) | 557 | for(i = connections[connection_id].recv_packetnum; i != maxnum; ++i) { |
645 | { | 558 | if(i == data_num) { |
646 | if(i == data_num) | ||
647 | { | ||
648 | memcpy(connections[connection_id].recvbuffer[i % MAX_QUEUE_NUM].data, data, size); | 559 | memcpy(connections[connection_id].recvbuffer[i % MAX_QUEUE_NUM].data, data, size); |
649 | connections[connection_id].recvbuffer[i % MAX_QUEUE_NUM].size = size; | 560 | connections[connection_id].recvbuffer[i % MAX_QUEUE_NUM].size = size; |
650 | connections[connection_id].last_recvdata = current_time(); | 561 | connections[connection_id].last_recvdata = current_time(); |
651 | if(sent_packet < BUFFER_PACKET_NUM) | 562 | if(sent_packet < BUFFER_PACKET_NUM) |
652 | { | ||
653 | connections[connection_id].osent_packetnum = data_num; | 563 | connections[connection_id].osent_packetnum = data_num; |
654 | } | ||
655 | break; | 564 | break; |
656 | } | 565 | } |
657 | } | 566 | } |
658 | for(i = connections[connection_id].recv_packetnum; i != maxnum; ++i) | 567 | for(i = connections[connection_id].recv_packetnum; i != maxnum; ++i) { |
659 | { | ||
660 | if(connections[connection_id].recvbuffer[i % MAX_QUEUE_NUM].size != 0) | 568 | if(connections[connection_id].recvbuffer[i % MAX_QUEUE_NUM].size != 0) |
661 | { | ||
662 | connections[connection_id].recv_packetnum = i; | 569 | connections[connection_id].recv_packetnum = i; |
663 | } | ||
664 | else | 570 | else |
665 | { | ||
666 | break; | 571 | break; |
667 | } | ||
668 | } | 572 | } |
669 | 573 | ||
670 | return 0; | 574 | return 0; |
@@ -673,54 +577,43 @@ int add_recv(int connection_id, uint32_t data_num, uint8_t * data, uint16_t size | |||
673 | int handle_data(uint8_t * packet, uint32_t length, IP_Port source) | 577 | int handle_data(uint8_t * packet, uint32_t length, IP_Port source) |
674 | { | 578 | { |
675 | int connection = getconnection_id(source); | 579 | int connection = getconnection_id(source); |
676 | 580 | ||
677 | if(connection == -1) | 581 | if(connection == -1) |
678 | { | ||
679 | return 1; | 582 | return 1; |
680 | } | 583 | |
681 | |||
682 | if(connections[connection].status != 3) /* Drop the data packet if connection is not connected. */ | 584 | if(connections[connection].status != 3) /* Drop the data packet if connection is not connected. */ |
683 | { | ||
684 | return 1; | 585 | return 1; |
685 | } | 586 | |
686 | |||
687 | if(length > 1 + 4 + MAX_DATA_SIZE || length < 1 + 4 + 1) | 587 | if(length > 1 + 4 + MAX_DATA_SIZE || length < 1 + 4 + 1) |
688 | { | ||
689 | return 1; | 588 | return 1; |
690 | } | ||
691 | uint32_t temp; | 589 | uint32_t temp; |
692 | uint32_t number; | 590 | uint32_t number; |
693 | uint16_t size = length - 1 - 4; | 591 | uint16_t size = length - 1 - 4; |
694 | 592 | ||
695 | memcpy(&temp, packet + 1, 4); | 593 | memcpy(&temp, packet + 1, 4); |
696 | number = ntohl(temp); | 594 | number = ntohl(temp); |
697 | return add_recv(connection, number, packet + 5, size); | 595 | return add_recv(connection, number, packet + 5, size); |
698 | |||
699 | } | 596 | } |
700 | 597 | ||
701 | /* END of packet handling functions */ | 598 | /* END of packet handling functions */ |
702 | 599 | ||
703 | |||
704 | int LosslessUDP_handlepacket(uint8_t * packet, uint32_t length, IP_Port source) | 600 | int LosslessUDP_handlepacket(uint8_t * packet, uint32_t length, IP_Port source) |
705 | { | 601 | { |
706 | |||
707 | switch (packet[0]) { | 602 | switch (packet[0]) { |
708 | case 16: | 603 | case 16: |
709 | return handle_handshake(packet, length, source); | 604 | return handle_handshake(packet, length, source); |
710 | 605 | ||
711 | case 17: | 606 | case 17: |
712 | return handle_SYNC(packet, length, source); | 607 | return handle_SYNC(packet, length, source); |
713 | 608 | ||
714 | case 18: | 609 | case 18: |
715 | return handle_data(packet, length, source); | 610 | return handle_data(packet, length, source); |
716 | 611 | ||
717 | default: | 612 | default: |
718 | return 1; | 613 | return 1; |
719 | |||
720 | } | 614 | } |
721 | 615 | ||
722 | return 0; | 616 | return 0; |
723 | |||
724 | } | 617 | } |
725 | 618 | ||
726 | /* Send handshake requests | 619 | /* Send handshake requests |
@@ -729,28 +622,20 @@ void doNew() | |||
729 | { | 622 | { |
730 | uint32_t i; | 623 | uint32_t i; |
731 | uint64_t temp_time = current_time(); | 624 | uint64_t temp_time = current_time(); |
732 | for(i = 0; i < MAX_CONNECTIONS; ++i) | 625 | for(i = 0; i < MAX_CONNECTIONS; ++i) { |
733 | { | ||
734 | if(connections[i].status == 1) | 626 | if(connections[i].status == 1) |
735 | { | 627 | if((connections[i].last_sent + (1000000UL/connections[i].SYNC_rate)) <= temp_time) { |
736 | if((connections[i].last_sent + (1000000UL/connections[i].SYNC_rate)) <= temp_time) | ||
737 | { | ||
738 | send_handshake(connections[i].ip_port, connections[i].handshake_id1, 0); | 628 | send_handshake(connections[i].ip_port, connections[i].handshake_id1, 0); |
739 | connections[i].last_sent = temp_time; | 629 | connections[i].last_sent = temp_time; |
740 | } | 630 | } |
741 | 631 | ||
742 | } | ||
743 | /* kill all timed out connections */ | 632 | /* kill all timed out connections */ |
744 | if( connections[i].status > 0 && (connections[i].last_recvSYNC + connections[i].timeout * 1000000UL) < temp_time && | 633 | if( connections[i].status > 0 && (connections[i].last_recvSYNC + connections[i].timeout * 1000000UL) < temp_time && |
745 | connections[i].status != 4) | 634 | connections[i].status != 4) |
746 | { | ||
747 | /* kill_connection(i); */ | 635 | /* kill_connection(i); */ |
748 | connections[i].status = 4; | 636 | connections[i].status = 4; |
749 | } | ||
750 | if(connections[i].status > 0 && connections[i].killat < temp_time) | 637 | if(connections[i].status > 0 && connections[i].killat < temp_time) |
751 | { | ||
752 | kill_connection(i); | 638 | kill_connection(i); |
753 | } | ||
754 | } | 639 | } |
755 | } | 640 | } |
756 | 641 | ||
@@ -758,16 +643,12 @@ void doSYNC() | |||
758 | { | 643 | { |
759 | uint32_t i; | 644 | uint32_t i; |
760 | uint64_t temp_time = current_time(); | 645 | uint64_t temp_time = current_time(); |
761 | for(i = 0; i < MAX_CONNECTIONS; ++i) | 646 | for(i = 0; i < MAX_CONNECTIONS; ++i) { |
762 | { | ||
763 | if(connections[i].status == 2 || connections[i].status == 3) | 647 | if(connections[i].status == 2 || connections[i].status == 3) |
764 | { | 648 | if((connections[i].last_SYNC + (1000000UL/connections[i].SYNC_rate)) <= temp_time) { |
765 | if((connections[i].last_SYNC + (1000000UL/connections[i].SYNC_rate)) <= temp_time) | ||
766 | { | ||
767 | send_SYNC(i); | 649 | send_SYNC(i); |
768 | connections[i].last_SYNC = temp_time; | 650 | connections[i].last_SYNC = temp_time; |
769 | } | 651 | } |
770 | } | ||
771 | } | 652 | } |
772 | } | 653 | } |
773 | 654 | ||
@@ -777,19 +658,12 @@ void doData() | |||
777 | uint64_t j; | 658 | uint64_t j; |
778 | uint64_t temp_time = current_time(); | 659 | uint64_t temp_time = current_time(); |
779 | for(i = 0; i < MAX_CONNECTIONS; ++i) | 660 | for(i = 0; i < MAX_CONNECTIONS; ++i) |
780 | { | ||
781 | if(connections[i].status == 3 && sendqueue(i) != 0) | 661 | if(connections[i].status == 3 && sendqueue(i) != 0) |
782 | { | 662 | if((connections[i].last_sent + (1000000UL/connections[i].data_rate)) <= temp_time) { |
783 | if((connections[i].last_sent + (1000000UL/connections[i].data_rate)) <= temp_time) | ||
784 | { | ||
785 | for(j = connections[i].last_sent; j < temp_time; j += (1000000UL/connections[i].data_rate)) | 663 | for(j = connections[i].last_sent; j < temp_time; j += (1000000UL/connections[i].data_rate)) |
786 | { | ||
787 | send_DATA(i); | 664 | send_DATA(i); |
788 | } | ||
789 | connections[i].last_sent = temp_time; | 665 | connections[i].last_sent = temp_time; |
790 | } | 666 | } |
791 | } | ||
792 | } | ||
793 | } | 667 | } |
794 | 668 | ||
795 | /* TODO: flow control. | 669 | /* TODO: flow control. |
@@ -801,33 +675,22 @@ void adjustRates() | |||
801 | { | 675 | { |
802 | uint32_t i; | 676 | uint32_t i; |
803 | uint64_t temp_time = current_time(); | 677 | uint64_t temp_time = current_time(); |
804 | for(i = 0; i < MAX_CONNECTIONS; ++i) | 678 | for(i = 0; i < MAX_CONNECTIONS; ++i) { |
805 | { | ||
806 | if(connections[i].status == 1 || connections[i].status == 2) | 679 | if(connections[i].status == 1 || connections[i].status == 2) |
807 | { | ||
808 | connections[i].SYNC_rate = MAX_SYNC_RATE; | 680 | connections[i].SYNC_rate = MAX_SYNC_RATE; |
809 | } | 681 | if(connections[i].status == 3) { |
810 | if(connections[i].status == 3) | 682 | if(sendqueue(i) != 0) { |
811 | { | ||
812 | if(sendqueue(i) != 0) | ||
813 | { | ||
814 | |||
815 | connections[i].data_rate = (BUFFER_PACKET_NUM - connections[i].num_req_paquets) * MAX_SYNC_RATE; | 683 | connections[i].data_rate = (BUFFER_PACKET_NUM - connections[i].num_req_paquets) * MAX_SYNC_RATE; |
816 | |||
817 | connections[i].SYNC_rate = MAX_SYNC_RATE; | 684 | connections[i].SYNC_rate = MAX_SYNC_RATE; |
818 | |||
819 | } | 685 | } |
820 | else if(connections[i].last_recvdata + 1000000UL > temp_time) | 686 | else if(connections[i].last_recvdata + 1000000UL > temp_time) |
821 | { | ||
822 | connections[i].SYNC_rate = MAX_SYNC_RATE; | 687 | connections[i].SYNC_rate = MAX_SYNC_RATE; |
823 | } | ||
824 | else | 688 | else |
825 | { | ||
826 | connections[i].SYNC_rate = SYNC_RATE; | 689 | connections[i].SYNC_rate = SYNC_RATE; |
827 | } | ||
828 | } | 690 | } |
829 | } | 691 | } |
830 | } | 692 | } |
693 | |||
831 | /* Call this function a couple times per second | 694 | /* Call this function a couple times per second |
832 | It's the main loop. */ | 695 | It's the main loop. */ |
833 | void doLossless_UDP() | 696 | void doLossless_UDP() |
@@ -836,6 +699,4 @@ void doLossless_UDP() | |||
836 | doSYNC(); | 699 | doSYNC(); |
837 | doData(); | 700 | doData(); |
838 | adjustRates(); | 701 | adjustRates(); |
839 | |||
840 | |||
841 | } | 702 | } |
diff --git a/core/Lossless_UDP.h b/core/Lossless_UDP.h index 0f5bb119..a9f1bb15 100644 --- a/core/Lossless_UDP.h +++ b/core/Lossless_UDP.h | |||
@@ -1,26 +1,25 @@ | |||
1 | /* Lossless_UDP.h | 1 | /* Lossless_UDP.h |
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 | */ | ||
24 | 23 | ||
25 | #ifndef LOSSLESS_UDP_H | 24 | #ifndef LOSSLESS_UDP_H |
26 | #define LOSSLESS_UDP_H | 25 | #define LOSSLESS_UDP_H |
@@ -34,8 +33,6 @@ extern "C" { | |||
34 | /* maximum length of the data in the data packets */ | 33 | /* maximum length of the data in the data packets */ |
35 | #define MAX_DATA_SIZE 1024 | 34 | #define MAX_DATA_SIZE 1024 |
36 | 35 | ||
37 | |||
38 | |||
39 | /* Functions */ | 36 | /* Functions */ |
40 | 37 | ||
41 | /* initialize a new connection to ip_port | 38 | /* initialize a new connection to ip_port |
@@ -53,7 +50,6 @@ int getconnection_id(IP_Port ip_port); | |||
53 | return -1 if there are no new incoming connections in the list. */ | 50 | return -1 if there are no new incoming connections in the list. */ |
54 | int incoming_connection(); | 51 | int incoming_connection(); |
55 | 52 | ||
56 | |||
57 | /* return -1 if it could not kill the connection. | 53 | /* return -1 if it could not kill the connection. |
58 | return 0 if killed successfully */ | 54 | return 0 if killed successfully */ |
59 | int kill_connection(int connection_id); | 55 | int kill_connection(int connection_id); |
@@ -75,21 +71,16 @@ char id_packet(int connection_id); | |||
75 | return length of received packet if successful */ | 71 | return length of received packet if successful */ |
76 | int read_packet(int connection_id, uint8_t * data); | 72 | int read_packet(int connection_id, uint8_t * data); |
77 | 73 | ||
78 | |||
79 | /* return 0 if data could not be put in packet queue | 74 | /* return 0 if data could not be put in packet queue |
80 | return 1 if data was put into the queue */ | 75 | return 1 if data was put into the queue */ |
81 | int write_packet(int connection_id, uint8_t * data, uint32_t length); | 76 | int write_packet(int connection_id, uint8_t * data, uint32_t length); |
82 | 77 | ||
83 | |||
84 | |||
85 | /* returns the number of packets in the queue waiting to be successfully sent. */ | 78 | /* returns the number of packets in the queue waiting to be successfully sent. */ |
86 | uint32_t sendqueue(int connection_id); | 79 | uint32_t sendqueue(int connection_id); |
87 | 80 | ||
88 | |||
89 | /* returns the number of packets in the queue waiting to be successfully read with read_packet(...) */ | 81 | /* returns the number of packets in the queue waiting to be successfully read with read_packet(...) */ |
90 | uint32_t recvqueue(int connection_id); | 82 | uint32_t recvqueue(int connection_id); |
91 | 83 | ||
92 | |||
93 | /* check if connection is connected | 84 | /* check if connection is connected |
94 | return 0 no. | 85 | return 0 no. |
95 | return 1 if attempting handshake | 86 | return 1 if attempting handshake |
@@ -98,7 +89,6 @@ uint32_t recvqueue(int connection_id); | |||
98 | return 4 if timed out and wating to be killed */ | 89 | return 4 if timed out and wating to be killed */ |
99 | int is_connected(int connection_id); | 90 | int is_connected(int connection_id); |
100 | 91 | ||
101 | |||
102 | /* Call this function a couple times per second | 92 | /* Call this function a couple times per second |
103 | It's the main loop. */ | 93 | It's the main loop. */ |
104 | void doLossless_UDP(); | 94 | void doLossless_UDP(); |
diff --git a/core/Messenger.c b/core/Messenger.c index 043700e5..69d33172 100644 --- a/core/Messenger.c +++ b/core/Messenger.c | |||
@@ -1,26 +1,25 @@ | |||
1 | /* Messenger.c | 1 | /* Messenger.c |
2 | * | 2 | * |
3 | * An implementation of a simple text chat only messenger on the tox network core. | 3 | * An implementation of a simple text chat only messenger on the tox network core. |
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 | */ | ||
24 | 23 | ||
25 | #include "Messenger.h" | 24 | #include "Messenger.h" |
26 | #define MIN(a,b) (((a)<(b))?(a):(b)) | 25 | #define MIN(a,b) (((a)<(b))?(a):(b)) |
@@ -39,8 +38,6 @@ typedef struct | |||
39 | uint8_t userstatus_sent; | 38 | uint8_t userstatus_sent; |
40 | uint16_t info_size; /* length of the info */ | 39 | uint16_t info_size; /* length of the info */ |
41 | }Friend; | 40 | }Friend; |
42 | |||
43 | |||
44 | 41 | ||
45 | uint8_t self_public_key[crypto_box_PUBLICKEYBYTES]; | 42 | uint8_t self_public_key[crypto_box_PUBLICKEYBYTES]; |
46 | 43 | ||
@@ -58,26 +55,20 @@ static uint32_t numfriends; | |||
58 | 0 if we are offline | 55 | 0 if we are offline |
59 | static uint8_t online; */ | 56 | static uint8_t online; */ |
60 | 57 | ||
61 | |||
62 | /* return the friend id associated to that public key. | 58 | /* return the friend id associated to that public key. |
63 | return -1 if no such friend */ | 59 | return -1 if no such friend */ |
64 | int getfriend_id(uint8_t * client_id) | 60 | int getfriend_id(uint8_t * client_id) |
65 | { | 61 | { |
66 | uint32_t i; | 62 | uint32_t i; |
63 | |||
67 | for(i = 0; i < numfriends; ++i) | 64 | for(i = 0; i < numfriends; ++i) |
68 | { | ||
69 | if(friendlist[i].status > 0) | 65 | if(friendlist[i].status > 0) |
70 | { | ||
71 | if(memcmp(client_id, friendlist[i].client_id, crypto_box_PUBLICKEYBYTES) == 0) | 66 | if(memcmp(client_id, friendlist[i].client_id, crypto_box_PUBLICKEYBYTES) == 0) |
72 | { | ||
73 | return i; | 67 | return i; |
74 | } | 68 | |
75 | } | ||
76 | } | ||
77 | return -1; | 69 | return -1; |
78 | } | 70 | } |
79 | 71 | ||
80 | |||
81 | /* copies the public key associated to that friend id into client_id buffer. | 72 | /* copies the public key associated to that friend id into client_id buffer. |
82 | make sure that client_id is of size CLIENT_ID_SIZE. | 73 | make sure that client_id is of size CLIENT_ID_SIZE. |
83 | return 0 if success | 74 | return 0 if success |
@@ -85,19 +76,16 @@ int getfriend_id(uint8_t * client_id) | |||
85 | int getclient_id(int friend_id, uint8_t * client_id) | 76 | int getclient_id(int friend_id, uint8_t * client_id) |
86 | { | 77 | { |
87 | if(friend_id >= numfriends || friend_id < 0) | 78 | if(friend_id >= numfriends || friend_id < 0) |
88 | { | ||
89 | return -1; | 79 | return -1; |
90 | } | ||
91 | 80 | ||
92 | if(friendlist[friend_id].status > 0) | 81 | if(friendlist[friend_id].status > 0) { |
93 | { | ||
94 | memcpy(client_id, friendlist[friend_id].client_id, CLIENT_ID_SIZE); | 82 | memcpy(client_id, friendlist[friend_id].client_id, CLIENT_ID_SIZE); |
95 | return 0; | 83 | return 0; |
96 | } | 84 | } |
85 | |||
97 | return -1; | 86 | return -1; |
98 | } | 87 | } |
99 | 88 | ||
100 | |||
101 | /* add a friend | 89 | /* add a friend |
102 | set the data that will be sent along with friend request | 90 | set the data that will be sent along with friend request |
103 | client_id is the client id of the friend | 91 | client_id is the client id of the friend |
@@ -108,17 +96,11 @@ int m_addfriend(uint8_t * client_id, uint8_t * data, uint16_t length) | |||
108 | { | 96 | { |
109 | if(length == 0 || length >= | 97 | if(length == 0 || length >= |
110 | (MAX_DATA_SIZE - crypto_box_PUBLICKEYBYTES - crypto_box_NONCEBYTES - crypto_box_BOXZEROBYTES + crypto_box_ZEROBYTES)) | 98 | (MAX_DATA_SIZE - crypto_box_PUBLICKEYBYTES - crypto_box_NONCEBYTES - crypto_box_BOXZEROBYTES + crypto_box_ZEROBYTES)) |
111 | { | ||
112 | return -1; | 99 | return -1; |
113 | } | ||
114 | if(memcmp(client_id, self_public_key, crypto_box_PUBLICKEYBYTES) == 0) | 100 | if(memcmp(client_id, self_public_key, crypto_box_PUBLICKEYBYTES) == 0) |
115 | { | ||
116 | return -1; | 101 | return -1; |
117 | } | ||
118 | if(getfriend_id(client_id) != -1) | 102 | if(getfriend_id(client_id) != -1) |
119 | { | ||
120 | return -1; | 103 | return -1; |
121 | } | ||
122 | uint32_t i; | 104 | uint32_t i; |
123 | for(i = 0; i <= numfriends; ++i) | 105 | for(i = 0; i <= numfriends; ++i) |
124 | { | 106 | { |
@@ -144,9 +126,7 @@ int m_addfriend(uint8_t * client_id, uint8_t * data, uint16_t length) | |||
144 | int m_addfriend_norequest(uint8_t * client_id) | 126 | int m_addfriend_norequest(uint8_t * client_id) |
145 | { | 127 | { |
146 | if(getfriend_id(client_id) != -1) | 128 | if(getfriend_id(client_id) != -1) |
147 | { | ||
148 | return -1; | 129 | return -1; |
149 | } | ||
150 | uint32_t i; | 130 | uint32_t i; |
151 | for(i = 0; i <= numfriends; ++i) | 131 | for(i = 0; i <= numfriends; ++i) |
152 | { | 132 | { |
@@ -172,9 +152,7 @@ int m_addfriend_norequest(uint8_t * client_id) | |||
172 | int m_delfriend(int friendnumber) | 152 | int m_delfriend(int friendnumber) |
173 | { | 153 | { |
174 | if(friendnumber >= numfriends || friendnumber < 0) | 154 | if(friendnumber >= numfriends || friendnumber < 0) |
175 | { | ||
176 | return -1; | 155 | return -1; |
177 | } | ||
178 | 156 | ||
179 | DHT_delfriend(friendlist[friendnumber].client_id); | 157 | DHT_delfriend(friendlist[friendnumber].client_id); |
180 | crypto_kill(friendlist[friendnumber].crypt_connection_id); | 158 | crypto_kill(friendlist[friendnumber].crypt_connection_id); |
@@ -182,17 +160,12 @@ int m_delfriend(int friendnumber) | |||
182 | memset(&friendlist[friendnumber], 0, sizeof(Friend)); | 160 | memset(&friendlist[friendnumber], 0, sizeof(Friend)); |
183 | uint32_t i; | 161 | uint32_t i; |
184 | for(i = numfriends; i != 0; --i) | 162 | for(i = numfriends; i != 0; --i) |
185 | { | ||
186 | if(friendlist[i].status != 0) | 163 | if(friendlist[i].status != 0) |
187 | { | ||
188 | break; | 164 | break; |
189 | } | ||
190 | } | ||
191 | numfriends = i; | 165 | numfriends = i; |
192 | return 0; | 166 | return 0; |
193 | } | 167 | } |
194 | 168 | ||
195 | |||
196 | /* return 4 if friend is online | 169 | /* return 4 if friend is online |
197 | return 3 if friend is confirmed | 170 | return 3 if friend is confirmed |
198 | return 2 if the friend request was sent | 171 | return 2 if the friend request was sent |
@@ -201,27 +174,20 @@ int m_delfriend(int friendnumber) | |||
201 | int m_friendstatus(int friendnumber) | 174 | int m_friendstatus(int friendnumber) |
202 | { | 175 | { |
203 | if(friendnumber < 0 || friendnumber >= MAX_NUM_FRIENDS) | 176 | if(friendnumber < 0 || friendnumber >= MAX_NUM_FRIENDS) |
204 | { | ||
205 | return 0; | 177 | return 0; |
206 | } | ||
207 | return friendlist[friendnumber].status; | 178 | return friendlist[friendnumber].status; |
208 | } | 179 | } |
209 | 180 | ||
210 | |||
211 | /* send a text chat message to an online friend | 181 | /* send a text chat message to an online friend |
212 | return 1 if packet was successfully put into the send queue | 182 | return 1 if packet was successfully put into the send queue |
213 | return 0 if it was not */ | 183 | return 0 if it was not */ |
214 | int m_sendmessage(int friendnumber, uint8_t * message, uint32_t length) | 184 | int m_sendmessage(int friendnumber, uint8_t * message, uint32_t length) |
215 | { | 185 | { |
216 | if(friendnumber < 0 || friendnumber >= MAX_NUM_FRIENDS) | 186 | if(friendnumber < 0 || friendnumber >= MAX_NUM_FRIENDS) |
217 | { | ||
218 | return 0; | 187 | return 0; |
219 | } | ||
220 | if(length >= MAX_DATA_SIZE || friendlist[friendnumber].status != 4) | 188 | if(length >= MAX_DATA_SIZE || friendlist[friendnumber].status != 4) |
221 | /* this does not mean the maximum message length is MAX_DATA_SIZE - 1, it is actually 17 bytes less. */ | 189 | /* this does not mean the maximum message length is MAX_DATA_SIZE - 1, it is actually 17 bytes less. */ |
222 | { | ||
223 | return 0; | 190 | return 0; |
224 | } | ||
225 | uint8_t temp[MAX_DATA_SIZE]; | 191 | uint8_t temp[MAX_DATA_SIZE]; |
226 | temp[0] = PACKET_ID_MESSAGE; | 192 | temp[0] = PACKET_ID_MESSAGE; |
227 | memcpy(temp + 1, message, length); | 193 | memcpy(temp + 1, message, length); |
@@ -240,18 +206,14 @@ static int m_sendname(int friendnumber, uint8_t * name) | |||
240 | /* set the name of a friend | 206 | /* set the name of a friend |
241 | return 0 if success | 207 | return 0 if success |
242 | return -1 if failure */ | 208 | return -1 if failure */ |
243 | |||
244 | static int setfriendname(int friendnumber, uint8_t * name) | 209 | static int setfriendname(int friendnumber, uint8_t * name) |
245 | { | 210 | { |
246 | if(friendnumber >= numfriends || friendnumber < 0) | 211 | if(friendnumber >= numfriends || friendnumber < 0) |
247 | { | ||
248 | return -1; | 212 | return -1; |
249 | } | ||
250 | memcpy(friendlist[friendnumber].name, name, MAX_NAME_LENGTH); | 213 | memcpy(friendlist[friendnumber].name, name, MAX_NAME_LENGTH); |
251 | return 0; | 214 | return 0; |
252 | } | 215 | } |
253 | 216 | ||
254 | |||
255 | /* Set our nickname | 217 | /* Set our nickname |
256 | name must be a string of maximum MAX_NAME_LENGTH length. | 218 | name must be a string of maximum MAX_NAME_LENGTH length. |
257 | return 0 if success | 219 | return 0 if success |
@@ -259,15 +221,11 @@ static int setfriendname(int friendnumber, uint8_t * name) | |||
259 | int setname(uint8_t * name, uint16_t length) | 221 | int setname(uint8_t * name, uint16_t length) |
260 | { | 222 | { |
261 | if(length > MAX_NAME_LENGTH) | 223 | if(length > MAX_NAME_LENGTH) |
262 | { | ||
263 | return -1; | 224 | return -1; |
264 | } | ||
265 | memcpy(self_name, name, length); | 225 | memcpy(self_name, name, length); |
266 | uint32_t i; | 226 | uint32_t i; |
267 | for(i = 0; i < numfriends; ++i) | 227 | for(i = 0; i < numfriends; ++i) |
268 | { | ||
269 | friendlist[i].name_sent = 0; | 228 | friendlist[i].name_sent = 0; |
270 | } | ||
271 | return 0; | 229 | return 0; |
272 | } | 230 | } |
273 | 231 | ||
@@ -279,9 +237,7 @@ int setname(uint8_t * name, uint16_t length) | |||
279 | int getname(int friendnumber, uint8_t * name) | 237 | int getname(int friendnumber, uint8_t * name) |
280 | { | 238 | { |
281 | if(friendnumber >= numfriends || friendnumber < 0) | 239 | if(friendnumber >= numfriends || friendnumber < 0) |
282 | { | ||
283 | return -1; | 240 | return -1; |
284 | } | ||
285 | memcpy(name, friendlist[friendnumber].name, MAX_NAME_LENGTH); | 241 | memcpy(name, friendlist[friendnumber].name, MAX_NAME_LENGTH); |
286 | return 0; | 242 | return 0; |
287 | } | 243 | } |
@@ -289,9 +245,7 @@ int getname(int friendnumber, uint8_t * name) | |||
289 | int m_set_userstatus(uint8_t *status, uint16_t length) | 245 | int m_set_userstatus(uint8_t *status, uint16_t length) |
290 | { | 246 | { |
291 | if(length > MAX_USERSTATUS_LENGTH) | 247 | if(length > MAX_USERSTATUS_LENGTH) |
292 | { | ||
293 | return -1; | 248 | return -1; |
294 | } | ||
295 | uint8_t *newstatus = calloc(length, 1); | 249 | uint8_t *newstatus = calloc(length, 1); |
296 | memcpy(newstatus, status, length); | 250 | memcpy(newstatus, status, length); |
297 | free(self_userstatus); | 251 | free(self_userstatus); |
@@ -300,9 +254,7 @@ int m_set_userstatus(uint8_t *status, uint16_t length) | |||
300 | 254 | ||
301 | uint32_t i; | 255 | uint32_t i; |
302 | for(i = 0; i < numfriends; ++i) | 256 | for(i = 0; i < numfriends; ++i) |
303 | { | ||
304 | friendlist[i].userstatus_sent = 0; | 257 | friendlist[i].userstatus_sent = 0; |
305 | } | ||
306 | return 0; | 258 | return 0; |
307 | } | 259 | } |
308 | 260 | ||
@@ -311,9 +263,7 @@ int m_set_userstatus(uint8_t *status, uint16_t length) | |||
311 | int m_get_userstatus_size(int friendnumber) | 263 | int m_get_userstatus_size(int friendnumber) |
312 | { | 264 | { |
313 | if(friendnumber >= numfriends || friendnumber < 0) | 265 | if(friendnumber >= numfriends || friendnumber < 0) |
314 | { | ||
315 | return -1; | 266 | return -1; |
316 | } | ||
317 | return friendlist[friendnumber].userstatus_length; | 267 | return friendlist[friendnumber].userstatus_length; |
318 | } | 268 | } |
319 | 269 | ||
@@ -322,9 +272,7 @@ int m_get_userstatus_size(int friendnumber) | |||
322 | int m_copy_userstatus(int friendnumber, uint8_t * buf, uint32_t maxlen) | 272 | int m_copy_userstatus(int friendnumber, uint8_t * buf, uint32_t maxlen) |
323 | { | 273 | { |
324 | if(friendnumber >= numfriends || friendnumber < 0) | 274 | if(friendnumber >= numfriends || friendnumber < 0) |
325 | { | ||
326 | return -1; | 275 | return -1; |
327 | } | ||
328 | memset(buf, 0, maxlen); | 276 | memset(buf, 0, maxlen); |
329 | memcpy(buf, friendlist[friendnumber].userstatus, MIN(maxlen, MAX_USERSTATUS_LENGTH) - 1); | 277 | memcpy(buf, friendlist[friendnumber].userstatus, MIN(maxlen, MAX_USERSTATUS_LENGTH) - 1); |
330 | return 0; | 278 | return 0; |
@@ -343,9 +291,7 @@ static int send_userstatus(int friendnumber, uint8_t * status, uint16_t length) | |||
343 | static int set_friend_userstatus(int friendnumber, uint8_t * status, uint16_t length) | 291 | static int set_friend_userstatus(int friendnumber, uint8_t * status, uint16_t length) |
344 | { | 292 | { |
345 | if(friendnumber >= numfriends || friendnumber < 0) | 293 | if(friendnumber >= numfriends || friendnumber < 0) |
346 | { | ||
347 | return -1; | 294 | return -1; |
348 | } | ||
349 | uint8_t *newstatus = calloc(length, 1); | 295 | uint8_t *newstatus = calloc(length, 1); |
350 | memcpy(newstatus, status, length); | 296 | memcpy(newstatus, status, length); |
351 | free(friendlist[friendnumber].userstatus); | 297 | free(friendlist[friendnumber].userstatus); |
@@ -353,17 +299,15 @@ static int set_friend_userstatus(int friendnumber, uint8_t * status, uint16_t le | |||
353 | friendlist[friendnumber].userstatus_length = length; | 299 | friendlist[friendnumber].userstatus_length = length; |
354 | return 0; | 300 | return 0; |
355 | } | 301 | } |
356 | /* | 302 | |
357 | static void (*friend_request)(uint8_t *, uint8_t *, uint16_t); | 303 | /* static void (*friend_request)(uint8_t *, uint8_t *, uint16_t); |
358 | static uint8_t friend_request_isset = 0; | 304 | static uint8_t friend_request_isset = 0; */ |
359 | */ | ||
360 | /* set the function that will be executed when a friend request is received. */ | 305 | /* set the function that will be executed when a friend request is received. */ |
361 | void m_callback_friendrequest(void (*function)(uint8_t *, uint8_t *, uint16_t)) | 306 | void m_callback_friendrequest(void (*function)(uint8_t *, uint8_t *, uint16_t)) |
362 | { | 307 | { |
363 | callback_friendrequest(function); | 308 | callback_friendrequest(function); |
364 | } | 309 | } |
365 | 310 | ||
366 | |||
367 | static void (*friend_message)(int, uint8_t *, uint16_t); | 311 | static void (*friend_message)(int, uint8_t *, uint16_t); |
368 | static uint8_t friend_message_isset = 0; | 312 | static uint8_t friend_message_isset = 0; |
369 | 313 | ||
@@ -374,7 +318,6 @@ void m_callback_friendmessage(void (*function)(int, uint8_t *, uint16_t)) | |||
374 | friend_message_isset = 1; | 318 | friend_message_isset = 1; |
375 | } | 319 | } |
376 | 320 | ||
377 | |||
378 | static void (*friend_namechange)(int, uint8_t *, uint16_t); | 321 | static void (*friend_namechange)(int, uint8_t *, uint16_t); |
379 | static uint8_t friend_namechange_isset = 0; | 322 | static uint8_t friend_namechange_isset = 0; |
380 | void m_callback_namechange(void (*function)(int, uint8_t *, uint16_t)) | 323 | void m_callback_namechange(void (*function)(int, uint8_t *, uint16_t)) |
@@ -402,9 +345,9 @@ int initMessenger() | |||
402 | ip.i = 0; | 345 | ip.i = 0; |
403 | 346 | ||
404 | if(init_networking(ip,PORT) == -1) | 347 | if(init_networking(ip,PORT) == -1) |
405 | return -1; | 348 | return -1; |
406 | 349 | ||
407 | return 0; | 350 | return 0; |
408 | } | 351 | } |
409 | 352 | ||
410 | static void doFriends() | 353 | static void doFriends() |
@@ -417,16 +360,12 @@ static void doFriends() | |||
417 | if(friendlist[i].status == 1) | 360 | if(friendlist[i].status == 1) |
418 | { | 361 | { |
419 | int fr = send_friendrequest(friendlist[i].client_id, friendlist[i].info, friendlist[i].info_size); | 362 | int fr = send_friendrequest(friendlist[i].client_id, friendlist[i].info, friendlist[i].info_size); |
420 | if(fr == 0)/*TODO: This needs to be fixed so that it sends the friend requests a couple of times in case | 363 | if(fr == 0) /* TODO: This needs to be fixed so that it sends the friend requests a couple of times in case |
421 | of packet loss*/ | 364 | of packet loss */ |
422 | { | ||
423 | friendlist[i].status = 2; | 365 | friendlist[i].status = 2; |
424 | } | ||
425 | else | 366 | else |
426 | if(fr > 0) | 367 | if(fr > 0) |
427 | { | ||
428 | friendlist[i].status = 2; | 368 | friendlist[i].status = 2; |
429 | } | ||
430 | } | 369 | } |
431 | if(friendlist[i].status == 2 || friendlist[i].status == 3) /* friend is not online */ | 370 | if(friendlist[i].status == 2 || friendlist[i].status == 3) /* friend is not online */ |
432 | { | 371 | { |
@@ -436,12 +375,10 @@ static void doFriends() | |||
436 | { | 375 | { |
437 | send_friendrequest(friendlist[i].client_id, friendlist[i].info, friendlist[i].info_size); | 376 | send_friendrequest(friendlist[i].client_id, friendlist[i].info, friendlist[i].info_size); |
438 | friendlist[i].friend_request_id = unix_time(); | 377 | friendlist[i].friend_request_id = unix_time(); |
439 | |||
440 | } | 378 | } |
441 | } | 379 | } |
442 | IP_Port friendip = DHT_getfriendip(friendlist[i].client_id); | 380 | IP_Port friendip = DHT_getfriendip(friendlist[i].client_id); |
443 | switch(is_cryptoconnected(friendlist[i].crypt_connection_id)) | 381 | switch(is_cryptoconnected(friendlist[i].crypt_connection_id)) { |
444 | { | ||
445 | case 0: | 382 | case 0: |
446 | if (friendip.ip.i > 1) | 383 | if (friendip.ip.i > 1) |
447 | friendlist[i].crypt_connection_id = crypto_connect(friendlist[i].client_id, friendip); | 384 | friendlist[i].crypt_connection_id = crypto_connect(friendlist[i].client_id, friendip); |
@@ -460,19 +397,11 @@ static void doFriends() | |||
460 | while(friendlist[i].status == 4) /* friend is online */ | 397 | while(friendlist[i].status == 4) /* friend is online */ |
461 | { | 398 | { |
462 | if(friendlist[i].name_sent == 0) | 399 | if(friendlist[i].name_sent == 0) |
463 | { | ||
464 | if(m_sendname(i, self_name)) | 400 | if(m_sendname(i, self_name)) |
465 | { | ||
466 | friendlist[i].name_sent = 1; | 401 | friendlist[i].name_sent = 1; |
467 | } | ||
468 | } | ||
469 | if(friendlist[i].userstatus_sent == 0) | 402 | if(friendlist[i].userstatus_sent == 0) |
470 | { | ||
471 | if(send_userstatus(i, self_userstatus, self_userstatus_len)) | 403 | if(send_userstatus(i, self_userstatus, self_userstatus_len)) |
472 | { | ||
473 | friendlist[i].userstatus_sent = 1; | 404 | friendlist[i].userstatus_sent = 1; |
474 | } | ||
475 | } | ||
476 | len = read_cryptpacket(friendlist[i].crypt_connection_id, temp); | 405 | len = read_cryptpacket(friendlist[i].crypt_connection_id, temp); |
477 | if(len > 0) | 406 | if(len > 0) |
478 | { | 407 | { |
@@ -500,9 +429,7 @@ static void doFriends() | |||
500 | } | 429 | } |
501 | case PACKET_ID_MESSAGE: { | 430 | case PACKET_ID_MESSAGE: { |
502 | if(friend_message_isset) | 431 | if(friend_message_isset) |
503 | { | ||
504 | (*friend_message)(i, temp + 1, len - 1); | 432 | (*friend_message)(i, temp + 1, len - 1); |
505 | } | ||
506 | break; | 433 | break; |
507 | } | 434 | } |
508 | } | 435 | } |
@@ -521,8 +448,6 @@ static void doFriends() | |||
521 | } | 448 | } |
522 | } | 449 | } |
523 | 450 | ||
524 | |||
525 | |||
526 | static void doInbound() | 451 | static void doInbound() |
527 | { | 452 | { |
528 | uint8_t secret_nonce[crypto_box_NONCEBYTES]; | 453 | uint8_t secret_nonce[crypto_box_NONCEBYTES]; |
@@ -543,6 +468,22 @@ static void doInbound() | |||
543 | } | 468 | } |
544 | } | 469 | } |
545 | 470 | ||
471 | /*Interval in seconds between LAN discovery packet sending*/ | ||
472 | #define LAN_DISCOVERY_INTERVAL 60 | ||
473 | |||
474 | static uint32_t last_LANdiscovery; | ||
475 | |||
476 | /*Send a LAN discovery packet every LAN_DISCOVERY_INTERVAL seconds*/ | ||
477 | static void LANdiscovery() | ||
478 | { | ||
479 | if(last_LANdiscovery + LAN_DISCOVERY_INTERVAL < unix_time()) | ||
480 | { | ||
481 | send_LANdiscovery(htons(PORT)); | ||
482 | last_LANdiscovery = unix_time(); | ||
483 | } | ||
484 | } | ||
485 | |||
486 | |||
546 | /* the main loop that needs to be run at least 200 times per second. */ | 487 | /* the main loop that needs to be run at least 200 times per second. */ |
547 | void doMessenger() | 488 | void doMessenger() |
548 | { | 489 | { |
@@ -554,21 +495,19 @@ void doMessenger() | |||
554 | #ifdef DEBUG | 495 | #ifdef DEBUG |
555 | /* if(rand() % 3 != 1) //simulate packet loss */ | 496 | /* if(rand() % 3 != 1) //simulate packet loss */ |
556 | /* { */ | 497 | /* { */ |
557 | if(DHT_handlepacket(data, length, ip_port) && LosslessUDP_handlepacket(data, length, ip_port) && friendreq_handlepacket(data, length, ip_port)) | 498 | if(DHT_handlepacket(data, length, ip_port) && LosslessUDP_handlepacket(data, length, ip_port) && |
558 | { | 499 | friendreq_handlepacket(data, length, ip_port) && LANdiscovery_handlepacket(data, length, ip_port)) |
559 | /* if packet is discarded */ | 500 | /* if packet is discarded */ |
560 | printf("Received unhandled packet with length: %u\n", length); | 501 | printf("Received unhandled packet with length: %u\n", length); |
561 | } | ||
562 | else | 502 | else |
563 | { | ||
564 | printf("Received handled packet with length: %u\n", length); | 503 | printf("Received handled packet with length: %u\n", length); |
565 | } | ||
566 | /* } */ | 504 | /* } */ |
567 | printf("Status: %u %u %u\n",friendlist[0].status ,is_cryptoconnected(friendlist[0].crypt_connection_id), friendlist[0].crypt_connection_id); | 505 | printf("Status: %u %u %u\n",friendlist[0].status ,is_cryptoconnected(friendlist[0].crypt_connection_id), friendlist[0].crypt_connection_id); |
568 | #else | 506 | #else |
569 | DHT_handlepacket(data, length, ip_port); | 507 | DHT_handlepacket(data, length, ip_port); |
570 | LosslessUDP_handlepacket(data, length, ip_port); | 508 | LosslessUDP_handlepacket(data, length, ip_port); |
571 | friendreq_handlepacket(data, length, ip_port); | 509 | friendreq_handlepacket(data, length, ip_port); |
510 | LANdiscovery_handlepacket(data, length, ip_port); | ||
572 | #endif | 511 | #endif |
573 | 512 | ||
574 | } | 513 | } |
@@ -577,6 +516,7 @@ void doMessenger() | |||
577 | doNetCrypto(); | 516 | doNetCrypto(); |
578 | doInbound(); | 517 | doInbound(); |
579 | doFriends(); | 518 | doFriends(); |
519 | LANdiscovery(); | ||
580 | } | 520 | } |
581 | 521 | ||
582 | /* returns the size of the messenger data (for saving) */ | 522 | /* returns the size of the messenger data (for saving) */ |
@@ -606,13 +546,9 @@ void Messenger_save(uint8_t * data) | |||
606 | int Messenger_load(uint8_t * data, uint32_t length) | 546 | int Messenger_load(uint8_t * data, uint32_t length) |
607 | { | 547 | { |
608 | if(length == ~0) | 548 | if(length == ~0) |
609 | { | ||
610 | return -1; | 549 | return -1; |
611 | } | ||
612 | if(length < crypto_box_PUBLICKEYBYTES + crypto_box_SECRETKEYBYTES + sizeof(uint32_t) * 2) | 550 | if(length < crypto_box_PUBLICKEYBYTES + crypto_box_SECRETKEYBYTES + sizeof(uint32_t) * 2) |
613 | { | ||
614 | return -1; | 551 | return -1; |
615 | } | ||
616 | length -= crypto_box_PUBLICKEYBYTES + crypto_box_SECRETKEYBYTES + sizeof(uint32_t) * 2; | 552 | length -= crypto_box_PUBLICKEYBYTES + crypto_box_SECRETKEYBYTES + sizeof(uint32_t) * 2; |
617 | load_keys(data); | 553 | load_keys(data); |
618 | data += crypto_box_PUBLICKEYBYTES + crypto_box_SECRETKEYBYTES; | 554 | data += crypto_box_PUBLICKEYBYTES + crypto_box_SECRETKEYBYTES; |
@@ -621,21 +557,15 @@ int Messenger_load(uint8_t * data, uint32_t length) | |||
621 | data += sizeof(size); | 557 | data += sizeof(size); |
622 | 558 | ||
623 | if(length < size) | 559 | if(length < size) |
624 | { | ||
625 | return -1; | 560 | return -1; |
626 | } | ||
627 | length -= size; | 561 | length -= size; |
628 | if(DHT_load(data, size) == -1) | 562 | if(DHT_load(data, size) == -1) |
629 | { | ||
630 | return -1; | 563 | return -1; |
631 | } | ||
632 | data += size; | 564 | data += size; |
633 | memcpy(&size, data, sizeof(size)); | 565 | memcpy(&size, data, sizeof(size)); |
634 | data += sizeof(size); | 566 | data += sizeof(size); |
635 | if(length != size || length % sizeof(Friend) != 0) | 567 | if(length != size || length % sizeof(Friend) != 0) |
636 | { | ||
637 | return -1; | 568 | return -1; |
638 | } | ||
639 | 569 | ||
640 | Friend * temp = malloc(size); | 570 | Friend * temp = malloc(size); |
641 | memcpy(temp, data, size); | 571 | memcpy(temp, data, size); |
diff --git a/core/Messenger.h b/core/Messenger.h index 6afe84ac..1067d156 100644 --- a/core/Messenger.h +++ b/core/Messenger.h | |||
@@ -1,35 +1,35 @@ | |||
1 | /* Messenger.h | 1 | /* Messenger.h |
2 | * | 2 | * |
3 | * An implementation of a simple text chat only messenger on the tox network core. | 3 | * An implementation of a simple text chat only messenger on the tox network core. |
4 | * | 4 | * |
5 | * NOTE: All the text in the messages must be encoded using UTF-8 | 5 | * NOTE: All the text in the messages must be encoded using UTF-8 |
6 | 6 | * | |
7 | Copyright (C) 2013 Tox project All Rights Reserved. | 7 | * Copyright (C) 2013 Tox project All Rights Reserved. |
8 | 8 | * | |
9 | This file is part of Tox. | 9 | * This file is part of Tox. |
10 | 10 | * | |
11 | Tox is free software: you can redistribute it and/or modify | 11 | * Tox is free software: you can redistribute it and/or modify |
12 | it under the terms of the GNU General Public License as published by | 12 | * it under the terms of the GNU General Public License as published by |
13 | the Free Software Foundation, either version 3 of the License, or | 13 | * the Free Software Foundation, either version 3 of the License, or |
14 | (at your option) any later version. | 14 | * (at your option) any later version. |
15 | 15 | * | |
16 | Tox is distributed in the hope that it will be useful, | 16 | * Tox is distributed in the hope that it will be useful, |
17 | but WITHOUT ANY WARRANTY; without even the implied warranty of | 17 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
18 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | 18 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
19 | GNU General Public License for more details. | 19 | * GNU General Public License for more details. |
20 | 20 | * | |
21 | You should have received a copy of the GNU General Public License | 21 | * You should have received a copy of the GNU General Public License |
22 | along with Tox. If not, see <http://www.gnu.org/licenses/>. | 22 | * along with Tox. If not, see <http://www.gnu.org/licenses/>. |
23 | 23 | * | |
24 | */ | 24 | */ |
25 | 25 | ||
26 | |||
27 | #ifndef MESSENGER_H | 26 | #ifndef MESSENGER_H |
28 | #define MESSENGER_H | 27 | #define MESSENGER_H |
29 | 28 | ||
30 | #include "net_crypto.h" | 29 | #include "net_crypto.h" |
31 | #include "DHT.h" | 30 | #include "DHT.h" |
32 | #include "friend_requests.h" | 31 | #include "friend_requests.h" |
32 | #include "LAN_discovery.h" | ||
33 | 33 | ||
34 | #ifdef __cplusplus | 34 | #ifdef __cplusplus |
35 | extern "C" { | 35 | extern "C" { |
@@ -79,7 +79,6 @@ int m_delfriend(int friendnumber); | |||
79 | return 0 if there is no friend with that number */ | 79 | return 0 if there is no friend with that number */ |
80 | int m_friendstatus(int friendnumber); | 80 | int m_friendstatus(int friendnumber); |
81 | 81 | ||
82 | |||
83 | /* send a text chat message to an online friend | 82 | /* send a text chat message to an online friend |
84 | returns 1 if packet was successfully put into the send queue | 83 | returns 1 if packet was successfully put into the send queue |
85 | return 0 if it was not */ | 84 | return 0 if it was not */ |
@@ -91,7 +90,6 @@ int m_sendmessage(int friendnumber, uint8_t * message, uint32_t length); | |||
91 | return -1 if failure */ | 90 | return -1 if failure */ |
92 | int setname(uint8_t * name, uint16_t length); | 91 | int setname(uint8_t * name, uint16_t length); |
93 | 92 | ||
94 | |||
95 | /* get name of friendnumber | 93 | /* get name of friendnumber |
96 | put it in name | 94 | put it in name |
97 | name needs to be a valid memory location with a size of at least MAX_NAME_LENGTH (128) bytes. | 95 | name needs to be a valid memory location with a size of at least MAX_NAME_LENGTH (128) bytes. |
@@ -117,7 +115,6 @@ int m_copy_userstatus(int friendnumber, uint8_t * buf, uint32_t maxlen); | |||
117 | function format is function(uint8_t * public_key, uint8_t * data, uint16_t length) */ | 115 | function format is function(uint8_t * public_key, uint8_t * data, uint16_t length) */ |
118 | void m_callback_friendrequest(void (*function)(uint8_t *, uint8_t *, uint16_t)); | 116 | void m_callback_friendrequest(void (*function)(uint8_t *, uint8_t *, uint16_t)); |
119 | 117 | ||
120 | |||
121 | /* set the function that will be executed when a message from a friend is received. | 118 | /* set the function that will be executed when a message from a friend is received. |
122 | function format is: function(int friendnumber, uint8_t * message, uint32_t length) */ | 119 | function format is: function(int friendnumber, uint8_t * message, uint32_t length) */ |
123 | void m_callback_friendmessage(void (*function)(int, uint8_t *, uint16_t)); | 120 | void m_callback_friendmessage(void (*function)(int, uint8_t *, uint16_t)); |
@@ -137,11 +134,9 @@ void m_callback_userstatus(void (*function)(int, uint8_t *, uint16_t)); | |||
137 | returns -1 if there are problems */ | 134 | returns -1 if there are problems */ |
138 | int initMessenger(); | 135 | int initMessenger(); |
139 | 136 | ||
140 | |||
141 | /* the main loop that needs to be run at least 200 times per second */ | 137 | /* the main loop that needs to be run at least 200 times per second */ |
142 | void doMessenger(); | 138 | void doMessenger(); |
143 | 139 | ||
144 | |||
145 | /* SAVING AND LOADING FUNCTIONS: */ | 140 | /* SAVING AND LOADING FUNCTIONS: */ |
146 | 141 | ||
147 | /* returns the size of the messenger data (for saving) */ | 142 | /* returns the size of the messenger data (for saving) */ |
diff --git a/core/friend_requests.c b/core/friend_requests.c index 18f0866b..d1b0da57 100644 --- a/core/friend_requests.c +++ b/core/friend_requests.c | |||
@@ -2,13 +2,29 @@ | |||
2 | * | 2 | * |
3 | * Handle friend requests. | 3 | * Handle friend requests. |
4 | * | 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 | * | ||
5 | */ | 22 | */ |
6 | 23 | ||
7 | #include "friend_requests.h" | 24 | #include "friend_requests.h" |
8 | 25 | ||
9 | uint8_t self_public_key[crypto_box_PUBLICKEYBYTES]; | 26 | uint8_t self_public_key[crypto_box_PUBLICKEYBYTES]; |
10 | 27 | ||
11 | |||
12 | /* Try to send a friendrequest to peer with public_key | 28 | /* Try to send a friendrequest to peer with public_key |
13 | data is the data in the request and length is the length. | 29 | data is the data in the request and length is the length. |
14 | return -1 if failure. | 30 | return -1 if failure. |
@@ -18,33 +34,30 @@ int send_friendrequest(uint8_t * public_key, uint8_t * data, uint32_t length) | |||
18 | { | 34 | { |
19 | uint8_t packet[MAX_DATA_SIZE]; | 35 | uint8_t packet[MAX_DATA_SIZE]; |
20 | int len = create_request(packet, public_key, data, length, 32); /* 32 is friend request packet id */ | 36 | int len = create_request(packet, public_key, data, length, 32); /* 32 is friend request packet id */ |
37 | |||
21 | if(len == -1) | 38 | if(len == -1) |
22 | { | ||
23 | return -1; | 39 | return -1; |
24 | } | 40 | |
25 | IP_Port ip_port = DHT_getfriendip(public_key); | 41 | IP_Port ip_port = DHT_getfriendip(public_key); |
42 | |||
26 | if(ip_port.ip.i == 1) | 43 | if(ip_port.ip.i == 1) |
27 | { | ||
28 | return -1; | 44 | return -1; |
29 | } | 45 | |
30 | if(ip_port.ip.i != 0) | 46 | if(ip_port.ip.i != 0) |
31 | { | 47 | { |
32 | if(sendpacket(ip_port, packet, len) != -1) | 48 | if(sendpacket(ip_port, packet, len) != -1) |
33 | { | ||
34 | return 0; | 49 | return 0; |
35 | } | ||
36 | return -1; | 50 | return -1; |
37 | } | 51 | } |
38 | 52 | ||
39 | int num = route_tofriend(public_key, packet, len); | 53 | int num = route_tofriend(public_key, packet, len); |
54 | |||
40 | if(num == 0) | 55 | if(num == 0) |
41 | { | ||
42 | return -1; | 56 | return -1; |
43 | } | 57 | |
44 | return num; | 58 | return num; |
45 | } | 59 | } |
46 | 60 | ||
47 | |||
48 | static void (*handle_friendrequest)(uint8_t *, uint8_t *, uint16_t); | 61 | static void (*handle_friendrequest)(uint8_t *, uint8_t *, uint16_t); |
49 | static uint8_t handle_friendrequest_isset = 0; | 62 | static uint8_t handle_friendrequest_isset = 0; |
50 | 63 | ||
@@ -56,6 +69,38 @@ void callback_friendrequest(void (*function)(uint8_t *, uint8_t *, uint16_t)) | |||
56 | } | 69 | } |
57 | 70 | ||
58 | 71 | ||
72 | /*NOTE: the following is just a temporary fix for the multiple friend requests recieved at the same time problem | ||
73 | TODO: Make this better (This will most likely tie in with the way we will handle spam.)*/ | ||
74 | |||
75 | #define MAX_RECIEVED_STORED 32 | ||
76 | |||
77 | static uint8_t recieved_requests[MAX_RECIEVED_STORED][crypto_box_PUBLICKEYBYTES]; | ||
78 | static uint16_t recieved_requests_index; | ||
79 | |||
80 | /*Add to list of recieved friend requests*/ | ||
81 | static void addto_recievedlist(uint8_t * client_id) | ||
82 | { | ||
83 | if(recieved_requests_index >= MAX_RECIEVED_STORED) | ||
84 | recieved_requests_index = 0; | ||
85 | |||
86 | memcpy(recieved_requests[recieved_requests_index], client_id, crypto_box_PUBLICKEYBYTES); | ||
87 | ++recieved_requests_index; | ||
88 | } | ||
89 | |||
90 | /* Check if a friend request was already recieved | ||
91 | return 0 if not, 1 if we did */ | ||
92 | static int request_recieved(uint8_t * client_id) | ||
93 | { | ||
94 | uint32_t i; | ||
95 | |||
96 | for(i = 0; i < MAX_RECIEVED_STORED; ++i) | ||
97 | if(memcmp(recieved_requests[i], client_id, crypto_box_PUBLICKEYBYTES) == 0) | ||
98 | return 1; | ||
99 | |||
100 | return 0; | ||
101 | } | ||
102 | |||
103 | |||
59 | int friendreq_handlepacket(uint8_t * packet, uint32_t length, IP_Port source) | 104 | int friendreq_handlepacket(uint8_t * packet, uint32_t length, IP_Port source) |
60 | { | 105 | { |
61 | 106 | ||
@@ -63,31 +108,27 @@ int friendreq_handlepacket(uint8_t * packet, uint32_t length, IP_Port source) | |||
63 | { | 108 | { |
64 | if(length <= crypto_box_PUBLICKEYBYTES * 2 + crypto_box_NONCEBYTES + 1 + ENCRYPTION_PADDING && | 109 | if(length <= crypto_box_PUBLICKEYBYTES * 2 + crypto_box_NONCEBYTES + 1 + ENCRYPTION_PADDING && |
65 | length > MAX_DATA_SIZE + ENCRYPTION_PADDING) | 110 | length > MAX_DATA_SIZE + ENCRYPTION_PADDING) |
66 | { | ||
67 | return 1; | 111 | return 1; |
68 | } | 112 | if(memcmp(packet + 1, self_public_key, crypto_box_PUBLICKEYBYTES) == 0) /* check if request is for us. */ |
69 | if(memcmp(packet + 1, self_public_key, crypto_box_PUBLICKEYBYTES) == 0)//check if request is for us. | ||
70 | { | 113 | { |
71 | if(handle_friendrequest_isset == 0) | 114 | if(handle_friendrequest_isset == 0) |
72 | { | ||
73 | return 1; | 115 | return 1; |
74 | } | 116 | |
75 | uint8_t public_key[crypto_box_PUBLICKEYBYTES]; | 117 | uint8_t public_key[crypto_box_PUBLICKEYBYTES]; |
76 | uint8_t data[MAX_DATA_SIZE]; | 118 | uint8_t data[MAX_DATA_SIZE]; |
77 | int len = handle_request(public_key, data, packet, length); | 119 | int len = handle_request(public_key, data, packet, length); |
120 | |||
78 | if(len == -1) | 121 | if(len == -1) |
79 | { | ||
80 | return 1; | 122 | return 1; |
81 | } | 123 | if(request_recieved(public_key)) |
124 | return 1; | ||
125 | |||
126 | addto_recievedlist(public_key); | ||
82 | (*handle_friendrequest)(public_key, data, len); | 127 | (*handle_friendrequest)(public_key, data, len); |
83 | } | 128 | } |
84 | else//if request is not for us, try routing it. | 129 | else /* if request is not for us, try routing it. */ |
85 | { | ||
86 | if(route_packet(packet + 1, packet, length) == length) | 130 | if(route_packet(packet + 1, packet, length) == length) |
87 | { | ||
88 | return 0; | 131 | return 0; |
89 | } | ||
90 | } | ||
91 | } | 132 | } |
92 | return 1; | 133 | return 1; |
93 | } \ No newline at end of file | 134 | } |
diff --git a/core/friend_requests.h b/core/friend_requests.h index de116b33..29bc2b88 100644 --- a/core/friend_requests.h +++ b/core/friend_requests.h | |||
@@ -2,13 +2,28 @@ | |||
2 | * | 2 | * |
3 | * Handle friend requests. | 3 | * Handle friend requests. |
4 | * | 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 | * | ||
5 | */ | 22 | */ |
6 | 23 | ||
7 | |||
8 | #ifndef FRIEND_REQUESTS_H | 24 | #ifndef FRIEND_REQUESTS_H |
9 | #define FRIEND_REQUESTS_H | 25 | #define FRIEND_REQUESTS_H |
10 | 26 | ||
11 | |||
12 | #include "DHT.h" | 27 | #include "DHT.h" |
13 | #include "net_crypto.h" | 28 | #include "net_crypto.h" |
14 | 29 | ||
@@ -20,7 +35,6 @@ extern "C" { | |||
20 | data is the data in the request and length is the length. */ | 35 | data is the data in the request and length is the length. */ |
21 | int send_friendrequest(uint8_t * public_key, uint8_t * data, uint32_t length); | 36 | int send_friendrequest(uint8_t * public_key, uint8_t * data, uint32_t length); |
22 | 37 | ||
23 | |||
24 | /* set the function that will be executed when a friend request for us is received. | 38 | /* set the function that will be executed when a friend request for us is received. |
25 | function format is function(uint8_t * public_key, uint8_t * data, uint16_t length) */ | 39 | function format is function(uint8_t * public_key, uint8_t * data, uint16_t length) */ |
26 | void callback_friendrequest(void (*function)(uint8_t *, uint8_t *, uint16_t)); | 40 | void callback_friendrequest(void (*function)(uint8_t *, uint8_t *, uint16_t)); |
@@ -30,8 +44,6 @@ void callback_friendrequest(void (*function)(uint8_t *, uint8_t *, uint16_t)); | |||
30 | return 1 if it didn't handle the packet or if the packet was shit. */ | 44 | return 1 if it didn't handle the packet or if the packet was shit. */ |
31 | int friendreq_handlepacket(uint8_t * packet, uint32_t length, IP_Port source); | 45 | int friendreq_handlepacket(uint8_t * packet, uint32_t length, IP_Port source); |
32 | 46 | ||
33 | |||
34 | |||
35 | #ifdef __cplusplus | 47 | #ifdef __cplusplus |
36 | } | 48 | } |
37 | #endif | 49 | #endif |
diff --git a/core/net_crypto.c b/core/net_crypto.c index e01ed695..28cb83e8 100644 --- a/core/net_crypto.c +++ b/core/net_crypto.c | |||
@@ -1,38 +1,35 @@ | |||
1 | /* net_crypto.c | 1 | /* net_crypto.c |
2 | * | 2 | * |
3 | * Functions for the core network crypto. | 3 | * Functions for the core network crypto. |
4 | * See also: docs/Crypto.txt | 4 | * See also: docs/Crypto.txt |
5 | * | 5 | * |
6 | * NOTE: This code has to be perfect. We don't mess around with encryption. | 6 | * NOTE: This code has to be perfect. We don't mess around with encryption. |
7 | * | 7 | * |
8 | 8 | * Copyright (C) 2013 Tox project All Rights Reserved. | |
9 | Copyright (C) 2013 Tox project All Rights Reserved. | 9 | * |
10 | 10 | * This file is part of Tox. | |
11 | This file is part of Tox. | 11 | * |
12 | 12 | * Tox is free software: you can redistribute it and/or modify | |
13 | Tox is free software: you can redistribute it and/or modify | 13 | * it under the terms of the GNU General Public License as published by |
14 | it under the terms of the GNU General Public License as published by | 14 | * the Free Software Foundation, either version 3 of the License, or |
15 | the Free Software Foundation, either version 3 of the License, or | 15 | * (at your option) any later version. |
16 | (at your option) any later version. | 16 | * |
17 | 17 | * Tox is distributed in the hope that it will be useful, | |
18 | Tox is distributed in the hope that it will be useful, | 18 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
19 | but WITHOUT ANY WARRANTY; without even the implied warranty of | 19 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
20 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | 20 | * GNU General Public License for more details. |
21 | GNU General Public License for more details. | 21 | * |
22 | 22 | * You should have received a copy of the GNU General Public License | |
23 | You should have received a copy of the GNU General Public License | 23 | * along with Tox. If not, see <http://www.gnu.org/licenses/>. |
24 | along with Tox. If not, see <http://www.gnu.org/licenses/>. | 24 | * |
25 | 25 | */ | |
26 | */ | ||
27 | 26 | ||
28 | #include "net_crypto.h" | 27 | #include "net_crypto.h" |
29 | 28 | ||
30 | |||
31 | /* Our public and secret keys. */ | 29 | /* Our public and secret keys. */ |
32 | uint8_t self_public_key[crypto_box_PUBLICKEYBYTES]; | 30 | uint8_t self_public_key[crypto_box_PUBLICKEYBYTES]; |
33 | uint8_t self_secret_key[crypto_box_SECRETKEYBYTES]; | 31 | uint8_t self_secret_key[crypto_box_SECRETKEYBYTES]; |
34 | 32 | ||
35 | |||
36 | typedef struct | 33 | typedef struct |
37 | { | 34 | { |
38 | uint8_t public_key[crypto_box_PUBLICKEYBYTES]; /* the real public key of the peer. */ | 35 | uint8_t public_key[crypto_box_PUBLICKEYBYTES]; /* the real public key of the peer. */ |
@@ -65,9 +62,7 @@ int encrypt_data(uint8_t * public_key, uint8_t * secret_key, uint8_t * nonce, | |||
65 | uint8_t * plain, uint32_t length, uint8_t * encrypted) | 62 | uint8_t * plain, uint32_t length, uint8_t * encrypted) |
66 | { | 63 | { |
67 | if(length - crypto_box_BOXZEROBYTES + crypto_box_ZEROBYTES > MAX_DATA_SIZE || length == 0) | 64 | if(length - crypto_box_BOXZEROBYTES + crypto_box_ZEROBYTES > MAX_DATA_SIZE || length == 0) |
68 | { | ||
69 | return -1; | 65 | return -1; |
70 | } | ||
71 | 66 | ||
72 | uint8_t temp_plain[MAX_DATA_SIZE + crypto_box_ZEROBYTES - crypto_box_BOXZEROBYTES] = {0}; | 67 | uint8_t temp_plain[MAX_DATA_SIZE + crypto_box_ZEROBYTES - crypto_box_BOXZEROBYTES] = {0}; |
73 | uint8_t temp_encrypted[MAX_DATA_SIZE + crypto_box_ZEROBYTES]; | 68 | uint8_t temp_encrypted[MAX_DATA_SIZE + crypto_box_ZEROBYTES]; |
@@ -79,9 +74,8 @@ int encrypt_data(uint8_t * public_key, uint8_t * secret_key, uint8_t * nonce, | |||
79 | 74 | ||
80 | /* if encryption is successful the first crypto_box_BOXZEROBYTES of the message will be zero */ | 75 | /* if encryption is successful the first crypto_box_BOXZEROBYTES of the message will be zero */ |
81 | if(memcmp(temp_encrypted, zeroes, crypto_box_BOXZEROBYTES) != 0) | 76 | if(memcmp(temp_encrypted, zeroes, crypto_box_BOXZEROBYTES) != 0) |
82 | { | ||
83 | return -1; | 77 | return -1; |
84 | } | 78 | |
85 | /* unpad the encrypted message */ | 79 | /* unpad the encrypted message */ |
86 | memcpy(encrypted, temp_encrypted + crypto_box_BOXZEROBYTES, length - crypto_box_BOXZEROBYTES + crypto_box_ZEROBYTES); | 80 | memcpy(encrypted, temp_encrypted + crypto_box_BOXZEROBYTES, length - crypto_box_BOXZEROBYTES + crypto_box_ZEROBYTES); |
87 | return length - crypto_box_BOXZEROBYTES + crypto_box_ZEROBYTES; | 81 | return length - crypto_box_BOXZEROBYTES + crypto_box_ZEROBYTES; |
@@ -95,9 +89,8 @@ int decrypt_data(uint8_t * public_key, uint8_t * secret_key, uint8_t * nonce, | |||
95 | uint8_t * encrypted, uint32_t length, uint8_t * plain) | 89 | uint8_t * encrypted, uint32_t length, uint8_t * plain) |
96 | { | 90 | { |
97 | if(length > MAX_DATA_SIZE || length <= crypto_box_BOXZEROBYTES) | 91 | if(length > MAX_DATA_SIZE || length <= crypto_box_BOXZEROBYTES) |
98 | { | ||
99 | return -1; | 92 | return -1; |
100 | } | 93 | |
101 | uint8_t temp_plain[MAX_DATA_SIZE - crypto_box_ZEROBYTES + crypto_box_BOXZEROBYTES]; | 94 | uint8_t temp_plain[MAX_DATA_SIZE - crypto_box_ZEROBYTES + crypto_box_BOXZEROBYTES]; |
102 | uint8_t temp_encrypted[MAX_DATA_SIZE + crypto_box_ZEROBYTES] = {0}; | 95 | uint8_t temp_encrypted[MAX_DATA_SIZE + crypto_box_ZEROBYTES] = {0}; |
103 | uint8_t zeroes[crypto_box_ZEROBYTES] = {0}; | 96 | uint8_t zeroes[crypto_box_ZEROBYTES] = {0}; |
@@ -106,14 +99,12 @@ int decrypt_data(uint8_t * public_key, uint8_t * secret_key, uint8_t * nonce, | |||
106 | 99 | ||
107 | if(crypto_box_open(temp_plain, temp_encrypted, length + crypto_box_BOXZEROBYTES, | 100 | if(crypto_box_open(temp_plain, temp_encrypted, length + crypto_box_BOXZEROBYTES, |
108 | nonce, public_key, secret_key) == -1) | 101 | nonce, public_key, secret_key) == -1) |
109 | { | ||
110 | return -1; | 102 | return -1; |
111 | } | 103 | |
112 | /* if decryption is successful the first crypto_box_ZEROBYTES of the message will be zero */ | 104 | /* if decryption is successful the first crypto_box_ZEROBYTES of the message will be zero */ |
113 | if(memcmp(temp_plain, zeroes, crypto_box_ZEROBYTES) != 0) | 105 | if(memcmp(temp_plain, zeroes, crypto_box_ZEROBYTES) != 0) |
114 | { | ||
115 | return -1; | 106 | return -1; |
116 | } | 107 | |
117 | /* unpad the plain message */ | 108 | /* unpad the plain message */ |
118 | memcpy(plain, temp_plain + crypto_box_ZEROBYTES, length - crypto_box_ZEROBYTES + crypto_box_BOXZEROBYTES); | 109 | memcpy(plain, temp_plain + crypto_box_ZEROBYTES, length - crypto_box_ZEROBYTES + crypto_box_BOXZEROBYTES); |
119 | return length - crypto_box_ZEROBYTES + crypto_box_BOXZEROBYTES; | 110 | return length - crypto_box_ZEROBYTES + crypto_box_BOXZEROBYTES; |
@@ -123,13 +114,10 @@ int decrypt_data(uint8_t * public_key, uint8_t * secret_key, uint8_t * nonce, | |||
123 | void increment_nonce(uint8_t * nonce) | 114 | void increment_nonce(uint8_t * nonce) |
124 | { | 115 | { |
125 | uint32_t i; | 116 | uint32_t i; |
126 | for(i = 0; i < crypto_box_NONCEBYTES; ++i) | 117 | for(i = 0; i < crypto_box_NONCEBYTES; ++i) { |
127 | { | ||
128 | ++nonce[i]; | 118 | ++nonce[i]; |
129 | if(nonce[i] != 0) | 119 | if(nonce[i] != 0) |
130 | { | ||
131 | break; | 120 | break; |
132 | } | ||
133 | } | 121 | } |
134 | } | 122 | } |
135 | 123 | ||
@@ -137,8 +125,7 @@ void increment_nonce(uint8_t * nonce) | |||
137 | void random_nonce(uint8_t * nonce) | 125 | void random_nonce(uint8_t * nonce) |
138 | { | 126 | { |
139 | uint32_t i, temp; | 127 | uint32_t i, temp; |
140 | for (i = 0; i < crypto_box_NONCEBYTES / 4; ++i) | 128 | for (i = 0; i < crypto_box_NONCEBYTES / 4; ++i) { |
141 | { | ||
142 | temp = random_int(); | 129 | temp = random_int(); |
143 | memcpy(nonce + 4 * i, &temp, 4); | 130 | memcpy(nonce + 4 * i, &temp, 4); |
144 | } | 131 | } |
@@ -150,64 +137,44 @@ void random_nonce(uint8_t * nonce) | |||
150 | int read_cryptpacket(int crypt_connection_id, uint8_t * data) | 137 | int read_cryptpacket(int crypt_connection_id, uint8_t * data) |
151 | { | 138 | { |
152 | if(crypt_connection_id < 0 || crypt_connection_id >= MAX_CRYPTO_CONNECTIONS) | 139 | if(crypt_connection_id < 0 || crypt_connection_id >= MAX_CRYPTO_CONNECTIONS) |
153 | { | ||
154 | return 0; | 140 | return 0; |
155 | } | ||
156 | if(crypto_connections[crypt_connection_id].status != 3) | 141 | if(crypto_connections[crypt_connection_id].status != 3) |
157 | { | ||
158 | return 0; | 142 | return 0; |
159 | } | ||
160 | uint8_t temp_data[MAX_DATA_SIZE]; | 143 | uint8_t temp_data[MAX_DATA_SIZE]; |
161 | int length = read_packet(crypto_connections[crypt_connection_id].number, temp_data); | 144 | int length = read_packet(crypto_connections[crypt_connection_id].number, temp_data); |
162 | if(length == 0) | 145 | if(length == 0) |
163 | { | ||
164 | return 0; | 146 | return 0; |
165 | } | ||
166 | if(temp_data[0] != 3) | 147 | if(temp_data[0] != 3) |
167 | { | ||
168 | return -1; | 148 | return -1; |
169 | } | ||
170 | int len = decrypt_data(crypto_connections[crypt_connection_id].peersessionpublic_key, | 149 | int len = decrypt_data(crypto_connections[crypt_connection_id].peersessionpublic_key, |
171 | crypto_connections[crypt_connection_id].sessionsecret_key, | 150 | crypto_connections[crypt_connection_id].sessionsecret_key, |
172 | crypto_connections[crypt_connection_id].recv_nonce, temp_data + 1, length - 1, data); | 151 | crypto_connections[crypt_connection_id].recv_nonce, temp_data + 1, length - 1, data); |
173 | if(len != -1) | 152 | if(len != -1) { |
174 | { | ||
175 | increment_nonce(crypto_connections[crypt_connection_id].recv_nonce); | 153 | increment_nonce(crypto_connections[crypt_connection_id].recv_nonce); |
176 | return len; | 154 | return len; |
177 | } | 155 | } |
178 | return -1; | 156 | return -1; |
179 | } | 157 | } |
180 | 158 | ||
181 | |||
182 | /* return 0 if data could not be put in packet queue | 159 | /* return 0 if data could not be put in packet queue |
183 | return 1 if data was put into the queue */ | 160 | return 1 if data was put into the queue */ |
184 | int write_cryptpacket(int crypt_connection_id, uint8_t * data, uint32_t length) | 161 | int write_cryptpacket(int crypt_connection_id, uint8_t * data, uint32_t length) |
185 | { | 162 | { |
186 | if(crypt_connection_id < 0 || crypt_connection_id >= MAX_CRYPTO_CONNECTIONS) | 163 | if(crypt_connection_id < 0 || crypt_connection_id >= MAX_CRYPTO_CONNECTIONS) |
187 | { | ||
188 | return 0; | 164 | return 0; |
189 | } | ||
190 | if(length - crypto_box_BOXZEROBYTES + crypto_box_ZEROBYTES > MAX_DATA_SIZE - 1) | 165 | if(length - crypto_box_BOXZEROBYTES + crypto_box_ZEROBYTES > MAX_DATA_SIZE - 1) |
191 | { | ||
192 | return 0; | 166 | return 0; |
193 | } | ||
194 | if(crypto_connections[crypt_connection_id].status != 3) | 167 | if(crypto_connections[crypt_connection_id].status != 3) |
195 | { | ||
196 | return 0; | 168 | return 0; |
197 | } | ||
198 | uint8_t temp_data[MAX_DATA_SIZE]; | 169 | uint8_t temp_data[MAX_DATA_SIZE]; |
199 | int len = encrypt_data(crypto_connections[crypt_connection_id].peersessionpublic_key, | 170 | int len = encrypt_data(crypto_connections[crypt_connection_id].peersessionpublic_key, |
200 | crypto_connections[crypt_connection_id].sessionsecret_key, | 171 | crypto_connections[crypt_connection_id].sessionsecret_key, |
201 | crypto_connections[crypt_connection_id].sent_nonce, data, length, temp_data + 1); | 172 | crypto_connections[crypt_connection_id].sent_nonce, data, length, temp_data + 1); |
202 | if(len == -1) | 173 | if(len == -1) |
203 | { | ||
204 | return 0; | 174 | return 0; |
205 | } | ||
206 | temp_data[0] = 3; | 175 | temp_data[0] = 3; |
207 | if(write_packet(crypto_connections[crypt_connection_id].number, temp_data, len + 1) == 0) | 176 | if(write_packet(crypto_connections[crypt_connection_id].number, temp_data, len + 1) == 0) |
208 | { | ||
209 | return 0; | 177 | return 0; |
210 | } | ||
211 | increment_nonce(crypto_connections[crypt_connection_id].sent_nonce); | 178 | increment_nonce(crypto_connections[crypt_connection_id].sent_nonce); |
212 | return 1; | 179 | return 1; |
213 | } | 180 | } |
@@ -221,17 +188,13 @@ int write_cryptpacket(int crypt_connection_id, uint8_t * data, uint32_t length) | |||
221 | int create_request(uint8_t * packet, uint8_t * public_key, uint8_t * data, uint32_t length, uint8_t request_id) | 188 | int create_request(uint8_t * packet, uint8_t * public_key, uint8_t * data, uint32_t length, uint8_t request_id) |
222 | { | 189 | { |
223 | if(MAX_DATA_SIZE < length + 1 + crypto_box_PUBLICKEYBYTES * 2 + crypto_box_NONCEBYTES + ENCRYPTION_PADDING) | 190 | if(MAX_DATA_SIZE < length + 1 + crypto_box_PUBLICKEYBYTES * 2 + crypto_box_NONCEBYTES + ENCRYPTION_PADDING) |
224 | { | ||
225 | return -1; | 191 | return -1; |
226 | } | ||
227 | uint8_t nonce[crypto_box_NONCEBYTES]; | 192 | uint8_t nonce[crypto_box_NONCEBYTES]; |
228 | random_nonce(nonce); | 193 | random_nonce(nonce); |
229 | int len = encrypt_data(public_key, self_secret_key, nonce, data, length, | 194 | int len = encrypt_data(public_key, self_secret_key, nonce, data, length, |
230 | 1 + crypto_box_PUBLICKEYBYTES * 2 + crypto_box_NONCEBYTES + packet); | 195 | 1 + crypto_box_PUBLICKEYBYTES * 2 + crypto_box_NONCEBYTES + packet); |
231 | if(len == -1) | 196 | if(len == -1) |
232 | { | ||
233 | return -1; | 197 | return -1; |
234 | } | ||
235 | packet[0] = request_id; | 198 | packet[0] = request_id; |
236 | memcpy(packet + 1, public_key, crypto_box_PUBLICKEYBYTES); | 199 | memcpy(packet + 1, public_key, crypto_box_PUBLICKEYBYTES); |
237 | memcpy(packet + 1 + crypto_box_PUBLICKEYBYTES, self_public_key, crypto_box_PUBLICKEYBYTES); | 200 | memcpy(packet + 1 + crypto_box_PUBLICKEYBYTES, self_public_key, crypto_box_PUBLICKEYBYTES); |
@@ -257,18 +220,13 @@ int handle_request(uint8_t * public_key, uint8_t * data, uint8_t * packet, uint1 | |||
257 | int len1 = decrypt_data(public_key, self_secret_key, nonce, packet + 1 + crypto_box_PUBLICKEYBYTES * 2 + crypto_box_NONCEBYTES, | 220 | int len1 = decrypt_data(public_key, self_secret_key, nonce, packet + 1 + crypto_box_PUBLICKEYBYTES * 2 + crypto_box_NONCEBYTES, |
258 | length - (crypto_box_PUBLICKEYBYTES * 2 + crypto_box_NONCEBYTES + 1), data); | 221 | length - (crypto_box_PUBLICKEYBYTES * 2 + crypto_box_NONCEBYTES + 1), data); |
259 | if(len1 == -1) | 222 | if(len1 == -1) |
260 | { | ||
261 | return -1; | 223 | return -1; |
262 | } | ||
263 | return len1; | 224 | return len1; |
264 | } | 225 | } |
265 | else | 226 | else |
266 | { | ||
267 | return -1; | 227 | return -1; |
268 | } | ||
269 | } | 228 | } |
270 | 229 | ||
271 | |||
272 | /* Send a crypto handshake packet containing an encrypted secret nonce and session public key | 230 | /* Send a crypto handshake packet containing an encrypted secret nonce and session public key |
273 | to peer with connection_id and public_key | 231 | to peer with connection_id and public_key |
274 | the packet is encrypted with a random nonce which is sent in plain text with the packet */ | 232 | the packet is encrypted with a random nonce which is sent in plain text with the packet */ |
@@ -285,9 +243,7 @@ int send_cryptohandshake(int connection_id, uint8_t * public_key, uint8_t * secr | |||
285 | int len = encrypt_data(public_key, self_secret_key, nonce, temp, crypto_box_NONCEBYTES + crypto_box_PUBLICKEYBYTES, | 243 | int len = encrypt_data(public_key, self_secret_key, nonce, temp, crypto_box_NONCEBYTES + crypto_box_PUBLICKEYBYTES, |
286 | 1 + crypto_box_PUBLICKEYBYTES + crypto_box_NONCEBYTES + temp_data); | 244 | 1 + crypto_box_PUBLICKEYBYTES + crypto_box_NONCEBYTES + temp_data); |
287 | if(len == -1) | 245 | if(len == -1) |
288 | { | ||
289 | return 0; | 246 | return 0; |
290 | } | ||
291 | temp_data[0] = 2; | 247 | temp_data[0] = 2; |
292 | memcpy(temp_data + 1, self_public_key, crypto_box_PUBLICKEYBYTES); | 248 | memcpy(temp_data + 1, self_public_key, crypto_box_PUBLICKEYBYTES); |
293 | memcpy(temp_data + 1 + crypto_box_PUBLICKEYBYTES, nonce, crypto_box_NONCEBYTES); | 249 | memcpy(temp_data + 1 + crypto_box_PUBLICKEYBYTES, nonce, crypto_box_NONCEBYTES); |
@@ -307,9 +263,7 @@ int handle_cryptohandshake(uint8_t * public_key, uint8_t * secret_nonce, | |||
307 | return 0; | 263 | return 0; |
308 | } | 264 | } |
309 | if(data[0] != 2) | 265 | if(data[0] != 2) |
310 | { | ||
311 | return 0; | 266 | return 0; |
312 | } | ||
313 | uint8_t temp[crypto_box_NONCEBYTES + crypto_box_PUBLICKEYBYTES]; | 267 | uint8_t temp[crypto_box_NONCEBYTES + crypto_box_PUBLICKEYBYTES]; |
314 | 268 | ||
315 | memcpy(public_key, data + 1, crypto_box_PUBLICKEYBYTES); | 269 | memcpy(public_key, data + 1, crypto_box_PUBLICKEYBYTES); |
@@ -319,18 +273,13 @@ int handle_cryptohandshake(uint8_t * public_key, uint8_t * secret_nonce, | |||
319 | crypto_box_NONCEBYTES + crypto_box_PUBLICKEYBYTES + pad, temp); | 273 | crypto_box_NONCEBYTES + crypto_box_PUBLICKEYBYTES + pad, temp); |
320 | 274 | ||
321 | if(len != crypto_box_NONCEBYTES + crypto_box_PUBLICKEYBYTES) | 275 | if(len != crypto_box_NONCEBYTES + crypto_box_PUBLICKEYBYTES) |
322 | { | ||
323 | return 0; | 276 | return 0; |
324 | } | ||
325 | 277 | ||
326 | memcpy(secret_nonce, temp, crypto_box_NONCEBYTES); | 278 | memcpy(secret_nonce, temp, crypto_box_NONCEBYTES); |
327 | memcpy(session_key, temp + crypto_box_NONCEBYTES, crypto_box_PUBLICKEYBYTES); | 279 | memcpy(session_key, temp + crypto_box_NONCEBYTES, crypto_box_PUBLICKEYBYTES); |
328 | return 1; | 280 | return 1; |
329 | } | 281 | } |
330 | 282 | ||
331 | |||
332 | |||
333 | |||
334 | /* get crypto connection id from public key of peer | 283 | /* get crypto connection id from public key of peer |
335 | return -1 if there are no connections like we are looking for | 284 | return -1 if there are no connections like we are looking for |
336 | return id if it found it */ | 285 | return id if it found it */ |
@@ -338,19 +287,12 @@ int getcryptconnection_id(uint8_t * public_key) | |||
338 | { | 287 | { |
339 | uint32_t i; | 288 | uint32_t i; |
340 | for(i = 0; i < MAX_CRYPTO_CONNECTIONS; ++i) | 289 | for(i = 0; i < MAX_CRYPTO_CONNECTIONS; ++i) |
341 | { | ||
342 | if(crypto_connections[i].status > 0) | 290 | if(crypto_connections[i].status > 0) |
343 | { | ||
344 | if(memcmp(public_key, crypto_connections[i].public_key, crypto_box_PUBLICKEYBYTES) == 0) | 291 | if(memcmp(public_key, crypto_connections[i].public_key, crypto_box_PUBLICKEYBYTES) == 0) |
345 | { | ||
346 | return i; | 292 | return i; |
347 | } | ||
348 | } | ||
349 | } | ||
350 | return -1; | 293 | return -1; |
351 | } | 294 | } |
352 | 295 | ||
353 | |||
354 | /* Start a secure connection with other peer who has public_key and ip_port | 296 | /* Start a secure connection with other peer who has public_key and ip_port |
355 | returns -1 if failure | 297 | returns -1 if failure |
356 | returns crypt_connection_id of the initialized connection if everything went well. */ | 298 | returns crypt_connection_id of the initialized connection if everything went well. */ |
@@ -358,23 +300,17 @@ int crypto_connect(uint8_t * public_key, IP_Port ip_port) | |||
358 | { | 300 | { |
359 | uint32_t i; | 301 | uint32_t i; |
360 | int id = getcryptconnection_id(public_key); | 302 | int id = getcryptconnection_id(public_key); |
361 | if(id != -1) | 303 | if(id != -1) { |
362 | { | ||
363 | IP_Port c_ip = connection_ip(crypto_connections[id].number); | 304 | IP_Port c_ip = connection_ip(crypto_connections[id].number); |
364 | if(c_ip.ip.i == ip_port.ip.i && c_ip.port == ip_port.port) | 305 | if(c_ip.ip.i == ip_port.ip.i && c_ip.port == ip_port.port) |
365 | { | ||
366 | return -1; | 306 | return -1; |
367 | } | ||
368 | } | 307 | } |
369 | for(i = 0; i < MAX_CRYPTO_CONNECTIONS; ++i) | 308 | for(i = 0; i < MAX_CRYPTO_CONNECTIONS; ++i) |
370 | { | 309 | { |
371 | if(crypto_connections[i].status == 0) | 310 | if(crypto_connections[i].status == 0) { |
372 | { | ||
373 | int id = new_connection(ip_port); | 311 | int id = new_connection(ip_port); |
374 | if(id == -1) | 312 | if(id == -1) |
375 | { | ||
376 | return -1; | 313 | return -1; |
377 | } | ||
378 | crypto_connections[i].number = id; | 314 | crypto_connections[i].number = id; |
379 | crypto_connections[i].status = 1; | 315 | crypto_connections[i].status = 1; |
380 | random_nonce(crypto_connections[i].recv_nonce); | 316 | random_nonce(crypto_connections[i].recv_nonce); |
@@ -405,20 +341,16 @@ int crypto_inbound(uint8_t * public_key, uint8_t * secret_nonce, uint8_t * sessi | |||
405 | uint32_t i; | 341 | uint32_t i; |
406 | for(i = 0; i < MAX_INCOMING; ++i) | 342 | for(i = 0; i < MAX_INCOMING; ++i) |
407 | { | 343 | { |
408 | if(incoming_connections[i] != -1) | 344 | if(incoming_connections[i] != -1) { |
409 | { | 345 | if(is_connected(incoming_connections[i]) == 4 || is_connected(incoming_connections[i]) == 0) { |
410 | if(is_connected(incoming_connections[i]) == 4 || is_connected(incoming_connections[i]) == 0) | ||
411 | { | ||
412 | kill_connection(incoming_connections[i]); | 346 | kill_connection(incoming_connections[i]); |
413 | incoming_connections[i] = -1; | 347 | incoming_connections[i] = -1; |
414 | continue; | 348 | continue; |
415 | } | 349 | } |
416 | if(id_packet(incoming_connections[i]) == 2) | 350 | if(id_packet(incoming_connections[i]) == 2) { |
417 | { | ||
418 | uint8_t temp_data[MAX_DATA_SIZE]; | 351 | uint8_t temp_data[MAX_DATA_SIZE]; |
419 | uint16_t len = read_packet(incoming_connections[i], temp_data); | 352 | uint16_t len = read_packet(incoming_connections[i], temp_data); |
420 | if(handle_cryptohandshake(public_key, secret_nonce, session_key, temp_data, len)) | 353 | if(handle_cryptohandshake(public_key, secret_nonce, session_key, temp_data, len)) { |
421 | { | ||
422 | int connection_id = incoming_connections[i]; | 354 | int connection_id = incoming_connections[i]; |
423 | incoming_connections[i] = -1; /* remove this connection from the incoming connection list. */ | 355 | incoming_connections[i] = -1; /* remove this connection from the incoming connection list. */ |
424 | return connection_id; | 356 | return connection_id; |
@@ -435,11 +367,8 @@ int crypto_inbound(uint8_t * public_key, uint8_t * secret_nonce, uint8_t * sessi | |||
435 | int crypto_kill(int crypt_connection_id) | 367 | int crypto_kill(int crypt_connection_id) |
436 | { | 368 | { |
437 | if(crypt_connection_id < 0 || crypt_connection_id >= MAX_CRYPTO_CONNECTIONS) | 369 | if(crypt_connection_id < 0 || crypt_connection_id >= MAX_CRYPTO_CONNECTIONS) |
438 | { | ||
439 | return 1; | 370 | return 1; |
440 | } | 371 | if(crypto_connections[crypt_connection_id].status != 0) { |
441 | if(crypto_connections[crypt_connection_id].status != 0) | ||
442 | { | ||
443 | crypto_connections[crypt_connection_id].status = 0; | 372 | crypto_connections[crypt_connection_id].status = 0; |
444 | kill_connection(crypto_connections[crypt_connection_id].number); | 373 | kill_connection(crypto_connections[crypt_connection_id].number); |
445 | crypto_connections[crypt_connection_id].number = ~0; | 374 | crypto_connections[crypt_connection_id].number = ~0; |
@@ -448,7 +377,6 @@ int crypto_kill(int crypt_connection_id) | |||
448 | return 1; | 377 | return 1; |
449 | } | 378 | } |
450 | 379 | ||
451 | |||
452 | /* accept an incoming connection using the parameters provided by crypto_inbound | 380 | /* accept an incoming connection using the parameters provided by crypto_inbound |
453 | return -1 if not successful | 381 | return -1 if not successful |
454 | returns the crypt_connection_id if successful */ | 382 | returns the crypt_connection_id if successful */ |
@@ -456,9 +384,7 @@ int accept_crypto_inbound(int connection_id, uint8_t * public_key, uint8_t * sec | |||
456 | { | 384 | { |
457 | uint32_t i; | 385 | uint32_t i; |
458 | if(connection_id == -1) | 386 | if(connection_id == -1) |
459 | { | ||
460 | return -1; | 387 | return -1; |
461 | } | ||
462 | /* | 388 | /* |
463 | if(getcryptconnection_id(public_key) != -1) | 389 | if(getcryptconnection_id(public_key) != -1) |
464 | { | 390 | { |
@@ -466,8 +392,7 @@ int accept_crypto_inbound(int connection_id, uint8_t * public_key, uint8_t * sec | |||
466 | }*/ | 392 | }*/ |
467 | for(i = 0; i < MAX_CRYPTO_CONNECTIONS; ++i) | 393 | for(i = 0; i < MAX_CRYPTO_CONNECTIONS; ++i) |
468 | { | 394 | { |
469 | if(crypto_connections[i].status == 0) | 395 | if(crypto_connections[i].status == 0) { |
470 | { | ||
471 | crypto_connections[i].number = connection_id; | 396 | crypto_connections[i].number = connection_id; |
472 | crypto_connections[i].status = 2; | 397 | crypto_connections[i].status = 2; |
473 | random_nonce(crypto_connections[i].recv_nonce); | 398 | random_nonce(crypto_connections[i].recv_nonce); |
@@ -494,19 +419,16 @@ int accept_crypto_inbound(int connection_id, uint8_t * public_key, uint8_t * sec | |||
494 | return -1; | 419 | return -1; |
495 | } | 420 | } |
496 | 421 | ||
497 | /* return 0 if no connection, 1 we have sent a handshake, 2 if connexion is not confirmed yet | 422 | /* return 0 if no connection, 1 we have sent a handshake, 2 if connection is not confirmed yet |
498 | (we have received a handshake but no empty data packet), 3 if the connection is established. | 423 | (we have received a handshake but no empty data packet), 3 if the connection is established. |
499 | 4 if the connection is timed out and waiting to be killed */ | 424 | 4 if the connection is timed out and waiting to be killed */ |
500 | int is_cryptoconnected(int crypt_connection_id) | 425 | int is_cryptoconnected(int crypt_connection_id) |
501 | { | 426 | { |
502 | if(crypt_connection_id >= 0 && crypt_connection_id < MAX_CRYPTO_CONNECTIONS) | 427 | if(crypt_connection_id >= 0 && crypt_connection_id < MAX_CRYPTO_CONNECTIONS) |
503 | { | ||
504 | return crypto_connections[crypt_connection_id].status; | 428 | return crypto_connections[crypt_connection_id].status; |
505 | } | ||
506 | return 0; | 429 | return 0; |
507 | } | 430 | } |
508 | 431 | ||
509 | |||
510 | /* Generate our public and private keys | 432 | /* Generate our public and private keys |
511 | Only call this function the first time the program starts. */ | 433 | Only call this function the first time the program starts. */ |
512 | void new_keys() | 434 | void new_keys() |
@@ -537,10 +459,8 @@ void load_keys(uint8_t * keys) | |||
537 | int new_incoming(int id) | 459 | int new_incoming(int id) |
538 | { | 460 | { |
539 | uint32_t i; | 461 | uint32_t i; |
540 | for(i = 0; i < MAX_INCOMING; ++i) | 462 | for(i = 0; i < MAX_INCOMING; ++i) { |
541 | { | 463 | if(incoming_connections[i] == -1) { |
542 | if(incoming_connections[i] == -1) | ||
543 | { | ||
544 | incoming_connections[i] = id; | 464 | incoming_connections[i] = id; |
545 | return 0; | 465 | return 0; |
546 | } | 466 | } |
@@ -553,13 +473,10 @@ int new_incoming(int id) | |||
553 | static void handle_incomings() | 473 | static void handle_incomings() |
554 | { | 474 | { |
555 | int income; | 475 | int income; |
556 | while(1) | 476 | while(1) { |
557 | { | ||
558 | income = incoming_connection(); | 477 | income = incoming_connection(); |
559 | if(income == -1 || new_incoming(income) ) | 478 | if(income == -1 || new_incoming(income) ) |
560 | { | ||
561 | break; | 479 | break; |
562 | } | ||
563 | } | 480 | } |
564 | } | 481 | } |
565 | 482 | ||
@@ -578,17 +495,11 @@ static void receive_crypto() | |||
578 | uint16_t len; | 495 | uint16_t len; |
579 | if(id_packet(crypto_connections[i].number) == 1) | 496 | if(id_packet(crypto_connections[i].number) == 1) |
580 | /* if the packet is a friend request drop it (because we are already friends) */ | 497 | /* if the packet is a friend request drop it (because we are already friends) */ |
581 | { | ||
582 | len = read_packet(crypto_connections[i].number, temp_data); | 498 | len = read_packet(crypto_connections[i].number, temp_data); |
583 | 499 | if(id_packet(crypto_connections[i].number) == 2) { /* handle handshake packet. */ | |
584 | } | ||
585 | if(id_packet(crypto_connections[i].number) == 2) /* handle handshake packet. */ | ||
586 | { | ||
587 | len = read_packet(crypto_connections[i].number, temp_data); | 500 | len = read_packet(crypto_connections[i].number, temp_data); |
588 | if(handle_cryptohandshake(public_key, secret_nonce, session_key, temp_data, len)) | 501 | if(handle_cryptohandshake(public_key, secret_nonce, session_key, temp_data, len)) { |
589 | { | 502 | if(memcmp(public_key, crypto_connections[i].public_key, crypto_box_PUBLICKEYBYTES) == 0) { |
590 | if(memcmp(public_key, crypto_connections[i].public_key, crypto_box_PUBLICKEYBYTES) == 0) | ||
591 | { | ||
592 | memcpy(crypto_connections[i].sent_nonce, secret_nonce, crypto_box_NONCEBYTES); | 503 | memcpy(crypto_connections[i].sent_nonce, secret_nonce, crypto_box_NONCEBYTES); |
593 | memcpy(crypto_connections[i].peersessionpublic_key, session_key, crypto_box_PUBLICKEYBYTES); | 504 | memcpy(crypto_connections[i].peersessionpublic_key, session_key, crypto_box_PUBLICKEYBYTES); |
594 | increment_nonce(crypto_connections[i].sent_nonce); | 505 | increment_nonce(crypto_connections[i].sent_nonce); |
@@ -600,17 +511,14 @@ static void receive_crypto() | |||
600 | } | 511 | } |
601 | } | 512 | } |
602 | else if(id_packet(crypto_connections[i].number) != -1) | 513 | else if(id_packet(crypto_connections[i].number) != -1) |
603 | { | ||
604 | /* This should not happen | 514 | /* This should not happen |
605 | kill the connection if it does */ | 515 | kill the connection if it does */ |
606 | crypto_kill(crypto_connections[i].number); | 516 | crypto_kill(crypto_connections[i].number); |
607 | } | ||
608 | 517 | ||
609 | } | 518 | } |
610 | if(crypto_connections[i].status == 2) | 519 | if(crypto_connections[i].status == 2) |
611 | { | 520 | { |
612 | if(id_packet(crypto_connections[i].number) == 3) | 521 | if(id_packet(crypto_connections[i].number) == 3) { |
613 | { | ||
614 | uint8_t temp_data[MAX_DATA_SIZE]; | 522 | uint8_t temp_data[MAX_DATA_SIZE]; |
615 | uint8_t data[MAX_DATA_SIZE]; | 523 | uint8_t data[MAX_DATA_SIZE]; |
616 | int length = read_packet(crypto_connections[i].number, temp_data); | 524 | int length = read_packet(crypto_connections[i].number, temp_data); |
@@ -618,8 +526,7 @@ static void receive_crypto() | |||
618 | crypto_connections[i].sessionsecret_key, | 526 | crypto_connections[i].sessionsecret_key, |
619 | crypto_connections[i].recv_nonce, temp_data + 1, length - 1, data); | 527 | crypto_connections[i].recv_nonce, temp_data + 1, length - 1, data); |
620 | uint32_t zero = 0; | 528 | uint32_t zero = 0; |
621 | if(len == sizeof(uint32_t) && memcmp(((uint8_t *)&zero), data, sizeof(uint32_t)) == 0) | 529 | if(len == sizeof(uint32_t) && memcmp(((uint8_t *)&zero), data, sizeof(uint32_t)) == 0) { |
622 | { | ||
623 | increment_nonce(crypto_connections[i].recv_nonce); | 530 | increment_nonce(crypto_connections[i].recv_nonce); |
624 | crypto_connections[i].status = 3; | 531 | crypto_connections[i].status = 3; |
625 | 532 | ||
@@ -627,18 +534,14 @@ static void receive_crypto() | |||
627 | kill_connection_in(crypto_connections[i].number, 3000000); | 534 | kill_connection_in(crypto_connections[i].number, 3000000); |
628 | } | 535 | } |
629 | else | 536 | else |
630 | { | ||
631 | /* This should not happen | 537 | /* This should not happen |
632 | kill the connection if it does */ | 538 | kill the connection if it does */ |
633 | crypto_kill(crypto_connections[i].number); | 539 | crypto_kill(crypto_connections[i].number); |
634 | } | ||
635 | } | 540 | } |
636 | else if(id_packet(crypto_connections[i].number) != -1) | 541 | else if(id_packet(crypto_connections[i].number) != -1) |
637 | { | ||
638 | /* This should not happen | 542 | /* This should not happen |
639 | kill the connection if it does */ | 543 | kill the connection if it does */ |
640 | crypto_kill(crypto_connections[i].number); | 544 | crypto_kill(crypto_connections[i].number); |
641 | } | ||
642 | } | 545 | } |
643 | } | 546 | } |
644 | } | 547 | } |
@@ -651,22 +554,16 @@ void initNetCrypto() | |||
651 | memset(incoming_connections, -1 ,sizeof(incoming_connections)); | 554 | memset(incoming_connections, -1 ,sizeof(incoming_connections)); |
652 | uint32_t i; | 555 | uint32_t i; |
653 | for(i = 0; i < MAX_CRYPTO_CONNECTIONS; ++i) | 556 | for(i = 0; i < MAX_CRYPTO_CONNECTIONS; ++i) |
654 | { | ||
655 | crypto_connections[i].number = ~0; | 557 | crypto_connections[i].number = ~0; |
656 | } | ||
657 | } | 558 | } |
658 | 559 | ||
659 | static void killTimedout() | 560 | static void killTimedout() |
660 | { | 561 | { |
661 | uint32_t i; | 562 | uint32_t i; |
662 | for(i = 0; i < MAX_CRYPTO_CONNECTIONS; ++i) | 563 | for(i = 0; i < MAX_CRYPTO_CONNECTIONS; ++i) { |
663 | { | ||
664 | if(crypto_connections[i].status != 0 && is_connected(crypto_connections[i].number) == 4) | 564 | if(crypto_connections[i].status != 0 && is_connected(crypto_connections[i].number) == 4) |
665 | { | ||
666 | crypto_connections[i].status = 4; | 565 | crypto_connections[i].status = 4; |
667 | } | 566 | else if(is_connected(crypto_connections[i].number) == 4) { |
668 | else if(is_connected(crypto_connections[i].number) == 4) | ||
669 | { | ||
670 | kill_connection(crypto_connections[i].number); | 567 | kill_connection(crypto_connections[i].number); |
671 | crypto_connections[i].number = ~0; | 568 | crypto_connections[i].number = ~0; |
672 | } | 569 | } |
diff --git a/core/net_crypto.h b/core/net_crypto.h index b497f1fb..b5bab17a 100644 --- a/core/net_crypto.h +++ b/core/net_crypto.h | |||
@@ -1,26 +1,26 @@ | |||
1 | /* net_crypto.h | 1 | /* net_crypto.h |
2 | * | 2 | * |
3 | * Functions for the core network crypto. | 3 | * Functions for the core network crypto. |
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 | #ifndef NET_CRYPTO_H | 24 | #ifndef NET_CRYPTO_H |
25 | #define NET_CRYPTO_H | 25 | #define NET_CRYPTO_H |
26 | 26 | ||
@@ -55,13 +55,11 @@ int decrypt_data(uint8_t * public_key, uint8_t * secret_key, uint8_t * nonce, | |||
55 | /* fill the given nonce with random bytes. */ | 55 | /* fill the given nonce with random bytes. */ |
56 | void random_nonce(uint8_t * nonce); | 56 | void random_nonce(uint8_t * nonce); |
57 | 57 | ||
58 | |||
59 | /* return 0 if there is no received data in the buffer | 58 | /* return 0 if there is no received data in the buffer |
60 | return -1 if the packet was discarded. | 59 | return -1 if the packet was discarded. |
61 | return length of received data if successful */ | 60 | return length of received data if successful */ |
62 | int read_cryptpacket(int crypt_connection_id, uint8_t * data); | 61 | int read_cryptpacket(int crypt_connection_id, uint8_t * data); |
63 | 62 | ||
64 | |||
65 | /* return 0 if data could not be put in packet queue | 63 | /* return 0 if data could not be put in packet queue |
66 | return 1 if data was put into the queue */ | 64 | return 1 if data was put into the queue */ |
67 | int write_cryptpacket(int crypt_connection_id, uint8_t * data, uint32_t length); | 65 | int write_cryptpacket(int crypt_connection_id, uint8_t * data, uint32_t length); |
@@ -74,20 +72,17 @@ int write_cryptpacket(int crypt_connection_id, uint8_t * data, uint32_t length); | |||
74 | returns the length of the created packet on success */ | 72 | returns the length of the created packet on success */ |
75 | int create_request(uint8_t * packet, uint8_t * public_key, uint8_t * data, uint32_t length, uint8_t request_id); | 73 | int create_request(uint8_t * packet, uint8_t * public_key, uint8_t * data, uint32_t length, uint8_t request_id); |
76 | 74 | ||
77 | |||
78 | /* puts the senders public key in the request in public_key, the data from the request | 75 | /* puts the senders public key in the request in public_key, the data from the request |
79 | in data if a friend or ping request was sent to us and returns the length of the data. | 76 | in data if a friend or ping request was sent to us and returns the length of the data. |
80 | packet is the request packet and length is its length | 77 | packet is the request packet and length is its length |
81 | return -1 if not valid request. */ | 78 | return -1 if not valid request. */ |
82 | int handle_request(uint8_t * public_key, uint8_t * data, uint8_t * packet, uint16_t length); | 79 | int handle_request(uint8_t * public_key, uint8_t * data, uint8_t * packet, uint16_t length); |
83 | 80 | ||
84 | |||
85 | /* Start a secure connection with other peer who has public_key and ip_port | 81 | /* Start a secure connection with other peer who has public_key and ip_port |
86 | returns -1 if failure | 82 | returns -1 if failure |
87 | returns crypt_connection_id of the initialized connection if everything went well. */ | 83 | returns crypt_connection_id of the initialized connection if everything went well. */ |
88 | int crypto_connect(uint8_t * public_key, IP_Port ip_port); | 84 | int crypto_connect(uint8_t * public_key, IP_Port ip_port); |
89 | 85 | ||
90 | |||
91 | /* kill a crypto connection | 86 | /* kill a crypto connection |
92 | return 0 if killed successfully | 87 | return 0 if killed successfully |
93 | return 1 if there was a problem. */ | 88 | return 1 if there was a problem. */ |
@@ -102,7 +97,6 @@ int crypto_kill(int crypt_connection_id); | |||
102 | to refuse it just call kill_connection(...) on the connection id */ | 97 | to refuse it just call kill_connection(...) on the connection id */ |
103 | int crypto_inbound(uint8_t * public_key, uint8_t * secret_nonce, uint8_t * session_key); | 98 | int crypto_inbound(uint8_t * public_key, uint8_t * secret_nonce, uint8_t * session_key); |
104 | 99 | ||
105 | |||
106 | /* accept an incoming connection using the parameters provided by crypto_inbound | 100 | /* accept an incoming connection using the parameters provided by crypto_inbound |
107 | return -1 if not successful | 101 | return -1 if not successful |
108 | returns the crypt_connection_id if successful */ | 102 | returns the crypt_connection_id if successful */ |
diff --git a/core/network.c b/core/network.c index d4e25c82..ec234593 100644 --- a/core/network.c +++ b/core/network.c | |||
@@ -1,30 +1,28 @@ | |||
1 | /* network.h | 1 | /* network.h |
2 | * | 2 | * |
3 | * Functions for the core networking. | 3 | * Functions for the core networking. |
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 | */ | ||
24 | 23 | ||
25 | #include "network.h" | 24 | #include "network.h" |
26 | 25 | ||
27 | |||
28 | /* returns current UNIX time in microseconds (us). */ | 26 | /* returns current UNIX time in microseconds (us). */ |
29 | uint64_t current_time() | 27 | uint64_t current_time() |
30 | { | 28 | { |
@@ -44,8 +42,6 @@ uint64_t current_time() | |||
44 | time = 1000000UL*a.tv_sec + a.tv_usec; | 42 | time = 1000000UL*a.tv_sec + a.tv_usec; |
45 | return time; | 43 | return time; |
46 | #endif | 44 | #endif |
47 | |||
48 | |||
49 | } | 45 | } |
50 | 46 | ||
51 | /* return a random number | 47 | /* return a random number |
@@ -69,7 +65,6 @@ int sendpacket(IP_Port ip_port, uint8_t * data, uint32_t length) | |||
69 | { | 65 | { |
70 | ADDR addr = {AF_INET, ip_port.port, ip_port.ip}; | 66 | ADDR addr = {AF_INET, ip_port.port, ip_port.ip}; |
71 | return sendto(sock,(char *) data, length, 0, (struct sockaddr *)&addr, sizeof(addr)); | 67 | return sendto(sock,(char *) data, length, 0, (struct sockaddr *)&addr, sizeof(addr)); |
72 | |||
73 | } | 68 | } |
74 | 69 | ||
75 | /* Function to receive data, ip and port of sender is put into ip_port | 70 | /* Function to receive data, ip and port of sender is put into ip_port |
@@ -94,7 +89,6 @@ int receivepacket(IP_Port * ip_port, uint8_t * data, uint32_t * length) | |||
94 | ip_port->ip = addr.ip; | 89 | ip_port->ip = addr.ip; |
95 | ip_port->port = addr.port; | 90 | ip_port->port = addr.port; |
96 | return 0; | 91 | return 0; |
97 | |||
98 | } | 92 | } |
99 | 93 | ||
100 | /* initialize networking | 94 | /* initialize networking |
@@ -103,32 +97,28 @@ int receivepacket(IP_Port * ip_port, uint8_t * data, uint32_t * length) | |||
103 | port is in host byte order (this means don't worry about it) | 97 | port is in host byte order (this means don't worry about it) |
104 | returns 0 if no problems | 98 | returns 0 if no problems |
105 | returns -1 if there are problems */ | 99 | returns -1 if there are problems */ |
106 | int init_networking(IP ip ,uint16_t port) | 100 | int init_networking(IP ip, uint16_t port) |
107 | { | 101 | { |
108 | #ifdef WIN32 | 102 | #ifdef WIN32 |
109 | WSADATA wsaData; | 103 | WSADATA wsaData; |
110 | if(WSAStartup(MAKEWORD(2,2), &wsaData) != NO_ERROR) | 104 | if(WSAStartup(MAKEWORD(2,2), &wsaData) != NO_ERROR) |
111 | { | ||
112 | return -1; | 105 | return -1; |
113 | } | ||
114 | |||
115 | #else | 106 | #else |
116 | srandom((uint32_t)current_time()); | 107 | srandom((uint32_t)current_time()); |
117 | #endif | 108 | #endif |
118 | srand((uint32_t)current_time()); | 109 | srand((uint32_t)current_time()); |
119 | 110 | ||
120 | /* initialize our socket */ | 111 | /* initialize our socket */ |
121 | sock = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP); | 112 | sock = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP); |
122 | 113 | ||
123 | /* Check for socket error */ | 114 | /* Check for socket error */ |
124 | #ifdef WIN32 | 115 | #ifdef WIN32 |
125 | if (sock == INVALID_SOCKET) //MSDN recommends this | 116 | if (sock == INVALID_SOCKET) /* MSDN recommends this */ |
126 | return -1; | 117 | return -1; |
127 | #else | 118 | #else |
128 | if (sock < 0) | 119 | if (sock < 0) |
129 | return -1; | 120 | return -1; |
130 | #endif | 121 | #endif |
131 | |||
132 | 122 | ||
133 | /* Functions to increase the size of the send and receive UDP buffers | 123 | /* Functions to increase the size of the send and receive UDP buffers |
134 | NOTE: uncomment if necessary */ | 124 | NOTE: uncomment if necessary */ |
@@ -140,10 +130,13 @@ int init_networking(IP ip ,uint16_t port) | |||
140 | } | 130 | } |
141 | 131 | ||
142 | if(setsockopt(sock, SOL_SOCKET, SO_SNDBUF, (char*)&n, sizeof(n)) == -1) | 132 | if(setsockopt(sock, SOL_SOCKET, SO_SNDBUF, (char*)&n, sizeof(n)) == -1) |
143 | { | ||
144 | return -1; | 133 | return -1; |
145 | }*/ | 134 | */ |
146 | 135 | ||
136 | /* Enable broadcast on socket */ | ||
137 | int broadcast = 1; | ||
138 | setsockopt(sock, SOL_SOCKET, SO_BROADCAST, (char*)&broadcast, sizeof(broadcast)); | ||
139 | |||
147 | /* Set socket nonblocking */ | 140 | /* Set socket nonblocking */ |
148 | #ifdef WIN32 | 141 | #ifdef WIN32 |
149 | /* I think this works for windows */ | 142 | /* I think this works for windows */ |
@@ -190,11 +183,9 @@ int resolve_addr(char *address) | |||
190 | 183 | ||
191 | int success = getaddrinfo(address, "7", &hints, &server); | 184 | int success = getaddrinfo(address, "7", &hints, &server); |
192 | if(success != 0) | 185 | if(success != 0) |
193 | { | ||
194 | return -1; | 186 | return -1; |
195 | } | ||
196 | 187 | ||
197 | int resolved = ((struct sockaddr_in*)server->ai_addr)->sin_addr.s_addr; | 188 | int resolved = ((struct sockaddr_in*)server->ai_addr)->sin_addr.s_addr; |
198 | freeaddrinfo(server); | 189 | freeaddrinfo(server); |
199 | return resolved; | 190 | return resolved; |
200 | } \ No newline at end of file | 191 | } |
diff --git a/core/network.h b/core/network.h index 3b999cec..eaf12003 100644 --- a/core/network.h +++ b/core/network.h | |||
@@ -1,27 +1,25 @@ | |||
1 | /* network.h | 1 | /* network.h |
2 | * | 2 | * |
3 | * Datatypes, functions and includes for the core networking. | 3 | * Datatypes, functions and includes for the core networking. |
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 | */ | ||
24 | |||
25 | 23 | ||
26 | #ifndef NETWORK_H | 24 | #ifndef NETWORK_H |
27 | #define NETWORK_H | 25 | #define NETWORK_H |
@@ -32,8 +30,6 @@ | |||
32 | #include <string.h> | 30 | #include <string.h> |
33 | #include <time.h> | 31 | #include <time.h> |
34 | 32 | ||
35 | |||
36 | |||
37 | #ifdef WIN32 /* Put win32 includes here */ | 33 | #ifdef WIN32 /* Put win32 includes here */ |
38 | //Windows XP | 34 | //Windows XP |
39 | #define WINVER 0x0501 | 35 | #define WINVER 0x0501 |
@@ -99,7 +95,6 @@ typedef struct | |||
99 | #endif | 95 | #endif |
100 | }ADDR; | 96 | }ADDR; |
101 | 97 | ||
102 | |||
103 | /* returns current time in milleseconds since the epoch. */ | 98 | /* returns current time in milleseconds since the epoch. */ |
104 | uint64_t current_time(); | 99 | uint64_t current_time(); |
105 | 100 | ||