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_PSEU:
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:
70 ns = GNUNET_DNSPARSER_parse_name (data,
77 GNUNET_free_non_null (ns);
82 case GNUNET_GNSRECORD_TYPE_VPN:
84 const struct GNUNET_TUN_GnsVpnRecord *vpn;
88 if ( (data_size <= sizeof (struct GNUNET_TUN_GnsVpnRecord)) ||
89 ('\0' != cdata[data_size - 1]) )
90 return NULL; /* malformed */
92 if (0 == GNUNET_asprintf (&vpn_str, "%u %s %s",
93 (unsigned int) ntohs (vpn->proto),
94 (const char*) GNUNET_i2s_full (&vpn->peer),
95 (const char*) &vpn[1]))
97 GNUNET_free (vpn_str);
109 * Convert human-readable version of a 'value' of a record to the binary
112 * @param cls closure, unused
113 * @param type type of the record
114 * @param s human-readable string
115 * @param data set to value in binary encoding (will be allocated)
116 * @param data_size set to number of bytes in @a data
117 * @return #GNUNET_OK on success
120 gns_string_to_value (void *cls,
126 struct GNUNET_CRYPTO_EcdsaPublicKey pkey;
127 struct GNUNET_TUN_GnsVpnRecord *vpn;
128 char s_peer[103 + 1];
129 char s_serv[253 + 1];
133 return GNUNET_SYSERR;
137 case GNUNET_GNSRECORD_TYPE_PKEY:
139 GNUNET_CRYPTO_ecdsa_public_key_from_string (s, strlen (s), &pkey))
141 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
142 _("Unable to parse PKEY record `%s'\n"),
144 return GNUNET_SYSERR;
146 *data = GNUNET_new (struct GNUNET_CRYPTO_EcdsaPublicKey);
147 memcpy (*data, &pkey, sizeof (pkey));
148 *data_size = sizeof (struct GNUNET_CRYPTO_EcdsaPublicKey);
151 case GNUNET_GNSRECORD_TYPE_PSEU:
152 *data = GNUNET_strdup (s);
153 *data_size = strlen (s);
155 case GNUNET_GNSRECORD_TYPE_LEHO:
156 *data = GNUNET_strdup (s);
157 *data_size = strlen (s);
159 case GNUNET_GNSRECORD_TYPE_GNS2DNS:
166 GNUNET_DNSPARSER_builder_add_name (nsbuf,
171 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
172 _("Failed to serialize GNS2DNS record with value `%s'\n"),
174 return GNUNET_SYSERR;
177 *data = GNUNET_malloc (off);
178 memcpy (*data, nsbuf, off);
181 case GNUNET_GNSRECORD_TYPE_VPN:
182 if (3 != SSCANF (s,"%u %103s %253s",
183 &proto, s_peer, s_serv))
185 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
186 _("Unable to parse VPN record string `%s'\n"),
188 return GNUNET_SYSERR;
190 *data_size = sizeof (struct GNUNET_TUN_GnsVpnRecord) + strlen (s_serv) + 1;
191 *data = vpn = GNUNET_malloc (*data_size);
192 if (GNUNET_OK != GNUNET_CRYPTO_eddsa_public_key_from_string ((char*) s_peer,
194 &vpn->peer.public_key))
198 return GNUNET_SYSERR;
200 vpn->proto = htons ((uint16_t) proto);
201 strcpy ((char*)&vpn[1], s_serv);
204 return GNUNET_SYSERR;
210 * Mapping of record type numbers to human-readable
217 { "PKEY", GNUNET_GNSRECORD_TYPE_PKEY },
218 { "PSEU", GNUNET_GNSRECORD_TYPE_PSEU },
219 { "LEHO", GNUNET_GNSRECORD_TYPE_LEHO },
220 { "VPN", GNUNET_GNSRECORD_TYPE_VPN },
221 { "GNS2DNS", GNUNET_GNSRECORD_TYPE_GNS2DNS },
227 * Convert a type name (i.e. "AAAA") to the corresponding number.
229 * @param cls closure, unused
230 * @param gns_typename name to convert
231 * @return corresponding number, UINT32_MAX on error
234 gns_typename_to_number (void *cls,
235 const char *gns_typename)
240 while ( (name_map[i].name != NULL) &&
241 (0 != strcasecmp (gns_typename, name_map[i].name)) )
243 return name_map[i].number;
248 * Convert a type number (i.e. 1) to the corresponding type string (i.e. "A")
250 * @param cls closure, unused
251 * @param type number of a type to convert
252 * @return corresponding typestring, NULL on error
255 gns_number_to_typename (void *cls,
261 while ( (name_map[i].name != NULL) &&
262 (type != name_map[i].number) )
264 return name_map[i].name;
269 * Entry point for the plugin.
272 * @return the exported block API
275 libgnunet_plugin_gnsrecord_gns_init (void *cls)
277 struct GNUNET_GNSRECORD_PluginFunctions *api;
279 api = GNUNET_new (struct GNUNET_GNSRECORD_PluginFunctions);
280 api->value_to_string = &gns_value_to_string;
281 api->string_to_value = &gns_string_to_value;
282 api->typename_to_number = &gns_typename_to_number;
283 api->number_to_typename = &gns_number_to_typename;
289 * Exit point from the plugin.
291 * @param cls the return value from #libgnunet_plugin_block_test_init
295 libgnunet_plugin_gnsrecord_gns_done (void *cls)
297 struct GNUNET_GNSRECORD_PluginFunctions *api = cls;
303 /* end of plugin_gnsrecord_gns.c */