diff options
author | irungentoo <irungentoo@gmail.com> | 2013-08-20 12:08:55 -0400 |
---|---|---|
committer | irungentoo <irungentoo@gmail.com> | 2013-08-20 12:08:55 -0400 |
commit | a1c40d753ee8faf15aa0dd314bd4249aa3750b2e (patch) | |
tree | fb52c5cffcb897afa709e2d4a9882e89e9c90b5d /core/DHT.h | |
parent | 9f0efe920170472a4f757f7d8dc84f41e794f945 (diff) |
More refactoring done.
Diffstat (limited to 'core/DHT.h')
-rw-r--r-- | core/DHT.h | 93 |
1 files changed, 77 insertions, 16 deletions
@@ -34,6 +34,20 @@ extern "C" { | |||
34 | /* size of the client_id in bytes */ | 34 | /* size of the client_id in bytes */ |
35 | #define CLIENT_ID_SIZE crypto_box_PUBLICKEYBYTES | 35 | #define CLIENT_ID_SIZE crypto_box_PUBLICKEYBYTES |
36 | 36 | ||
37 | /* maximum number of clients stored per friend. */ | ||
38 | #define MAX_FRIEND_CLIENTS 8 | ||
39 | |||
40 | /* A list of the clients mathematically closest to ours. */ | ||
41 | #define LCLIENT_LIST 32 | ||
42 | |||
43 | /* The list of ip ports along with the ping_id of what we sent them and a timestamp */ | ||
44 | #define LPING_ARRAY 256 //NOTE Deprecated (doesn't do anything) | ||
45 | |||
46 | #define LSEND_NODES_ARRAY LPING_ARRAY/2 | ||
47 | |||
48 | /*Maximum newly announced nodes to ping per TIME_TOPING seconds*/ | ||
49 | #define MAX_TOPING 16 | ||
50 | |||
37 | typedef struct { | 51 | typedef struct { |
38 | uint8_t client_id[CLIENT_ID_SIZE]; | 52 | uint8_t client_id[CLIENT_ID_SIZE]; |
39 | IP_Port ip_port; | 53 | IP_Port ip_port; |
@@ -45,19 +59,64 @@ typedef struct { | |||
45 | uint64_t ret_timestamp; | 59 | uint64_t ret_timestamp; |
46 | } Client_data; | 60 | } Client_data; |
47 | 61 | ||
48 | Client_data *DHT_get_close_list(void); | 62 | /*----------------------------------------------------------------------------------*/ |
63 | |||
64 | typedef struct { | ||
65 | uint8_t client_id[CLIENT_ID_SIZE]; | ||
66 | Client_data client_list[MAX_FRIEND_CLIENTS]; | ||
67 | |||
68 | /* time at which the last get_nodes request was sent. */ | ||
69 | uint64_t lastgetnode; | ||
70 | |||
71 | /* Symetric NAT hole punching stuff */ | ||
72 | |||
73 | /* 1 if currently hole punching, otherwise 0 */ | ||
74 | uint8_t hole_punching; | ||
75 | uint32_t punching_index; | ||
76 | uint64_t punching_timestamp; | ||
77 | uint64_t recvNATping_timestamp; | ||
78 | uint64_t NATping_id; | ||
79 | uint64_t NATping_timestamp; | ||
80 | } DHT_Friend; | ||
81 | |||
82 | typedef struct { | ||
83 | uint8_t client_id[CLIENT_ID_SIZE]; | ||
84 | IP_Port ip_port; | ||
85 | } Node_format; | ||
86 | |||
87 | typedef struct { | ||
88 | IP_Port ip_port; | ||
89 | uint64_t ping_id; | ||
90 | uint64_t timestamp; | ||
91 | } Pinged; | ||
92 | |||
93 | /*----------------------------------------------------------------------------------*/ | ||
94 | typedef struct { | ||
95 | Net_Crypto *c; | ||
96 | Client_data close_clientlist[LCLIENT_LIST]; | ||
97 | DHT_Friend *friends_list; | ||
98 | uint16_t num_friends; | ||
99 | Pinged send_nodes[LSEND_NODES_ARRAY]; | ||
100 | Node_format toping[MAX_TOPING]; | ||
101 | uint64_t last_toping; | ||
102 | uint64_t close_lastgetnodes; | ||
103 | } DHT; | ||
104 | /*----------------------------------------------------------------------------------*/ | ||
105 | DHT * temp_DHT; //TODO: remove | ||
106 | |||
107 | Client_data *DHT_get_close_list(DHT * dht); | ||
49 | 108 | ||
50 | /* Add a new friend to the friends list | 109 | /* Add a new friend to the friends list |
51 | client_id must be CLIENT_ID_SIZE bytes long. | 110 | client_id must be CLIENT_ID_SIZE bytes long. |
52 | returns 0 if success | 111 | returns 0 if success |
53 | returns 1 if failure (friends list is full) */ | 112 | returns 1 if failure (friends list is full) */ |
54 | int DHT_addfriend(uint8_t *client_id); | 113 | int DHT_addfriend(DHT * dht, uint8_t *client_id); |
55 | 114 | ||
56 | /* Delete a friend from the friends list | 115 | /* Delete a friend from the friends list |
57 | client_id must be CLIENT_ID_SIZE bytes long. | 116 | client_id must be CLIENT_ID_SIZE bytes long. |
58 | returns 0 if success | 117 | returns 0 if success |
59 | returns 1 if failure (client_id not in friends list) */ | 118 | returns 1 if failure (client_id not in friends list) */ |
60 | int DHT_delfriend(uint8_t *client_id); | 119 | int DHT_delfriend(DHT * dht, uint8_t *client_id); |
61 | 120 | ||
62 | /* Get ip of friend | 121 | /* Get ip of friend |
63 | client_id must be CLIENT_ID_SIZE bytes long. | 122 | client_id must be CLIENT_ID_SIZE bytes long. |
@@ -66,14 +125,14 @@ int DHT_delfriend(uint8_t *client_id); | |||
66 | returns ip if success | 125 | returns ip if success |
67 | returns ip of 0 if failure (This means the friend is either offline or we have not found him yet.) | 126 | returns ip of 0 if failure (This means the friend is either offline or we have not found him yet.) |
68 | returns ip of 1 if friend is not in list. */ | 127 | returns ip of 1 if friend is not in list. */ |
69 | IP_Port DHT_getfriendip(uint8_t *client_id); | 128 | IP_Port DHT_getfriendip(DHT * dht, uint8_t *client_id); |
70 | 129 | ||
71 | /* Run this function at least a couple times per second (It's the main loop) */ | 130 | /* Run this function at least a couple times per second (It's the main loop) */ |
72 | void doDHT(void); | 131 | void do_DHT(DHT * dht); |
73 | 132 | ||
74 | /* Use this function to bootstrap the client | 133 | /* Use this function to bootstrap the client |
75 | Sends a get nodes request to the given node with ip port and public_key */ | 134 | 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); | 135 | void DHT_bootstrap(DHT * dht, IP_Port ip_port, uint8_t *public_key); |
77 | 136 | ||
78 | /* Add nodes to the toping list | 137 | /* Add nodes to the toping list |
79 | all nodes in this list are pinged every TIME_TOPING seconds | 138 | all nodes in this list are pinged every TIME_TOPING seconds |
@@ -83,17 +142,17 @@ void DHT_bootstrap(IP_Port ip_port, uint8_t *public_key); | |||
83 | network while preventing amplification attacks. | 142 | network while preventing amplification attacks. |
84 | return 0 if node was added | 143 | return 0 if node was added |
85 | return -1 if node was not added */ | 144 | return -1 if node was not added */ |
86 | int add_toping(uint8_t *client_id, IP_Port ip_port); | 145 | int add_toping(DHT * dht, uint8_t *client_id, IP_Port ip_port); |
87 | 146 | ||
88 | /* ROUTING FUNCTIONS */ | 147 | /* ROUTING FUNCTIONS */ |
89 | 148 | ||
90 | /* send the given packet to node with client_id | 149 | /* send the given packet to node with client_id |
91 | returns -1 if failure */ | 150 | returns -1 if failure */ |
92 | int route_packet(uint8_t *client_id, uint8_t *packet, uint32_t length); | 151 | int route_packet(DHT * dht, uint8_t *client_id, uint8_t *packet, uint32_t length); |
93 | 152 | ||
94 | /* Send the following packet to everyone who tells us they are connected to friend_id | 153 | /* Send the following packet to everyone who tells us they are connected to friend_id |
95 | returns the number of nodes it sent the packet to */ | 154 | returns the number of nodes it sent the packet to */ |
96 | int route_tofriend(uint8_t *friend_id, uint8_t *packet, uint32_t length); | 155 | int route_tofriend(DHT * dht, uint8_t *friend_id, uint8_t *packet, uint32_t length); |
97 | 156 | ||
98 | /* NAT PUNCHING FUNCTIONS */ | 157 | /* NAT PUNCHING FUNCTIONS */ |
99 | 158 | ||
@@ -101,29 +160,31 @@ int route_tofriend(uint8_t *friend_id, uint8_t *packet, uint32_t length); | |||
101 | ip_portlist must be at least MAX_FRIEND_CLIENTS big | 160 | ip_portlist must be at least MAX_FRIEND_CLIENTS big |
102 | returns the number of ips returned | 161 | returns the number of ips returned |
103 | returns -1 if no such friend*/ | 162 | returns -1 if no such friend*/ |
104 | int friend_ips(IP_Port *ip_portlist, uint8_t *friend_id); | 163 | int friend_ips(DHT * dht, IP_Port *ip_portlist, uint8_t *friend_id); |
105 | 164 | ||
106 | /* SAVE/LOAD functions */ | 165 | /* SAVE/LOAD functions */ |
107 | 166 | ||
108 | /* get the size of the DHT (for saving) */ | 167 | /* get the size of the DHT (for saving) */ |
109 | uint32_t DHT_size(void); | 168 | uint32_t DHT_size(DHT * dht); |
110 | 169 | ||
111 | /* save the DHT in data where data is an array of size DHT_size() */ | 170 | /* save the DHT in data where data is an array of size DHT_size() */ |
112 | void DHT_save(uint8_t *data); | 171 | void DHT_save(DHT * dht, uint8_t *data); |
113 | 172 | ||
114 | /* init DHT */ | 173 | /* init DHT */ |
115 | void DHT_init(void); | 174 | DHT * new_DHT(Net_Crypto *c); |
175 | |||
176 | void kill_DHT(DHT * dht); | ||
116 | 177 | ||
117 | /* load the DHT from data of size size; | 178 | /* load the DHT from data of size size; |
118 | return -1 if failure | 179 | return -1 if failure |
119 | return 0 if success */ | 180 | return 0 if success */ |
120 | int DHT_load(uint8_t *data, uint32_t size); | 181 | int DHT_load(DHT * dht, uint8_t *data, uint32_t size); |
121 | 182 | ||
122 | /* returns 0 if we are not connected to the DHT | 183 | /* returns 0 if we are not connected to the DHT |
123 | returns 1 if we are */ | 184 | returns 1 if we are */ |
124 | int DHT_isconnected(); | 185 | int DHT_isconnected(DHT * dht); |
125 | 186 | ||
126 | void addto_lists(IP_Port ip_port, uint8_t *client_id); | 187 | void addto_lists(DHT * dht, IP_Port ip_port, uint8_t *client_id); |
127 | 188 | ||
128 | #ifdef __cplusplus | 189 | #ifdef __cplusplus |
129 | } | 190 | } |