commit 2825c2b3391e1f65d58b6754ea603b83fd6959f1
parent c11052c64b020d3913014b6065c9b16a8cac4547
Author: Jacki <jacki@thejackimonster.de>
Date: Fri, 5 Jan 2024 00:37:18 +0100
Implement functions to consume tickets from contacts
Signed-off-by: Jacki <jacki@thejackimonster.de>
Diffstat:
5 files changed, 170 insertions(+), 1 deletion(-)
diff --git a/include/gnunet/gnunet_chat_lib.h b/include/gnunet/gnunet_chat_lib.h
@@ -338,6 +338,20 @@ typedef void
uint64_t size);
/**
+ * Method called during a chat ticket consumption for each of its attributes.
+ *
+ * @param[in,out] cls Closure from #GNUNET_CHAT_ticket_consume
+ * @param[in] ticket Chat ticket
+ * @param[in] name Attribute name
+ * @param[in] value Attribute value
+ */
+typedef void
+(*GNUNET_CHAT_TicketAttributeCallback) (void *cls,
+ const struct GNUNET_CHAT_Ticket *ticket,
+ const char *name,
+ const char *value);
+
+/**
* Start a chat handle with a certain configuration.
*
* 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);
enum GNUNET_GenericReturnValue
GNUNET_CHAT_invitation_is_accepted (const struct GNUNET_CHAT_Invitation *invitation);
+/**
+ * Returns the contact of the issuer from a given chat <i>ticket</i>.
+ *
+ * @param[in] invitation Chat invitation
+ * @return Chat contact
+ */
+const struct GNUNET_CHAT_Contact*
+GNUNET_CHAT_ticket_get_contact (const struct GNUNET_CHAT_Ticket *ticket);
+
+/**
+ * Consumes a given chat <i>ticket</i>.
+ *
+ * @param[in,out] ticket Chat ticket
+ * @param[in] callback Callback for ticket consumption (optional)
+ * @param[in,out] cls Closure for ticket consumption (optional)
+ */
+void
+GNUNET_CHAT_ticket_consume (struct GNUNET_CHAT_Ticket *ticket,
+ GNUNET_CHAT_TicketAttributeCallback callback,
+ void *cls);
+
/**@}*/
#endif /* GNUNET_CHAT_LIB_H_ */
diff --git a/src/gnunet_chat_lib.c b/src/gnunet_chat_lib.c
@@ -40,7 +40,7 @@
#include "gnunet_chat_invitation.h"
#include "gnunet_chat_lobby.h"
#include "gnunet_chat_message.h"
-
+#include "gnunet_chat_ticket.h"
#include "gnunet_chat_util.h"
#include "gnunet_chat_lib_intern.c"
@@ -1959,3 +1959,34 @@ GNUNET_CHAT_invitation_is_accepted (const struct GNUNET_CHAT_Invitation *invitat
&(invitation->key)
);
}
+
+
+const struct GNUNET_CHAT_Contact*
+GNUNET_CHAT_ticket_get_contact (const struct GNUNET_CHAT_Ticket *ticket)
+{
+ GNUNET_CHAT_VERSION_ASSERT();
+
+ if (!ticket)
+ return NULL;
+
+ struct GNUNET_ShortHashCode shorthash;
+ util_shorthash_from_member(ticket->issuer, &shorthash);
+
+ return GNUNET_CONTAINER_multishortmap_get(
+ ticket->handle->contacts, &shorthash
+ );
+}
+
+
+void
+GNUNET_CHAT_ticket_consume (struct GNUNET_CHAT_Ticket *ticket,
+ GNUNET_CHAT_TicketAttributeCallback callback,
+ void *cls)
+{
+ GNUNET_CHAT_VERSION_ASSERT();
+
+ if (!ticket)
+ return;
+
+ ticket_consume(ticket, callback, cls);
+}
diff --git a/src/gnunet_chat_ticket.c b/src/gnunet_chat_ticket.c
@@ -24,8 +24,11 @@
#include "gnunet_chat_ticket.h"
+#include "gnunet_chat_ticket_intern.c"
#include "gnunet_chat_handle.h"
#include <gnunet/gnunet_messenger_service.h>
+#include <gnunet/gnunet_reclaim_service.h>
+#include <string.h>
struct GNUNET_CHAT_Ticket*
ticket_create_from_message (struct GNUNET_CHAT_Handle *handle,
@@ -45,6 +48,8 @@ ticket_create_from_message (struct GNUNET_CHAT_Handle *handle,
struct GNUNET_CHAT_Ticket *ticket = GNUNET_new(struct GNUNET_CHAT_Ticket);
+ memset(ticket, 0, sizeof(struct GNUNET_CHAT_Ticket));
+
ticket->handle = handle;
ticket->issuer = issuer;
@@ -56,9 +61,41 @@ ticket_create_from_message (struct GNUNET_CHAT_Handle *handle,
}
void
+ticket_consume(struct GNUNET_CHAT_Ticket *ticket,
+ GNUNET_CHAT_TicketAttributeCallback callback,
+ void *cls)
+{
+ GNUNET_assert(ticket);
+
+ const struct GNUNET_CRYPTO_PrivateKey *key = handle_get_key(
+ ticket->handle
+ );
+
+ if (!key)
+ return;
+
+ if (ticket->op)
+ GNUNET_RECLAIM_cancel(ticket->op);
+
+ ticket->callback = callback;
+ ticket->closure = cls;
+
+ ticket->op = GNUNET_RECLAIM_ticket_consume(
+ ticket->handle->reclaim,
+ key,
+ &(ticket->ticket),
+ cb_ticket_consume_attribute,
+ ticket
+ );
+}
+
+void
ticket_destroy (struct GNUNET_CHAT_Ticket *ticket)
{
GNUNET_assert(ticket);
+ if (ticket->op)
+ GNUNET_RECLAIM_cancel(ticket->op);
+
GNUNET_free(ticket);
}
diff --git a/src/gnunet_chat_ticket.h b/src/gnunet_chat_ticket.h
@@ -29,6 +29,8 @@
#include <gnunet/gnunet_reclaim_service.h>
#include <gnunet/gnunet_util_lib.h>
+#include "gnunet_chat_lib.h"
+
struct GNUNET_CHAT_Handle;
struct GNUNET_CHAT_Ticket
@@ -37,6 +39,11 @@ struct GNUNET_CHAT_Ticket
const struct GNUNET_MESSENGER_Contact *issuer;
+ GNUNET_CHAT_TicketAttributeCallback callback;
+ void *closure;
+
+ struct GNUNET_RECLAIM_Operation *op;
+
struct GNUNET_RECLAIM_Ticket ticket;
};
@@ -56,6 +63,16 @@ ticket_create_from_message (struct GNUNET_CHAT_Handle *handle,
const struct GNUNET_MESSENGER_MessageTicket *message);
/**
+ * Consumes a chat <i>ticket</i>.
+ *
+ * @param[in,out] ticket Chat ticket
+ */
+void
+ticket_consume(struct GNUNET_CHAT_Ticket *ticket,
+ GNUNET_CHAT_TicketAttributeCallback callback,
+ void *cls);
+
+/**
* Destroys a chat <i>ticket</i> and frees its memory.
*
* @param[in,out] ticket Chat ticket
diff --git a/src/gnunet_chat_ticket_intern.c b/src/gnunet_chat_ticket_intern.c
@@ -0,0 +1,49 @@
+/*
+ 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_intern.c
+ */
+
+#include <gnunet/gnunet_reclaim_lib.h>
+
+#define GNUNET_UNUSED __attribute__ ((unused))
+
+void
+cb_ticket_consume_attribute (void *cls,
+ const struct GNUNET_CRYPTO_PublicKey *identity,
+ const struct GNUNET_RECLAIM_Attribute *attribute,
+ const struct GNUNET_RECLAIM_Presentation *presentation)
+{
+ GNUNET_assert(cls);
+
+ struct GNUNET_CHAT_Ticket *ticket = (
+ (struct GNUNET_CHAT_Ticket*) cls
+ );
+
+ const char *value = GNUNET_RECLAIM_attribute_value_to_string(
+ attribute->type,
+ attribute->data,
+ attribute->data_size
+ );
+
+ if (ticket->callback)
+ ticket->callback(ticket->closure, ticket, attribute->name, value);
+}