GNUnet  0.11.x
Data Structures | Functions
gnunet-service-messenger_message_store.h File Reference

GNUnet MESSENGER service. More...

#include "platform.h"
#include "gnunet_container_lib.h"
#include "gnunet_disk_lib.h"
Include dependency graph for gnunet-service-messenger_message_store.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Data Structures

struct  GNUNET_MESSENGER_MessageEntry
 
struct  GNUNET_MESSENGER_MessageStore
 

Functions

void init_message_store (struct GNUNET_MESSENGER_MessageStore *store)
 Initializes a message store as fully empty. More...
 
void clear_message_store (struct GNUNET_MESSENGER_MessageStore *store)
 Clears a message store, wipes its content and deallocates its memory. More...
 
void load_message_store (struct GNUNET_MESSENGER_MessageStore *store, const char *directory)
 Loads messages from a directory into a message store. More...
 
void save_message_store (struct GNUNET_MESSENGER_MessageStore *store, const char *directory)
 Saves messages from a message store into a directory. More...
 
int contains_store_message (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...
 
int 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...
 

Detailed Description

GNUnet MESSENGER service.

Author
Tobias Frisch

Definition in file gnunet-service-messenger_message_store.h.

Function Documentation

◆ init_message_store()

void init_message_store ( struct GNUNET_MESSENGER_MessageStore store)

Initializes a message store as fully empty.

Parameters
storeMessage store

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

References GNUNET_MESSENGER_MessageStore::entries, GNUNET_CONTAINER_multihashmap_create(), GNUNET_NO, GNUNET_MESSENGER_MessageStore::messages, and GNUNET_MESSENGER_MessageStore::storage_messages.

Referenced by create_room().

31 {
32  store->storage_messages = NULL;
33 
36 }
struct GNUNET_DISK_FileHandle * storage_messages
struct GNUNET_CONTAINER_MultiHashMap * messages
struct GNUNET_CONTAINER_MultiHashMap * GNUNET_CONTAINER_multihashmap_create(unsigned int len, int do_not_copy_keys)
Create a multi hash map.
struct GNUNET_CONTAINER_MultiHashMap * entries
Here is the call graph for this function:
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
storeMessage store

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

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

Referenced by destroy_room().

60 {
61  if (store->storage_messages)
62  {
64 
65  store->storage_messages = NULL;
66  }
67 
70 
73 }
int GNUNET_DISK_file_close(struct GNUNET_DISK_FileHandle *h)
Close an open file.
Definition: disk.c:1280
struct GNUNET_DISK_FileHandle * storage_messages
struct GNUNET_CONTAINER_MultiHashMap * messages
void GNUNET_CONTAINER_multihashmap_destroy(struct GNUNET_CONTAINER_MultiHashMap *map)
Destroy a hash map.
static int iterate_destroy_messages(void *cls, const struct GNUNET_HashCode *key, void *value)
static int iterate_destroy_entries(void *cls, const struct GNUNET_HashCode *key, void *value)
int GNUNET_CONTAINER_multihashmap_iterate(struct GNUNET_CONTAINER_MultiHashMap *map, GNUNET_CONTAINER_MulitHashMapIteratorCallback it, void *it_cls)
Iterate over all entries in the map.
struct GNUNET_CONTAINER_MultiHashMap * entries
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
storeMessage store
directoryPath to a directory

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

References GNUNET_MESSENGER_MessageStore::entries, entries, GNUNET_MESSENGER_MessageEntryStorage::entry, filename, GNUNET_asprintf(), GNUNET_CONTAINER_multihashmap_put(), GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST, GNUNET_DISK_file_close(), GNUNET_DISK_file_open(), GNUNET_DISK_file_read(), GNUNET_DISK_file_test(), GNUNET_DISK_OPEN_READ, GNUNET_DISK_PERM_USER_READ, GNUNET_DISK_PERM_USER_WRITE, GNUNET_free, GNUNET_memcpy, GNUNET_new, GNUNET_OK, GNUNET_YES, GNUNET_MESSENGER_MessageEntryStorage::hash, and GNUNET_MESSENGER_MessageStore::storage_messages.

Referenced by load_service_room_and_messages().

83 {
85 
86  if (store->storage_messages)
88 
89  char *filename;
90  GNUNET_asprintf (&filename, "%s%s", directory, "messages.store");
91 
92  if (GNUNET_YES == GNUNET_DISK_file_test (filename))
93  store->storage_messages = GNUNET_DISK_file_open (filename, GNUNET_DISK_OPEN_READ, permission);
94  else
95  store->storage_messages = NULL;
96 
97  GNUNET_free(filename);
98 
99  if (!store->storage_messages)
100  return;
101 
102  GNUNET_asprintf (&filename, "%s%s", directory, "entries.store");
103 
104  if (GNUNET_YES != GNUNET_DISK_file_test (filename))
105  goto free_filename;
106 
108 
109  if (!entries)
110  goto free_filename;
111 
113  struct GNUNET_MESSENGER_MessageEntry *entry;
114 
115  do
116  {
118 
119  if (GNUNET_DISK_file_read (entries, &storage, sizeof(storage)) == sizeof(storage))
120  {
121  GNUNET_memcpy(entry, &(storage.entry), sizeof(*entry));
122 
123  if (GNUNET_OK != GNUNET_CONTAINER_multihashmap_put (store->entries, &(storage.hash), entry,
125  GNUNET_free(entry);
126  }
127  else
128  {
129  GNUNET_free(entry);
130 
131  entry = NULL;
132  }
133  }
134  while (entry);
135 
136  GNUNET_DISK_file_close (entries);
137 
138 free_filename:
139  GNUNET_free(filename);
140 }
int 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:544
Open the file for reading.
int GNUNET_DISK_file_close(struct GNUNET_DISK_FileHandle *h)
Close an open file.
Definition: disk.c:1280
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:732
struct GNUNET_DISK_FileHandle * storage_messages
#define GNUNET_memcpy(dst, src, n)
Call memcpy() but check for n being 0 first.
#define GNUNET_new(type)
Allocate a struct or union of the given type.
, ' bother checking if a value already exists (faster than GNUNET_CONTAINER_MULTIHASHMAPOPTION_...
struct ListEntry * entries
List of peers in the list.
static char * filename
GNUNET_DISK_AccessPermissions
File access permissions, UNIX-style.
int int GNUNET_asprintf(char **buf, const char *format,...) __attribute__((format(printf
Like asprintf, just portable.
int 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_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:1203
Handle used to access files (and pipes).
struct GNUNET_CONTAINER_MultiHashMap * entries
#define GNUNET_free(ptr)
Wrapper around free.
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
storeMessage store
directoryPath to a directory

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

References filename, GNUNET_asprintf(), 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_free, GNUNET_SYSERR, iterate_save_messages(), GNUNET_MESSENGER_MessageStore::messages, GNUNET_MESSENGER_MessageSave::storage_entries, GNUNET_MESSENGER_MessageStore::storage_messages, and GNUNET_MESSENGER_MessageSave::store.

Referenced by save_service_room_and_messages().

182 {
184 
186 
187  char *filename;
188  GNUNET_asprintf (&filename, "%s%s", directory, "entries.store");
189 
190  save.store = store;
191  save.storage_entries = GNUNET_DISK_file_open (filename, GNUNET_DISK_OPEN_WRITE | GNUNET_DISK_OPEN_CREATE, permission);
192 
193  GNUNET_free(filename);
194 
195  if (!save.storage_entries)
196  return;
197 
198  if (GNUNET_SYSERR == GNUNET_DISK_file_seek (save.storage_entries, 0, GNUNET_DISK_SEEK_END))
199  goto close_entries;
200 
201  if (store->storage_messages)
203 
204  GNUNET_asprintf (&filename, "%s%s", directory, "messages.store");
205 
207  permission);
208 
209  GNUNET_free(filename);
210 
211  if (store->storage_messages)
212  {
214 
216  GNUNET_DISK_file_sync (save.storage_entries);
217  }
218 
219 close_entries:
220  GNUNET_DISK_file_close (save.storage_entries);
221 }
int GNUNET_DISK_file_sync(const struct GNUNET_DISK_FileHandle *h)
Write file changes to disk.
Definition: disk.c:1433
Create file if it doesn't exist.
int GNUNET_DISK_file_close(struct GNUNET_DISK_FileHandle *h)
Close an open file.
Definition: disk.c:1280
struct GNUNET_DISK_FileHandle * storage_messages
struct GNUNET_CONTAINER_MultiHashMap * messages
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:226
static void save()
Write persistent statistics to disk.
static int iterate_save_messages(void *cls, const struct GNUNET_HashCode *key, void *value)
static char * filename
GNUNET_DISK_AccessPermissions
File access permissions, UNIX-style.
int int GNUNET_asprintf(char **buf, const char *format,...) __attribute__((format(printf
Like asprintf, just portable.
Seek an absolute position from the end of the file.
Open the file for writing.
Open the file for both reading and writing.
int GNUNET_CONTAINER_multihashmap_iterate(struct GNUNET_CONTAINER_MultiHashMap *map, GNUNET_CONTAINER_MulitHashMapIteratorCallback it, void *it_cls)
Iterate over all entries in the map.
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:1203
struct GNUNET_MESSENGER_MessageStore * store
#define GNUNET_free(ptr)
Wrapper around free.
Here is the call graph for this function:
Here is the caller graph for this function:

◆ contains_store_message()

int contains_store_message ( 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
storeMessage store
hashHash of message
Returns
GNUNET_YES on match, otherwise GNUNET_NO

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

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

Referenced by callback_verify_room_message().

225 {
227  return GNUNET_YES;
228 
229  return GNUNET_CONTAINER_multihashmap_contains (store->entries, hash);
230 }
struct GNUNET_CONTAINER_MultiHashMap * messages
int 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)...
struct GNUNET_CONTAINER_MultiHashMap * entries
Here is the call graph for this function:
Here is the caller 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
storeMessage store
hashHash of message
Returns
Message or NULL

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

References create_message(), decode_message(), destroy_message(), GNUNET_MESSENGER_MessageStore::entries, GNUNET_CONTAINER_multihashmap_get(), GNUNET_CONTAINER_multihashmap_put(), GNUNET_CONTAINER_multihashmap_remove(), GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST, GNUNET_DISK_file_read(), GNUNET_DISK_file_seek(), GNUNET_DISK_SEEK_SET, GNUNET_free, GNUNET_malloc, GNUNET_MESSENGER_KIND_UNKNOWN, GNUNET_OK, GNUNET_YES, GNUNET_MESSENGER_MessageEntry::length, GNUNET_MESSENGER_MessageStore::messages, GNUNET_MESSENGER_MessageEntry::offset, and GNUNET_MESSENGER_MessageStore::storage_messages.

Referenced by get_room_message().

234 {
236 
237  if (message)
238  return message;
239 
240  if (!store->storage_messages)
241  return NULL;
242 
243  const struct GNUNET_MESSENGER_MessageEntry *entry = GNUNET_CONTAINER_multihashmap_get (store->entries, hash);
244 
245  if (!entry)
246  return NULL;
247 
249  return message;
250 
251  char *buffer = GNUNET_malloc(entry->length);
252 
253  if (GNUNET_DISK_file_read (store->storage_messages, buffer, entry->length) != entry->length)
254  goto free_buffer;
255 
256 
258 
259  if ((GNUNET_YES != decode_message (message, entry->length, buffer)) || (GNUNET_OK
260  != GNUNET_CONTAINER_multihashmap_put (store->messages, hash, message,
262  {
263  destroy_message (message);
264 
265  message = NULL;
266 
267  GNUNET_CONTAINER_multihashmap_remove (store->entries, hash, entry);
268  }
269 
270 free_buffer:
271  GNUNET_free(buffer);
272 
273  return message;
274 }
int decode_message(struct GNUNET_MESSENGER_Message *message, uint16_t length, const char *buffer)
Decodes a message from a given buffer of a maximal length in bytes.
void destroy_message(struct GNUNET_MESSENGER_Message *message)
Destroys a message and frees its memory fully.
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:732
struct GNUNET_DISK_FileHandle * storage_messages
struct GNUNET_CONTAINER_MultiHashMap * messages
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:226
struct GNUNET_MESSENGER_Message * create_message(enum GNUNET_MESSENGER_MessageKind kind)
Creates and allocates a new message with a specific kind.
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.
, ' bother checking if a value already exists (faster than GNUNET_CONTAINER_MULTIHASHMAPOPTION_...
int 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.
int 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.
Seek an absolute position (from the start of the file).
#define GNUNET_malloc(size)
Wrapper around malloc.
struct GNUNET_CONTAINER_MultiHashMap * entries
#define GNUNET_free(ptr)
Wrapper around free.
Here is the call graph for this function:
Here is the caller graph for this function:

◆ put_store_message()

int 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
storeMessage store
hashHash of message
messageMessage
Returns
GNUNET_OK on success, otherwise GNUNET_NO

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

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

Referenced by callback_room_recv(), and callback_room_sent().

279 {
280  return GNUNET_CONTAINER_multihashmap_put (store->messages, hash, message,
282 }
struct GNUNET_CONTAINER_MultiHashMap * messages
, ' bother checking if a value already exists (faster than GNUNET_CONTAINER_MULTIHASHMAPOPTION_...
int 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.
Here is the call graph for this function:
Here is the caller graph for this function: