GNUnet 0.21.1
gnunet-service-messenger_message_store.c File Reference
Include dependency graph for gnunet-service-messenger_message_store.c:

Go to the source code of this file.

Data Structures

struct  GNUNET_MESSENGER_MessageEntryStorage
 
struct  GNUNET_MESSENGER_MessageLinkStorage
 
struct  GNUNET_MESSENGER_ClosureMessageSave
 

Macros

#define load_message_store_attribute_failed(file, attribute)
 
#define save_message_store_attribute_failed(file, attribute)
 

Functions

void init_message_store (struct GNUNET_MESSENGER_MessageStore *store)
 Initializes a message store as fully empty. More...
 
static enum GNUNET_GenericReturnValue iterate_destroy_entries (void *cls, const struct GNUNET_HashCode *key, void *value)
 
static enum GNUNET_GenericReturnValue iterate_destroy_messages (void *cls, const struct GNUNET_HashCode *key, void *value)
 
static enum GNUNET_GenericReturnValue iterate_destroy_links (void *cls, const struct GNUNET_HashCode *key, void *value)
 
void clear_message_store (struct GNUNET_MESSENGER_MessageStore *store)
 Clears a message store, wipes its content and deallocates its memory. More...
 
static void load_message_store_entries (struct GNUNET_MESSENGER_MessageStore *store, const char *filename)
 
static void load_message_store_links (struct GNUNET_MESSENGER_MessageStore *store, const char *filename)
 
void load_message_store (struct GNUNET_MESSENGER_MessageStore *store, const char *directory)
 Loads messages from a directory into a message store. More...
 
static enum GNUNET_GenericReturnValue iterate_save_entries (void *cls, const struct GNUNET_HashCode *key, void *value)
 
static enum GNUNET_GenericReturnValue iterate_save_messages (void *cls, const struct GNUNET_HashCode *key, void *value)
 
static enum GNUNET_GenericReturnValue iterate_save_links (void *cls, const struct GNUNET_HashCode *key, void *value)
 
void save_message_store (struct GNUNET_MESSENGER_MessageStore *store, const char *directory)
 Saves messages from a message store into a directory. More...
 
enum GNUNET_GenericReturnValue contains_store_message (const struct GNUNET_MESSENGER_MessageStore *store, const struct GNUNET_HashCode *hash)
 Checks if a message matching a given hash is stored in a message store. More...
 
const struct GNUNET_MESSENGER_Messageget_store_message (struct GNUNET_MESSENGER_MessageStore *store, const struct GNUNET_HashCode *hash)
 Returns the message from a message store matching a given hash. More...
 
const struct GNUNET_MESSENGER_MessageLinkget_store_message_link (struct GNUNET_MESSENGER_MessageStore *store, const struct GNUNET_HashCode *hash, enum GNUNET_GenericReturnValue deleted_only)
 Returns the message link from a message store matching a given hash. More...
 
enum GNUNET_GenericReturnValue put_store_message (struct GNUNET_MESSENGER_MessageStore *store, const struct GNUNET_HashCode *hash, struct GNUNET_MESSENGER_Message *message)
 Stores a message into the message store. More...
 
static void add_link (struct GNUNET_MESSENGER_MessageStore *store, const struct GNUNET_HashCode *hash, const struct GNUNET_MESSENGER_Message *message)
 
enum GNUNET_GenericReturnValue delete_store_message (struct GNUNET_MESSENGER_MessageStore *store, const struct GNUNET_HashCode *hash)
 Deletes a message in the message store. More...
 

Macro Definition Documentation

◆ load_message_store_attribute_failed

#define load_message_store_attribute_failed (   file,
  attribute 
)
Value:
sizeof(attribute) != GNUNET_DISK_file_read (file, &(attribute), \
sizeof(attribute))
ssize_t GNUNET_DISK_file_read(const struct GNUNET_DISK_FileHandle *h, void *result, size_t len)
Read the contents of a binary file into a buffer.
Definition: disk.c:622

Definition at line 115 of file gnunet-service-messenger_message_store.c.

◆ save_message_store_attribute_failed

#define save_message_store_attribute_failed (   file,
  attribute 
)
Value:
sizeof(attribute) != GNUNET_DISK_file_write (file, &(attribute), \
sizeof(attribute))
ssize_t GNUNET_DISK_file_write(const struct GNUNET_DISK_FileHandle *h, const void *buffer, size_t n)
Write a buffer to a file.
Definition: disk.c:686

Definition at line 119 of file gnunet-service-messenger_message_store.c.

Function Documentation

◆ init_message_store()

void init_message_store ( struct GNUNET_MESSENGER_MessageStore store)

Initializes a message store as fully empty.

Parameters
[out]storeMessage store

Definition at line 30 of file gnunet-service-messenger_message_store.c.

