GNUnet 0.21.0
messenger_api_contact.c
Go to the documentation of this file.
1/*
2 This file is part of GNUnet.
3 Copyright (C) 2020--2023 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 */
27
30 size_t unique_id)
31{
33
34 struct GNUNET_MESSENGER_Contact *contact = GNUNET_new (struct
36
37 contact->name = NULL;
38 contact->rc = 0;
39 contact->id = unique_id;
40
41 GNUNET_memcpy (&(contact->public_key), key, sizeof(contact->public_key));
42
43 return contact;
44}
45
46
47void
49{
50 GNUNET_assert (contact);
51
52 if (contact->name)
53 GNUNET_free (contact->name);
54
55 GNUNET_free (contact);
56}
57
58
59const char*
61{
62 GNUNET_assert (contact);
63
64 return contact->name;
65}
66
67
68void
70 const char *name)
71{
72 GNUNET_assert (contact);
73
74 if (contact->name)
75 GNUNET_free (contact->name);
76
77 contact->name = name ? GNUNET_strdup (name) : NULL;
78}
79
80
81const struct GNUNET_CRYPTO_PublicKey*
83{
84 GNUNET_assert (contact);
85
86 return &(contact->public_key);
87}
88
89
90void
92{
93 GNUNET_assert (contact);
94
95 contact->rc++;
96}
97
98
101{
102 GNUNET_assert (contact);
103
104 if (contact->rc > 0)
105 contact->rc--;
106
107 return contact->rc ? GNUNET_NO : GNUNET_YES;
108}
109
110
111size_t
113{
114 GNUNET_assert (contact);
115
116 return contact->id;
117}
118
119
120void
122 const struct GNUNET_ShortHashCode *id,
123 struct GNUNET_HashCode *context)
124{
125 GNUNET_assert ((key) && (id) && (context));
126
127 GNUNET_CRYPTO_hash (id, sizeof(*id), context);
129}
struct GNUNET_HashCode key
The key used in the DHT.
static pa_context * context
Pulseaudio context.
static char * name
Name (label) of the records to list.
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 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:135
#define GNUNET_memcpy(dst, src, n)
Call memcpy() but check for n being 0 first.
GNUNET_GenericReturnValue
Named constants for return values.
@ GNUNET_YES
@ GNUNET_NO
#define GNUNET_assert(cond)
Use this for fatal errors that cannot be handled.
#define GNUNET_strdup(a)
Wrapper around GNUNET_xstrdup_.
#define GNUNET_new(type)
Allocate a struct or union of the given type.
#define GNUNET_free(ptr)
Wrapper around free.
enum GNUNET_GenericReturnValue decrease_contact_rc(struct GNUNET_MESSENGER_Contact *contact)
Decreases the reference counter if possible (can not underflow!) of a given contact and returns GNUNE...
const struct GNUNET_CRYPTO_PublicKey * get_contact_key(const struct GNUNET_MESSENGER_Contact *contact)
Returns the public key of a given contact.
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.
void destroy_contact(struct GNUNET_MESSENGER_Contact *contact)
Destroys a contact and frees its memory fully.
void increase_contact_rc(struct GNUNET_MESSENGER_Contact *contact)
Increases the reference counter of a given contact which is zero as default.
size_t get_contact_id(const struct GNUNET_MESSENGER_Contact *contact)
Returns the locally unique identifier of a given contact.
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.
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.
struct GNUNET_MESSENGER_Contact * create_contact(const struct GNUNET_CRYPTO_PublicKey *key, size_t unique_id)
Creates and allocates a new contact with a given public key.
An identity key as per LSD0001.
A 512-bit hashcode.
struct GNUNET_CRYPTO_PublicKey public_key
A 256-bit hashcode.