libgnunetchat

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

gnunet_chat_ticket.c (3012B)


      1 /*
      2    This file is part of GNUnet.
      3    Copyright (C) 2024--2025 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.c
     23  */
     24 
     25 #include "gnunet_chat_ticket.h"
     26 
     27 #include "gnunet_chat_contact.h"
     28 #include "gnunet_chat_ticket_intern.c"
     29 #include "gnunet_chat_handle.h"
     30 
     31 #include <gnunet/gnunet_common.h>
     32 #include <gnunet/gnunet_messenger_service.h>
     33 #include <gnunet/gnunet_reclaim_service.h>
     34 #include <string.h>
     35 
     36 struct GNUNET_CHAT_Ticket*
     37 ticket_create_from_message (struct GNUNET_CHAT_Handle *handle,
     38                             struct GNUNET_CHAT_Contact *issuer,
     39                             const struct GNUNET_MESSENGER_MessageTicket *message)
     40 {
     41   GNUNET_assert((handle) && (issuer) && (message));
     42 
     43   const struct GNUNET_CRYPTO_BlindablePublicKey *identity;
     44   const struct GNUNET_CRYPTO_BlindablePublicKey *audience;
     45 
     46   identity = contact_get_key(issuer);
     47   audience = GNUNET_MESSENGER_get_key(handle->messenger);
     48 
     49   if ((!identity) || (!audience))
     50     return NULL;
     51 
     52   struct GNUNET_CHAT_Ticket *ticket = GNUNET_new(struct GNUNET_CHAT_Ticket);
     53 
     54   ticket->handle = handle;
     55   ticket->issuer = issuer;
     56 
     57   ticket->callback = NULL;
     58   ticket->closure = NULL;
     59 
     60   ticket->op = NULL;
     61 
     62   strncpy(ticket->ticket.gns_name, message->identifier, sizeof(ticket->ticket.gns_name));
     63   ticket->ticket.gns_name[sizeof(ticket->ticket.gns_name) - 1] = '\0';
     64 
     65   return ticket;
     66 }
     67 
     68 void
     69 ticket_consume(struct GNUNET_CHAT_Ticket *ticket,
     70                GNUNET_CHAT_ContactAttributeCallback callback,
     71                void *cls)
     72 {
     73   GNUNET_assert(ticket);
     74 
     75   const struct GNUNET_CRYPTO_BlindablePrivateKey *key = handle_get_key(
     76     ticket->handle
     77   );
     78 
     79   if (!key)
     80     return;
     81 
     82   struct GNUNET_CRYPTO_BlindablePublicKey pubkey;
     83   GNUNET_CRYPTO_blindable_key_get_public(key, &pubkey);
     84 
     85   char *rp_uri = GNUNET_CRYPTO_blindable_public_key_to_string(&pubkey);
     86 
     87   ticket->callback = callback;
     88   ticket->closure = cls;
     89 
     90   if (ticket->op)
     91     GNUNET_RECLAIM_cancel(ticket->op);
     92 
     93   ticket->op = GNUNET_RECLAIM_ticket_consume(
     94     ticket->handle->reclaim,
     95     &(ticket->ticket),
     96     rp_uri,
     97     cb_ticket_consume_attribute,
     98     ticket
     99   );
    100 
    101   GNUNET_free(rp_uri);
    102 }
    103 
    104 void
    105 ticket_destroy (struct GNUNET_CHAT_Ticket *ticket)
    106 {
    107   GNUNET_assert(ticket);
    108 
    109   if (ticket->op)
    110     GNUNET_RECLAIM_cancel(ticket->op);
    111 
    112   GNUNET_free(ticket);
    113 }