31{
32 GNUNET_assert (store);
33
34 store->storage_messages = NULL;
35
39
41 store->write_links = GNUNET_NO;
42}
struct GNUNET_CONTAINER_MultiHashMap * GNUNET_CONTAINER_multihashmap_create(unsigned int len, int do_not_copy_keys)
Create a multi hash map.
@ GNUNET_NO
#define GNUNET_assert(cond)
Use this for fatal errors that cannot be handled.
struct GNUNET_CONTAINER_MultiHashMap * links
struct GNUNET_DISK_FileHandle * storage_messages
struct GNUNET_CONTAINER_MultiHashMap * messages
struct GNUNET_CONTAINER_MultiHashMap * entries

References GNUNET_MESSENGER_MessageStore::entries, GNUNET_assert, GNUNET_CONTAINER_multihashmap_create(), GNUNET_NO, GNUNET_MESSENGER_MessageStore::links, GNUNET_MESSENGER_MessageStore::messages, GNUNET_MESSENGER_MessageStore::rewrite_entries, GNUNET_MESSENGER_MessageStore::storage_messages, and GNUNET_MESSENGER_MessageStore::write_links.

Referenced by create_srv_room().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ iterate_destroy_entries()

static enum GNUNET_GenericReturnValue iterate_destroy_entries ( void *  cls,
const struct GNUNET_HashCode key,
void *  value 
)
static

Definition at line 46 of file gnunet-service-messenger_message_store.c.

49{
51
52 GNUNET_free (entry);
53
54 return GNUNET_YES;
55}
static char * value
Value of the record to add/remove.
@ GNUNET_YES
#define GNUNET_free(ptr)
Wrapper around free.

References GNUNET_free, GNUNET_YES, and value.

Referenced by clear_message_store().

Here is the caller graph for this function:

◆ iterate_destroy_messages()

static enum GNUNET_GenericReturnValue iterate_destroy_messages ( void *  cls,
const struct GNUNET_HashCode key,
void *  value 
)
static

Definition at line 59 of file gnunet-service-messenger_message_store.c.

62{
63 struct GNUNET_MESSENGER_Message *message = value;
64
65 destroy_message (message);
66
67 return GNUNET_YES;
68}
void destroy_message(struct GNUNET_MESSENGER_Message *message)
Destroys a message and frees its memory fully.

References destroy_message(), GNUNET_YES, and value.

Referenced by clear_message_store().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ iterate_destroy_links()

static enum GNUNET_GenericReturnValue iterate_destroy_links ( void *  cls,
const struct GNUNET_HashCode key,
void *  value 
)
static

Definition at line 72 of file gnunet-service-messenger_message_store.c.

75{
76 struct GNUNET_HashCode *previous = value;
77
78 GNUNET_free (previous);
79
80 return GNUNET_YES;
81}
A 512-bit hashcode.

References GNUNET_free, GNUNET_YES, and value.

Referenced by clear_message_store().

Here is the caller graph for this function:

◆ clear_message_store()

void clear_message_store ( struct GNUNET_MESSENGER_MessageStore store)

Clears a message store, wipes its content and deallocates its memory.

Parameters
[in,out]storeMessage store

Definition at line 85 of file gnunet-service-messenger_message_store.c.

86{
87 GNUNET_assert (store);
88
89 if (store->storage_messages)
90 {
92
93 store->storage_messages = NULL;
94 }
95
101 NULL);
102
106}
static enum GNUNET_GenericReturnValue iterate_destroy_entries(void *cls, const struct GNUNET_HashCode *key, void *value)
static enum GNUNET_GenericReturnValue iterate_destroy_messages(void *cls, const struct GNUNET_HashCode *key, void *value)
static enum GNUNET_GenericReturnValue iterate_destroy_links(void *cls, const struct GNUNET_HashCode *key, void *value)
enum GNUNET_GenericReturnValue GNUNET_DISK_file_close(struct GNUNET_DISK_FileHandle *h)
Close an open file.
Definition: disk.c:1308
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_destroy(struct GNUNET_CONTAINER_MultiHashMap *map)
Destroy a hash map.

References GNUNET_MESSENGER_MessageStore::entries, GNUNET_assert, GNUNET_CONTAINER_multihashmap_destroy(), GNUNET_CONTAINER_multihashmap_iterate(), GNUNET_DISK_file_close(), iterate_destroy_entries(), iterate_destroy_links(), iterate_destroy_messages(), GNUNET_MESSENGER_MessageStore::links, GNUNET_MESSENGER_MessageStore::messages, and GNUNET_MESSENGER_MessageStore::storage_messages.

Referenced by destroy_srv_room().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ load_message_store_entries()

static void load_message_store_entries ( struct GNUNET_MESSENGER_MessageStore store,
const char *  filename 
)
static

Definition at line 124 of file gnunet-service-messenger_message_store.c.

