diff options
Diffstat (limited to 'src/service/messenger/messenger_api_contact_store.c')
-rw-r--r-- | src/service/messenger/messenger_api_contact_store.c | 77 |
1 files changed, 46 insertions, 31 deletions
diff --git a/src/service/messenger/messenger_api_contact_store.c b/src/service/messenger/messenger_api_contact_store.c index b0e0d026b..7583a8b07 100644 --- a/src/service/messenger/messenger_api_contact_store.c +++ b/src/service/messenger/messenger_api_contact_store.c | |||
@@ -1,6 +1,6 @@ | |||
1 | /* | 1 | /* |
2 | This file is part of GNUnet. | 2 | This file is part of GNUnet. |
3 | Copyright (C) 2020--2021 GNUnet e.V. | 3 | Copyright (C) 2020--2023 GNUnet e.V. |
4 | 4 | ||
5 | GNUnet is free software: you can redistribute it and/or modify it | 5 | GNUnet is free software: you can redistribute it and/or modify it |
6 | under the terms of the GNU Affero General Public License as published | 6 | under the terms of the GNU Affero General Public License as published |
@@ -36,8 +36,11 @@ init_contact_store (struct GNUNET_MESSENGER_ContactStore *store) | |||
36 | 36 | ||
37 | store->anonymous = GNUNET_CONTAINER_multihashmap_create (8, GNUNET_NO); | 37 | store->anonymous = GNUNET_CONTAINER_multihashmap_create (8, GNUNET_NO); |
38 | store->contacts = GNUNET_CONTAINER_multihashmap_create (8, GNUNET_NO); | 38 | store->contacts = GNUNET_CONTAINER_multihashmap_create (8, GNUNET_NO); |
39 | |||
40 | store->counter = 0; | ||
39 | } | 41 | } |
40 | 42 | ||
43 | |||
41 | static int | 44 | static int |
42 | iterate_destroy_contacts (void *cls, | 45 | iterate_destroy_contacts (void *cls, |
43 | const struct GNUNET_HashCode *key, | 46 | const struct GNUNET_HashCode *key, |
@@ -48,37 +51,43 @@ iterate_destroy_contacts (void *cls, | |||
48 | return GNUNET_YES; | 51 | return GNUNET_YES; |
49 | } | 52 | } |
50 | 53 | ||
54 | |||
51 | void | 55 | void |
52 | clear_contact_store (struct GNUNET_MESSENGER_ContactStore *store) | 56 | clear_contact_store (struct GNUNET_MESSENGER_ContactStore *store) |
53 | { | 57 | { |
54 | GNUNET_assert ((store) && (store->contacts)); | 58 | GNUNET_assert ((store) && (store->contacts)); |
55 | 59 | ||
56 | GNUNET_CONTAINER_multihashmap_iterate (store->anonymous, iterate_destroy_contacts, NULL); | 60 | GNUNET_CONTAINER_multihashmap_iterate (store->anonymous, |
57 | GNUNET_CONTAINER_multihashmap_iterate (store->contacts, iterate_destroy_contacts, NULL); | 61 | iterate_destroy_contacts, NULL); |
62 | GNUNET_CONTAINER_multihashmap_iterate (store->contacts, | ||
63 | iterate_destroy_contacts, NULL); | ||
58 | 64 | ||
59 | GNUNET_CONTAINER_multihashmap_destroy (store->anonymous); | 65 | GNUNET_CONTAINER_multihashmap_destroy (store->anonymous); |
60 | GNUNET_CONTAINER_multihashmap_destroy (store->contacts); | 66 | GNUNET_CONTAINER_multihashmap_destroy (store->contacts); |
61 | } | 67 | } |
62 | 68 | ||
69 | |||
63 | static struct GNUNET_CONTAINER_MultiHashMap* | 70 | static struct GNUNET_CONTAINER_MultiHashMap* |
64 | select_store_contact_map (struct GNUNET_MESSENGER_ContactStore *store, | 71 | select_store_contact_map (struct GNUNET_MESSENGER_ContactStore *store, |
65 | const struct GNUNET_HashCode *context, | 72 | const struct GNUNET_HashCode *context, |
66 | struct GNUNET_HashCode *hash) | 73 | struct GNUNET_HashCode *hash) |
67 | { | 74 | { |
68 | const struct GNUNET_CRYPTO_PublicKey *anonymous = get_anonymous_public_key (); | 75 | const struct GNUNET_CRYPTO_PublicKey *anonymous = |
76 | get_anonymous_public_key (); | ||
69 | 77 | ||
70 | struct GNUNET_HashCode anonHash; | 78 | struct GNUNET_HashCode anonHash; |
71 | GNUNET_CRYPTO_hash (anonymous, sizeof(*anonymous), &anonHash); | 79 | GNUNET_CRYPTO_hash (anonymous, sizeof(*anonymous), &anonHash); |
72 | 80 | ||
73 | if ((context) && (0 == GNUNET_CRYPTO_hash_cmp(hash, &anonHash))) | 81 | if ((context) && (0 == GNUNET_CRYPTO_hash_cmp (hash, &anonHash))) |
74 | { | 82 | { |
75 | GNUNET_memcpy(hash, context, sizeof(*context)); | 83 | GNUNET_memcpy (hash, context, sizeof(*context)); |
76 | return store->anonymous; | 84 | return store->anonymous; |
77 | } | 85 | } |
78 | else | 86 | else |
79 | return store->contacts; | 87 | return store->contacts; |
80 | } | 88 | } |
81 | 89 | ||
90 | |||
82 | struct GNUNET_MESSENGER_Contact* | 91 | struct GNUNET_MESSENGER_Contact* |
83 | get_store_contact_raw (struct GNUNET_MESSENGER_ContactStore *store, | 92 | get_store_contact_raw (struct GNUNET_MESSENGER_ContactStore *store, |
84 | const struct GNUNET_HashCode *context, | 93 | const struct GNUNET_HashCode *context, |
@@ -87,15 +96,16 @@ get_store_contact_raw (struct GNUNET_MESSENGER_ContactStore *store, | |||
87 | GNUNET_assert ((store) && (store->contacts) && (context) && (key_hash)); | 96 | GNUNET_assert ((store) && (store->contacts) && (context) && (key_hash)); |
88 | 97 | ||
89 | struct GNUNET_HashCode hash; | 98 | struct GNUNET_HashCode hash; |
90 | GNUNET_memcpy(&hash, key_hash, sizeof(*key_hash)); | 99 | GNUNET_memcpy (&hash, key_hash, sizeof(*key_hash)); |
91 | 100 | ||
92 | struct GNUNET_CONTAINER_MultiHashMap *map = select_store_contact_map ( | 101 | struct GNUNET_CONTAINER_MultiHashMap *map = select_store_contact_map ( |
93 | store, context, &hash | 102 | store, context, &hash |
94 | ); | 103 | ); |
95 | 104 | ||
96 | return GNUNET_CONTAINER_multihashmap_get (map, &hash); | 105 | return GNUNET_CONTAINER_multihashmap_get (map, &hash); |
97 | } | 106 | } |
98 | 107 | ||
108 | |||
99 | struct GNUNET_MESSENGER_Contact* | 109 | struct GNUNET_MESSENGER_Contact* |
100 | get_store_contact (struct GNUNET_MESSENGER_ContactStore *store, | 110 | get_store_contact (struct GNUNET_MESSENGER_ContactStore *store, |
101 | const struct GNUNET_HashCode *context, | 111 | const struct GNUNET_HashCode *context, |
@@ -107,17 +117,20 @@ get_store_contact (struct GNUNET_MESSENGER_ContactStore *store, | |||
107 | GNUNET_CRYPTO_hash (pubkey, sizeof(*pubkey), &hash); | 117 | GNUNET_CRYPTO_hash (pubkey, sizeof(*pubkey), &hash); |
108 | 118 | ||
109 | struct GNUNET_CONTAINER_MultiHashMap *map = select_store_contact_map ( | 119 | struct GNUNET_CONTAINER_MultiHashMap *map = select_store_contact_map ( |
110 | store, context, &hash | 120 | store, context, &hash |
111 | ); | 121 | ); |
112 | 122 | ||
113 | struct GNUNET_MESSENGER_Contact *contact = GNUNET_CONTAINER_multihashmap_get (map, &hash); | 123 | struct GNUNET_MESSENGER_Contact *contact = GNUNET_CONTAINER_multihashmap_get ( |
124 | map, &hash); | ||
114 | 125 | ||
115 | if (contact) | 126 | if (contact) |
116 | { | 127 | { |
117 | if (0 != GNUNET_memcmp(pubkey, get_contact_key(contact))) | 128 | if (0 != GNUNET_memcmp (pubkey, get_contact_key (contact))) |
118 | { | 129 | { |
119 | char* str = GNUNET_CRYPTO_public_key_to_string (get_contact_key(contact)); | 130 | char *str = GNUNET_CRYPTO_public_key_to_string (get_contact_key ( |
120 | GNUNET_log (GNUNET_ERROR_TYPE_INVALID, "Contact in store uses wrong key: %s\n", str); | 131 | contact)); |
132 | GNUNET_log (GNUNET_ERROR_TYPE_INVALID, | ||
133 | "Contact in store uses wrong key: %s\n", str); | ||
121 | GNUNET_free (str); | 134 | GNUNET_free (str); |
122 | return NULL; | 135 | return NULL; |
123 | } | 136 | } |
@@ -125,7 +138,7 @@ get_store_contact (struct GNUNET_MESSENGER_ContactStore *store, | |||
125 | return contact; | 138 | return contact; |
126 | } | 139 | } |
127 | 140 | ||
128 | contact = create_contact (pubkey); | 141 | contact = create_contact (pubkey, ++(store->counter)); |
129 | 142 | ||
130 | if (GNUNET_OK == GNUNET_CONTAINER_multihashmap_put (map, &hash, contact, | 143 | if (GNUNET_OK == GNUNET_CONTAINER_multihashmap_put (map, &hash, contact, |
131 | GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST)) | 144 | GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST)) |
@@ -135,60 +148,62 @@ get_store_contact (struct GNUNET_MESSENGER_ContactStore *store, | |||
135 | return NULL; | 148 | return NULL; |
136 | } | 149 | } |
137 | 150 | ||
151 | |||
138 | void | 152 | void |
139 | update_store_contact (struct GNUNET_MESSENGER_ContactStore *store, | 153 | update_store_contact (struct GNUNET_MESSENGER_ContactStore *store, |
140 | struct GNUNET_MESSENGER_Contact* contact, | 154 | struct GNUNET_MESSENGER_Contact *contact, |
141 | const struct GNUNET_HashCode *context, | 155 | const struct GNUNET_HashCode *context, |
142 | const struct GNUNET_HashCode *next_context, | 156 | const struct GNUNET_HashCode *next_context, |
143 | const struct GNUNET_CRYPTO_PublicKey *pubkey) | 157 | const struct GNUNET_CRYPTO_PublicKey *pubkey) |
144 | { | 158 | { |
145 | GNUNET_assert ((store) && (store->contacts) && (contact) && (pubkey)); | 159 | GNUNET_assert ((store) && (store->contacts) && (contact) && (pubkey)); |
146 | 160 | ||
147 | const struct GNUNET_CRYPTO_PublicKey* oldkey = get_contact_key (contact); | 161 | const struct GNUNET_CRYPTO_PublicKey *oldkey = get_contact_key (contact); |
148 | 162 | ||
149 | struct GNUNET_HashCode hash; | 163 | struct GNUNET_HashCode hash; |
150 | GNUNET_CRYPTO_hash (oldkey, sizeof(*oldkey), &hash); | 164 | GNUNET_CRYPTO_hash (oldkey, sizeof(*oldkey), &hash); |
151 | 165 | ||
152 | struct GNUNET_CONTAINER_MultiHashMap *map = select_store_contact_map ( | 166 | struct GNUNET_CONTAINER_MultiHashMap *map = select_store_contact_map ( |
153 | store, context, &hash | 167 | store, context, &hash |
154 | ); | 168 | ); |
155 | 169 | ||
156 | if (GNUNET_YES == GNUNET_CONTAINER_multihashmap_remove (map, &hash, contact)) | 170 | if (GNUNET_YES == GNUNET_CONTAINER_multihashmap_remove (map, &hash, contact)) |
157 | { | 171 | { |
158 | GNUNET_memcpy(&(contact->public_key), pubkey, sizeof(*pubkey)); | 172 | GNUNET_memcpy (&(contact->public_key), pubkey, sizeof(*pubkey)); |
159 | 173 | ||
160 | GNUNET_CRYPTO_hash (pubkey, sizeof(*pubkey), &hash); | 174 | GNUNET_CRYPTO_hash (pubkey, sizeof(*pubkey), &hash); |
161 | 175 | ||
162 | map = select_store_contact_map ( | 176 | map = select_store_contact_map ( |
163 | store, next_context, &hash | 177 | store, next_context, &hash |
164 | ); | 178 | ); |
165 | 179 | ||
166 | if (GNUNET_OK != GNUNET_CONTAINER_multihashmap_put (map, &hash, contact, | 180 | if (GNUNET_OK != GNUNET_CONTAINER_multihashmap_put (map, &hash, contact, |
167 | GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST)) | 181 | GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST)) |
168 | GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Updating a contact failed: %s\n", | 182 | GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Updating a contact failed: %s\n", |
169 | GNUNET_h2s(&hash)); | 183 | GNUNET_h2s (&hash)); |
170 | } | 184 | } |
171 | } | 185 | } |
172 | 186 | ||
187 | |||
173 | void | 188 | void |
174 | remove_store_contact (struct GNUNET_MESSENGER_ContactStore *store, | 189 | remove_store_contact (struct GNUNET_MESSENGER_ContactStore *store, |
175 | struct GNUNET_MESSENGER_Contact* contact, | 190 | struct GNUNET_MESSENGER_Contact *contact, |
176 | const struct GNUNET_HashCode *context) | 191 | const struct GNUNET_HashCode *context) |
177 | { | 192 | { |
178 | GNUNET_assert ((store) && (store->contacts) && (contact)); | 193 | GNUNET_assert ((store) && (store->contacts) && (contact)); |
179 | 194 | ||
180 | const struct GNUNET_CRYPTO_PublicKey* pubkey = get_contact_key(contact); | 195 | const struct GNUNET_CRYPTO_PublicKey *pubkey = get_contact_key (contact); |
181 | 196 | ||
182 | struct GNUNET_HashCode hash; | 197 | struct GNUNET_HashCode hash; |
183 | GNUNET_CRYPTO_hash (pubkey, sizeof(*pubkey), &hash); | 198 | GNUNET_CRYPTO_hash (pubkey, sizeof(*pubkey), &hash); |
184 | 199 | ||
185 | struct GNUNET_CONTAINER_MultiHashMap *map = select_store_contact_map ( | 200 | struct GNUNET_CONTAINER_MultiHashMap *map = select_store_contact_map ( |
186 | store, context, &hash | 201 | store, context, &hash |
187 | ); | 202 | ); |
188 | 203 | ||
189 | if (GNUNET_YES != GNUNET_CONTAINER_multihashmap_remove (map, &hash, contact)) | 204 | if (GNUNET_YES != GNUNET_CONTAINER_multihashmap_remove (map, &hash, contact)) |
190 | GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Removing a contact failed: %s\n", | 205 | GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Removing a contact failed: %s\n", |
191 | GNUNET_h2s(&hash)); | 206 | GNUNET_h2s (&hash)); |
192 | 207 | ||
193 | destroy_contact (contact); | 208 | destroy_contact (contact); |
194 | } | 209 | } |