aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJacki <jacki@thejackimonster.de>2024-01-05 00:37:18 +0100
committerJacki <jacki@thejackimonster.de>2024-01-05 00:37:18 +0100
commit2825c2b3391e1f65d58b6754ea603b83fd6959f1 (patch)
tree6190b440d417e48ce8b495c12a62882f8d30d11c
parentc11052c64b020d3913014b6065c9b16a8cac4547 (diff)
downloadlibgnunetchat-2825c2b3391e1f65d58b6754ea603b83fd6959f1.tar.gz
libgnunetchat-2825c2b3391e1f65d58b6754ea603b83fd6959f1.zip
Implement functions to consume tickets from contacts
Signed-off-by: Jacki <jacki@thejackimonster.de>
-rw-r--r--include/gnunet/gnunet_chat_lib.h35
-rw-r--r--src/gnunet_chat_lib.c33
-rw-r--r--src/gnunet_chat_ticket.c37
-rw-r--r--src/gnunet_chat_ticket.h17
-rw-r--r--src/gnunet_chat_ticket_intern.c49
5 files changed, 170 insertions, 1 deletions
diff --git a/include/gnunet/gnunet_chat_lib.h b/include/gnunet/gnunet_chat_lib.h
index 12bf244..0a2fd7c 100644
--- a/include/gnunet/gnunet_chat_lib.h
+++ b/include/gnunet/gnunet_chat_lib.h
@@ -338,6 +338,20 @@ typedef void
338 uint64_t size); 338 uint64_t size);
339 339
340/** 340/**
341 * Method called during a chat ticket consumption for each of its attributes.
342 *
343 * @param[in,out] cls Closure from #GNUNET_CHAT_ticket_consume
344 * @param[in] ticket Chat ticket
345 * @param[in] name Attribute name
346 * @param[in] value Attribute value
347 */
348typedef void
349(*GNUNET_CHAT_TicketAttributeCallback) (void *cls,
350 const struct GNUNET_CHAT_Ticket *ticket,
351 const char *name,
352 const char *value);
353
354/**
341 * Start a chat handle with a certain configuration. 355 * Start a chat handle with a certain configuration.
342 * 356 *
343 * A custom callback for warnings and message events can be provided optionally 357 * A custom callback for warnings and message events can be provided optionally
@@ -1312,6 +1326,27 @@ GNUNET_CHAT_invitation_accept (struct GNUNET_CHAT_Invitation *invitation);
1312enum GNUNET_GenericReturnValue 1326enum GNUNET_GenericReturnValue
1313GNUNET_CHAT_invitation_is_accepted (const struct GNUNET_CHAT_Invitation *invitation); 1327GNUNET_CHAT_invitation_is_accepted (const struct GNUNET_CHAT_Invitation *invitation);
1314 1328
1329/**
1330 * Returns the contact of the issuer from a given chat <i>ticket</i>.
1331 *
1332 * @param[in] invitation Chat invitation
1333 * @return Chat contact
1334 */
1335const struct GNUNET_CHAT_Contact*
1336GNUNET_CHAT_ticket_get_contact (const struct GNUNET_CHAT_Ticket *ticket);
1337
1338/**
1339 * Consumes a given chat <i>ticket</i>.
1340 *
1341 * @param[in,out] ticket Chat ticket
1342 * @param[in] callback Callback for ticket consumption (optional)
1343 * @param[in,out] cls Closure for ticket consumption (optional)
1344 */
1345void
1346GNUNET_CHAT_ticket_consume (struct GNUNET_CHAT_Ticket *ticket,
1347 GNUNET_CHAT_TicketAttributeCallback callback,
1348 void *cls);
1349
1315/**@}*/ 1350/**@}*/
1316 1351
1317#endif /* GNUNET_CHAT_LIB_H_ */ 1352#endif /* GNUNET_CHAT_LIB_H_ */
diff --git a/src/gnunet_chat_lib.c b/src/gnunet_chat_lib.c
index b922171..5ab0a31 100644
--- a/src/gnunet_chat_lib.c
+++ b/src/gnunet_chat_lib.c
@@ -40,7 +40,7 @@
40#include "gnunet_chat_invitation.h" 40#include "gnunet_chat_invitation.h"
41#include "gnunet_chat_lobby.h" 41#include "gnunet_chat_lobby.h"
42#include "gnunet_chat_message.h" 42#include "gnunet_chat_message.h"
43 43#include "gnunet_chat_ticket.h"
44#include "gnunet_chat_util.h" 44#include "gnunet_chat_util.h"
45 45
46#include "gnunet_chat_lib_intern.c" 46#include "gnunet_chat_lib_intern.c"
@@ -1959,3 +1959,34 @@ GNUNET_CHAT_invitation_is_accepted (const struct GNUNET_CHAT_Invitation *invitat
1959 &(invitation->key) 1959 &(invitation->key)
1960 ); 1960 );
1961} 1961}
1962
1963
1964const struct GNUNET_CHAT_Contact*
1965GNUNET_CHAT_ticket_get_contact (const struct GNUNET_CHAT_Ticket *ticket)
1966{
1967 GNUNET_CHAT_VERSION_ASSERT();
1968
1969 if (!ticket)
1970 return NULL;
1971
1972 struct GNUNET_ShortHashCode shorthash;
1973 util_shorthash_from_member(ticket->issuer, &shorthash);
1974
1975 return GNUNET_CONTAINER_multishortmap_get(
1976 ticket->handle->contacts, &shorthash
1977 );
1978}
1979
1980
1981void
1982GNUNET_CHAT_ticket_consume (struct GNUNET_CHAT_Ticket *ticket,
1983 GNUNET_CHAT_TicketAttributeCallback callback,
1984 void *cls)
1985{
1986 GNUNET_CHAT_VERSION_ASSERT();
1987
1988 if (!ticket)
1989 return;
1990
1991 ticket_consume(ticket, callback, cls);
1992}
diff --git a/src/gnunet_chat_ticket.c b/src/gnunet_chat_ticket.c
index 332b4ae..4aa0066 100644
--- a/src/gnunet_chat_ticket.c
+++ b/src/gnunet_chat_ticket.c
@@ -24,8 +24,11 @@
24 24
25#include "gnunet_chat_ticket.h" 25#include "gnunet_chat_ticket.h"
26 26
27#include "gnunet_chat_ticket_intern.c"
27#include "gnunet_chat_handle.h" 28#include "gnunet_chat_handle.h"
28#include <gnunet/gnunet_messenger_service.h> 29#include <gnunet/gnunet_messenger_service.h>
30#include <gnunet/gnunet_reclaim_service.h>
31#include <string.h>
29 32
30struct GNUNET_CHAT_Ticket* 33struct GNUNET_CHAT_Ticket*
31ticket_create_from_message (struct GNUNET_CHAT_Handle *handle, 34ticket_create_from_message (struct GNUNET_CHAT_Handle *handle,
@@ -45,6 +48,8 @@ ticket_create_from_message (struct GNUNET_CHAT_Handle *handle,
45 48
46 struct GNUNET_CHAT_Ticket *ticket = GNUNET_new(struct GNUNET_CHAT_Ticket); 49 struct GNUNET_CHAT_Ticket *ticket = GNUNET_new(struct GNUNET_CHAT_Ticket);
47 50
51 memset(ticket, 0, sizeof(struct GNUNET_CHAT_Ticket));
52
48 ticket->handle = handle; 53 ticket->handle = handle;
49 ticket->issuer = issuer; 54 ticket->issuer = issuer;
50 55
@@ -56,9 +61,41 @@ ticket_create_from_message (struct GNUNET_CHAT_Handle *handle,
56} 61}
57 62
58void 63void
64ticket_consume(struct GNUNET_CHAT_Ticket *ticket,
65 GNUNET_CHAT_TicketAttributeCallback callback,
66 void *cls)
67{
68 GNUNET_assert(ticket);
69
70 const struct GNUNET_CRYPTO_PrivateKey *key = handle_get_key(
71 ticket->handle
72 );
73
74 if (!key)
75 return;
76
77 if (ticket->op)
78 GNUNET_RECLAIM_cancel(ticket->op);
79
80 ticket->callback = callback;
81 ticket->closure = cls;
82
83 ticket->op = GNUNET_RECLAIM_ticket_consume(
84 ticket->handle->reclaim,
85 key,
86 &(ticket->ticket),
87 cb_ticket_consume_attribute,
88 ticket
89 );
90}
91
92void
59ticket_destroy (struct GNUNET_CHAT_Ticket *ticket) 93ticket_destroy (struct GNUNET_CHAT_Ticket *ticket)
60{ 94{
61 GNUNET_assert(ticket); 95 GNUNET_assert(ticket);
62 96
97 if (ticket->op)
98 GNUNET_RECLAIM_cancel(ticket->op);
99
63 GNUNET_free(ticket); 100 GNUNET_free(ticket);
64} 101}
diff --git a/src/gnunet_chat_ticket.h b/src/gnunet_chat_ticket.h
index a2827ed..ff8e3df 100644
--- a/src/gnunet_chat_ticket.h
+++ b/src/gnunet_chat_ticket.h
@@ -29,6 +29,8 @@
29#include <gnunet/gnunet_reclaim_service.h> 29#include <gnunet/gnunet_reclaim_service.h>
30#include <gnunet/gnunet_util_lib.h> 30#include <gnunet/gnunet_util_lib.h>
31 31
32#include "gnunet_chat_lib.h"
33
32struct GNUNET_CHAT_Handle; 34struct GNUNET_CHAT_Handle;
33 35
34struct GNUNET_CHAT_Ticket 36struct GNUNET_CHAT_Ticket
@@ -37,6 +39,11 @@ struct GNUNET_CHAT_Ticket
37 39
38 const struct GNUNET_MESSENGER_Contact *issuer; 40 const struct GNUNET_MESSENGER_Contact *issuer;
39 41
42 GNUNET_CHAT_TicketAttributeCallback callback;
43 void *closure;
44
45 struct GNUNET_RECLAIM_Operation *op;
46
40 struct GNUNET_RECLAIM_Ticket ticket; 47 struct GNUNET_RECLAIM_Ticket ticket;
41}; 48};
42 49
@@ -56,6 +63,16 @@ ticket_create_from_message (struct GNUNET_CHAT_Handle *handle,
56 const struct GNUNET_MESSENGER_MessageTicket *message); 63 const struct GNUNET_MESSENGER_MessageTicket *message);
57 64
58/** 65/**
66 * Consumes a chat <i>ticket</i>.
67 *
68 * @param[in,out] ticket Chat ticket
69 */
70void
71ticket_consume(struct GNUNET_CHAT_Ticket *ticket,
72 GNUNET_CHAT_TicketAttributeCallback callback,
73 void *cls);
74
75/**
59 * Destroys a chat <i>ticket</i> and frees its memory. 76 * Destroys a chat <i>ticket</i> and frees its memory.
60 * 77 *
61 * @param[in,out] ticket Chat ticket 78 * @param[in,out] ticket Chat ticket
diff --git a/src/gnunet_chat_ticket_intern.c b/src/gnunet_chat_ticket_intern.c
new file mode 100644
index 0000000..d3d64c2
--- /dev/null
+++ b/src/gnunet_chat_ticket_intern.c
@@ -0,0 +1,49 @@
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_intern.c
23 */
24
25#include <gnunet/gnunet_reclaim_lib.h>
26
27#define GNUNET_UNUSED __attribute__ ((unused))
28
29void
30cb_ticket_consume_attribute (void *cls,
31 const struct GNUNET_CRYPTO_PublicKey *identity,
32 const struct GNUNET_RECLAIM_Attribute *attribute,
33 const struct GNUNET_RECLAIM_Presentation *presentation)
34{
35 GNUNET_assert(cls);
36
37 struct GNUNET_CHAT_Ticket *ticket = (
38 (struct GNUNET_CHAT_Ticket*) cls
39 );
40
41 const char *value = GNUNET_RECLAIM_attribute_value_to_string(
42 attribute->type,
43 attribute->data,
44 attribute->data_size
45 );
46
47 if (ticket->callback)
48 ticket->callback(ticket->closure, ticket, attribute->name, value);
49}