126{
128
131 permission);
132
133 if (! entries)
134 return;
135
137 struct GNUNET_MESSENGER_MessageEntry *entry;
138
139 memset (&storage, 0, sizeof(storage));
140
141 do
142 {
143 entry = NULL;
144
145 if ((load_message_store_attribute_failed (entries, storage.hash)) ||
146 (load_message_store_attribute_failed (entries, storage.entry.offset)) ||
147 (load_message_store_attribute_failed (entries, storage.entry.length)))
148 break;
149
151
152 GNUNET_memcpy (entry, &(storage.entry), sizeof(*entry));
153
155 &(storage.hash)))
156 ||
158 &(storage.hash), entry,
160 {
162 break;
163 }
164 }
165 while (entry);
166
167 if (entry)
168 GNUNET_free (entry);
169
170 GNUNET_DISK_file_close (entries);
171}
static char * filename
#define load_message_store_attribute_failed(file, attribute)
struct GNUNET_DISK_FileHandle * GNUNET_DISK_file_open(const char *fn, enum GNUNET_DISK_OpenFlags flags, enum GNUNET_DISK_AccessPermissions perm)
Open a file.
Definition: disk.c:1237
GNUNET_DISK_AccessPermissions
File access permissions, UNIX-style.
@ GNUNET_DISK_OPEN_READ
Open the file for reading.
@ GNUNET_DISK_PERM_USER_READ
Owner can read.
enum GNUNET_GenericReturnValue GNUNET_CONTAINER_multihashmap_contains(const struct GNUNET_CONTAINER_MultiHashMap *map, const struct GNUNET_HashCode *key)
Check if the map contains any value under the given key (including values that are NULL).
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.
@ GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST
, ' bother checking if a value already exists (faster than GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE...
#define GNUNET_memcpy(dst, src, n)
Call memcpy() but check for n being 0 first.
@ GNUNET_OK
#define GNUNET_new(type)
Allocate a struct or union of the given type.
Handle used to access files (and pipes).

References GNUNET_MESSENGER_MessageStore::entries, GNUNET_MESSENGER_MessageEntryStorage::entry, filename, GNUNET_CONTAINER_multihashmap_contains(), GNUNET_CONTAINER_multihashmap_put(), GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST, GNUNET_DISK_file_close(), GNUNET_DISK_file_open(), GNUNET_DISK_OPEN_READ, GNUNET_DISK_PERM_USER_READ, GNUNET_free, GNUNET_memcpy, GNUNET_new, GNUNET_OK, GNUNET_YES, GNUNET_MESSENGER_MessageEntryStorage::hash, GNUNET_MESSENGER_MessageEntry::length, load_message_store_attribute_failed, GNUNET_MESSENGER_MessageEntry::offset, and GNUNET_MESSENGER_MessageStore::rewrite_entries.

Referenced by load_message_store().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ load_message_store_links()

static void load_message_store_links ( struct GNUNET_MESSENGER_MessageStore store,
const char *  filename 
)
static

Definition at line 181 of file gnunet-service-messenger_message_store.c.

183{
185
188 permission);
189
190 if (! entries)
191 return;
192
194 struct GNUNET_MESSENGER_MessageLink *link;
195
196 memset (&storage, 0, sizeof(storage));
197
198 do
199 {
200 link = NULL;
201
202 if ((load_message_store_attribute_failed (entries, storage.hash)) ||
204 storage.link.multiple)) ||
205 (load_message_store_attribute_failed (entries, storage.link.first)) ||
206 ((GNUNET_YES == storage.link.multiple) &&
207 (load_message_store_attribute_failed (entries, storage.link.second))))
208 break;
209
211
212 GNUNET_memcpy (link, &(storage.link), sizeof(*link));
213
215 &(storage.hash)))
216 ||
218 &(storage.hash), link,
220 break;
221 }
222 while (link);
223
224 if (link)
225 GNUNET_free (link);
226
227 GNUNET_DISK_file_close (entries);
228}

References filename, GNUNET_MESSENGER_MessageLink::first, GNUNET_CONTAINER_multihashmap_contains(), GNUNET_CONTAINER_multihashmap_put(), GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST, GNUNET_DISK_file_close(), GNUNET_DISK_file_open(), GNUNET_DISK_OPEN_READ, GNUNET_DISK_PERM_USER_READ, GNUNET_free, GNUNET_memcpy, GNUNET_new, GNUNET_OK, GNUNET_YES, GNUNET_MESSENGER_MessageLinkStorage::hash, GNUNET_MESSENGER_MessageLinkStorage::link, GNUNET_MESSENGER_MessageStore::links, load_message_store_attribute_failed, GNUNET_MESSENGER_MessageLink::multiple, and GNUNET_MESSENGER_MessageLink::second.

Referenced by load_message_store().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ load_message_store()

void load_message_store ( struct GNUNET_MESSENGER_MessageStore store,
const char *  directory 
)

Loads messages from a directory into a message store.

Parameters
[out]storeMessage store
[in]directoryPath to a directory

Definition at line 232 of file gnunet-service-messenger_message_store.c.

