diff options
Diffstat (limited to 'src/messenger/messenger_api_handle.c')
-rw-r--r-- | src/messenger/messenger_api_handle.c | 213 |
1 files changed, 213 insertions, 0 deletions
diff --git a/src/messenger/messenger_api_handle.c b/src/messenger/messenger_api_handle.c new file mode 100644 index 000000000..20ef77254 --- /dev/null +++ b/src/messenger/messenger_api_handle.c | |||
@@ -0,0 +1,213 @@ | |||
1 | /* | ||
2 | This file is part of GNUnet. | ||
3 | Copyright (C) 2020 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 src/messenger/messenger_api_handle.c | ||
23 | * @brief messenger api: client implementation of GNUnet MESSENGER service | ||
24 | */ | ||
25 | |||
26 | #include "messenger_api_handle.h" | ||
27 | |||
28 | struct GNUNET_MESSENGER_Handle* | ||
29 | create_handle (const struct GNUNET_CONFIGURATION_Handle *cfg, GNUNET_MESSENGER_IdentityCallback identity_callback, | ||
30 | void *identity_cls, GNUNET_MESSENGER_MessageCallback msg_callback, void *msg_cls) | ||
31 | { | ||
32 | struct GNUNET_MESSENGER_Handle *handle = GNUNET_new(struct GNUNET_MESSENGER_Handle); | ||
33 | |||
34 | handle->cfg = cfg; | ||
35 | handle->mq = NULL; | ||
36 | |||
37 | handle->identity_callback = identity_callback; | ||
38 | handle->identity_cls = identity_cls; | ||
39 | |||
40 | handle->msg_callback = msg_callback; | ||
41 | handle->msg_cls = msg_cls; | ||
42 | |||
43 | handle->name = NULL; | ||
44 | handle->pubkey = NULL; | ||
45 | |||
46 | handle->reconnect_time = GNUNET_TIME_relative_get_zero_ (); | ||
47 | handle->reconnect_task = NULL; | ||
48 | |||
49 | handle->rooms = GNUNET_CONTAINER_multihashmap_create (8, GNUNET_NO); | ||
50 | handle->contacts = GNUNET_CONTAINER_multihashmap_create (8, GNUNET_NO); | ||
51 | |||
52 | return handle; | ||
53 | } | ||
54 | |||
55 | static int | ||
56 | iterate_destroy_room (void *cls, const struct GNUNET_HashCode *key, void *value) | ||
57 | { | ||
58 | struct GNUNET_MESSENGER_Room *room = value; | ||
59 | |||
60 | destroy_room (room); | ||
61 | |||
62 | return GNUNET_YES; | ||
63 | } | ||
64 | |||
65 | static int | ||
66 | iterate_destroy_contact (void *cls, const struct GNUNET_HashCode *key, void *value) | ||
67 | { | ||
68 | struct GNUNET_MESSENGER_Contact *contact = value; | ||
69 | |||
70 | destroy_contact (contact); | ||
71 | |||
72 | return GNUNET_YES; | ||
73 | } | ||
74 | |||
75 | void | ||
76 | destroy_handle (struct GNUNET_MESSENGER_Handle *handle) | ||
77 | { | ||
78 | if (handle->reconnect_task) | ||
79 | GNUNET_SCHEDULER_cancel (handle->reconnect_task); | ||
80 | |||
81 | if (handle->mq) | ||
82 | GNUNET_MQ_destroy (handle->mq); | ||
83 | |||
84 | if (handle->name) | ||
85 | GNUNET_free(handle->name); | ||
86 | |||
87 | if (handle->pubkey) | ||
88 | GNUNET_free(handle->pubkey); | ||
89 | |||
90 | if (handle->rooms) | ||
91 | { | ||
92 | GNUNET_CONTAINER_multihashmap_iterate (handle->rooms, iterate_destroy_room, NULL); | ||
93 | |||
94 | GNUNET_CONTAINER_multihashmap_destroy (handle->rooms); | ||
95 | } | ||
96 | |||
97 | if (handle->contacts) | ||
98 | { | ||
99 | GNUNET_CONTAINER_multihashmap_iterate (handle->contacts, iterate_destroy_contact, NULL); | ||
100 | |||
101 | GNUNET_CONTAINER_multihashmap_destroy (handle->contacts); | ||
102 | } | ||
103 | |||
104 | GNUNET_free(handle->name); | ||
105 | } | ||
106 | |||
107 | void | ||
108 | set_handle_name (struct GNUNET_MESSENGER_Handle *handle, const char *name) | ||
109 | { | ||
110 | if (handle->name) | ||
111 | GNUNET_free(handle->name); | ||
112 | |||
113 | handle->name = name? GNUNET_strdup(name) : NULL; | ||
114 | } | ||
115 | |||
116 | const char* | ||
117 | get_handle_name (const struct GNUNET_MESSENGER_Handle *handle) | ||
118 | { | ||
119 | return handle->name; | ||
120 | } | ||
121 | |||
122 | void | ||
123 | set_handle_key (struct GNUNET_MESSENGER_Handle *handle, const struct GNUNET_IDENTITY_PublicKey *pubkey) | ||
124 | { | ||
125 | if (!handle->pubkey) | ||
126 | handle->pubkey = GNUNET_new(struct GNUNET_IDENTITY_PublicKey); | ||
127 | |||
128 | GNUNET_memcpy(handle->pubkey, pubkey, sizeof(*pubkey)); | ||
129 | } | ||
130 | |||
131 | const struct GNUNET_IDENTITY_PublicKey* | ||
132 | get_handle_key (const struct GNUNET_MESSENGER_Handle *handle) | ||
133 | { | ||
134 | if (!handle->pubkey) | ||
135 | { | ||
136 | struct GNUNET_IDENTITY_Ego *anonymous = GNUNET_IDENTITY_ego_get_anonymous (); | ||
137 | static struct GNUNET_IDENTITY_PublicKey pubkey; | ||
138 | |||
139 | GNUNET_IDENTITY_ego_get_public_key (anonymous, &pubkey); | ||
140 | |||
141 | return &pubkey; | ||
142 | } | ||
143 | |||
144 | return handle->pubkey; | ||
145 | } | ||
146 | |||
147 | struct GNUNET_MESSENGER_Contact* | ||
148 | get_handle_contact_by_pubkey (const struct GNUNET_MESSENGER_Handle *handle, | ||
149 | const struct GNUNET_IDENTITY_PublicKey *pubkey) | ||
150 | { | ||
151 | struct GNUNET_HashCode hash; | ||
152 | |||
153 | GNUNET_CRYPTO_hash (pubkey, sizeof(*pubkey), &hash); | ||
154 | |||
155 | struct GNUNET_MESSENGER_Contact *contact = GNUNET_CONTAINER_multihashmap_get (handle->contacts, &hash); | ||
156 | |||
157 | if (contact) | ||
158 | return contact; | ||
159 | |||
160 | contact = create_contact (pubkey); | ||
161 | |||
162 | if (GNUNET_OK == GNUNET_CONTAINER_multihashmap_put (handle->contacts, &hash, contact, | ||
163 | GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST)) | ||
164 | return contact; | ||
165 | |||
166 | destroy_contact (contact); | ||
167 | return NULL; | ||
168 | } | ||
169 | |||
170 | void | ||
171 | swap_handle_contact_by_pubkey (struct GNUNET_MESSENGER_Handle *handle, struct GNUNET_MESSENGER_Contact *contact, | ||
172 | const struct GNUNET_IDENTITY_PublicKey *pubkey) | ||
173 | { | ||
174 | const struct GNUNET_HashCode *hash = get_contact_id_from_key (contact); | ||
175 | |||
176 | if (GNUNET_YES == GNUNET_CONTAINER_multihashmap_remove (handle->contacts, hash, contact)) | ||
177 | { | ||
178 | GNUNET_memcpy(&(contact->public_key), pubkey, sizeof(*pubkey)); | ||
179 | |||
180 | hash = get_contact_id_from_key (contact); | ||
181 | |||
182 | GNUNET_CONTAINER_multihashmap_put (handle->contacts, hash, contact, | ||
183 | GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST); | ||
184 | } | ||
185 | } | ||
186 | |||
187 | void | ||
188 | open_handle_room (struct GNUNET_MESSENGER_Handle *handle, const struct GNUNET_HashCode *key) | ||
189 | { | ||
190 | struct GNUNET_MESSENGER_Room *room = GNUNET_CONTAINER_multihashmap_get (handle->rooms, key); | ||
191 | |||
192 | if (room) | ||
193 | room->opened = GNUNET_YES; | ||
194 | } | ||
195 | |||
196 | void | ||
197 | entry_handle_room_at (struct GNUNET_MESSENGER_Handle *handle, const struct GNUNET_PeerIdentity *door, | ||
198 | const struct GNUNET_HashCode *key) | ||
199 | { | ||
200 | struct GNUNET_MESSENGER_Room *room = GNUNET_CONTAINER_multihashmap_get (handle->rooms, key); | ||
201 | |||
202 | if (room) | ||
203 | add_to_list_tunnels (&(room->entries), door); | ||
204 | } | ||
205 | |||
206 | void | ||
207 | close_handle_room (struct GNUNET_MESSENGER_Handle *handle, const struct GNUNET_HashCode *key) | ||
208 | { | ||
209 | struct GNUNET_MESSENGER_Room *room = GNUNET_CONTAINER_multihashmap_get (handle->rooms, key); | ||
210 | |||
211 | if ((room) && (GNUNET_YES == GNUNET_CONTAINER_multihashmap_remove (handle->rooms, key, room))) | ||
212 | destroy_room (room); | ||
213 | } | ||