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),
150 * Convert human-readable version of a 'value' of a record to the binary
153 * @param cls closure, unused
154 * @param type type of the record
155 * @param s human-readable string
156 * @param data set to value in binary encoding (will be allocated)
157 * @param data_size set to number of bytes in @a data
158 * @return #GNUNET_OK on success
161 gns_string_to_value (void *cls,
167 struct GNUNET_CRYPTO_EcdsaPublicKey pkey;
170 return GNUNET_SYSERR;
174 case GNUNET_GNSRECORD_TYPE_PKEY:
176 GNUNET_CRYPTO_ecdsa_public_key_from_string (s, strlen (s), &pkey))
178 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
179 _("Unable to parse PKEY record `%s'\n"),
181 return GNUNET_SYSERR;
183 *data = GNUNET_new (struct GNUNET_CRYPTO_EcdsaPublicKey);
184 GNUNET_memcpy (*data, &pkey, sizeof (pkey));
185 *data_size = sizeof (struct GNUNET_CRYPTO_EcdsaPublicKey);
188 case GNUNET_GNSRECORD_TYPE_NICK:
189 *data = GNUNET_strdup (s);
190 *data_size = strlen (s);
192 case GNUNET_GNSRECORD_TYPE_LEHO:
193 *data = GNUNET_strdup (s);
194 *data_size = strlen (s);
196 case GNUNET_GNSRECORD_TYPE_GNS2DNS:
203 cpy = GNUNET_strdup (s);
204 at = strchr (cpy, '@');
207 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
208 _("Unable to parse GNS2DNS record `%s'\n"),
211 return GNUNET_SYSERR;
218 GNUNET_DNSPARSER_builder_add_name (nsbuf,
223 GNUNET_DNSPARSER_builder_add_name (nsbuf,
228 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
229 _("Failed to serialize GNS2DNS record with value `%s'\n"),
232 return GNUNET_SYSERR;
236 *data = GNUNET_malloc (off);
237 GNUNET_memcpy (*data, nsbuf, off);
240 case GNUNET_GNSRECORD_TYPE_VPN:
242 struct GNUNET_TUN_GnsVpnRecord *vpn;
243 char s_peer[103 + 1];
244 char s_serv[253 + 1];
249 &proto, s_peer, s_serv))
251 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
252 _("Unable to parse VPN record string `%s'\n"),
254 return GNUNET_SYSERR;
256 *data_size = sizeof (struct GNUNET_TUN_GnsVpnRecord) + strlen (s_serv) + 1;
257 *data = vpn = GNUNET_malloc (*data_size);
259 GNUNET_CRYPTO_eddsa_public_key_from_string ((char*) s_peer,
261 &vpn->peer.public_key))
265 return GNUNET_SYSERR;
267 vpn->proto = htons ((uint16_t) proto);
268 strcpy ((char*)&vpn[1], s_serv);
271 case GNUNET_GNSRECORD_TYPE_BOX:
273 struct GNUNET_GNSRECORD_BoxRecord *box;
275 unsigned int protocol;
276 unsigned int service;
277 unsigned int record_type;
287 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
288 _("Unable to parse BOX record string `%s'\n"),
290 return GNUNET_SYSERR;
292 rest = snprintf (NULL, 0,
298 GNUNET_GNSRECORD_string_to_value (record_type,
302 return GNUNET_SYSERR;
303 *data_size = sizeof (struct GNUNET_GNSRECORD_BoxRecord) + bval_size;
304 *data = box = GNUNET_malloc (*data_size);
305 box->protocol = htons (protocol);
306 box->service = htons (service);
307 box->record_type = htonl (record_type);
308 GNUNET_memcpy (&box[1],
315 return GNUNET_SYSERR;
321 * Mapping of record type numbers to human-readable
328 { "PKEY", GNUNET_GNSRECORD_TYPE_PKEY },
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 },
339 * Convert a type name (i.e. "AAAA") to the corresponding number.
341 * @param cls closure, unused
342 * @param gns_typename name to convert
343 * @return corresponding number, UINT32_MAX on error
346 gns_typename_to_number (void *cls,
347 const char *gns_typename)
352 while ( (NULL != gns_name_map[i].name) &&
353 (0 != strcasecmp (gns_typename,
354 gns_name_map[i].name)) )
356 return gns_name_map[i].number;
361 * Convert a type number (i.e. 1) to the corresponding type string (i.e. "A")
363 * @param cls closure, unused
364 * @param type number of a type to convert
365 * @return corresponding typestring, NULL on error
368 gns_number_to_typename (void *cls,
374 while ( (NULL != gns_name_map[i].name) &&
375 (type != gns_name_map[i].number) )
377 return gns_name_map[i].name;
382 * Entry point for the plugin.
385 * @return the exported block API
388 libgnunet_plugin_gnsrecord_gns_init (void *cls)
390 struct GNUNET_GNSRECORD_PluginFunctions *api;
392 api = GNUNET_new (struct GNUNET_GNSRECORD_PluginFunctions);
393 api->value_to_string = &gns_value_to_string;
394 api->string_to_value = &gns_string_to_value;
395 api->typename_to_number = &gns_typename_to_number;
396 api->number_to_typename = &gns_number_to_typename;
402 * Exit point from the plugin.
404 * @param cls the return value from #libgnunet_plugin_block_test_init()
408 libgnunet_plugin_gnsrecord_gns_done (void *cls)
410 struct GNUNET_GNSRECORD_PluginFunctions *api = cls;
416 /* end of plugin_gnsrecord_gns.c */