gnunet_chat_ticket_process.c (3449B)
1 /* 2 This file is part of GNUnet. 3 Copyright (C) 2024 GNUnet e.V. 4 5 GNUnet is free software: you can redistribute it and/or modify it 6 under the terms of the GNU Affero General Public License as published 7 by the Free Software Foundation, either version 3 of the License, 8 or (at your option) any later version. 9 10 GNUnet is distributed in the hope that it will be useful, but 11 WITHOUT ANY WARRANTY; without even the implied warranty of 12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 13 Affero General Public License for more details. 14 15 You should have received a copy of the GNU Affero General Public License 16 along with this program. If not, see <http://www.gnu.org/licenses/>. 17 18 SPDX-License-Identifier: AGPL3.0-or-later 19 */ 20 /* 21 * @author Tobias Frisch 22 * @file gnunet_chat_ticket_process.c 23 */ 24 25 #include "gnunet_chat_ticket_process.h" 26 27 #include "../gnunet_chat_handle.h" 28 29 #include <gnunet/gnunet_common.h> 30 31 struct GNUNET_CHAT_TicketProcess* 32 internal_tickets_create(struct GNUNET_CHAT_Handle *handle, 33 struct GNUNET_CHAT_Contact *contact, 34 const char *name) 35 { 36 GNUNET_assert((handle) && (contact)); 37 38 struct GNUNET_CHAT_TicketProcess *tickets = GNUNET_new( 39 struct GNUNET_CHAT_TicketProcess 40 ); 41 42 if (!tickets) 43 return NULL; 44 45 memset(tickets, 0, sizeof(*tickets)); 46 47 tickets->handle = handle; 48 tickets->contact = contact; 49 50 if (!name) 51 goto skip_name; 52 53 tickets->name = GNUNET_strdup(name); 54 55 if (!(tickets->name)) 56 { 57 GNUNET_free(tickets); 58 return NULL; 59 } 60 61 skip_name: 62 GNUNET_CONTAINER_DLL_insert_tail( 63 tickets->handle->tickets_head, 64 tickets->handle->tickets_tail, 65 tickets 66 ); 67 68 return tickets; 69 } 70 71 struct GNUNET_CHAT_TicketProcess* 72 internal_tickets_copy(const struct GNUNET_CHAT_TicketProcess* tickets, 73 const struct GNUNET_RECLAIM_Ticket *ticket) 74 { 75 GNUNET_assert(tickets); 76 77 struct GNUNET_CHAT_TicketProcess* new_tickets; 78 new_tickets = internal_tickets_create( 79 tickets->handle, 80 tickets->contact, 81 tickets->name 82 ); 83 84 if (!new_tickets) 85 return NULL; 86 87 if (!ticket) 88 goto skip_ticket; 89 90 new_tickets->ticket = GNUNET_new(struct GNUNET_RECLAIM_Ticket); 91 92 if (!(new_tickets->ticket)) 93 { 94 internal_tickets_destroy(new_tickets); 95 return NULL; 96 } 97 98 GNUNET_memcpy( 99 new_tickets->ticket, 100 ticket, 101 sizeof(*ticket) 102 ); 103 104 skip_ticket: 105 new_tickets->callback = tickets->callback; 106 new_tickets->closure = tickets->closure; 107 108 return new_tickets; 109 } 110 111 void 112 internal_tickets_destroy(struct GNUNET_CHAT_TicketProcess *tickets) 113 { 114 GNUNET_assert((tickets) && (tickets->handle)); 115 116 GNUNET_CONTAINER_DLL_remove( 117 tickets->handle->tickets_head, 118 tickets->handle->tickets_tail, 119 tickets 120 ); 121 122 if (tickets->ticket) 123 GNUNET_free(tickets->ticket); 124 if (tickets->name) 125 GNUNET_free(tickets->name); 126 127 if (tickets->iter) 128 GNUNET_RECLAIM_ticket_iteration_stop(tickets->iter); 129 if (tickets->op) 130 GNUNET_RECLAIM_cancel(tickets->op); 131 132 GNUNET_free(tickets); 133 } 134 135 void 136 internal_tickets_next_iter(struct GNUNET_CHAT_TicketProcess *tickets) 137 { 138 GNUNET_assert((tickets) && (tickets->iter)); 139 140 GNUNET_RECLAIM_ticket_iteration_next(tickets->iter); 141 } 142 143 void 144 internal_tickets_stop_iter(struct GNUNET_CHAT_TicketProcess *tickets) 145 { 146 GNUNET_assert((tickets) && (tickets->iter)); 147 148 GNUNET_RECLAIM_ticket_iteration_stop(tickets->iter); 149 tickets->iter = NULL; 150 }