GNUnet debian-0.24.3-24-gfea921bd2
gnunet-service-messenger_operation.c
Go to the documentation of this file.
1/*
2 This file is part of GNUnet.
3 Copyright (C) 2021--2025 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
31#include "gnunet_common.h"
32
35{
37
39
40 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Create new operation: %s\n",
42
44
46 GNUNET_memcpy (&(op->hash), hash, sizeof(*hash));
47 op->timestamp = GNUNET_TIME_absolute_get_zero_ ();
48 op->store = NULL;
49 op->task = NULL;
50
51 return op;
52}
53
54
55void
57{
59
60 if (op->task)
62
63 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Free operation: %s\n",
64 GNUNET_h2s (&(op->hash)));
65
67}
68
69
70static void
71callback_operation (void *cls);
72
75 const char *path)
76{
79
80 GNUNET_assert ((store) && (path));
81
82 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Load operation configuration: %s\n",
83 path);
84
86
87 if (! cfg)
88 return NULL;
89
90 op = NULL;
91
93 goto destroy_config;
94
95 {
96 struct GNUNET_HashCode hash;
97
98 if (GNUNET_OK != GNUNET_CONFIGURATION_get_data (cfg, "operation", "hash",
99 &hash, sizeof(hash)))
100 goto destroy_config;
101
102 op = create_operation (&hash);
103 }
104
105 {
106 unsigned long long type_number;
108 "type", &type_number
109 ))
110 switch (type_number)
111 {
114 break;
117 break;
120 break;
121 default:
122 break;
123 }
124 }
125
126 if ((GNUNET_MESSENGER_OP_UNKNOWN == op->type) ||
128 "timestamp",
129 &(op->timestamp),
130 sizeof(op->timestamp))))
131 {
133 op = NULL;
134 goto destroy_config;
135 }
136
137 {
138 struct GNUNET_TIME_Relative delay;
139 delay = GNUNET_TIME_absolute_get_remaining (op->timestamp);
140
142 delay,
145 op);
146 }
147
148 op->store = store;
149
150destroy_config:
152
153 return op;
154}
155
156
157void
159 const char *path)
160{
162 char *hash_data;
163 char *timestamp_data;
164
165 GNUNET_assert ((path) && (op));
166
167 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Save operation configuration: %s\n",
168 path);
169
171
172 if (! cfg)
173 return;
174
175 hash_data = GNUNET_STRINGS_data_to_string_alloc (&(op->hash),
176 sizeof(op->hash));
177
178 if (hash_data)
179 {
180 GNUNET_CONFIGURATION_set_value_string (cfg, "operation", "hash", hash_data);
181
182 GNUNET_free (hash_data);
183 }
184
185 GNUNET_CONFIGURATION_set_value_number (cfg, "operation", "type", op->type);
186
187 timestamp_data = GNUNET_STRINGS_data_to_string_alloc (&(op->timestamp),
188 sizeof(op->timestamp));
189
190 if (timestamp_data)
191 {
192 GNUNET_CONFIGURATION_set_value_string (cfg, "operation", "timestamp",
193 timestamp_data);
194
195 GNUNET_free (timestamp_data);
196 }
197
200}
201
202
203extern void
205 const struct GNUNET_HashCode *hash);
206
207extern void
209 const struct GNUNET_HashCode *hash);
210
211static void
213{
216 struct GNUNET_MESSENGER_SrvRoom *room;
218 struct GNUNET_HashCode hash;
219
220 GNUNET_assert (cls);
221
222 op = cls;
223 op->task = NULL;
224
225 GNUNET_assert ((op->store) && (op->store->room));
226
227 store = op->store;
228 room = store->room;
229 type = op->type;
230
231 GNUNET_memcpy (&hash, &(op->hash), sizeof(hash));
232
233 cancel_store_operation (store, &hash);
234
235 if (GNUNET_is_zero (&hash))
236 return;
237
238 switch (type)
239 {
241 break;
243 {
245 &hash))
246 {
248 "Deletion of message failed! (%s)\n",
249 GNUNET_h2s (&hash));
250 break;
251 }
252
253 break;
254 }
256 {
257 struct GNUNET_MESSENGER_Message *message;
258
259 if (! room->host)
260 break;
261
262 message = create_message_merge (&hash);
263
264 if (! message)
265 {
267 "Merging operation failed: %s\n",
268 GNUNET_h2s (&(room->key)));
269 break;
270 }
271
272 send_srv_room_message (room, room->host, message);
273 break;
274 }
275 default:
276 break;
277 }
278}
279
280
285 struct GNUNET_TIME_Relative delay)
286{
288
289 GNUNET_assert ((op) && (store));
290
291 if (op->task)
292 return GNUNET_SYSERR;
293
295
297 delay,
300 op);
301
302 op->type = type;
303 op->timestamp = timestamp;
304 op->store = store;
305
306 return GNUNET_OK;
307}
308
309
312{
314
315 if (! op->task)
316 return GNUNET_SYSERR;
317
319 op->task = NULL;
320
322 op->timestamp = GNUNET_TIME_absolute_get_zero_ ();
323 op->store = NULL;
324
325 return GNUNET_OK;
326}
static struct GNUNET_CONFIGURATION_Handle * cfg
Our configuration.
Definition: gnunet-arm.c:108
static uint64_t timestamp(void)
Get current timestamp.
struct GNUNET_PILS_Operation * op
PILS op.
Definition: gnunet-hello.c:94
static uint32_t type
Type string converted to DNS type value.
struct GNUNET_MESSENGER_Message * create_message_merge(const struct GNUNET_HashCode *previous)
Creates and allocates a new merge message containing the hash of a second previous message besides th...
enum GNUNET_GenericReturnValue delete_store_message(struct GNUNET_MESSENGER_MessageStore *store, const struct GNUNET_HashCode *hash)
Deletes a message in the message store.
struct GNUNET_MESSENGER_Operation * create_operation(const struct GNUNET_HashCode *hash)
Creates and allocates a new operation under a given hash.
enum GNUNET_GenericReturnValue stop_operation(struct GNUNET_MESSENGER_Operation *op)
Stops an active operation and resets its type to be GNUNET_MESSENGER_OP_UNKNOWN.
struct GNUNET_MESSENGER_Operation * load_operation(struct GNUNET_MESSENGER_OperationStore *store, const char *path)
Loads data from a configuration file at a selected path into a new allocated and created operation fo...
enum GNUNET_GenericReturnValue start_operation(struct GNUNET_MESSENGER_Operation *op, enum GNUNET_MESSENGER_OperationType type, struct GNUNET_MESSENGER_OperationStore *store, struct GNUNET_TIME_Relative delay)
Starts an inactive operation with a given delay in a specific operation store.
void save_operation(const struct GNUNET_MESSENGER_Operation *op, const char *path)
Saves data from an operation into a configuration file at a selected path which can be load to restor...
static void callback_operation(void *cls)
void destroy_operation(struct GNUNET_MESSENGER_Operation *op)
Destroys an operation and frees its memory fully.
void callback_room_deletion(struct GNUNET_MESSENGER_SrvRoom *room, const struct GNUNET_HashCode *hash)
void callback_room_merge(struct GNUNET_MESSENGER_SrvRoom *room, const struct GNUNET_HashCode *hash)
void cancel_store_operation(struct GNUNET_MESSENGER_OperationStore *store, const struct GNUNET_HashCode *hash)
Stops any active operation under a given hash in a specific operation store.
struct GNUNET_MESSENGER_MessageStore * get_srv_room_message_store(struct GNUNET_MESSENGER_SrvRoom *room)
Returns the used message store of a given room.
enum GNUNET_GenericReturnValue 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.
commonly used definitions; globals in this file are exempt from the rule that the module name ("commo...
enum GNUNET_GenericReturnValue GNUNET_CONFIGURATION_get_data(const struct GNUNET_CONFIGURATION_Handle *cfg, const char *section, const char *option, void *buf, size_t buf_size)
Get Crockford32-encoded fixed-size binary data from a configuration.
struct GNUNET_CONFIGURATION_Handle * GNUNET_CONFIGURATION_create(const struct GNUNET_OS_ProjectData *pd)
Create a new configuration object.
void GNUNET_CONFIGURATION_set_value_string(struct GNUNET_CONFIGURATION_Handle *cfg, const char *section, const char *option, const char *value)
Set a configuration value that should be a string.
enum GNUNET_GenericReturnValue GNUNET_CONFIGURATION_get_value_number(const struct GNUNET_CONFIGURATION_Handle *cfg, const char *section, const char *option, unsigned long long *number)
Get a configuration value that should be a number.
void GNUNET_CONFIGURATION_destroy(struct GNUNET_CONFIGURATION_Handle *cfg)
Destroy configuration object.
void GNUNET_CONFIGURATION_set_value_number(struct GNUNET_CONFIGURATION_Handle *cfg, const char *section, const char *option, unsigned long long number)
Set a configuration value that should be a number.
enum GNUNET_GenericReturnValue GNUNET_CONFIGURATION_parse(struct GNUNET_CONFIGURATION_Handle *cfg, const char *filename)
Parse a configuration file, add all of the options in the file to the configuration environment.
enum GNUNET_GenericReturnValue GNUNET_CONFIGURATION_write(struct GNUNET_CONFIGURATION_Handle *cfg, const char *filename)
Write configuration file.
#define GNUNET_is_zero(a)
Check that memory in a is all zeros.
#define GNUNET_log(kind,...)
#define GNUNET_memcpy(dst, src, n)
Call memcpy() but check for n being 0 first.
GNUNET_GenericReturnValue
Named constants for return values.
@ GNUNET_SCHEDULER_PRIORITY_BACKGROUND
Run as background job (higher than idle, lower than default).
@ GNUNET_OK
@ GNUNET_SYSERR
#define GNUNET_assert(cond)
Use this for fatal errors that cannot be handled.
const char * GNUNET_h2s(const struct GNUNET_HashCode *hc)
Convert a hash value to a string (for printing debug messages).
@ GNUNET_ERROR_TYPE_WARNING
@ GNUNET_ERROR_TYPE_ERROR
@ GNUNET_ERROR_TYPE_DEBUG
#define GNUNET_new(type)
Allocate a struct or union of the given type.
#define GNUNET_free(ptr)
Wrapper around free.
const struct GNUNET_OS_ProjectData * GNUNET_OS_project_data_gnunet(void)
Return default project data used by 'libgnunetutil' for GNUnet.
void * GNUNET_SCHEDULER_cancel(struct GNUNET_SCHEDULER_Task *task)
Cancel the task with the specified identifier.
Definition: scheduler.c:980
struct GNUNET_SCHEDULER_Task * GNUNET_SCHEDULER_add_delayed_with_priority(struct GNUNET_TIME_Relative delay, enum GNUNET_SCHEDULER_Priority priority, GNUNET_SCHEDULER_TaskCallback task, void *task_cls)
Schedule a new task to be run with a specified delay.
Definition: scheduler.c:1207
char * GNUNET_STRINGS_data_to_string_alloc(const void *buf, size_t size)
Return the base32crockford encoding of the given buffer.
Definition: strings.c:807
struct GNUNET_TIME_Absolute GNUNET_TIME_absolute_get_zero_(void)
Return absolute time of 0ms.
Definition: time.c:142
struct GNUNET_TIME_Relative GNUNET_TIME_absolute_get_remaining(struct GNUNET_TIME_Absolute future)
Given a timestamp in the future, how much time remains until then?
Definition: time.c:406
struct GNUNET_TIME_Absolute GNUNET_TIME_absolute_get(void)
Get the current time.
Definition: time.c:111
struct GNUNET_TIME_Absolute GNUNET_TIME_absolute_add(struct GNUNET_TIME_Absolute start, struct GNUNET_TIME_Relative duration)
Add a given relative duration to the given start time.
Definition: time.c:452
A 512-bit hashcode.
struct GNUNET_MESSENGER_OperationStore * store
struct GNUNET_MESSENGER_SrvHandle * host
Time for absolute times used by GNUnet, in microseconds.
Time for relative time used by GNUnet, in microseconds.