-comment / naming cleanups
[oweals/gnunet.git] / src / peerinfo / peerinfo_api.c
1 /*
2      This file is part of GNUnet.
3      (C) 2001, 2002, 2004, 2005, 2007, 2009, 2010, 2012 Christian Grothoff (and other contributing authors)
4
5      GNUnet is free software; you can redistribute it and/or modify
6      it under the terms of the GNU General Public License as published
7      by the Free Software Foundation; either version 3, or (at your
8      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      General Public License for more details.
14
15      You should have received a copy of the GNU General Public License
16      along with GNUnet; see the file COPYING.  If not, write to the
17      Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18      Boston, MA 02111-1307, USA.
19 */
20
21 /**
22  * @file peerinfo/peerinfo_api.c
23  * @brief API to access peerinfo service
24  * @author Christian Grothoff
25  */
26 #include "platform.h"
27 #include "gnunet_client_lib.h"
28 #include "gnunet_container_lib.h"
29 #include "gnunet_peerinfo_service.h"
30 #include "gnunet_protocols.h"
31 #include "gnunet_time_lib.h"
32 #include "peerinfo.h"
33
34 #define LOG(kind,...) GNUNET_log_from (kind, "peerinfo-api",__VA_ARGS__)
35
36
37 /**
38  * Entry in the transmission queue to PEERINFO service.  We use
39  * the same structure for queueing 'iteration' requests and
40  * actual 'add' messages.
41  */
42 struct GNUNET_PEERINFO_AddContext
43 {
44   /**
45    * This is a linked list.
46    */
47   struct GNUNET_PEERINFO_AddContext *next;
48
49   /**
50    * This is a linked list.
51    */
52   struct GNUNET_PEERINFO_AddContext *prev;
53
54   /**
55    * Handle to the PEERINFO service.
56    */
57   struct GNUNET_PEERINFO_Handle *h;
58
59   /**
60    * Function to call after request has been transmitted, or NULL.
61    */
62   GNUNET_PEERINFO_Continuation cont;
63
64   /**
65    * Closure for 'cont'.
66    */
67   void *cont_cls;
68
69   /**
70    * Number of bytes of the request message (follows after this struct).
71    */
72   size_t size;
73
74 };
75
76
77 /**
78  * Context for an iteration request.
79  */
80 struct GNUNET_PEERINFO_IteratorContext
81 {
82
83   /**
84    * Kept in a DLL.
85    */
86   struct GNUNET_PEERINFO_IteratorContext *next;
87
88   /**
89    * Kept in a DLL.
90    */
91   struct GNUNET_PEERINFO_IteratorContext *prev;
92
93   /**
94    * Handle to the PEERINFO service.
95    */
96   struct GNUNET_PEERINFO_Handle *h;
97
98   /**
99    * Function to call with the results.
100    */
101   GNUNET_PEERINFO_Processor callback;
102
103   /**
104    * Closure for 'callback'.
105    */
106   void *callback_cls;
107
108   /**
109    * Our entry in the transmission queue.
110    */
111   struct GNUNET_PEERINFO_AddContext *ac;
112
113   /**
114    * Task responsible for timeout.
115    */
116   GNUNET_SCHEDULER_TaskIdentifier timeout_task;
117
118   /**
119    * Timeout for the operation.
120    */
121   struct GNUNET_TIME_Absolute timeout;
122
123   /**
124    * Peer we are interested in (only valid if iteration was restricted to one peer).
125    */
126   struct GNUNET_PeerIdentity peer;
127
128   /**
129    * Is 'peer' set?
130    */
131   int have_peer;
132
133   /**
134    * Set to GNUNET_YES if we are currently receiving replies from the
135    * service.
136    */
137   int request_transmitted;
138
139 };
140
141
142 /**
143  * Handle to the peerinfo service.
144  */
145 struct GNUNET_PEERINFO_Handle
146 {
147   /**
148    * Our configuration.
149    */
150   const struct GNUNET_CONFIGURATION_Handle *cfg;
151
152   /**
153    * Connection to the service.
154    */
155   struct GNUNET_CLIENT_Connection *client;
156
157   /**
158    * Head of transmission queue.
159    */
160   struct GNUNET_PEERINFO_AddContext *ac_head;
161
162   /**
163    * Tail of transmission queue.
164    */
165   struct GNUNET_PEERINFO_AddContext *ac_tail;
166
167   /**
168    * Handle for the current transmission request, or NULL if none is pending.
169    */
170   struct GNUNET_CLIENT_TransmitHandle *th;
171
172   /**
173    * Head of iterator DLL.
174    */
175   struct GNUNET_PEERINFO_IteratorContext *ic_head;
176
177   /**
178    * Tail of iterator DLL.
179    */
180   struct GNUNET_PEERINFO_IteratorContext *ic_tail;
181
182   /**
183    * ID for a reconnect task.
184    */
185   GNUNET_SCHEDULER_TaskIdentifier r_task;
186
187   /**
188    * Are we now receiving?
189    */
190   int in_receive;
191
192 };
193
194
195 /**
196  * Connect to the peerinfo service.
197  *
198  * @param cfg configuration to use
199  * @return NULL on error (configuration related, actual connection
200  *         establishment may happen asynchronously).
201  */
202 struct GNUNET_PEERINFO_Handle *
203 GNUNET_PEERINFO_connect (const struct GNUNET_CONFIGURATION_Handle *cfg)
204 {
205   struct GNUNET_PEERINFO_Handle *h;
206
207   h = GNUNET_malloc (sizeof (struct GNUNET_PEERINFO_Handle));
208   h->client = GNUNET_CLIENT_connect ("peerinfo", cfg);
209   h->cfg = cfg;
210   return h;
211 }
212
213
214 /**
215  * Disconnect from the peerinfo service.  Note that all iterators must
216  * have completed or have been cancelled by the time this function is
217  * called (otherwise, calling this function is a serious error).
218  * Furthermore, if 'GNUNET_PEERINFO_add_peer' operations are still
219  * pending, they will be cancelled silently on disconnect.
220  *
221  * @param h handle to disconnect
222  */
223 void
224 GNUNET_PEERINFO_disconnect (struct GNUNET_PEERINFO_Handle *h)
225 {
226   struct GNUNET_PEERINFO_AddContext *ac;
227   struct GNUNET_PEERINFO_IteratorContext *ic;
228
229   while (NULL != (ic = h->ic_head))
230   {
231     GNUNET_break (GNUNET_YES == ic->request_transmitted);
232     ic->request_transmitted = GNUNET_NO;
233     GNUNET_PEERINFO_iterate_cancel (ic);
234   }
235   while (NULL != (ac = h->ac_head))
236   {
237     GNUNET_CONTAINER_DLL_remove (h->ac_head, h->ac_tail, ac);
238     if (NULL != ac->cont)
239       ac->cont (ac->cont_cls, _("aborted due to explicit disconnect request"));
240     GNUNET_free (ac);
241   }
242   if (NULL != h->th)
243   {
244     GNUNET_CLIENT_notify_transmit_ready_cancel (h->th);
245     h->th = NULL;
246   }
247   if (NULL != h->client)
248   {
249     GNUNET_CLIENT_disconnect (h->client);
250     h->client = NULL;
251   }
252   if (GNUNET_SCHEDULER_NO_TASK != h->r_task)
253   {
254     GNUNET_SCHEDULER_cancel (h->r_task);
255     h->r_task = GNUNET_SCHEDULER_NO_TASK;
256   }
257   GNUNET_free (h);
258 }
259
260
261 /**
262  * Check if we have a request pending in the transmission queue and are
263  * able to transmit it right now.  If so, schedule transmission.
264  *
265  * @param h handle to the service
266  */
267 static void
268 trigger_transmit (struct GNUNET_PEERINFO_Handle *h);
269
270
271 /**
272  * Close the existing connection to PEERINFO and reconnect.
273  *
274  * @param h handle to the service
275  */
276 static void
277 reconnect (struct GNUNET_PEERINFO_Handle *h);
278
279
280 /**
281  * Task scheduled to re-try connecting to the peerinfo service.
282  *
283  * @param cls the 'struct GNUNET_PEERINFO_Handle'
284  * @param tc scheduler context
285  */
286 static void
287 reconnect_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
288 {
289   struct GNUNET_PEERINFO_Handle *h = cls;
290
291   h->r_task = GNUNET_SCHEDULER_NO_TASK;
292   reconnect (h);
293 }
294
295
296 /**
297  * Close the existing connection to PEERINFO and reconnect.
298  *
299  * @param h handle to the service
300  */
301 static void
302 reconnect (struct GNUNET_PEERINFO_Handle *h)
303 {
304   if (GNUNET_SCHEDULER_NO_TASK != h->r_task)
305   {
306     GNUNET_SCHEDULER_cancel (h->r_task);
307     h->r_task = GNUNET_SCHEDULER_NO_TASK;
308   }
309   if (NULL != h->th)
310   {
311     GNUNET_CLIENT_notify_transmit_ready_cancel (h->th);
312     h->th = NULL;
313   }
314   if (NULL != h->client)
315   {
316     GNUNET_CLIENT_disconnect (h->client);
317     h->client = NULL;
318   }
319   h->in_receive = GNUNET_NO;
320   h->client = GNUNET_CLIENT_connect ("peerinfo", h->cfg);
321   if (NULL == h->client)
322   {
323     h->r_task =
324         GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS, &reconnect_task,
325                                       h);
326     return;
327   }
328   trigger_transmit (h);
329 }
330
331
332 /**
333  * Transmit the request at the head of the transmission queue
334  * and trigger continuation (if any).
335  *
336  * @param cls the 'struct GNUNET_PEERINFO_Handle' (with the queue)
337  * @param size size of the buffer (0 on error)
338  * @param buf where to copy the message
339  * @return number of bytes copied to buf
340  */
341 static size_t
342 do_transmit (void *cls, size_t size, void *buf)
343 {
344   struct GNUNET_PEERINFO_Handle *h = cls;
345   struct GNUNET_PEERINFO_AddContext *ac = h->ac_head;
346   size_t ret;
347
348   h->th = NULL;
349   if (NULL == ac)
350     return 0; /* request was cancelled in the meantime */
351   if (NULL == buf)
352   {
353     /* peerinfo service died */
354     LOG (GNUNET_ERROR_TYPE_DEBUG | GNUNET_ERROR_TYPE_BULK,
355          "Failed to transmit message to `%s' service.\n", "PEERINFO");
356     GNUNET_CONTAINER_DLL_remove (h->ac_head, h->ac_tail, ac);
357     reconnect (h);
358     if (NULL != ac->cont)
359       ac->cont (ac->cont_cls, _("failed to transmit request (service down?)"));
360     GNUNET_free (ac);
361     return 0;
362   }
363   ret = ac->size;
364   if (size < ret)
365   {
366     /* change in head of queue (i.e. cancel + add), try again */
367     trigger_transmit (h);
368     return 0;
369   }
370   LOG (GNUNET_ERROR_TYPE_DEBUG,
371        "Transmitting request of size %u to `%s' service.\n", ret, "PEERINFO");
372   memcpy (buf, &ac[1], ret);
373   GNUNET_CONTAINER_DLL_remove (h->ac_head, h->ac_tail, ac);
374   trigger_transmit (h);
375   if (NULL != ac->cont)
376     ac->cont (ac->cont_cls, NULL);
377   GNUNET_free (ac);
378   return ret;
379 }
380
381
382 /**
383  * Check if we have a request pending in the transmission queue and are
384  * able to transmit it right now.  If so, schedule transmission.
385  *
386  * @param h handle to the service
387  */
388 static void
389 trigger_transmit (struct GNUNET_PEERINFO_Handle *h)
390 {
391   struct GNUNET_PEERINFO_AddContext *ac;
392
393   if (NULL == (ac = h->ac_head))
394     return; /* no requests queued */
395   if (NULL != h->th)
396     return; /* request already pending */
397   if (NULL == h->client)
398   {
399     /* disconnected, try to reconnect */
400     reconnect (h);
401     return;
402   }
403   h->th =
404     GNUNET_CLIENT_notify_transmit_ready (h->client, ac->size,
405                                          GNUNET_TIME_UNIT_FOREVER_REL,
406                                          GNUNET_YES,
407                                          &do_transmit, h);
408 }
409
410
411 /**
412  * Add a host to the persistent list.  This method operates in
413  * semi-reliable mode: if the transmission is not completed by
414  * the time 'GNUNET_PEERINFO_disconnect' is called, it will be
415  * aborted.  Furthermore, if a second HELLO is added for the
416  * same peer before the first one was transmitted, PEERINFO may
417  * merge the two HELLOs prior to transmission to the service.
418  *
419  * @param h handle to the peerinfo service
420  * @param hello the verified (!) HELLO message
421  * @param cont continuation to call when done, NULL is allowed
422  * @param cont_cls closure for 'cont'
423  * @return handle to cancel add operation; all pending
424  *         'add' operations will be cancelled automatically
425  *        on disconnect, so it is not necessary to keep this
426  *        handle (unless 'cont' is NULL and at some point
427  *        calling 'cont' must be prevented)
428  */
429 struct GNUNET_PEERINFO_AddContext *
430 GNUNET_PEERINFO_add_peer (struct GNUNET_PEERINFO_Handle *h,
431                           const struct GNUNET_HELLO_Message *hello,
432                           GNUNET_PEERINFO_Continuation cont,
433                           void *cont_cls)
434 {
435   uint16_t hs = GNUNET_HELLO_size (hello);
436   struct GNUNET_PEERINFO_AddContext *ac;
437   struct GNUNET_PeerIdentity peer;
438
439   GNUNET_assert (GNUNET_OK == GNUNET_HELLO_get_id (hello, &peer));
440   LOG (GNUNET_ERROR_TYPE_DEBUG,
441        "Adding peer `%s' to PEERINFO database (%u bytes of `%s')\n",
442        GNUNET_i2s (&peer), hs, "HELLO");
443   ac = GNUNET_malloc (sizeof (struct GNUNET_PEERINFO_AddContext) + hs);
444   ac->h = h;
445   ac->size = hs;
446   ac->cont = cont;
447   ac->cont_cls = cont_cls;
448   memcpy (&ac[1], hello, hs);
449   GNUNET_CONTAINER_DLL_insert_tail (h->ac_head, h->ac_tail, ac);
450   trigger_transmit (h);
451   return ac;
452 }
453
454
455 /**
456  * Cancel pending 'add' operation.  Must only be called before
457  * either 'cont' or 'GNUNET_PEERINFO_disconnect' are invoked.
458  *
459  * @param ac handle for the add operation to cancel
460  */
461 void 
462 GNUNET_PEERINFO_add_peer_cancel (struct GNUNET_PEERINFO_AddContext *ac)
463 {
464   struct GNUNET_PEERINFO_Handle *h = ac->h;
465
466   GNUNET_CONTAINER_DLL_remove (h->ac_head, h->ac_tail, ac);
467   GNUNET_free (ac);
468 }
469
470
471 /**
472  * Type of a function to call when we receive a message from the
473  * service.  Call the iterator with the result and (if applicable)
474  * continue to receive more messages or trigger processing the next
475  * event (if applicable).
476  *
477  * @param cls closure
478  * @param msg message received, NULL on timeout or fatal error
479  */
480 static void
481 peerinfo_handler (void *cls, const struct GNUNET_MessageHeader *msg)
482 {
483   struct GNUNET_PEERINFO_Handle *h = cls;
484   struct GNUNET_PEERINFO_IteratorContext *ic = h->ic_head;
485   const struct InfoMessage *im;
486   const struct GNUNET_HELLO_Message *hello;
487   GNUNET_PEERINFO_Processor cb;
488   struct GNUNET_PeerIdentity id;
489   void *cb_cls;
490   uint16_t ms;
491
492   h->in_receive = GNUNET_NO;
493   if (NULL == msg)
494   {
495     /* peerinfo service died, signal error */
496     if (NULL != ic)
497     {
498       cb = ic->callback;
499       cb_cls = ic->callback_cls;
500       GNUNET_PEERINFO_iterate_cancel (ic);
501     }
502     else
503     {
504       cb = NULL;
505     }
506     reconnect (h);
507     if (NULL != cb)
508       cb (cb_cls, NULL, NULL,
509           _("Failed to receive response from `PEERINFO' service."));
510     return;
511   }
512   if (NULL == ic)
513   {
514     /* didn't expect a response, reconnect */
515     reconnect (h);
516     return;
517   }
518   ic->request_transmitted = GNUNET_NO;
519   cb = ic->callback;
520   cb_cls = ic->callback_cls;
521   if (GNUNET_MESSAGE_TYPE_PEERINFO_INFO_END == ntohs (msg->type))
522   {
523     /* normal end of list of peers, signal end, process next pending request */
524     LOG (GNUNET_ERROR_TYPE_DEBUG,
525          "Received end of list of peers from `%s' service\n", "PEERINFO");
526     GNUNET_PEERINFO_iterate_cancel (ic);   
527     trigger_transmit (h);
528     if (GNUNET_NO == h->in_receive)
529     {
530       h->in_receive = GNUNET_YES;
531       GNUNET_CLIENT_receive (h->client, &peerinfo_handler, h,
532                              GNUNET_TIME_absolute_get_remaining (ic->timeout));
533     }
534     if (NULL != cb)
535       cb (cb_cls, NULL, NULL, NULL);
536     return;
537   }
538
539   ms = ntohs (msg->size);
540   if ((ms < sizeof (struct InfoMessage)) ||
541       (ntohs (msg->type) != GNUNET_MESSAGE_TYPE_PEERINFO_INFO))
542   {
543     /* malformed message */
544     GNUNET_break (0);
545     GNUNET_PEERINFO_iterate_cancel (ic);
546     reconnect (h);
547     if (NULL != cb)
548       cb (cb_cls, NULL, NULL,
549           _("Received invalid message from `PEERINFO' service."));
550     return;
551   }
552   im = (const struct InfoMessage *) msg;
553   GNUNET_break (0 == ntohl (im->reserved));
554   if ( (GNUNET_YES == ic->have_peer) &&
555        (0 != memcmp (&ic->peer, &im->peer, sizeof (struct GNUNET_PeerIdentity))) )
556   {
557     /* bogus message (from a different iteration call?); out of sequence! */
558     LOG (GNUNET_ERROR_TYPE_ERROR,
559          "Received HELLO for peer `%s', expected peer `%s'\n",
560          GNUNET_h2s (&im->peer.hashPubKey),
561          GNUNET_i2s (&ic->peer));
562     
563     GNUNET_break (0);
564     GNUNET_PEERINFO_iterate_cancel (ic);
565     reconnect (h);
566     if (NULL != cb)      
567       cb (cb_cls, NULL, NULL,
568           _("Received invalid message from `PEERINFO' service."));
569     return;
570   }
571   hello = NULL;
572   if (ms > sizeof (struct InfoMessage) + sizeof (struct GNUNET_MessageHeader))
573   {
574     hello = (const struct GNUNET_HELLO_Message *) &im[1];
575     if (ms != sizeof (struct InfoMessage) + GNUNET_HELLO_size (hello))
576     {
577       /* malformed message */
578       GNUNET_break (0);
579       GNUNET_PEERINFO_iterate_cancel (ic);
580       reconnect (h);
581       if (NULL != cb)      
582         cb (cb_cls, NULL, NULL,
583             _("Received invalid message from `PEERINFO' service."));
584       return;
585     }
586     if (GNUNET_OK != GNUNET_HELLO_get_id (hello, &id))
587     {
588       /* malformed message */
589       GNUNET_break (0);
590       GNUNET_PEERINFO_iterate_cancel (ic);
591       reconnect (h);
592       if (NULL != cb)      
593         cb (cb_cls, NULL, NULL,
594             _("Received invalid message from `PEERINFO' service."));
595       return;
596     }
597     if (0 != memcmp (&im->peer, &id, sizeof (struct GNUNET_PeerIdentity)))
598     {
599       /* malformed message */
600       GNUNET_break (0);
601       GNUNET_PEERINFO_iterate_cancel (ic);
602       reconnect (h);
603       if (NULL != cb)      
604         cb (cb_cls, NULL, NULL,
605             _("Received invalid message from `PEERINFO' service."));
606       return;
607     }
608   }
609
610   /* normal data message */
611   LOG (GNUNET_ERROR_TYPE_DEBUG,
612        "Received %u bytes of `%s' information about peer `%s' from `%s' service\n",
613        (hello == NULL) ? 0 : (unsigned int) GNUNET_HELLO_size (hello), "HELLO",
614        GNUNET_i2s (&im->peer), "PEERINFO");
615   h->in_receive = GNUNET_YES;
616   GNUNET_CLIENT_receive (h->client, &peerinfo_handler, h,
617                          GNUNET_TIME_absolute_get_remaining (ic->timeout));
618   if (NULL != cb)
619     cb (cb_cls, &im->peer, hello, NULL);
620 }
621
622
623 /**
624  * We've transmitted the iteration request.  Now get ready to process
625  * the results (or handle transmission error).
626  *
627  * @param cls the 'struct GNUNET_PEERINFO_IteratorContext'
628  * @param emsg error message, NULL if transmission worked
629  */
630 static void
631 iterator_start_receive (void *cls, const char *emsg)
632 {
633   struct GNUNET_PEERINFO_IteratorContext *ic = cls;
634   struct GNUNET_PEERINFO_Handle *h = ic->h;
635   GNUNET_PEERINFO_Processor cb;
636   void *cb_cls;
637
638   ic->ac = NULL;
639   if (NULL != emsg)
640   {
641     cb = ic->callback;
642     cb_cls = ic->callback_cls;
643     GNUNET_PEERINFO_iterate_cancel (ic);
644     reconnect (h);
645     if (NULL != cb)
646       cb (cb_cls, NULL, NULL, emsg);
647     return;
648   }
649   LOG (GNUNET_ERROR_TYPE_DEBUG, "Waiting for response from `%s' service.\n",
650        "PEERINFO");
651   ic->request_transmitted = GNUNET_YES;
652   if (GNUNET_NO == h->in_receive)
653   {
654     h->in_receive = GNUNET_YES;
655     GNUNET_CLIENT_receive (h->client, &peerinfo_handler, h,
656                            GNUNET_TIME_absolute_get_remaining (ic->timeout));
657   }
658 }
659
660
661 /**
662  * Peerinfo iteration request has timed out.
663  *
664  * @param cls the 'struct GNUNET_PEERINFO_IteratorContext*'
665  * @param tc scheduler context
666  */
667 static void
668 signal_timeout (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
669 {
670   struct GNUNET_PEERINFO_IteratorContext *ic = cls;
671   GNUNET_PEERINFO_Processor cb;
672   void *cb_cls;
673
674   ic->timeout_task = GNUNET_SCHEDULER_NO_TASK;
675   cb = ic->callback;
676   cb_cls = ic->callback_cls;
677   GNUNET_PEERINFO_iterate_cancel (ic);
678   if (NULL != cb)
679     cb (cb_cls, NULL, NULL,
680         _("Timeout transmitting iteration request to `PEERINFO' service."));
681 }
682
683
684 /**
685  * Call a method for each known matching host.  The callback method
686  * will be invoked once for each matching host and then finally once
687  * with a NULL pointer.  After that final invocation, the iterator
688  * context must no longer be used.
689  *
690  * Instead of calling this function with 'peer == NULL' it is often
691  * better to use 'GNUNET_PEERINFO_notify'.
692  *
693  * @param h handle to the peerinfo service
694  * @param peer restrict iteration to this peer only (can be NULL)
695  * @param timeout how long to wait until timing out
696  * @param callback the method to call for each peer
697  * @param callback_cls closure for callback
698  * @return iterator context
699  */
700 struct GNUNET_PEERINFO_IteratorContext *
701 GNUNET_PEERINFO_iterate (struct GNUNET_PEERINFO_Handle *h,
702                          const struct GNUNET_PeerIdentity *peer,
703                          struct GNUNET_TIME_Relative timeout,
704                          GNUNET_PEERINFO_Processor callback, void *callback_cls)
705 {
706   struct GNUNET_MessageHeader *lapm;
707   struct ListPeerMessage *lpm;
708   struct GNUNET_PEERINFO_IteratorContext *ic;
709   struct GNUNET_PEERINFO_AddContext *ac;
710
711   ic = GNUNET_malloc (sizeof (struct GNUNET_PEERINFO_IteratorContext));
712   if (NULL == peer)
713   {
714     LOG (GNUNET_ERROR_TYPE_DEBUG,
715          "Requesting list of peers from PEERINFO service\n");
716     ac =
717         GNUNET_malloc (sizeof (struct GNUNET_PEERINFO_AddContext) +
718                        sizeof (struct GNUNET_MessageHeader));
719     ac->size = sizeof (struct GNUNET_MessageHeader);
720     lapm = (struct GNUNET_MessageHeader *) &ac[1];
721     lapm->size = htons (sizeof (struct GNUNET_MessageHeader));
722     lapm->type = htons (GNUNET_MESSAGE_TYPE_PEERINFO_GET_ALL);
723   }
724   else
725   {
726     LOG (GNUNET_ERROR_TYPE_DEBUG,
727          "Requesting information on peer `%4s' from PEERINFO service\n",
728          GNUNET_i2s (peer));
729     ac =
730         GNUNET_malloc (sizeof (struct GNUNET_PEERINFO_AddContext) +
731                        sizeof (struct ListPeerMessage));
732     ac->size = sizeof (struct ListPeerMessage);
733     lpm = (struct ListPeerMessage *) &ac[1];
734     lpm->header.size = htons (sizeof (struct ListPeerMessage));
735     lpm->header.type = htons (GNUNET_MESSAGE_TYPE_PEERINFO_GET);
736     memcpy (&lpm->peer, peer, sizeof (struct GNUNET_PeerIdentity));
737     ic->have_peer = GNUNET_YES;
738     ic->peer = *peer;
739   }
740   ic->h = h;
741   ic->ac = ac;
742   ic->callback = callback;
743   ic->callback_cls = callback_cls;
744   ic->timeout = GNUNET_TIME_relative_to_absolute (timeout);
745   ic->timeout_task =
746       GNUNET_SCHEDULER_add_delayed (timeout, &signal_timeout, ic);
747   ac->cont = &iterator_start_receive;
748   ac->cont_cls = ic;
749   GNUNET_CONTAINER_DLL_insert_tail (h->ac_head, h->ac_tail, ac);
750   GNUNET_CONTAINER_DLL_insert_tail (h->ic_head,
751                                     h->ic_tail,
752                                     ic);
753   trigger_transmit (h);
754   return ic;
755 }
756
757
758 /**
759  * Cancel an iteration over peer information.
760  *
761  * @param ic context of the iterator to cancel
762  */
763 void
764 GNUNET_PEERINFO_iterate_cancel (struct GNUNET_PEERINFO_IteratorContext *ic)
765 {
766   struct GNUNET_PEERINFO_Handle *h;
767
768   h = ic->h;
769   if (GNUNET_SCHEDULER_NO_TASK != ic->timeout_task)
770   {
771     GNUNET_SCHEDULER_cancel (ic->timeout_task);
772     ic->timeout_task = GNUNET_SCHEDULER_NO_TASK;
773   }
774   ic->callback = NULL;
775   if (GNUNET_YES == ic->request_transmitted)
776     return;                     /* need to finish processing */
777   GNUNET_CONTAINER_DLL_remove (h->ic_head,
778                                h->ic_tail,
779                                ic);
780   if (NULL != ic->ac)
781   {
782     GNUNET_CONTAINER_DLL_remove (h->ac_head, h->ac_tail, ic->ac);
783     GNUNET_free (ic->ac);
784   }
785   GNUNET_free (ic);
786 }
787
788
789 /* end of peerinfo_api.c */