GNUnet  0.11.x
messenger_api_contact.c
Go to the documentation of this file.
1 /*
2  This file is part of GNUnet.
3  Copyright (C) 2020--2021 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  */
26 #include "messenger_api_contact.h"
27 
30 {
31  GNUNET_assert(key);
32 
34 
35  contact->name = NULL;
36  contact->rc = 0;
37 
38  GNUNET_memcpy(&(contact->public_key), key, sizeof(contact->public_key));
39 
40  return contact;
41 }
42 
43 void
45 {
46  GNUNET_assert(contact);
47 
48  if (contact->name)
49  GNUNET_free(contact->name);
50 
51  GNUNET_free(contact);
52 }
53 
54 const char*
56 {
57  GNUNET_assert(contact);
58 
59  return contact->name;
60 }
61 
62 void
63 set_contact_name (struct GNUNET_MESSENGER_Contact *contact, const char *name)
64 {
65  GNUNET_assert(contact);
66 
67  if (contact->name)
68  GNUNET_free(contact->name);
69 
70  contact->name = name ? GNUNET_strdup(name) : NULL;
71 }
72 
73 const struct GNUNET_IDENTITY_PublicKey*
75 {
76  GNUNET_assert(contact);
77 
78  return &(contact->public_key);
79 }
80 
81 void
83 {
84  GNUNET_assert(contact);
85 
86  contact->rc++;
87 }
88 
89 int
91 {
92  GNUNET_assert(contact);
93 
94  if (contact->rc > 0)
95  contact->rc--;
96 
97  return contact->rc ? GNUNET_NO : GNUNET_YES;
98 }
99 
100 void
102  struct GNUNET_HashCode *context)
103 {
104  GNUNET_assert((key) && (id) && (context));
105 
106  GNUNET_CRYPTO_hash (id, sizeof(*id), context);
107  GNUNET_CRYPTO_hash_xor (key, context, context);
108 }
int decrease_contact_rc(struct GNUNET_MESSENGER_Contact *contact)
Decreases the reference counter if possible (can not underflow!) of a given contact and returns GNUNE...
void get_context_from_member(const struct GNUNET_HashCode *key, const struct GNUNET_ShortHashCode *id, struct GNUNET_HashCode *context)
Calculates the context hash of a member in a room and returns it.
const struct GNUNET_IDENTITY_PublicKey * get_contact_key(const struct GNUNET_MESSENGER_Contact *contact)
Returns the public key of a given contact.
#define GNUNET_assert(cond)
Use this for fatal errors that cannot be handled.
struct GNUNET_IDENTITY_PublicKey public_key
#define GNUNET_memcpy(dst, src, n)
Call memcpy() but check for n being 0 first.
#define GNUNET_new(type)
Allocate a struct or union of the given type.
#define GNUNET_strdup(a)
Wrapper around GNUNET_xstrdup_.
struct GNUNET_MESSENGER_Contact * create_contact(const struct GNUNET_IDENTITY_PublicKey *key)
Creates and allocates a new contact with a given public key from an EGO.
static pa_context * context
Pulseaudio context.
A 256-bit hashcode.
void destroy_contact(struct GNUNET_MESSENGER_Contact *contact)
Destroys a contact and frees its memory fully.
void GNUNET_CRYPTO_hash(const void *block, size_t size, struct GNUNET_HashCode *ret)
Compute hash of a given block.
Definition: crypto_hash.c:41
void increase_contact_rc(struct GNUNET_MESSENGER_Contact *contact)
Increases the reference counter of a given contact which is zero as default.
messenger api: client implementation of GNUnet MESSENGER service
A 512-bit hashcode.
const char * get_contact_name(const struct GNUNET_MESSENGER_Contact *contact)
Returns the current name of a given contact or NULL if no valid name was assigned yet...
struct GNUNET_HashCode key
The key used in the DHT.
An identity key as per LSD0001.
const char * name
void GNUNET_CRYPTO_hash_xor(const struct GNUNET_HashCode *a, const struct GNUNET_HashCode *b, struct GNUNET_HashCode *result)
compute result = a ^ b
Definition: crypto_hash.c:134
void set_contact_name(struct GNUNET_MESSENGER_Contact *contact, const char *name)
Changes the current name of a given contact by copying it from the parameter name.
#define GNUNET_free(ptr)
Wrapper around free.