aboutsummaryrefslogtreecommitdiff
path: root/src/gns/gns_api.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/gns/gns_api.c')
-rw-r--r--src/gns/gns_api.c257
1 files changed, 127 insertions, 130 deletions
diff --git a/src/gns/gns_api.c b/src/gns/gns_api.c
index ba83b73cd..62f250959 100644
--- a/src/gns/gns_api.c
+++ b/src/gns/gns_api.c
@@ -16,7 +16,7 @@
16 along with this program. If not, see <http://www.gnu.org/licenses/>. 16 along with this program. If not, see <http://www.gnu.org/licenses/>.
17 17
18 SPDX-License-Identifier: AGPL3.0-or-later 18 SPDX-License-Identifier: AGPL3.0-or-later
19*/ 19 */
20/** 20/**
21 * @file gns/gns_api.c 21 * @file gns/gns_api.c
22 * @brief library to access the GNS service 22 * @brief library to access the GNS service
@@ -34,14 +34,12 @@
34#include "gns_api.h" 34#include "gns_api.h"
35 35
36 36
37#define LOG(kind,...) GNUNET_log_from (kind, "gns-api",__VA_ARGS__) 37#define LOG(kind, ...) GNUNET_log_from(kind, "gns-api", __VA_ARGS__)
38 38
39/** 39/**
40 * Handle to a lookup request 40 * Handle to a lookup request
41 */ 41 */
42struct GNUNET_GNS_LookupRequest 42struct GNUNET_GNS_LookupRequest {
43{
44
45 /** 43 /**
46 * DLL 44 * DLL
47 */ 45 */
@@ -76,7 +74,6 @@ struct GNUNET_GNS_LookupRequest
76 * request id 74 * request id
77 */ 75 */
78 uint32_t r_id; 76 uint32_t r_id;
79
80}; 77};
81 78
82 79
@@ -86,7 +83,7 @@ struct GNUNET_GNS_LookupRequest
86 * @param handle the handle to the GNS service 83 * @param handle the handle to the GNS service
87 */ 84 */
88static void 85static void
89reconnect (struct GNUNET_GNS_Handle *handle); 86reconnect(struct GNUNET_GNS_Handle *handle);
90 87
91 88
92/** 89/**
@@ -95,12 +92,12 @@ reconnect (struct GNUNET_GNS_Handle *handle);
95 * @param cls the handle 92 * @param cls the handle
96 */ 93 */
97static void 94static void
98reconnect_task (void *cls) 95reconnect_task(void *cls)
99{ 96{
100 struct GNUNET_GNS_Handle *handle = cls; 97 struct GNUNET_GNS_Handle *handle = cls;
101 98
102 handle->reconnect_task = NULL; 99 handle->reconnect_task = NULL;
103 reconnect (handle); 100 reconnect(handle);
104} 101}
105 102
106 103
@@ -110,16 +107,16 @@ reconnect_task (void *cls)
110 * @param handle our handle 107 * @param handle our handle
111 */ 108 */
112static void 109static void
113force_reconnect (struct GNUNET_GNS_Handle *handle) 110force_reconnect(struct GNUNET_GNS_Handle *handle)
114{ 111{
115 GNUNET_MQ_destroy (handle->mq); 112 GNUNET_MQ_destroy(handle->mq);
116 handle->mq = NULL; 113 handle->mq = NULL;
117 handle->reconnect_backoff 114 handle->reconnect_backoff
118 = GNUNET_TIME_STD_BACKOFF (handle->reconnect_backoff); 115 = GNUNET_TIME_STD_BACKOFF(handle->reconnect_backoff);
119 handle->reconnect_task 116 handle->reconnect_task
120 = GNUNET_SCHEDULER_add_delayed (handle->reconnect_backoff, 117 = GNUNET_SCHEDULER_add_delayed(handle->reconnect_backoff,
121 &reconnect_task, 118 &reconnect_task,
122 handle); 119 handle);
123} 120}
124 121
125 122
@@ -132,15 +129,15 @@ force_reconnect (struct GNUNET_GNS_Handle *handle)
132 * @param error error code 129 * @param error error code
133 */ 130 */
134static void 131static void
135mq_error_handler (void *cls, 132mq_error_handler(void *cls,
136 enum GNUNET_MQ_Error error) 133 enum GNUNET_MQ_Error error)
137{ 134{
138 struct GNUNET_GNS_Handle *handle = cls; 135 struct GNUNET_GNS_Handle *handle = cls;
139 136
140 LOG (GNUNET_ERROR_TYPE_WARNING, 137 LOG(GNUNET_ERROR_TYPE_WARNING,
141 "Problem with message queue. error: %i\n", 138 "Problem with message queue. error: %i\n",
142 error); 139 error);
143 force_reconnect (handle); 140 force_reconnect(handle);
144} 141}
145 142
146 143
@@ -151,23 +148,23 @@ mq_error_handler (void *cls,
151 * @param loookup_msg the incoming message 148 * @param loookup_msg the incoming message
152 */ 149 */
153static int 150static int
154check_result (void *cls, 151check_result(void *cls,
155 const struct LookupResultMessage *lookup_msg) 152 const struct LookupResultMessage *lookup_msg)
156{ 153{
157 size_t mlen = ntohs (lookup_msg->header.size) - sizeof (*lookup_msg); 154 size_t mlen = ntohs(lookup_msg->header.size) - sizeof(*lookup_msg);
158 uint32_t rd_count = ntohl (lookup_msg->rd_count); 155 uint32_t rd_count = ntohl(lookup_msg->rd_count);
159 struct GNUNET_GNSRECORD_Data rd[rd_count]; 156 struct GNUNET_GNSRECORD_Data rd[rd_count];
160 157
161 (void) cls; 158 (void)cls;
162 if (GNUNET_SYSERR == 159 if (GNUNET_SYSERR ==
163 GNUNET_GNSRECORD_records_deserialize (mlen, 160 GNUNET_GNSRECORD_records_deserialize(mlen,
164 (const char*) &lookup_msg[1], 161 (const char*)&lookup_msg[1],
165 rd_count, 162 rd_count,
166 rd)) 163 rd))
167 { 164 {
168 GNUNET_break (0); 165 GNUNET_break(0);
169 return GNUNET_SYSERR; 166 return GNUNET_SYSERR;
170 } 167 }
171 return GNUNET_OK; 168 return GNUNET_OK;
172} 169}
173 170
@@ -179,21 +176,21 @@ check_result (void *cls,
179 * @param loookup_msg the incoming message 176 * @param loookup_msg the incoming message
180 */ 177 */
181static void 178static void
182handle_result (void *cls, 179handle_result(void *cls,
183 const struct LookupResultMessage *lookup_msg) 180 const struct LookupResultMessage *lookup_msg)
184{ 181{
185 struct GNUNET_GNS_Handle *handle = cls; 182 struct GNUNET_GNS_Handle *handle = cls;
186 size_t mlen = ntohs (lookup_msg->header.size) - sizeof (*lookup_msg); 183 size_t mlen = ntohs(lookup_msg->header.size) - sizeof(*lookup_msg);
187 uint32_t rd_count = ntohl (lookup_msg->rd_count); 184 uint32_t rd_count = ntohl(lookup_msg->rd_count);
188 struct GNUNET_GNSRECORD_Data rd[rd_count]; 185 struct GNUNET_GNSRECORD_Data rd[rd_count];
189 uint32_t r_id = ntohl (lookup_msg->id); 186 uint32_t r_id = ntohl(lookup_msg->id);
190 struct GNUNET_GNS_LookupRequest *lr; 187 struct GNUNET_GNS_LookupRequest *lr;
191 GNUNET_GNS_LookupResultProcessor proc; 188 GNUNET_GNS_LookupResultProcessor proc;
192 void *proc_cls; 189 void *proc_cls;
193 190
194 LOG (GNUNET_ERROR_TYPE_DEBUG, 191 LOG(GNUNET_ERROR_TYPE_DEBUG,
195 "Received lookup reply from GNS service (%u records)\n", 192 "Received lookup reply from GNS service (%u records)\n",
196 (unsigned int) rd_count); 193 (unsigned int)rd_count);
197 for (lr = handle->lookup_head; NULL != lr; lr = lr->next) 194 for (lr = handle->lookup_head; NULL != lr; lr = lr->next)
198 if (lr->r_id == r_id) 195 if (lr->r_id == r_id)
199 break; 196 break;
@@ -202,20 +199,20 @@ handle_result (void *cls,
202 proc = lr->lookup_proc; 199 proc = lr->lookup_proc;
203 proc_cls = lr->proc_cls; 200 proc_cls = lr->proc_cls;
204 201
205 GNUNET_assert (GNUNET_OK == 202 GNUNET_assert(GNUNET_OK ==
206 GNUNET_GNSRECORD_records_deserialize (mlen, 203 GNUNET_GNSRECORD_records_deserialize(mlen,
207 (const char*) &lookup_msg[1], 204 (const char*)&lookup_msg[1],
208 rd_count, 205 rd_count,
209 rd)); 206 rd));
210 proc (proc_cls, 207 proc(proc_cls,
211 rd_count, 208 rd_count,
212 rd); 209 rd);
213 GNUNET_CONTAINER_DLL_remove (handle->lookup_head, 210 GNUNET_CONTAINER_DLL_remove(handle->lookup_head,
214 handle->lookup_tail, 211 handle->lookup_tail,
215 lr); 212 lr);
216 if (NULL != lr->env) 213 if (NULL != lr->env)
217 GNUNET_MQ_discard (lr->env); 214 GNUNET_MQ_discard(lr->env);
218 GNUNET_free (lr); 215 GNUNET_free(lr);
219} 216}
220 217
221 218
@@ -225,31 +222,31 @@ handle_result (void *cls,
225 * @param handle the handle to the GNS service 222 * @param handle the handle to the GNS service
226 */ 223 */
227static void 224static void
228reconnect (struct GNUNET_GNS_Handle *handle) 225reconnect(struct GNUNET_GNS_Handle *handle)
229{ 226{
230 struct GNUNET_MQ_MessageHandler handlers[] = { 227 struct GNUNET_MQ_MessageHandler handlers[] = {
231 GNUNET_MQ_hd_var_size (result, 228 GNUNET_MQ_hd_var_size(result,
232 GNUNET_MESSAGE_TYPE_GNS_LOOKUP_RESULT, 229 GNUNET_MESSAGE_TYPE_GNS_LOOKUP_RESULT,
233 struct LookupResultMessage, 230 struct LookupResultMessage,
234 handle), 231 handle),
235 GNUNET_MQ_handler_end () 232 GNUNET_MQ_handler_end()
236 }; 233 };
237 234
238 GNUNET_assert (NULL == handle->mq); 235 GNUNET_assert(NULL == handle->mq);
239 LOG (GNUNET_ERROR_TYPE_DEBUG, 236 LOG(GNUNET_ERROR_TYPE_DEBUG,
240 "Trying to connect to GNS\n"); 237 "Trying to connect to GNS\n");
241 handle->mq = GNUNET_CLIENT_connect (handle->cfg, 238 handle->mq = GNUNET_CLIENT_connect(handle->cfg,
242 "gns", 239 "gns",
243 handlers, 240 handlers,
244 &mq_error_handler, 241 &mq_error_handler,
245 handle); 242 handle);
246 if (NULL == handle->mq) 243 if (NULL == handle->mq)
247 return; 244 return;
248 for (struct GNUNET_GNS_LookupRequest *lh = handle->lookup_head; 245 for (struct GNUNET_GNS_LookupRequest *lh = handle->lookup_head;
249 NULL != lh; 246 NULL != lh;
250 lh = lh->next) 247 lh = lh->next)
251 GNUNET_MQ_send_copy (handle->mq, 248 GNUNET_MQ_send_copy(handle->mq,
252 lh->env); 249 lh->env);
253} 250}
254 251
255 252
@@ -260,18 +257,18 @@ reconnect (struct GNUNET_GNS_Handle *handle)
260 * @return handle to the GNS service, or NULL on error 257 * @return handle to the GNS service, or NULL on error
261 */ 258 */
262struct GNUNET_GNS_Handle * 259struct GNUNET_GNS_Handle *
263GNUNET_GNS_connect (const struct GNUNET_CONFIGURATION_Handle *cfg) 260GNUNET_GNS_connect(const struct GNUNET_CONFIGURATION_Handle *cfg)
264{ 261{
265 struct GNUNET_GNS_Handle *handle; 262 struct GNUNET_GNS_Handle *handle;
266 263
267 handle = GNUNET_new (struct GNUNET_GNS_Handle); 264 handle = GNUNET_new(struct GNUNET_GNS_Handle);
268 handle->cfg = cfg; 265 handle->cfg = cfg;
269 reconnect (handle); 266 reconnect(handle);
270 if (NULL == handle->mq) 267 if (NULL == handle->mq)
271 { 268 {
272 GNUNET_free (handle); 269 GNUNET_free(handle);
273 return NULL; 270 return NULL;
274 } 271 }
275 return handle; 272 return handle;
276} 273}
277 274
@@ -282,20 +279,20 @@ GNUNET_GNS_connect (const struct GNUNET_CONFIGURATION_Handle *cfg)
282 * @param handle handle of the GNS connection to stop 279 * @param handle handle of the GNS connection to stop
283 */ 280 */
284void 281void
285GNUNET_GNS_disconnect (struct GNUNET_GNS_Handle *handle) 282GNUNET_GNS_disconnect(struct GNUNET_GNS_Handle *handle)
286{ 283{
287 if (NULL != handle->mq) 284 if (NULL != handle->mq)
288 { 285 {
289 GNUNET_MQ_destroy (handle->mq); 286 GNUNET_MQ_destroy(handle->mq);
290 handle->mq = NULL; 287 handle->mq = NULL;
291 } 288 }
292 if (NULL != handle->reconnect_task) 289 if (NULL != handle->reconnect_task)
293 { 290 {
294 GNUNET_SCHEDULER_cancel (handle->reconnect_task); 291 GNUNET_SCHEDULER_cancel(handle->reconnect_task);
295 handle->reconnect_task = NULL; 292 handle->reconnect_task = NULL;
296 } 293 }
297 GNUNET_assert (NULL == handle->lookup_head); 294 GNUNET_assert(NULL == handle->lookup_head);
298 GNUNET_free (handle); 295 GNUNET_free(handle);
299} 296}
300 297
301 298
@@ -306,17 +303,17 @@ GNUNET_GNS_disconnect (struct GNUNET_GNS_Handle *handle)
306 * @return closure from the lookup result processor 303 * @return closure from the lookup result processor
307 */ 304 */
308void * 305void *
309GNUNET_GNS_lookup_cancel (struct GNUNET_GNS_LookupRequest *lr) 306GNUNET_GNS_lookup_cancel(struct GNUNET_GNS_LookupRequest *lr)
310{ 307{
311 struct GNUNET_GNS_Handle *handle = lr->gns_handle; 308 struct GNUNET_GNS_Handle *handle = lr->gns_handle;
312 void *ret; 309 void *ret;
313 310
314 GNUNET_CONTAINER_DLL_remove (handle->lookup_head, 311 GNUNET_CONTAINER_DLL_remove(handle->lookup_head,
315 handle->lookup_tail, 312 handle->lookup_tail,
316 lr); 313 lr);
317 GNUNET_MQ_discard (lr->env); 314 GNUNET_MQ_discard(lr->env);
318 ret = lr->proc_cls; 315 ret = lr->proc_cls;
319 GNUNET_free (lr); 316 GNUNET_free(lr);
320 return ret; 317 return ret;
321} 318}
322 319
@@ -334,13 +331,13 @@ GNUNET_GNS_lookup_cancel (struct GNUNET_GNS_LookupRequest *lr)
334 * @return handle to the get request 331 * @return handle to the get request
335 */ 332 */
336struct GNUNET_GNS_LookupRequest* 333struct GNUNET_GNS_LookupRequest*
337GNUNET_GNS_lookup (struct GNUNET_GNS_Handle *handle, 334GNUNET_GNS_lookup(struct GNUNET_GNS_Handle *handle,
338 const char *name, 335 const char *name,
339 const struct GNUNET_CRYPTO_EcdsaPublicKey *zone, 336 const struct GNUNET_CRYPTO_EcdsaPublicKey *zone,
340 uint32_t type, 337 uint32_t type,
341 enum GNUNET_GNS_LocalOptions options, 338 enum GNUNET_GNS_LocalOptions options,
342 GNUNET_GNS_LookupResultProcessor proc, 339 GNUNET_GNS_LookupResultProcessor proc,
343 void *proc_cls) 340 void *proc_cls)
344{ 341{
345 /* IPC to shorten gns names, return shorten_handle */ 342 /* IPC to shorten gns names, return shorten_handle */
346 struct LookupMessage *lookup_msg; 343 struct LookupMessage *lookup_msg;
@@ -348,40 +345,40 @@ GNUNET_GNS_lookup (struct GNUNET_GNS_Handle *handle,
348 size_t nlen; 345 size_t nlen;
349 346
350 if (NULL == name) 347 if (NULL == name)
351 { 348 {
352 GNUNET_break (0); 349 GNUNET_break(0);
353 return NULL; 350 return NULL;
354 } 351 }
355 LOG (GNUNET_ERROR_TYPE_DEBUG, 352 LOG(GNUNET_ERROR_TYPE_DEBUG,
356 "Trying to lookup `%s' in GNS\n", 353 "Trying to lookup `%s' in GNS\n",
357 name); 354 name);
358 nlen = strlen (name) + 1; 355 nlen = strlen(name) + 1;
359 if (nlen >= GNUNET_MAX_MESSAGE_SIZE - sizeof (*lr)) 356 if (nlen >= GNUNET_MAX_MESSAGE_SIZE - sizeof(*lr))
360 { 357 {
361 GNUNET_break (0); 358 GNUNET_break(0);
362 return NULL; 359 return NULL;
363 } 360 }
364 lr = GNUNET_new (struct GNUNET_GNS_LookupRequest); 361 lr = GNUNET_new(struct GNUNET_GNS_LookupRequest);
365 lr->gns_handle = handle; 362 lr->gns_handle = handle;
366 lr->lookup_proc = proc; 363 lr->lookup_proc = proc;
367 lr->proc_cls = proc_cls; 364 lr->proc_cls = proc_cls;
368 lr->r_id = handle->r_id_gen++; 365 lr->r_id = handle->r_id_gen++;
369 lr->env = GNUNET_MQ_msg_extra (lookup_msg, 366 lr->env = GNUNET_MQ_msg_extra(lookup_msg,
370 nlen, 367 nlen,
371 GNUNET_MESSAGE_TYPE_GNS_LOOKUP); 368 GNUNET_MESSAGE_TYPE_GNS_LOOKUP);
372 lookup_msg->id = htonl (lr->r_id); 369 lookup_msg->id = htonl(lr->r_id);
373 lookup_msg->options = htons ((uint16_t) options); 370 lookup_msg->options = htons((uint16_t)options);
374 lookup_msg->zone = *zone; 371 lookup_msg->zone = *zone;
375 lookup_msg->type = htonl (type); 372 lookup_msg->type = htonl(type);
376 GNUNET_memcpy (&lookup_msg[1], 373 GNUNET_memcpy(&lookup_msg[1],
377 name, 374 name,
378 nlen); 375 nlen);
379 GNUNET_CONTAINER_DLL_insert (handle->lookup_head, 376 GNUNET_CONTAINER_DLL_insert(handle->lookup_head,
380 handle->lookup_tail, 377 handle->lookup_tail,
381 lr); 378 lr);
382 if (NULL != handle->mq) 379 if (NULL != handle->mq)
383 GNUNET_MQ_send_copy (handle->mq, 380 GNUNET_MQ_send_copy(handle->mq,
384 lr->env); 381 lr->env);
385 return lr; 382 return lr;
386} 383}
387 384