diff options
Diffstat (limited to 'src/contrib/service/conversation/plugin_gnsrecord_conversation.c')
-rw-r--r-- | src/contrib/service/conversation/plugin_gnsrecord_conversation.c | 262 |
1 files changed, 262 insertions, 0 deletions
diff --git a/src/contrib/service/conversation/plugin_gnsrecord_conversation.c b/src/contrib/service/conversation/plugin_gnsrecord_conversation.c new file mode 100644 index 000000000..786711c8e --- /dev/null +++ b/src/contrib/service/conversation/plugin_gnsrecord_conversation.c | |||
@@ -0,0 +1,262 @@ | |||
1 | /* | ||
2 | This file is part of GNUnet | ||
3 | Copyright (C) 2013 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 conversation/plugin_gnsrecord_conversation.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 | ||
28 | */ | ||
29 | |||
30 | #include "platform.h" | ||
31 | #include "gnunet_util_lib.h" | ||
32 | #include "gnunet_gnsrecord_lib.h" | ||
33 | #include "gnunet_conversation_service.h" | ||
34 | #include "gnunet_gnsrecord_plugin.h" | ||
35 | |||
36 | |||
37 | /** | ||
38 | * Convert the 'value' of a record to a string. | ||
39 | * | ||
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 | ||
45 | */ | ||
46 | static char * | ||
47 | conversation_value_to_string (void *cls, | ||
48 | uint32_t type, | ||
49 | const void *data, | ||
50 | size_t data_size) | ||
51 | { | ||
52 | char *s; | ||
53 | |||
54 | (void) cls; | ||
55 | switch (type) | ||
56 | { | ||
57 | case GNUNET_GNSRECORD_TYPE_PHONE: | ||
58 | { | ||
59 | const struct GNUNET_CONVERSATION_PhoneRecord *pr; | ||
60 | char *ret; | ||
61 | char *pkey; | ||
62 | |||
63 | if (data_size != sizeof(struct GNUNET_CONVERSATION_PhoneRecord)) | ||
64 | { | ||
65 | GNUNET_break_op (0); | ||
66 | return NULL; | ||
67 | } | ||
68 | pr = data; | ||
69 | if (1 != ntohl (pr->version)) | ||
70 | { | ||
71 | GNUNET_log (GNUNET_ERROR_TYPE_ERROR, | ||
72 | _ ("PHONE version %u not supported\n"), | ||
73 | ntohl (pr->version)); | ||
74 | return NULL; | ||
75 | } | ||
76 | pkey = GNUNET_CRYPTO_eddsa_public_key_to_string (&pr->peer.public_key); | ||
77 | s = GNUNET_STRINGS_data_to_string_alloc (&pr->line_port, | ||
78 | sizeof(struct GNUNET_HashCode)); | ||
79 | |||
80 | GNUNET_asprintf (&ret, | ||
81 | "%s-%s", | ||
82 | s, | ||
83 | pkey); | ||
84 | GNUNET_free (s); | ||
85 | GNUNET_free (pkey); | ||
86 | return ret; | ||
87 | } | ||
88 | |||
89 | default: | ||
90 | return NULL; | ||
91 | } | ||
92 | } | ||
93 | |||
94 | |||
95 | /** | ||
96 | * Convert human-readable version of a 'value' of a record to the binary | ||
97 | * representation. | ||
98 | * | ||
99 | * @param cls closure, unused | ||
100 | * @param type type of the record | ||
101 | * @param s human-readable string | ||
102 | * @param data set to value in binary encoding (will be allocated) | ||
103 | * @param data_size set to number of bytes in @a data | ||
104 | * @return #GNUNET_OK on success | ||
105 | */ | ||
106 | static int | ||
107 | conversation_string_to_value (void *cls, | ||
108 | uint32_t type, | ||
109 | const char *s, | ||
110 | void **data, | ||
111 | size_t *data_size) | ||
112 | { | ||
113 | (void) cls; | ||
114 | if (NULL == s) | ||
115 | { | ||
116 | GNUNET_break (0); | ||
117 | return GNUNET_SYSERR; | ||
118 | } | ||
119 | switch (type) | ||
120 | { | ||
121 | case GNUNET_GNSRECORD_TYPE_PHONE: | ||
122 | { | ||
123 | struct GNUNET_CONVERSATION_PhoneRecord *pr; | ||
124 | char line_port[103]; | ||
125 | const char *dash; | ||
126 | struct GNUNET_PeerIdentity peer; | ||
127 | |||
128 | if ((NULL == (dash = strchr (s, '-'))) || | ||
129 | (1 != sscanf (s, "%103s-", line_port)) || | ||
130 | (GNUNET_OK != | ||
131 | GNUNET_CRYPTO_eddsa_public_key_from_string (dash + 1, | ||
132 | strlen (dash + 1), | ||
133 | &peer.public_key))) | ||
134 | { | ||
135 | GNUNET_log (GNUNET_ERROR_TYPE_ERROR, | ||
136 | _ ("Unable to parse PHONE record `%s'\n"), | ||
137 | s); | ||
138 | return GNUNET_SYSERR; | ||
139 | } | ||
140 | pr = GNUNET_new (struct GNUNET_CONVERSATION_PhoneRecord); | ||
141 | pr->version = htonl (1); | ||
142 | pr->reserved = htonl (0); | ||
143 | if (GNUNET_OK != | ||
144 | GNUNET_STRINGS_string_to_data (line_port, | ||
145 | strlen (line_port), | ||
146 | &pr->line_port, | ||
147 | sizeof(struct GNUNET_HashCode))) | ||
148 | { | ||
149 | GNUNET_log (GNUNET_ERROR_TYPE_ERROR, | ||
150 | _ ("Unable to parse PHONE record `%s'\n"), | ||
151 | s); | ||
152 | GNUNET_free (pr); | ||
153 | return GNUNET_SYSERR; | ||
154 | } | ||
155 | pr->peer = peer; | ||
156 | *data = pr; | ||
157 | *data_size = sizeof(struct GNUNET_CONVERSATION_PhoneRecord); | ||
158 | return GNUNET_OK; | ||
159 | } | ||
160 | |||
161 | default: | ||
162 | return GNUNET_SYSERR; | ||
163 | } | ||
164 | } | ||
165 | |||
166 | |||
167 | /** | ||
168 | * Mapping of record type numbers to human-readable | ||
169 | * record type names. | ||
170 | */ | ||
171 | static struct | ||
172 | { | ||
173 | const char *name; | ||
174 | uint32_t number; | ||
175 | } name_map[] = { | ||
176 | { "PHONE", GNUNET_GNSRECORD_TYPE_PHONE }, | ||
177 | { NULL, UINT32_MAX } | ||
178 | }; | ||
179 | |||
180 | |||
181 | /** | ||
182 | * Convert a type name (e.g. "AAAA") to the corresponding number. | ||
183 | * | ||
184 | * @param cls closure, unused | ||
185 | * @param gns_typename name to convert | ||
186 | * @return corresponding number, UINT32_MAX on error | ||
187 | */ | ||
188 | static uint32_t | ||
189 | conversation_typename_to_number (void *cls, | ||
190 | const char *gns_typename) | ||
191 | { | ||
192 | unsigned int i; | ||
193 | |||
194 | (void) cls; | ||
195 | i = 0; | ||
196 | while ((name_map[i].name != NULL) && | ||
197 | (0 != strcasecmp (gns_typename, name_map[i].name))) | ||
198 | i++; | ||
199 | return name_map[i].number; | ||
200 | } | ||
201 | |||
202 | |||
203 | /** | ||
204 | * Convert a type number to the corresponding type string (e.g. 1 to "A") | ||
205 | * | ||
206 | * @param cls closure, unused | ||
207 | * @param type number of a type to convert | ||
208 | * @return corresponding typestring, NULL on error | ||
209 | */ | ||
210 | static const char * | ||
211 | conversation_number_to_typename (void *cls, | ||
212 | uint32_t type) | ||
213 | { | ||
214 | unsigned int i; | ||
215 | |||
216 | (void) cls; | ||
217 | i = 0; | ||
218 | while ((name_map[i].name != NULL) && | ||
219 | (type != name_map[i].number)) | ||
220 | i++; | ||
221 | return name_map[i].name; | ||
222 | } | ||
223 | |||
224 | |||
225 | /** | ||
226 | * Entry point for the plugin. | ||
227 | * | ||
228 | * @param cls NULL | ||
229 | * @return the exported block API | ||
230 | */ | ||
231 | void * | ||
232 | libgnunet_plugin_gnsrecord_conversation_init (void *cls) | ||
233 | { | ||
234 | struct GNUNET_GNSRECORD_PluginFunctions *api; | ||
235 | |||
236 | (void) cls; | ||
237 | api = GNUNET_new (struct GNUNET_GNSRECORD_PluginFunctions); | ||
238 | api->value_to_string = &conversation_value_to_string; | ||
239 | api->string_to_value = &conversation_string_to_value; | ||
240 | api->typename_to_number = &conversation_typename_to_number; | ||
241 | api->number_to_typename = &conversation_number_to_typename; | ||
242 | return api; | ||
243 | } | ||
244 | |||
245 | |||
246 | /** | ||
247 | * Exit point from the plugin. | ||
248 | * | ||
249 | * @param cls the return value from #libgnunet_plugin_block_test_init | ||
250 | * @return NULL | ||
251 | */ | ||
252 | void * | ||
253 | libgnunet_plugin_gnsrecord_conversation_done (void *cls) | ||
254 | { | ||
255 | struct GNUNET_GNSRECORD_PluginFunctions *api = cls; | ||
256 | |||
257 | GNUNET_free (api); | ||
258 | return NULL; | ||
259 | } | ||
260 | |||
261 | |||
262 | /* end of plugin_gnsrecord_conversation.c */ | ||