GNUnet  0.11.x
gnunet-service-messenger_service.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  */
29 
30 static void
32 {
33  struct GNUNET_MESSENGER_Service *service = cls;
34 
35  if (service)
36  {
37  service->shutdown = NULL;
38 
39  destroy_service (service);
40  }
41 }
42 
45 {
46  GNUNET_assert((config) && (service_handle));
47 
49 
50  service->config = config;
51  service->service = service_handle;
52 
54 
55  service->dir = NULL;
56 
59  "MESSENGER_DIR", &(service->dir)))
60  {
61  if (service->dir)
62  GNUNET_free(service->dir);
63 
64  service->dir = NULL;
65  }
66  else
67  {
69  != GNUNET_DISK_directory_create (service->dir)))
70  {
71  GNUNET_free(service->dir);
72 
73  service->dir = NULL;
74  }
75  }
76 
77  service->cadet = GNUNET_CADET_connect (service->config);
78 
79  init_ego_store(get_service_ego_store(service), service->config);
80 
81  init_list_handles (&(service->handles));
82 
84 
86 
87  return service;
88 }
89 
90 static int
91 iterate_destroy_rooms (void *cls, const struct GNUNET_HashCode *key, void *value)
92 {
93  struct GNUNET_MESSENGER_SrvRoom *room = value;
94  destroy_room (room);
95  return GNUNET_YES;
96 }
97 
98 void
100 {
101  GNUNET_assert(service);
102 
103  if (service->shutdown)
104  {
106 
107  service->shutdown = NULL;
108  }
109 
111  clear_list_handles (&(service->handles));
112 
115 
117 
118  if (service->cadet)
119  {
120  GNUNET_CADET_disconnect (service->cadet);
121 
122  service->cadet = NULL;
123  }
124 
125  if (service->dir)
126  {
127  GNUNET_free(service->dir);
128 
129  service->dir = NULL;
130  }
131 
132  GNUNET_SERVICE_shutdown (service->service);
133 
134  GNUNET_free(service);
135 }
136 
139 {
140  GNUNET_assert(service);
141 
142  return &(service->ego_store);
143 }
144 
147 {
148  GNUNET_assert(service);
149 
150  return &(service->contact_store);
151 }
152 
155 {
156  GNUNET_assert((service) && (mq));
157 
158  struct GNUNET_MESSENGER_SrvHandle *handle = create_handle (service, mq);
159 
160  if (handle)
161  {
162  add_list_handle (&(service->handles), handle);
163  }
164 
165  return handle;
166 }
167 
168 void
170 {
171  GNUNET_assert((service) && (handle));
172 
173  if (!handle)
174  return;
175 
176  if (GNUNET_YES == remove_list_handle (&(service->handles), handle))
177  destroy_handle (handle);
178 }
179 
180 int
182 {
183  GNUNET_assert((service) && (peer));
184 
185  return GNUNET_CRYPTO_get_peer_identity (service->config, peer);
186 }
187 
190 {
191  GNUNET_assert((service) && (key));
192 
193  return GNUNET_CONTAINER_multihashmap_get (service->rooms, key);
194 }
195 
196 int
198  const struct GNUNET_HashCode *key)
199 {
200  GNUNET_assert((service) && (handle) && (key));
201 
202  struct GNUNET_MESSENGER_SrvRoom *room = get_service_room (service, key);
203 
204  if (room)
205  return open_room (room, handle);
206 
207  room = create_room (handle, key);
208 
209  if ((GNUNET_YES == open_room (room, handle)) &&
211  key, room,
213  return GNUNET_YES;
214 
215  destroy_room (room);
216  return GNUNET_NO;
217 }
218 
219 int
221  const struct GNUNET_PeerIdentity *door, const struct GNUNET_HashCode *key)
222 {
223  GNUNET_assert((service) && (handle) && (door) && (key));
224 
225  struct GNUNET_MESSENGER_SrvRoom *room = get_service_room (service, key);
226 
227  if (room)
228  {
229  if (GNUNET_YES == enter_room_at (room, handle, door))
230  return GNUNET_YES;
231  else
232  return GNUNET_NO;
233  }
234 
235  room = create_room (handle, key);
236 
237  if ((GNUNET_YES == enter_room_at (room, handle, door)) &&
239  key, room,
241  {
242  return GNUNET_YES;
243  }
244  else
245  {
246  destroy_room (room);
247  return GNUNET_NO;
248  }
249 
250 }
251 
252 int
254  const struct GNUNET_HashCode *key)
255 {
256  GNUNET_assert((service) && (handle) && (key));
257 
258  struct GNUNET_MESSENGER_SrvRoom *room = get_service_room (service, key);
259 
260  if (!room)
261  return GNUNET_NO;
262 
263  send_room_message (room, handle, create_message_leave ());
264 
265  const struct GNUNET_ShortHashCode *id = get_handle_member_id (handle, key);
266 
267  GNUNET_assert(id);
268 
270  return GNUNET_NO;
271 
273  &(service->handles), key);
274 
275  if (!member_handle)
276  {
277  if (GNUNET_OK == GNUNET_CONTAINER_multihashmap_remove (service->rooms, key, room))
278  {
279  destroy_room (room);
280  return GNUNET_YES;
281  }
282  else
283  return GNUNET_NO;
284  }
285 
286  if (room->host == handle)
287  room->host = member_handle;
288 
289  return GNUNET_YES;
290 }
291 
292 void
294  const struct GNUNET_MESSENGER_MemberSession *session,
295  const struct GNUNET_MESSENGER_Message *message, const struct GNUNET_HashCode *hash)
296 {
297  GNUNET_assert((service) && (room) && (session) && (message) && (hash));
298 
299  struct GNUNET_MESSENGER_ListHandle *element = service->handles.head;
300 
301  while (element)
302  {
303  notify_handle_message (element->handle, get_room_key(room), session, message, hash);
304  element = element->next;
305  }
306 }
int open_room(struct GNUNET_MESSENGER_SrvRoom *room, struct GNUNET_MESSENGER_SrvHandle *handle)
Tries to open a room for a given handle.
struct GNUNET_MESSENGER_SrvHandle * find_list_handle_by_member(const struct GNUNET_MESSENGER_ListHandles *handles, const struct GNUNET_HashCode *key)
Searches linearly through the list of handles for members of a specific room which is identified by a...
void GNUNET_CADET_disconnect(struct GNUNET_CADET_Handle *handle)
Disconnect from the cadet service.
Definition: cadet_api.c:775
struct GNUNET_MESSENGER_ContactStore * get_service_contact_store(struct GNUNET_MESSENGER_Service *service)
Returns the used contact store of a given service.
static struct GNUNET_SERVICE_Handle * service
Handle to our service instance.
GNUnet MESSENGER service.
int remove_list_handle(struct GNUNET_MESSENGER_ListHandles *handles, struct GNUNET_MESSENGER_SrvHandle *handle)
Removes the first entry matching with a specific handle from the list of handles and returns GNUNET_Y...
struct GNUNET_CADET_Handle * cadet
int entry_service_room(struct GNUNET_MESSENGER_Service *service, struct GNUNET_MESSENGER_SrvHandle *handle, const struct GNUNET_PeerIdentity *door, const struct GNUNET_HashCode *key)
Tries to enter a room using a given key for a service by a specific handle.
GNUnet MESSENGER service.
struct GNUNET_MESSENGER_ContactStore contact_store
Handle to a service.
Definition: service.c:116
int open_service_room(struct GNUNET_MESSENGER_Service *service, struct GNUNET_MESSENGER_SrvHandle *handle, const struct GNUNET_HashCode *key)
Tries to open a room using a given key for a service by a specific handle.
struct GNUNET_MESSENGER_ListHandles handles
struct GNUNET_SCHEDULER_Task * GNUNET_SCHEDULER_add_shutdown(GNUNET_SCHEDULER_TaskCallback task, void *task_cls)
Schedule a new task to be run on shutdown, that is when a CTRL-C signal is received, or when GNUNET_SCHEDULER_shutdown() is being invoked.
Definition: scheduler.c:1331
void init_contact_store(struct GNUNET_MESSENGER_ContactStore *store)
Initializes a contact store as fully empty.
int get_service_peer_identity(const struct GNUNET_MESSENGER_Service *service, struct GNUNET_PeerIdentity *peer)
Tries to write the peer identity of the peer running a service on to the peer parameter.
void destroy_room(struct GNUNET_MESSENGER_SrvRoom *room)
Destroys a room and frees its memory fully.
#define GNUNET_assert(cond)
Use this for fatal errors that cannot be handled.
void add_list_handle(struct GNUNET_MESSENGER_ListHandles *handles, struct GNUNET_MESSENGER_SrvHandle *handle)
Adds a specific handle to the end of the list.
static void callback_shutdown_service(void *cls)
void clear_ego_store(struct GNUNET_MESSENGER_EgoStore *store)
Clears an EGO-store, wipes its content and deallocates its memory.
int send_room_message(struct GNUNET_MESSENGER_SrvRoom *room, struct GNUNET_MESSENGER_SrvHandle *handle, struct GNUNET_MESSENGER_Message *message)
Sends a message from a given handle into a room.
#define GNUNET_new(type)
Allocate a struct or union of the given type.
enum GNUNET_GenericReturnValue GNUNET_CONFIGURATION_get_value_filename(const struct GNUNET_CONFIGURATION_Handle *cfg, const char *section, const char *option, char **value)
Get a configuration value that should be the name of a file or directory.
int close_service_room(struct GNUNET_MESSENGER_Service *service, struct GNUNET_MESSENGER_SrvHandle *handle, const struct GNUNET_HashCode *key)
Tries to close a room using a given key for a service by a specific handle.
void init_ego_store(struct GNUNET_MESSENGER_EgoStore *store, const struct GNUNET_CONFIGURATION_Handle *config)
Initializes an EGO-store as fully empty.
struct GNUNET_MESSENGER_SrvHandle * add_service_handle(struct GNUNET_MESSENGER_Service *service, struct GNUNET_MQ_Handle *mq)
Creates and adds a new handle to a service using a given message queue.
struct GNUNET_MESSENGER_ListHandle * next
struct GNUNET_SCHEDULER_Task * shutdown
struct GNUNET_CADET_Handle * GNUNET_CADET_connect(const struct GNUNET_CONFIGURATION_Handle *cfg)
Connect to the MQ-based cadet service.
Definition: cadet_api.c:910
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.
struct GNUNET_MESSENGER_SrvRoom * get_service_room(const struct GNUNET_MESSENGER_Service *service, const struct GNUNET_HashCode *key)
Returns the room identified by a given key for a service.
struct GNUNET_MESSENGER_EgoStore * get_service_ego_store(struct GNUNET_MESSENGER_Service *service)
Returns the used EGO-store of a given service.
, &#39; bother checking if a value already exists (faster than GNUNET_CONTAINER_MULTIHASHMAPOPTION_...
static char * value
Value of the record to add/remove.
struct GNUNET_CONTAINER_MultiHashMap * rooms
void GNUNET_CONTAINER_multihashmap_destroy(struct GNUNET_CONTAINER_MultiHashMap *map)
Destroy a hash map.
static const struct GNUNET_CONFIGURATION_Handle * config
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.
static struct GNUNET_DNS_Handle * handle
Handle to transport service.
enum GNUNET_GenericReturnValue GNUNET_DISK_directory_create(const char *dir)
Implementation of "mkdir -p".
Definition: disk.c:476
struct GNUNET_SERVICE_Handle * service
A 512-bit hashcode.
void handle_service_message(struct GNUNET_MESSENGER_Service *service, struct GNUNET_MESSENGER_SrvRoom *room, const struct GNUNET_MESSENGER_MemberSession *session, const struct GNUNET_MESSENGER_Message *message, const struct GNUNET_HashCode *hash)
Sends a received or sent message with a given hash to each handle of a service which is currently mem...
void notify_handle_message(struct GNUNET_MESSENGER_SrvHandle *handle, const struct GNUNET_HashCode *key, const struct GNUNET_MESSENGER_MemberSession *session, const struct GNUNET_MESSENGER_Message *message, const struct GNUNET_HashCode *hash)
Notifies the handle that a new message was received or sent.
void clear_list_handles(struct GNUNET_MESSENGER_ListHandles *handles)
Destroys remaining handles and clears the list.
struct GNUNET_TESTBED_Peer * peer
The peer associated with this model.
void init_list_handles(struct GNUNET_MESSENGER_ListHandles *handles)
Initializes list of handles as empty list.
struct GNUNET_HashCode key
The key used in the DHT.
struct GNUNET_MESSENGER_SrvHandle * host
void remove_service_handle(struct GNUNET_MESSENGER_Service *service, struct GNUNET_MESSENGER_SrvHandle *handle)
Removes a handle from a service and destroys it.
void destroy_handle(struct GNUNET_MESSENGER_SrvHandle *handle)
Destroys a handle and frees its memory fully.
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.
struct GNUNET_MESSENGER_Message * create_message_leave()
Creates and allocates a new leave message.
#define GNUNET_MESSENGER_SERVICE_NAME
Identifier of GNUnet MESSENGER Service.
struct GNUNET_MESSENGER_EgoStore ego_store
Handle to a message queue.
Definition: mq.c:85
const struct GNUNET_CONFIGURATION_Handle * config
struct GNUNET_MESSENGER_SrvRoom * create_room(struct GNUNET_MESSENGER_SrvHandle *handle, const struct GNUNET_HashCode *key)
Creates and allocates a new room for a handle with a given key.
int enter_room_at(struct GNUNET_MESSENGER_SrvRoom *room, struct GNUNET_MESSENGER_SrvHandle *handle, const struct GNUNET_PeerIdentity *door)
Connects a tunnel to a hosting peer of a room through a so called door which is represented by a peer...
The identity of the host (wraps the signing key of the peer).
struct GNUNET_MESSENGER_SrvHandle * create_handle(struct GNUNET_MESSENGER_Service *service, struct GNUNET_MQ_Handle *mq)
Creates and allocates a new handle related to a service and using a given mq (message queue)...
configuration data
Definition: configuration.c:84
struct GNUNET_MQ_Handle * mq
Definition: 003.c:5
static int iterate_destroy_rooms(void *cls, const struct GNUNET_HashCode *key, void *value)
struct GNUNET_CONTAINER_MultiHashMap * member_ids
struct GNUNET_CONTAINER_MultiHashMap * GNUNET_CONTAINER_multihashmap_create(unsigned int len, int do_not_copy_keys)
Create a multi hash map.
int GNUNET_CRYPTO_get_peer_identity(const struct GNUNET_CONFIGURATION_Handle *cfg, struct GNUNET_PeerIdentity *dst)
Retrieve the identity of the host&#39;s peer.
const struct GNUNET_HashCode * get_room_key(const struct GNUNET_MESSENGER_SrvRoom *room)
Returns the shared secret you need to access a room.
const struct GNUNET_ShortHashCode * get_handle_member_id(const struct GNUNET_MESSENGER_SrvHandle *handle, const struct GNUNET_HashCode *key)
Returns the member id of a given handle in a specific room.
struct GNUNET_MESSENGER_Service * create_service(const struct GNUNET_CONFIGURATION_Handle *config, struct GNUNET_SERVICE_Handle *service_handle)
Creates and allocates a new service using a given config and a GNUnet service handle.
void destroy_service(struct GNUNET_MESSENGER_Service *service)
Destroys a service and frees its memory fully.
struct GNUNET_MESSENGER_ListHandle * head
void clear_contact_store(struct GNUNET_MESSENGER_ContactStore *store)
Clears a contact store, wipes its content and deallocates its memory.
int GNUNET_CONTAINER_multihashmap_iterate(struct GNUNET_CONTAINER_MultiHashMap *map, GNUNET_CONTAINER_MulitHashMapIteratorCallback it, void *it_cls)
Iterate over all entries in the map.
enum GNUNET_GenericReturnValue GNUNET_DISK_directory_test(const char *fil, int is_readable)
Test if fil is a directory and listable.
Definition: disk.c:404
GNUnet MESSENGER service.
struct GNUNET_MESSENGER_SrvHandle * handle
#define GNUNET_free(ptr)
Wrapper around free.
void GNUNET_SERVICE_shutdown(struct GNUNET_SERVICE_Handle *sh)
Explicitly stops the service.
Definition: service.c:2384
void * GNUNET_SCHEDULER_cancel(struct GNUNET_SCHEDULER_Task *task)
Cancel the task with the specified identifier.
Definition: scheduler.c:972