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.c193
1 files changed, 0 insertions, 193 deletions
diff --git a/src/messenger/messenger_api_contact_store.c b/src/messenger/messenger_api_contact_store.c
deleted file mode 100644
index 1c10a8fbf..000000000
--- a/src/messenger/messenger_api_contact_store.c
+++ /dev/null
@@ -1,193 +0,0 @@
1/*
2 This file is part of GNUnet.
3 Copyright (C) 2020--2021 GNUnet e.V.
4
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
7 by the Free Software Foundation, either version 3 of the License,
8 or (at your option) any later version.
9
10 GNUnet is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Affero General Public License for more details.
14
15 You should have received a copy of the GNU Affero General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
17
18 SPDX-License-Identifier: AGPL3.0-or-later
19 */
20/**
21 * @author Tobias Frisch
22 * @file src/messenger/messenger_api_contact_store.c
23 * @brief messenger api: client implementation of GNUnet MESSENGER service
24 */
25
26#include "messenger_api_contact_store.h"
27
28#include "messenger_api_contact.h"
29#include "messenger_api_util.h"
30
31void
32init_contact_store (struct GNUNET_MESSENGER_ContactStore *store)
33{
34 GNUNET_assert (store);
35
36 store->anonymous = GNUNET_CONTAINER_multihashmap_create (8, GNUNET_NO);
37 store->contacts = GNUNET_CONTAINER_multihashmap_create (8, GNUNET_NO);
38}
39
40static int
41iterate_destroy_contacts (void *cls,
42 const struct GNUNET_HashCode *key,
43 void *value)
44{
45 struct GNUNET_MESSENGER_Contact *contact = value;
46 destroy_contact (contact);
47 return GNUNET_YES;
48}
49
50void
51clear_contact_store (struct GNUNET_MESSENGER_ContactStore *store)
52{
53 GNUNET_assert ((store) && (store->contacts));
54
55 GNUNET_CONTAINER_multihashmap_iterate (store->anonymous, iterate_destroy_contacts, NULL);
56 GNUNET_CONTAINER_multihashmap_iterate (store->contacts, iterate_destroy_contacts, NULL);
57
58 GNUNET_CONTAINER_multihashmap_destroy (store->anonymous);
59 GNUNET_CONTAINER_multihashmap_destroy (store->contacts);
60}
61
62static struct GNUNET_CONTAINER_MultiHashMap*
63select_store_contact_map (struct GNUNET_MESSENGER_ContactStore *store,
64 const struct GNUNET_HashCode *context,
65 struct GNUNET_HashCode *hash)
66{
67 const struct GNUNET_IDENTITY_PublicKey *anonymous = get_anonymous_public_key ();
68
69 struct GNUNET_HashCode anonHash;
70 GNUNET_CRYPTO_hash (anonymous, sizeof(*anonymous), &anonHash);
71
72 if ((context) && (0 == GNUNET_CRYPTO_hash_cmp(hash, &anonHash)))
73 {
74 GNUNET_memcpy(hash, context, sizeof(*context));
75 return store->anonymous;
76 }
77 else
78 return store->contacts;
79}
80
81struct GNUNET_MESSENGER_Contact*
82get_store_contact_raw (struct GNUNET_MESSENGER_ContactStore *store,
83 const struct GNUNET_HashCode *context,
84 const struct GNUNET_HashCode *key_hash)
85{
86 GNUNET_assert ((store) && (store->contacts) && (context) && (key_hash));
87
88 struct GNUNET_HashCode hash;
89 GNUNET_memcpy(&hash, key_hash, sizeof(*key_hash));
90
91 struct GNUNET_CONTAINER_MultiHashMap *map = select_store_contact_map (
92 store, context, &hash
93 );
94
95 return GNUNET_CONTAINER_multihashmap_get (map, &hash);
96}
97
98struct GNUNET_MESSENGER_Contact*
99get_store_contact (struct GNUNET_MESSENGER_ContactStore *store,
100 const struct GNUNET_HashCode *context,
101 const struct GNUNET_IDENTITY_PublicKey *pubkey)
102{
103 GNUNET_assert ((store) && (store->contacts) && (context) && (pubkey));
104
105 struct GNUNET_HashCode hash;
106 GNUNET_CRYPTO_hash (pubkey, sizeof(*pubkey), &hash);
107
108 struct GNUNET_CONTAINER_MultiHashMap *map = select_store_contact_map (
109 store, context, &hash
110 );
111
112 struct GNUNET_MESSENGER_Contact *contact = GNUNET_CONTAINER_multihashmap_get (map, &hash);
113
114 if (contact)
115 {
116 if (0 != GNUNET_memcmp(pubkey, get_contact_key(contact)))
117 {
118 char* str = GNUNET_IDENTITY_public_key_to_string (get_contact_key(contact));
119 GNUNET_log (GNUNET_ERROR_TYPE_INVALID, "Contact in store uses wrong key: %s\n", str);
120 GNUNET_free (str);
121 return NULL;
122 }
123
124 return contact;
125 }
126
127 contact = create_contact (pubkey);
128
129 if (GNUNET_OK == GNUNET_CONTAINER_multihashmap_put (map, &hash, contact,
130 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST))
131 return contact;
132
133 destroy_contact (contact);
134 return NULL;
135}
136
137void
138update_store_contact (struct GNUNET_MESSENGER_ContactStore *store,
139 struct GNUNET_MESSENGER_Contact* contact,
140 const struct GNUNET_HashCode *context,
141 const struct GNUNET_HashCode *next_context,
142 const struct GNUNET_IDENTITY_PublicKey *pubkey)
143{
144 GNUNET_assert ((store) && (store->contacts) && (contact) && (pubkey));
145
146 const struct GNUNET_IDENTITY_PublicKey* oldkey = get_contact_key (contact);
147
148 struct GNUNET_HashCode hash;
149 GNUNET_CRYPTO_hash (oldkey, sizeof(*oldkey), &hash);
150
151 struct GNUNET_CONTAINER_MultiHashMap *map = select_store_contact_map (
152 store, context, &hash
153 );
154
155 if (GNUNET_YES == GNUNET_CONTAINER_multihashmap_remove (map, &hash, contact))
156 {
157 GNUNET_memcpy(&(contact->public_key), pubkey, sizeof(*pubkey));
158
159 GNUNET_CRYPTO_hash (pubkey, sizeof(*pubkey), &hash);
160
161 map = select_store_contact_map (
162 store, next_context, &hash
163 );
164
165 if (GNUNET_OK != GNUNET_CONTAINER_multihashmap_put (map, &hash, contact,
166 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST))
167 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Updating a contact failed: %s\n",
168 GNUNET_h2s(&hash));
169 }
170}
171
172void
173remove_store_contact (struct GNUNET_MESSENGER_ContactStore *store,
174 struct GNUNET_MESSENGER_Contact* contact,
175 const struct GNUNET_HashCode *context)
176{
177 GNUNET_assert ((store) && (store->contacts) && (contact));
178
179 const struct GNUNET_IDENTITY_PublicKey* pubkey = get_contact_key(contact);
180
181 struct GNUNET_HashCode hash;
182 GNUNET_CRYPTO_hash (pubkey, sizeof(*pubkey), &hash);
183
184 struct GNUNET_CONTAINER_MultiHashMap *map = select_store_contact_map (
185 store, context, &hash
186 );
187
188 if (GNUNET_YES != GNUNET_CONTAINER_multihashmap_remove (map, &hash, contact))
189 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Removing a contact failed: %s\n",
190 GNUNET_h2s(&hash));
191
192 destroy_contact (contact);
193}