GNUnet  0.11.x
messenger_api_contact_store.c
Go to the documentation of this file.
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  */
27 
28 #include "messenger_api_contact.h"
29 #include "messenger_api_util.h"
30 
31 void
33 {
34  GNUNET_assert (store);
35 
38 }
39 
40 static int
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 
50 void
52 {
53  GNUNET_assert ((store) && (store->contacts));
54 
57 
60 }
61 
62 static struct GNUNET_CONTAINER_MultiHashMap*
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 
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 
92  store, context, &hash
93  );
94 
95  return GNUNET_CONTAINER_multihashmap_get (map, &hash);
96 }
97 
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 
109  store, context, &hash
110  );
111 
113 
114  if (contact)
115  {
116  if (0 != GNUNET_memcmp(pubkey, get_contact_key(contact)))
117  {
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,
131  return contact;
132 
133  destroy_contact (contact);
134  return NULL;
135 }
136 
137 void
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 
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 
162  store, next_context, &hash
163  );
164 
165  if (GNUNET_OK != GNUNET_CONTAINER_multihashmap_put (map, &hash, contact,
167  GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Updating a contact failed: %s\n",
168  GNUNET_h2s(&hash));
169  }
170 }
171 
172 void
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 
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 }
static struct GNUNET_CONTAINER_MultiPeerMap * map
Handle to the map used to store old latency values for peers.
struct GNUNET_HashCode key
The key used in the DHT.
static pa_context * context
Pulseaudio context.
static struct GNUNET_IDENTITY_PublicKey pubkey
Public key of the zone to look in.
static char * value
Value of the record to add/remove.
#define GNUNET_log(kind,...)
#define GNUNET_memcmp(a, b)
Compare memory in a and b, where both must be of the same pointer type.
#define GNUNET_memcpy(dst, src, n)
Call memcpy() but check for n being 0 first.
@ GNUNET_OK
Definition: gnunet_common.h:95
@ GNUNET_YES
Definition: gnunet_common.h:97
@ GNUNET_NO
Definition: gnunet_common.h:94
void GNUNET_CRYPTO_hash(const void *block, size_t size, struct GNUNET_HashCode *ret)
Compute hash of a given block.
Definition: crypto_hash.c:41
int GNUNET_CRYPTO_hash_cmp(const struct GNUNET_HashCode *h1, const struct GNUNET_HashCode *h2)
Compare function for HashCodes, producing a total ordering of all hashcodes.
Definition: crypto_hash.c:201
int GNUNET_CONTAINER_multihashmap_remove(struct GNUNET_CONTAINER_MultiHashMap *map, const struct GNUNET_HashCode *key, const void *value)
Remove the given key-value pair from the map.
struct GNUNET_CONTAINER_MultiHashMap * GNUNET_CONTAINER_multihashmap_create(unsigned int len, int do_not_copy_keys)
Create a multi hash map.
int GNUNET_CONTAINER_multihashmap_put(struct GNUNET_CONTAINER_MultiHashMap *map, const struct GNUNET_HashCode *key, void *value, enum GNUNET_CONTAINER_MultiHashMapOption opt)
Store a key-value pair in the map.
int GNUNET_CONTAINER_multihashmap_iterate(struct GNUNET_CONTAINER_MultiHashMap *map, GNUNET_CONTAINER_MulitHashMapIteratorCallback it, void *it_cls)
Iterate over all entries in the map.
void GNUNET_CONTAINER_multihashmap_destroy(struct GNUNET_CONTAINER_MultiHashMap *map)
Destroy a hash map.
void * GNUNET_CONTAINER_multihashmap_get(const struct GNUNET_CONTAINER_MultiHashMap *map, const struct GNUNET_HashCode *key)
Given a key find a value in the map matching the key.
@ GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST
, ' bother checking if a value already exists (faster than GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE...
char * GNUNET_IDENTITY_public_key_to_string(const struct GNUNET_IDENTITY_PublicKey *key)
Creates a (Base32) string representation of the public key.
#define GNUNET_assert(cond)
Use this for fatal errors that cannot be handled.
const char * GNUNET_h2s(const struct GNUNET_HashCode *hc)
Convert a hash value to a string (for printing debug messages).
@ GNUNET_ERROR_TYPE_WARNING
@ GNUNET_ERROR_TYPE_INVALID
#define GNUNET_free(ptr)
Wrapper around free.
struct GNUNET_MESSENGER_Contact * create_contact(const struct GNUNET_IDENTITY_PublicKey *key)
Creates and allocates a new contact with a given public key from an EGO.
void destroy_contact(struct GNUNET_MESSENGER_Contact *contact)
Destroys a contact and frees its memory fully.
const struct GNUNET_IDENTITY_PublicKey * get_contact_key(const struct GNUNET_MESSENGER_Contact *contact)
Returns the public key of a given contact.
messenger api: client implementation of GNUnet MESSENGER service
struct GNUNET_MESSENGER_Contact * get_store_contact(struct GNUNET_MESSENGER_ContactStore *store, const struct GNUNET_HashCode *context, const struct GNUNET_IDENTITY_PublicKey *pubkey)
Returns a contact using a specific public key.
void clear_contact_store(struct GNUNET_MESSENGER_ContactStore *store)
Clears a contact store, wipes its content and deallocates its memory.
void init_contact_store(struct GNUNET_MESSENGER_ContactStore *store)
Initializes a contact store as fully empty.
static struct GNUNET_CONTAINER_MultiHashMap * select_store_contact_map(struct GNUNET_MESSENGER_ContactStore *store, const struct GNUNET_HashCode *context, struct GNUNET_HashCode *hash)
void remove_store_contact(struct GNUNET_MESSENGER_ContactStore *store, struct GNUNET_MESSENGER_Contact *contact, const struct GNUNET_HashCode *context)
Removes a contact from the store which uses a given member context.
void update_store_contact(struct GNUNET_MESSENGER_ContactStore *store, struct GNUNET_MESSENGER_Contact *contact, const struct GNUNET_HashCode *context, const struct GNUNET_HashCode *next_context, const struct GNUNET_IDENTITY_PublicKey *pubkey)
Moves a contact from the store to another location matching a given public key and member context.
static int iterate_destroy_contacts(void *cls, const struct GNUNET_HashCode *key, void *value)
struct GNUNET_MESSENGER_Contact * get_store_contact_raw(struct GNUNET_MESSENGER_ContactStore *store, const struct GNUNET_HashCode *context, const struct GNUNET_HashCode *key_hash)
Returns a contact using the hash of a specific public key.
messenger api: client implementation of GNUnet MESSENGER service
const struct GNUNET_IDENTITY_PublicKey * get_anonymous_public_key()
Returns the public identity key of GNUNET_IDENTITY_ego_get_anonymous() without recalculating it every...
messenger api: client implementation of GNUnet MESSENGER service
Internal representation of the hash map.
A 512-bit hashcode.
An identity key as per LSD0001.
struct GNUNET_CONTAINER_MultiHashMap * contacts
struct GNUNET_CONTAINER_MultiHashMap * anonymous
struct GNUNET_IDENTITY_PublicKey public_key