234{
235 GNUNET_assert ((store) && (directory));
236
239
240 if (store->storage_messages)
242
243 char *filename;
244 GNUNET_asprintf (&filename, "%s%s", directory, "messages.store");
245
249 permission);
250 else
251 store->storage_messages = NULL;
252
254
255 if (! store->storage_messages)
256 return;
257
258 GNUNET_asprintf (&filename, "%s%s", directory, "entries.store");
259
262
264
265 GNUNET_asprintf (&filename, "%s%s", directory, "links.store");
266
269
271}
static void load_message_store_entries(struct GNUNET_MESSENGER_MessageStore *store, const char *filename)
static void load_message_store_links(struct GNUNET_MESSENGER_MessageStore *store, const char *filename)
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
@ GNUNET_DISK_OPEN_READWRITE
Open the file for both reading and writing.
@ GNUNET_DISK_PERM_USER_WRITE
Owner can write.
int int GNUNET_asprintf(char **buf, const char *format,...) __attribute__((format(printf
Like asprintf, just portable.

References filename, GNUNET_asprintf(), GNUNET_assert, GNUNET_DISK_file_close(), GNUNET_DISK_file_open(), GNUNET_DISK_file_test(), GNUNET_DISK_OPEN_READWRITE, GNUNET_DISK_PERM_USER_READ, GNUNET_DISK_PERM_USER_WRITE, GNUNET_free, GNUNET_YES, load_message_store_entries(), load_message_store_links(), and GNUNET_MESSENGER_MessageStore::storage_messages.

Referenced by load_srv_room().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ iterate_save_entries()

static enum GNUNET_GenericReturnValue iterate_save_entries ( void *  cls,
const struct GNUNET_HashCode key,
void *  value 
)
static

Definition at line 282 of file gnunet-service-messenger_message_store.c.

285{
287 struct GNUNET_MESSENGER_MessageEntry *entry = value;
288
289 GNUNET_DISK_file_write (save->storage, key, sizeof(*key));
290 GNUNET_DISK_file_write (save->storage, &(entry->offset),
291 sizeof(entry->offset));
292 GNUNET_DISK_file_write (save->storage, &(entry->length),
293 sizeof(entry->length));
294
295 return GNUNET_YES;
296}
struct GNUNET_HashCode key
The key used in the DHT.
static void save()
Write persistent statistics to disk.

References GNUNET_DISK_file_write(), GNUNET_YES, key, GNUNET_MESSENGER_MessageEntry::length, GNUNET_MESSENGER_MessageEntry::offset, save(), and value.

Referenced by save_message_store().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ iterate_save_messages()

static enum GNUNET_GenericReturnValue iterate_save_messages ( void *  cls,
const struct GNUNET_HashCode key,
void *  value 
)
static

Definition at line 300 of file gnunet-service-messenger_message_store.c.

303{
305
307 key))
308 return GNUNET_YES;
309
310 struct GNUNET_MESSENGER_Message *message = value;
312
313 GNUNET_memcpy (&(storage.hash), key, sizeof(storage.hash));
314
315 storage.entry.length = get_message_size (message, GNUNET_YES);
316 storage.entry.offset = GNUNET_DISK_file_seek (save->store->storage_messages,
318
319 if ((GNUNET_SYSERR == storage.entry.offset) ||
320 (save_message_store_attribute_failed (save->storage, storage.hash)) ||
322 storage.entry.offset)) ||
324 storage.entry.length)))
325 return GNUNET_YES;
326
327 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Storing message with hash: %s\n",
328 GNUNET_h2s (&(storage.hash)));
329
330 char *buffer = GNUNET_malloc (storage.entry.length);
331
332 encode_message (message, storage.entry.length, buffer, GNUNET_YES);
333
334 GNUNET_DISK_file_write (save->store->storage_messages, buffer,
335 storage.entry.length);
336
337 GNUNET_free (buffer);
338 return GNUNET_YES;
339}
#define save_message_store_attribute_failed(file, attribute)
off_t GNUNET_DISK_file_seek(const struct GNUNET_DISK_FileHandle *h, off_t offset, enum GNUNET_DISK_Seek whence)
Move the read/write pointer in a file.
Definition: disk.c:205
@ GNUNET_DISK_SEEK_END
Seek an absolute position from the end of the file.
#define GNUNET_log(kind,...)
@ GNUNET_SYSERR
const char * GNUNET_h2s(const struct GNUNET_HashCode *hc)
Convert a hash value to a string (for printing debug messages).
@ GNUNET_ERROR_TYPE_DEBUG
#define GNUNET_malloc(size)
Wrapper around malloc.
void encode_message(const struct GNUNET_MESSENGER_Message *message, uint16_t length, char *buffer, enum GNUNET_GenericReturnValue include_header)
Encodes a given message into a buffer of a maximal length in bytes.
uint16_t get_message_size(const struct GNUNET_MESSENGER_Message *message, enum GNUNET_GenericReturnValue include_header)
Returns the exact size in bytes to encode a given message.

References encode_message(), GNUNET_MESSENGER_MessageEntryStorage::entry, get_message_size(), GNUNET_CONTAINER_multihashmap_contains(), GNUNET_DISK_file_seek(), GNUNET_DISK_file_write(), GNUNET_DISK_SEEK_END, GNUNET_ERROR_TYPE_DEBUG, GNUNET_free, GNUNET_h2s(), GNUNET_log, GNUNET_malloc, GNUNET_memcpy, GNUNET_NO, GNUNET_SYSERR, GNUNET_YES, GNUNET_MESSENGER_MessageEntryStorage::hash, key, GNUNET_MESSENGER_MessageEntry::length, GNUNET_MESSENGER_MessageEntry::offset, save(), save_message_store_attribute_failed, and value.

