GNUnet 0.21.1
gnunet-service-messenger_operation_store.c
Go to the documentation of this file.
1/*
2 This file is part of GNUnet.
3 Copyright (C) 2021--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 */
26#include "platform.h"
28
31
32void
34 struct GNUNET_MESSENGER_SrvRoom *room)
35{
36 GNUNET_assert ((store) && (room));
37
38 store->room = room;
40}
41
42
45 const struct GNUNET_HashCode *key,
46 void *value)
47{
49
51
52 return GNUNET_YES;
53}
54
55
56void
58{
60
61 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Clear operation store of room: %s\n",
63
67}
68
69
72 const char *filename)
73{
74 struct GNUNET_MESSENGER_OperationStore *store = cls;
75
77 return GNUNET_OK;
78
79 if ((strlen (filename) <= 4) || (0 != strcmp (filename + strlen (filename)
80 - 4, ".cfg")))
81 return GNUNET_OK;
82
84
87 &(op->hash), op,
89 {
91 }
92
93 return GNUNET_OK;
94}
95
96
97void
99 const char *directory)
100{
101 GNUNET_assert ((store) && (directory));
102
103 char *load_dir;
104 GNUNET_asprintf (&load_dir, "%s%s%c", directory, "operations", DIR_SEPARATOR);
105
107 "Load operation store from directory: %s\n",
108 load_dir);
109
112
113 GNUNET_free (load_dir);
114}
115
116
119 const struct GNUNET_HashCode *key,
120 void *value)
121{
122 const char *save_dir = cls;
123
125
126 if (! op)
127 return GNUNET_YES;
128
129 char *op_dir;
130 GNUNET_asprintf (&op_dir, "%s%s.cfg", save_dir, GNUNET_h2s (key));
131 save_operation (op, op_dir);
132
133 GNUNET_free (op_dir);
134 return GNUNET_YES;
135}
136
137
138void
140 const char *directory)
141{
142 GNUNET_assert ((store) && (directory));
143
144 char *save_dir;
145 GNUNET_asprintf (&save_dir, "%s%s%c", directory, "operations", DIR_SEPARATOR);
146
148 "Save operation store to directory: %s\n",
149 save_dir);
150
151 if ((GNUNET_YES == GNUNET_DISK_directory_test (save_dir, GNUNET_NO)) ||
154 iterate_save_operations, save_dir);
155
156 GNUNET_free (save_dir);
157}
158
159
162 const struct GNUNET_HashCode *hash)
163{
164 GNUNET_assert ((store) && (hash));
165
168
169 if (! op)
171
172 return op->type;
173}
174
175
178 const struct GNUNET_HashCode *hash,
180 struct GNUNET_TIME_Relative delay)
181{
182 GNUNET_assert ((store) && (hash));
183
186
187 if (op)
188 goto use_op;
189
191
193 op,
195 {
197
198 return GNUNET_SYSERR;
199 }
200
201use_op:
202 if ((op->type != GNUNET_MESSENGER_OP_UNKNOWN) &&
205
206 return start_operation (op, type, store, delay);
207}
208
209
210void
212 const struct GNUNET_HashCode *hash)
213{
214 GNUNET_assert ((store) && (hash));
215
218
219 if (! op)
220 return;
221
223
225 hash, op))
227 "Canceled operation could not be removed: %s\n",
228 GNUNET_h2s (hash));
229
231}
232
233
234extern void
236 const struct GNUNET_HashCode *hash);
237
238extern void
240 const struct GNUNET_HashCode *hash);
241
242void
245 const struct GNUNET_HashCode *hash)
246{
247 GNUNET_assert ((store) && (hash));
248
249 struct GNUNET_HashCode op_hash;
250 GNUNET_memcpy (&op_hash, hash, sizeof(op_hash));
251 cancel_store_operation (store, &op_hash);
252
253 struct GNUNET_MESSENGER_SrvRoom *room = store->room;
254
255 switch (type)
256 {
258 break;
260 callback_room_deletion (room, &op_hash);
261 break;
263 callback_room_merge (room, &op_hash);
264 break;
265 default:
266 break;
267 }
268}
static struct GNUNET_ARM_Operation * op
Current operation.
Definition: gnunet-arm.c:144
struct GNUNET_HashCode key
The key used in the DHT.
static char * filename
static char * value
Value of the record to add/remove.
static uint32_t type
Type string converted to DNS type value.
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...
void destroy_operation(struct GNUNET_MESSENGER_Operation *op)
Destroys an operation and frees its memory fully.
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.
void save_operation_store(const struct GNUNET_MESSENGER_OperationStore *store, const char *directory)
Saves operations from an operation store into a directory.
void clear_operation_store(struct GNUNET_MESSENGER_OperationStore *store)
Clears an operation store, stops all operations and deallocates its memory.
void callback_store_operation(struct GNUNET_MESSENGER_OperationStore *store, enum GNUNET_MESSENGER_OperationType type, const struct GNUNET_HashCode *hash)
static enum GNUNET_GenericReturnValue callback_scan_for_operations(void *cls, const char *filename)
void load_operation_store(struct GNUNET_MESSENGER_OperationStore *store, const char *directory)
Loads operations from a directory into an operation store.
enum GNUNET_MESSENGER_OperationType get_store_operation_type(const struct GNUNET_MESSENGER_OperationStore *store, const struct GNUNET_HashCode *hash)
Returns the type of the active operation under a given hash in a specific operation store.
void init_operation_store(struct GNUNET_MESSENGER_OperationStore *store, struct GNUNET_MESSENGER_SrvRoom *room)
Initializes an operation store as fully empty with a given room.
static enum GNUNET_GenericReturnValue iterate_destroy_operations(void *cls, const struct GNUNET_HashCode *key, void *value)
static enum GNUNET_GenericReturnValue iterate_save_operations(void *cls, const struct GNUNET_HashCode *key, void *value)
enum GNUNET_GenericReturnValue use_store_operation(struct GNUNET_MESSENGER_OperationStore *store, const struct GNUNET_HashCode *hash, enum GNUNET_MESSENGER_OperationType type, struct GNUNET_TIME_Relative delay)
Tries to use an operation under a given hash in a specific operation store.
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)
const struct GNUNET_HashCode * get_srv_room_key(const struct GNUNET_MESSENGER_SrvRoom *room)
Returns the shared secret you need to access a room.
enum GNUNET_GenericReturnValue GNUNET_DISK_file_test(const char *fil)
Check that fil corresponds to a filename (of a file that exists and that is not a directory).
Definition: disk.c:482
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:496
int GNUNET_DISK_directory_scan(const char *dir_name, GNUNET_FileNameCallback callback, void *callback_cls)
Scan a directory for files.
Definition: disk.c:814
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.
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.
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.
@ GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST
, ' bother checking if a value already exists (faster than GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE...
#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_OK
@ GNUNET_YES
@ GNUNET_NO
@ 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_DEBUG
int int GNUNET_asprintf(char **buf, const char *format,...) __attribute__((format(printf
Like asprintf, just portable.
#define GNUNET_free(ptr)
Wrapper around free.
#define DIR_SEPARATOR
Definition: platform.h:165
A 512-bit hashcode.
struct GNUNET_CONTAINER_MultiHashMap * operations
struct GNUNET_MESSENGER_OperationStore * store
Time for relative time used by GNUnet, in microseconds.