2 This file is part of GNUnet
3 Copyright (C) 2013 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.
17 * @file conversation/plugin_gnsrecord_conversation.c
18 * @brief gnsrecord plugin to provide the API for fundamental GNS records
19 * This includes the VPN record because GNS resolution
20 * is expected to understand VPN records and (if needed)
21 * map the result to A/AAAA.
22 * @author Christian Grothoff
26 #include "gnunet_util_lib.h"
27 #include "gnunet_gnsrecord_lib.h"
28 #include "gnunet_conversation_service.h"
29 #include "gnunet_gnsrecord_plugin.h"
33 * Convert the 'value' of a record to a string.
35 * @param cls closure, unused
36 * @param type type of the record
37 * @param data value in binary encoding
38 * @param data_size number of bytes in @a data
39 * @return NULL on error, otherwise human-readable representation of the value
42 conversation_value_to_string (void *cls,
52 case GNUNET_GNSRECORD_TYPE_PHONE:
54 const struct GNUNET_CONVERSATION_PhoneRecord *pr;
58 if (data_size != sizeof (struct GNUNET_CONVERSATION_PhoneRecord))
64 if (1 != ntohl (pr->version))
66 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
67 _("PHONE version %u not supported\n"),
71 pkey = GNUNET_CRYPTO_eddsa_public_key_to_string (&pr->peer.public_key);
72 s = GNUNET_STRINGS_data_to_string_alloc (&pr->line_port,
73 sizeof (struct GNUNET_HashCode));
75 GNUNET_asprintf (&ret,
90 * Convert human-readable version of a 'value' of a record to the binary
93 * @param cls closure, unused
94 * @param type type of the record
95 * @param s human-readable string
96 * @param data set to value in binary encoding (will be allocated)
97 * @param data_size set to number of bytes in @a data
98 * @return #GNUNET_OK on success
101 conversation_string_to_value (void *cls,
111 return GNUNET_SYSERR;
115 case GNUNET_GNSRECORD_TYPE_PHONE:
117 struct GNUNET_CONVERSATION_PhoneRecord *pr;
120 struct GNUNET_PeerIdentity peer;
122 if ( (NULL == (dash = strchr (s, '-'))) ||
123 (1 != sscanf (s, "%103s-", line_port)) ||
125 GNUNET_CRYPTO_eddsa_public_key_from_string (dash + 1,
129 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
130 _("Unable to parse PHONE record `%s'\n"),
132 return GNUNET_SYSERR;
134 pr = GNUNET_new (struct GNUNET_CONVERSATION_PhoneRecord);
135 pr->version = htonl (1);
136 pr->reserved = htonl (0);
138 GNUNET_STRINGS_string_to_data (line_port,
141 sizeof (struct GNUNET_HashCode)))
143 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
144 _("Unable to parse PHONE record `%s'\n"),
147 return GNUNET_SYSERR;
151 *data_size = sizeof (struct GNUNET_CONVERSATION_PhoneRecord);
155 return GNUNET_SYSERR;
161 * Mapping of record type numbers to human-readable
168 { "PHONE", GNUNET_GNSRECORD_TYPE_PHONE },
174 * Convert a type name (i.e. "AAAA") to the corresponding number.
176 * @param cls closure, unused
177 * @param gns_typename name to convert
178 * @return corresponding number, UINT32_MAX on error
181 conversation_typename_to_number (void *cls,
182 const char *gns_typename)
188 while ( (name_map[i].name != NULL) &&
189 (0 != strcasecmp (gns_typename, name_map[i].name)) )
191 return name_map[i].number;
196 * Convert a type number (i.e. 1) to the corresponding type string (i.e. "A")
198 * @param cls closure, unused
199 * @param type number of a type to convert
200 * @return corresponding typestring, NULL on error
203 conversation_number_to_typename (void *cls,
210 while ( (name_map[i].name != NULL) &&
211 (type != name_map[i].number) )
213 return name_map[i].name;
218 * Entry point for the plugin.
221 * @return the exported block API
224 libgnunet_plugin_gnsrecord_conversation_init (void *cls)
226 struct GNUNET_GNSRECORD_PluginFunctions *api;
229 api = GNUNET_new (struct GNUNET_GNSRECORD_PluginFunctions);
230 api->value_to_string = &conversation_value_to_string;
231 api->string_to_value = &conversation_string_to_value;
232 api->typename_to_number = &conversation_typename_to_number;
233 api->number_to_typename = &conversation_number_to_typename;
239 * Exit point from the plugin.
241 * @param cls the return value from #libgnunet_plugin_block_test_init
245 libgnunet_plugin_gnsrecord_conversation_done (void *cls)
247 struct GNUNET_GNSRECORD_PluginFunctions *api = cls;
253 /* end of plugin_gnsrecord_conversation.c */