Referenced by save_message_store().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ iterate_save_links()

static enum GNUNET_GenericReturnValue iterate_save_links ( void *  cls,
const struct GNUNET_HashCode key,
void *  value 
)
static

Definition at line 343 of file gnunet-service-messenger_message_store.c.

346{
348 struct GNUNET_MESSENGER_MessageLink *link = value;
349
350 GNUNET_DISK_file_write (save->storage, key, sizeof(*key));
351 GNUNET_DISK_file_write (save->storage, &(link->multiple),
352 sizeof(link->multiple));
353 GNUNET_DISK_file_write (save->storage, &(link->first), sizeof(link->first));
354
355 if (GNUNET_YES == link->multiple)
356 GNUNET_DISK_file_write (save->storage, &(link->second),
357 sizeof(link->second));
358
359 return GNUNET_YES;
360}

References GNUNET_MESSENGER_MessageLink::first, GNUNET_DISK_file_write(), GNUNET_YES, key, GNUNET_MESSENGER_MessageLink::multiple, save(), GNUNET_MESSENGER_MessageLink::second, and value.

Referenced by save_message_store().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ save_message_store()

void save_message_store ( struct GNUNET_MESSENGER_MessageStore store,
const char *  directory 
)

Saves messages from a message store into a directory.

Parameters
[in]storeMessage store
[in]directoryPath to a directory

Definition at line 364 of file gnunet-service-messenger_message_store.c.

366{
367 GNUNET_assert ((store) && (directory));
368
370
373
374 char *filename;
375
377 goto save_entries;
378
379 GNUNET_asprintf (&filename, "%s%s", directory, "links.store");
380
381 save.store = store;
383 | GNUNET_DISK_OPEN_CREATE, permission);
384
386
387 if (! save.storage)
388 goto save_entries;
389
390 if (GNUNET_SYSERR == GNUNET_DISK_file_seek (save.storage, 0,
392 goto close_links;
393
395 &save);
397
398close_links:
400
401save_entries:
402 GNUNET_asprintf (&filename, "%s%s", directory, "entries.store");
403
404 save.store = store;
406 | GNUNET_DISK_OPEN_CREATE, permission);
407
409
410 if (! save.storage)
411 return;
412
414 {
415 if (GNUNET_SYSERR == GNUNET_DISK_file_seek (save.storage, 0,
417 goto close_entries;
418
420 &save);
422 }
423 else if (GNUNET_SYSERR == GNUNET_DISK_file_seek (save.storage, 0,
425 goto close_entries;
426
429
430 GNUNET_asprintf (&filename, "%s%s", directory, "messages.store");
431
435 permission);
436
438
440 {
443
445 GNUNET_DISK_file_sync (save.storage);
446 }
447
448close_entries:
450}
static enum GNUNET_GenericReturnValue iterate_save_entries(void *cls, const struct GNUNET_HashCode *key, void *value)
static enum GNUNET_GenericReturnValue iterate_save_links(void *cls, const struct GNUNET_HashCode *key, void *value)
static enum GNUNET_GenericReturnValue iterate_save_messages(void *cls, const struct GNUNET_HashCode *key, void *value)
enum GNUNET_GenericReturnValue GNUNET_DISK_file_sync(const struct GNUNET_DISK_FileHandle *h)
Write file changes to disk.
Definition: disk.c:1427
@ GNUNET_DISK_OPEN_WRITE
Open the file for writing.
@ GNUNET_DISK_OPEN_CREATE
Create file if it doesn't exist.
@ GNUNET_DISK_SEEK_SET
Seek an absolute position (from the start of the file).

References GNUNET_MESSENGER_MessageStore::entries, filename, GNUNET_asprintf(), GNUNET_assert, GNUNET_CONTAINER_multihashmap_iterate(), GNUNET_DISK_file_close(), GNUNET_DISK_file_open(), GNUNET_DISK_file_seek(), GNUNET_DISK_file_sync(), GNUNET_DISK_OPEN_CREATE, GNUNET_DISK_OPEN_READWRITE, GNUNET_DISK_OPEN_WRITE, GNUNET_DISK_PERM_USER_READ, GNUNET_DISK_PERM_USER_WRITE, GNUNET_DISK_SEEK_END, GNUNET_DISK_SEEK_SET, GNUNET_free, GNUNET_NO, GNUNET_SYSERR, GNUNET_YES, iterate_save_entries(), iterate_save_links(), iterate_save_messages(), GNUNET_MESSENGER_MessageStore::links, GNUNET_MESSENGER_MessageStore::messages, GNUNET_MESSENGER_MessageStore::rewrite_entries, save(), GNUNET_MESSENGER_MessageStore::storage_messages, GNUNET_MESSENGER_ClosureMessageSave::store, and GNUNET_MESSENGER_MessageStore::write_links.

Referenced by save_srv_room().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ contains_store_message()

enum GNUNET_GenericReturnValue contains_store_message ( const struct GNUNET_MESSENGER_MessageStore store,
const struct GNUNET_HashCode hash 
)

Checks if a message matching a given hash is stored in a message store.

The function returns GNUNET_YES if a match is found, GNUNET_NO otherwise.

The message has not to be loaded from disk into memory for this check!

Parameters
[in]storeMessage store
[in]hashHash of message
Returns
GNUNET_YES on match, otherwise GNUNET_NO

Definition at line 454 of file gnunet-service-messenger_message_store.c.

456{
457 GNUNET_assert ((store) && (hash));
458
460 hash))
461 return GNUNET_YES;
462
464}

