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