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 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 {
34 
35  room->handle = handle;
36  GNUNET_memcpy(&(room->key), key, sizeof(*key));
37 
38  room->opened = GNUNET_NO;
39  room->contact_id = NULL;
40 
42 
43  init_list_tunnels (&(room->entries));
44 
46 
47  return room;
48 }
49 
50 static int
51 iterate_destroy_message (void *cls, const struct GNUNET_HashCode *key, void *value)
52 {
53  struct GNUNET_MESSENGER_Message *message = value;
54 
55  destroy_message (message);
56 
57  return GNUNET_YES;
58 }
59 
60 void
62 {
63  if (room->members)
65 
66  clear_list_tunnels (&(room->entries));
67 
68  if (room->messages)
69  {
71 
73  }
74 
75  if (room->contact_id)
76  GNUNET_free(room->contact_id);
77 
78  GNUNET_free(room);
79 }
80 
81 const struct GNUNET_MESSENGER_Message*
82 get_room_message (const struct GNUNET_MESSENGER_Room *room, const struct GNUNET_HashCode *hash)
83 {
84  return GNUNET_CONTAINER_multihashmap_get (room->messages, hash);
85 }
86 
87 static void
89  const struct GNUNET_HashCode *hash)
90 {
91  struct GNUNET_MESSENGER_Contact *contact = get_handle_contact_by_pubkey (room->handle, &(message->body.join.key));
92 
93  if (contact)
94  GNUNET_CONTAINER_multishortmap_put (room->members, &(message->header.sender_id), contact,
96 }
97 
98 static void
100  const struct GNUNET_HashCode *hash)
101 {
103 }
104 
105 static void
107  const struct GNUNET_HashCode *hash)
108 {
110  &(message->header.sender_id));
111 
112  if (contact)
113  set_contact_name (contact, message->body.name.name);
114 }
115 
116 static void
118  const struct GNUNET_HashCode *hash)
119 {
121  &(message->header.sender_id));
122 
123  if (contact)
124  swap_handle_contact_by_pubkey (room->handle, contact, &(message->body.key.key));
125 }
126 
127 static void
129  const struct GNUNET_HashCode *hash)
130 {
132  &(message->header.sender_id));
133 
134  if ((contact) && (GNUNET_OK
135  == GNUNET_CONTAINER_multishortmap_put (room->members, &(message->body.id.id), contact,
137  GNUNET_CONTAINER_multishortmap_remove (room->members, &(message->header.sender_id), contact);
138 }
139 
140 static void
142  const struct GNUNET_HashCode *hash)
143 {
144  if ((room->contact_id) && (0 == GNUNET_memcmp(&(message->header.sender_id), room->contact_id)))
145  {
146  struct GNUNET_MESSENGER_ListTunnel *match = find_list_tunnels (&(room->entries), &(message->body.miss.peer), NULL);
147 
148  if (match)
149  remove_from_list_tunnels (&(room->entries), match);
150  }
151 }
152 
153 void
155  const struct GNUNET_HashCode *hash)
156 {
158  return;
159 
160  switch (message->header.kind)
161  {
163  handle_join_message (room, message, hash);
164  break;
166  handle_leave_message (room, message, hash);
167  break;
169  handle_name_message (room, message, hash);
170  break;
172  handle_key_message (room, message, hash);
173  break;
175  handle_id_message (room, message, hash);
176  break;
178  handle_miss_message (room, message, hash);
179  break;
180  default:
181  break;
182  }
183 
184  struct GNUNET_MESSENGER_Message *clone = copy_message (message);
185 
186  if (GNUNET_OK != GNUNET_CONTAINER_multihashmap_put (room->messages, hash, clone,
188  destroy_message (clone);
189 }
struct GNUNET_PeerIdentity peer
The peer identity of a disconnected door to a room.
struct GNUNET_MESSENGER_MessageHeader header
Header.
static void handle_name_message(struct GNUNET_MESSENGER_Room *room, const struct GNUNET_MESSENGER_Message *message, const struct GNUNET_HashCode *hash)
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.
struct GNUNET_HashCode key
void destroy_room(struct GNUNET_MESSENGER_Room *room)
Destroys a room and frees its memory fully from the client API.
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
#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)...
struct GNUNET_CONTAINER_MultiHashMap * messages
struct GNUNET_MESSENGER_ListTunnels entries
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.
struct GNUNET_ShortHashCode * contact_id
static void handle_leave_message(struct GNUNET_MESSENGER_Room *room, const struct GNUNET_MESSENGER_Message *message, const struct GNUNET_HashCode *hash)
, &#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.
static char * value
Value of the record to add/remove.
void set_contact_name(struct GNUNET_MESSENGER_SrvContact *contact, const char *name)
Changes the current name of a given contact by copying it from the parameter name.
static void handle_id_message(struct GNUNET_MESSENGER_Room *room, const struct GNUNET_MESSENGER_Message *message, const struct GNUNET_HashCode *hash)
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_MessageBody body
Body.
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).
int GNUNET_CONTAINER_multishortmap_remove_all(struct GNUNET_CONTAINER_MultiShortmap *map, const struct GNUNET_ShortHashCode *key)
Remove all entries for the given key from the map.
A 512-bit hashcode.
messenger api: client implementation of GNUnet MESSENGER service
static void handle_miss_message(struct GNUNET_MESSENGER_Room *room, const struct GNUNET_MESSENGER_Message *message, const struct GNUNET_HashCode *hash)
struct GNUNET_MESSENGER_Handle * handle
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.
void handle_room_message(struct GNUNET_MESSENGER_Room *room, 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 swap_handle_contact_by_pubkey(struct GNUNET_MESSENGER_Handle *handle, struct GNUNET_MESSENGER_Contact *contact, const struct GNUNET_IDENTITY_PublicKey *pubkey)
Changes the public key for a contact known to a handle to a specific public key and updates local map...
void init_list_tunnels(struct GNUNET_MESSENGER_ListTunnels *tunnels)
Initializes list of tunnels peer identities as empty list.
void * GNUNET_CONTAINER_multishortmap_get(const struct GNUNET_CONTAINER_MultiShortmap *map, const struct GNUNET_ShortHashCode *key)
Given a key find a value in the map matching the key.
struct GNUNET_CONTAINER_MultiHashMap * GNUNET_CONTAINER_multihashmap_create(unsigned int len, int do_not_copy_keys)
Create a multi hash map.
struct GNUNET_MESSENGER_Contact * get_handle_contact_by_pubkey(const struct GNUNET_MESSENGER_Handle *handle, const struct GNUNET_IDENTITY_PublicKey *pubkey)
Returns a contact known to a handle identified by a given public key.
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)
messenger api: client implementation of GNUnet MESSENGER service
struct GNUNET_MESSENGER_MessageJoin join
int GNUNET_CONTAINER_multihashmap_iterate(struct GNUNET_CONTAINER_MultiHashMap *map, GNUNET_CONTAINER_MulitHashMapIteratorCallback it, void *it_cls)
Iterate over all entries in the map.
static void handle_join_message(struct GNUNET_MESSENGER_Room *room, const struct GNUNET_MESSENGER_Message *message, const struct GNUNET_HashCode *hash)
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
#define GNUNET_free(ptr)
Wrapper around free.
static void handle_key_message(struct GNUNET_MESSENGER_Room *room, const struct GNUNET_MESSENGER_Message *message, const struct GNUNET_HashCode *hash)
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...