References GNUNET_MESSENGER_MessageStore::entries, GNUNET_assert, GNUNET_CONTAINER_multihashmap_contains(), GNUNET_YES, GNUNET_MESSENGER_MessageStore::messages, and GNUNET_MESSENGER_ClosureMessageSave::store.

Here is the call graph for this function:

◆ get_store_message()

const struct GNUNET_MESSENGER_Message * get_store_message ( struct GNUNET_MESSENGER_MessageStore store,
const struct GNUNET_HashCode hash 
)

Returns the message from a message store matching a given hash.

If no matching message is found, NULL gets returned.

This function requires the message to be loaded into memory!

See also
contains_store_message()
Parameters
[in,out]storeMessage store
[in]hashHash of message
Returns
Message or NULL

Definition at line 468 of file gnunet-service-messenger_message_store.c.

470{
471 GNUNET_assert ((store) && (hash));
472
474 store->messages, hash);
475
476 if (message)
477 return message;
478
479 if (! store->storage_messages)
480 return NULL;
481
482 const struct GNUNET_MESSENGER_MessageEntry *entry =
484
485 if (! entry)
486 return NULL;
487
488 if (entry->offset != GNUNET_DISK_file_seek (store->storage_messages,
489 entry->offset,
491 return message;
492
493 char *buffer = GNUNET_malloc (entry->length);
494
495 if (! buffer)
496 return NULL;
497
498 if ((GNUNET_DISK_file_read (store->storage_messages, buffer, entry->length) !=
499 entry->length) ||
501 GNUNET_YES)))
502 goto free_buffer;
503
505
506 enum GNUNET_GenericReturnValue decoding;
507 decoding = decode_message (message, entry->length, buffer,
508 GNUNET_YES, NULL);
509
510 struct GNUNET_HashCode check;
511 hash_message (message, entry->length, buffer, &check);
512
513 if ((GNUNET_YES != decoding) || (GNUNET_CRYPTO_hash_cmp (hash, &check) != 0))
514 {
516 hash, entry))
518 "Corrupted entry could not be removed from store: %s\n",
519 GNUNET_h2s (hash));
520
522
523 goto free_message;
524 }
525
527 message,
529 goto free_buffer;
530
531 free_message : destroy_message (message);
532 message = NULL;
533
534free_buffer:
535 GNUNET_free (buffer);
536
537 return message;
538}
int GNUNET_CRYPTO_hash_cmp(const struct GNUNET_HashCode *h1, const struct GNUNET_HashCode *h2)
Compare function for HashCodes, producing a total ordering of all hashcodes.
Definition: crypto_hash.c:221
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.
GNUNET_GenericReturnValue
Named constants for return values.
@ GNUNET_ERROR_TYPE_WARNING
@ GNUNET_MESSENGER_KIND_UNKNOWN
The unknown kind.
void hash_message(const struct GNUNET_MESSENGER_Message *message, uint16_t length, const char *buffer, struct GNUNET_HashCode *hash)
Calculates a hash of a given buffer with a length in bytes from a message.
struct GNUNET_MESSENGER_Message * create_message(enum GNUNET_MESSENGER_MessageKind kind)
Creates and allocates a new message with a specific kind.
uint16_t get_message_kind_size(enum GNUNET_MESSENGER_MessageKind kind, enum GNUNET_GenericReturnValue include_header)
Returns the minimal size in bytes to encode a message of a specific kind.
enum GNUNET_GenericReturnValue decode_message(struct GNUNET_MESSENGER_Message *message, uint16_t length, const char *buffer, enum GNUNET_GenericReturnValue include_header, uint16_t *padding)
Decodes a message from a given buffer of a maximal length in bytes.

