summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSean Qureshi <stqism@risingstormgames.com>2013-08-05 17:50:08 -0700
committerSean Qureshi <stqism@risingstormgames.com>2013-08-05 17:50:08 -0700
commit56ac2af45b32d47bfafb5d14763fab34dfa2f416 (patch)
treec2feed62f06e5fea232b293992cff9cce500cc27
parentcaee2c3fe58da698a228e3c07a8aa14f666e362b (diff)
parent7aa45107bebd4efaa5caee5b2221cea559c493b4 (diff)
Merge branch 'master' of https://github.com/irungentoo/ProjectTox-Core
-rw-r--r--core/CMakeLists.txt4
-rw-r--r--core/DHT.c61
-rw-r--r--core/ping.c103
-rw-r--r--core/ping.h13
-rw-r--r--core/util.c34
-rw-r--r--core/util.h10
-rw-r--r--testing/toxic/friendlist.c2
7 files changed, 168 insertions, 59 deletions
diff --git a/core/CMakeLists.txt b/core/CMakeLists.txt
index eacb772c..55a41912 100644
--- a/core/CMakeLists.txt
+++ b/core/CMakeLists.txt
@@ -8,7 +8,9 @@ set(core_sources
8 net_crypto.c 8 net_crypto.c
9 friend_requests.c 9 friend_requests.c
10 LAN_discovery.c 10 LAN_discovery.c
11 Messenger.c) 11 Messenger.c
12 util.c
13 ping.c)
12 14
13if(SHARED_TOXCORE) 15if(SHARED_TOXCORE)
14 add_library(toxcore SHARED ${core_sources}) 16 add_library(toxcore SHARED ${core_sources})
diff --git a/core/DHT.c b/core/DHT.c
index f6c48a6d..f0b32df5 100644
--- a/core/DHT.c
+++ b/core/DHT.c
@@ -24,6 +24,7 @@
24/*----------------------------------------------------------------------------------*/ 24/*----------------------------------------------------------------------------------*/
25 25
26#include "DHT.h" 26#include "DHT.h"
27#include "ping.h"
27 28
28/* maximum number of clients stored per friend. */ 29/* maximum number of clients stored per friend. */
29#define MAX_FRIEND_CLIENTS 8 30#define MAX_FRIEND_CLIENTS 8
@@ -109,7 +110,6 @@ uint8_t self_secret_key[crypto_box_SECRETKEYBYTES];
109static Client_data close_clientlist[LCLIENT_LIST]; 110static Client_data close_clientlist[LCLIENT_LIST];
110static Friend * friends_list; 111static Friend * friends_list;
111static uint16_t num_friends; 112static uint16_t num_friends;
112static Pinged pings[LPING_ARRAY];
113static Pinged send_nodes[LSEND_NODES_ARRAY]; 113static Pinged send_nodes[LSEND_NODES_ARRAY];
114 114
115/*----------------------------------------------------------------------------------*/ 115/*----------------------------------------------------------------------------------*/
@@ -426,35 +426,6 @@ static void returnedip_ports(IP_Port ip_port, uint8_t * client_id, uint8_t * nod
426 } 426 }
427} 427}
428 428
429/* check if we are currently pinging an ip_port and/or a ping_id variables with
430 * values of zero will not be checked. If we are already, return 1 else return 0
431 *
432 * TODO: optimize this
433 */
434static int is_pinging(IP_Port ip_port, uint64_t ping_id)
435{
436 uint32_t i;
437 uint8_t pinging;
438 uint64_t temp_time = unix_time();
439
440 for (i = 0; i < LPING_ARRAY; ++i ) {
441 if (!is_timeout(temp_time, pings[i].timestamp, PING_TIMEOUT)) {
442 pinging = 0;
443
444 if (ip_port.ip.i != 0 && ipport_equal(pings[i].ip_port, ip_port))
445 ++pinging;
446
447 if (ping_id != 0 && pings[i].ping_id == ping_id)
448 ++pinging;
449
450 if (pinging == ((ping_id != 0) + (ip_port.ip.i != 0)))
451 return 1;
452 }
453 }
454
455 return 0;
456}
457
458/* Same as last function but for get_node requests. */ 429/* Same as last function but for get_node requests. */
459static int is_gettingnodes(IP_Port ip_port, uint64_t ping_id) 430static int is_gettingnodes(IP_Port ip_port, uint64_t ping_id)
460{ 431{
@@ -480,32 +451,6 @@ static int is_gettingnodes(IP_Port ip_port, uint64_t ping_id)
480 return 0; 451 return 0;
481} 452}
482 453
483/* Add a new ping request to the list of ping requests
484 * returns the ping_id to put in the ping request
485 * returns 0 if problem.
486 *
487 * TODO: optimize this
488 */
489static uint64_t add_pinging(IP_Port ip_port)
490{
491 uint32_t i, j;
492 uint64_t ping_id = ((uint64_t)random_int() << 32) + random_int();
493 uint64_t temp_time = unix_time();
494
495 for(i = 0; i < PING_TIMEOUT; ++i ) {
496 for(j = 0; j < LPING_ARRAY; ++j ) {
497 if(is_timeout(temp_time, pings[j].timestamp, PING_TIMEOUT - i)) {
498 pings[j].timestamp = temp_time;
499 pings[j].ip_port = ip_port;
500 pings[j].ping_id = ping_id;
501 return ping_id;
502 }
503 }
504 }
505
506 return 0;
507}
508
509/* Same but for get node requests */ 454/* Same but for get node requests */
510static uint64_t add_gettingnodes(IP_Port ip_port) 455static uint64_t add_gettingnodes(IP_Port ip_port)
511{ 456{
@@ -536,7 +481,7 @@ static int pingreq(IP_Port ip_port, uint8_t * public_key)
536 if(id_equal(public_key, self_public_key) || is_pinging(ip_port, 0)) 481 if(id_equal(public_key, self_public_key) || is_pinging(ip_port, 0))
537 return 1; 482 return 1;
538 483
539 uint64_t ping_id = add_pinging(ip_port); 484 uint64_t ping_id = add_ping(ip_port);
540 if(ping_id == 0) 485 if(ping_id == 0)
541 return 1; 486 return 1;
542 487
@@ -1301,6 +1246,8 @@ void DHT_save(uint8_t * data)
1301 */ 1246 */
1302int DHT_load(uint8_t * data, uint32_t size) 1247int DHT_load(uint8_t * data, uint32_t size)
1303{ 1248{
1249 init_ping();
1250
1304 if(size < sizeof(close_clientlist)) 1251 if(size < sizeof(close_clientlist))
1305 return -1; 1252 return -1;
1306 1253
diff --git a/core/ping.c b/core/ping.c
new file mode 100644
index 00000000..ffabe221
--- /dev/null
+++ b/core/ping.c
@@ -0,0 +1,103 @@
1/*
2 * ping.c -- Buffered pinging using cyclic arrays.
3 *
4 * This file is donated to the Tox Project.
5 * Copyright 2013 plutooo
6 */
7
8#include <stdbool.h>
9#include <stdint.h>
10
11#include "network.h"
12#include "util.h"
13
14#define PING_NUM_MAX 256
15#define PING_TIMEOUT 5 // 5s
16
17typedef struct {
18 IP_Port ipp;
19 uint64_t id;
20 uint64_t timestamp;
21} pinged_t;
22
23static pinged_t pings[PING_NUM_MAX];
24static size_t num_pings;
25static size_t pos_pings;
26
27
28void init_ping()
29{
30 num_pings = 0;
31 pos_pings = 0;
32}
33
34static bool is_timeout(uint64_t time)
35{
36 return (time + PING_TIMEOUT) < now();
37}
38
39static void remove_timeouts() // O(n)
40{
41 size_t i, id;
42 size_t new_pos = pos_pings;
43 size_t new_num = num_pings;
44
45 // Loop through buffer, oldest first
46 for (i=0; i<num_pings; i++) {
47 id = (pos_pings + i) % PING_NUM_MAX;
48
49 if(is_timeout(pings[id].timestamp)) {
50 new_pos++;
51 new_num--;
52 }
53 // Break here because list is sorted.
54 else
55 break;
56 }
57
58 num_pings = new_num;
59 pos_pings = new_pos % PING_NUM_MAX;
60}
61
62uint64_t add_ping(IP_Port ipp) // O(n)
63{
64 size_t p;
65
66 remove_timeouts();
67
68 // Remove oldest ping if full buffer
69 if (num_pings == PING_NUM_MAX) {
70 num_pings--;
71 pos_pings = (pos_pings + 1) % PING_NUM_MAX;
72 }
73
74 // Insert new ping at end of list
75 p = (pos_pings + num_pings) % PING_NUM_MAX;
76
77 pings[p].ipp = ipp;
78 pings[p].timestamp = now();
79 pings[p].id = random_64b();
80
81 num_pings++;
82 return pings[p].id;
83}
84
85bool is_pinging(IP_Port ipp, uint64_t ping_id) // O(n) TODO: replace this with something else.
86{
87 if (ipp.ip.i == 0 && ping_id == 0)
88 return false;
89
90 size_t i, id;
91
92 remove_timeouts();
93
94 for (i=0; i<num_pings; i++) {
95 id = (pos_pings + i) % PING_NUM_MAX;
96
97 if ((ipp_eq(pings[id].ipp, ipp) || ipp.ip.i == 0) && (pings[id].id == ping_id || ping_id == 0)) {
98 return true;
99 }
100 }
101
102 return false;
103}
diff --git a/core/ping.h b/core/ping.h
new file mode 100644
index 00000000..1d23df97
--- /dev/null
+++ b/core/ping.h
@@ -0,0 +1,13 @@
1/*
2 * ping.h -- Buffered pinging using cyclic arrays.
3 *
4 * This file is donated to the Tox Project.
5 * Copyright 2013 plutooo
6 */
7
8#include <stdbool.h>
9
10void init_ping();
11uint64_t add_ping(IP_Port ipp);
12bool is_pinging(IP_Port ipp, uint64_t ping_id);
13
diff --git a/core/util.c b/core/util.c
new file mode 100644
index 00000000..4ce9271e
--- /dev/null
+++ b/core/util.c
@@ -0,0 +1,34 @@
1/*
2 * util.c -- Utilities.
3 *
4 * This file is donated to the Tox Project.
5 * Copyright 2013 plutooo
6 */
7
8#include <time.h>
9#include <stdint.h>
10#include <stdbool.h>
11
12#include "network.h"
13
14uint64_t now()
15{
16 return time(NULL);
17}
18
19uint64_t random_64b()
20{
21 uint64_t r;
22
23 // This is probably not random enough?
24 r = random_int();
25 r <<= 32;
26 r |= random_int();
27
28 return r;
29}
30
31bool ipp_eq(IP_Port a, IP_Port b)
32{
33 return (a.ip.i == b.ip.i) && (a.port == b.port);
34}
diff --git a/core/util.h b/core/util.h
new file mode 100644
index 00000000..aab2ead9
--- /dev/null
+++ b/core/util.h
@@ -0,0 +1,10 @@
1/*
2 * util.h -- Utilities.
3 *
4 * This file is donated to the Tox Project.
5 * Copyright 2013 plutooo
6 */
7
8uint64_t now();
9uint64_t random_64b();
10bool ipp_eq(IP_Port a, IP_Port b);
diff --git a/testing/toxic/friendlist.c b/testing/toxic/friendlist.c
index b4b619a2..05651b0e 100644
--- a/testing/toxic/friendlist.c
+++ b/testing/toxic/friendlist.c
@@ -116,7 +116,7 @@ static void friendlist_onDraw(ToxWindow* self) {
116 curs_set(0); 116 curs_set(0);
117 size_t i; 117 size_t i;
118 118
119 wclear(self->window); 119 werase(self->window);
120 120
121 if(num_friends == 0) { 121 if(num_friends == 0) {
122 wprintw(self->window, "Empty. Add some friends! :-)\n"); 122 wprintw(self->window, "Empty. Add some friends! :-)\n");