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.
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 abd/plugin_gnsrecord_abd.c
23 * @brief gnsrecord plugin to provide the API for ABD records
24 * @author Martin Schanzenbach
28 #include "gnunet_util_lib.h"
30 #include "delegate_misc.h"
31 #include "abd_serialization.h"
32 #include "gnunet_abd_service.h"
33 #include "gnunet_gnsrecord_lib.h"
34 #include "gnunet_gnsrecord_plugin.h"
35 #include "gnunet_signatures.h"
37 * Convert the 'value' of a record to a string.
39 * @param cls closure, unused
40 * @param type type of the record
41 * @param data value in binary encoding
42 * @param data_size number of bytes in @a data
43 * @return NULL on error, otherwise human-readable representation of the value
46 abd_value_to_string (void *cls,
55 case GNUNET_GNSRECORD_TYPE_ATTRIBUTE:
57 struct GNUNET_ABD_DelegationRecord sets;
62 if (data_size < sizeof (struct GNUNET_ABD_DelegationRecord))
63 return NULL; /* malformed */
65 GNUNET_memcpy (&sets, data, sizeof (sets));
68 struct GNUNET_ABD_DelegationSet set[ntohl (sets.set_count)];
70 GNUNET_ABD_delegation_set_deserialize (GNUNET_ntohll (
72 &cdata[sizeof (sets)],
73 ntohl (sets.set_count),
77 for (i = 0; i < ntohl (sets.set_count); i++)
80 GNUNET_CRYPTO_ecdsa_public_key_to_string (&set[i].subject_key);
82 if (0 == set[i].subject_attribute_len)
86 GNUNET_asprintf (&attr_str, "%s", subject_pkey);
90 GNUNET_asprintf (&tmp_str, "%s,%s", attr_str, subject_pkey);
91 GNUNET_free (attr_str);
99 GNUNET_asprintf (&attr_str,
102 set[i].subject_attribute);
106 GNUNET_asprintf (&tmp_str,
110 set[i].subject_attribute);
111 GNUNET_free (attr_str);
115 GNUNET_free (subject_pkey);
119 case GNUNET_GNSRECORD_TYPE_DELEGATE:
121 struct GNUNET_ABD_Delegate *cred;
124 cred = GNUNET_ABD_delegate_deserialize (data, data_size);
125 cred_str = GNUNET_ABD_delegate_to_string (cred);
136 * Convert human-readable version of a 'value' of a record to the binary
139 * @param cls closure, unused
140 * @param type type of the record
141 * @param s human-readable string
142 * @param data set to value in binary encoding (will be allocated)
143 * @param data_size set to number of bytes in @a data
144 * @return #GNUNET_OK on success
147 abd_string_to_value (void *cls,
154 return GNUNET_SYSERR;
157 case GNUNET_GNSRECORD_TYPE_ATTRIBUTE:
159 struct GNUNET_ABD_DelegationRecord *sets;
160 char attr_str[253 + 1];
161 char subject_pkey[52 + 1];
166 size_t tmp_data_size;
169 tmp_str = GNUNET_strdup (s);
170 token = strtok (tmp_str, ",");
173 *data_size = sizeof (struct GNUNET_ABD_DelegationRecord);
174 while (NULL != token)
176 // also fills the variables subject_pley and attr_str if "regex"-like match
177 matches = sscanf (token, "%s %s", subject_pkey, attr_str);
181 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
182 _ ("Unable to parse ATTR record string `%s'\n"),
184 GNUNET_free (tmp_str);
185 return GNUNET_SYSERR;
189 token = strtok (NULL, ",");
191 GNUNET_free (tmp_str);
193 tmp_str = GNUNET_strdup (s);
194 token = strtok (tmp_str, ",");
197 GNUNET_free (tmp_str);
198 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Malformed string %s\n", s);
199 return GNUNET_SYSERR;
202 struct GNUNET_ABD_DelegationSet set[entries];
203 // sets memory to be 0, starting at *set for the size of struct * entries
204 memset (set, 0, sizeof (struct GNUNET_ABD_DelegationSet) * entries);
205 for (i = 0; i < entries; i++)
207 matches = sscanf (token, "%s %s", subject_pkey, attr_str);
209 // sets the public key for the set entry
210 GNUNET_CRYPTO_ecdsa_public_key_from_string (subject_pkey,
211 strlen (subject_pkey),
212 &set[i].subject_key);
214 // If not just key, also set subject attribute (Not A.a <- B but A.a <- B.b)
217 set[i].subject_attribute_len = strlen (attr_str) + 1;
218 set[i].subject_attribute = GNUNET_strdup (attr_str);
220 // If more entries, then token string can take the next entry (separated by ',') by calling strtok again
221 token = strtok (NULL, ",");
223 tmp_data_size = GNUNET_ABD_delegation_set_get_size (entries, set);
225 if (-1 == tmp_data_size)
227 GNUNET_free (tmp_str);
228 return GNUNET_SYSERR;
230 *data_size += tmp_data_size;
231 *data = sets = GNUNET_malloc (*data_size);
232 GNUNET_ABD_delegation_set_serialize (entries,
236 for (i = 0; i < entries; i++)
238 if (0 != set[i].subject_attribute_len)
239 GNUNET_free ((char *) set[i].subject_attribute);
241 sets->set_count = htonl (entries);
242 sets->data_size = GNUNET_htonll (tmp_data_size);
244 GNUNET_free (tmp_str);
247 case GNUNET_GNSRECORD_TYPE_DELEGATE:
249 struct GNUNET_ABD_Delegate *cred;
250 cred = GNUNET_ABD_delegate_from_string (s);
252 *data_size = GNUNET_ABD_delegate_serialize (cred, (char **) data);
257 return GNUNET_SYSERR;
263 * Mapping of record type numbers to human-readable
270 } name_map[] = {{"ATTR", GNUNET_GNSRECORD_TYPE_ATTRIBUTE},
271 {"DEL", GNUNET_GNSRECORD_TYPE_DELEGATE},
276 * Convert a type name (i.e. "AAAA") to the corresponding number.
278 * @param cls closure, unused
279 * @param gns_typename name to convert
280 * @return corresponding number, UINT32_MAX on error
283 abd_typename_to_number (void *cls, const char *gns_typename)
288 while ((name_map[i].name != NULL) &&
289 (0 != strcasecmp (gns_typename, name_map[i].name)))
291 return name_map[i].number;
296 * Convert a type number (i.e. 1) to the corresponding type string (i.e. "A")
298 * @param cls closure, unused
299 * @param type number of a type to convert
300 * @return corresponding typestring, NULL on error
303 abd_number_to_typename (void *cls, uint32_t type)
308 while ((name_map[i].name != NULL) && (type != name_map[i].number))
310 return name_map[i].name;
315 * Entry point for the plugin.
318 * @return the exported block API
321 libgnunet_plugin_gnsrecord_abd_init (void *cls)
323 struct GNUNET_GNSRECORD_PluginFunctions *api;
325 api = GNUNET_new (struct GNUNET_GNSRECORD_PluginFunctions);
326 api->value_to_string = &abd_value_to_string;
327 api->string_to_value = &abd_string_to_value;
328 api->typename_to_number = &abd_typename_to_number;
329 api->number_to_typename = &abd_number_to_typename;
335 * Exit point from the plugin.
337 * @param cls the return value from #libgnunet_plugin_block_test_init
341 libgnunet_plugin_gnsrecord_abd_done (void *cls)
343 struct GNUNET_GNSRECORD_PluginFunctions *api = cls;
350 /* end of plugin_gnsrecord_abd.c */