diff options
Diffstat (limited to 'src/escrow/plugin_gnsrecord_escrow.c')
-rw-r--r-- | src/escrow/plugin_gnsrecord_escrow.c | 173 |
1 files changed, 173 insertions, 0 deletions
diff --git a/src/escrow/plugin_gnsrecord_escrow.c b/src/escrow/plugin_gnsrecord_escrow.c new file mode 100644 index 000000000..82ed3af96 --- /dev/null +++ b/src/escrow/plugin_gnsrecord_escrow.c | |||
@@ -0,0 +1,173 @@ | |||
1 | /* | ||
2 | This file is part of GNUnet | ||
3 | Copyright (C) 2020 GNUnet e.V. | ||
4 | |||
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. | ||
9 | |||
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. | ||
14 | |||
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/>. | ||
17 | |||
18 | SPDX-License-Identifier: AGPL3.0-or-later | ||
19 | */ | ||
20 | |||
21 | /** | ||
22 | * @file escrow/plugin_gnsrecord_escrow.c | ||
23 | * @brief gnsrecord plugin to provide the API for escrow records | ||
24 | * | ||
25 | * @author Johannes Späth | ||
26 | */ | ||
27 | |||
28 | #include "platform.h" | ||
29 | #include "gnunet_util_lib.h" | ||
30 | |||
31 | #include "gnunet_gnsrecord_lib.h" | ||
32 | #include "gnunet_gnsrecord_plugin.h" | ||
33 | |||
34 | /** | ||
35 | * Convert the 'value' of a record to a string. | ||
36 | * | ||
37 | * @param cls closure, unused | ||
38 | * @param type type of the record | ||
39 | * @param data value in binary encoding | ||
40 | * @param data_size number of bytes in @a data | ||
41 | * @return NULL on error, otherwise human-readable representation of the value | ||
42 | */ | ||
43 | static char * | ||
44 | value_to_string (void *cls, uint32_t type, const void *data, size_t data_size) | ||
45 | { | ||
46 | switch (type) | ||
47 | { | ||
48 | case GNUNET_GNSRECORD_TYPE_ESCROW_KEYSHARE: | ||
49 | return GNUNET_STRINGS_data_to_string_alloc (data, data_size); | ||
50 | |||
51 | default: | ||
52 | return NULL; | ||
53 | } | ||
54 | } | ||
55 | |||
56 | /** | ||
57 | * Convert human-readable version of a 'value' of a record to the binary | ||
58 | * representation. | ||
59 | * | ||
60 | * @param cls closure, unused | ||
61 | * @param type type of the record | ||
62 | * @param s human-readable string | ||
63 | * @param data set to value in binary encoding (will be allocated) | ||
64 | * @param data_size set to number of bytes in @a data | ||
65 | * @return #GNUNET_OK on success | ||
66 | */ | ||
67 | static int | ||
68 | string_to_value (void *cls, uint32_t type, const char *s, void **data, | ||
69 | size_t *data_size) | ||
70 | { | ||
71 | if (NULL == s) | ||
72 | return GNUNET_SYSERR; | ||
73 | switch (type) | ||
74 | { | ||
75 | case GNUNET_GNSRECORD_TYPE_ESCROW_KEYSHARE: | ||
76 | return GNUNET_STRINGS_string_to_data (s, strlen (s), *data, *data_size); | ||
77 | |||
78 | default: | ||
79 | return GNUNET_SYSERR; | ||
80 | } | ||
81 | } | ||
82 | |||
83 | |||
84 | /** | ||
85 | * Mapping of record type numbers to human-readable | ||
86 | * record type names. | ||
87 | */ | ||
88 | static struct | ||
89 | { | ||
90 | const char *name; | ||
91 | uint32_t number; | ||
92 | } name_map[] = { | ||
93 | { "GNUNET_GNSRECORD_TYPE_ESCROW_KEYSHARE", GNUNET_GNSRECORD_TYPE_ESCROW_KEYSHARE }, | ||
94 | { NULL, UINT32_MAX } | ||
95 | }; | ||
96 | |||
97 | |||
98 | /** | ||
99 | * Convert a type name (i.e. "AAAA") to the corresponding number. | ||
100 | * | ||
101 | * @param cls closure, unused | ||
102 | * @param dns_typename name to convert | ||
103 | * @return corresponding number, UINT32_MAX on error | ||
104 | */ | ||
105 | static uint32_t | ||
106 | typename_to_number (void *cls, const char *dns_typename) | ||
107 | { | ||
108 | unsigned int i; | ||
109 | |||
110 | i = 0; | ||
111 | while ((NULL != name_map[i].name) && | ||
112 | (0 != strcasecmp (dns_typename, name_map[i].name))) | ||
113 | i++; | ||
114 | return name_map[i].number; | ||
115 | } | ||
116 | |||
117 | |||
118 | /** | ||
119 | * Convert a type number (i.e. 1) to the corresponding type string (i.e. "A") | ||
120 | * | ||
121 | * @param cls closure, unused | ||
122 | * @param type number of a type to convert | ||
123 | * @return corresponding typestring, NULL on error | ||
124 | */ | ||
125 | static const char * | ||
126 | number_to_typename (void *cls, uint32_t type) | ||
127 | { | ||
128 | unsigned int i; | ||
129 | |||
130 | i = 0; | ||
131 | while ((NULL != name_map[i].name) && (type != name_map[i].number)) | ||
132 | i++; | ||
133 | return name_map[i].name; | ||
134 | } | ||
135 | |||
136 | |||
137 | /** | ||
138 | * Entry point for the plugin. | ||
139 | * | ||
140 | * @param cls NULL | ||
141 | * @return the exported block API | ||
142 | */ | ||
143 | void * | ||
144 | libgnunet_plugin_gnsrecord_escrow_init (void *cls) | ||
145 | { | ||
146 | struct GNUNET_GNSRECORD_PluginFunctions *api; | ||
147 | |||
148 | api = GNUNET_new (struct GNUNET_GNSRECORD_PluginFunctions); | ||
149 | api->value_to_string = &value_to_string; | ||
150 | api->string_to_value = &string_to_value; | ||
151 | api->typename_to_number = &typename_to_number; | ||
152 | api->number_to_typename = &number_to_typename; | ||
153 | return api; | ||
154 | } | ||
155 | |||
156 | |||
157 | /** | ||
158 | * Exit point from the plugin. | ||
159 | * | ||
160 | * @param cls the return value from #libgnunet_plugin_block_test_init | ||
161 | * @return NULL | ||
162 | */ | ||
163 | void * | ||
164 | libgnunet_plugin_gnsrecord_escrow_done (void *cls) | ||
165 | { | ||
166 | struct GNUNET_GNSRECORD_PluginFunctions *api = cls; | ||
167 | |||
168 | GNUNET_free (api); | ||
169 | return NULL; | ||
170 | } | ||
171 | |||
172 | |||
173 | /* end of plugin_gnsrecord_escrow.c */ | ||