diff options
author | irungentoo <irungentoo@gmail.com> | 2014-01-13 20:11:54 -0500 |
---|---|---|
committer | irungentoo <irungentoo@gmail.com> | 2014-01-13 20:11:54 -0500 |
commit | 878762a8e4ec6873daa29bcf32a9416844943ae2 (patch) | |
tree | 2047f3790f7663be4c2ede59b9f9f47aa07fb776 /toxcore | |
parent | 675487936e5424021e1cef677643f469a5ed03ec (diff) |
Added functions to add/remove friends in onion_client.
Added a tiny optimization to net_crypto.
Diffstat (limited to 'toxcore')
-rw-r--r-- | toxcore/net_crypto.c | 10 | ||||
-rw-r--r-- | toxcore/onion_client.c | 121 | ||||
-rw-r--r-- | toxcore/onion_client.h | 27 |
3 files changed, 152 insertions, 6 deletions
diff --git a/toxcore/net_crypto.c b/toxcore/net_crypto.c index aec2a9e7..a6c3ecd9 100644 --- a/toxcore/net_crypto.c +++ b/toxcore/net_crypto.c | |||
@@ -473,8 +473,9 @@ static int getcryptconnection_id(Net_Crypto *c, uint8_t *public_key) | |||
473 | /* Set the size of the friend list to numfriends. | 473 | /* Set the size of the friend list to numfriends. |
474 | * | 474 | * |
475 | * return -1 if realloc fails. | 475 | * return -1 if realloc fails. |
476 | * return 0 if it succeeds. | ||
476 | */ | 477 | */ |
477 | int realloc_cryptoconnection(Net_Crypto *c, uint32_t num) | 478 | static int realloc_cryptoconnection(Net_Crypto *c, uint32_t num) |
478 | { | 479 | { |
479 | if (num == 0) { | 480 | if (num == 0) { |
480 | free(c->crypto_connections); | 481 | free(c->crypto_connections); |
@@ -604,8 +605,11 @@ int crypto_kill(Net_Crypto *c, int crypt_connection_id) | |||
604 | break; | 605 | break; |
605 | } | 606 | } |
606 | 607 | ||
607 | c->crypto_connections_length = i; | 608 | if (c->crypto_connections_length != i) { |
608 | realloc_cryptoconnection(c, c->crypto_connections_length); | 609 | c->crypto_connections_length = i; |
610 | realloc_cryptoconnection(c, c->crypto_connections_length); | ||
611 | } | ||
612 | |||
609 | return 0; | 613 | return 0; |
610 | } | 614 | } |
611 | 615 | ||
diff --git a/toxcore/onion_client.c b/toxcore/onion_client.c index f4ac9e01..2d049f7a 100644 --- a/toxcore/onion_client.c +++ b/toxcore/onion_client.c | |||
@@ -319,6 +319,125 @@ static int handle_data_response(void *object, IP_Port source, uint8_t *packet, u | |||
319 | return 0; | 319 | return 0; |
320 | } | 320 | } |
321 | 321 | ||
322 | /* Get the friend_num of a friend. | ||
323 | * | ||
324 | * return -1 on failure. | ||
325 | * return friend number on success. | ||
326 | */ | ||
327 | int onion_friend_num(Onion_Client *onion_c, uint8_t *client_id) | ||
328 | { | ||
329 | uint32_t i; | ||
330 | |||
331 | for (i = 0; i < onion_c->num_friends; ++i) { | ||
332 | if (onion_c->friends_list[i].status == 0) | ||
333 | continue; | ||
334 | |||
335 | if (memcmp(client_id, onion_c->friends_list[i].real_client_id, crypto_box_PUBLICKEYBYTES) == 0) | ||
336 | return i; | ||
337 | } | ||
338 | |||
339 | return -1; | ||
340 | } | ||
341 | |||
342 | /* Set the size of the friend list to num. | ||
343 | * | ||
344 | * return -1 if realloc fails. | ||
345 | * return 0 if it succeeds. | ||
346 | */ | ||
347 | static int realloc_onion_friends(Onion_Client *onion_c, uint32_t num) | ||
348 | { | ||
349 | if (num == 0) { | ||
350 | free(onion_c->friends_list); | ||
351 | onion_c->friends_list = NULL; | ||
352 | return 0; | ||
353 | } | ||
354 | |||
355 | Onion_Friend *newonion_friends = realloc(onion_c->friends_list, num * sizeof(Onion_Friend)); | ||
356 | |||
357 | if (newonion_friends == NULL) | ||
358 | return -1; | ||
359 | |||
360 | onion_c->friends_list = newonion_friends; | ||
361 | return 0; | ||
362 | } | ||
363 | |||
364 | /* Add a friend who we want to connect to. | ||
365 | * | ||
366 | * return -1 on failure. | ||
367 | * return the friend number on success or if the friend was already added. | ||
368 | */ | ||
369 | int onion_addfriend(Onion_Client *onion_c, uint8_t *client_id) | ||
370 | { | ||
371 | int num = onion_friend_num(onion_c, client_id); | ||
372 | |||
373 | if (num != -1) | ||
374 | return num; | ||
375 | |||
376 | uint32_t i, index = ~0; | ||
377 | |||
378 | for (i = 0; i < onion_c->num_friends; ++i) { | ||
379 | if (onion_c->friends_list[i].status == 0) { | ||
380 | index = i; | ||
381 | break; | ||
382 | } | ||
383 | } | ||
384 | |||
385 | if (index == ~0) { | ||
386 | if (realloc_onion_friends(onion_c, onion_c->num_friends + 1) == -1) | ||
387 | return -1; | ||
388 | |||
389 | index = onion_c->num_friends; | ||
390 | memset(&(onion_c->friends_list[onion_c->num_friends]), 0, sizeof(Onion_Friend)); | ||
391 | ++onion_c->num_friends; | ||
392 | } | ||
393 | |||
394 | onion_c->friends_list[index].status = 1; | ||
395 | memcpy(onion_c->friends_list[index].real_client_id, client_id, crypto_box_PUBLICKEYBYTES); | ||
396 | return index; | ||
397 | } | ||
398 | |||
399 | /* Delete a friend. | ||
400 | * | ||
401 | * return -1 on failure. | ||
402 | * return the deleted friend number on success. | ||
403 | */ | ||
404 | int onion_delfriend(Onion_Client *onion_c, int friend_num) | ||
405 | { | ||
406 | if ((uint32_t)friend_num >= onion_c->num_friends) | ||
407 | return -1; | ||
408 | |||
409 | //TODO | ||
410 | memset(&(onion_c->friends_list[friend_num]), 0, sizeof(Onion_Friend)); | ||
411 | uint32_t i; | ||
412 | |||
413 | for (i = onion_c->num_friends; i != 0; --i) { | ||
414 | if (onion_c->friends_list[i].status != 0) | ||
415 | break; | ||
416 | } | ||
417 | |||
418 | if (onion_c->num_friends != i) { | ||
419 | onion_c->num_friends = i; | ||
420 | realloc_onion_friends(onion_c, onion_c->num_friends); | ||
421 | } | ||
422 | |||
423 | return friend_num; | ||
424 | } | ||
425 | |||
426 | /* Get the ip of friend friendnum and put it in ip_port | ||
427 | * | ||
428 | * return -1 on failure | ||
429 | * return 0 on success | ||
430 | * | ||
431 | */ | ||
432 | int onion_getfriendip(Onion_Client *onion_c, int friend_num, IP_Port *ip_port) | ||
433 | { | ||
434 | if ((uint32_t)friend_num >= onion_c->num_friends) | ||
435 | return -1; | ||
436 | |||
437 | //TODO | ||
438 | return 0; | ||
439 | } | ||
440 | |||
322 | /* Takes 3 random nodes that we know and puts them in nodes | 441 | /* Takes 3 random nodes that we know and puts them in nodes |
323 | * | 442 | * |
324 | * nodes must be longer than 3. | 443 | * nodes must be longer than 3. |
@@ -329,7 +448,7 @@ static int handle_data_response(void *object, IP_Port source, uint8_t *packet, u | |||
329 | */ | 448 | */ |
330 | int random_path(Onion_Client *onion_c, Node_format *nodes) | 449 | int random_path(Onion_Client *onion_c, Node_format *nodes) |
331 | { | 450 | { |
332 | 451 | //TODO | |
333 | return -1; | 452 | return -1; |
334 | } | 453 | } |
335 | 454 | ||
diff --git a/toxcore/onion_client.h b/toxcore/onion_client.h index 78a12b0d..0ec381e6 100644 --- a/toxcore/onion_client.h +++ b/toxcore/onion_client.h | |||
@@ -60,11 +60,34 @@ typedef struct { | |||
60 | uint64_t last_run; | 60 | uint64_t last_run; |
61 | } Onion_Client; | 61 | } Onion_Client; |
62 | 62 | ||
63 | /* Add a friend who we want to connect to. | ||
64 | * | ||
65 | * return -1 on failure. | ||
66 | * return the friend number on success or if the friend was already added. | ||
67 | */ | ||
68 | int onion_friend_num(Onion_Client *onion_c, uint8_t *client_id); | ||
69 | |||
70 | /* Add a friend who we want to connect to. | ||
71 | * | ||
72 | * return -1 on failure. | ||
73 | * return the friend number on success. | ||
74 | */ | ||
63 | int onion_addfriend(Onion_Client *onion_c, uint8_t *client_id); | 75 | int onion_addfriend(Onion_Client *onion_c, uint8_t *client_id); |
64 | 76 | ||
65 | int onion_delfriend(Onion_Client *onion_c, uint8_t *client_id); | 77 | /* Delete a friend. |
78 | * | ||
79 | * return -1 on failure. | ||
80 | * return the deleted friend number on success. | ||
81 | */ | ||
82 | int onion_delfriend(Onion_Client *onion_c, int friend_num); | ||
66 | 83 | ||
67 | int onion_getfriendip(Onion_Client *onion_c, uint8_t *client_id, IP_Port *ip_port); | 84 | /* Get the ip of friend friendnum and put it in ip_port |
85 | * | ||
86 | * return -1 on failure | ||
87 | * return 0 on success | ||
88 | * | ||
89 | */ | ||
90 | int onion_getfriendip(Onion_Client *onion_c, int friend_num, IP_Port *ip_port); | ||
68 | 91 | ||
69 | /* Takes 3 random nodes that we know and puts them in nodes | 92 | /* Takes 3 random nodes that we know and puts them in nodes |
70 | * | 93 | * |