summaryrefslogtreecommitdiff
path: root/toxcore/assoc.h
diff options
context:
space:
mode:
authorirungentoo <irungentoo@gmail.com>2013-11-18 18:02:29 -0800
committerirungentoo <irungentoo@gmail.com>2013-11-18 18:02:29 -0800
commitcdfe09d221490f15150881b59a345c6e19957483 (patch)
treeec65664e3920152d4d56ba93ccfe5ac50ae1f07f /toxcore/assoc.h
parent26efa858ec7a21e938679242cdb46799b8db7a04 (diff)
parentb132c92b3ae3aeee293a4e815336cac76c7cfe69 (diff)
Merge pull request #650 from FullName/ID-IP-basic
Significantly trimmed down version of an ID<=>IP cache.
Diffstat (limited to 'toxcore/assoc.h')
-rw-r--r--toxcore/assoc.h87
1 files changed, 87 insertions, 0 deletions
diff --git a/toxcore/assoc.h b/toxcore/assoc.h
new file mode 100644
index 00000000..ee9e5e21
--- /dev/null
+++ b/toxcore/assoc.h
@@ -0,0 +1,87 @@
1
2#ifndef __ASSOC_H__
3#define __ASSOC_H__
4
5/* used by rendezvous */
6#define ASSOC_AVAILABLE
7
8/* For the legalese parts, see tox.h. */
9
10/*
11 * Module to store currently unused ID <=> IP associations
12 * for a potential future use
13 */
14
15typedef struct IP_Port IP_Port;
16typedef struct Assoc Assoc;
17
18/*****************************************************************************/
19
20/* custom distance handler, if it's not ID-distance based
21 * return values exactly like id_closest() */
22typedef int (*Assoc_distance_relative_callback)(Assoc *assoc, void *callback_data, uint8_t *client_id,
23 uint8_t *client_id1, uint8_t *client_id2);
24
25#define DISTANCE_INDEX_DISTANCE_BITS 44
26
27/* absolute distance: can be same for different client_id_check values
28 * return value should have DISTANCE_INDEX_DISTANCE_BITS valid bits */
29typedef uint64_t (*Assoc_distance_absolute_callback)(Assoc *assoc, void *callback_data,
30 uint8_t *client_id_ref, uint8_t *client_id_check);
31
32/*****************************************************************************/
33
34/* Central entry point for new associations: add a new candidate to the cache
35 * returns 1 if entry is stored, 2 if existing entry was updated, 0 else */
36uint8_t Assoc_add_entry(Assoc *assoc, uint8_t *id, IPPTs *ippts_send, IP_Port *ipp_recv, uint8_t used);
37
38/*****************************************************************************/
39
40typedef struct Assoc_close_entries {
41 uint8_t *wanted_id; /* the target client_id */
42 void *custom_data; /* given to distance functions */
43
44 Assoc_distance_relative_callback distance_relative_func;
45 Assoc_distance_absolute_callback distance_absolute_func;
46
47 uint8_t count_good; /* that many should be "good" w.r.t. timeout */
48 uint8_t count; /* allocated number of close_indices */
49 Client_data **result;
50} Assoc_close_entries;
51
52/* find up to close_count nodes to put into close_nodes_used of ID_Nodes
53 * the distance functions can be NULL, then standard distance functions will be used
54 * the caller is responsible for allocating close_indices of sufficient size
55 *
56 * returns 0 on error
57 * returns the number of found nodes and the list of indices usable by Assoc_client()
58 * the caller is assumed to be registered from Assoc_register_callback()
59 * if they aren't, they should copy the Client_data and call Assoc_client_drop()
60 */
61uint8_t Assoc_get_close_entries(Assoc *assoc, Assoc_close_entries *close_entries);
62
63/*****************************************************************************/
64
65/* create: default sizes (6, 5 => 320 entries) */
66Assoc *new_Assoc_default(uint8_t *public_id);
67
68/* create: customized sizes
69 * total is (2^bits) * entries
70 * bits should be between 2 and 15 (else it's trimmed)
71 * entries will be reduced to the closest prime smaller or equal
72 *
73 * preferably bits should be large and entries small to ensure spread
74 * in the search space (e. g. 5, 5 is preferable to 2, 41) */
75Assoc *new_Assoc(size_t bits, size_t entries, uint8_t *public_id);
76
77/* public_id changed (loaded), update which entry isn't stored */
78void Assoc_self_client_id_changed(Assoc *assoc, uint8_t *public_id);
79
80/* destroy */
81void kill_Assoc(Assoc *assoc);
82
83#ifdef LOGGING
84void Assoc_status(Assoc *assoc);
85#endif
86
87#endif /* !__ASSOC_H__ */