aboutsummaryrefslogtreecommitdiff
path: root/src/messenger/plugin_gnsrecord_messenger.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/messenger/plugin_gnsrecord_messenger.c')
-rw-r--r--src/messenger/plugin_gnsrecord_messenger.c298
1 files changed, 0 insertions, 298 deletions
diff --git a/src/messenger/plugin_gnsrecord_messenger.c b/src/messenger/plugin_gnsrecord_messenger.c
deleted file mode 100644
index e09a0330d..000000000
--- a/src/messenger/plugin_gnsrecord_messenger.c
+++ /dev/null
@@ -1,298 +0,0 @@
1/*
2 This file is part of GNUnet.
3 Copyright (C) 2021--2022 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/plugin_gnsrecord_messenger.c
23 * @brief Plugin to provide the API for useful GNS records to improve
24 * the usability of the messenger service.
25 */
26
27#include "platform.h"
28#include "gnunet_util_lib.h"
29#include "gnunet_gnsrecord_lib.h"
30#include "gnunet_messenger_service.h"
31#include "gnunet_gnsrecord_plugin.h"
32
33
34/**
35 * Convert the 'value' of a record to a string.
36 *
37 * @param cls closure, unused
38 * @param type type of the record
39 * @param data value in binary encoding
40 * @param data_size number of bytes in @a data
41 * @return NULL on error, otherwise human-readable representation of the value
42 */
43static char *
44messenger_value_to_string (void *cls,
45 uint32_t type,
46 const void *data,
47 size_t data_size)
48{
49 (void) cls;
50 switch (type)
51 {
52 case GNUNET_GNSRECORD_TYPE_MESSENGER_ROOM_ENTRY:
53 {
54 if (data_size != sizeof(struct GNUNET_MESSENGER_RoomEntryRecord))
55 {
56 GNUNET_break_op (0);
57 return NULL;
58 }
59
60 const struct GNUNET_MESSENGER_RoomEntryRecord *record = data;
61
62 char *door = GNUNET_CRYPTO_eddsa_public_key_to_string (&(record->door.public_key));
63 char *key = GNUNET_STRINGS_data_to_string_alloc (&(record->key), sizeof(struct GNUNET_HashCode));
64
65 char *ret;
66 GNUNET_asprintf (&ret, "%s-%s", key, door);
67 GNUNET_free (key);
68 GNUNET_free (door);
69 return ret;
70 }
71 case GNUNET_GNSRECORD_TYPE_MESSENGER_ROOM_DETAILS:
72 {
73 if (data_size != sizeof(struct GNUNET_MESSENGER_RoomDetailsRecord))
74 {
75 GNUNET_break_op (0);
76 return NULL;
77 }
78
79 const struct GNUNET_MESSENGER_RoomDetailsRecord *record = data;
80
81 char *name = GNUNET_strndup(record->name, 256);
82 char *flags = GNUNET_STRINGS_data_to_string_alloc (&(record->flags), sizeof(uint32_t));
83
84 char *ret;
85 GNUNET_asprintf (&ret, "%s-%s", flags, name);
86 GNUNET_free (flags);
87 GNUNET_free (name);
88 return ret;
89 }
90 default:
91 return NULL;
92 }
93}
94
95
96/**
97 * Convert human-readable version of a 'value' of a record to the binary
98 * representation.
99 *
100 * @param cls closure, unused
101 * @param type type of the record
102 * @param s human-readable string
103 * @param data set to value in binary encoding (will be allocated)
104 * @param data_size set to number of bytes in @a data
105 * @return #GNUNET_OK on success
106 */
107static int
108messenger_string_to_value (void *cls,
109 uint32_t type,
110 const char *s,
111 void **data,
112 size_t *data_size)
113{
114 (void) cls;
115 if (NULL == s)
116 {
117 GNUNET_break (0);
118 return GNUNET_SYSERR;
119 }
120
121 switch (type)
122 {
123 case GNUNET_GNSRECORD_TYPE_MESSENGER_ROOM_ENTRY:
124 {
125 char key [103];
126 const char *dash;
127 struct GNUNET_PeerIdentity door;
128
129 if ((NULL == (dash = strchr (s, '-'))) ||
130 (1 != sscanf (s, "%103s-", key)) ||
131 (GNUNET_OK != GNUNET_CRYPTO_eddsa_public_key_from_string (dash + 1,
132 strlen (dash + 1),
133 &(door.public_key))))
134 {
135 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
136 _ ("Unable to parse MESSENGER_ROOM_ENTRY record `%s'\n"),
137 s);
138 return GNUNET_SYSERR;
139 }
140
141 struct GNUNET_MESSENGER_RoomEntryRecord *record = GNUNET_new (
142 struct GNUNET_MESSENGER_RoomEntryRecord
143 );
144
145 if (GNUNET_OK != GNUNET_STRINGS_string_to_data (key,
146 strlen (key),
147 &(record->key),
148 sizeof(struct GNUNET_HashCode)))
149 {
150 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
151 _ ("Unable to parse MESSENGER_ROOM_ENTRY record `%s'\n"),
152 s);
153 GNUNET_free (record);
154 return GNUNET_SYSERR;
155 }
156
157 record->door = door;
158 *data = record;
159 *data_size = sizeof(struct GNUNET_MESSENGER_RoomEntryRecord);
160 return GNUNET_OK;
161 }
162 case GNUNET_GNSRECORD_TYPE_MESSENGER_ROOM_DETAILS:
163 {
164 char flags [7];
165 const char *dash;
166
167 if ((NULL == (dash = strchr (s, '-'))) ||
168 (1 != sscanf (s, "%7s-", flags)) ||
169 (strlen (dash + 1) > 256))
170 {
171 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
172 _ ("Unable to parse MESSENGER_ROOM_DETAILS record `%s'\n"),
173 s);
174 return GNUNET_SYSERR;
175 }
176
177 struct GNUNET_MESSENGER_RoomDetailsRecord *record = GNUNET_new (
178 struct GNUNET_MESSENGER_RoomDetailsRecord
179 );
180
181 if (GNUNET_OK != GNUNET_STRINGS_string_to_data (flags,
182 strlen (flags),
183 &(record->flags),
184 sizeof(uint32_t)))
185 {
186 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
187 _ ("Unable to parse MESSENGER_ROOM_DETAILS record `%s'\n"),
188 s);
189 GNUNET_free (record);
190 return GNUNET_SYSERR;
191 }
192
193 GNUNET_memcpy(record->name, dash + 1, strlen(dash + 1));
194
195 *data = record;
196 *data_size = sizeof(struct GNUNET_MESSENGER_RoomDetailsRecord);
197 return GNUNET_OK;
198 }
199 default:
200 return GNUNET_SYSERR;
201 }
202}
203
204
205/**
206 * Mapping of record type numbers to human-readable
207 * record type names.
208 */
209static struct
210{
211 const char *name;
212 uint32_t number;
213} name_map[] = {
214 { "MESSENGER_ROOM_ENTRY", GNUNET_GNSRECORD_TYPE_MESSENGER_ROOM_ENTRY },
215 { "MESSENGER_ROOM_DETAILS", GNUNET_GNSRECORD_TYPE_MESSENGER_ROOM_DETAILS },
216 { NULL, UINT32_MAX }
217};
218
219
220/**
221 * Convert a type name (e.g. "AAAA") to the corresponding number.
222 *
223 * @param cls closure, unused
224 * @param gns_typename name to convert
225 * @return corresponding number, UINT32_MAX on error
226 */
227static uint32_t
228messenger_typename_to_number (void *cls,
229 const char *gns_typename)
230{
231 unsigned int i;
232
233 (void) cls;
234 i = 0;
235 while ((name_map[i].name != NULL) &&
236 (0 != strcasecmp (gns_typename, name_map[i].name)))
237 i++;
238 return name_map[i].number;
239}
240
241
242/**
243 * Convert a type number to the corresponding type string (e.g. 1 to "A")
244 *
245 * @param cls closure, unused
246 * @param type number of a type to convert
247 * @return corresponding typestring, NULL on error
248 */
249static const char *
250messenger_number_to_typename (void *cls,
251 uint32_t type)
252{
253 unsigned int i;
254
255 (void) cls;
256 i = 0;
257 while ((name_map[i].name != NULL) &&
258 (type != name_map[i].number))
259 i++;
260 return name_map[i].name;
261}
262
263
264/**
265 * Entry point for the plugin.
266 *
267 * @param cls NULL
268 * @return the exported block API
269 */
270void *
271libgnunet_plugin_gnsrecord_messenger_init (void *cls)
272{
273 struct GNUNET_GNSRECORD_PluginFunctions *api;
274
275 (void) cls;
276 api = GNUNET_new (struct GNUNET_GNSRECORD_PluginFunctions);
277 api->value_to_string = &messenger_value_to_string;
278 api->string_to_value = &messenger_string_to_value;
279 api->typename_to_number = &messenger_typename_to_number;
280 api->number_to_typename = &messenger_number_to_typename;
281 return api;
282}
283
284
285/**
286 * Exit point from the plugin.
287 *
288 * @param cls the return value from #libgnunet_plugin_block_test_init
289 * @return NULL
290 */
291void *
292libgnunet_plugin_gnsrecord_messenger_done (void *cls)
293{
294 struct GNUNET_GNSRECORD_PluginFunctions *api = cls;
295
296 GNUNET_free (api);
297 return NULL;
298}