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]);
118 * Convert human-readable version of a 'value' of a record to the binary
121 * @param cls closure, unused
122 * @param type type of the record
123 * @param s human-readable string
124 * @param data set to value in binary encoding (will be allocated)
125 * @param data_size set to number of bytes in @a data
126 * @return #GNUNET_OK on success
129 gns_string_to_value (void *cls,
135 struct GNUNET_CRYPTO_EcdsaPublicKey pkey;
136 struct GNUNET_TUN_GnsVpnRecord *vpn;
137 char s_peer[103 + 1];
138 char s_serv[253 + 1];
142 return GNUNET_SYSERR;
146 case GNUNET_GNSRECORD_TYPE_PKEY:
148 GNUNET_CRYPTO_ecdsa_public_key_from_string (s, strlen (s), &pkey))
150 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
151 _("Unable to parse PKEY record `%s'\n"),
153 return GNUNET_SYSERR;
155 *data = GNUNET_new (struct GNUNET_CRYPTO_EcdsaPublicKey);
156 memcpy (*data, &pkey, sizeof (pkey));
157 *data_size = sizeof (struct GNUNET_CRYPTO_EcdsaPublicKey);
160 case GNUNET_GNSRECORD_TYPE_NICK:
161 *data = GNUNET_strdup (s);
162 *data_size = strlen (s);
164 case GNUNET_GNSRECORD_TYPE_LEHO:
165 *data = GNUNET_strdup (s);
166 *data_size = strlen (s);
168 case GNUNET_GNSRECORD_TYPE_GNS2DNS:
175 cpy = GNUNET_strdup (s);
176 at = strchr (cpy, '@');
179 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
180 _("Unable to parse GNS2DNS record `%s'\n"),
183 return GNUNET_SYSERR;
190 GNUNET_DNSPARSER_builder_add_name (nsbuf,
195 GNUNET_DNSPARSER_builder_add_name (nsbuf,
200 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
201 _("Failed to serialize GNS2DNS record with value `%s'\n"),
204 return GNUNET_SYSERR;
208 *data = GNUNET_malloc (off);
209 memcpy (*data, nsbuf, off);
212 case GNUNET_GNSRECORD_TYPE_VPN:
213 if (3 != SSCANF (s,"%u %103s %253s",
214 &proto, s_peer, s_serv))
216 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
217 _("Unable to parse VPN record string `%s'\n"),
219 return GNUNET_SYSERR;
221 *data_size = sizeof (struct GNUNET_TUN_GnsVpnRecord) + strlen (s_serv) + 1;
222 *data = vpn = GNUNET_malloc (*data_size);
223 if (GNUNET_OK != GNUNET_CRYPTO_eddsa_public_key_from_string ((char*) s_peer,
225 &vpn->peer.public_key))
229 return GNUNET_SYSERR;
231 vpn->proto = htons ((uint16_t) proto);
232 strcpy ((char*)&vpn[1], s_serv);
235 return GNUNET_SYSERR;
241 * Mapping of record type numbers to human-readable
248 { "PKEY", GNUNET_GNSRECORD_TYPE_PKEY },
249 { "NICK", GNUNET_GNSRECORD_TYPE_NICK },
250 { "LEHO", GNUNET_GNSRECORD_TYPE_LEHO },
251 { "VPN", GNUNET_GNSRECORD_TYPE_VPN },
252 { "GNS2DNS", GNUNET_GNSRECORD_TYPE_GNS2DNS },
258 * Convert a type name (i.e. "AAAA") to the corresponding number.
260 * @param cls closure, unused
261 * @param gns_typename name to convert
262 * @return corresponding number, UINT32_MAX on error
265 gns_typename_to_number (void *cls,
266 const char *gns_typename)
271 while ( (name_map[i].name != NULL) &&
272 (0 != strcasecmp (gns_typename, name_map[i].name)) )
274 return name_map[i].number;
279 * Convert a type number (i.e. 1) to the corresponding type string (i.e. "A")
281 * @param cls closure, unused
282 * @param type number of a type to convert
283 * @return corresponding typestring, NULL on error
286 gns_number_to_typename (void *cls,
292 while ( (name_map[i].name != NULL) &&
293 (type != name_map[i].number) )
295 return name_map[i].name;
300 * Entry point for the plugin.
303 * @return the exported block API
306 libgnunet_plugin_gnsrecord_gns_init (void *cls)
308 struct GNUNET_GNSRECORD_PluginFunctions *api;
310 api = GNUNET_new (struct GNUNET_GNSRECORD_PluginFunctions);
311 api->value_to_string = &gns_value_to_string;
312 api->string_to_value = &gns_string_to_value;
313 api->typename_to_number = &gns_typename_to_number;
314 api->number_to_typename = &gns_number_to_typename;
320 * Exit point from the plugin.
322 * @param cls the return value from #libgnunet_plugin_block_test_init
326 libgnunet_plugin_gnsrecord_gns_done (void *cls)
328 struct GNUNET_GNSRECORD_PluginFunctions *api = cls;
334 /* end of plugin_gnsrecord_gns.c */