2 This file is part of GNUnet
3 (C) 2013 Christian Grothoff (and other contributing authors)
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., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, 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
31 #include "gnunet_util_lib.h"
32 #include "gnunet_gnsrecord_lib.h"
33 #include "gnunet_dnsparser_lib.h"
34 #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 const struct GNUNET_TUN_GnsVpnRecord *vpn;
101 if ( (data_size <= sizeof (struct GNUNET_TUN_GnsVpnRecord)) ||
102 ('\0' != cdata[data_size - 1]) )
103 return NULL; /* malformed */
105 GNUNET_asprintf (&vpn_str, "%u %s %s",
106 (unsigned int) ntohs (vpn->proto),
107 (const char*) GNUNET_i2s_full (&vpn->peer),
108 (const char*) &vpn[1]);
111 case GNUNET_GNSRECORD_TYPE_BOX:
112 /* FIXME: to be implemented! */
122 * Convert human-readable version of a 'value' of a record to the binary
125 * @param cls closure, unused
126 * @param type type of the record
127 * @param s human-readable string
128 * @param data set to value in binary encoding (will be allocated)
129 * @param data_size set to number of bytes in @a data
130 * @return #GNUNET_OK on success
133 gns_string_to_value (void *cls,
139 struct GNUNET_CRYPTO_EcdsaPublicKey pkey;
140 struct GNUNET_TUN_GnsVpnRecord *vpn;
141 char s_peer[103 + 1];
142 char s_serv[253 + 1];
146 return GNUNET_SYSERR;
150 case GNUNET_GNSRECORD_TYPE_PKEY:
152 GNUNET_CRYPTO_ecdsa_public_key_from_string (s, strlen (s), &pkey))
154 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
155 _("Unable to parse PKEY record `%s'\n"),
157 return GNUNET_SYSERR;
159 *data = GNUNET_new (struct GNUNET_CRYPTO_EcdsaPublicKey);
160 memcpy (*data, &pkey, sizeof (pkey));
161 *data_size = sizeof (struct GNUNET_CRYPTO_EcdsaPublicKey);
164 case GNUNET_GNSRECORD_TYPE_NICK:
165 *data = GNUNET_strdup (s);
166 *data_size = strlen (s);
168 case GNUNET_GNSRECORD_TYPE_LEHO:
169 *data = GNUNET_strdup (s);
170 *data_size = strlen (s);
172 case GNUNET_GNSRECORD_TYPE_GNS2DNS:
179 cpy = GNUNET_strdup (s);
180 at = strchr (cpy, '@');
183 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
184 _("Unable to parse GNS2DNS record `%s'\n"),
187 return GNUNET_SYSERR;
194 GNUNET_DNSPARSER_builder_add_name (nsbuf,
199 GNUNET_DNSPARSER_builder_add_name (nsbuf,
204 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
205 _("Failed to serialize GNS2DNS record with value `%s'\n"),
208 return GNUNET_SYSERR;
212 *data = GNUNET_malloc (off);
213 memcpy (*data, nsbuf, off);
216 case GNUNET_GNSRECORD_TYPE_VPN:
217 if (3 != SSCANF (s,"%u %103s %253s",
218 &proto, s_peer, s_serv))
220 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
221 _("Unable to parse VPN record string `%s'\n"),
223 return GNUNET_SYSERR;
225 *data_size = sizeof (struct GNUNET_TUN_GnsVpnRecord) + strlen (s_serv) + 1;
226 *data = vpn = GNUNET_malloc (*data_size);
227 if (GNUNET_OK != GNUNET_CRYPTO_eddsa_public_key_from_string ((char*) s_peer,
229 &vpn->peer.public_key))
233 return GNUNET_SYSERR;
235 vpn->proto = htons ((uint16_t) proto);
236 strcpy ((char*)&vpn[1], s_serv);
238 case GNUNET_GNSRECORD_TYPE_BOX:
239 /* FIXME: to be implemented! */
241 return GNUNET_SYSERR;
243 return GNUNET_SYSERR;
249 * Mapping of record type numbers to human-readable
256 { "PKEY", GNUNET_GNSRECORD_TYPE_PKEY },
257 { "NICK", GNUNET_GNSRECORD_TYPE_NICK },
258 { "LEHO", GNUNET_GNSRECORD_TYPE_LEHO },
259 { "VPN", GNUNET_GNSRECORD_TYPE_VPN },
260 { "GNS2DNS", GNUNET_GNSRECORD_TYPE_GNS2DNS },
261 { "BOX", GNUNET_GNSRECORD_TYPE_BOX },
267 * Convert a type name (i.e. "AAAA") to the corresponding number.
269 * @param cls closure, unused
270 * @param gns_typename name to convert
271 * @return corresponding number, UINT32_MAX on error
274 gns_typename_to_number (void *cls,
275 const char *gns_typename)
280 while ( (gns_name_map[i].name != NULL) &&
281 (0 != strcasecmp (gns_typename, gns_name_map[i].name)) )
283 return gns_name_map[i].number;
288 * Convert a type number (i.e. 1) to the corresponding type string (i.e. "A")
290 * @param cls closure, unused
291 * @param type number of a type to convert
292 * @return corresponding typestring, NULL on error
295 gns_number_to_typename (void *cls,
301 while ( (gns_name_map[i].name != NULL) &&
302 (type != gns_name_map[i].number) )
304 return gns_name_map[i].name;
309 * Entry point for the plugin.
312 * @return the exported block API
315 libgnunet_plugin_gnsrecord_gns_init (void *cls)
317 struct GNUNET_GNSRECORD_PluginFunctions *api;
319 api = GNUNET_new (struct GNUNET_GNSRECORD_PluginFunctions);
320 api->value_to_string = &gns_value_to_string;
321 api->string_to_value = &gns_string_to_value;
322 api->typename_to_number = &gns_typename_to_number;
323 api->number_to_typename = &gns_number_to_typename;
329 * Exit point from the plugin.
331 * @param cls the return value from #libgnunet_plugin_block_test_init
335 libgnunet_plugin_gnsrecord_gns_done (void *cls)
337 struct GNUNET_GNSRECORD_PluginFunctions *api = cls;
343 /* end of plugin_gnsrecord_gns.c */