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