diff options
Diffstat (limited to 'core')
-rw-r--r-- | core/CMakeLists.txt | 1 | ||||
-rw-r--r-- | core/Messenger.c | 47 | ||||
-rw-r--r-- | core/Messenger.h | 1 | ||||
-rw-r--r-- | core/friend_requests.c | 93 | ||||
-rw-r--r-- | core/friend_requests.h | 34 | ||||
-rw-r--r-- | core/net_crypto.c | 139 | ||||
-rw-r--r-- | core/net_crypto.h | 26 |
7 files changed, 192 insertions, 149 deletions
diff --git a/core/CMakeLists.txt b/core/CMakeLists.txt index 51f30e17..ab4ff4bc 100644 --- a/core/CMakeLists.txt +++ b/core/CMakeLists.txt | |||
@@ -10,6 +10,7 @@ set(core_sources | |||
10 | network.c | 10 | network.c |
11 | Lossless_UDP.c | 11 | Lossless_UDP.c |
12 | net_crypto.c | 12 | net_crypto.c |
13 | friend_requests.c | ||
13 | Messenger.c) | 14 | Messenger.c) |
14 | 15 | ||
15 | add_library(core ${core_sources}) | 16 | add_library(core ${core_sources}) |
diff --git a/core/Messenger.c b/core/Messenger.c index 25ce067d..3d874837 100644 --- a/core/Messenger.c +++ b/core/Messenger.c | |||
@@ -353,15 +353,14 @@ static int set_friend_userstatus(int friendnumber, uint8_t * status, uint16_t le | |||
353 | friendlist[friendnumber].userstatus_length = length; | 353 | friendlist[friendnumber].userstatus_length = length; |
354 | return 0; | 354 | return 0; |
355 | } | 355 | } |
356 | 356 | /* | |
357 | static void (*friend_request)(uint8_t *, uint8_t *, uint16_t); | 357 | static void (*friend_request)(uint8_t *, uint8_t *, uint16_t); |
358 | static uint8_t friend_request_isset = 0; | 358 | static uint8_t friend_request_isset = 0; |
359 | 359 | */ | |
360 | /* set the function that will be executed when a friend request is received. */ | 360 | /* 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)) | 361 | void m_callback_friendrequest(void (*function)(uint8_t *, uint8_t *, uint16_t)) |
362 | { | 362 | { |
363 | friend_request = function; | 363 | callback_friendrequest(function); |
364 | friend_request_isset = 1; | ||
365 | } | 364 | } |
366 | 365 | ||
367 | 366 | ||
@@ -413,19 +412,20 @@ static void doFriends() | |||
413 | { | 412 | { |
414 | if(friendlist[i].status == 1) | 413 | if(friendlist[i].status == 1) |
415 | { | 414 | { |
416 | IP_Port friendip = DHT_getfriendip(friendlist[i].client_id); | 415 | int fr = send_friendrequest(friendlist[i].client_id, friendlist[i].info, friendlist[i].info_size); |
417 | int request = check_friendrequest(friendlist[i].friend_request_id); | 416 | if(fr == 0)/*TODO: This needs to be fixed so that it sends the friend requests a couple of times in case |
418 | /* printf("\n%u %u %u\n", friendip.ip.i, request, friendlist[i].friend_request_id); */ | 417 | of packet loss*/ |
419 | if(friendip.ip.i > 1 && request == -1) | 418 | { |
420 | { | 419 | friendlist[i].status = 2; |
421 | friendlist[i].friend_request_id = send_friendrequest(friendlist[i].client_id, | 420 | } |
422 | friendip, friendlist[i].info, friendlist[i].info_size); | 421 | else |
423 | friendlist[i].status = 2; | 422 | if(fr > 0) |
424 | } | 423 | { |
424 | friendlist[i].status = 2; | ||
425 | } | ||
425 | } | 426 | } |
426 | if(friendlist[i].status == 2 || friendlist[i].status == 3) /* friend is not online */ | 427 | if(friendlist[i].status == 2 || friendlist[i].status == 3) /* friend is not online */ |
427 | { | 428 | { |
428 | check_friendrequest(friendlist[i].friend_request_id); /* for now this is used to kill the friend request */ | ||
429 | IP_Port friendip = DHT_getfriendip(friendlist[i].client_id); | 429 | IP_Port friendip = DHT_getfriendip(friendlist[i].client_id); |
430 | switch(is_cryptoconnected(friendlist[i].crypt_connection_id)) | 430 | switch(is_cryptoconnected(friendlist[i].crypt_connection_id)) |
431 | { | 431 | { |
@@ -508,21 +508,6 @@ static void doFriends() | |||
508 | } | 508 | } |
509 | } | 509 | } |
510 | 510 | ||
511 | static void doFriendRequest() | ||
512 | { | ||
513 | uint8_t public_key[crypto_box_PUBLICKEYBYTES]; | ||
514 | uint8_t temp[MAX_DATA_SIZE]; | ||
515 | |||
516 | int len = handle_friendrequest(public_key, temp); | ||
517 | if(len >= 0) | ||
518 | { | ||
519 | if(friend_request_isset) | ||
520 | { | ||
521 | (*friend_request)(public_key, temp, len); | ||
522 | } | ||
523 | } | ||
524 | } | ||
525 | |||
526 | 511 | ||
527 | 512 | ||
528 | static void doInbound() | 513 | static void doInbound() |
@@ -556,7 +541,7 @@ void doMessenger() | |||
556 | #ifdef DEBUG | 541 | #ifdef DEBUG |
557 | /* if(rand() % 3 != 1) //simulate packet loss */ | 542 | /* if(rand() % 3 != 1) //simulate packet loss */ |
558 | /* { */ | 543 | /* { */ |
559 | if(DHT_handlepacket(data, length, ip_port) && LosslessUDP_handlepacket(data, length, ip_port)) | 544 | if(DHT_handlepacket(data, length, ip_port) && LosslessUDP_handlepacket(data, length, ip_port) && friendreq_handlepacket(data, length, ip_port)) |
560 | { | 545 | { |
561 | /* if packet is discarded */ | 546 | /* if packet is discarded */ |
562 | printf("Received unhandled packet with length: %u\n", length); | 547 | printf("Received unhandled packet with length: %u\n", length); |
@@ -570,6 +555,7 @@ void doMessenger() | |||
570 | #else | 555 | #else |
571 | DHT_handlepacket(data, length, ip_port); | 556 | DHT_handlepacket(data, length, ip_port); |
572 | LosslessUDP_handlepacket(data, length, ip_port); | 557 | LosslessUDP_handlepacket(data, length, ip_port); |
558 | friendreq_handlepacket(data, length, ip_port); | ||
573 | #endif | 559 | #endif |
574 | 560 | ||
575 | } | 561 | } |
@@ -577,7 +563,6 @@ void doMessenger() | |||
577 | doLossless_UDP(); | 563 | doLossless_UDP(); |
578 | doNetCrypto(); | 564 | doNetCrypto(); |
579 | doInbound(); | 565 | doInbound(); |
580 | doFriendRequest(); | ||
581 | doFriends(); | 566 | doFriends(); |
582 | } | 567 | } |
583 | 568 | ||
diff --git a/core/Messenger.h b/core/Messenger.h index 6fd98972..a7f114cf 100644 --- a/core/Messenger.h +++ b/core/Messenger.h | |||
@@ -29,6 +29,7 @@ | |||
29 | 29 | ||
30 | #include "net_crypto.h" | 30 | #include "net_crypto.h" |
31 | #include "DHT.h" | 31 | #include "DHT.h" |
32 | #include "friend_requests.h" | ||
32 | 33 | ||
33 | #define MAX_NAME_LENGTH 128 | 34 | #define MAX_NAME_LENGTH 128 |
34 | #define MAX_USERSTATUS_LENGTH 128 | 35 | #define MAX_USERSTATUS_LENGTH 128 |
diff --git a/core/friend_requests.c b/core/friend_requests.c new file mode 100644 index 00000000..18f0866b --- /dev/null +++ b/core/friend_requests.c | |||
@@ -0,0 +1,93 @@ | |||
1 | /* friend_requests.c | ||
2 | * | ||
3 | * Handle friend requests. | ||
4 | * | ||
5 | */ | ||
6 | |||
7 | #include "friend_requests.h" | ||
8 | |||
9 | uint8_t self_public_key[crypto_box_PUBLICKEYBYTES]; | ||
10 | |||
11 | |||
12 | /* Try to send a friendrequest to peer with public_key | ||
13 | data is the data in the request and length is the length. | ||
14 | return -1 if failure. | ||
15 | return 0 if it sent the friend request directly to the friend. | ||
16 | return the number of peers it was routed through if it did not send it directly.*/ | ||
17 | int send_friendrequest(uint8_t * public_key, uint8_t * data, uint32_t length) | ||
18 | { | ||
19 | uint8_t packet[MAX_DATA_SIZE]; | ||
20 | int len = create_request(packet, public_key, data, length, 32); /* 32 is friend request packet id */ | ||
21 | if(len == -1) | ||
22 | { | ||
23 | return -1; | ||
24 | } | ||
25 | IP_Port ip_port = DHT_getfriendip(public_key); | ||
26 | if(ip_port.ip.i == 1) | ||
27 | { | ||
28 | return -1; | ||
29 | } | ||
30 | if(ip_port.ip.i != 0) | ||
31 | { | ||
32 | if(sendpacket(ip_port, packet, len) != -1) | ||
33 | { | ||
34 | return 0; | ||
35 | } | ||
36 | return -1; | ||
37 | } | ||
38 | |||
39 | int num = route_tofriend(public_key, packet, len); | ||
40 | if(num == 0) | ||
41 | { | ||
42 | return -1; | ||
43 | } | ||
44 | return num; | ||
45 | } | ||
46 | |||
47 | |||
48 | static void (*handle_friendrequest)(uint8_t *, uint8_t *, uint16_t); | ||
49 | static uint8_t handle_friendrequest_isset = 0; | ||
50 | |||
51 | /* set the function that will be executed when a friend request is received. */ | ||
52 | void callback_friendrequest(void (*function)(uint8_t *, uint8_t *, uint16_t)) | ||
53 | { | ||
54 | handle_friendrequest = function; | ||
55 | handle_friendrequest_isset = 1; | ||
56 | } | ||
57 | |||
58 | |||
59 | int friendreq_handlepacket(uint8_t * packet, uint32_t length, IP_Port source) | ||
60 | { | ||
61 | |||
62 | if(packet[0] == 32) | ||
63 | { | ||
64 | if(length <= crypto_box_PUBLICKEYBYTES * 2 + crypto_box_NONCEBYTES + 1 + ENCRYPTION_PADDING && | ||
65 | length > MAX_DATA_SIZE + ENCRYPTION_PADDING) | ||
66 | { | ||
67 | return 1; | ||
68 | } | ||
69 | if(memcmp(packet + 1, self_public_key, crypto_box_PUBLICKEYBYTES) == 0)//check if request is for us. | ||
70 | { | ||
71 | if(handle_friendrequest_isset == 0) | ||
72 | { | ||
73 | return 1; | ||
74 | } | ||
75 | uint8_t public_key[crypto_box_PUBLICKEYBYTES]; | ||
76 | uint8_t data[MAX_DATA_SIZE]; | ||
77 | int len = handle_request(public_key, data, packet, length); | ||
78 | if(len == -1) | ||
79 | { | ||
80 | return 1; | ||
81 | } | ||
82 | (*handle_friendrequest)(public_key, data, len); | ||
83 | } | ||
84 | else//if request is not for us, try routing it. | ||
85 | { | ||
86 | if(route_packet(packet + 1, packet, length) == length) | ||
87 | { | ||
88 | return 0; | ||
89 | } | ||
90 | } | ||
91 | } | ||
92 | return 1; | ||
93 | } \ No newline at end of file | ||
diff --git a/core/friend_requests.h b/core/friend_requests.h new file mode 100644 index 00000000..a3de46c8 --- /dev/null +++ b/core/friend_requests.h | |||
@@ -0,0 +1,34 @@ | |||
1 | /* friend_requests.h | ||
2 | * | ||
3 | * Handle friend requests. | ||
4 | * | ||
5 | */ | ||
6 | |||
7 | |||
8 | #ifndef FRIEND_REQUESTS_H | ||
9 | #define FRIEND_REQUESTS_H | ||
10 | |||
11 | |||
12 | #include "DHT.h" | ||
13 | #include "net_crypto.h" | ||
14 | |||
15 | |||
16 | /* Try to send a friendrequest to peer with public_key | ||
17 | data is the data in the request and length is the length. */ | ||
18 | int send_friendrequest(uint8_t * public_key, uint8_t * data, uint32_t length); | ||
19 | |||
20 | |||
21 | /* set the function that will be executed when a friend request for us is received. | ||
22 | function format is function(uint8_t * public_key, uint8_t * data, uint16_t length) */ | ||
23 | void callback_friendrequest(void (*function)(uint8_t *, uint8_t *, uint16_t)); | ||
24 | |||
25 | /* if we receive a packet we call this function so it can be handled. | ||
26 | return 0 if packet is handled correctly. | ||
27 | return 1 if it didn't handle the packet or if the packet was shit. */ | ||
28 | int friendreq_handlepacket(uint8_t * packet, uint32_t length, IP_Port source); | ||
29 | |||
30 | |||
31 | |||
32 | |||
33 | |||
34 | #endif \ No newline at end of file | ||
diff --git a/core/net_crypto.c b/core/net_crypto.c index bdde7063..e01ed695 100644 --- a/core/net_crypto.c +++ b/core/net_crypto.c | |||
@@ -52,11 +52,6 @@ typedef struct | |||
52 | 52 | ||
53 | static Crypto_Connection crypto_connections[MAX_CRYPTO_CONNECTIONS]; | 53 | static Crypto_Connection crypto_connections[MAX_CRYPTO_CONNECTIONS]; |
54 | 54 | ||
55 | #define MAX_FRIEND_REQUESTS 32 | ||
56 | |||
57 | /* keeps track of the connection numbers for friends request so we can check later if they were sent */ | ||
58 | static int outbound_friendrequests[MAX_FRIEND_REQUESTS]; | ||
59 | |||
60 | #define MAX_INCOMING 64 | 55 | #define MAX_INCOMING 64 |
61 | 56 | ||
62 | /* keeps track of the connection numbers for friends request so we can check later if they were sent */ | 57 | /* keeps track of the connection numbers for friends request so we can check later if they were sent */ |
@@ -217,88 +212,63 @@ int write_cryptpacket(int crypt_connection_id, uint8_t * data, uint32_t length) | |||
217 | return 1; | 212 | return 1; |
218 | } | 213 | } |
219 | 214 | ||
220 | /* send a friend request to peer with public_key and ip_port. | 215 | /* create a request to peer with public_key. |
221 | Data represents the data we send with the friends request. | 216 | packet must be an array of MAX_DATA_SIZE big. |
217 | Data represents the data we send with the request with length being the length of the data. | ||
218 | request_id is the id of the request (32 = friend request, 254 = ping request) | ||
222 | returns -1 on failure | 219 | returns -1 on failure |
223 | returns a positive friend request id that can be used later to see if it was sent correctly on success. */ | 220 | returns the length of the created packet on success */ |
224 | int send_friendrequest(uint8_t * public_key, IP_Port ip_port, 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) |
225 | { | 222 | { |
226 | if(length > MAX_DATA_SIZE - 1 - crypto_box_PUBLICKEYBYTES - crypto_box_NONCEBYTES) | 223 | if(MAX_DATA_SIZE < length + 1 + crypto_box_PUBLICKEYBYTES * 2 + crypto_box_NONCEBYTES + ENCRYPTION_PADDING) |
227 | { | ||
228 | return -1; | ||
229 | } | ||
230 | uint32_t i; | ||
231 | for(i = 0; i < MAX_FRIEND_REQUESTS; ++i) | ||
232 | { | ||
233 | if(outbound_friendrequests[i] == -1) | ||
234 | { | ||
235 | break; | ||
236 | } | ||
237 | } | ||
238 | if(i == MAX_FRIEND_REQUESTS) | ||
239 | { | 224 | { |
240 | return -1; | 225 | return -1; |
241 | } | 226 | } |
242 | uint8_t temp_data[MAX_DATA_SIZE]; | ||
243 | uint8_t nonce[crypto_box_NONCEBYTES]; | 227 | uint8_t nonce[crypto_box_NONCEBYTES]; |
244 | random_nonce(nonce); | 228 | random_nonce(nonce); |
245 | int len = encrypt_data(public_key, self_secret_key, nonce, data, length, | 229 | int len = encrypt_data(public_key, self_secret_key, nonce, data, length, |
246 | 1 + crypto_box_PUBLICKEYBYTES + crypto_box_NONCEBYTES + temp_data); | 230 | 1 + crypto_box_PUBLICKEYBYTES * 2 + crypto_box_NONCEBYTES + packet); |
247 | if(len == -1) | 231 | if(len == -1) |
248 | { | 232 | { |
249 | return -1; | 233 | return -1; |
250 | } | 234 | } |
251 | temp_data[0] = 1; | 235 | packet[0] = request_id; |
252 | memcpy(temp_data + 1, self_public_key, crypto_box_PUBLICKEYBYTES); | 236 | memcpy(packet + 1, public_key, crypto_box_PUBLICKEYBYTES); |
253 | memcpy(temp_data + 1 + crypto_box_PUBLICKEYBYTES, nonce, crypto_box_NONCEBYTES); | 237 | memcpy(packet + 1 + crypto_box_PUBLICKEYBYTES, self_public_key, crypto_box_PUBLICKEYBYTES); |
254 | int id = new_connection(ip_port); | 238 | memcpy(packet + 1 + crypto_box_PUBLICKEYBYTES * 2, nonce, crypto_box_NONCEBYTES); |
255 | if(id == -1) | 239 | |
256 | { | 240 | return len + 1 + crypto_box_PUBLICKEYBYTES * 2 + crypto_box_NONCEBYTES; |
257 | return -1; | ||
258 | } | ||
259 | if(write_packet(id, temp_data, len + 1 + crypto_box_PUBLICKEYBYTES + crypto_box_NONCEBYTES) == 1) | ||
260 | { | ||
261 | outbound_friendrequests[i] = id; | ||
262 | return i; | ||
263 | } | ||
264 | return -1; | ||
265 | } | 241 | } |
266 | 242 | ||
267 | /* return -1 if failure | 243 | /* puts the senders public key in the request in public_key, the data from the request |
268 | return 0 if connection is still trying to send the request. | 244 | in data if a friend or ping request was sent to us and returns the length of the data. |
269 | return 1 if sent correctly | 245 | packet is the request packet and length is its length |
270 | return 2 if connection timed out */ | 246 | return -1 if not valid request. */ |
271 | int check_friendrequest(int friend_request) | 247 | int handle_request(uint8_t * public_key, uint8_t * data, uint8_t * packet, uint16_t length) |
272 | { | 248 | { |
273 | if(friend_request < 0 || friend_request > MAX_FRIEND_REQUESTS) | 249 | |
250 | if(length > crypto_box_PUBLICKEYBYTES * 2 + crypto_box_NONCEBYTES + 1 + ENCRYPTION_PADDING && | ||
251 | length <= MAX_DATA_SIZE + ENCRYPTION_PADDING && | ||
252 | memcmp(packet + 1, self_public_key, crypto_box_PUBLICKEYBYTES) == 0) | ||
274 | { | 253 | { |
275 | return -1; | 254 | memcpy(public_key, packet + 1 + crypto_box_PUBLICKEYBYTES, crypto_box_PUBLICKEYBYTES); |
255 | uint8_t nonce[crypto_box_NONCEBYTES]; | ||
256 | memcpy(nonce, packet + 1 + crypto_box_PUBLICKEYBYTES * 2, crypto_box_NONCEBYTES); | ||
257 | 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); | ||
259 | if(len1 == -1) | ||
260 | { | ||
261 | return -1; | ||
262 | } | ||
263 | return len1; | ||
276 | } | 264 | } |
277 | if(outbound_friendrequests[friend_request] == -1) | 265 | else |
278 | { | 266 | { |
279 | return -1; | 267 | return -1; |
280 | } | 268 | } |
281 | if(sendqueue(outbound_friendrequests[friend_request]) == 0) | ||
282 | { | ||
283 | kill_connection(outbound_friendrequests[friend_request]); | ||
284 | outbound_friendrequests[friend_request] = -1; | ||
285 | return 1; | ||
286 | } | ||
287 | int status = is_connected(outbound_friendrequests[friend_request]); | ||
288 | if(status == 4) | ||
289 | { | ||
290 | kill_connection(outbound_friendrequests[friend_request]); | ||
291 | outbound_friendrequests[friend_request] = -1; | ||
292 | return 2; | ||
293 | } | ||
294 | if(status == 0) | ||
295 | { | ||
296 | outbound_friendrequests[friend_request] = -1; | ||
297 | return 2; | ||
298 | } | ||
299 | return 0; | ||
300 | } | 269 | } |
301 | 270 | ||
271 | |||
302 | /* Send a crypto handshake packet containing an encrypted secret nonce and session public key | 272 | /* Send a crypto handshake packet containing an encrypted secret nonce and session public key |
303 | to peer with connection_id and public_key | 273 | to peer with connection_id and public_key |
304 | the packet is encrypted with a random nonce which is sent in plain text with the packet */ | 274 | the packet is encrypted with a random nonce which is sent in plain text with the packet */ |
@@ -359,43 +329,7 @@ int handle_cryptohandshake(uint8_t * public_key, uint8_t * secret_nonce, | |||
359 | } | 329 | } |
360 | 330 | ||
361 | 331 | ||
362 | /* puts the public key of the friend if public_key, the data from the request | 332 | |
363 | in data if a friend request was sent to us and returns the length of the data. | ||
364 | return -1 if no valid friend requests. */ | ||
365 | int handle_friendrequest(uint8_t * public_key, uint8_t * data) | ||
366 | { | ||
367 | uint32_t i; | ||
368 | for(i = 0; i < MAX_INCOMING; ++i) | ||
369 | { | ||
370 | if(incoming_connections[i] != -1) | ||
371 | { | ||
372 | if(id_packet(incoming_connections[i]) == 1) | ||
373 | { | ||
374 | uint8_t temp_data[MAX_DATA_SIZE]; | ||
375 | uint16_t len = read_packet(incoming_connections[i], temp_data); | ||
376 | if(len > crypto_box_PUBLICKEYBYTES + crypto_box_NONCEBYTES + 1 | ||
377 | - crypto_box_BOXZEROBYTES + crypto_box_ZEROBYTES) | ||
378 | { | ||
379 | memcpy(public_key, temp_data + 1, crypto_box_PUBLICKEYBYTES); | ||
380 | uint8_t nonce[crypto_box_NONCEBYTES]; | ||
381 | memcpy(nonce, temp_data + 1 + crypto_box_PUBLICKEYBYTES, crypto_box_NONCEBYTES); | ||
382 | int len1 = decrypt_data(public_key, self_secret_key, nonce, temp_data + 1 + crypto_box_PUBLICKEYBYTES + crypto_box_NONCEBYTES, | ||
383 | len - (crypto_box_PUBLICKEYBYTES + crypto_box_NONCEBYTES + 1), data); | ||
384 | if(len1 != -1) | ||
385 | { | ||
386 | kill_connection(incoming_connections[i]); | ||
387 | /* kill_connection_in(incoming_connections[i], 1); //conection is useless now, kill it in 1 seconds */ | ||
388 | incoming_connections[i] = -1; | ||
389 | return len1; | ||
390 | } | ||
391 | } | ||
392 | kill_connection(incoming_connections[i]); /* conection is useless now, kill it. */ | ||
393 | incoming_connections[i] = -1; | ||
394 | } | ||
395 | } | ||
396 | } | ||
397 | return -1; | ||
398 | } | ||
399 | 333 | ||
400 | /* get crypto connection id from public key of peer | 334 | /* get crypto connection id from public key of peer |
401 | return -1 if there are no connections like we are looking for | 335 | return -1 if there are no connections like we are looking for |
@@ -714,7 +648,6 @@ static void receive_crypto() | |||
714 | void initNetCrypto() | 648 | void initNetCrypto() |
715 | { | 649 | { |
716 | memset(crypto_connections, 0 ,sizeof(crypto_connections)); | 650 | memset(crypto_connections, 0 ,sizeof(crypto_connections)); |
717 | memset(outbound_friendrequests, -1 ,sizeof(outbound_friendrequests)); | ||
718 | memset(incoming_connections, -1 ,sizeof(incoming_connections)); | 651 | memset(incoming_connections, -1 ,sizeof(incoming_connections)); |
719 | uint32_t i; | 652 | uint32_t i; |
720 | for(i = 0; i < MAX_CRYPTO_CONNECTIONS; ++i) | 653 | for(i = 0; i < MAX_CRYPTO_CONNECTIONS; ++i) |
diff --git a/core/net_crypto.h b/core/net_crypto.h index 0bf21f60..5a282002 100644 --- a/core/net_crypto.h +++ b/core/net_crypto.h | |||
@@ -62,24 +62,20 @@ int read_cryptpacket(int crypt_connection_id, uint8_t * data); | |||
62 | return 1 if data was put into the queue */ | 62 | return 1 if data was put into the queue */ |
63 | int write_cryptpacket(int crypt_connection_id, uint8_t * data, uint32_t length); | 63 | int write_cryptpacket(int crypt_connection_id, uint8_t * data, uint32_t length); |
64 | 64 | ||
65 | /* send a friend request to peer with public_key and ip_port. | 65 | /* create a request to peer with public_key. |
66 | Data represents the data we send with the friends request. | 66 | packet must be an array of MAX_DATA_SIZE big. |
67 | Data represents the data we send with the request with length being the length of the data. | ||
68 | request_id is the id of the request (32 = friend request, 254 = ping request) | ||
67 | returns -1 on failure | 69 | returns -1 on failure |
68 | returns a positive friend request id that can be used later to see if it was sent correctly on success. */ | 70 | returns the length of the created packet on success */ |
69 | int send_friendrequest(uint8_t * public_key, IP_Port ip_port, uint8_t * data, uint32_t length); | 71 | int create_request(uint8_t * packet, uint8_t * public_key, uint8_t * data, uint32_t length, uint8_t request_id); |
70 | 72 | ||
71 | 73 | ||
72 | /* return -1 if failure | 74 | /* puts the senders public key in the request in public_key, the data from the request |
73 | return 0 if connection is still trying to send the request. | 75 | in data if a friend or ping request was sent to us and returns the length of the data. |
74 | return 1 if sent correctly | 76 | packet is the request packet and length is its length |
75 | return 2 if connection timed out */ | 77 | return -1 if not valid request. */ |
76 | int check_friendrequest(int friend_request); | 78 | int handle_request(uint8_t * public_key, uint8_t * data, uint8_t * packet, uint16_t length); |
77 | |||
78 | |||
79 | /* puts the public key of the friend if public_key, the data from the request | ||
80 | in data if a friend request was sent to us and returns the length of the data. | ||
81 | return -1 if no valid friend requests. */ | ||
82 | int handle_friendrequest(uint8_t * public_key, uint8_t * data); | ||
83 | 79 | ||
84 | 80 | ||
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 |