libgnunetchat

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

commit e6062f4a30a55053011e67c4ef1733f9ab650bec
parent 10bdc736a489383d5cee691f60ad5b1d19f8c39f
Author: Jacki <jacki@thejackimonster.de>
Date:   Sun, 28 Apr 2024 19:54:22 +0200

Separate internal process memory management into own source files

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

Diffstat:
Msrc/gnunet_chat_handle.c | 46++--------------------------------------------
Msrc/gnunet_chat_handle.h | 44++------------------------------------------
Msrc/gnunet_chat_lib.c | 137++++++++++---------------------------------------------------------------------
Msrc/gnunet_chat_lib_intern.c | 232+++++++++++++++----------------------------------------------------------------
Msrc/gnunet_chat_ticket.c | 6+++---
Asrc/internal/gnunet_chat_attribute_process.c | 179+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Asrc/internal/gnunet_chat_attribute_process.h | 87+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Asrc/internal/gnunet_chat_ticket_process.c | 150+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Asrc/internal/gnunet_chat_ticket_process.h | 71+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Msrc/internal/meson.build | 4+++-
10 files changed, 557 insertions(+), 399 deletions(-)

diff --git a/src/gnunet_chat_handle.c b/src/gnunet_chat_handle.c @@ -380,53 +380,11 @@ handle_disconnect (struct GNUNET_CHAT_Handle *handle) NULL ); - struct GNUNET_CHAT_AttributeProcess *attributes; while (handle->attributes_head) - { - attributes = handle->attributes_head; - - if (attributes->attribute) - GNUNET_free(attributes->attribute); - if (attributes->name) - GNUNET_free(attributes->name); - - if (attributes->iter) - GNUNET_RECLAIM_get_attributes_stop(attributes->iter); - if (attributes->op) - GNUNET_RECLAIM_cancel(attributes->op); - - GNUNET_CONTAINER_DLL_remove( - handle->attributes_head, - handle->attributes_tail, - attributes - ); - - GNUNET_free(attributes); - } + internal_attributes_destroy(handle->attributes_head); - struct GNUNET_CHAT_TicketProcess *tickets; while (handle->tickets_head) - { - tickets = handle->tickets_head; - - if (tickets->ticket) - GNUNET_free(tickets->ticket); - if (tickets->name) - GNUNET_free(tickets->name); - - if (tickets->iter) - GNUNET_RECLAIM_ticket_iteration_stop(tickets->iter); - if (tickets->op) - GNUNET_RECLAIM_cancel(tickets->op); - - GNUNET_CONTAINER_DLL_remove( - handle->tickets_head, - handle->tickets_tail, - tickets - ); - - GNUNET_free(tickets); - } + internal_tickets_destroy(handle->tickets_head); GNUNET_CONTAINER_multihashmap_iterate( handle->groups, it_destroy_handle_groups, NULL diff --git a/src/gnunet_chat_handle.h b/src/gnunet_chat_handle.h @@ -44,6 +44,8 @@ #include "gnunet_chat_uri.h" #include "internal/gnunet_chat_accounts.h" +#include "internal/gnunet_chat_attribute_process.h" +#include "internal/gnunet_chat_ticket_process.h" struct GNUNET_CHAT_Handle; @@ -80,48 +82,6 @@ struct GNUNET_CHAT_UriLookups struct GNUNET_CHAT_UriLookups *prev; }; -struct GNUNET_CHAT_AttributeProcess -{ - const struct GNUNET_CHAT_Account *account; - - struct GNUNET_CHAT_Handle *handle; - struct GNUNET_CHAT_Contact *contact; - - struct GNUNET_RECLAIM_Attribute *attribute; - struct GNUNET_TIME_Relative expires; - char *name; - void *data; - - GNUNET_CHAT_AttributeCallback callback; - GNUNET_CHAT_AccountAttributeCallback account_callback; - - void *closure; - - struct GNUNET_RECLAIM_AttributeIterator *iter; - struct GNUNET_RECLAIM_Operation *op; - - struct GNUNET_CHAT_AttributeProcess *next; - struct GNUNET_CHAT_AttributeProcess *prev; -}; - -struct GNUNET_CHAT_TicketProcess -{ - struct GNUNET_CHAT_Handle *handle; - struct GNUNET_CHAT_Contact *contact; - - struct GNUNET_RECLAIM_Ticket *ticket; - char *name; - - GNUNET_CHAT_ContactAttributeCallback callback; - void *closure; - - struct GNUNET_RECLAIM_TicketIterator *iter; - struct GNUNET_RECLAIM_Operation *op; - - struct GNUNET_CHAT_TicketProcess *next; - struct GNUNET_CHAT_TicketProcess *prev; -}; - struct GNUNET_CHAT_Handle { const struct GNUNET_CONFIGURATION_Handle* cfg; diff --git a/src/gnunet_chat_lib.c b/src/gnunet_chat_lib.c @@ -296,30 +296,11 @@ GNUNET_CHAT_set_attribute (struct GNUNET_CHAT_Handle *handle, if ((!key) || (!name)) return; - struct GNUNET_CHAT_AttributeProcess *attributes = GNUNET_new( - struct GNUNET_CHAT_AttributeProcess - ); - - memset(attributes, 0, sizeof(struct GNUNET_CHAT_AttributeProcess)); + struct GNUNET_CHAT_AttributeProcess *attributes; + attributes = internal_attributes_create_store(handle, name, expires); - attributes->handle = handle; - attributes->name = GNUNET_strdup(name); - attributes->expires = expires; - attributes->attribute = GNUNET_RECLAIM_attribute_new( - name, - NULL, - GNUNET_RECLAIM_ATTRIBUTE_TYPE_NONE, - NULL, - 0 - ); - - if (!attributes->attribute) - { - GNUNET_free(attributes); + if (!attributes) return; - } - - attributes->data = NULL; if (value) { @@ -333,8 +314,7 @@ GNUNET_CHAT_set_attribute (struct GNUNET_CHAT_Handle *handle, if (GNUNET_OK != result) { - GNUNET_free(attributes->attribute); - GNUNET_free(attributes); + internal_attributes_destroy(attributes); return; } @@ -352,12 +332,6 @@ GNUNET_CHAT_set_attribute (struct GNUNET_CHAT_Handle *handle, cb_task_finish_iterate_attribute, attributes ); - - GNUNET_CONTAINER_DLL_insert_tail( - handle->attributes_head, - handle->attributes_tail, - attributes - ); } @@ -377,24 +351,12 @@ GNUNET_CHAT_delete_attribute (struct GNUNET_CHAT_Handle *handle, if ((!key) || (!name)) return; - struct GNUNET_CHAT_AttributeProcess *attributes = GNUNET_new( - struct GNUNET_CHAT_AttributeProcess - ); + struct GNUNET_CHAT_AttributeProcess *attributes; + attributes = internal_attributes_create(handle, name); if (!attributes) return; - memset(attributes, 0, sizeof(struct GNUNET_CHAT_AttributeProcess)); - - attributes->handle = handle; - attributes->name = GNUNET_strdup(name); - - if (!attributes->name) - { - GNUNET_free(attributes); - return; - } - attributes->iter = GNUNET_RECLAIM_get_attributes_start( handle->reclaim, key, @@ -405,12 +367,6 @@ GNUNET_CHAT_delete_attribute (struct GNUNET_CHAT_Handle *handle, cb_task_finish_iterate_attribute, attributes ); - - GNUNET_CONTAINER_DLL_insert_tail( - handle->attributes_head, - handle->attributes_tail, - attributes - ); } @@ -431,17 +387,12 @@ GNUNET_CHAT_get_attributes (struct GNUNET_CHAT_Handle *handle, if (!key) return; - struct GNUNET_CHAT_AttributeProcess *attributes = GNUNET_new( - struct GNUNET_CHAT_AttributeProcess - ); + struct GNUNET_CHAT_AttributeProcess *attributes; + attributes = internal_attributes_create(handle, NULL); if (!attributes) return; - memset(attributes, 0, sizeof(struct GNUNET_CHAT_AttributeProcess)); - - attributes->handle = handle; - attributes->callback = callback; attributes->closure = cls; @@ -455,12 +406,6 @@ GNUNET_CHAT_get_attributes (struct GNUNET_CHAT_Handle *handle, cb_task_finish_iterate_attribute, attributes ); - - GNUNET_CONTAINER_DLL_insert_tail( - handle->attributes_head, - handle->attributes_tail, - attributes - ); } @@ -485,19 +430,12 @@ GNUNET_CHAT_share_attribute_with (struct GNUNET_CHAT_Handle *handle, if ((!key) || (!pubkey) || (!name)) return; - struct GNUNET_CHAT_AttributeProcess *attributes = GNUNET_new( - struct GNUNET_CHAT_AttributeProcess - ); + struct GNUNET_CHAT_AttributeProcess *attributes; + attributes = internal_attributes_create_share(handle, contact, name); if (!attributes) return; - memset(attributes, 0, sizeof(struct GNUNET_CHAT_AttributeProcess)); - - attributes->handle = handle; - attributes->contact = contact; - attributes->name = GNUNET_strdup(name); - attributes->iter = GNUNET_RECLAIM_get_attributes_start( handle->reclaim, key, @@ -508,12 +446,6 @@ GNUNET_CHAT_share_attribute_with (struct GNUNET_CHAT_Handle *handle, cb_task_finish_iterate_attribute, attributes ); - - GNUNET_CONTAINER_DLL_insert_tail( - handle->attributes_head, - handle->attributes_tail, - attributes - ); } @@ -534,19 +466,12 @@ GNUNET_CHAT_unshare_attribute_from (struct GNUNET_CHAT_Handle *handle, if ((!key) || (!name)) return; - struct GNUNET_CHAT_TicketProcess *tickets = GNUNET_new( - struct GNUNET_CHAT_TicketProcess - ); + struct GNUNET_CHAT_TicketProcess *tickets; + tickets = internal_tickets_create(handle, contact, name); if (!tickets) return; - memset(tickets, 0, sizeof(struct GNUNET_CHAT_TicketProcess)); - - tickets->handle = handle; - tickets->contact = contact; - tickets->name = GNUNET_strdup(name); - tickets->iter = GNUNET_RECLAIM_ticket_iteration_start( handle->reclaim, key, @@ -557,12 +482,6 @@ GNUNET_CHAT_unshare_attribute_from (struct GNUNET_CHAT_Handle *handle, cb_task_finish_iterate_ticket, tickets ); - - GNUNET_CONTAINER_DLL_insert_tail( - handle->tickets_head, - handle->tickets_tail, - tickets - ); } @@ -584,18 +503,12 @@ GNUNET_CHAT_get_shared_attributes (struct GNUNET_CHAT_Handle *handle, if (!key) return; - struct GNUNET_CHAT_TicketProcess *tickets = GNUNET_new( - struct GNUNET_CHAT_TicketProcess - ); + struct GNUNET_CHAT_TicketProcess *tickets; + tickets = internal_tickets_create(handle, contact, NULL); if (!tickets) return; - memset(tickets, 0, sizeof(struct GNUNET_CHAT_TicketProcess)); - - tickets->handle = handle; - tickets->contact = contact; - tickets->callback = callback; tickets->closure = cls; @@ -609,12 +522,6 @@ GNUNET_CHAT_get_shared_attributes (struct GNUNET_CHAT_Handle *handle, cb_task_finish_iterate_ticket, tickets ); - - GNUNET_CONTAINER_DLL_insert_tail( - handle->tickets_head, - handle->tickets_tail, - tickets - ); } @@ -1001,18 +908,12 @@ GNUNET_CHAT_account_get_attributes (struct GNUNET_CHAT_Handle *handle, if (!key) return; - struct GNUNET_CHAT_AttributeProcess *attributes = GNUNET_new( - struct GNUNET_CHAT_AttributeProcess - ); + struct GNUNET_CHAT_AttributeProcess *attributes; + attributes = internal_attributes_create_request(handle, account); if (!attributes) return; - memset(attributes, 0, sizeof(struct GNUNET_CHAT_AttributeProcess)); - - attributes->handle = handle; - attributes->account = account; - attributes->account_callback = callback; attributes->closure = cls; @@ -1026,12 +927,6 @@ GNUNET_CHAT_account_get_attributes (struct GNUNET_CHAT_Handle *handle, cb_task_finish_iterate_attribute, attributes ); - - GNUNET_CONTAINER_DLL_insert_tail( - handle->attributes_head, - handle->attributes_tail, - attributes - ); } diff --git a/src/gnunet_chat_lib_intern.c b/src/gnunet_chat_lib_intern.c @@ -469,16 +469,7 @@ cont_update_attribute_with_status (void *cls, attribute_name ); - if (attributes->attribute) - GNUNET_free(attributes->attribute); - - GNUNET_CONTAINER_DLL_remove( - handle->attributes_head, - handle->attributes_tail, - attributes - ); - - GNUNET_free(attributes); + internal_attributes_destroy(attributes); } void @@ -490,6 +481,8 @@ cb_task_finish_iterate_attribute (void *cls) (struct GNUNET_CHAT_AttributeProcess*) cls ); + attributes->iter = NULL; + struct GNUNET_CHAT_Handle *handle = attributes->handle; const struct GNUNET_CRYPTO_PrivateKey *key; @@ -499,8 +492,6 @@ cb_task_finish_iterate_attribute (void *cls) else key = handle_get_key(handle); - attributes->iter = NULL; - if (attributes->name) GNUNET_free(attributes->name); @@ -525,16 +516,7 @@ cb_task_finish_iterate_attribute (void *cls) if (attributes->op) return; - if (attributes->attribute) - GNUNET_free(attributes->attribute); - - GNUNET_CONTAINER_DLL_remove( - handle->attributes_head, - handle->attributes_tail, - attributes - ); - - GNUNET_free(attributes); + internal_attributes_destroy(attributes); } void @@ -576,19 +558,13 @@ cb_store_attribute (void *cls, if (! attributes->name) { - if (attributes->iter) - GNUNET_RECLAIM_get_attributes_stop(attributes->iter); - - attributes->iter = NULL; + internal_attributes_stop_iter(attributes); return; } if (0 == strcmp(attribute->name, attributes->name)) { - if (attributes->iter) - GNUNET_RECLAIM_get_attributes_stop(attributes->iter); - - attributes->iter = NULL; + internal_attributes_stop_iter(attributes); if (attributes->attribute) { @@ -616,8 +592,7 @@ cb_store_attribute (void *cls, return; } - if (attributes->iter) - GNUNET_RECLAIM_get_attributes_next(attributes->iter); + internal_attributes_next_iter(attributes); } void @@ -633,10 +608,7 @@ cb_delete_attribute (void *cls, if (! attributes->name) { - if (attributes->iter) - GNUNET_RECLAIM_get_attributes_stop(attributes->iter); - - attributes->iter = NULL; + internal_attributes_stop_iter(attributes); return; } @@ -648,10 +620,8 @@ cb_delete_attribute (void *cls, if (0 == strcmp(attribute->name, attributes->name)) { - if (attributes->iter) - GNUNET_RECLAIM_get_attributes_stop(attributes->iter); + internal_attributes_stop_iter(attributes); - attributes->iter = NULL; attributes->op = GNUNET_RECLAIM_attribute_delete( handle->reclaim, key, @@ -665,8 +635,7 @@ cb_delete_attribute (void *cls, return; } - if (attributes->iter) - GNUNET_RECLAIM_get_attributes_next(attributes->iter); + internal_attributes_next_iter(attributes); } void @@ -701,17 +670,11 @@ cb_iterate_attribute (void *cls, if (value) GNUNET_free (value); - - if (! attributes->iter) - return; if (GNUNET_YES != result) - { - GNUNET_RECLAIM_get_attributes_stop(attributes->iter); - attributes->iter = NULL; - } + internal_attributes_stop_iter(attributes); else - GNUNET_RECLAIM_get_attributes_next(attributes->iter); + internal_attributes_next_iter(attributes); } void @@ -725,7 +688,8 @@ cb_issue_ticket (void *cls, (struct GNUNET_CHAT_AttributeProcess*) cls ); - struct GNUNET_CHAT_Handle *handle = attributes->handle; + attributes->op = NULL; + struct GNUNET_CHAT_Context *context = contact_find_context( attributes->contact, GNUNET_YES @@ -734,13 +698,7 @@ cb_issue_ticket (void *cls, if ((context) && (ticket)) GNUNET_MESSENGER_send_ticket(context->room, ticket); - GNUNET_CONTAINER_DLL_remove( - handle->attributes_head, - handle->attributes_tail, - attributes - ); - - GNUNET_free(attributes); + internal_attributes_destroy(attributes); } static struct GNUNET_RECLAIM_AttributeList* @@ -795,10 +753,7 @@ cb_share_attribute (void *cls, if (! attributes->name) { - if (attributes->iter) - GNUNET_RECLAIM_get_attributes_stop(attributes->iter); - - attributes->iter = NULL; + internal_attributes_stop_iter(attributes); return; } @@ -806,6 +761,8 @@ cb_share_attribute (void *cls, if (0 == strcmp(attribute->name, attributes->name)) { + internal_attributes_stop_iter(attributes); + GNUNET_free(attributes->name); attributes->name = NULL; @@ -817,12 +774,6 @@ cb_share_attribute (void *cls, attributes->contact ); - if (attributes->iter) - { - GNUNET_RECLAIM_get_attributes_stop(attributes->iter); - attributes->iter = NULL; - } - struct GNUNET_RECLAIM_AttributeList *attrs; attrs = attribute_list_from_attribute(attribute); @@ -842,27 +793,7 @@ cb_share_attribute (void *cls, return; } - if (attributes->iter) - GNUNET_RECLAIM_get_attributes_next(attributes->iter); -} - -static void -ticket_process_next (struct GNUNET_CHAT_TicketProcess *tickets) -{ - if (! tickets->iter) - return; - - GNUNET_RECLAIM_ticket_iteration_next (tickets->iter); -} - -static void -ticket_process_stop (struct GNUNET_CHAT_TicketProcess *tickets) -{ - if (! tickets->iter) - return; - - GNUNET_RECLAIM_ticket_iteration_stop (tickets->iter); - tickets->iter = NULL; + internal_attributes_next_iter(attributes); } void @@ -876,18 +807,7 @@ cb_task_finish_iterate_ticket (void *cls) tickets->iter = NULL; - struct GNUNET_CHAT_Handle *handle = tickets->handle; - - if (tickets->name) - GNUNET_free(tickets->name); - - GNUNET_CONTAINER_DLL_remove( - handle->tickets_head, - handle->tickets_tail, - tickets - ); - - GNUNET_free(tickets); + internal_tickets_destroy(tickets); } void @@ -921,6 +841,8 @@ cont_revoke_ticket (void *cls, (struct GNUNET_CHAT_TicketProcess*) cls ); + tickets->op = NULL; + struct GNUNET_CHAT_Handle *handle = tickets->handle; if (success == GNUNET_SYSERR) @@ -939,14 +861,8 @@ cont_revoke_ticket (void *cls, GNUNET_CHAT_FLAG_SHARE_ATTRIBUTES, NULL ); - - GNUNET_CONTAINER_DLL_remove( - handle->tickets_head, - handle->tickets_tail, - tickets - ); - GNUNET_free(tickets); + internal_tickets_destroy(tickets); } void @@ -994,13 +910,7 @@ cb_consume_ticket_check (void *cls, if (tickets->op) return; - GNUNET_CONTAINER_DLL_remove( - handle->tickets_head, - handle->tickets_tail, - tickets - ); - - GNUNET_free(tickets); + internal_tickets_destroy(tickets); return; } @@ -1035,7 +945,7 @@ cb_iterate_ticket_check (void *cls, if ((!pubkey) || (0 != GNUNET_memcmp(pubkey, &(ticket->audience)))) { - ticket_process_next (tickets); + internal_tickets_next_iter(tickets); return; } @@ -1045,55 +955,28 @@ cb_iterate_ticket_check (void *cls, if (!key) { - ticket_process_stop (tickets); + internal_tickets_stop_iter(tickets); return; } - struct GNUNET_CHAT_TicketProcess *tick = GNUNET_new( - struct GNUNET_CHAT_TicketProcess - ); + struct GNUNET_CHAT_TicketProcess *new_tickets; + new_tickets = internal_tickets_copy(tickets, ticket); - if (!tick) + if (!new_tickets) { - ticket_process_stop (tickets); + internal_tickets_stop_iter(tickets); return; } - memset(tick, 0, sizeof (struct GNUNET_CHAT_TicketProcess)); - - tick->handle = handle; - tick->contact = tickets->contact; - - if (tickets->name) - tick->name = GNUNET_strdup (tickets->name); - - tick->ticket = GNUNET_new (struct GNUNET_RECLAIM_Ticket); - - if (tick->ticket) - GNUNET_memcpy( - tick->ticket, - ticket, - sizeof (struct GNUNET_RECLAIM_Ticket) - ); - - tick->callback = tickets->callback; - tick->closure = tickets->closure; - - tick->op = GNUNET_RECLAIM_ticket_consume( + new_tickets->op = GNUNET_RECLAIM_ticket_consume( handle->reclaim, key, ticket, cb_consume_ticket_check, - tick + new_tickets ); - GNUNET_CONTAINER_DLL_insert_tail( - handle->tickets_head, - handle->tickets_tail, - tick - ); - - ticket_process_next (tickets); + internal_tickets_next_iter(tickets); } void @@ -1110,15 +993,9 @@ cb_consume_ticket (void *cls, if ((!identity) && (!attribute) && (!presentation)) { - struct GNUNET_CHAT_Handle *handle = tickets->handle; - - GNUNET_CONTAINER_DLL_remove( - handle->tickets_head, - handle->tickets_tail, - tickets - ); + tickets->op = NULL; - GNUNET_free(tickets); + internal_tickets_destroy(tickets); return; } @@ -1158,7 +1035,7 @@ cb_iterate_ticket (void *cls, if ((!pubkey) || (0 != GNUNET_memcmp(pubkey, &(ticket->audience)))) { - ticket_process_next (tickets); + internal_tickets_next_iter(tickets); return; } @@ -1168,47 +1045,26 @@ cb_iterate_ticket (void *cls, if (!key) { - ticket_process_stop (tickets); + internal_tickets_stop_iter(tickets); return; } - struct GNUNET_CHAT_TicketProcess *tick = GNUNET_new( - struct GNUNET_CHAT_TicketProcess - ); + struct GNUNET_CHAT_TicketProcess *new_tickets; + new_tickets = internal_tickets_copy(tickets, NULL); - if (!tick) + if (!new_tickets) { - ticket_process_stop (tickets); + internal_tickets_stop_iter(tickets); return; } - memset(tick, 0, sizeof (struct GNUNET_CHAT_TicketProcess)); - - tick->handle = handle; - tick->contact = tickets->contact; - - tick->callback = tickets->callback; - tick->closure = tickets->closure; - - tick->op = GNUNET_RECLAIM_ticket_consume( + new_tickets->op = GNUNET_RECLAIM_ticket_consume( handle->reclaim, key, ticket, cb_consume_ticket, - tick - ); - - if (! tick->op) - { - GNUNET_free(tick); - return; - } - - GNUNET_CONTAINER_DLL_insert_tail( - handle->tickets_head, - handle->tickets_tail, - tick + new_tickets ); - ticket_process_next (tickets); + internal_tickets_next_iter(tickets); } diff --git a/src/gnunet_chat_ticket.c b/src/gnunet_chat_ticket.c @@ -79,12 +79,12 @@ ticket_consume(struct GNUNET_CHAT_Ticket *ticket, if (!key) return; - if (ticket->op) - GNUNET_RECLAIM_cancel(ticket->op); - ticket->callback = callback; ticket->closure = cls; + if (ticket->op) + GNUNET_RECLAIM_cancel(ticket->op); + ticket->op = GNUNET_RECLAIM_ticket_consume( ticket->handle->reclaim, key, diff --git a/src/internal/gnunet_chat_attribute_process.c b/src/internal/gnunet_chat_attribute_process.c @@ -0,0 +1,179 @@ +/* + This file is part of GNUnet. + Copyright (C) 2024 GNUnet e.V. + + GNUnet is free software: you can redistribute it and/or modify it + under the terms of the GNU Affero General Public License as published + by the Free Software Foundation, either version 3 of the License, + or (at your option) any later version. + + GNUnet is distributed in the hope that it will be useful, but + WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Affero General Public License for more details. + + You should have received a copy of the GNU Affero General Public License + along with this program. If not, see <http://www.gnu.org/licenses/>. + + SPDX-License-Identifier: AGPL3.0-or-later + */ +/* + * @author Tobias Frisch + * @file gnunet_chat_attribute_process.c + */ + +#include "gnunet_chat_attribute_process.h" + +#include "../gnunet_chat_handle.h" + +#include <gnunet/gnunet_common.h> +#include <gnunet/gnunet_reclaim_service.h> +#include <string.h> + +struct GNUNET_CHAT_AttributeProcess* +internal_attributes_create(struct GNUNET_CHAT_Handle *handle, + const char *name) +{ + GNUNET_assert(handle); + + struct GNUNET_CHAT_AttributeProcess *attributes = GNUNET_new( + struct GNUNET_CHAT_AttributeProcess + ); + + if (!attributes) + return NULL; + + memset(attributes, 0, sizeof(*attributes)); + + attributes->handle = handle; + + if (!name) + goto skip_name; + + attributes->name = GNUNET_strdup(name); + + if (!(attributes->name)) + { + GNUNET_free(attributes); + return NULL; + } + +skip_name: + GNUNET_CONTAINER_DLL_insert_tail( + attributes->handle->attributes_head, + attributes->handle->attributes_tail, + attributes + ); + + return attributes; +} + +struct GNUNET_CHAT_AttributeProcess* +internal_attributes_create_store(struct GNUNET_CHAT_Handle *handle, + const char *name, + struct GNUNET_TIME_Relative expires) +{ + GNUNET_assert((handle) && (name)); + + struct GNUNET_CHAT_AttributeProcess *attributes; + attributes = internal_attributes_create(handle, name); + + if (!attributes) + return NULL; + + attributes->attribute = GNUNET_RECLAIM_attribute_new( + name, + NULL, + GNUNET_RECLAIM_ATTRIBUTE_TYPE_NONE, + NULL, + 0 + ); + + if (!(attributes->attribute)) + { + internal_attributes_destroy(attributes); + return NULL; + } + + attributes->expires = expires; + + return attributes; +} + +struct GNUNET_CHAT_AttributeProcess* +internal_attributes_create_share(struct GNUNET_CHAT_Handle *handle, + struct GNUNET_CHAT_Contact *contact, + const char *name) +{ + GNUNET_assert((handle) && (contact) && (name)); + + struct GNUNET_CHAT_AttributeProcess *attributes; + attributes = internal_attributes_create(handle, name); + + if (!attributes) + return NULL; + + attributes->contact = contact; + + return attributes; +} + +struct GNUNET_CHAT_AttributeProcess* +internal_attributes_create_request(struct GNUNET_CHAT_Handle *handle, + const struct GNUNET_CHAT_Account *account) +{ + GNUNET_assert((handle) && (account)); + + struct GNUNET_CHAT_AttributeProcess *attributes; + attributes = internal_attributes_create(handle, NULL); + + if (!attributes) + return NULL; + + attributes->account = account; + + return attributes; +} + +void +internal_attributes_destroy(struct GNUNET_CHAT_AttributeProcess *attributes) +{ + GNUNET_assert((attributes) && (attributes->handle)); + + GNUNET_CONTAINER_DLL_remove( + attributes->handle->attributes_head, + attributes->handle->attributes_tail, + attributes + ); + + if (attributes->attribute) + GNUNET_free(attributes->attribute); + if (attributes->name) + GNUNET_free(attributes->name); + if (attributes->data) + GNUNET_free(attributes->data); + + if (attributes->iter) + GNUNET_RECLAIM_get_attributes_stop(attributes->iter); + if (attributes->op) + GNUNET_RECLAIM_cancel(attributes->op); + + GNUNET_free(attributes); +} + +void +internal_attributes_next_iter(struct GNUNET_CHAT_AttributeProcess *attributes) +{ + GNUNET_assert((attributes) && (attributes->iter)); + + GNUNET_RECLAIM_get_attributes_next(attributes->iter); +} + +void +internal_attributes_stop_iter(struct GNUNET_CHAT_AttributeProcess *attributes) +{ + GNUNET_assert((attributes) && (attributes->iter)); + + GNUNET_RECLAIM_get_attributes_stop(attributes->iter); + attributes->iter = NULL; +} diff --git a/src/internal/gnunet_chat_attribute_process.h b/src/internal/gnunet_chat_attribute_process.h @@ -0,0 +1,87 @@ +/* + This file is part of GNUnet. + Copyright (C) 2024 GNUnet e.V. + + GNUnet is free software: you can redistribute it and/or modify it + under the terms of the GNU Affero General Public License as published + by the Free Software Foundation, either version 3 of the License, + or (at your option) any later version. + + GNUnet is distributed in the hope that it will be useful, but + WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Affero General Public License for more details. + + You should have received a copy of the GNU Affero General Public License + along with this program. If not, see <http://www.gnu.org/licenses/>. + + SPDX-License-Identifier: AGPL3.0-or-later + */ +/* + * @author Tobias Frisch + * @file gnunet_chat_attribute_process.h + */ + +#ifndef GNUNET_CHAT_INTERNAL_ATTRIBUTE_PROCESS_H_ +#define GNUNET_CHAT_INTERNAL_ATTRIBUTE_PROCESS_H_ + +#include <gnunet/gnunet_reclaim_service.h> + +#include "gnunet_chat_lib.h" + +struct GNUNET_CHAT_Handle; +struct GNUNET_CHAT_Account; +struct GNUNET_CHAT_Contact; + +struct GNUNET_CHAT_AttributeProcess +{ + struct GNUNET_CHAT_Handle *handle; + + const struct GNUNET_CHAT_Account *account; + struct GNUNET_CHAT_Contact *contact; + + struct GNUNET_RECLAIM_Attribute *attribute; + struct GNUNET_TIME_Relative expires; + char *name; + void *data; + + GNUNET_CHAT_AttributeCallback callback; + GNUNET_CHAT_AccountAttributeCallback account_callback; + + void *closure; + + struct GNUNET_RECLAIM_AttributeIterator *iter; + struct GNUNET_RECLAIM_Operation *op; + + struct GNUNET_CHAT_AttributeProcess *next; + struct GNUNET_CHAT_AttributeProcess *prev; +}; + +struct GNUNET_CHAT_AttributeProcess* +internal_attributes_create(struct GNUNET_CHAT_Handle *handle, + const char *name); + +struct GNUNET_CHAT_AttributeProcess* +internal_attributes_create_store(struct GNUNET_CHAT_Handle *handle, + const char *name, + struct GNUNET_TIME_Relative expires); + +struct GNUNET_CHAT_AttributeProcess* +internal_attributes_create_share(struct GNUNET_CHAT_Handle *handle, + struct GNUNET_CHAT_Contact *contact, + const char *name); + +struct GNUNET_CHAT_AttributeProcess* +internal_attributes_create_request(struct GNUNET_CHAT_Handle *handle, + const struct GNUNET_CHAT_Account *account); + +void +internal_attributes_destroy(struct GNUNET_CHAT_AttributeProcess *attributes); + +void +internal_attributes_next_iter(struct GNUNET_CHAT_AttributeProcess *attributes); + +void +internal_attributes_stop_iter(struct GNUNET_CHAT_AttributeProcess *attributes); + +#endif /* GNUNET_CHAT_INTERNAL_ATTRIBUTE_PROCESS_H_ */ diff --git a/src/internal/gnunet_chat_ticket_process.c b/src/internal/gnunet_chat_ticket_process.c @@ -0,0 +1,150 @@ +/* + This file is part of GNUnet. + Copyright (C) 2024 GNUnet e.V. + + GNUnet is free software: you can redistribute it and/or modify it + under the terms of the GNU Affero General Public License as published + by the Free Software Foundation, either version 3 of the License, + or (at your option) any later version. + + GNUnet is distributed in the hope that it will be useful, but + WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Affero General Public License for more details. + + You should have received a copy of the GNU Affero General Public License + along with this program. If not, see <http://www.gnu.org/licenses/>. + + SPDX-License-Identifier: AGPL3.0-or-later + */ +/* + * @author Tobias Frisch + * @file gnunet_chat_ticket_process.c + */ + +#include "gnunet_chat_ticket_process.h" + +#include "../gnunet_chat_handle.h" + +#include <gnunet/gnunet_common.h> + +struct GNUNET_CHAT_TicketProcess* +internal_tickets_create(struct GNUNET_CHAT_Handle *handle, + struct GNUNET_CHAT_Contact *contact, + const char *name) +{ + GNUNET_assert((handle) && (contact)); + + struct GNUNET_CHAT_TicketProcess *tickets = GNUNET_new( + struct GNUNET_CHAT_TicketProcess + ); + + if (!tickets) + return NULL; + + memset(tickets, 0, sizeof(*tickets)); + + tickets->handle = handle; + tickets->contact = contact; + + if (!name) + goto skip_name; + + tickets->name = GNUNET_strdup(name); + + if (!(tickets->name)) + { + GNUNET_free(tickets); + return NULL; + } + +skip_name: + GNUNET_CONTAINER_DLL_insert_tail( + tickets->handle->tickets_head, + tickets->handle->tickets_tail, + tickets + ); + + return tickets; +} + +struct GNUNET_CHAT_TicketProcess* +internal_tickets_copy(const struct GNUNET_CHAT_TicketProcess* tickets, + const struct GNUNET_RECLAIM_Ticket *ticket) +{ + GNUNET_assert(tickets); + + struct GNUNET_CHAT_TicketProcess* new_tickets; + new_tickets = internal_tickets_create( + tickets->handle, + tickets->contact, + tickets->name + ); + + if (!new_tickets) + return NULL; + + if (!ticket) + goto skip_ticket; + + new_tickets->ticket = GNUNET_new(struct GNUNET_RECLAIM_Ticket); + + if (!(new_tickets->ticket)) + { + internal_tickets_destroy(new_tickets); + return NULL; + } + + GNUNET_memcpy( + new_tickets->ticket, + ticket, + sizeof(*ticket) + ); + +skip_ticket: + new_tickets->callback = tickets->callback; + new_tickets->closure = tickets->closure; + + return new_tickets; +} + +void +internal_tickets_destroy(struct GNUNET_CHAT_TicketProcess *tickets) +{ + GNUNET_assert((tickets) && (tickets->handle)); + + GNUNET_CONTAINER_DLL_remove( + tickets->handle->tickets_head, + tickets->handle->tickets_tail, + tickets + ); + + if (tickets->ticket) + GNUNET_free(tickets->ticket); + if (tickets->name) + GNUNET_free(tickets->name); + + if (tickets->iter) + GNUNET_RECLAIM_ticket_iteration_stop(tickets->iter); + if (tickets->op) + GNUNET_RECLAIM_cancel(tickets->op); + + GNUNET_free(tickets); +} + +void +internal_tickets_next_iter(struct GNUNET_CHAT_TicketProcess *tickets) +{ + GNUNET_assert((tickets) && (tickets->iter)); + + GNUNET_RECLAIM_ticket_iteration_next(tickets->iter); +} + +void +internal_tickets_stop_iter(struct GNUNET_CHAT_TicketProcess *tickets) +{ + GNUNET_assert((tickets) && (tickets->iter)); + + GNUNET_RECLAIM_ticket_iteration_stop(tickets->iter); + tickets->iter = NULL; +} diff --git a/src/internal/gnunet_chat_ticket_process.h b/src/internal/gnunet_chat_ticket_process.h @@ -0,0 +1,71 @@ +/* + This file is part of GNUnet. + Copyright (C) 2024 GNUnet e.V. + + GNUnet is free software: you can redistribute it and/or modify it + under the terms of the GNU Affero General Public License as published + by the Free Software Foundation, either version 3 of the License, + or (at your option) any later version. + + GNUnet is distributed in the hope that it will be useful, but + WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Affero General Public License for more details. + + You should have received a copy of the GNU Affero General Public License + along with this program. If not, see <http://www.gnu.org/licenses/>. + + SPDX-License-Identifier: AGPL3.0-or-later + */ +/* + * @author Tobias Frisch + * @file gnunet_chat_ticket_process.h + */ + +#ifndef GNUNET_CHAT_INTERNAL_TICKET_PROCESS_H_ +#define GNUNET_CHAT_INTERNAL_TICKET_PROCESS_H_ + +#include <gnunet/gnunet_reclaim_service.h> + +#include "gnunet_chat_lib.h" + +struct GNUNET_CHAT_Handle; +struct GNUNET_CHAT_Contact; + +struct GNUNET_CHAT_TicketProcess +{ + struct GNUNET_CHAT_Handle *handle; + struct GNUNET_CHAT_Contact *contact; + + struct GNUNET_RECLAIM_Ticket *ticket; + char *name; + + GNUNET_CHAT_ContactAttributeCallback callback; + void *closure; + + struct GNUNET_RECLAIM_TicketIterator *iter; + struct GNUNET_RECLAIM_Operation *op; + + struct GNUNET_CHAT_TicketProcess *next; + struct GNUNET_CHAT_TicketProcess *prev; +}; + +struct GNUNET_CHAT_TicketProcess* +internal_tickets_create(struct GNUNET_CHAT_Handle *handle, + struct GNUNET_CHAT_Contact *contact, + const char *name); + +struct GNUNET_CHAT_TicketProcess* +internal_tickets_copy(const struct GNUNET_CHAT_TicketProcess* tickets, + const struct GNUNET_RECLAIM_Ticket *ticket); + +void +internal_tickets_destroy(struct GNUNET_CHAT_TicketProcess *tickets); + +void +internal_tickets_next_iter(struct GNUNET_CHAT_TicketProcess *tickets); + +void +internal_tickets_stop_iter(struct GNUNET_CHAT_TicketProcess *tickets); + +#endif /* GNUNET_CHAT_INTERNAL_TICKET_PROCESS_H_ */ diff --git a/src/internal/meson.build b/src/internal/meson.build @@ -19,5 +19,7 @@ # gnunetchat_internal_sources = files([ - 'gnunet_chat_accounts.c', 'gnunet_chat_accounts.h' + 'gnunet_chat_accounts.c', 'gnunet_chat_accounts.h', + 'gnunet_chat_attribute_process.c', 'gnunet_chat_attribute_process.h', + 'gnunet_chat_ticket_process.c', 'gnunet_chat_ticket_process.h' ])