GNUnet 0.22.1
messenger_api_handle.c
Go to the documentation of this file.
1/*
2 This file is part of GNUnet.
3 Copyright (C) 2020--2024 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_room.h"
29#include "messenger_api_util.h"
30
34 void *msg_cls)
35{
37
39
41
42 handle->cfg = cfg;
43 handle->mq = NULL;
44
45 handle->msg_callback = msg_callback;
46 handle->msg_cls = msg_cls;
47
48 handle->name = NULL;
49 handle->key = NULL;
50 handle->pubkey = NULL;
51
52 handle->reconnect_time = GNUNET_TIME_relative_get_zero_ ();
53 handle->reconnect_task = NULL;
54
56
58
59 return handle;
60}
61
62
65 const struct GNUNET_HashCode *key,
66 void *value)
67{
68 struct GNUNET_MESSENGER_Room *room;
69
71
72 room = value;
73
74 destroy_room (room);
75 return GNUNET_YES;
76}
77
78
79void
81{
83
85
86 if (handle->rooms)
87 {
89 NULL);
90
92 }
93
94 if (handle->reconnect_task)
95 GNUNET_SCHEDULER_cancel (handle->reconnect_task);
96
97 if (handle->mq)
99
100 if (handle->name)
101 GNUNET_free (handle->name);
102
103 if (handle->key)
104 GNUNET_free (handle->key);
105
106 if (handle->pubkey)
107 GNUNET_free (handle->pubkey);
108
110}
111
112
113void
115 const char *name)
116{
118
119 if (handle->name)
120 GNUNET_free (handle->name);
121
122 handle->name = name ? GNUNET_strdup (name) : NULL;
123}
124
125
126const char*
128{
130
131 return handle->name;
132}
133
134
135void
137 const struct GNUNET_CRYPTO_PrivateKey *key)
138{
140
141 if (! key)
142 {
143 if (handle->key)
144 GNUNET_free (handle->key);
145
146 if (handle->pubkey)
147 GNUNET_free (handle->pubkey);
148
149 handle->key = NULL;
150 handle->pubkey = NULL;
151 return;
152 }
153
154 if (! handle->key)
156
157 if (! handle->pubkey)
158 handle->pubkey = GNUNET_new (struct GNUNET_CRYPTO_PublicKey);
159
160 GNUNET_memcpy (handle->key, key, sizeof(*key));
162}
163
164
165const struct GNUNET_CRYPTO_PrivateKey*
167{
169
170 if (handle->key)
171 return handle->key;
172
174}
175
176
177const struct GNUNET_CRYPTO_PublicKey*
179{
181
182 if (handle->pubkey)
183 return handle->pubkey;
184
185 return get_anonymous_public_key ();
186}
187
188
191{
193
194 return &(handle->contact_store);
195}
196
197
200 const struct GNUNET_HashCode *key)
201{
202 struct GNUNET_MESSENGER_Room *room;
203 const struct GNUNET_ShortHashCode *contact_id;
204
205 GNUNET_assert ((handle) && (key));
206
208
209 if (! room)
210 return NULL;
211
212 contact_id = get_room_sender_id (room);
213
214 if (! contact_id)
215 return NULL;
216
217 {
219 get_context_from_member (key, contact_id, &context);
220
222 &context,
224 }
225}
226
227
228void
230 const struct GNUNET_HashCode *key)
231{
232 struct GNUNET_MESSENGER_Room *room;
233
234 GNUNET_assert ((handle) && (key));
235
237
238 if (room)
239 room->opened = GNUNET_YES;
240}
241
242
243void
245 const struct GNUNET_PeerIdentity *door,
246 const struct GNUNET_HashCode *key)
247{
248 struct GNUNET_MESSENGER_Room *room;
249
250 GNUNET_assert ((handle) && (door) && (key));
251
253
254 if (room)
255 add_to_list_tunnels (&(room->entries), door, NULL);
256}
257
258
259void
261 const struct GNUNET_HashCode *key)
262{
263 struct GNUNET_MESSENGER_Room *room;
264
265 GNUNET_assert ((handle) && (key));
266
268
270 handle->rooms, key, room)))
271 destroy_room (room);
272}
273
274
277 const struct GNUNET_HashCode *key)
278{
279 GNUNET_assert ((handle) && (key));
280
282}
static struct GNUNET_CONFIGURATION_Handle * cfg
Our configuration.
Definition: gnunet-arm.c:108
struct GNUNET_HashCode key
The key used in the DHT.
static pa_context * context
Pulseaudio context.
static char * name
Name (label) of the records to list.
static char * value
Value of the record to add/remove.
static struct GNUNET_VPN_Handle * handle
Handle to vpn service.
Definition: gnunet-vpn.c:35
int GNUNET_CONTAINER_multihashmap_iterate(struct GNUNET_CONTAINER_MultiHashMap *map, GNUNET_CONTAINER_MultiHashMapIteratorCallback it, void *it_cls)
Iterate over all entries in the 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.
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.
void GNUNET_CONTAINER_multihashmap_destroy(struct GNUNET_CONTAINER_MultiHashMap *map)
Destroy a hash map.
struct GNUNET_CONTAINER_MultiHashMap * GNUNET_CONTAINER_multihashmap_create(unsigned int len, int do_not_copy_keys)
Create a multi hash map.
enum GNUNET_GenericReturnValue GNUNET_CRYPTO_key_get_public(const struct GNUNET_CRYPTO_PrivateKey *privkey, struct GNUNET_CRYPTO_PublicKey *key)
Retrieves the public key representation of a private key.
Definition: crypto_pkey.c:430
#define GNUNET_memcpy(dst, src, n)
Call memcpy() but check for n being 0 first.
GNUNET_GenericReturnValue
Named constants for return values.
@ GNUNET_YES
@ GNUNET_NO
#define GNUNET_assert(cond)
Use this for fatal errors that cannot be handled.
#define GNUNET_strdup(a)
Wrapper around GNUNET_xstrdup_.
#define GNUNET_new(type)
Allocate a struct or union of the given type.
#define GNUNET_free(ptr)
Wrapper around free.
void(* GNUNET_MESSENGER_MessageCallback)(void *cls, struct GNUNET_MESSENGER_Room *room, const struct GNUNET_MESSENGER_Contact *sender, const struct GNUNET_MESSENGER_Contact *recipient, const struct GNUNET_MESSENGER_Message *message, const struct GNUNET_HashCode *hash, enum GNUNET_MESSENGER_MessageFlags flags)
Method called whenever a message is sent or received from a room.
void GNUNET_MQ_destroy(struct GNUNET_MQ_Handle *mq)
Destroy the message queue.
Definition: mq.c:700
void * GNUNET_SCHEDULER_cancel(struct GNUNET_SCHEDULER_Task *task)
Cancel the task with the specified identifier.
Definition: scheduler.c:979
struct GNUNET_TIME_Relative GNUNET_TIME_relative_get_zero_(void)
Return relative time of 0ms.
Definition: time.c:133
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.
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.
struct GNUNET_MESSENGER_Contact * get_store_contact(struct GNUNET_MESSENGER_ContactStore *store, const struct GNUNET_HashCode *context, const struct GNUNET_CRYPTO_PublicKey *pubkey)
Returns a contact using a specific public key.
void entry_handle_room_at(struct GNUNET_MESSENGER_Handle *handle, const struct GNUNET_PeerIdentity *door, const struct GNUNET_HashCode *key)
Adds a tunnel for a room known to a handle identified by a given key to a list of opened connections.
static enum GNUNET_GenericReturnValue iterate_destroy_room(void *cls, const struct GNUNET_HashCode *key, void *value)
struct GNUNET_MESSENGER_ContactStore * get_handle_contact_store(struct GNUNET_MESSENGER_Handle *handle)
Returns the used contact store of a given handle.
void open_handle_room(struct GNUNET_MESSENGER_Handle *handle, const struct GNUNET_HashCode *key)
Marks a room known to a handle identified by a given key as open.
struct GNUNET_MESSENGER_Handle * create_handle(const struct GNUNET_CONFIGURATION_Handle *cfg, GNUNET_MESSENGER_MessageCallback msg_callback, void *msg_cls)
Creates and allocates a new handle using a given configuration and a custom message callback with a g...
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.
const char * get_handle_name(const struct GNUNET_MESSENGER_Handle *handle)
Returns the current name of a given handle or NULL if no valid name was assigned yet.
void destroy_handle(struct GNUNET_MESSENGER_Handle *handle)
Destroys a handle and frees its memory fully from the client API.
struct GNUNET_MESSENGER_Room * get_handle_room(struct GNUNET_MESSENGER_Handle *handle, const struct GNUNET_HashCode *key)
Returns the room known to a handle identified by a given key.
void set_handle_key(struct GNUNET_MESSENGER_Handle *handle, const struct GNUNET_CRYPTO_PrivateKey *key)
Sets the keypair of a given handle to the keypair of a specific private key.
void close_handle_room(struct GNUNET_MESSENGER_Handle *handle, const struct GNUNET_HashCode *key)
Destroys and so implicitly closes a room known to a handle identified by a given key.
void set_handle_name(struct GNUNET_MESSENGER_Handle *handle, const char *name)
Sets the name of a handle to a specific name.
const struct GNUNET_CRYPTO_PrivateKey * get_handle_key(const struct GNUNET_MESSENGER_Handle *handle)
Returns the private key of a given handle.
const struct GNUNET_CRYPTO_PublicKey * get_handle_pubkey(const struct GNUNET_MESSENGER_Handle *handle)
Returns the public key of a given handle.
void add_to_list_tunnels(struct GNUNET_MESSENGER_ListTunnels *tunnels, const struct GNUNET_PeerIdentity *peer, const struct GNUNET_HashCode *hash)
Adds a specific peer from a tunnel to the end of the list.
const struct GNUNET_ShortHashCode * get_room_sender_id(const struct GNUNET_MESSENGER_Room *room)
Returns the member id of the room's sender.
void destroy_room(struct GNUNET_MESSENGER_Room *room)
Destroys a room and frees its memory fully from the client API.
const struct GNUNET_CRYPTO_PrivateKey * get_anonymous_private_key(void)
Returns the private identity key of GNUNET_IDENTITY_ego_get_anonymous() without recalculating it ever...
const struct GNUNET_CRYPTO_PublicKey * get_anonymous_public_key(void)
Returns the public identity key of GNUNET_IDENTITY_ego_get_anonymous() without recalculating it every...
A private key for an identity as per LSD0001.
An identity key as per LSD0001.
A 512-bit hashcode.
GNUNET_MESSENGER_MessageCallback msg_callback
enum GNUNET_GenericReturnValue opened
struct GNUNET_MESSENGER_ListTunnels entries
The identity of the host (wraps the signing key of the peer).
A 256-bit hashcode.
const struct GNUNET_CONFIGURATION_Handle * cfg
Configuration we use.
Definition: vpn_api.c:39
struct GNUNET_MQ_Handle * mq
Connection to VPN service.
Definition: vpn_api.c:44