References create_message(), decode_message(), destroy_message(), GNUNET_MESSENGER_MessageStore::entries, get_message_kind_size(), GNUNET_assert, GNUNET_CONTAINER_multihashmap_get(), GNUNET_CONTAINER_multihashmap_put(), GNUNET_CONTAINER_multihashmap_remove(), GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST, GNUNET_CRYPTO_hash_cmp(), GNUNET_DISK_file_read(), GNUNET_DISK_file_seek(), GNUNET_DISK_SEEK_SET, GNUNET_ERROR_TYPE_WARNING, GNUNET_free, GNUNET_h2s(), GNUNET_log, GNUNET_malloc, GNUNET_MESSENGER_KIND_UNKNOWN, GNUNET_OK, GNUNET_YES, hash_message(), GNUNET_MESSENGER_MessageEntry::length, GNUNET_MESSENGER_MessageStore::messages, GNUNET_MESSENGER_MessageEntry::offset, GNUNET_MESSENGER_MessageStore::rewrite_entries, and GNUNET_MESSENGER_MessageStore::storage_messages.

Referenced by callback_verify_room_message(), check_srv_room_peer_status(), delete_srv_room_message(), delete_store_message(), forward_about_members(), get_store_message_link(), handle_room_messages(), notify_about_members(), recv_message_info(), request_room_message_step(), update_room_message(), and update_tunnel_last_message().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ get_store_message_link()

const struct GNUNET_MESSENGER_MessageLink * get_store_message_link ( struct GNUNET_MESSENGER_MessageStore store,
const struct GNUNET_HashCode hash,
enum GNUNET_GenericReturnValue  deleted_only 
)

Returns the message link from a message store matching a given hash.

If the flag is set to GNUNET_YES, only links from deleted messages will be returned or NULL.

Otherwise message links will also returned for messages found in the store under the given hash. The link which will be returned copies link information from the message for temporary usage.

Parameters
[in,out]storeMessage store
[in]hashHash of message
[in]deleted_onlyFlag
Returns
Message link or NULL

Definition at line 542 of file gnunet-service-messenger_message_store.c.

545{
546 if (deleted_only)
547 goto get_link;
548
549 const struct GNUNET_MESSENGER_Message *message = get_store_message (store,
550 hash);
551
552 if (! message)
553 goto get_link;
554
555 static struct GNUNET_MESSENGER_MessageLink link;
556
557 GNUNET_memcpy (&(link.first), &(message->header.previous),
558 sizeof(link.first));
559
560 link.multiple = GNUNET_MESSENGER_KIND_MERGE == message->header.kind?
562
563 if (GNUNET_YES == link.multiple)
564 GNUNET_memcpy (&(link.second), &(message->body.merge.previous),
565 sizeof(link.second));
566 else
567 GNUNET_memcpy (&(link.second), &(message->header.previous),
568 sizeof(link.second));
569
570 return &link;
571
572get_link:
573 return GNUNET_CONTAINER_multihashmap_get (store->links, hash);
574}
const struct GNUNET_MESSENGER_Message * get_store_message(struct GNUNET_MESSENGER_MessageStore *store, const struct GNUNET_HashCode *hash)
Returns the message from a message store matching a given hash.
@ GNUNET_MESSENGER_KIND_MERGE
The merge kind.
struct GNUNET_MESSENGER_MessageMerge merge
struct GNUNET_HashCode previous
The hash of the previous message from the senders perspective.
enum GNUNET_MESSENGER_MessageKind kind
The kind of the message.
struct GNUNET_HashCode previous
The hash of a second previous message.
struct GNUNET_MESSENGER_MessageHeader header
Header.
struct GNUNET_MESSENGER_MessageBody body
Body.

References GNUNET_MESSENGER_Message::body, GNUNET_MESSENGER_MessageLink::first, get_store_message(), GNUNET_CONTAINER_multihashmap_get(), GNUNET_memcpy, GNUNET_MESSENGER_KIND_MERGE, GNUNET_NO, GNUNET_YES, GNUNET_MESSENGER_Message::header, GNUNET_MESSENGER_MessageHeader::kind, GNUNET_MESSENGER_MessageStore::links, GNUNET_MESSENGER_MessageBody::merge, GNUNET_MESSENGER_MessageLink::multiple, GNUNET_MESSENGER_MessageHeader::previous, GNUNET_MESSENGER_MessageMerge::previous, and GNUNET_MESSENGER_MessageLink::second.

Referenced by check_member_session_completion(), and request_room_message_step().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ put_store_message()

enum GNUNET_GenericReturnValue put_store_message ( struct GNUNET_MESSENGER_MessageStore store,
const struct GNUNET_HashCode hash,
struct GNUNET_MESSENGER_Message message 
)

Stores a message into the message store.

The result indicates if the operation was successful.

Parameters
[in,out]storeMessage store
[in]hashHash of message
[in,out]messageMessage
Returns
GNUNET_OK on success, otherwise GNUNET_NO

Definition at line 578 of file gnunet-service-messenger_message_store.c.

