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"
37 * Convert the 'value' of a record to a string.
39 * @param cls closure, unused
40 * @param type type of the record
41 * @param data value in binary encoding
42 * @param data_size number of bytes in @a data
43 * @return NULL on error, otherwise human-readable representation of the value
46 gns_value_to_string (void *cls,
55 case GNUNET_GNSRECORD_TYPE_PKEY:
56 if (data_size != sizeof (struct GNUNET_CRYPTO_EcdsaPublicKey))
58 return GNUNET_CRYPTO_ecdsa_public_key_to_string (data);
59 case GNUNET_GNSRECORD_TYPE_NICK:
60 return GNUNET_strndup (data, data_size);
61 case GNUNET_GNSRECORD_TYPE_LEHO:
62 return GNUNET_strndup (data, data_size);
63 case GNUNET_GNSRECORD_TYPE_GNS2DNS:
71 ns = GNUNET_DNSPARSER_parse_name (data,
74 ip = GNUNET_DNSPARSER_parse_name (data,
82 GNUNET_free_non_null (ns);
83 GNUNET_free_non_null (ip);
86 GNUNET_asprintf (&nstr,
90 GNUNET_free_non_null (ns);
91 GNUNET_free_non_null (ip);
94 case GNUNET_GNSRECORD_TYPE_VPN:
96 struct GNUNET_TUN_GnsVpnRecord vpn;
100 if ( (data_size <= sizeof (vpn)) ||
101 ('\0' != cdata[data_size - 1]) )
102 return NULL; /* malformed */
103 /* need to memcpy for alignment */
107 GNUNET_asprintf (&vpn_str,
109 (unsigned int) ntohs (vpn.proto),
110 (const char*) GNUNET_i2s_full (&vpn.peer),
111 (const char*) &cdata[sizeof (vpn)]);
114 case GNUNET_GNSRECORD_TYPE_BOX:
116 struct GNUNET_GNSRECORD_BoxRecord box;
122 if (data_size < sizeof (struct GNUNET_GNSRECORD_BoxRecord))
123 return NULL; /* malformed */
127 rt = ntohl (box.record_type);
128 ival = GNUNET_GNSRECORD_value_to_string (rt,
129 &cdata[sizeof (box)],
130 data_size - sizeof (box));
132 return NULL; /* malformed */
133 GNUNET_asprintf (&box_str,
135 (unsigned int) ntohs (box.protocol),
136 (unsigned int) ntohs (box.service),
149 * Convert human-readable version of a 'value' of a record to the binary
152 * @param cls closure, unused
153 * @param type type of the record
154 * @param s human-readable string
155 * @param data set to value in binary encoding (will be allocated)
156 * @param data_size set to number of bytes in @a data
157 * @return #GNUNET_OK on success
160 gns_string_to_value (void *cls,
166 struct GNUNET_CRYPTO_EcdsaPublicKey pkey;
169 return GNUNET_SYSERR;
173 case GNUNET_GNSRECORD_TYPE_PKEY:
175 GNUNET_CRYPTO_ecdsa_public_key_from_string (s, strlen (s), &pkey))
177 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
178 _("Unable to parse PKEY record `%s'\n"),
180 return GNUNET_SYSERR;
182 *data = GNUNET_new (struct GNUNET_CRYPTO_EcdsaPublicKey);
183 GNUNET_memcpy (*data, &pkey, sizeof (pkey));
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, nsbuf, off);
239 case GNUNET_GNSRECORD_TYPE_VPN:
241 struct GNUNET_TUN_GnsVpnRecord *vpn;
242 char s_peer[103 + 1];
243 char s_serv[253 + 1];
248 &proto, s_peer, s_serv))
250 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
251 _("Unable to parse VPN record string `%s'\n"),
253 return GNUNET_SYSERR;
255 *data_size = sizeof (struct GNUNET_TUN_GnsVpnRecord) + strlen (s_serv) + 1;
256 *data = vpn = GNUNET_malloc (*data_size);
257 if (GNUNET_OK != GNUNET_CRYPTO_eddsa_public_key_from_string ((char*) s_peer,
259 &vpn->peer.public_key))
263 return GNUNET_SYSERR;
265 vpn->proto = htons ((uint16_t) proto);
266 strcpy ((char*)&vpn[1], s_serv);
269 case GNUNET_GNSRECORD_TYPE_BOX:
271 struct GNUNET_GNSRECORD_BoxRecord *box;
273 unsigned int protocol;
274 unsigned int service;
275 unsigned int record_type;
285 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
286 _("Unable to parse BOX record string `%s'\n"),
288 return GNUNET_SYSERR;
290 rest = snprintf (NULL, 0,
296 GNUNET_GNSRECORD_string_to_value (record_type,
300 return GNUNET_SYSERR;
301 *data_size = sizeof (struct GNUNET_GNSRECORD_BoxRecord) + bval_size;
302 *data = box = GNUNET_malloc (*data_size);
303 box->protocol = htons (protocol);
304 box->service = htons (service);
305 box->record_type = htonl (record_type);
306 GNUNET_memcpy (&box[1],
313 return GNUNET_SYSERR;
319 * Mapping of record type numbers to human-readable
326 { "PKEY", GNUNET_GNSRECORD_TYPE_PKEY },
327 { "NICK", GNUNET_GNSRECORD_TYPE_NICK },
328 { "LEHO", GNUNET_GNSRECORD_TYPE_LEHO },
329 { "VPN", GNUNET_GNSRECORD_TYPE_VPN },
330 { "GNS2DNS", GNUNET_GNSRECORD_TYPE_GNS2DNS },
331 { "BOX", GNUNET_GNSRECORD_TYPE_BOX },
337 * Convert a type name (i.e. "AAAA") to the corresponding number.
339 * @param cls closure, unused
340 * @param gns_typename name to convert
341 * @return corresponding number, UINT32_MAX on error
344 gns_typename_to_number (void *cls,
345 const char *gns_typename)
350 while ( (NULL != gns_name_map[i].name) &&
351 (0 != strcasecmp (gns_typename,
352 gns_name_map[i].name)) )
354 return gns_name_map[i].number;
359 * Convert a type number (i.e. 1) to the corresponding type string (i.e. "A")
361 * @param cls closure, unused
362 * @param type number of a type to convert
363 * @return corresponding typestring, NULL on error
366 gns_number_to_typename (void *cls,
372 while ( (NULL != gns_name_map[i].name) &&
373 (type != gns_name_map[i].number) )
375 return gns_name_map[i].name;
380 * Entry point for the plugin.
383 * @return the exported block API
386 libgnunet_plugin_gnsrecord_gns_init (void *cls)
388 struct GNUNET_GNSRECORD_PluginFunctions *api;
390 api = GNUNET_new (struct GNUNET_GNSRECORD_PluginFunctions);
391 api->value_to_string = &gns_value_to_string;
392 api->string_to_value = &gns_string_to_value;
393 api->typename_to_number = &gns_typename_to_number;
394 api->number_to_typename = &gns_number_to_typename;
400 * Exit point from the plugin.
402 * @param cls the return value from #libgnunet_plugin_block_test_init()
406 libgnunet_plugin_gnsrecord_gns_done (void *cls)
408 struct GNUNET_GNSRECORD_PluginFunctions *api = cls;
414 /* end of plugin_gnsrecord_gns.c */