diff options
Diffstat (limited to 'src/gns/gns_api.c')
-rw-r--r-- | src/gns/gns_api.c | 431 |
1 files changed, 0 insertions, 431 deletions
diff --git a/src/gns/gns_api.c b/src/gns/gns_api.c deleted file mode 100644 index 841a0d240..000000000 --- a/src/gns/gns_api.c +++ /dev/null | |||
@@ -1,431 +0,0 @@ | |||
1 | /* | ||
2 | This file is part of GNUnet. | ||
3 | Copyright (C) 2009-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 | * @file gns/gns_api.c | ||
22 | * @brief library to access the GNS service | ||
23 | * @author Martin Schanzenbach | ||
24 | * @author Christian Grothoff | ||
25 | */ | ||
26 | #include "platform.h" | ||
27 | #include "gnunet_util_lib.h" | ||
28 | #include "gnunet_constants.h" | ||
29 | #include "gnunet_arm_service.h" | ||
30 | #include "gnunet_hello_lib.h" | ||
31 | #include "gnunet_protocols.h" | ||
32 | #include "gnunet_dht_service.h" | ||
33 | #include "gns.h" | ||
34 | #include "gns_api.h" | ||
35 | |||
36 | |||
37 | #define LOG(kind, ...) GNUNET_log_from (kind, "gns-api", __VA_ARGS__) | ||
38 | |||
39 | /** | ||
40 | * Default recursion depth limit to apply if | ||
41 | * the application does not specify any. | ||
42 | */ | ||
43 | #define DEFAULT_LIMIT 128 | ||
44 | |||
45 | /** | ||
46 | * Handle to a lookup request | ||
47 | */ | ||
48 | struct GNUNET_GNS_LookupRequest | ||
49 | { | ||
50 | /** | ||
51 | * DLL | ||
52 | */ | ||
53 | struct GNUNET_GNS_LookupRequest *next; | ||
54 | |||
55 | /** | ||
56 | * DLL | ||
57 | */ | ||
58 | struct GNUNET_GNS_LookupRequest *prev; | ||
59 | |||
60 | /** | ||
61 | * handle to gns | ||
62 | */ | ||
63 | struct GNUNET_GNS_Handle *gns_handle; | ||
64 | |||
65 | /** | ||
66 | * processor to call on lookup result | ||
67 | */ | ||
68 | GNUNET_GNS_LookupResultProcessor lookup_proc; | ||
69 | |||
70 | /** | ||
71 | * @e lookup_proc closure | ||
72 | */ | ||
73 | void *proc_cls; | ||
74 | |||
75 | /** | ||
76 | * Envelope with the message for this queue entry. | ||
77 | */ | ||
78 | struct GNUNET_MQ_Envelope *env; | ||
79 | |||
80 | /** | ||
81 | * request id | ||
82 | */ | ||
83 | uint32_t r_id; | ||
84 | }; | ||
85 | |||
86 | |||
87 | /** | ||
88 | * Reconnect to GNS service. | ||
89 | * | ||
90 | * @param handle the handle to the GNS service | ||
91 | */ | ||
92 | static void | ||
93 | reconnect (struct GNUNET_GNS_Handle *handle); | ||
94 | |||
95 | |||
96 | /** | ||
97 | * Reconnect to GNS | ||
98 | * | ||
99 | * @param cls the handle | ||
100 | */ | ||
101 | static void | ||
102 | reconnect_task (void *cls) | ||
103 | { | ||
104 | struct GNUNET_GNS_Handle *handle = cls; | ||
105 | |||
106 | handle->reconnect_task = NULL; | ||
107 | reconnect (handle); | ||
108 | } | ||
109 | |||
110 | |||
111 | /** | ||
112 | * Disconnect from service and then reconnect. | ||
113 | * | ||
114 | * @param handle our handle | ||
115 | */ | ||
116 | static void | ||
117 | force_reconnect (struct GNUNET_GNS_Handle *handle) | ||
118 | { | ||
119 | GNUNET_MQ_destroy (handle->mq); | ||
120 | handle->mq = NULL; | ||
121 | handle->reconnect_backoff | ||
122 | = GNUNET_TIME_STD_BACKOFF (handle->reconnect_backoff); | ||
123 | handle->reconnect_task | ||
124 | = GNUNET_SCHEDULER_add_delayed (handle->reconnect_backoff, | ||
125 | &reconnect_task, | ||
126 | handle); | ||
127 | } | ||
128 | |||
129 | |||
130 | /** | ||
131 | * Generic error handler, called with the appropriate error code and | ||
132 | * the same closure specified at the creation of the message queue. | ||
133 | * Not every message queue implementation supports an error handler. | ||
134 | * | ||
135 | * @param cls closure with the `struct GNUNET_GNS_Handle *` | ||
136 | * @param error error code | ||
137 | */ | ||
138 | static void | ||
139 | mq_error_handler (void *cls, | ||
140 | enum GNUNET_MQ_Error error) | ||
141 | { | ||
142 | struct GNUNET_GNS_Handle *handle = cls; | ||
143 | |||
144 | LOG (GNUNET_ERROR_TYPE_WARNING, | ||
145 | "Problem with message queue. error: %i\n", | ||
146 | error); | ||
147 | force_reconnect (handle); | ||
148 | } | ||
149 | |||
150 | |||
151 | /** | ||
152 | * Check validity of message received from the GNS service | ||
153 | * | ||
154 | * @param cls the `struct GNUNET_GNS_Handle *` | ||
155 | * @param loookup_msg the incoming message | ||
156 | */ | ||
157 | static int | ||
158 | check_result (void *cls, | ||
159 | const struct LookupResultMessage *lookup_msg) | ||
160 | { | ||
161 | size_t mlen = ntohs (lookup_msg->header.size) - sizeof(*lookup_msg); | ||
162 | uint32_t rd_count = ntohl (lookup_msg->rd_count); | ||
163 | struct GNUNET_GNSRECORD_Data rd[rd_count]; | ||
164 | |||
165 | (void) cls; | ||
166 | if (GNUNET_SYSERR == | ||
167 | GNUNET_GNSRECORD_records_deserialize (mlen, | ||
168 | (const char *) &lookup_msg[1], | ||
169 | rd_count, | ||
170 | rd)) | ||
171 | { | ||
172 | GNUNET_break (0); | ||
173 | return GNUNET_SYSERR; | ||
174 | } | ||
175 | return GNUNET_OK; | ||
176 | } | ||
177 | |||
178 | |||
179 | /** | ||
180 | * Handler for messages received from the GNS service | ||
181 | * | ||
182 | * @param cls the `struct GNUNET_GNS_Handle *` | ||
183 | * @param loookup_msg the incoming message | ||
184 | */ | ||
185 | static void | ||
186 | handle_result (void *cls, | ||
187 | const struct LookupResultMessage *lookup_msg) | ||
188 | { | ||
189 | struct GNUNET_GNS_Handle *handle = cls; | ||
190 | size_t mlen = ntohs (lookup_msg->header.size) - sizeof(*lookup_msg); | ||
191 | uint32_t rd_count = ntohl (lookup_msg->rd_count); | ||
192 | struct GNUNET_GNSRECORD_Data rd[rd_count]; | ||
193 | uint32_t r_id = ntohl (lookup_msg->id); | ||
194 | struct GNUNET_GNS_LookupRequest *lr; | ||
195 | GNUNET_GNS_LookupResultProcessor proc; | ||
196 | void *proc_cls; | ||
197 | |||
198 | LOG (GNUNET_ERROR_TYPE_DEBUG, | ||
199 | "Received lookup reply from GNS service (%u records)\n", | ||
200 | (unsigned int) rd_count); | ||
201 | for (lr = handle->lookup_head; NULL != lr; lr = lr->next) | ||
202 | if (lr->r_id == r_id) | ||
203 | break; | ||
204 | if (NULL == lr) | ||
205 | return; | ||
206 | proc = lr->lookup_proc; | ||
207 | proc_cls = lr->proc_cls; | ||
208 | |||
209 | GNUNET_assert (GNUNET_OK == | ||
210 | GNUNET_GNSRECORD_records_deserialize (mlen, | ||
211 | (const | ||
212 | char *) &lookup_msg[1], | ||
213 | rd_count, | ||
214 | rd)); | ||
215 | proc (proc_cls, | ||
216 | rd_count, | ||
217 | rd); | ||
218 | GNUNET_CONTAINER_DLL_remove (handle->lookup_head, | ||
219 | handle->lookup_tail, | ||
220 | lr); | ||
221 | if (NULL != lr->env) | ||
222 | GNUNET_MQ_discard (lr->env); | ||
223 | GNUNET_free (lr); | ||
224 | } | ||
225 | |||
226 | |||
227 | /** | ||
228 | * Reconnect to GNS service. | ||
229 | * | ||
230 | * @param handle the handle to the GNS service | ||
231 | */ | ||
232 | static void | ||
233 | reconnect (struct GNUNET_GNS_Handle *handle) | ||
234 | { | ||
235 | struct GNUNET_MQ_MessageHandler handlers[] = { | ||
236 | GNUNET_MQ_hd_var_size (result, | ||
237 | GNUNET_MESSAGE_TYPE_GNS_LOOKUP_RESULT, | ||
238 | struct LookupResultMessage, | ||
239 | handle), | ||
240 | GNUNET_MQ_handler_end () | ||
241 | }; | ||
242 | |||
243 | GNUNET_assert (NULL == handle->mq); | ||
244 | LOG (GNUNET_ERROR_TYPE_DEBUG, | ||
245 | "Trying to connect to GNS\n"); | ||
246 | handle->mq = GNUNET_CLIENT_connect (handle->cfg, | ||
247 | "gns", | ||
248 | handlers, | ||
249 | &mq_error_handler, | ||
250 | handle); | ||
251 | if (NULL == handle->mq) | ||
252 | return; | ||
253 | for (struct GNUNET_GNS_LookupRequest *lh = handle->lookup_head; | ||
254 | NULL != lh; | ||
255 | lh = lh->next) | ||
256 | GNUNET_MQ_send_copy (handle->mq, | ||
257 | lh->env); | ||
258 | } | ||
259 | |||
260 | |||
261 | /** | ||
262 | * Initialize the connection with the GNS service. | ||
263 | * | ||
264 | * @param cfg configuration to use | ||
265 | * @return handle to the GNS service, or NULL on error | ||
266 | */ | ||
267 | struct GNUNET_GNS_Handle * | ||
268 | GNUNET_GNS_connect (const struct GNUNET_CONFIGURATION_Handle *cfg) | ||
269 | { | ||
270 | struct GNUNET_GNS_Handle *handle; | ||
271 | |||
272 | handle = GNUNET_new (struct GNUNET_GNS_Handle); | ||
273 | handle->cfg = cfg; | ||
274 | reconnect (handle); | ||
275 | if (NULL == handle->mq) | ||
276 | { | ||
277 | GNUNET_free (handle); | ||
278 | return NULL; | ||
279 | } | ||
280 | return handle; | ||
281 | } | ||
282 | |||
283 | |||
284 | /** | ||
285 | * Shutdown connection with the GNS service. | ||
286 | * | ||
287 | * @param handle handle of the GNS connection to stop | ||
288 | */ | ||
289 | void | ||
290 | GNUNET_GNS_disconnect (struct GNUNET_GNS_Handle *handle) | ||
291 | { | ||
292 | if (NULL != handle->mq) | ||
293 | { | ||
294 | GNUNET_MQ_destroy (handle->mq); | ||
295 | handle->mq = NULL; | ||
296 | } | ||
297 | if (NULL != handle->reconnect_task) | ||
298 | { | ||
299 | GNUNET_SCHEDULER_cancel (handle->reconnect_task); | ||
300 | handle->reconnect_task = NULL; | ||
301 | } | ||
302 | GNUNET_assert (NULL == handle->lookup_head); | ||
303 | GNUNET_free (handle); | ||
304 | } | ||
305 | |||
306 | |||
307 | /** | ||
308 | * Cancel pending lookup request | ||
309 | * | ||
310 | * @param lr the lookup request to cancel | ||
311 | * @return closure from the lookup result processor | ||
312 | */ | ||
313 | void * | ||
314 | GNUNET_GNS_lookup_cancel (struct GNUNET_GNS_LookupRequest *lr) | ||
315 | { | ||
316 | struct GNUNET_GNS_Handle *handle = lr->gns_handle; | ||
317 | void *ret; | ||
318 | |||
319 | GNUNET_CONTAINER_DLL_remove (handle->lookup_head, | ||
320 | handle->lookup_tail, | ||
321 | lr); | ||
322 | GNUNET_MQ_discard (lr->env); | ||
323 | ret = lr->proc_cls; | ||
324 | GNUNET_free (lr); | ||
325 | return ret; | ||
326 | } | ||
327 | |||
328 | |||
329 | /** | ||
330 | * Perform an asynchronous lookup operation on the GNS. | ||
331 | * | ||
332 | * @param handle handle to the GNS service | ||
333 | * @param name the name to look up (in UTF-8 encoding) | ||
334 | * @param zone zone to look in | ||
335 | * @param type the GNS record type to look for | ||
336 | * @param options local options for the lookup | ||
337 | * @param recursion_depth_limit maximum number of zones | ||
338 | * that the lookup may (still) traverse | ||
339 | * @param proc function to call on result | ||
340 | * @param proc_cls closure for @a proc | ||
341 | * @return handle to the queued request | ||
342 | */ | ||
343 | struct GNUNET_GNS_LookupRequest * | ||
344 | GNUNET_GNS_lookup_limited (struct GNUNET_GNS_Handle *handle, | ||
345 | const char *name, | ||
346 | const struct GNUNET_IDENTITY_PublicKey *zone, | ||
347 | uint32_t type, | ||
348 | enum GNUNET_GNS_LocalOptions options, | ||
349 | uint16_t recursion_depth_limit, | ||
350 | GNUNET_GNS_LookupResultProcessor proc, | ||
351 | void *proc_cls) | ||
352 | { | ||
353 | /* IPC to shorten gns names, return shorten_handle */ | ||
354 | struct LookupMessage *lookup_msg; | ||
355 | struct GNUNET_GNS_LookupRequest *lr; | ||
356 | size_t nlen; | ||
357 | |||
358 | if (NULL == name) | ||
359 | { | ||
360 | GNUNET_break (0); | ||
361 | return NULL; | ||
362 | } | ||
363 | LOG (GNUNET_ERROR_TYPE_DEBUG, | ||
364 | "Trying to lookup `%s' in GNS\n", | ||
365 | name); | ||
366 | nlen = strlen (name) + 1; | ||
367 | if (nlen >= GNUNET_MAX_MESSAGE_SIZE - sizeof(*lr)) | ||
368 | { | ||
369 | GNUNET_break (0); | ||
370 | return NULL; | ||
371 | } | ||
372 | lr = GNUNET_new (struct GNUNET_GNS_LookupRequest); | ||
373 | lr->gns_handle = handle; | ||
374 | lr->lookup_proc = proc; | ||
375 | lr->proc_cls = proc_cls; | ||
376 | lr->r_id = handle->r_id_gen++; | ||
377 | lr->env = GNUNET_MQ_msg_extra (lookup_msg, | ||
378 | nlen, | ||
379 | GNUNET_MESSAGE_TYPE_GNS_LOOKUP); | ||
380 | lookup_msg->id = htonl (lr->r_id); | ||
381 | lookup_msg->options = htons ((uint16_t) options); | ||
382 | lookup_msg->recursion_depth_limit | ||
383 | = htons (recursion_depth_limit); | ||
384 | lookup_msg->zone = *zone; | ||
385 | lookup_msg->type = htonl (type); | ||
386 | GNUNET_memcpy (&lookup_msg[1], | ||
387 | name, | ||
388 | nlen); | ||
389 | GNUNET_CONTAINER_DLL_insert (handle->lookup_head, | ||
390 | handle->lookup_tail, | ||
391 | lr); | ||
392 | if (NULL != handle->mq) | ||
393 | GNUNET_MQ_send_copy (handle->mq, | ||
394 | lr->env); | ||
395 | return lr; | ||
396 | } | ||
397 | |||
398 | |||
399 | /** | ||
400 | * Perform an asynchronous lookup operation on the GNS. | ||
401 | * | ||
402 | * @param handle handle to the GNS service | ||
403 | * @param name the name to look up (in UTF-8 encoding) | ||
404 | * @param zone the zone to start the resolution in | ||
405 | * @param type the record type to look up | ||
406 | * @param options local options for the lookup | ||
407 | * @param proc processor to call on result | ||
408 | * @param proc_cls closure for @a proc | ||
409 | * @return handle to the get request | ||
410 | */ | ||
411 | struct GNUNET_GNS_LookupRequest* | ||
412 | GNUNET_GNS_lookup (struct GNUNET_GNS_Handle *handle, | ||
413 | const char *name, | ||
414 | const struct GNUNET_IDENTITY_PublicKey *zone, | ||
415 | uint32_t type, | ||
416 | enum GNUNET_GNS_LocalOptions options, | ||
417 | GNUNET_GNS_LookupResultProcessor proc, | ||
418 | void *proc_cls) | ||
419 | { | ||
420 | return GNUNET_GNS_lookup_limited (handle, | ||
421 | name, | ||
422 | zone, | ||
423 | type, | ||
424 | options, | ||
425 | DEFAULT_LIMIT, | ||
426 | proc, | ||
427 | proc_cls); | ||
428 | } | ||
429 | |||
430 | |||
431 | /* end of gns_api.c */ | ||