libgnunetchat

library for GNUnet Messenger
Log | Files | Refs | README | LICENSE

commit 0643c99b65df3e2ca2bf63b9b664b07c84de4f39
parent eb314fb342dcdc6fd12438ef1ce98050f74a45ac
Author: Jacki <jacki@thejackimonster.de>
Date:   Thu, 18 Apr 2024 01:27:12 +0200

Add function to request file from chk uri

Signed-off-by: Jacki <jacki@thejackimonster.de>

Diffstat:
Minclude/gnunet/gnunet_chat_lib.h | 10++++++++++
Msrc/gnunet_chat_file.c | 50+++++++++++++++++++++++++++++++++++++++++++++-----
Msrc/gnunet_chat_file.h | 12++++++++++++
Msrc/gnunet_chat_lib.c | 27++++++++++++++++++++++++---
4 files changed, 91 insertions(+), 8 deletions(-)

diff --git a/include/gnunet/gnunet_chat_lib.h b/include/gnunet/gnunet_chat_lib.h @@ -685,6 +685,16 @@ void GNUNET_CHAT_lobby_join (struct GNUNET_CHAT_Handle *handle, const struct GNUNET_CHAT_Uri *uri); +/** + * Requests a file with a given chat <i>handle</i> from a selected chat + * <i>uri</i> to potentially download it. + * + * @param[in,out] handle Chat handle + * @param[in] uri Chat URI + */ +struct GNUNET_CHAT_File* +GNUNET_CHAT_request_file (struct GNUNET_CHAT_Handle *handle, + const struct GNUNET_CHAT_Uri *uri); /** * Sets a custom <i>user pointer</i> to a given chat <i>handle</i> so it can diff --git a/src/gnunet_chat_file.c b/src/gnunet_chat_file.c @@ -25,19 +25,17 @@ #include "gnunet_chat_file.h" #include "gnunet_chat_context.h" - -#include <limits.h> +#include <gnunet/gnunet_common.h> struct GNUNET_CHAT_File* file_create_from_message (struct GNUNET_CHAT_Handle *handle, const struct GNUNET_MESSENGER_MessageFile *message) { - GNUNET_assert((handle) && (message) && (message->name)); + GNUNET_assert((handle) && (message) && (message->uri)); struct GNUNET_CHAT_File* file = GNUNET_new(struct GNUNET_CHAT_File); file->handle = handle; - file->name = GNUNET_strndup(message->name, NAME_MAX); GNUNET_memcpy(&(file->key), &(message->key), sizeof(file->key)); @@ -68,6 +66,49 @@ file_create_from_message (struct GNUNET_CHAT_Handle *handle, } struct GNUNET_CHAT_File* +file_create_from_chk_uri (struct GNUNET_CHAT_Handle *handle, + const struct GNUNET_FS_Uri *uri) +{ + GNUNET_assert((handle) && (uri)); + + const struct GNUNET_HashCode *hash = GNUNET_FS_uri_chk_get_file_hash(uri); + + if (!hash) + return NULL; + + struct GNUNET_CHAT_File* file = GNUNET_new(struct GNUNET_CHAT_File); + + file->handle = handle; + file->name = NULL; + + memset(&(file->key), 0, sizeof(file->key)); + GNUNET_memcpy(&(file->hash), hash, sizeof(file->hash)); + + file->meta = GNUNET_FS_meta_data_create(); + + file->uri = GNUNET_FS_uri_dup(uri); + file->download = NULL; + file->publish = NULL; + file->unindex = NULL; + + file->upload_head = NULL; + file->upload_tail = NULL; + + file->download_head = NULL; + file->download_tail = NULL; + + file->unindex_head = NULL; + file->unindex_tail = NULL; + + file->status = 0; + file->preview = NULL; + + file->user_pointer = NULL; + + return file; +} + +struct GNUNET_CHAT_File* file_create_from_disk (struct GNUNET_CHAT_Handle *handle, const char *name, const struct GNUNET_HashCode *hash, @@ -78,7 +119,6 @@ file_create_from_disk (struct GNUNET_CHAT_Handle *handle, struct GNUNET_CHAT_File* file = GNUNET_new(struct GNUNET_CHAT_File); file->handle = handle; - file->name = GNUNET_strndup(name, NAME_MAX); GNUNET_memcpy(&(file->key), key, sizeof(file->key)); diff --git a/src/gnunet_chat_file.h b/src/gnunet_chat_file.h @@ -113,6 +113,18 @@ file_create_from_message (struct GNUNET_CHAT_Handle *handle, const struct GNUNET_MESSENGER_MessageFile *message); /** + * Creates a chat file handle from a FS CHK URI and + * with a selected chat <i>handle</i>. + * + * @param[in,out] handle Chat handle + * @param[in] uri FS CHK URI + * @return New chat file handle + */ +struct GNUNET_CHAT_File* +file_create_from_chk_uri (struct GNUNET_CHAT_Handle *handle, + const struct GNUNET_FS_Uri *uri); + +/** * Creates a chat file handle from a local file on disk * under a given <i>name</i> using a <i>hash</i> and a * selected symmetric <i>key</i> with a selected chat diff --git a/src/gnunet_chat_lib.c b/src/gnunet_chat_lib.c @@ -25,6 +25,7 @@ #include "gnunet_chat_lib.h" #include <gnunet/gnunet_common.h> +#include <gnunet/gnunet_fs_service.h> #include <gnunet/gnunet_messenger_service.h> #include <gnunet/gnunet_reclaim_lib.h> #include <gnunet/gnunet_reclaim_service.h> @@ -764,6 +765,23 @@ GNUNET_CHAT_lobby_join (struct GNUNET_CHAT_Handle *handle, } +struct GNUNET_CHAT_File* +GNUNET_CHAT_request_file (struct GNUNET_CHAT_Handle *handle, + const struct GNUNET_CHAT_Uri *uri) +{ + GNUNET_CHAT_VERSION_ASSERT(); + + if ((!handle) || (handle->destruction) || + (!uri) || (GNUNET_CHAT_URI_TYPE_FS != uri->type)) + return NULL; + + if (!GNUNET_FS_uri_test_chk(uri->fs.uri)) + return NULL; + + return file_create_from_chk_uri(handle, uri->fs.uri); +} + + void GNUNET_CHAT_set_user_pointer (struct GNUNET_CHAT_Handle *handle, void *user_pointer) @@ -1703,8 +1721,9 @@ GNUNET_CHAT_context_share_file (struct GNUNET_CHAT_Context *context, { GNUNET_CHAT_VERSION_ASSERT(); - if ((!context) || (!file) || (strlen(file->name) > NAME_MAX) || - (!(context->room))) + if ((!context) || (!file) || + (!(file->name)) || (strlen(file->name) > NAME_MAX) || + (!(file->uri)) || (!(context->room))) return GNUNET_SYSERR; struct GNUNET_MESSENGER_Message msg; @@ -2235,7 +2254,9 @@ GNUNET_CHAT_file_open_preview (struct GNUNET_CHAT_File *file) if (GNUNET_YES != GNUNET_DISK_file_test(filename)) goto free_filename; - file->preview = GNUNET_DISK_mktemp(file->name); + file->preview = GNUNET_DISK_mktemp( + file->name? file->name : "" + ); if (!(file->preview)) goto free_filename;