diff options
Diffstat (limited to 'src/gns/plugin_gnsrecord_gns.c')
-rw-r--r-- | src/gns/plugin_gnsrecord_gns.c | 432 |
1 files changed, 0 insertions, 432 deletions
diff --git a/src/gns/plugin_gnsrecord_gns.c b/src/gns/plugin_gnsrecord_gns.c deleted file mode 100644 index 0ce782a43..000000000 --- a/src/gns/plugin_gnsrecord_gns.c +++ /dev/null | |||
@@ -1,432 +0,0 @@ | |||
1 | /* | ||
2 | This file is part of GNUnet | ||
3 | Copyright (C) 2013, 2014, 2016 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 | /** | ||
22 | * @file gns/plugin_gnsrecord_gns.c | ||
23 | * @brief gnsrecord plugin to provide the API for fundamental GNS records | ||
24 | * This includes the VPN record because GNS resolution | ||
25 | * is expected to understand VPN records and (if needed) | ||
26 | * map the result to A/AAAA. | ||
27 | * @author Christian Grothoff | ||
28 | */ | ||
29 | #include "platform.h" | ||
30 | #include "gnunet_util_lib.h" | ||
31 | #include "gnunet_gnsrecord_lib.h" | ||
32 | #include "gnunet_dnsparser_lib.h" | ||
33 | #include "gnunet_gnsrecord_plugin.h" | ||
34 | #include <inttypes.h> | ||
35 | |||
36 | |||
37 | /** | ||
38 | * Convert the 'value' of a record to a string. | ||
39 | * | ||
40 | * @param cls closure, unused | ||
41 | * @param type type of the record | ||
42 | * @param data value in binary encoding | ||
43 | * @param data_size number of bytes in @a data | ||
44 | * @return NULL on error, otherwise human-readable representation of the value | ||
45 | */ | ||
46 | static char * | ||
47 | gns_value_to_string (void *cls, | ||
48 | uint32_t type, | ||
49 | const void *data, | ||
50 | size_t data_size) | ||
51 | { | ||
52 | const char *cdata; | ||
53 | struct GNUNET_IDENTITY_PublicKey pk; | ||
54 | |||
55 | switch (type) | ||
56 | { | ||
57 | case GNUNET_GNSRECORD_TYPE_PKEY: | ||
58 | case GNUNET_GNSRECORD_TYPE_EDKEY: | ||
59 | if (GNUNET_OK != | ||
60 | GNUNET_GNSRECORD_identity_from_data (data, | ||
61 | data_size, | ||
62 | type, | ||
63 | &pk)) | ||
64 | return NULL; | ||
65 | return GNUNET_IDENTITY_public_key_to_string (&pk); | ||
66 | |||
67 | case GNUNET_GNSRECORD_TYPE_NICK: | ||
68 | case GNUNET_GNSRECORD_TYPE_REDIRECT: | ||
69 | case GNUNET_GNSRECORD_TYPE_LEHO: | ||
70 | return GNUNET_strndup (data, data_size); | ||
71 | |||
72 | case GNUNET_GNSRECORD_TYPE_GNS2DNS: { | ||
73 | char *ns; | ||
74 | char *ip; | ||
75 | size_t off; | ||
76 | char *nstr; | ||
77 | |||
78 | off = 0; | ||
79 | ns = GNUNET_DNSPARSER_parse_name (data, data_size, &off); | ||
80 | if (NULL == ns) | ||
81 | { | ||
82 | GNUNET_break_op (0); | ||
83 | GNUNET_free (ns); | ||
84 | return NULL; | ||
85 | } | ||
86 | /* DNS server IP/name must be UTF-8 */ | ||
87 | ip = GNUNET_strdup (&((const char*) data)[off]); | ||
88 | GNUNET_asprintf (&nstr, "%s@%s", ns, ip); | ||
89 | GNUNET_free (ns); | ||
90 | GNUNET_free (ip); | ||
91 | return nstr; | ||
92 | } | ||
93 | |||
94 | case GNUNET_GNSRECORD_TYPE_VPN: { | ||
95 | struct GNUNET_TUN_GnsVpnRecord vpn; | ||
96 | char *vpn_str; | ||
97 | |||
98 | cdata = data; | ||
99 | if ((data_size <= sizeof(vpn)) || ('\0' != cdata[data_size - 1])) | ||
100 | return NULL; /* malformed */ | ||
101 | /* need to memcpy for alignment */ | ||
102 | GNUNET_memcpy (&vpn, data, sizeof(vpn)); | ||
103 | GNUNET_asprintf (&vpn_str, | ||
104 | "%u %s %s", | ||
105 | (unsigned int) ntohs (vpn.proto), | ||
106 | (const char *) GNUNET_i2s_full (&vpn.peer), | ||
107 | (const char *) &cdata[sizeof(vpn)]); | ||
108 | return vpn_str; | ||
109 | } | ||
110 | |||
111 | case GNUNET_GNSRECORD_TYPE_BOX: { | ||
112 | struct GNUNET_GNSRECORD_BoxRecord box; | ||
113 | uint32_t rt; | ||
114 | char *box_str; | ||
115 | char *ival; | ||
116 | |||
117 | cdata = data; | ||
118 | if (data_size < sizeof(struct GNUNET_GNSRECORD_BoxRecord)) | ||
119 | return NULL; /* malformed */ | ||
120 | GNUNET_memcpy (&box, data, sizeof(box)); | ||
121 | rt = ntohl (box.record_type); | ||
122 | ival = GNUNET_GNSRECORD_value_to_string (rt, | ||
123 | &cdata[sizeof(box)], | ||
124 | data_size - sizeof(box)); | ||
125 | if (NULL == ival) | ||
126 | return NULL; /* malformed */ | ||
127 | GNUNET_asprintf (&box_str, | ||
128 | "%u %u %u %s", | ||
129 | (unsigned int) ntohs (box.protocol), | ||
130 | (unsigned int) ntohs (box.service), | ||
131 | (unsigned int) rt, | ||
132 | ival); | ||
133 | GNUNET_free (ival); | ||
134 | return box_str; | ||
135 | } | ||
136 | case GNUNET_GNSRECORD_TYPE_TOMBSTONE: { | ||
137 | return GNUNET_strdup (_ ( | ||
138 | "This is a memento of an older block for internal maintenance.")); | ||
139 | } | ||
140 | default: | ||
141 | return NULL; | ||
142 | } | ||
143 | } | ||
144 | |||
145 | |||
146 | /** | ||
147 | * Convert human-readable version of a 'value' of a record to the binary | ||
148 | * representation. | ||
149 | * | ||
150 | * @param cls closure, unused | ||
151 | * @param type type of the record | ||
152 | * @param s human-readable string | ||
153 | * @param data set to value in binary encoding (will be allocated) | ||
154 | * @param data_size set to number of bytes in @a data | ||
155 | * @return #GNUNET_OK on success | ||
156 | */ | ||
157 | static int | ||
158 | gns_string_to_value (void *cls, | ||
159 | uint32_t type, | ||
160 | const char *s, | ||
161 | void **data, | ||
162 | size_t *data_size) | ||
163 | { | ||
164 | struct GNUNET_IDENTITY_PublicKey pk; | ||
165 | uint32_t record_type; | ||
166 | |||
167 | if (NULL == s) | ||
168 | return GNUNET_SYSERR; | ||
169 | switch (type) | ||
170 | { | ||
171 | case GNUNET_GNSRECORD_TYPE_PKEY: | ||
172 | case GNUNET_GNSRECORD_TYPE_EDKEY: | ||
173 | if (GNUNET_OK != | ||
174 | GNUNET_IDENTITY_public_key_from_string (s, &pk)) | ||
175 | { | ||
176 | GNUNET_log (GNUNET_ERROR_TYPE_ERROR, | ||
177 | _ ("Unable to parse zone key record `%s'\n"), | ||
178 | s); | ||
179 | return GNUNET_SYSERR; | ||
180 | } | ||
181 | *data_size = GNUNET_IDENTITY_key_get_length (&pk); | ||
182 | if (GNUNET_OK != | ||
183 | GNUNET_GNSRECORD_data_from_identity (&pk, | ||
184 | (char **) data, | ||
185 | data_size, | ||
186 | &record_type)) | ||
187 | return GNUNET_SYSERR; | ||
188 | if (record_type != type) | ||
189 | { | ||
190 | GNUNET_log (GNUNET_ERROR_TYPE_ERROR, | ||
191 | _ ("Record type does not match parsed record type\n")); | ||
192 | return GNUNET_SYSERR; | ||
193 | } | ||
194 | return GNUNET_OK; | ||
195 | |||
196 | case GNUNET_GNSRECORD_TYPE_NICK: | ||
197 | case GNUNET_GNSRECORD_TYPE_REDIRECT: | ||
198 | case GNUNET_GNSRECORD_TYPE_LEHO: | ||
199 | *data = GNUNET_strdup (s); | ||
200 | *data_size = strlen (s); | ||
201 | return GNUNET_OK; | ||
202 | |||
203 | case GNUNET_GNSRECORD_TYPE_GNS2DNS: { | ||
204 | char nsbuf[514]; | ||
205 | char *cpy; | ||
206 | char *at; | ||
207 | size_t off; | ||
208 | |||
209 | cpy = GNUNET_strdup (s); | ||
210 | at = strchr (cpy, '@'); | ||
211 | if (NULL == at) | ||
212 | { | ||
213 | GNUNET_log (GNUNET_ERROR_TYPE_ERROR, | ||
214 | _ ("Unable to parse GNS2DNS record `%s'\n"), | ||
215 | s); | ||
216 | GNUNET_free (cpy); | ||
217 | return GNUNET_SYSERR; | ||
218 | } | ||
219 | *at = '\0'; | ||
220 | at++; | ||
221 | |||
222 | off = 0; | ||
223 | if (GNUNET_OK != | ||
224 | GNUNET_DNSPARSER_builder_add_name (nsbuf, | ||
225 | sizeof(nsbuf), | ||
226 | &off, | ||
227 | cpy)) | ||
228 | { | ||
229 | GNUNET_log (GNUNET_ERROR_TYPE_ERROR, | ||
230 | _ ( | ||
231 | "Failed to serialize GNS2DNS record with value `%s': Not a DNS name.\n"), | ||
232 | s); | ||
233 | GNUNET_free (cpy); | ||
234 | return GNUNET_SYSERR; | ||
235 | } | ||
236 | /* The DNS server location/name is in UTF-8 */ | ||
237 | GNUNET_memcpy (&nsbuf[off], at, strlen (at) + 1); | ||
238 | off += strlen (at) + 1; | ||
239 | GNUNET_free (cpy); | ||
240 | *data_size = off; | ||
241 | *data = GNUNET_malloc (off); | ||
242 | GNUNET_memcpy (*data, nsbuf, off); | ||
243 | return GNUNET_OK; | ||
244 | } | ||
245 | |||
246 | case GNUNET_GNSRECORD_TYPE_VPN: { | ||
247 | struct GNUNET_TUN_GnsVpnRecord *vpn; | ||
248 | char s_peer[103 + 1]; | ||
249 | char s_serv[253 + 1]; | ||
250 | unsigned int proto; | ||
251 | |||
252 | if (3 != sscanf (s, "%u %103s %253s", &proto, s_peer, s_serv)) | ||
253 | { | ||
254 | GNUNET_log (GNUNET_ERROR_TYPE_ERROR, | ||
255 | _ ("Unable to parse VPN record string `%s'\n"), | ||
256 | s); | ||
257 | return GNUNET_SYSERR; | ||
258 | } | ||
259 | *data_size = sizeof(struct GNUNET_TUN_GnsVpnRecord) + strlen (s_serv) + 1; | ||
260 | *data = vpn = GNUNET_malloc (*data_size); | ||
261 | if (GNUNET_OK != | ||
262 | GNUNET_CRYPTO_eddsa_public_key_from_string ((char *) s_peer, | ||
263 | strlen (s_peer), | ||
264 | &vpn->peer.public_key)) | ||
265 | { | ||
266 | GNUNET_free (vpn); | ||
267 | *data_size = 0; | ||
268 | return GNUNET_SYSERR; | ||
269 | } | ||
270 | vpn->proto = htons ((uint16_t) proto); | ||
271 | strcpy ((char *) &vpn[1], s_serv); | ||
272 | return GNUNET_OK; | ||
273 | } | ||
274 | |||
275 | case GNUNET_GNSRECORD_TYPE_BOX: { | ||
276 | struct GNUNET_GNSRECORD_BoxRecord *box; | ||
277 | size_t rest; | ||
278 | unsigned int protocol; | ||
279 | unsigned int service; | ||
280 | unsigned int record_type; | ||
281 | void *bval; | ||
282 | size_t bval_size; | ||
283 | |||
284 | if (3 != sscanf (s, "%u %u %u ", &protocol, &service, &record_type)) | ||
285 | { | ||
286 | GNUNET_log (GNUNET_ERROR_TYPE_ERROR, | ||
287 | _ ("Unable to parse BOX record string `%s'\n"), | ||
288 | s); | ||
289 | return GNUNET_SYSERR; | ||
290 | } | ||
291 | rest = snprintf (NULL, 0, "%u %u %u ", protocol, service, record_type); | ||
292 | if (GNUNET_OK != GNUNET_GNSRECORD_string_to_value (record_type, | ||
293 | &s[rest], | ||
294 | &bval, | ||
295 | &bval_size)) | ||
296 | return GNUNET_SYSERR; | ||
297 | *data_size = sizeof(struct GNUNET_GNSRECORD_BoxRecord) + bval_size; | ||
298 | *data = box = GNUNET_malloc (*data_size); | ||
299 | box->protocol = htons (protocol); | ||
300 | box->service = htons (service); | ||
301 | box->record_type = htonl (record_type); | ||
302 | GNUNET_memcpy (&box[1], bval, bval_size); | ||
303 | GNUNET_free (bval); | ||
304 | return GNUNET_OK; | ||
305 | } | ||
306 | case GNUNET_GNSRECORD_TYPE_TOMBSTONE: { | ||
307 | *data_size = 0; | ||
308 | *data = NULL; | ||
309 | return GNUNET_OK; | ||
310 | } | ||
311 | |||
312 | default: | ||
313 | return GNUNET_SYSERR; | ||
314 | } | ||
315 | } | ||
316 | |||
317 | |||
318 | /** | ||
319 | * Mapping of record type numbers to human-readable | ||
320 | * record type names. | ||
321 | */ | ||
322 | static struct | ||
323 | { | ||
324 | const char *name; | ||
325 | uint32_t number; | ||
326 | } gns_name_map[] = { | ||
327 | { "PKEY", GNUNET_GNSRECORD_TYPE_PKEY }, | ||
328 | { "EDKEY", GNUNET_GNSRECORD_TYPE_EDKEY }, | ||
329 | { "NICK", GNUNET_GNSRECORD_TYPE_NICK }, | ||
330 | { "LEHO", GNUNET_GNSRECORD_TYPE_LEHO }, | ||
331 | { "VPN", GNUNET_GNSRECORD_TYPE_VPN }, | ||
332 | { "GNS2DNS", GNUNET_GNSRECORD_TYPE_GNS2DNS }, | ||
333 | { "BOX", GNUNET_GNSRECORD_TYPE_BOX }, | ||
334 | { "REDIRECT", GNUNET_GNSRECORD_TYPE_REDIRECT }, | ||
335 | /* Tombstones should never be added manually | ||
336 | * so this makes sense, kind of */ | ||
337 | { "\u271E", GNUNET_GNSRECORD_TYPE_TOMBSTONE }, | ||
338 | { NULL, UINT32_MAX } | ||
339 | }; | ||
340 | |||
341 | |||
342 | /** | ||
343 | * Convert a type name (e.g. "AAAA") to the corresponding number. | ||
344 | * | ||
345 | * @param cls closure, unused | ||
346 | * @param gns_typename name to convert | ||
347 | * @return corresponding number, UINT32_MAX on error | ||
348 | */ | ||
349 | static uint32_t | ||
350 | gns_typename_to_number (void *cls, | ||
351 | const char *gns_typename) | ||
352 | { | ||
353 | unsigned int i; | ||
354 | |||
355 | i = 0; | ||
356 | while ((NULL != gns_name_map[i].name) && | ||
357 | (0 != strcasecmp (gns_typename, gns_name_map[i].name))) | ||
358 | i++; | ||
359 | return gns_name_map[i].number; | ||
360 | } | ||
361 | |||
362 | |||
363 | /** | ||
364 | * Convert a type number to the corresponding type string (e.g. 1 to "A") | ||
365 | * | ||
366 | * @param cls closure, unused | ||
367 | * @param type number of a type to convert | ||
368 | * @return corresponding typestring, NULL on error | ||
369 | */ | ||
370 | static const char * | ||
371 | gns_number_to_typename (void *cls, | ||
372 | uint32_t type) | ||
373 | { | ||
374 | unsigned int i; | ||
375 | |||
376 | i = 0; | ||
377 | while ( (NULL != gns_name_map[i].name) && | ||
378 | (type != gns_name_map[i].number) ) | ||
379 | i++; | ||
380 | return gns_name_map[i].name; | ||
381 | } | ||
382 | |||
383 | |||
384 | static enum GNUNET_GenericReturnValue | ||
385 | gns_is_critical (void *cls, uint32_t type) | ||
386 | { | ||
387 | return ((type == GNUNET_GNSRECORD_TYPE_PKEY) || | ||
388 | (type == GNUNET_GNSRECORD_TYPE_EDKEY) || | ||
389 | (type == GNUNET_GNSRECORD_TYPE_GNS2DNS) || | ||
390 | (type == GNUNET_GNSRECORD_TYPE_REDIRECT) ? | ||
391 | GNUNET_YES : GNUNET_NO); | ||
392 | } | ||
393 | |||
394 | |||
395 | /** | ||
396 | * Entry point for the plugin. | ||
397 | * | ||
398 | * @param cls NULL | ||
399 | * @return the exported block API | ||
400 | */ | ||
401 | void * | ||
402 | libgnunet_plugin_gnsrecord_gns_init (void *cls) | ||
403 | { | ||
404 | struct GNUNET_GNSRECORD_PluginFunctions *api; | ||
405 | |||
406 | api = GNUNET_new (struct GNUNET_GNSRECORD_PluginFunctions); | ||
407 | api->value_to_string = &gns_value_to_string; | ||
408 | api->string_to_value = &gns_string_to_value; | ||
409 | api->typename_to_number = &gns_typename_to_number; | ||
410 | api->number_to_typename = &gns_number_to_typename; | ||
411 | api->is_critical = &gns_is_critical; | ||
412 | return api; | ||
413 | } | ||
414 | |||
415 | |||
416 | /** | ||
417 | * Exit point from the plugin. | ||
418 | * | ||
419 | * @param cls the return value from #libgnunet_plugin_block_test_init() | ||
420 | * @return NULL | ||
421 | */ | ||
422 | void * | ||
423 | libgnunet_plugin_gnsrecord_gns_done (void *cls) | ||
424 | { | ||
425 | struct GNUNET_GNSRECORD_PluginFunctions *api = cls; | ||
426 | |||
427 | GNUNET_free (api); | ||
428 | return NULL; | ||
429 | } | ||
430 | |||
431 | |||
432 | /* end of plugin_gnsrecord_gns.c */ | ||