summaryrefslogtreecommitdiff
path: root/toxcore/assoc.c
diff options
context:
space:
mode:
Diffstat (limited to 'toxcore/assoc.c')
-rw-r--r--toxcore/assoc.c43
1 files changed, 22 insertions, 21 deletions
diff --git a/toxcore/assoc.c b/toxcore/assoc.c
index 736f246e..4bacde0e 100644
--- a/toxcore/assoc.c
+++ b/toxcore/assoc.c
@@ -103,7 +103,7 @@ struct Assoc {
103 103
104/* the complete distance would be CLIENT_ID_SIZE long... 104/* the complete distance would be CLIENT_ID_SIZE long...
105 * returns DISTANCE_INDEX_DISTANCE_BITS valid bits */ 105 * returns DISTANCE_INDEX_DISTANCE_BITS valid bits */
106static uint64_t id_distance(Assoc *assoc, void *callback_data, uint8_t *id_ref, uint8_t *id_test) 106static uint64_t id_distance(const Assoc *assoc, void *callback_data, const uint8_t *id_ref, const uint8_t *id_test)
107{ 107{
108 /* with BIG_ENDIAN, this would be a one-liner... */ 108 /* with BIG_ENDIAN, this would be a one-liner... */
109 uint64_t retval = 0; 109 uint64_t retval = 0;
@@ -196,7 +196,7 @@ static void dist_index_bubble(Assoc *assoc, uint64_t *dist_list, size_t first, s
196 * 196 *
197 * Result is NOT MAPPED to CANDIDATES_TO_KEEP range, i.e. map before using 197 * Result is NOT MAPPED to CANDIDATES_TO_KEEP range, i.e. map before using
198 * it for list access. */ 198 * it for list access. */
199static hash_t id_hash(Assoc *assoc, uint8_t *id) 199static hash_t id_hash(const Assoc *assoc, const uint8_t *id)
200{ 200{
201 uint32_t i, res = 0x19a64e82; 201 uint32_t i, res = 0x19a64e82;
202 202
@@ -213,7 +213,7 @@ static hash_t id_hash(Assoc *assoc, uint8_t *id)
213 213
214/* up to HASH_COLLIDE_COUNT calls to different spots, 214/* up to HASH_COLLIDE_COUNT calls to different spots,
215 * result IS mapped to CANDIDATES_TO_KEEP range */ 215 * result IS mapped to CANDIDATES_TO_KEEP range */
216static hash_t hash_collide(Assoc *assoc, hash_t hash) 216static hash_t hash_collide(const Assoc *assoc, hash_t hash)
217{ 217{
218 uint64_t hash64 = hash % assoc->candidates_bucket_size; 218 uint64_t hash64 = hash % assoc->candidates_bucket_size;
219 hash64 = (hash64 * HASH_COLLIDE_PRIME) % assoc->candidates_bucket_size; 219 hash64 = (hash64 * HASH_COLLIDE_PRIME) % assoc->candidates_bucket_size;
@@ -241,7 +241,7 @@ static hash_t hash_collide(Assoc *assoc, hash_t hash)
241} 241}
242 242
243/* returns the "seen" assoc related to the ipp */ 243/* returns the "seen" assoc related to the ipp */
244static IPPTsPng *entry_assoc(Client_entry *cl_entry, IP_Port *ipp) 244static IPPTsPng *entry_assoc(Client_entry *cl_entry, const IP_Port *ipp)
245{ 245{
246 if (!cl_entry) 246 if (!cl_entry)
247 return NULL; 247 return NULL;
@@ -256,7 +256,7 @@ static IPPTsPng *entry_assoc(Client_entry *cl_entry, IP_Port *ipp)
256} 256}
257 257
258/* returns the "heard" assoc related to the ipp */ 258/* returns the "heard" assoc related to the ipp */
259static IP_Port *entry_heard_get(Client_entry *entry, IP_Port *ipp) 259static IP_Port *entry_heard_get(Client_entry *entry, const IP_Port *ipp)
260{ 260{
261 if (ipp->ip.family == AF_INET) 261 if (ipp->ip.family == AF_INET)
262 return &entry->assoc_heard4; 262 return &entry->assoc_heard4;
@@ -271,7 +271,7 @@ static IP_Port *entry_heard_get(Client_entry *entry, IP_Port *ipp)
271 * LAN ip 271 * LAN ip
272 * 272 *
273 * returns 1 if the entry did change */ 273 * returns 1 if the entry did change */
274static int entry_heard_store(Client_entry *entry, IPPTs *ippts) 274static int entry_heard_store(Client_entry *entry, const IPPTs *ippts)
275{ 275{
276 if (!entry || !ippts) 276 if (!entry || !ippts)
277 return 0; 277 return 0;
@@ -279,7 +279,8 @@ static int entry_heard_store(Client_entry *entry, IPPTs *ippts)
279 if (!ipport_isset(&ippts->ip_port)) 279 if (!ipport_isset(&ippts->ip_port))
280 return 0; 280 return 0;
281 281
282 IP_Port *heard, *ipp = &ippts->ip_port; 282 IP_Port *heard;
283 const IP_Port *ipp = &ippts->ip_port;
283 284
284 if (ipp->ip.family == AF_INET) 285 if (ipp->ip.family == AF_INET)
285 heard = &entry->assoc_heard4; 286 heard = &entry->assoc_heard4;
@@ -314,13 +315,13 @@ static int entry_heard_store(Client_entry *entry, IPPTs *ippts)
314} 315}
315 316
316/* maps Assoc callback signature to id_closest() */ 317/* maps Assoc callback signature to id_closest() */
317static int assoc_id_closest(Assoc *assoc, void *callback_data, uint8_t *client_id, uint8_t *client_id1, 318static int assoc_id_closest(const Assoc *assoc, void *callback_data, const uint8_t *client_id, const uint8_t *client_id1,
318 uint8_t *client_id2) 319 const uint8_t *client_id2)
319{ 320{
320 return id_closest(client_id, client_id1, client_id2); 321 return id_closest(client_id, client_id1, client_id2);
321} 322}
322 323
323static bucket_t id_bucket(uint8_t *id, uint8_t bits) 324static bucket_t id_bucket(const uint8_t *id, uint8_t bits)
324{ 325{
325 /* return the first "bits" bits of id */ 326 /* return the first "bits" bits of id */
326 bucket_t retval = 0; 327 bucket_t retval = 0;
@@ -340,12 +341,12 @@ static bucket_t id_bucket(uint8_t *id, uint8_t bits)
340/*****************************************************************************/ 341/*****************************************************************************/
341 342
342 343
343static bucket_t candidates_id_bucket(Assoc *assoc, uint8_t *id) 344static bucket_t candidates_id_bucket(const Assoc *assoc, const uint8_t *id)
344{ 345{
345 return id_bucket(id, assoc->candidates_bucket_bits); 346 return id_bucket(id, assoc->candidates_bucket_bits);
346} 347}
347 348
348static uint8_t candidates_search(Assoc *assoc, uint8_t *id, hash_t hash, Client_entry **entryptr) 349static uint8_t candidates_search(const Assoc *assoc, const uint8_t *id, hash_t hash, Client_entry **entryptr)
349{ 350{
350 bucket_t bucket = candidates_id_bucket(assoc, id); 351 bucket_t bucket = candidates_id_bucket(assoc, id);
351 candidates_bucket *cnd_bckt = &assoc->candidates[bucket]; 352 candidates_bucket *cnd_bckt = &assoc->candidates[bucket];
@@ -365,8 +366,8 @@ static uint8_t candidates_search(Assoc *assoc, uint8_t *id, hash_t hash, Client_
365 return 0; 366 return 0;
366} 367}
367 368
368static void candidates_update_assoc(Assoc *assoc, Client_entry *entry, uint8_t used, IPPTs *ippts_send, 369static void candidates_update_assoc(const Assoc *assoc, Client_entry *entry, uint8_t used, const IPPTs *ippts_send,
369 IP_Port *ipp_recv) 370 const IP_Port *ipp_recv)
370{ 371{
371 if (!assoc || !entry || !ippts_send) 372 if (!assoc || !entry || !ippts_send)
372 return; 373 return;
@@ -397,7 +398,7 @@ static void candidates_update_assoc(Assoc *assoc, Client_entry *entry, uint8_t u
397 entry_heard_store(entry, ippts_send); 398 entry_heard_store(entry, ippts_send);
398} 399}
399 400
400static uint8_t candidates_create_internal(Assoc *assoc, hash_t hash, uint8_t *id, uint8_t seen, 401static uint8_t candidates_create_internal(const Assoc *assoc, hash_t const hash, const uint8_t *id, uint8_t seen,
401 uint8_t used, bucket_t *bucketptr, size_t *posptr) 402 uint8_t used, bucket_t *bucketptr, size_t *posptr)
402{ 403{
403 if (!assoc || !id || !bucketptr || !posptr) 404 if (!assoc || !id || !bucketptr || !posptr)
@@ -454,8 +455,8 @@ static uint8_t candidates_create_internal(Assoc *assoc, hash_t hash, uint8_t *id
454 return 0; 455 return 0;
455} 456}
456 457
457static uint8_t candidates_create_new(Assoc *assoc, hash_t hash, uint8_t *id, uint8_t used, 458static uint8_t candidates_create_new(const Assoc *assoc, hash_t hash, const uint8_t *id, uint8_t used,
458 IPPTs *ippts_send, IP_Port *ipp_recv) 459 const IPPTs *ippts_send, const IP_Port *ipp_recv)
459{ 460{
460 if (!assoc || !id || !ippts_send) 461 if (!assoc || !id || !ippts_send)
461 return 0; 462 return 0;
@@ -550,7 +551,7 @@ static void client_id_self_update(Assoc *assoc)
550 * seen should be 0 (zero), if the candidate was announced by someone else, 551 * seen should be 0 (zero), if the candidate was announced by someone else,
551 * seen should be 1 (one), if there is confirmed connectivity (a definite response) 552 * seen should be 1 (one), if there is confirmed connectivity (a definite response)
552 */ 553 */
553uint8_t Assoc_add_entry(Assoc *assoc, uint8_t *id, IPPTs *ippts_send, IP_Port *ipp_recv, uint8_t used) 554uint8_t Assoc_add_entry(Assoc *assoc, const uint8_t *id, const IPPTs *ippts_send, const IP_Port *ipp_recv, uint8_t used)
554{ 555{
555 if (!assoc || !id || !ippts_send) 556 if (!assoc || !id || !ippts_send)
556 return 0; 557 return 0;
@@ -777,7 +778,7 @@ static size_t prime_upto_min9(size_t limit)
777} 778}
778 779
779/* create */ 780/* create */
780Assoc *new_Assoc(size_t bits, size_t entries, uint8_t *public_id) 781Assoc *new_Assoc(size_t bits, size_t entries, const uint8_t *public_id)
781{ 782{
782 if (!public_id) 783 if (!public_id)
783 return NULL; 784 return NULL;
@@ -850,7 +851,7 @@ Assoc *new_Assoc(size_t bits, size_t entries, uint8_t *public_id)
850 return assoc; 851 return assoc;
851} 852}
852 853
853Assoc *new_Assoc_default(uint8_t *public_id) 854Assoc *new_Assoc_default(const uint8_t *public_id)
854{ 855{
855 /* original 8, 251 averages to ~32k entries... probably the whole DHT :D 856 /* original 8, 251 averages to ~32k entries... probably the whole DHT :D
856 * 320 entries is fine, hopefully */ 857 * 320 entries is fine, hopefully */
@@ -858,7 +859,7 @@ Assoc *new_Assoc_default(uint8_t *public_id)
858} 859}
859 860
860/* own client_id, assocs for this have to be ignored */ 861/* own client_id, assocs for this have to be ignored */
861void Assoc_self_client_id_changed(Assoc *assoc, uint8_t *id) 862void Assoc_self_client_id_changed(Assoc *assoc, const uint8_t *id)
862{ 863{
863 if (assoc && id) { 864 if (assoc && id) {
864 assoc->self_hash = 0; 865 assoc->self_hash = 0;