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.
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/>.
20 * @file gns/plugin_gnsrecord_gns.c
21 * @brief gnsrecord plugin to provide the API for fundamental GNS records
22 * This includes the VPN record because GNS resolution
23 * is expected to understand VPN records and (if needed)
24 * map the result to A/AAAA.
25 * @author Christian Grothoff
28 #include "gnunet_util_lib.h"
29 #include "gnunet_gnsrecord_lib.h"
30 #include "gnunet_dnsparser_lib.h"
31 #include "gnunet_gnsrecord_plugin.h"
36 * Convert the 'value' of a record to a string.
38 * @param cls closure, unused
39 * @param type type of the record
40 * @param data value in binary encoding
41 * @param data_size number of bytes in @a data
42 * @return NULL on error, otherwise human-readable representation of the value
45 gns_value_to_string (void *cls,
54 case GNUNET_GNSRECORD_TYPE_PKEY:
55 if (data_size != sizeof (struct GNUNET_CRYPTO_EcdsaPublicKey))
57 return GNUNET_CRYPTO_ecdsa_public_key_to_string (data);
58 case GNUNET_GNSRECORD_TYPE_NICK:
59 return GNUNET_strndup (data, data_size);
60 case GNUNET_GNSRECORD_TYPE_LEHO:
61 return GNUNET_strndup (data, data_size);
62 case GNUNET_GNSRECORD_TYPE_GNS2DNS:
70 ns = GNUNET_DNSPARSER_parse_name (data,
73 ip = GNUNET_DNSPARSER_parse_name (data,
81 GNUNET_free_non_null (ns);
82 GNUNET_free_non_null (ip);
85 GNUNET_asprintf (&nstr,
89 GNUNET_free_non_null (ns);
90 GNUNET_free_non_null (ip);
93 case GNUNET_GNSRECORD_TYPE_VPN:
95 struct GNUNET_TUN_GnsVpnRecord vpn;
99 if ( (data_size <= sizeof (vpn)) ||
100 ('\0' != cdata[data_size - 1]) )
101 return NULL; /* malformed */
102 /* need to memcpy for alignment */
106 GNUNET_asprintf (&vpn_str,
108 (unsigned int) ntohs (vpn.proto),
109 (const char*) GNUNET_i2s_full (&vpn.peer),
110 (const char*) &cdata[sizeof (vpn)]);
113 case GNUNET_GNSRECORD_TYPE_BOX:
115 struct GNUNET_GNSRECORD_BoxRecord box;
121 if (data_size < sizeof (struct GNUNET_GNSRECORD_BoxRecord))
122 return NULL; /* malformed */
126 rt = ntohl (box.record_type);
127 ival = GNUNET_GNSRECORD_value_to_string (rt,
128 &cdata[sizeof (box)],
129 data_size - sizeof (box));
131 return NULL; /* malformed */
132 GNUNET_asprintf (&box_str,
134 (unsigned int) ntohs (box.protocol),
135 (unsigned int) ntohs (box.service),
148 * Convert human-readable version of a 'value' of a record to the binary
151 * @param cls closure, unused
152 * @param type type of the record
153 * @param s human-readable string
154 * @param data set to value in binary encoding (will be allocated)
155 * @param data_size set to number of bytes in @a data
156 * @return #GNUNET_OK on success
159 gns_string_to_value (void *cls,
165 struct GNUNET_CRYPTO_EcdsaPublicKey pkey;
168 return GNUNET_SYSERR;
172 case GNUNET_GNSRECORD_TYPE_PKEY:
174 GNUNET_CRYPTO_ecdsa_public_key_from_string (s,
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,
187 *data_size = sizeof (struct GNUNET_CRYPTO_EcdsaPublicKey);
190 case GNUNET_GNSRECORD_TYPE_NICK:
191 *data = GNUNET_strdup (s);
192 *data_size = strlen (s);
194 case GNUNET_GNSRECORD_TYPE_LEHO:
195 *data = GNUNET_strdup (s);
196 *data_size = strlen (s);
198 case GNUNET_GNSRECORD_TYPE_GNS2DNS:
205 cpy = GNUNET_strdup (s);
206 at = strchr (cpy, '@');
209 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
210 _("Unable to parse GNS2DNS record `%s'\n"),
213 return GNUNET_SYSERR;
220 GNUNET_DNSPARSER_builder_add_name (nsbuf,
225 GNUNET_DNSPARSER_builder_add_name (nsbuf,
230 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
231 _("Failed to serialize GNS2DNS record with value `%s'\n"),
234 return GNUNET_SYSERR;
238 *data = GNUNET_malloc (off);
239 GNUNET_memcpy (*data,
244 case GNUNET_GNSRECORD_TYPE_VPN:
246 struct GNUNET_TUN_GnsVpnRecord *vpn;
247 char s_peer[103 + 1];
248 char s_serv[253 + 1];
253 &proto, s_peer, s_serv))
255 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
256 _("Unable to parse VPN record string `%s'\n"),
258 return GNUNET_SYSERR;
260 *data_size = sizeof (struct GNUNET_TUN_GnsVpnRecord) + strlen (s_serv) + 1;
261 *data = vpn = GNUNET_malloc (*data_size);
263 GNUNET_CRYPTO_eddsa_public_key_from_string ((char*) s_peer,
265 &vpn->peer.public_key))
269 return GNUNET_SYSERR;
271 vpn->proto = htons ((uint16_t) proto);
272 strcpy ((char*)&vpn[1], s_serv);
275 case GNUNET_GNSRECORD_TYPE_BOX:
277 struct GNUNET_GNSRECORD_BoxRecord *box;
279 unsigned int protocol;
280 unsigned int service;
281 unsigned int record_type;
291 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
292 _("Unable to parse BOX record string `%s'\n"),
294 return GNUNET_SYSERR;
296 rest = snprintf (NULL, 0,
302 GNUNET_GNSRECORD_string_to_value (record_type,
306 return GNUNET_SYSERR;
307 *data_size = sizeof (struct GNUNET_GNSRECORD_BoxRecord) + bval_size;
308 *data = box = GNUNET_malloc (*data_size);
309 box->protocol = htons (protocol);
310 box->service = htons (service);
311 box->record_type = htonl (record_type);
312 GNUNET_memcpy (&box[1],
319 return GNUNET_SYSERR;
325 * Mapping of record type numbers to human-readable
332 { "PKEY", GNUNET_GNSRECORD_TYPE_PKEY },
333 { "NICK", GNUNET_GNSRECORD_TYPE_NICK },
334 { "LEHO", GNUNET_GNSRECORD_TYPE_LEHO },
335 { "VPN", GNUNET_GNSRECORD_TYPE_VPN },
336 { "GNS2DNS", GNUNET_GNSRECORD_TYPE_GNS2DNS },
337 { "BOX", GNUNET_GNSRECORD_TYPE_BOX },
343 * Convert a type name (i.e. "AAAA") to the corresponding number.
345 * @param cls closure, unused
346 * @param gns_typename name to convert
347 * @return corresponding number, UINT32_MAX on error
350 gns_typename_to_number (void *cls,
351 const char *gns_typename)
356 while ( (NULL != gns_name_map[i].name) &&
357 (0 != strcasecmp (gns_typename,
358 gns_name_map[i].name)) )
360 return gns_name_map[i].number;
365 * Convert a type number (i.e. 1) to the corresponding type string (i.e. "A")
367 * @param cls closure, unused
368 * @param type number of a type to convert
369 * @return corresponding typestring, NULL on error
372 gns_number_to_typename (void *cls,
378 while ( (NULL != gns_name_map[i].name) &&
379 (type != gns_name_map[i].number) )
381 return gns_name_map[i].name;
386 * Entry point for the plugin.
389 * @return the exported block API
392 libgnunet_plugin_gnsrecord_gns_init (void *cls)
394 struct GNUNET_GNSRECORD_PluginFunctions *api;
396 api = GNUNET_new (struct GNUNET_GNSRECORD_PluginFunctions);
397 api->value_to_string = &gns_value_to_string;
398 api->string_to_value = &gns_string_to_value;
399 api->typename_to_number = &gns_typename_to_number;
400 api->number_to_typename = &gns_number_to_typename;
406 * Exit point from the plugin.
408 * @param cls the return value from #libgnunet_plugin_block_test_init()
412 libgnunet_plugin_gnsrecord_gns_done (void *cls)
414 struct GNUNET_GNSRECORD_PluginFunctions *api = cls;
420 /* end of plugin_gnsrecord_gns.c */