581{
582 GNUNET_assert ((store) && (hash) && (message));
583
584 return GNUNET_CONTAINER_multihashmap_put (store->messages, hash, message,
586}

References GNUNET_assert, GNUNET_CONTAINER_multihashmap_put(), GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST, and GNUNET_MESSENGER_MessageStore::messages.

Referenced by update_room_message().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ add_link()

static void add_link ( struct GNUNET_MESSENGER_MessageStore store,
const struct GNUNET_HashCode hash,
const struct GNUNET_MESSENGER_Message message 
)
static

Definition at line 590 of file gnunet-service-messenger_message_store.c.

593{
594 struct GNUNET_MESSENGER_MessageLink *link = GNUNET_new (struct
596
597 GNUNET_memcpy (&(link->first), &(message->header.previous),
598 sizeof(link->first));
599
602
603 if (GNUNET_YES == link->multiple)
604 GNUNET_memcpy (&(link->second), &(message->body.merge.previous),
605 sizeof(link->second));
606 else
607 GNUNET_memcpy (&(link->second), &(message->header.previous),
608 sizeof(link->second));
609
610 if (GNUNET_OK != GNUNET_CONTAINER_multihashmap_put (store->links, hash, link,
612 GNUNET_free (link);
613 else
614 store->write_links = GNUNET_YES;
615}

References GNUNET_MESSENGER_Message::body, GNUNET_MESSENGER_MessageLink::first, GNUNET_CONTAINER_multihashmap_put(), GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST, GNUNET_free, GNUNET_memcpy, GNUNET_MESSENGER_KIND_MERGE, GNUNET_new, GNUNET_NO, GNUNET_OK, GNUNET_YES, GNUNET_MESSENGER_Message::header, GNUNET_MESSENGER_MessageHeader::kind, GNUNET_MESSENGER_MessageStore::links, GNUNET_MESSENGER_MessageBody::merge, GNUNET_MESSENGER_MessageLink::multiple, GNUNET_MESSENGER_MessageHeader::previous, GNUNET_MESSENGER_MessageMerge::previous, GNUNET_MESSENGER_MessageLink::second, and GNUNET_MESSENGER_MessageStore::write_links.

Referenced by delete_store_message().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ delete_store_message()

enum GNUNET_GenericReturnValue delete_store_message ( struct GNUNET_MESSENGER_MessageStore store,
const struct GNUNET_HashCode hash 
)

Deletes a message in the message store.

It will be removed from disk space and memory. The result indicates if the operation was successful.

Parameters
[in,out]storeMessage store
[in]hashHash of message
Returns
GNUNET_OK on success, GNUNET_SYSERR on failure

Definition at line 619 of file gnunet-service-messenger_message_store.c.

621{
622 GNUNET_assert ((store) && (hash));
623
624 const struct GNUNET_MESSENGER_MessageEntry *entry =
626
627 if (! entry)
628 goto clear_memory;
629
630 const struct GNUNET_MESSENGER_Message *message = get_store_message (store,
631 hash);
632
633 if (message)
634 add_link (store, hash, message);
635
636 if (! store->storage_messages)
637 goto clear_entry;
638
639 if (entry->offset != GNUNET_DISK_file_seek (store->storage_messages,
640 entry->offset,
642 return GNUNET_SYSERR;
643
644 char *clear_buffer = GNUNET_malloc (entry->length);
645
646 if (! clear_buffer)
647 return GNUNET_SYSERR;
648
649 GNUNET_CRYPTO_zero_keys (clear_buffer, entry->length);
650
651 if ((entry->length != GNUNET_DISK_file_write (store->storage_messages,
652 clear_buffer, entry->length)) ||
653 (GNUNET_OK
654 !=
656 store->storage_messages)))
657 {
658 GNUNET_free (clear_buffer);
659 return GNUNET_SYSERR;
660 }
661
662 GNUNET_free (clear_buffer);
663
664clear_entry:
666 entry))
668
669clear_memory:
671 return GNUNET_OK;
672}
static void add_link(struct GNUNET_MESSENGER_MessageStore *store, const struct GNUNET_HashCode *hash, const struct GNUNET_MESSENGER_Message *message)
void GNUNET_CRYPTO_zero_keys(void *buffer, size_t length)
Zero out buffer, securely against compiler optimizations.
int GNUNET_CONTAINER_multihashmap_remove_all(struct GNUNET_CONTAINER_MultiHashMap *map, const struct GNUNET_HashCode *key)
Remove all entries for the given key from the map.

References add_link(), GNUNET_MESSENGER_MessageStore::entries, get_store_message(), GNUNET_assert, GNUNET_CONTAINER_multihashmap_get(), GNUNET_CONTAINER_multihashmap_remove(), GNUNET_CONTAINER_multihashmap_remove_all(), GNUNET_CRYPTO_zero_keys(), GNUNET_DISK_file_seek(), GNUNET_DISK_file_sync(), GNUNET_DISK_file_write(), GNUNET_DISK_SEEK_SET, GNUNET_free, GNUNET_malloc, GNUNET_OK, GNUNET_SYSERR, GNUNET_YES, GNUNET_MESSENGER_MessageEntry::length, GNUNET_MESSENGER_MessageStore::messages, GNUNET_MESSENGER_MessageEntry::offset, GNUNET_MESSENGER_MessageStore::rewrite_entries, and GNUNET_MESSENGER_MessageStore::storage_messages.

Referenced by callback_room_deletion().

Here is the call graph for this function:
Here is the caller graph for this function: