2 This file is part of GNUnet
3 Copyright (C) 2013, 2014, 2016 GNUnet e.V.
5 GNUnet is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published
7 by the Free Software Foundation; either version 3, or (at your
8 option) any later version.
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 General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with GNUnet; see the file COPYING. If not, write to the
17 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18 Boston, MA 02110-1301, USA.
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
30 #include "gnunet_util_lib.h"
31 #include "gnunet_gnsrecord_lib.h"
32 #include "gnunet_dnsparser_lib.h"
33 #include "gnunet_gnsrecord_plugin.h"
38 * Convert the 'value' of a record to a string.
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
47 gns_value_to_string (void *cls,
56 case GNUNET_GNSRECORD_TYPE_PKEY:
57 if (data_size != sizeof (struct GNUNET_CRYPTO_EcdsaPublicKey))
59 return GNUNET_CRYPTO_ecdsa_public_key_to_string (data);
60 case GNUNET_GNSRECORD_TYPE_NICK:
61 return GNUNET_strndup (data, data_size);
62 case GNUNET_GNSRECORD_TYPE_LEHO:
63 return GNUNET_strndup (data, data_size);
64 case GNUNET_GNSRECORD_TYPE_GNS2DNS:
72 ns = GNUNET_DNSPARSER_parse_name (data,
75 ip = GNUNET_DNSPARSER_parse_name (data,
83 GNUNET_free_non_null (ns);
84 GNUNET_free_non_null (ip);
87 GNUNET_asprintf (&nstr,
91 GNUNET_free_non_null (ns);
92 GNUNET_free_non_null (ip);
95 case GNUNET_GNSRECORD_TYPE_VPN:
97 struct GNUNET_TUN_GnsVpnRecord vpn;
101 if ( (data_size <= sizeof (vpn)) ||
102 ('\0' != cdata[data_size - 1]) )
103 return NULL; /* malformed */
104 /* need to memcpy for alignment */
108 GNUNET_asprintf (&vpn_str,
110 (unsigned int) ntohs (vpn.proto),
111 (const char*) GNUNET_i2s_full (&vpn.peer),
112 (const char*) &cdata[sizeof (vpn)]);
115 case GNUNET_GNSRECORD_TYPE_BOX:
117 struct GNUNET_GNSRECORD_BoxRecord box;
123 if (data_size < sizeof (struct GNUNET_GNSRECORD_BoxRecord))
124 return NULL; /* malformed */
128 rt = ntohl (box.record_type);
129 ival = GNUNET_GNSRECORD_value_to_string (rt,
130 &cdata[sizeof (box)],
131 data_size - sizeof (box));
133 return NULL; /* malformed */
134 GNUNET_asprintf (&box_str,
136 (unsigned int) ntohs (box.protocol),
137 (unsigned int) ntohs (box.service),
143 case GNUNET_GNSRECORD_TYPE_REVERSE:
145 struct GNUNET_GNSRECORD_ReverseRecord rev;
149 if (data_size < sizeof (struct GNUNET_GNSRECORD_ReverseRecord))
150 return NULL; /* malformed */
156 pkey_str = GNUNET_CRYPTO_ecdsa_public_key_to_string (&rev.pkey);
158 GNUNET_asprintf (&rev_str,
160 &cdata[sizeof (rev)],
162 rev.expiration.abs_value_us);
163 GNUNET_free (pkey_str);
174 * Convert human-readable version of a 'value' of a record to the binary
177 * @param cls closure, unused
178 * @param type type of the record
179 * @param s human-readable string
180 * @param data set to value in binary encoding (will be allocated)
181 * @param data_size set to number of bytes in @a data
182 * @return #GNUNET_OK on success
185 gns_string_to_value (void *cls,
191 struct GNUNET_CRYPTO_EcdsaPublicKey pkey;
194 return GNUNET_SYSERR;
198 case GNUNET_GNSRECORD_TYPE_PKEY:
200 GNUNET_CRYPTO_ecdsa_public_key_from_string (s, strlen (s), &pkey))
202 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
203 _("Unable to parse PKEY record `%s'\n"),
205 return GNUNET_SYSERR;
207 *data = GNUNET_new (struct GNUNET_CRYPTO_EcdsaPublicKey);
208 GNUNET_memcpy (*data, &pkey, sizeof (pkey));
209 *data_size = sizeof (struct GNUNET_CRYPTO_EcdsaPublicKey);
212 case GNUNET_GNSRECORD_TYPE_NICK:
213 *data = GNUNET_strdup (s);
214 *data_size = strlen (s);
216 case GNUNET_GNSRECORD_TYPE_LEHO:
217 *data = GNUNET_strdup (s);
218 *data_size = strlen (s);
220 case GNUNET_GNSRECORD_TYPE_GNS2DNS:
227 cpy = GNUNET_strdup (s);
228 at = strchr (cpy, '@');
231 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
232 _("Unable to parse GNS2DNS record `%s'\n"),
235 return GNUNET_SYSERR;
242 GNUNET_DNSPARSER_builder_add_name (nsbuf,
247 GNUNET_DNSPARSER_builder_add_name (nsbuf,
252 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
253 _("Failed to serialize GNS2DNS record with value `%s'\n"),
256 return GNUNET_SYSERR;
260 *data = GNUNET_malloc (off);
261 GNUNET_memcpy (*data, nsbuf, off);
264 case GNUNET_GNSRECORD_TYPE_VPN:
266 struct GNUNET_TUN_GnsVpnRecord *vpn;
267 char s_peer[103 + 1];
268 char s_serv[253 + 1];
273 &proto, s_peer, s_serv))
275 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
276 _("Unable to parse VPN record string `%s'\n"),
278 return GNUNET_SYSERR;
280 *data_size = sizeof (struct GNUNET_TUN_GnsVpnRecord) + strlen (s_serv) + 1;
281 *data = vpn = GNUNET_malloc (*data_size);
283 GNUNET_CRYPTO_eddsa_public_key_from_string ((char*) s_peer,
285 &vpn->peer.public_key))
289 return GNUNET_SYSERR;
291 vpn->proto = htons ((uint16_t) proto);
292 strcpy ((char*)&vpn[1], s_serv);
295 case GNUNET_GNSRECORD_TYPE_BOX:
297 struct GNUNET_GNSRECORD_BoxRecord *box;
299 unsigned int protocol;
300 unsigned int service;
301 unsigned int record_type;
311 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
312 _("Unable to parse BOX record string `%s'\n"),
314 return GNUNET_SYSERR;
316 rest = snprintf (NULL, 0,
322 GNUNET_GNSRECORD_string_to_value (record_type,
326 return GNUNET_SYSERR;
327 *data_size = sizeof (struct GNUNET_GNSRECORD_BoxRecord) + bval_size;
328 *data = box = GNUNET_malloc (*data_size);
329 box->protocol = htons (protocol);
330 box->service = htons (service);
331 box->record_type = htonl (record_type);
332 GNUNET_memcpy (&box[1],
338 case GNUNET_GNSRECORD_TYPE_REVERSE:
340 struct GNUNET_GNSRECORD_ReverseRecord *rev;
341 char known_by[253 + 1];
342 struct GNUNET_TIME_Absolute expiration;
344 /* TODO: From crypto_ecc.c
345 * Why is this not a constant???
347 size_t enclen = (sizeof (struct GNUNET_CRYPTO_EcdsaPublicKey)) * 8;
349 enclen += 5 - enclen % 5;
350 enclen /= 5; /* 260/5 = 52 */
351 char pkey_str[enclen + 1];
354 "%253s %52s %"SCNu64,
357 &expiration.abs_value_us))
359 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
360 _("Unable to parse REVERSE record string `%s'\n"),
362 return GNUNET_SYSERR;
364 *data_size = sizeof (struct GNUNET_GNSRECORD_ReverseRecord) + strlen (known_by) + 1;
365 *data = rev = GNUNET_malloc (*data_size);
367 GNUNET_CRYPTO_ecdsa_public_key_from_string (pkey_str,
372 return GNUNET_SYSERR;
374 rev->expiration = expiration;
375 GNUNET_memcpy (&rev[1],
381 return GNUNET_SYSERR;
387 * Mapping of record type numbers to human-readable
394 { "PKEY", GNUNET_GNSRECORD_TYPE_PKEY },
395 { "NICK", GNUNET_GNSRECORD_TYPE_NICK },
396 { "LEHO", GNUNET_GNSRECORD_TYPE_LEHO },
397 { "VPN", GNUNET_GNSRECORD_TYPE_VPN },
398 { "GNS2DNS", GNUNET_GNSRECORD_TYPE_GNS2DNS },
399 { "BOX", GNUNET_GNSRECORD_TYPE_BOX },
400 { "REVERSE", GNUNET_GNSRECORD_TYPE_REVERSE },
406 * Convert a type name (i.e. "AAAA") to the corresponding number.
408 * @param cls closure, unused
409 * @param gns_typename name to convert
410 * @return corresponding number, UINT32_MAX on error
413 gns_typename_to_number (void *cls,
414 const char *gns_typename)
419 while ( (NULL != gns_name_map[i].name) &&
420 (0 != strcasecmp (gns_typename,
421 gns_name_map[i].name)) )
423 return gns_name_map[i].number;
428 * Convert a type number (i.e. 1) to the corresponding type string (i.e. "A")
430 * @param cls closure, unused
431 * @param type number of a type to convert
432 * @return corresponding typestring, NULL on error
435 gns_number_to_typename (void *cls,
441 while ( (NULL != gns_name_map[i].name) &&
442 (type != gns_name_map[i].number) )
444 return gns_name_map[i].name;
449 * Entry point for the plugin.
452 * @return the exported block API
455 libgnunet_plugin_gnsrecord_gns_init (void *cls)
457 struct GNUNET_GNSRECORD_PluginFunctions *api;
459 api = GNUNET_new (struct GNUNET_GNSRECORD_PluginFunctions);
460 api->value_to_string = &gns_value_to_string;
461 api->string_to_value = &gns_string_to_value;
462 api->typename_to_number = &gns_typename_to_number;
463 api->number_to_typename = &gns_number_to_typename;
469 * Exit point from the plugin.
471 * @param cls the return value from #libgnunet_plugin_block_test_init()
475 libgnunet_plugin_gnsrecord_gns_done (void *cls)
477 struct GNUNET_GNSRECORD_PluginFunctions *api = cls;
483 /* end of plugin_gnsrecord_gns.c */