GNUnet  0.19.3
gnunet-service-messenger_service.c
Go to the documentation of this file.
1 /*
2  This file is part of GNUnet.
3  Copyright (C) 2020--2022 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 "platform.h"
30 
31 static void
33 {
34  struct GNUNET_MESSENGER_Service *service = cls;
35 
36  if (service)
37  {
38  service->shutdown = NULL;
39 
41  }
42 }
43 
46  struct GNUNET_SERVICE_Handle *service_handle)
47 {
48  GNUNET_assert((config) && (service_handle));
49 
51 
52  service->config = config;
53  service->service = service_handle;
54 
56 
57  service->dir = NULL;
58 
61  "MESSENGER_DIR", &(service->dir)))
62  {
63  if (service->dir)
64  GNUNET_free(service->dir);
65 
66  service->dir = NULL;
67  }
68  else
69  {
72  {
73  GNUNET_free(service->dir);
74 
75  service->dir = NULL;
76  }
77  }
78 
79  service->cadet = GNUNET_CADET_connect (service->config);
80 
82 
83  init_list_handles (&(service->handles));
84 
86 
88 
89  return service;
90 }
91 
92 static int
94  const struct GNUNET_HashCode *key,
95  void *value)
96 {
97  struct GNUNET_MESSENGER_SrvRoom *room = value;
99  return GNUNET_YES;
100 }
101 
102 void
104 {
106 
107  if (service->shutdown)
108  {
109  GNUNET_SCHEDULER_cancel (service->shutdown);
110 
111  service->shutdown = NULL;
112  }
113 
115  clear_list_handles (&(service->handles));
116 
119 
121 
122  if (service->cadet)
123  {
125 
126  service->cadet = NULL;
127  }
128 
129  if (service->dir)
130  {
131  GNUNET_free(service->dir);
132 
133  service->dir = NULL;
134  }
135 
136  GNUNET_SERVICE_shutdown (service->service);
137 
139 }
140 
143 {
145 
146  return &(service->ego_store);
147 }
148 
151 {
153 
154  return &(service->contact_store);
155 }
156 
159  struct GNUNET_MQ_Handle *mq)
160 {
161  GNUNET_assert((service) && (mq));
162 
164 
165  if (handle)
166  {
167  add_list_handle (&(service->handles), handle);
168  }
169 
170  return handle;
171 }
172 
173 void
176 {
177  GNUNET_assert((service) && (handle));
178 
179  if (!handle)
180  return;
181 
182  if (GNUNET_YES == remove_list_handle (&(service->handles), handle))
184 }
185 
186 int
188  struct GNUNET_PeerIdentity *peer)
189 {
190  GNUNET_assert((service) && (peer));
191 
192  return GNUNET_CRYPTO_get_peer_identity (service->config, peer);
193 }
194 
197  const struct GNUNET_HashCode *key)
198 {
199  GNUNET_assert((service) && (key));
200 
202 }
203 
204 int
207  const struct GNUNET_HashCode *key)
208 {
209  GNUNET_assert((service) && (handle) && (key));
210 
212 
213  if (room)
214  return open_srv_room (room, handle);
215 
216  room = create_srv_room (handle, key);
217 
218  if ((GNUNET_YES == open_srv_room (room, handle)) &&
220  key, room,
222  return GNUNET_YES;
223 
225  return GNUNET_NO;
226 }
227 
228 int
231  const struct GNUNET_PeerIdentity *door,
232  const struct GNUNET_HashCode *key)
233 {
234  GNUNET_assert((service) && (handle) && (door) && (key));
235 
237 
238  if (room)
239  {
240  if (GNUNET_YES == enter_srv_room_at (room, handle, door))
241  return GNUNET_YES;
242  else
243  return GNUNET_NO;
244  }
245 
246  room = create_srv_room (handle, key);
247 
248  if ((GNUNET_YES == enter_srv_room_at (room, handle, door)) &&
250  key, room,
252  {
253  return GNUNET_YES;
254  }
255  else
256  {
258  return GNUNET_NO;
259  }
260 
261 }
262 
263 int
266  const struct GNUNET_HashCode *key)
267 {
268  GNUNET_assert((service) && (handle) && (key));
269 
271 
272  if (!room)
273  return GNUNET_NO;
274 
276 
278 
279  GNUNET_assert(id);
280 
281  if (GNUNET_YES != GNUNET_CONTAINER_multihashmap_remove (handle->member_ids, key, id))
282  return GNUNET_NO;
283 
285  &(service->handles), key);
286 
287  if (!member_handle)
288  {
290  {
292  return GNUNET_YES;
293  }
294  else
295  return GNUNET_NO;
296  }
297 
298  if (room->host == handle)
299  room->host = member_handle;
300 
301  return GNUNET_YES;
302 }
303 
304 void
306  struct GNUNET_MESSENGER_SrvRoom *room,
307  const struct GNUNET_MESSENGER_MemberSession *session,
308  const struct GNUNET_MESSENGER_Message *message,
309  const struct GNUNET_HashCode *hash)
310 {
311  GNUNET_assert((service) && (room) && (session) && (message) && (hash));
312 
313  struct GNUNET_MESSENGER_ListHandle *element = service->handles.head;
314 
315  while (element)
316  {
317  notify_srv_handle_message (element->handle, room, session, message, hash);
318  element = element->next;
319  }
320 }
struct GNUNET_MQ_Handle * mq
Definition: 003.c:5
struct GNUNET_HashCode key
The key used in the DHT.
static struct GNUNET_DNS_Handle * handle
Handle to transport service.
static char * value
Value of the record to add/remove.
static struct GNUNET_SERVICE_Handle * service
Handle to our service instance.
GNUnet MESSENGER service.
void clear_ego_store(struct GNUNET_MESSENGER_EgoStore *store)
Clears an EGO-store, wipes its content and deallocates its memory.
void init_ego_store(struct GNUNET_MESSENGER_EgoStore *store, const struct GNUNET_CONFIGURATION_Handle *config)
Initializes an EGO-store as fully empty.
void notify_srv_handle_message(struct GNUNET_MESSENGER_SrvHandle *handle, struct GNUNET_MESSENGER_SrvRoom *room, 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.
const struct GNUNET_ShortHashCode * get_srv_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.
void destroy_srv_handle(struct GNUNET_MESSENGER_SrvHandle *handle)
Destroys a handle and frees its memory fully.
struct GNUNET_MESSENGER_SrvHandle * create_srv_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).
void add_list_handle(struct GNUNET_MESSENGER_ListHandles *handles, struct GNUNET_MESSENGER_SrvHandle *handle)
Adds a specific handle to the end of the list.
void init_list_handles(struct GNUNET_MESSENGER_ListHandles *handles)
Initializes list of handles as empty list.
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...
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...
void clear_list_handles(struct GNUNET_MESSENGER_ListHandles *handles)
Destroys remaining handles and clears the list.
struct GNUNET_MESSENGER_Message * create_message_leave()
Creates and allocates a new leave message.
int send_srv_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.
int open_srv_room(struct GNUNET_MESSENGER_SrvRoom *room, struct GNUNET_MESSENGER_SrvHandle *handle)
Tries to open a room for a given handle.
int enter_srv_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...
struct GNUNET_MESSENGER_SrvRoom * create_srv_room(struct GNUNET_MESSENGER_SrvHandle *handle, const struct GNUNET_HashCode *key)
Creates and allocates a new room for a handle with a given key.
void destroy_srv_room(struct GNUNET_MESSENGER_SrvRoom *room, int deletion)
Destroys a room and frees its memory fully.
void remove_service_handle(struct GNUNET_MESSENGER_Service *service, struct GNUNET_MESSENGER_SrvHandle *handle)
Removes a handle from a service and destroys it.
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.
void destroy_service(struct GNUNET_MESSENGER_Service *service)
Destroys a service and frees its memory fully.
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.
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.
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...
struct GNUNET_MESSENGER_EgoStore * get_service_ego_store(struct GNUNET_MESSENGER_Service *service)
Returns the used EGO-store of a given service.
static void callback_shutdown_service(void *cls)
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.
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.
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.
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.
struct GNUNET_MESSENGER_ContactStore * get_service_contact_store(struct GNUNET_MESSENGER_Service *service)
Returns the used contact store of a given service.
static int iterate_destroy_rooms(void *cls, const struct GNUNET_HashCode *key, void *value)
GNUnet MESSENGER service.
static const struct GNUNET_CONFIGURATION_Handle * config
void GNUNET_CADET_disconnect(struct GNUNET_CADET_Handle *handle)
Disconnect from the cadet service.
Definition: cadet_api.c:774
struct GNUNET_CADET_Handle * GNUNET_CADET_connect(const struct GNUNET_CONFIGURATION_Handle *cfg)
Connect to the MQ-based cadet service.
Definition: cadet_api.c:894
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.
enum GNUNET_GenericReturnValue GNUNET_CRYPTO_get_peer_identity(const struct GNUNET_CONFIGURATION_Handle *cfg, struct GNUNET_PeerIdentity *dst)
Retrieve the identity of the host's peer.
enum GNUNET_GenericReturnValue GNUNET_DISK_directory_test(const char *fil, int is_readable)
Test if fil is a directory and listable.
Definition: disk.c:403
enum GNUNET_GenericReturnValue GNUNET_DISK_directory_create(const char *dir)
Implementation of "mkdir -p".
Definition: disk.c:495
int GNUNET_CONTAINER_multihashmap_iterate(struct GNUNET_CONTAINER_MultiHashMap *map, GNUNET_CONTAINER_MultiHashMapIteratorCallback it, void *it_cls)
Iterate over all entries in the map.
enum GNUNET_GenericReturnValue 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.
enum GNUNET_GenericReturnValue 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_CONTAINER_MultiHashMap * GNUNET_CONTAINER_multihashmap_create(unsigned int len, int do_not_copy_keys)
Create a multi hash 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...
@ GNUNET_OK
@ GNUNET_YES
@ GNUNET_NO
#define GNUNET_assert(cond)
Use this for fatal errors that cannot be handled.
#define GNUNET_new(type)
Allocate a struct or union of the given type.
#define GNUNET_free(ptr)
Wrapper around free.
#define GNUNET_MESSENGER_SERVICE_NAME
Identifier of GNUnet MESSENGER Service.
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,...
Definition: scheduler.c:1303
void * GNUNET_SCHEDULER_cancel(struct GNUNET_SCHEDULER_Task *task)
Cancel the task with the specified identifier.
Definition: scheduler.c:944
void GNUNET_SERVICE_shutdown(struct GNUNET_SERVICE_Handle *sh)
Explicitly stops the service.
Definition: service.c:2389
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.
A 512-bit hashcode.
struct GNUNET_MESSENGER_ListHandle * next
struct GNUNET_MESSENGER_SrvHandle * handle
struct GNUNET_MESSENGER_SrvHandle * host
Handle to a message queue.
Definition: mq.c:87
The identity of the host (wraps the signing key of the peer).
Handle to a service.
Definition: service.c:118
A 256-bit hashcode.
struct GNUNET_TESTBED_Peer * peer
The peer associated with this model.