-remove trailing whitespace
[oweals/gnunet.git] / src / core / core_api.c
1 /*
2      This file is part of GNUnet.
3      (C) 2009, 2010 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 core/core_api.c
23  * @brief core service; this is the main API for encrypted P2P
24  *        communications
25  * @author Christian Grothoff
26  */
27 #include "platform.h"
28 #include "gnunet_constants.h"
29 #include "gnunet_core_service.h"
30 #include "core.h"
31
32 #define LOG(kind,...) GNUNET_log_from (kind, "core-api",__VA_ARGS__)
33
34
35 /**
36  * Handle for a transmission request.
37  */
38 struct GNUNET_CORE_TransmitHandle
39 {
40
41   /**
42    * Corresponding peer record.
43    */
44   struct PeerRecord *peer;
45
46   /**
47    * Corresponding SEND_REQUEST message.  Only non-NULL
48    * while SEND_REQUEST message is pending.
49    */
50   struct ControlMessage *cm;
51
52   /**
53    * Function that will be called to get the actual request
54    * (once we are ready to transmit this request to the core).
55    * The function will be called with a NULL buffer to signal
56    * timeout.
57    */
58   GNUNET_CONNECTION_TransmitReadyNotify get_message;
59
60   /**
61    * Closure for get_message.
62    */
63   void *get_message_cls;
64
65   /**
66    * Timeout for this handle.
67    */
68   struct GNUNET_TIME_Absolute timeout;
69
70   /**
71    * How important is this message?
72    */
73   uint32_t priority;
74
75   /**
76    * Size of this request.
77    */
78   uint16_t msize;
79
80   /**
81    * Send message request ID for this request.
82    */
83   uint16_t smr_id;
84
85   /**
86    * Is corking allowed?
87    */
88   int cork;
89
90 };
91
92
93 /**
94  * Information we track for each peer.
95  */
96 struct PeerRecord
97 {
98
99   /**
100    * We generally do NOT keep peer records in a DLL; this
101    * DLL is only used IF this peer's 'pending_head' message
102    * is ready for transmission.
103    */
104   struct PeerRecord *prev;
105
106   /**
107    * We generally do NOT keep peer records in a DLL; this
108    * DLL is only used IF this peer's 'pending_head' message
109    * is ready for transmission.
110    */
111   struct PeerRecord *next;
112
113   /**
114    * Peer the record is about.
115    */
116   struct GNUNET_PeerIdentity peer;
117
118   /**
119    * Corresponding core handle.
120    */
121   struct GNUNET_CORE_Handle *ch;
122
123   /**
124    * Pending request, if any.  'th->peer' is set to NULL if the
125    * request is not active.
126    */
127   struct GNUNET_CORE_TransmitHandle th;
128
129   /**
130    * ID of timeout task for the 'pending_head' handle
131    * which is the one with the smallest timeout.
132    */
133   GNUNET_SCHEDULER_TaskIdentifier timeout_task;
134
135   /**
136    * ID of task to run 'next_request_transmission'.
137    */
138   GNUNET_SCHEDULER_TaskIdentifier ntr_task;
139
140   /**
141    * SendMessageRequest ID generator for this peer.
142    */
143   uint16_t smr_id_gen;
144
145 };
146
147
148 /**
149  * Type of function called upon completion.
150  *
151  * @param cls closure
152  * @param success GNUNET_OK on success (which for request_connect
153  *        ONLY means that we transmitted the connect request to CORE,
154  *        it does not mean that we are actually now connected!);
155  *        GNUNET_NO on timeout,
156  *        GNUNET_SYSERR if core was shut down
157  */
158 typedef void (*GNUNET_CORE_ControlContinuation) (void *cls, int success);
159
160
161 /**
162  * Entry in a doubly-linked list of control messages to be transmitted
163  * to the core service.  Control messages include traffic allocation,
164  * connection requests and of course our initial 'init' request.
165  *
166  * The actual message is allocated at the end of this struct.
167  */
168 struct ControlMessage
169 {
170   /**
171    * This is a doubly-linked list.
172    */
173   struct ControlMessage *next;
174
175   /**
176    * This is a doubly-linked list.
177    */
178   struct ControlMessage *prev;
179
180   /**
181    * Function to run after transmission failed/succeeded.
182    */
183   GNUNET_CORE_ControlContinuation cont;
184
185   /**
186    * Closure for 'cont'.
187    */
188   void *cont_cls;
189
190   /**
191    * Transmit handle (if one is associated with this ControlMessage), or NULL.
192    */
193   struct GNUNET_CORE_TransmitHandle *th;
194 };
195
196
197
198 /**
199  * Context for the core service connection.
200  */
201 struct GNUNET_CORE_Handle
202 {
203
204   /**
205    * Configuration we're using.
206    */
207   const struct GNUNET_CONFIGURATION_Handle *cfg;
208
209   /**
210    * Closure for the various callbacks.
211    */
212   void *cls;
213
214   /**
215    * Function to call once we've handshaked with the core service.
216    */
217   GNUNET_CORE_StartupCallback init;
218
219   /**
220    * Function to call whenever we're notified about a peer connecting.
221    */
222   GNUNET_CORE_ConnectEventHandler connects;
223
224   /**
225    * Function to call whenever we're notified about a peer disconnecting.
226    */
227   GNUNET_CORE_DisconnectEventHandler disconnects;
228
229   /**
230    * Function to call whenever we receive an inbound message.
231    */
232   GNUNET_CORE_MessageCallback inbound_notify;
233
234   /**
235    * Function to call whenever we receive an outbound message.
236    */
237   GNUNET_CORE_MessageCallback outbound_notify;
238
239   /**
240    * Function handlers for messages of particular type.
241    */
242   const struct GNUNET_CORE_MessageHandler *handlers;
243
244   /**
245    * Our connection to the service.
246    */
247   struct GNUNET_CLIENT_Connection *client;
248
249   /**
250    * Handle for our current transmission request.
251    */
252   struct GNUNET_CLIENT_TransmitHandle *cth;
253
254   /**
255    * Head of doubly-linked list of pending requests.
256    */
257   struct ControlMessage *control_pending_head;
258
259   /**
260    * Tail of doubly-linked list of pending requests.
261    */
262   struct ControlMessage *control_pending_tail;
263
264   /**
265    * Head of doubly-linked list of peers that are core-approved
266    * to send their next message.
267    */
268   struct PeerRecord *ready_peer_head;
269
270   /**
271    * Tail of doubly-linked list of peers that are core-approved
272    * to send their next message.
273    */
274   struct PeerRecord *ready_peer_tail;
275
276   /**
277    * Hash map listing all of the peers that we are currently
278    * connected to.
279    */
280   struct GNUNET_CONTAINER_MultiPeerMap *peers;
281
282   /**
283    * Identity of this peer.
284    */
285   struct GNUNET_PeerIdentity me;
286
287   /**
288    * ID of reconnect task (if any).
289    */
290   GNUNET_SCHEDULER_TaskIdentifier reconnect_task;
291
292   /**
293    * Current delay we use for re-trying to connect to core.
294    */
295   struct GNUNET_TIME_Relative retry_backoff;
296
297   /**
298    * Number of entries in the handlers array.
299    */
300   unsigned int hcnt;
301
302   /**
303    * For inbound notifications without a specific handler, do
304    * we expect to only receive headers?
305    */
306   int inbound_hdr_only;
307
308   /**
309    * For outbound notifications without a specific handler, do
310    * we expect to only receive headers?
311    */
312   int outbound_hdr_only;
313
314   /**
315    * Are we currently disconnected and hence unable to forward
316    * requests?
317    */
318   int currently_down;
319
320 };
321
322
323 /**
324  * Our current client connection went down.  Clean it up
325  * and try to reconnect!
326  *
327  * @param h our handle to the core service
328  */
329 static void
330 reconnect (struct GNUNET_CORE_Handle *h);
331
332
333 /**
334  * Task schedule to try to re-connect to core.
335  *
336  * @param cls the 'struct GNUNET_CORE_Handle'
337  * @param tc task context
338  */
339 static void
340 reconnect_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
341 {
342   struct GNUNET_CORE_Handle *h = cls;
343
344   h->reconnect_task = GNUNET_SCHEDULER_NO_TASK;
345   LOG (GNUNET_ERROR_TYPE_DEBUG, "Connecting to CORE service after delay\n");
346   reconnect (h);
347 }
348
349
350 /**
351  * Notify clients about disconnect and free
352  * the entry for connected peer.
353  *
354  * @param cls the 'struct GNUNET_CORE_Handle*'
355  * @param key the peer identity (not used)
356  * @param value the 'struct PeerRecord' to free.
357  * @return #GNUNET_YES (continue)
358  */
359 static int
360 disconnect_and_free_peer_entry (void *cls,
361                                 const struct GNUNET_PeerIdentity *key,
362                                 void *value)
363 {
364   struct GNUNET_CORE_Handle *h = cls;
365   struct GNUNET_CORE_TransmitHandle *th;
366   struct PeerRecord *pr = value;
367
368   if (GNUNET_SCHEDULER_NO_TASK != pr->timeout_task)
369   {
370     GNUNET_SCHEDULER_cancel (pr->timeout_task);
371     pr->timeout_task = GNUNET_SCHEDULER_NO_TASK;
372   }
373   if (GNUNET_SCHEDULER_NO_TASK != pr->ntr_task)
374   {
375     GNUNET_SCHEDULER_cancel (pr->ntr_task);
376     pr->ntr_task = GNUNET_SCHEDULER_NO_TASK;
377   }
378   if ((NULL != pr->prev) || (NULL != pr->next) || (h->ready_peer_head == pr))
379     GNUNET_CONTAINER_DLL_remove (h->ready_peer_head, h->ready_peer_tail, pr);
380   if (NULL != h->disconnects)
381     h->disconnects (h->cls, &pr->peer);
382   /* all requests should have been cancelled, clean up anyway, just in case */
383   th = &pr->th;
384   if (NULL != th->peer)
385   {
386     GNUNET_break (0);
387     th->peer = NULL;
388     if (NULL != th->cm)
389       th->cm->th = NULL;
390   }
391   /* done with 'voluntary' cleanups, now on to normal freeing */
392   GNUNET_assert (GNUNET_YES ==
393                  GNUNET_CONTAINER_multipeermap_remove (h->peers, key, pr));
394   GNUNET_assert (pr->ch == h);
395   GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == pr->timeout_task);
396   GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == pr->ntr_task);
397   GNUNET_free (pr);
398   return GNUNET_YES;
399 }
400
401
402 /**
403  * Close down any existing connection to the CORE service and
404  * try re-establishing it later.
405  *
406  * @param h our handle
407  */
408 static void
409 reconnect_later (struct GNUNET_CORE_Handle *h)
410 {
411   struct ControlMessage *cm;
412   struct PeerRecord *pr;
413
414   GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == h->reconnect_task);
415   if (NULL != h->cth)
416   {
417     GNUNET_CLIENT_notify_transmit_ready_cancel (h->cth);
418     h->cth = NULL;
419   }
420   if (NULL != h->client)
421   {
422     GNUNET_CLIENT_disconnect (h->client);
423     h->client = NULL;
424   }
425   h->currently_down = GNUNET_YES;
426   GNUNET_assert (h->reconnect_task == GNUNET_SCHEDULER_NO_TASK);
427   h->reconnect_task =
428       GNUNET_SCHEDULER_add_delayed (h->retry_backoff, &reconnect_task, h);
429   while (NULL != (cm = h->control_pending_head))
430   {
431     GNUNET_CONTAINER_DLL_remove (h->control_pending_head,
432                                  h->control_pending_tail, cm);
433     if (NULL != cm->th)
434       cm->th->cm = NULL;
435     if (NULL != cm->cont)
436       cm->cont (cm->cont_cls, GNUNET_NO);
437     GNUNET_free (cm);
438   }
439   GNUNET_CONTAINER_multipeermap_iterate (h->peers,
440                                          &disconnect_and_free_peer_entry, h);
441   while (NULL != (pr = h->ready_peer_head))
442     GNUNET_CONTAINER_DLL_remove (h->ready_peer_head, h->ready_peer_tail, pr);
443   GNUNET_assert (h->control_pending_head == NULL);
444   h->retry_backoff = GNUNET_TIME_STD_BACKOFF (h->retry_backoff);
445 }
446
447
448 /**
449  * Check the list of pending requests, send the next
450  * one to the core.
451  *
452  * @param h core handle
453  * @param ignore_currently_down transmit message even if not initialized?
454  */
455 static void
456 trigger_next_request (struct GNUNET_CORE_Handle *h, int ignore_currently_down);
457
458
459 /**
460  * The given request hit its timeout.  Remove from the
461  * doubly-linked list and call the respective continuation.
462  *
463  * @param cls the transmit handle of the request that timed out
464  * @param tc context, can be NULL (!)
465  */
466 static void
467 transmission_timeout (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
468
469
470 /**
471  * Send a control message to the peer asking for transmission
472  * of the message in the given peer record.
473  *
474  * @param pr peer to request transmission to
475  */
476 static void
477 request_next_transmission (struct PeerRecord *pr)
478 {
479   struct GNUNET_CORE_Handle *h = pr->ch;
480   struct ControlMessage *cm;
481   struct SendMessageRequest *smr;
482   struct GNUNET_CORE_TransmitHandle *th;
483
484   if (pr->timeout_task != GNUNET_SCHEDULER_NO_TASK)
485   {
486     GNUNET_SCHEDULER_cancel (pr->timeout_task);
487     pr->timeout_task = GNUNET_SCHEDULER_NO_TASK;
488   }
489   th = &pr->th;
490   if (NULL == th->peer)
491   {
492     trigger_next_request (h, GNUNET_NO);
493     return;
494   }
495   if (th->cm != NULL)
496     return;                     /* already done */
497   GNUNET_assert (pr->prev == NULL);
498   GNUNET_assert (pr->next == NULL);
499   pr->timeout_task =
500       GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_absolute_get_remaining
501                                     (th->timeout), &transmission_timeout, pr);
502   cm = GNUNET_malloc (sizeof (struct ControlMessage) +
503                       sizeof (struct SendMessageRequest));
504   th->cm = cm;
505   cm->th = th;
506   smr = (struct SendMessageRequest *) &cm[1];
507   smr->header.type = htons (GNUNET_MESSAGE_TYPE_CORE_SEND_REQUEST);
508   smr->header.size = htons (sizeof (struct SendMessageRequest));
509   smr->priority = htonl (th->priority);
510   smr->deadline = GNUNET_TIME_absolute_hton (th->timeout);
511   smr->peer = pr->peer;
512   smr->reserved = htonl (0);
513   smr->size = htons (th->msize);
514   smr->smr_id = htons (th->smr_id = pr->smr_id_gen++);
515   GNUNET_CONTAINER_DLL_insert_tail (h->control_pending_head,
516                                     h->control_pending_tail, cm);
517   LOG (GNUNET_ERROR_TYPE_DEBUG,
518        "Adding SEND REQUEST for peer `%s' to message queue\n",
519        GNUNET_i2s (&pr->peer));
520   trigger_next_request (h, GNUNET_NO);
521 }
522
523
524 /**
525  * The given request hit its timeout.  Remove from the
526  * doubly-linked list and call the respective continuation.
527  *
528  * @param cls the transmit handle of the request that timed out
529  * @param tc context, can be NULL (!)
530  */
531 static void
532 transmission_timeout (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
533 {
534   struct PeerRecord *pr = cls;
535   struct GNUNET_CORE_Handle *h = pr->ch;
536   struct GNUNET_CORE_TransmitHandle *th;
537
538   pr->timeout_task = GNUNET_SCHEDULER_NO_TASK;
539   if (GNUNET_SCHEDULER_NO_TASK != pr->ntr_task)
540   {
541     GNUNET_SCHEDULER_cancel (pr->ntr_task);
542     pr->ntr_task = GNUNET_SCHEDULER_NO_TASK;
543   }
544   th = &pr->th;
545   th->peer = NULL;
546   if ((NULL != pr->prev) || (NULL != pr->next) || (pr == h->ready_peer_head))
547   {
548     /* the request that was 'approved' by core was
549      * canceled before it could be transmitted; remove
550      * us from the 'ready' list */
551     GNUNET_CONTAINER_DLL_remove (h->ready_peer_head, h->ready_peer_tail, pr);
552   }
553   if (NULL != th->cm)
554   {
555      /* we're currently in the control queue, remove */
556     GNUNET_CONTAINER_DLL_remove (h->control_pending_head,
557                                  h->control_pending_tail, th->cm);
558     GNUNET_free (th->cm);
559   }
560   LOG (GNUNET_ERROR_TYPE_DEBUG,
561        "Signalling timeout of request for transmission to peer `%s' via CORE\n",
562        GNUNET_i2s (&pr->peer));
563   trigger_next_request (h, GNUNET_NO);
564
565   GNUNET_assert (0 == th->get_message (th->get_message_cls, 0, NULL));
566 }
567
568
569 /**
570  * Transmit the next message to the core service.
571  *
572  * @param cls closure with the 'struct GNUNET_CORE_Handle'
573  * @param size number of bytes available in @a buf
574  * @param buf where the callee should write the message
575  * @return number of bytes written to buf
576  */
577 static size_t
578 transmit_message (void *cls, size_t size, void *buf)
579 {
580   struct GNUNET_CORE_Handle *h = cls;
581   struct ControlMessage *cm;
582   struct GNUNET_CORE_TransmitHandle *th;
583   struct PeerRecord *pr;
584   struct SendMessage *sm;
585   const struct GNUNET_MessageHeader *hdr;
586   uint16_t msize;
587   size_t ret;
588
589   GNUNET_assert (h->reconnect_task == GNUNET_SCHEDULER_NO_TASK);
590   h->cth = NULL;
591   if (NULL == buf)
592   {
593     LOG (GNUNET_ERROR_TYPE_DEBUG,
594          "Transmission failed, initiating reconnect\n");
595     reconnect_later (h);
596     return 0;
597   }
598   /* first check for control messages */
599   if (NULL != (cm = h->control_pending_head))
600   {
601     hdr = (const struct GNUNET_MessageHeader *) &cm[1];
602     msize = ntohs (hdr->size);
603     if (size < msize)
604     {
605       trigger_next_request (h, GNUNET_NO);
606       return 0;
607     }
608     LOG (GNUNET_ERROR_TYPE_DEBUG,
609          "Transmitting control message with %u bytes of type %u to core.\n",
610          (unsigned int) msize, (unsigned int) ntohs (hdr->type));
611     memcpy (buf, hdr, msize);
612     GNUNET_CONTAINER_DLL_remove (h->control_pending_head,
613                                  h->control_pending_tail, cm);
614     if (NULL != cm->th)
615       cm->th->cm = NULL;
616     if (NULL != cm->cont)
617       cm->cont (cm->cont_cls, GNUNET_OK);
618     GNUNET_free (cm);
619     trigger_next_request (h, GNUNET_NO);
620     return msize;
621   }
622   /* now check for 'ready' P2P messages */
623   if (NULL == (pr = h->ready_peer_head))
624     return 0;
625   GNUNET_assert (NULL != pr->th.peer);
626   th = &pr->th;
627   if (size < th->msize + sizeof (struct SendMessage))
628   {
629     trigger_next_request (h, GNUNET_NO);
630     return 0;
631   }
632   GNUNET_CONTAINER_DLL_remove (h->ready_peer_head, h->ready_peer_tail, pr);
633   th->peer = NULL;
634   if (GNUNET_SCHEDULER_NO_TASK != pr->timeout_task)
635   {
636     GNUNET_SCHEDULER_cancel (pr->timeout_task);
637     pr->timeout_task = GNUNET_SCHEDULER_NO_TASK;
638   }
639   LOG (GNUNET_ERROR_TYPE_DEBUG,
640        "Transmitting SEND request to `%s' with %u bytes.\n",
641        GNUNET_i2s (&pr->peer), (unsigned int) th->msize);
642   sm = (struct SendMessage *) buf;
643   sm->header.type = htons (GNUNET_MESSAGE_TYPE_CORE_SEND);
644   sm->priority = htonl (th->priority);
645   sm->deadline = GNUNET_TIME_absolute_hton (th->timeout);
646   sm->peer = pr->peer;
647   sm->cork = htonl ((uint32_t) th->cork);
648   sm->reserved = htonl (0);
649   ret =
650     th->get_message (th->get_message_cls,
651                      size - sizeof (struct SendMessage), &sm[1]);
652
653   LOG (GNUNET_ERROR_TYPE_DEBUG,
654        "Transmitting SEND request to `%s' yielded %u bytes.\n",
655        GNUNET_i2s (&pr->peer), ret);
656   if (0 == ret)
657   {
658     LOG (GNUNET_ERROR_TYPE_DEBUG,
659          "Size of clients message to peer %s is 0!\n",
660          GNUNET_i2s (&pr->peer));
661     /* client decided to send nothing! */
662     request_next_transmission (pr);
663     return 0;
664   }
665   LOG (GNUNET_ERROR_TYPE_DEBUG,
666        "Produced SEND message to core with %u bytes payload\n",
667        (unsigned int) ret);
668   GNUNET_assert (ret >= sizeof (struct GNUNET_MessageHeader));
669   if (ret + sizeof (struct SendMessage) >= GNUNET_SERVER_MAX_MESSAGE_SIZE)
670   {
671     GNUNET_break (0);
672     request_next_transmission (pr);
673     return 0;
674   }
675   ret += sizeof (struct SendMessage);
676   sm->header.size = htons (ret);
677   GNUNET_assert (ret <= size);
678   request_next_transmission (pr);
679   return ret;
680 }
681
682
683 /**
684  * Check the list of pending requests, send the next
685  * one to the core.
686  *
687  * @param h core handle
688  * @param ignore_currently_down transmit message even if not initialized?
689  */
690 static void
691 trigger_next_request (struct GNUNET_CORE_Handle *h, int ignore_currently_down)
692 {
693   uint16_t msize;
694
695   if ((GNUNET_YES == h->currently_down) && (ignore_currently_down == GNUNET_NO))
696   {
697     LOG (GNUNET_ERROR_TYPE_DEBUG,
698          "Core connection down, not processing queue\n");
699     return;
700   }
701   if (NULL != h->cth)
702   {
703     LOG (GNUNET_ERROR_TYPE_DEBUG, "Request pending, not processing queue\n");
704     return;
705   }
706   if (NULL != h->control_pending_head)
707     msize =
708         ntohs (((struct GNUNET_MessageHeader *) &h->
709                 control_pending_head[1])->size);
710   else if (h->ready_peer_head != NULL)
711     msize =
712       h->ready_peer_head->th.msize + sizeof (struct SendMessage);
713   else
714   {
715     LOG (GNUNET_ERROR_TYPE_DEBUG,
716          "Request queue empty, not processing queue\n");
717     return;                     /* no pending message */
718   }
719   h->cth =
720       GNUNET_CLIENT_notify_transmit_ready (h->client, msize,
721                                            GNUNET_TIME_UNIT_FOREVER_REL,
722                                            GNUNET_NO, &transmit_message, h);
723 }
724
725
726 /**
727  * Handler for notification messages received from the core.
728  *
729  * @param cls our "struct GNUNET_CORE_Handle"
730  * @param msg the message received from the core service
731  */
732 static void
733 main_notify_handler (void *cls, const struct GNUNET_MessageHeader *msg)
734 {
735   struct GNUNET_CORE_Handle *h = cls;
736   const struct InitReplyMessage *m;
737   const struct ConnectNotifyMessage *cnm;
738   const struct DisconnectNotifyMessage *dnm;
739   const struct NotifyTrafficMessage *ntm;
740   const struct GNUNET_MessageHeader *em;
741   const struct SendMessageReady *smr;
742   const struct GNUNET_CORE_MessageHandler *mh;
743   GNUNET_CORE_StartupCallback init;
744   struct PeerRecord *pr;
745   struct GNUNET_CORE_TransmitHandle *th;
746   unsigned int hpos;
747   int trigger;
748   uint16_t msize;
749   uint16_t et;
750   if (NULL == msg)
751   {
752     LOG (GNUNET_ERROR_TYPE_INFO,
753          _("Client was disconnected from core service, trying to reconnect.\n"));
754     reconnect_later (h);
755     return;
756   }
757   msize = ntohs (msg->size);
758   LOG (GNUNET_ERROR_TYPE_DEBUG,
759        "Processing message of type %u and size %u from core service\n",
760        ntohs (msg->type), msize);
761   switch (ntohs (msg->type))
762   {
763   case GNUNET_MESSAGE_TYPE_CORE_INIT_REPLY:
764     if (ntohs (msg->size) != sizeof (struct InitReplyMessage))
765     {
766       GNUNET_break (0);
767       reconnect_later (h);
768       return;
769     }
770     m = (const struct InitReplyMessage *) msg;
771     GNUNET_break (0 == ntohl (m->reserved));
772     /* start our message processing loop */
773     if (GNUNET_YES == h->currently_down)
774     {
775       h->currently_down = GNUNET_NO;
776       trigger_next_request (h, GNUNET_NO);
777     }
778     h->retry_backoff = GNUNET_TIME_UNIT_MILLISECONDS;
779     h->me = m->my_identity;
780     if (NULL != (init = h->init))
781     {
782       /* mark so we don't call init on reconnect */
783       h->init = NULL;
784       LOG (GNUNET_ERROR_TYPE_DEBUG, "Connected to core service of peer `%s'.\n",
785            GNUNET_i2s (&h->me));
786       init (h->cls, &h->me);
787     }
788     else
789     {
790       LOG (GNUNET_ERROR_TYPE_DEBUG,
791            "Successfully reconnected to core service.\n");
792     }
793     /* fake 'connect to self' */
794     pr = GNUNET_CONTAINER_multipeermap_get (h->peers, &h->me);
795     GNUNET_assert (NULL == pr);
796     pr = GNUNET_new (struct PeerRecord);
797     pr->peer = h->me;
798     pr->ch = h;
799     GNUNET_assert (GNUNET_YES ==
800                    GNUNET_CONTAINER_multipeermap_put (h->peers,
801                                                       &h->me, pr,
802                                                       GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST));
803     if (NULL != h->connects)
804       h->connects (h->cls, &h->me);
805     break;
806   case GNUNET_MESSAGE_TYPE_CORE_NOTIFY_CONNECT:
807     if (msize < sizeof (struct ConnectNotifyMessage))
808     {
809       GNUNET_break (0);
810       reconnect_later (h);
811       return;
812     }
813     cnm = (const struct ConnectNotifyMessage *) msg;
814     if (msize !=
815         sizeof (struct ConnectNotifyMessage))
816     {
817       GNUNET_break (0);
818       reconnect_later (h);
819       return;
820     }
821     LOG (GNUNET_ERROR_TYPE_DEBUG,
822          "Received notification about connection from `%s'.\n",
823          GNUNET_i2s (&cnm->peer));
824     if (0 == memcmp (&h->me, &cnm->peer, sizeof (struct GNUNET_PeerIdentity)))
825     {
826       /* connect to self!? */
827       GNUNET_break (0);
828       return;
829     }
830     pr = GNUNET_CONTAINER_multipeermap_get (h->peers, &cnm->peer);
831     if (NULL != pr)
832     {
833       GNUNET_break (0);
834       reconnect_later (h);
835       return;
836     }
837     pr = GNUNET_new (struct PeerRecord);
838     pr->peer = cnm->peer;
839     pr->ch = h;
840     GNUNET_assert (GNUNET_YES ==
841                    GNUNET_CONTAINER_multipeermap_put (h->peers,
842                                                       &cnm->peer, pr,
843                                                       GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST));
844     if (NULL != h->connects)
845       h->connects (h->cls, &cnm->peer);
846     break;
847   case GNUNET_MESSAGE_TYPE_CORE_NOTIFY_DISCONNECT:
848     if (msize != sizeof (struct DisconnectNotifyMessage))
849     {
850       GNUNET_break (0);
851       reconnect_later (h);
852       return;
853     }
854     dnm = (const struct DisconnectNotifyMessage *) msg;
855     if (0 == memcmp (&h->me, &dnm->peer, sizeof (struct GNUNET_PeerIdentity)))
856     {
857       /* connection to self!? */
858       GNUNET_break (0);
859       return;
860     }
861     GNUNET_break (0 == ntohl (dnm->reserved));
862     LOG (GNUNET_ERROR_TYPE_DEBUG,
863          "Received notification about disconnect from `%s'.\n",
864          GNUNET_i2s (&dnm->peer));
865     pr = GNUNET_CONTAINER_multipeermap_get (h->peers, &dnm->peer);
866     if (NULL == pr)
867     {
868       GNUNET_break (0);
869       reconnect_later (h);
870       return;
871     }
872     trigger = ((pr->prev != NULL) || (pr->next != NULL) ||
873                (h->ready_peer_head == pr));
874     disconnect_and_free_peer_entry (h, &dnm->peer, pr);
875     if (trigger)
876       trigger_next_request (h, GNUNET_NO);
877     break;
878   case GNUNET_MESSAGE_TYPE_CORE_NOTIFY_INBOUND:
879     if (msize < sizeof (struct NotifyTrafficMessage))
880     {
881       GNUNET_break (0);
882       reconnect_later (h);
883       return;
884     }
885     ntm = (const struct NotifyTrafficMessage *) msg;
886     if ((msize <
887          sizeof (struct NotifyTrafficMessage) +
888          sizeof (struct GNUNET_MessageHeader)) )
889     {
890       GNUNET_break (0);
891       reconnect_later (h);
892       return;
893     }
894     em = (const struct GNUNET_MessageHeader *) &ntm[1];
895     LOG (GNUNET_ERROR_TYPE_DEBUG,
896          "Received message of type %u and size %u from peer `%4s'\n",
897          ntohs (em->type), ntohs (em->size), GNUNET_i2s (&ntm->peer));
898     if ((GNUNET_NO == h->inbound_hdr_only) &&
899         (msize !=
900          ntohs (em->size) + sizeof (struct NotifyTrafficMessage)))
901     {
902       GNUNET_break (0);
903       reconnect_later (h);
904       return;
905     }
906     et = ntohs (em->type);
907     for (hpos = 0; hpos < h->hcnt; hpos++)
908     {
909       mh = &h->handlers[hpos];
910       if (mh->type != et)
911         continue;
912       if ((mh->expected_size != ntohs (em->size)) && (mh->expected_size != 0))
913       {
914         LOG (GNUNET_ERROR_TYPE_ERROR,
915              "Unexpected message size %u for message of type %u from peer `%4s'\n",
916              htons (em->size), mh->type, GNUNET_i2s (&ntm->peer));
917         GNUNET_break_op (0);
918         continue;
919       }
920       pr = GNUNET_CONTAINER_multipeermap_get (h->peers, &ntm->peer);
921       if (NULL == pr)
922       {
923         GNUNET_break (0);
924         reconnect_later (h);
925         return;
926       }
927       if (GNUNET_OK !=
928           h->handlers[hpos].callback (h->cls, &ntm->peer, em))
929       {
930         /* error in processing, do not process other messages! */
931         break;
932       }
933     }
934     if (NULL != h->inbound_notify)
935       h->inbound_notify (h->cls, &ntm->peer, em);
936     break;
937   case GNUNET_MESSAGE_TYPE_CORE_NOTIFY_OUTBOUND:
938     if (msize < sizeof (struct NotifyTrafficMessage))
939     {
940       GNUNET_break (0);
941       reconnect_later (h);
942       return;
943     }
944     ntm = (const struct NotifyTrafficMessage *) msg;
945     if ((msize <
946          sizeof (struct NotifyTrafficMessage) +
947          sizeof (struct GNUNET_MessageHeader)) )
948     {
949       GNUNET_break (0);
950       reconnect_later (h);
951       return;
952     }
953     em = (const struct GNUNET_MessageHeader *) &ntm[1];
954     LOG (GNUNET_ERROR_TYPE_DEBUG,
955          "Received notification about transmission to `%s'.\n",
956          GNUNET_i2s (&ntm->peer));
957     if ((GNUNET_NO == h->outbound_hdr_only) &&
958         (msize !=
959          ntohs (em->size) + sizeof (struct NotifyTrafficMessage)))
960     {
961       GNUNET_break (0);
962       reconnect_later (h);
963       return;
964     }
965     if (NULL == h->outbound_notify)
966     {
967       GNUNET_break (0);
968       break;
969     }
970     h->outbound_notify (h->cls, &ntm->peer, em);
971     break;
972   case GNUNET_MESSAGE_TYPE_CORE_SEND_READY:
973     if (msize != sizeof (struct SendMessageReady))
974     {
975       GNUNET_break (0);
976       reconnect_later (h);
977       return;
978     }
979     smr = (const struct SendMessageReady *) msg;
980     pr = GNUNET_CONTAINER_multipeermap_get (h->peers, &smr->peer);
981     if (NULL == pr)
982     {
983       GNUNET_break (0);
984       reconnect_later (h);
985       return;
986     }
987     LOG (GNUNET_ERROR_TYPE_DEBUG,
988          "Received notification about transmission readiness to `%s'.\n",
989          GNUNET_i2s (&smr->peer));
990     if (NULL == pr->th.peer)
991     {
992       /* request must have been cancelled between the original request
993        * and the response from core, ignore core's readiness */
994       break;
995     }
996
997     th = &pr->th;
998     if (ntohs (smr->smr_id) != th->smr_id)
999     {
1000       /* READY message is for expired or cancelled message,
1001        * ignore! (we should have already sent another request) */
1002       break;
1003     }
1004     if ((NULL != pr->prev) || (NULL != pr->next) || (h->ready_peer_head == pr))
1005     {
1006       /* we should not already be on the ready list... */
1007       GNUNET_break (0);
1008       reconnect_later (h);
1009       return;
1010     }
1011     GNUNET_CONTAINER_DLL_insert (h->ready_peer_head, h->ready_peer_tail, pr);
1012     trigger_next_request (h, GNUNET_NO);
1013     break;
1014   default:
1015     reconnect_later (h);
1016     return;
1017   }
1018   GNUNET_CLIENT_receive (h->client, &main_notify_handler, h,
1019                          GNUNET_TIME_UNIT_FOREVER_REL);
1020 }
1021
1022
1023 /**
1024  * Task executed once we are done transmitting the INIT message.
1025  * Starts our 'receive' loop.
1026  *
1027  * @param cls the 'struct GNUNET_CORE_Handle'
1028  * @param success were we successful
1029  */
1030 static void
1031 init_done_task (void *cls, int success)
1032 {
1033   struct GNUNET_CORE_Handle *h = cls;
1034
1035   if (GNUNET_SYSERR == success)
1036     return;                     /* shutdown */
1037   if (GNUNET_NO == success)
1038   {
1039     LOG (GNUNET_ERROR_TYPE_DEBUG,
1040          "Failed to exchange INIT with core, retrying\n");
1041     if (h->reconnect_task == GNUNET_SCHEDULER_NO_TASK)
1042       reconnect_later (h);
1043     return;
1044   }
1045   GNUNET_CLIENT_receive (h->client, &main_notify_handler, h,
1046                          GNUNET_TIME_UNIT_FOREVER_REL);
1047 }
1048
1049
1050 /**
1051  * Our current client connection went down.  Clean it up
1052  * and try to reconnect!
1053  *
1054  * @param h our handle to the core service
1055  */
1056 static void
1057 reconnect (struct GNUNET_CORE_Handle *h)
1058 {
1059   struct ControlMessage *cm;
1060   struct InitMessage *init;
1061   uint32_t opt;
1062   uint16_t msize;
1063   uint16_t *ts;
1064   unsigned int hpos;
1065
1066   GNUNET_assert (NULL == h->client);
1067   GNUNET_assert (h->currently_down == GNUNET_YES);
1068   h->client = GNUNET_CLIENT_connect ("core", h->cfg);
1069   if (NULL == h->client)
1070   {
1071     reconnect_later (h);
1072     return;
1073   }
1074   msize = h->hcnt * sizeof (uint16_t) + sizeof (struct InitMessage);
1075   cm = GNUNET_malloc (sizeof (struct ControlMessage) + msize);
1076   cm->cont = &init_done_task;
1077   cm->cont_cls = h;
1078   init = (struct InitMessage *) &cm[1];
1079   init->header.type = htons (GNUNET_MESSAGE_TYPE_CORE_INIT);
1080   init->header.size = htons (msize);
1081   opt = 0;
1082   if (h->inbound_notify != NULL)
1083   {
1084     if (h->inbound_hdr_only)
1085       opt |= GNUNET_CORE_OPTION_SEND_HDR_INBOUND;
1086     else
1087       opt |= GNUNET_CORE_OPTION_SEND_FULL_INBOUND;
1088   }
1089   if (h->outbound_notify != NULL)
1090   {
1091     if (h->outbound_hdr_only)
1092       opt |= GNUNET_CORE_OPTION_SEND_HDR_OUTBOUND;
1093     else
1094       opt |= GNUNET_CORE_OPTION_SEND_FULL_OUTBOUND;
1095   }
1096   LOG (GNUNET_ERROR_TYPE_INFO,
1097        "(Re)connecting to CORE service, monitoring messages of type %u\n",
1098        opt);
1099
1100   init->options = htonl (opt);
1101   ts = (uint16_t *) & init[1];
1102   for (hpos = 0; hpos < h->hcnt; hpos++)
1103     ts[hpos] = htons (h->handlers[hpos].type);
1104   GNUNET_CONTAINER_DLL_insert (h->control_pending_head, h->control_pending_tail,
1105                                cm);
1106   trigger_next_request (h, GNUNET_YES);
1107 }
1108
1109
1110
1111 /**
1112  * Connect to the core service.  Note that the connection may
1113  * complete (or fail) asynchronously.
1114  *
1115  * @param cfg configuration to use
1116  * @param cls closure for the various callbacks that follow (including handlers in the handlers array)
1117  * @param init callback to call once we have successfully
1118  *        connected to the core service
1119  * @param connects function to call on peer connect, can be NULL
1120  * @param disconnects function to call on peer disconnect / timeout, can be NULL
1121  * @param inbound_notify function to call for all inbound messages, can be NULL
1122  * @param inbound_hdr_only set to #GNUNET_YES if inbound_notify will only read the
1123  *                GNUNET_MessageHeader and hence we do not need to give it the full message;
1124  *                can be used to improve efficiency, ignored if @a inbound_notify is NULLL
1125  * @param outbound_notify function to call for all outbound messages, can be NULL
1126  * @param outbound_hdr_only set to #GNUNET_YES if outbound_notify will only read the
1127  *                GNUNET_MessageHeader and hence we do not need to give it the full message
1128  *                can be used to improve efficiency, ignored if @a outbound_notify is NULLL
1129  * @param handlers callbacks for messages we care about, NULL-terminated
1130  * @return handle to the core service (only useful for disconnect until 'init' is called);
1131  *                NULL on error (in this case, init is never called)
1132  */
1133 struct GNUNET_CORE_Handle *
1134 GNUNET_CORE_connect (const struct GNUNET_CONFIGURATION_Handle *cfg,
1135                      void *cls,
1136                      GNUNET_CORE_StartupCallback init,
1137                      GNUNET_CORE_ConnectEventHandler connects,
1138                      GNUNET_CORE_DisconnectEventHandler disconnects,
1139                      GNUNET_CORE_MessageCallback inbound_notify,
1140                      int inbound_hdr_only,
1141                      GNUNET_CORE_MessageCallback outbound_notify,
1142                      int outbound_hdr_only,
1143                      const struct GNUNET_CORE_MessageHandler *handlers)
1144 {
1145   struct GNUNET_CORE_Handle *h;
1146
1147   GNUNET_assert (NULL != cfg);
1148   h = GNUNET_new (struct GNUNET_CORE_Handle);
1149   h->cfg = cfg;
1150   h->cls = cls;
1151   h->init = init;
1152   h->connects = connects;
1153   h->disconnects = disconnects;
1154   h->inbound_notify = inbound_notify;
1155   h->outbound_notify = outbound_notify;
1156   h->inbound_hdr_only = inbound_hdr_only;
1157   h->outbound_hdr_only = outbound_hdr_only;
1158   h->handlers = handlers;
1159   h->hcnt = 0;
1160   h->currently_down = GNUNET_YES;
1161   h->peers = GNUNET_CONTAINER_multipeermap_create (128, GNUNET_NO);
1162   if (NULL != handlers)
1163     while (handlers[h->hcnt].callback != NULL)
1164       h->hcnt++;
1165   GNUNET_assert (h->hcnt <
1166                  (GNUNET_SERVER_MAX_MESSAGE_SIZE -
1167                   sizeof (struct InitMessage)) / sizeof (uint16_t));
1168   LOG (GNUNET_ERROR_TYPE_DEBUG, "Connecting to CORE service\n");
1169   reconnect (h);
1170   return h;
1171 }
1172
1173
1174 /**
1175  * Disconnect from the core service.  This function can only
1176  * be called *after* all pending 'GNUNET_CORE_notify_transmit_ready'
1177  * requests have been explicitly canceled.
1178  *
1179  * @param handle connection to core to disconnect
1180  */
1181 void
1182 GNUNET_CORE_disconnect (struct GNUNET_CORE_Handle *handle)
1183 {
1184   struct ControlMessage *cm;
1185
1186   GNUNET_assert (NULL != handle);
1187
1188   LOG (GNUNET_ERROR_TYPE_DEBUG, "Disconnecting from CORE service\n");
1189   if (NULL != handle->cth)
1190   {
1191     GNUNET_CLIENT_notify_transmit_ready_cancel (handle->cth);
1192     handle->cth = NULL;
1193   }
1194   while (NULL != (cm = handle->control_pending_head))
1195   {
1196     GNUNET_CONTAINER_DLL_remove (handle->control_pending_head,
1197                                  handle->control_pending_tail, cm);
1198     if (NULL != cm->th)
1199       cm->th->cm = NULL;
1200     if (NULL != cm->cont)
1201       cm->cont (cm->cont_cls, GNUNET_SYSERR);
1202     GNUNET_free (cm);
1203   }
1204   if (NULL != handle->client)
1205   {
1206     GNUNET_CLIENT_disconnect (handle->client);
1207     handle->client = NULL;
1208   }
1209   GNUNET_CONTAINER_multipeermap_iterate (handle->peers,
1210                                          &disconnect_and_free_peer_entry,
1211                                          handle);
1212   if (handle->reconnect_task != GNUNET_SCHEDULER_NO_TASK)
1213   {
1214     GNUNET_SCHEDULER_cancel (handle->reconnect_task);
1215     handle->reconnect_task = GNUNET_SCHEDULER_NO_TASK;
1216   }
1217   GNUNET_CONTAINER_multipeermap_destroy (handle->peers);
1218   handle->peers = NULL;
1219   GNUNET_break (handle->ready_peer_head == NULL);
1220   GNUNET_free (handle);
1221 }
1222
1223
1224 /**
1225  * Task that calls 'request_next_transmission'.
1226  *
1227  * @param cls the 'struct PeerRecord *'
1228  * @param tc scheduler context
1229  */
1230 static void
1231 run_request_next_transmission (void *cls,
1232                                const struct GNUNET_SCHEDULER_TaskContext *tc)
1233 {
1234   struct PeerRecord *pr = cls;
1235
1236   pr->ntr_task = GNUNET_SCHEDULER_NO_TASK;
1237   request_next_transmission (pr);
1238 }
1239
1240
1241 /**
1242  * Ask the core to call @a notify once it is ready to transmit the
1243  * given number of bytes to the specified @a target.  Must only be
1244  * called after a connection to the respective peer has been
1245  * established (and the client has been informed about this).  You may
1246  * have one request of this type pending for each connected peer at
1247  * any time.  If a peer disconnects, the application MUST call
1248  * #GNUNET_CORE_notify_transmit_ready_cancel on the respective
1249  * transmission request, if one such request is pending.
1250  *
1251  * @param handle connection to core service
1252  * @param cork is corking allowed for this transmission?
1253  * @param priority how important is the message?
1254  * @param maxdelay how long can the message wait?
1255  * @param target who should receive the message, never NULL (can be this peer's identity for loopback)
1256  * @param notify_size how many bytes of buffer space does @a notify want?
1257  * @param notify function to call when buffer space is available;
1258  *        will be called with NULL on timeout; clients MUST cancel
1259  *        all pending transmission requests DURING the disconnect
1260  *        handler
1261  * @param notify_cls closure for notify
1262  * @return non-NULL if the notify callback was queued,
1263  *         NULL if we can not even queue the request (request already pending);
1264  *         if NULL is returned, @a notify will NOT be called.
1265  */
1266 struct GNUNET_CORE_TransmitHandle *
1267 GNUNET_CORE_notify_transmit_ready (struct GNUNET_CORE_Handle *handle, int cork,
1268                                    uint32_t priority,
1269                                    struct GNUNET_TIME_Relative maxdelay,
1270                                    const struct GNUNET_PeerIdentity *target,
1271                                    size_t notify_size,
1272                                    GNUNET_CONNECTION_TransmitReadyNotify notify,
1273                                    void *notify_cls)
1274 {
1275   struct PeerRecord *pr;
1276   struct GNUNET_CORE_TransmitHandle *th;
1277
1278   GNUNET_assert (NULL != handle);
1279   GNUNET_assert (NULL != target);
1280
1281   if (notify_size > GNUNET_CONSTANTS_MAX_ENCRYPTED_MESSAGE_SIZE)
1282   {
1283      GNUNET_break (0);
1284      return NULL;
1285   }
1286   GNUNET_assert (NULL != notify);
1287   LOG (GNUNET_ERROR_TYPE_DEBUG,
1288        "Asking core for transmission of %u bytes to `%s'\n",
1289        (unsigned int) notify_size,
1290        GNUNET_i2s (target));
1291   pr = GNUNET_CONTAINER_multipeermap_get (handle->peers, target);
1292   if (NULL == pr)
1293   {
1294     /* attempt to send to peer that is not connected */
1295     GNUNET_break (0);
1296     return NULL;
1297   }
1298   if (NULL != pr->th.peer)
1299   {
1300     /* attempting to queue a second request for the same destination */
1301     GNUNET_break (0);
1302     return NULL;
1303   }
1304   GNUNET_assert (notify_size + sizeof (struct SendMessage) <
1305                  GNUNET_SERVER_MAX_MESSAGE_SIZE);
1306   th = &pr->th;
1307   memset (th, 0, sizeof (struct GNUNET_CORE_TransmitHandle));
1308   th->peer = pr;
1309   th->get_message = notify;
1310   th->get_message_cls = notify_cls;
1311   th->timeout = GNUNET_TIME_relative_to_absolute (maxdelay);
1312   th->priority = priority;
1313   th->msize = notify_size;
1314   th->cork = cork;
1315   GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == pr->ntr_task);
1316   pr->ntr_task =
1317     GNUNET_SCHEDULER_add_now (&run_request_next_transmission, pr);
1318   LOG (GNUNET_ERROR_TYPE_DEBUG, "Transmission request added to queue\n");
1319   return th;
1320 }
1321
1322
1323 /**
1324  * Cancel the specified transmission-ready notification.
1325  *
1326  * @param th handle that was returned by "notify_transmit_ready".
1327  */
1328 void
1329 GNUNET_CORE_notify_transmit_ready_cancel (struct GNUNET_CORE_TransmitHandle *th)
1330 {
1331   struct PeerRecord *pr = th->peer;
1332   struct GNUNET_CORE_Handle *h;
1333
1334   GNUNET_assert (NULL != th);
1335   GNUNET_assert (NULL != pr);
1336   LOG (GNUNET_ERROR_TYPE_DEBUG,
1337        "Aborting transmission request to core for %u bytes to `%s'\n",
1338        (unsigned int) th->msize,
1339        GNUNET_i2s (&pr->peer));
1340   th->peer = NULL;
1341   h = pr->ch;
1342   if (NULL != th->cm)
1343   {
1344     /* we're currently in the control queue, remove */
1345     GNUNET_CONTAINER_DLL_remove (h->control_pending_head,
1346                                  h->control_pending_tail, th->cm);
1347     GNUNET_free (th->cm);
1348     th->cm = NULL;
1349   }
1350   if ((NULL != pr->prev) || (NULL != pr->next) || (pr == h->ready_peer_head))
1351   {
1352     /* the request that was 'approved' by core was
1353      * canceled before it could be transmitted; remove
1354      * us from the 'ready' list */
1355     GNUNET_CONTAINER_DLL_remove (h->ready_peer_head, h->ready_peer_tail, pr);
1356   }
1357   if (GNUNET_SCHEDULER_NO_TASK != pr->ntr_task)
1358   {
1359     GNUNET_SCHEDULER_cancel (pr->ntr_task);
1360     pr->ntr_task = GNUNET_SCHEDULER_NO_TASK;
1361   }
1362 }
1363
1364
1365 /**
1366  * Check if the given peer is currently connected. This function is for special
1367  * cirumstances (GNUNET_TESTBED uses it), normal users of the CORE API are
1368  * expected to track which peers are connected based on the connect/disconnect
1369  * callbacks from GNUNET_CORE_connect.  This function is NOT part of the
1370  * 'versioned', 'official' API. The difference between this function and the
1371  * function GNUNET_CORE_is_peer_connected() is that this one returns
1372  * synchronously after looking in the CORE API cache. The function
1373  * GNUNET_CORE_is_peer_connected() sends a message to the CORE service and hence
1374  * its response is given asynchronously.
1375  *
1376  * @param h the core handle
1377  * @param pid the identity of the peer to check if it has been connected to us
1378  * @return GNUNET_YES if the peer is connected to us; GNUNET_NO if not
1379  */
1380 int
1381 GNUNET_CORE_is_peer_connected_sync (const struct GNUNET_CORE_Handle *h,
1382                                     const struct GNUNET_PeerIdentity *pid)
1383 {
1384   GNUNET_assert (NULL != h);
1385   GNUNET_assert (NULL != pid);
1386   return GNUNET_CONTAINER_multipeermap_contains (h->peers, pid);
1387 }
1388
1389
1390 /* end of core_api.c */