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/>.
18 SPDX-License-Identifier: AGPL3.0-or-later
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);
61 case GNUNET_GNSRECORD_TYPE_NICK:
62 return GNUNET_strndup(data, data_size);
64 case GNUNET_GNSRECORD_TYPE_LEHO:
65 return GNUNET_strndup(data, data_size);
67 case GNUNET_GNSRECORD_TYPE_GNS2DNS: {
74 ns = GNUNET_DNSPARSER_parse_name(data, data_size, &off);
75 ip = GNUNET_DNSPARSER_parse_name(data, data_size, &off);
76 if ((NULL == ns) || (NULL == ip) || (off != data_size))
79 GNUNET_free_non_null(ns);
80 GNUNET_free_non_null(ip);
83 GNUNET_asprintf(&nstr, "%s@%s", ns, ip);
84 GNUNET_free_non_null(ns);
85 GNUNET_free_non_null(ip);
89 case GNUNET_GNSRECORD_TYPE_VPN: {
90 struct GNUNET_TUN_GnsVpnRecord vpn;
94 if ((data_size <= sizeof(vpn)) || ('\0' != cdata[data_size - 1]))
95 return NULL; /* malformed */
96 /* need to memcpy for alignment */
97 GNUNET_memcpy(&vpn, data, sizeof(vpn));
98 GNUNET_asprintf(&vpn_str,
100 (unsigned int)ntohs(vpn.proto),
101 (const char *)GNUNET_i2s_full(&vpn.peer),
102 (const char *)&cdata[sizeof(vpn)]);
106 case GNUNET_GNSRECORD_TYPE_BOX: {
107 struct GNUNET_GNSRECORD_BoxRecord box;
113 if (data_size < sizeof(struct GNUNET_GNSRECORD_BoxRecord))
114 return NULL; /* malformed */
115 GNUNET_memcpy(&box, data, sizeof(box));
116 rt = ntohl(box.record_type);
117 ival = GNUNET_GNSRECORD_value_to_string(rt,
119 data_size - sizeof(box));
121 return NULL; /* malformed */
122 GNUNET_asprintf(&box_str,
124 (unsigned int)ntohs(box.protocol),
125 (unsigned int)ntohs(box.service),
139 * Convert human-readable version of a 'value' of a record to the binary
142 * @param cls closure, unused
143 * @param type type of the record
144 * @param s human-readable string
145 * @param data set to value in binary encoding (will be allocated)
146 * @param data_size set to number of bytes in @a data
147 * @return #GNUNET_OK on success
150 gns_string_to_value(void *cls,
156 struct GNUNET_CRYPTO_EcdsaPublicKey pkey;
159 return GNUNET_SYSERR;
162 case GNUNET_GNSRECORD_TYPE_PKEY:
164 GNUNET_CRYPTO_ecdsa_public_key_from_string(s, strlen(s), &pkey))
166 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
167 _("Unable to parse PKEY record `%s'\n"),
169 return GNUNET_SYSERR;
171 *data = GNUNET_new(struct GNUNET_CRYPTO_EcdsaPublicKey);
172 GNUNET_memcpy(*data, &pkey, sizeof(pkey));
173 *data_size = sizeof(struct GNUNET_CRYPTO_EcdsaPublicKey);
176 case GNUNET_GNSRECORD_TYPE_NICK:
177 *data = GNUNET_strdup(s);
178 *data_size = strlen(s);
181 case GNUNET_GNSRECORD_TYPE_LEHO:
182 *data = GNUNET_strdup(s);
183 *data_size = strlen(s);
186 case GNUNET_GNSRECORD_TYPE_GNS2DNS: {
192 cpy = GNUNET_strdup(s);
193 at = strchr(cpy, '@');
196 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
197 _("Unable to parse GNS2DNS record `%s'\n"),
200 return GNUNET_SYSERR;
206 if ((GNUNET_OK != GNUNET_DNSPARSER_builder_add_name(nsbuf,
211 GNUNET_DNSPARSER_builder_add_name(nsbuf, sizeof(nsbuf), &off, at)))
213 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
214 _("Failed to serialize GNS2DNS record with value `%s'\n"),
217 return GNUNET_SYSERR;
221 *data = GNUNET_malloc(off);
222 GNUNET_memcpy(*data, nsbuf, off);
226 case GNUNET_GNSRECORD_TYPE_VPN: {
227 struct GNUNET_TUN_GnsVpnRecord *vpn;
228 char s_peer[103 + 1];
229 char s_serv[253 + 1];
232 if (3 != sscanf(s, "%u %103s %253s", &proto, s_peer, s_serv))
234 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
235 _("Unable to parse VPN record string `%s'\n"),
237 return GNUNET_SYSERR;
239 *data_size = sizeof(struct GNUNET_TUN_GnsVpnRecord) + strlen(s_serv) + 1;
240 *data = vpn = GNUNET_malloc(*data_size);
242 GNUNET_CRYPTO_eddsa_public_key_from_string((char *)s_peer,
244 &vpn->peer.public_key))
248 return GNUNET_SYSERR;
250 vpn->proto = htons((uint16_t)proto);
251 strcpy((char *)&vpn[1], s_serv);
255 case GNUNET_GNSRECORD_TYPE_BOX: {
256 struct GNUNET_GNSRECORD_BoxRecord *box;
258 unsigned int protocol;
259 unsigned int service;
260 unsigned int record_type;
264 if (3 != sscanf(s, "%u %u %u ", &protocol, &service, &record_type))
266 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
267 _("Unable to parse BOX record string `%s'\n"),
269 return GNUNET_SYSERR;
271 rest = snprintf(NULL, 0, "%u %u %u ", protocol, service, record_type);
272 if (GNUNET_OK != GNUNET_GNSRECORD_string_to_value(record_type,
276 return GNUNET_SYSERR;
277 *data_size = sizeof(struct GNUNET_GNSRECORD_BoxRecord) + bval_size;
278 *data = box = GNUNET_malloc(*data_size);
279 box->protocol = htons(protocol);
280 box->service = htons(service);
281 box->record_type = htonl(record_type);
282 GNUNET_memcpy(&box[1], bval, bval_size);
288 return GNUNET_SYSERR;
294 * Mapping of record type numbers to human-readable
300 } gns_name_map[] = { { "PKEY", GNUNET_GNSRECORD_TYPE_PKEY },
301 { "NICK", GNUNET_GNSRECORD_TYPE_NICK },
302 { "LEHO", GNUNET_GNSRECORD_TYPE_LEHO },
303 { "VPN", GNUNET_GNSRECORD_TYPE_VPN },
304 { "GNS2DNS", GNUNET_GNSRECORD_TYPE_GNS2DNS },
305 { "BOX", GNUNET_GNSRECORD_TYPE_BOX },
306 { NULL, UINT32_MAX } };
310 * Convert a type name (i.e. "AAAA") to the corresponding number.
312 * @param cls closure, unused
313 * @param gns_typename name to convert
314 * @return corresponding number, UINT32_MAX on error
317 gns_typename_to_number(void *cls, const char *gns_typename)
322 while ((NULL != gns_name_map[i].name) &&
323 (0 != strcasecmp(gns_typename, gns_name_map[i].name)))
325 return gns_name_map[i].number;
330 * Convert a type number (i.e. 1) to the corresponding type string (i.e. "A")
332 * @param cls closure, unused
333 * @param type number of a type to convert
334 * @return corresponding typestring, NULL on error
337 gns_number_to_typename(void *cls, uint32_t type)
342 while ((NULL != gns_name_map[i].name) && (type != gns_name_map[i].number))
344 return gns_name_map[i].name;
349 * Entry point for the plugin.
352 * @return the exported block API
355 libgnunet_plugin_gnsrecord_gns_init(void *cls)
357 struct GNUNET_GNSRECORD_PluginFunctions *api;
359 api = GNUNET_new(struct GNUNET_GNSRECORD_PluginFunctions);
360 api->value_to_string = &gns_value_to_string;
361 api->string_to_value = &gns_string_to_value;
362 api->typename_to_number = &gns_typename_to_number;
363 api->number_to_typename = &gns_number_to_typename;
369 * Exit point from the plugin.
371 * @param cls the return value from #libgnunet_plugin_block_test_init()
375 libgnunet_plugin_gnsrecord_gns_done(void *cls)
377 struct GNUNET_GNSRECORD_PluginFunctions *api = cls;
383 /* end of plugin_gnsrecord_gns.c */