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 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.
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.
17 * @file gns/plugin_gnsrecord_gns.c
18 * @brief gnsrecord plugin to provide the API for fundamental GNS records
19 * This includes the VPN record because GNS resolution
20 * is expected to understand VPN records and (if needed)
21 * map the result to A/AAAA.
22 * @author Christian Grothoff
25 #include "gnunet_util_lib.h"
26 #include "gnunet_gnsrecord_lib.h"
27 #include "gnunet_dnsparser_lib.h"
28 #include "gnunet_gnsrecord_plugin.h"
33 * Convert the 'value' of a record to a string.
35 * @param cls closure, unused
36 * @param type type of the record
37 * @param data value in binary encoding
38 * @param data_size number of bytes in @a data
39 * @return NULL on error, otherwise human-readable representation of the value
42 gns_value_to_string (void *cls,
51 case GNUNET_GNSRECORD_TYPE_PKEY:
52 if (data_size != sizeof (struct GNUNET_CRYPTO_EcdsaPublicKey))
54 return GNUNET_CRYPTO_ecdsa_public_key_to_string (data);
55 case GNUNET_GNSRECORD_TYPE_NICK:
56 return GNUNET_strndup (data, data_size);
57 case GNUNET_GNSRECORD_TYPE_LEHO:
58 return GNUNET_strndup (data, data_size);
59 case GNUNET_GNSRECORD_TYPE_GNS2DNS:
67 ns = GNUNET_DNSPARSER_parse_name (data,
70 ip = GNUNET_DNSPARSER_parse_name (data,
78 GNUNET_free_non_null (ns);
79 GNUNET_free_non_null (ip);
82 GNUNET_asprintf (&nstr,
86 GNUNET_free_non_null (ns);
87 GNUNET_free_non_null (ip);
90 case GNUNET_GNSRECORD_TYPE_VPN:
92 struct GNUNET_TUN_GnsVpnRecord vpn;
96 if ( (data_size <= sizeof (vpn)) ||
97 ('\0' != cdata[data_size - 1]) )
98 return NULL; /* malformed */
99 /* need to memcpy for alignment */
103 GNUNET_asprintf (&vpn_str,
105 (unsigned int) ntohs (vpn.proto),
106 (const char*) GNUNET_i2s_full (&vpn.peer),
107 (const char*) &cdata[sizeof (vpn)]);
110 case GNUNET_GNSRECORD_TYPE_BOX:
112 struct GNUNET_GNSRECORD_BoxRecord box;
118 if (data_size < sizeof (struct GNUNET_GNSRECORD_BoxRecord))
119 return NULL; /* malformed */
123 rt = ntohl (box.record_type);
124 ival = GNUNET_GNSRECORD_value_to_string (rt,
125 &cdata[sizeof (box)],
126 data_size - sizeof (box));
128 return NULL; /* malformed */
129 GNUNET_asprintf (&box_str,
131 (unsigned int) ntohs (box.protocol),
132 (unsigned int) ntohs (box.service),
145 * Convert human-readable version of a 'value' of a record to the binary
148 * @param cls closure, unused
149 * @param type type of the record
150 * @param s human-readable string
151 * @param data set to value in binary encoding (will be allocated)
152 * @param data_size set to number of bytes in @a data
153 * @return #GNUNET_OK on success
156 gns_string_to_value (void *cls,
162 struct GNUNET_CRYPTO_EcdsaPublicKey pkey;
165 return GNUNET_SYSERR;
169 case GNUNET_GNSRECORD_TYPE_PKEY:
171 GNUNET_CRYPTO_ecdsa_public_key_from_string (s,
175 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
176 _("Unable to parse PKEY record `%s'\n"),
178 return GNUNET_SYSERR;
180 *data = GNUNET_new (struct GNUNET_CRYPTO_EcdsaPublicKey);
181 GNUNET_memcpy (*data,
184 *data_size = sizeof (struct GNUNET_CRYPTO_EcdsaPublicKey);
187 case GNUNET_GNSRECORD_TYPE_NICK:
188 *data = GNUNET_strdup (s);
189 *data_size = strlen (s);
191 case GNUNET_GNSRECORD_TYPE_LEHO:
192 *data = GNUNET_strdup (s);
193 *data_size = strlen (s);
195 case GNUNET_GNSRECORD_TYPE_GNS2DNS:
202 cpy = GNUNET_strdup (s);
203 at = strchr (cpy, '@');
206 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
207 _("Unable to parse GNS2DNS record `%s'\n"),
210 return GNUNET_SYSERR;
217 GNUNET_DNSPARSER_builder_add_name (nsbuf,
222 GNUNET_DNSPARSER_builder_add_name (nsbuf,
227 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
228 _("Failed to serialize GNS2DNS record with value `%s'\n"),
231 return GNUNET_SYSERR;
235 *data = GNUNET_malloc (off);
236 GNUNET_memcpy (*data,
241 case GNUNET_GNSRECORD_TYPE_VPN:
243 struct GNUNET_TUN_GnsVpnRecord *vpn;
244 char s_peer[103 + 1];
245 char s_serv[253 + 1];
250 &proto, s_peer, s_serv))
252 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
253 _("Unable to parse VPN record string `%s'\n"),
255 return GNUNET_SYSERR;
257 *data_size = sizeof (struct GNUNET_TUN_GnsVpnRecord) + strlen (s_serv) + 1;
258 *data = vpn = GNUNET_malloc (*data_size);
260 GNUNET_CRYPTO_eddsa_public_key_from_string ((char*) s_peer,
262 &vpn->peer.public_key))
266 return GNUNET_SYSERR;
268 vpn->proto = htons ((uint16_t) proto);
269 strcpy ((char*)&vpn[1], s_serv);
272 case GNUNET_GNSRECORD_TYPE_BOX:
274 struct GNUNET_GNSRECORD_BoxRecord *box;
276 unsigned int protocol;
277 unsigned int service;
278 unsigned int record_type;
288 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
289 _("Unable to parse BOX record string `%s'\n"),
291 return GNUNET_SYSERR;
293 rest = snprintf (NULL, 0,
299 GNUNET_GNSRECORD_string_to_value (record_type,
303 return GNUNET_SYSERR;
304 *data_size = sizeof (struct GNUNET_GNSRECORD_BoxRecord) + bval_size;
305 *data = box = GNUNET_malloc (*data_size);
306 box->protocol = htons (protocol);
307 box->service = htons (service);
308 box->record_type = htonl (record_type);
309 GNUNET_memcpy (&box[1],
316 return GNUNET_SYSERR;
322 * Mapping of record type numbers to human-readable
329 { "PKEY", GNUNET_GNSRECORD_TYPE_PKEY },
330 { "NICK", GNUNET_GNSRECORD_TYPE_NICK },
331 { "LEHO", GNUNET_GNSRECORD_TYPE_LEHO },
332 { "VPN", GNUNET_GNSRECORD_TYPE_VPN },
333 { "GNS2DNS", GNUNET_GNSRECORD_TYPE_GNS2DNS },
334 { "BOX", GNUNET_GNSRECORD_TYPE_BOX },
340 * Convert a type name (i.e. "AAAA") to the corresponding number.
342 * @param cls closure, unused
343 * @param gns_typename name to convert
344 * @return corresponding number, UINT32_MAX on error
347 gns_typename_to_number (void *cls,
348 const char *gns_typename)
353 while ( (NULL != gns_name_map[i].name) &&
354 (0 != strcasecmp (gns_typename,
355 gns_name_map[i].name)) )
357 return gns_name_map[i].number;
362 * Convert a type number (i.e. 1) to the corresponding type string (i.e. "A")
364 * @param cls closure, unused
365 * @param type number of a type to convert
366 * @return corresponding typestring, NULL on error
369 gns_number_to_typename (void *cls,
375 while ( (NULL != gns_name_map[i].name) &&
376 (type != gns_name_map[i].number) )
378 return gns_name_map[i].name;
383 * Entry point for the plugin.
386 * @return the exported block API
389 libgnunet_plugin_gnsrecord_gns_init (void *cls)
391 struct GNUNET_GNSRECORD_PluginFunctions *api;
393 api = GNUNET_new (struct GNUNET_GNSRECORD_PluginFunctions);
394 api->value_to_string = &gns_value_to_string;
395 api->string_to_value = &gns_string_to_value;
396 api->typename_to_number = &gns_typename_to_number;
397 api->number_to_typename = &gns_number_to_typename;
403 * Exit point from the plugin.
405 * @param cls the return value from #libgnunet_plugin_block_test_init()
409 libgnunet_plugin_gnsrecord_gns_done (void *cls)
411 struct GNUNET_GNSRECORD_PluginFunctions *api = cls;
417 /* end of plugin_gnsrecord_gns.c */