GNUnet  0.11.x
messenger_api_room.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  */
26 #include "messenger_api_room.h"
27 
28 #include "messenger_api_handle.h"
29 
32 {
33  GNUNET_assert((handle) && (key));
34 
36 
37  room->handle = handle;
38  GNUNET_memcpy(&(room->key), key, sizeof(*key));
39 
40  room->opened = GNUNET_NO;
41  room->contact_id = NULL;
42 
43  init_list_tunnels (&(room->entries));
44 
47 
48  return room;
49 }
50 
51 static int
52 iterate_destroy_message (void *cls, const struct GNUNET_HashCode *key, void *value)
53 {
55 
56  destroy_message (entry->message);
57  GNUNET_free(entry);
58 
59  return GNUNET_YES;
60 }
61 
62 void
64 {
65  GNUNET_assert(room);
66 
67  clear_list_tunnels (&(room->entries));
68 
69  if (room->messages)
70  {
72 
74  }
75 
76  if (room->members)
78 
79  if (room->contact_id)
80  GNUNET_free(room->contact_id);
81 
82  GNUNET_free(room);
83 }
84 
85 const struct GNUNET_MESSENGER_Message*
86 get_room_message (const struct GNUNET_MESSENGER_Room *room, const struct GNUNET_HashCode *hash)
87 {
88  GNUNET_assert((room) && (hash));
89 
91  room->messages, hash
92  );
93 
94  return (entry? entry->message : NULL);
95 }
96 
98 get_room_sender (const struct GNUNET_MESSENGER_Room *room, const struct GNUNET_HashCode *hash)
99 {
100  GNUNET_assert((room) && (hash));
101 
103  room->messages, hash
104  );
105 
106  return (entry? entry->sender : NULL);
107 }
108 
109 static struct GNUNET_MESSENGER_Contact*
111  const struct GNUNET_MESSENGER_Message *message, const struct GNUNET_HashCode *hash)
112 {
113  if (!sender)
114  {
116  struct GNUNET_HashCode context;
117 
118  get_context_from_member(&(room->key), &(message->header.sender_id), &context);
119 
120  sender = get_store_contact(store, &context, &(message->body.join.key));
121  }
122 
126  increase_contact_rc(sender);
127 
128  return sender;
129 }
130 
131 static void
133  const struct GNUNET_MESSENGER_Message *message, const struct GNUNET_HashCode *hash)
134 {
135  if ((!sender) ||
137  return;
138 
139  struct GNUNET_HashCode context;
140  get_context_from_member(&(room->key), &(message->header.sender_id), &context);
141 
143 
144  if (GNUNET_YES == decrease_contact_rc(sender))
145  GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "A contact does not share any room with you anymore!\n");
146 }
147 
148 static void
150  const struct GNUNET_MESSENGER_Message *message, const struct GNUNET_HashCode *hash)
151 {
152  if (!sender)
153  return;
154 
155  set_contact_name (sender, message->body.name.name);
156 }
157 
158 static void
160  const struct GNUNET_MESSENGER_Message *message, const struct GNUNET_HashCode *hash)
161 {
162  if (!sender)
163  return;
164 
165  struct GNUNET_HashCode context;
166  get_context_from_member(&(room->key), &(message->header.sender_id), &context);
167 
169 
170  update_store_contact(store, sender, &context, &context, &(message->body.key.key));
171 }
172 
173 static void
175  const struct GNUNET_MESSENGER_Message *message, const struct GNUNET_HashCode *hash)
176 {
177  if ((!sender) ||
178  (GNUNET_YES != GNUNET_CONTAINER_multishortmap_remove(room->members, &(message->header.sender_id), sender)) ||
179  (GNUNET_OK != GNUNET_CONTAINER_multishortmap_put(room->members, &(message->body.id.id), sender,
181  return;
182 
183  struct GNUNET_HashCode context, next_context;
184  get_context_from_member(&(room->key), &(message->header.sender_id), &context);
185  get_context_from_member(&(room->key), &(message->body.id.id), &next_context);
186 
188 
189  update_store_contact(store, sender, &context, &next_context, get_contact_key(sender));
190 }
191 
192 static void
194  const struct GNUNET_MESSENGER_Message *message, const struct GNUNET_HashCode *hash)
195 {
196  if ((room->contact_id) && (0 == GNUNET_memcmp(&(message->header.sender_id), room->contact_id)))
197  {
198  struct GNUNET_MESSENGER_ListTunnel *match = find_list_tunnels (&(room->entries), &(message->body.miss.peer), NULL);
199 
200  if (match)
201  remove_from_list_tunnels (&(room->entries), match);
202  }
203 }
204 
205 static void
207  const struct GNUNET_MESSENGER_Message *message, const struct GNUNET_HashCode *hash)
208 {
210  room->messages, &(message->body.delete.hash)
211  );
212 
213  if ((entry) && ((entry->sender == sender) || (get_handle_contact (room->handle, &(room->key)) == sender)) &&
214  (GNUNET_YES == GNUNET_CONTAINER_multihashmap_remove (room->messages, &(message->body.delete.hash), entry)))
215  {
216  destroy_message (entry->message);
217  GNUNET_free(entry);
218  }
219 }
220 
223  const struct GNUNET_MESSENGER_Message *message, const struct GNUNET_HashCode *hash)
224 {
226  return sender;
227 
228  switch (message->header.kind)
229  {
231  sender = handle_join_message (room, sender, message, hash);
232  break;
234  handle_leave_message (room, sender, message, hash);
235  break;
237  handle_name_message (room, sender, message, hash);
238  break;
240  handle_key_message (room, sender, message, hash);
241  break;
243  handle_id_message (room, sender, message, hash);
244  break;
246  handle_miss_message (room, sender, message, hash);
247  break;
249  handle_delete_message (room, sender, message, hash);
250  break;
251  default:
252  break;
253  }
254 
256 
257  if (!entry)
258  return sender;
259 
260  entry->sender = sender;
261  entry->message = copy_message (message);
262 
263  if (GNUNET_OK != GNUNET_CONTAINER_multihashmap_put (room->messages, hash, entry,
265  {
266  destroy_message (entry->message);
267  GNUNET_free(entry);
268  }
269 
270  return sender;
271 }
272 
274 {
277  void *cls;
278 };
279 
280 static int
282 {
284  struct GNUNET_MESSENGER_Contact *contact = value;
285 
286  return call->callback(call->cls, call->room, contact);
287 }
288 
289 int
291  void* cls)
292 {
293  GNUNET_assert(room);
294 
295  if (!callback)
296  return GNUNET_CONTAINER_multishortmap_iterate(room->members, NULL, NULL);
297 
298  struct GNUNET_MESSENGER_MemberCall call;
299 
300  call.room = room;
301  call.callback = callback;
302  call.cls = cls;
303 
304  GNUNET_assert(callback);
305 
307 }
struct GNUNET_PeerIdentity peer
The peer identity of a disconnected door to a room.
struct GNUNET_MESSENGER_MessageHeader header
Header.
struct GNUNET_MESSENGER_Contact * get_room_sender(const struct GNUNET_MESSENGER_Room *room, const struct GNUNET_HashCode *hash)
Returns a messages sender locally stored from a map for a given hash in a room.
void destroy_message(struct GNUNET_MESSENGER_Message *message)
Destroys a message and frees its memory fully.
struct GNUNET_ShortHashCode sender_id
The senders id inside of the room the message was sent in.
struct GNUNET_MESSENGER_Room * create_room(struct GNUNET_MESSENGER_Handle *handle, const struct GNUNET_HashCode *key)
Creates and allocates a new room for a handle with a given key for the client API.
static int iterate_local_members(void *cls, const struct GNUNET_ShortHashCode *key, void *value)
static void handle_miss_message(struct GNUNET_MESSENGER_Room *room, struct GNUNET_MESSENGER_Contact *sender, const struct GNUNET_MESSENGER_Message *message, const struct GNUNET_HashCode *hash)
int decrease_contact_rc(struct GNUNET_MESSENGER_Contact *contact)
Decreases the reference counter if possible (can not underflow!) of a given contact and returns GNUNE...
struct GNUNET_HashCode key
struct GNUNET_MESSENGER_Contact * handle_room_message(struct GNUNET_MESSENGER_Room *room, struct GNUNET_MESSENGER_Contact *sender, const struct GNUNET_MESSENGER_Message *message, const struct GNUNET_HashCode *hash)
Handles a message with a given hash in a room for the client API to update members and its informatio...
void destroy_room(struct GNUNET_MESSENGER_Room *room)
Destroys a room and frees its memory fully from the client API.
void get_context_from_member(const struct GNUNET_HashCode *key, const struct GNUNET_ShortHashCode *id, struct GNUNET_HashCode *context)
Calculates the context hash of a member in a room and returns it.
const struct GNUNET_IDENTITY_PublicKey * get_contact_key(const struct GNUNET_MESSENGER_Contact *contact)
Returns the public key of a given contact.
static void handle_key_message(struct GNUNET_MESSENGER_Room *room, struct GNUNET_MESSENGER_Contact *sender, const struct GNUNET_MESSENGER_Message *message, const struct GNUNET_HashCode *hash)
int iterate_room_members(struct GNUNET_MESSENGER_Room *room, GNUNET_MESSENGER_MemberCallback callback, void *cls)
Iterates through all members of a given room to forward each of them to a selected callback with a cu...
#define GNUNET_assert(cond)
Use this for fatal errors that cannot be handled.
struct GNUNET_CONTAINER_MultiShortmap * members
struct GNUNET_ShortHashCode id
The new id which will replace the senders id in a room.
struct GNUNET_MESSENGER_MessageKey key
struct GNUNET_MESSENGER_Room * room
#define GNUNET_memcpy(dst, src, n)
Call memcpy() but check for n being 0 first.
int GNUNET_CONTAINER_multishortmap_remove(struct GNUNET_CONTAINER_MultiShortmap *map, const struct GNUNET_ShortHashCode *key, const void *value)
Remove the given key-value pair from the map.
#define GNUNET_new(type)
Allocate a struct or union of the given type.
int GNUNET_CONTAINER_multihashmap_contains(const struct GNUNET_CONTAINER_MultiHashMap *map, const struct GNUNET_HashCode *key)
Check if the map contains any value under the given key (including values that are NULL)...
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...
GNUNET_MESSENGER_MemberCallback callback
struct GNUNET_MESSENGER_MessageDelete delete
struct GNUNET_HashCode hash
The hash of the message to delete.
struct GNUNET_CONTAINER_MultiHashMap * messages
struct GNUNET_MESSENGER_ListTunnels entries
A 256-bit hashcode.
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.
static void handle_id_message(struct GNUNET_MESSENGER_Room *room, struct GNUNET_MESSENGER_Contact *sender, const struct GNUNET_MESSENGER_Message *message, const struct GNUNET_HashCode *hash)
static void handle_leave_message(struct GNUNET_MESSENGER_Room *room, struct GNUNET_MESSENGER_Contact *sender, const struct GNUNET_MESSENGER_Message *message, const struct GNUNET_HashCode *hash)
struct GNUNET_ShortHashCode * contact_id
, &#39; bother checking if a value already exists (faster than GNUNET_CONTAINER_MULTIHASHMAPOPTION_...
struct GNUNET_IDENTITY_PublicKey key
The senders public key to verify its signatures.
int GNUNET_CONTAINER_multishortmap_iterate(struct GNUNET_CONTAINER_MultiShortmap *map, GNUNET_CONTAINER_ShortmapIterator it, void *it_cls)
Iterate over all entries in the map.
static char * value
Value of the record to add/remove.
void GNUNET_CONTAINER_multihashmap_destroy(struct GNUNET_CONTAINER_MultiHashMap *map)
Destroy a hash map.
struct GNUNET_IDENTITY_PublicKey key
The new public key which replaces the current senders public key.
struct GNUNET_MESSENGER_MessageMiss miss
struct GNUNET_MESSENGER_Message * message
struct GNUNET_MESSENGER_MessageBody body
Body.
void increase_contact_rc(struct GNUNET_MESSENGER_Contact *contact)
Increases the reference counter of a given contact which is zero as default.
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.
int GNUNET_CONTAINER_multishortmap_put(struct GNUNET_CONTAINER_MultiShortmap *map, const struct GNUNET_ShortHashCode *key, void *value, enum GNUNET_CONTAINER_MultiHashMapOption opt)
Store a key-value pair in the map.
void clear_list_tunnels(struct GNUNET_MESSENGER_ListTunnels *tunnels)
Clears the list of tunnels peer identities.
static struct GNUNET_DNS_Handle * handle
Handle to transport service.
const struct GNUNET_MESSENGER_Message * get_room_message(const struct GNUNET_MESSENGER_Room *room, const struct GNUNET_HashCode *hash)
Returns a message locally stored from a map for a given hash in a room.
struct GNUNET_CONTAINER_MultiShortmap * GNUNET_CONTAINER_multishortmap_create(unsigned int len, int do_not_copy_keys)
Create a multi peer map (hash map for public keys of peers).
A 512-bit hashcode.
messenger api: client implementation of GNUnet MESSENGER service
static void handle_name_message(struct GNUNET_MESSENGER_Room *room, struct GNUNET_MESSENGER_Contact *sender, const struct GNUNET_MESSENGER_Message *message, const struct GNUNET_HashCode *hash)
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.
struct GNUNET_MESSENGER_Contact * sender
struct GNUNET_MESSENGER_Handle * handle
int(* GNUNET_MESSENGER_MemberCallback)(void *cls, struct GNUNET_MESSENGER_Room *room, const struct GNUNET_MESSENGER_Contact *contact)
Method called for each member in a room during iteration.
struct GNUNET_MESSENGER_MessageName name
struct GNUNET_HashCode key
The key used in the DHT.
struct GNUNET_MESSENGER_Message * copy_message(const struct GNUNET_MESSENGER_Message *message)
Creates and allocates a copy of a given message.
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.
void GNUNET_CONTAINER_multishortmap_destroy(struct GNUNET_CONTAINER_MultiShortmap *map)
Destroy a hash map.
#define GNUNET_memcmp(a, b)
Compare memory in a and b, where both must be of the same pointer type.
struct GNUNET_MESSENGER_Contact * get_handle_contact(struct GNUNET_MESSENGER_Handle *handle, const struct GNUNET_HashCode *key)
Returns the contact of a given handle in a room identified by a given key.
struct GNUNET_MESSENGER_ContactStore * get_handle_contact_store(struct GNUNET_MESSENGER_Handle *handle)
Returns the used contact store of a given handle.
Allow multiple values with the same key.
static struct GNUNET_CONVERSATION_Call * call
Call handle (for active outgoing call).
void init_list_tunnels(struct GNUNET_MESSENGER_ListTunnels *tunnels)
Initializes list of tunnels peer identities as empty list.
#define GNUNET_log(kind,...)
struct GNUNET_CONTAINER_MultiHashMap * GNUNET_CONTAINER_multihashmap_create(unsigned int len, int do_not_copy_keys)
Create a multi hash map.
char * name
The new name which replaces the current senders name.
static int iterate_destroy_message(void *cls, const struct GNUNET_HashCode *key, void *value)
static struct GNUNET_MESSENGER_Contact * handle_join_message(struct GNUNET_MESSENGER_Room *room, struct GNUNET_MESSENGER_Contact *sender, const struct GNUNET_MESSENGER_Message *message, const struct GNUNET_HashCode *hash)
messenger api: client implementation of GNUnet MESSENGER service
struct GNUNET_MESSENGER_MessageJoin join
static void handle_delete_message(struct GNUNET_MESSENGER_Room *room, struct GNUNET_MESSENGER_Contact *sender, const struct GNUNET_MESSENGER_Message *message, const struct GNUNET_HashCode *hash)
int GNUNET_CONTAINER_multihashmap_iterate(struct GNUNET_CONTAINER_MultiHashMap *map, GNUNET_CONTAINER_MulitHashMapIteratorCallback it, void *it_cls)
Iterate over all entries in the map.
struct GNUNET_MESSENGER_ListTunnel * find_list_tunnels(struct GNUNET_MESSENGER_ListTunnels *tunnels, const struct GNUNET_PeerIdentity *peer, size_t *index)
Searches linearly through the list of tunnels peer identities for matching a specific peer identity a...
struct GNUNET_MESSENGER_MessageId id
void set_contact_name(struct GNUNET_MESSENGER_Contact *contact, const char *name)
Changes the current name of a given contact by copying it from the parameter name.
int GNUNET_CONTAINER_multishortmap_contains_value(const struct GNUNET_CONTAINER_MultiShortmap *map, const struct GNUNET_ShortHashCode *key, const void *value)
Check if the map contains the given value under the given key.
#define GNUNET_free(ptr)
Wrapper around free.
enum GNUNET_MESSENGER_MessageKind kind
The kind of the message.
struct GNUNET_MESSENGER_ListTunnel * remove_from_list_tunnels(struct GNUNET_MESSENGER_ListTunnels *tunnels, struct GNUNET_MESSENGER_ListTunnel *element)
Removes a specific element from the list of tunnels peer identities and returns the next element in t...