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