how did this test ever work without assigning core handles?
[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_MultiHashMap *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, const struct GNUNET_HashCode * key,
361                                 void *value)
362 {
363   struct GNUNET_CORE_Handle *h = cls;
364   struct GNUNET_CORE_TransmitHandle *th;
365   struct PeerRecord *pr = value;
366
367   if (GNUNET_SCHEDULER_NO_TASK != pr->timeout_task)
368   {
369     GNUNET_SCHEDULER_cancel (pr->timeout_task);
370     pr->timeout_task = GNUNET_SCHEDULER_NO_TASK;
371   }
372   if (GNUNET_SCHEDULER_NO_TASK != pr->ntr_task)
373   {
374     GNUNET_SCHEDULER_cancel (pr->ntr_task);
375     pr->ntr_task = GNUNET_SCHEDULER_NO_TASK;
376   }
377   if ((NULL != pr->prev) || (NULL != pr->next) || (h->ready_peer_head == pr))
378     GNUNET_CONTAINER_DLL_remove (h->ready_peer_head, h->ready_peer_tail, pr);
379   if (NULL != h->disconnects)
380     h->disconnects (h->cls, &pr->peer);
381   /* all requests should have been cancelled, clean up anyway, just in case */
382   th = &pr->th;
383   if (NULL != th->peer)
384   {
385     GNUNET_break (0);
386     th->peer = NULL;
387     if (NULL != th->cm)
388       th->cm->th = NULL;
389   }
390   /* done with 'voluntary' cleanups, now on to normal freeing */
391   GNUNET_assert (GNUNET_YES ==
392                  GNUNET_CONTAINER_multihashmap_remove (h->peers, key, pr));
393   GNUNET_assert (pr->ch == h);
394   GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == pr->timeout_task);
395   GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == pr->ntr_task);
396   GNUNET_free (pr);
397   return GNUNET_YES;
398 }
399
400
401 /**
402  * Close down any existing connection to the CORE service and
403  * try re-establishing it later.
404  *
405  * @param h our handle
406  */
407 static void
408 reconnect_later (struct GNUNET_CORE_Handle *h)
409 {
410   struct ControlMessage *cm;
411   struct PeerRecord *pr;
412
413   GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == h->reconnect_task);
414   if (NULL != h->cth)
415   {
416     GNUNET_CLIENT_notify_transmit_ready_cancel (h->cth);
417     h->cth = NULL;
418   }
419   if (NULL != h->client)
420   {
421     GNUNET_CLIENT_disconnect (h->client);
422     h->client = NULL;
423   }
424   h->currently_down = GNUNET_YES;
425   GNUNET_assert (h->reconnect_task == GNUNET_SCHEDULER_NO_TASK);
426   h->reconnect_task =
427       GNUNET_SCHEDULER_add_delayed (h->retry_backoff, &reconnect_task, h);
428   while (NULL != (cm = h->control_pending_head))
429   {
430     GNUNET_CONTAINER_DLL_remove (h->control_pending_head,
431                                  h->control_pending_tail, cm);
432     if (NULL != cm->th)
433       cm->th->cm = NULL;
434     if (NULL != cm->cont)
435       cm->cont (cm->cont_cls, GNUNET_NO);
436     GNUNET_free (cm);
437   }
438   GNUNET_CONTAINER_multihashmap_iterate (h->peers,
439                                          &disconnect_and_free_peer_entry, h);
440   while (NULL != (pr = h->ready_peer_head))
441     GNUNET_CONTAINER_DLL_remove (h->ready_peer_head, h->ready_peer_tail, pr);
442   GNUNET_assert (h->control_pending_head == NULL);
443   h->retry_backoff = GNUNET_TIME_STD_BACKOFF (h->retry_backoff);
444 }
445
446
447 /**
448  * Check the list of pending requests, send the next
449  * one to the core.
450  *
451  * @param h core handle
452  * @param ignore_currently_down transmit message even if not initialized?
453  */
454 static void
455 trigger_next_request (struct GNUNET_CORE_Handle *h, int ignore_currently_down);
456
457
458 /**
459  * The given request hit its timeout.  Remove from the
460  * doubly-linked list and call the respective continuation.
461  *
462  * @param cls the transmit handle of the request that timed out
463  * @param tc context, can be NULL (!)
464  */
465 static void
466 transmission_timeout (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
467
468
469 /**
470  * Send a control message to the peer asking for transmission
471  * of the message in the given peer record.
472  *
473  * @param pr peer to request transmission to
474  */
475 static void
476 request_next_transmission (struct PeerRecord *pr)
477 {
478   struct GNUNET_CORE_Handle *h = pr->ch;
479   struct ControlMessage *cm;
480   struct SendMessageRequest *smr;
481   struct GNUNET_CORE_TransmitHandle *th;
482
483   if (pr->timeout_task != GNUNET_SCHEDULER_NO_TASK)
484   {
485     GNUNET_SCHEDULER_cancel (pr->timeout_task);
486     pr->timeout_task = GNUNET_SCHEDULER_NO_TASK;
487   }
488   th = &pr->th;
489   if (NULL == th->peer)
490   {
491     trigger_next_request (h, GNUNET_NO);
492     return;
493   }
494   if (th->cm != NULL)
495     return;                     /* already done */
496   GNUNET_assert (pr->prev == NULL);
497   GNUNET_assert (pr->next == NULL);
498   pr->timeout_task =
499       GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_absolute_get_remaining
500                                     (th->timeout), &transmission_timeout, pr);
501   cm = GNUNET_malloc (sizeof (struct ControlMessage) +
502                       sizeof (struct SendMessageRequest));
503   th->cm = cm;
504   cm->th = th;
505   smr = (struct SendMessageRequest *) &cm[1];
506   smr->header.type = htons (GNUNET_MESSAGE_TYPE_CORE_SEND_REQUEST);
507   smr->header.size = htons (sizeof (struct SendMessageRequest));
508   smr->priority = htonl (th->priority);
509   smr->deadline = GNUNET_TIME_absolute_hton (th->timeout);
510   smr->peer = pr->peer;
511   smr->reserved = htonl (0);
512   smr->size = htons (th->msize);
513   smr->smr_id = htons (th->smr_id = pr->smr_id_gen++);
514   GNUNET_CONTAINER_DLL_insert_tail (h->control_pending_head,
515                                     h->control_pending_tail, cm);
516   LOG (GNUNET_ERROR_TYPE_DEBUG,
517        "Adding SEND REQUEST for peer `%s' to message queue\n",
518        GNUNET_i2s (&pr->peer));
519   trigger_next_request (h, GNUNET_NO);
520 }
521
522
523 /**
524  * The given request hit its timeout.  Remove from the
525  * doubly-linked list and call the respective continuation.
526  *
527  * @param cls the transmit handle of the request that timed out
528  * @param tc context, can be NULL (!)
529  */
530 static void
531 transmission_timeout (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
532 {
533   struct PeerRecord *pr = cls;
534   struct GNUNET_CORE_Handle *h = pr->ch;
535   struct GNUNET_CORE_TransmitHandle *th;
536
537   pr->timeout_task = GNUNET_SCHEDULER_NO_TASK;
538   if (GNUNET_SCHEDULER_NO_TASK != pr->ntr_task)
539   {
540     GNUNET_SCHEDULER_cancel (pr->ntr_task);
541     pr->ntr_task = GNUNET_SCHEDULER_NO_TASK;
542   }
543   th = &pr->th;
544   th->peer = NULL;
545   if ((NULL != pr->prev) || (NULL != pr->next) || (pr == h->ready_peer_head))
546   {
547     /* the request that was 'approved' by core was
548      * canceled before it could be transmitted; remove
549      * us from the 'ready' list */
550     GNUNET_CONTAINER_DLL_remove (h->ready_peer_head, h->ready_peer_tail, pr);
551   }
552   if (NULL != th->cm)
553   {
554      /* we're currently in the control queue, remove */
555     GNUNET_CONTAINER_DLL_remove (h->control_pending_head,
556                                  h->control_pending_tail, th->cm);
557     GNUNET_free (th->cm);
558   }
559   LOG (GNUNET_ERROR_TYPE_DEBUG,
560        "Signalling timeout of request for transmission to peer `%s' via CORE\n",
561        GNUNET_i2s (&pr->peer));
562   trigger_next_request (h, GNUNET_NO);
563
564   GNUNET_assert (0 == th->get_message (th->get_message_cls, 0, NULL));
565 }
566
567
568 /**
569  * Transmit the next message to the core service.
570  *
571  * @param cls closure with the 'struct GNUNET_CORE_Handle'
572  * @param size number of bytes available in @a buf
573  * @param buf where the callee should write the message
574  * @return number of bytes written to buf 
575  */
576 static size_t
577 transmit_message (void *cls, size_t size, void *buf)
578 {
579   struct GNUNET_CORE_Handle *h = cls;
580   struct ControlMessage *cm;
581   struct GNUNET_CORE_TransmitHandle *th;
582   struct PeerRecord *pr;
583   struct SendMessage *sm;
584   const struct GNUNET_MessageHeader *hdr;
585   uint16_t msize;
586   size_t ret;
587
588   GNUNET_assert (h->reconnect_task == GNUNET_SCHEDULER_NO_TASK);
589   h->cth = NULL;
590   if (NULL == buf)
591   {
592     LOG (GNUNET_ERROR_TYPE_DEBUG,
593          "Transmission failed, initiating reconnect\n");
594     reconnect_later (h);
595     return 0;
596   }
597   /* first check for control messages */
598   if (NULL != (cm = h->control_pending_head))
599   {
600     hdr = (const struct GNUNET_MessageHeader *) &cm[1];
601     msize = ntohs (hdr->size);
602     if (size < msize)
603     {
604       trigger_next_request (h, GNUNET_NO);
605       return 0;
606     }
607     LOG (GNUNET_ERROR_TYPE_DEBUG,
608          "Transmitting control message with %u bytes of type %u to core.\n",
609          (unsigned int) msize, (unsigned int) ntohs (hdr->type));
610     memcpy (buf, hdr, msize);
611     GNUNET_CONTAINER_DLL_remove (h->control_pending_head,
612                                  h->control_pending_tail, cm);
613     if (NULL != cm->th)
614       cm->th->cm = NULL;
615     if (NULL != cm->cont)
616       cm->cont (cm->cont_cls, GNUNET_OK);
617     GNUNET_free (cm);
618     trigger_next_request (h, GNUNET_NO);
619     return msize;
620   }
621   /* now check for 'ready' P2P messages */
622   if (NULL == (pr = h->ready_peer_head))
623     return 0;
624   GNUNET_assert (NULL != pr->th.peer);
625   th = &pr->th;
626   if (size < th->msize + sizeof (struct SendMessage))
627   {
628     trigger_next_request (h, GNUNET_NO);
629     return 0;
630   }
631   GNUNET_CONTAINER_DLL_remove (h->ready_peer_head, h->ready_peer_tail, pr);
632   th->peer = NULL;
633   if (GNUNET_SCHEDULER_NO_TASK != pr->timeout_task)
634   {
635     GNUNET_SCHEDULER_cancel (pr->timeout_task);
636     pr->timeout_task = GNUNET_SCHEDULER_NO_TASK;
637   }
638   LOG (GNUNET_ERROR_TYPE_DEBUG,
639        "Transmitting SEND request to `%s' with %u bytes.\n",
640        GNUNET_i2s (&pr->peer), (unsigned int) th->msize);
641   sm = (struct SendMessage *) buf;
642   sm->header.type = htons (GNUNET_MESSAGE_TYPE_CORE_SEND);
643   sm->priority = htonl (th->priority);
644   sm->deadline = GNUNET_TIME_absolute_hton (th->timeout);
645   sm->peer = pr->peer;
646   sm->cork = htonl ((uint32_t) th->cork);
647   sm->reserved = htonl (0);
648   ret =
649     th->get_message (th->get_message_cls,
650                      size - sizeof (struct SendMessage), &sm[1]);
651   
652   LOG (GNUNET_ERROR_TYPE_DEBUG,
653        "Transmitting SEND request to `%s' yielded %u bytes.\n",
654        GNUNET_i2s (&pr->peer), ret);
655   if (0 == ret)
656   {
657     LOG (GNUNET_ERROR_TYPE_DEBUG,
658          "Size of clients message to peer %s is 0!\n",
659          GNUNET_i2s (&pr->peer));
660     /* client decided to send nothing! */
661     request_next_transmission (pr);
662     return 0;
663   }
664   LOG (GNUNET_ERROR_TYPE_DEBUG,
665        "Produced SEND message to core with %u bytes payload\n",
666        (unsigned int) ret);
667   GNUNET_assert (ret >= sizeof (struct GNUNET_MessageHeader));
668   if (ret + sizeof (struct SendMessage) >= GNUNET_SERVER_MAX_MESSAGE_SIZE)
669   {
670     GNUNET_break (0);
671     request_next_transmission (pr);
672     return 0;
673   }
674   ret += sizeof (struct SendMessage);
675   sm->header.size = htons (ret);
676   GNUNET_assert (ret <= size);
677   request_next_transmission (pr);
678   return ret;
679 }
680
681
682 /**
683  * Check the list of pending requests, send the next
684  * one to the core.
685  *
686  * @param h core handle
687  * @param ignore_currently_down transmit message even if not initialized?
688  */
689 static void
690 trigger_next_request (struct GNUNET_CORE_Handle *h, int ignore_currently_down)
691 {
692   uint16_t msize;
693
694   if ((GNUNET_YES == h->currently_down) && (ignore_currently_down == GNUNET_NO))
695   {
696     LOG (GNUNET_ERROR_TYPE_DEBUG,
697          "Core connection down, not processing queue\n");
698     return;
699   }
700   if (NULL != h->cth)
701   {
702     LOG (GNUNET_ERROR_TYPE_DEBUG, "Request pending, not processing queue\n");
703     return;
704   }
705   if (NULL != h->control_pending_head)
706     msize =
707         ntohs (((struct GNUNET_MessageHeader *) &h->
708                 control_pending_head[1])->size);
709   else if (h->ready_peer_head != NULL)
710     msize =
711       h->ready_peer_head->th.msize + sizeof (struct SendMessage);
712   else
713   {
714     LOG (GNUNET_ERROR_TYPE_DEBUG,
715          "Request queue empty, not processing queue\n");
716     return;                     /* no pending message */
717   }
718   h->cth =
719       GNUNET_CLIENT_notify_transmit_ready (h->client, msize,
720                                            GNUNET_TIME_UNIT_FOREVER_REL,
721                                            GNUNET_NO, &transmit_message, h);
722 }
723
724
725 /**
726  * Handler for notification messages received from the core.
727  *
728  * @param cls our "struct GNUNET_CORE_Handle"
729  * @param msg the message received from the core service
730  */
731 static void
732 main_notify_handler (void *cls, const struct GNUNET_MessageHeader *msg)
733 {
734   struct GNUNET_CORE_Handle *h = cls;
735   const struct InitReplyMessage *m;
736   const struct ConnectNotifyMessage *cnm;
737   const struct DisconnectNotifyMessage *dnm;
738   const struct NotifyTrafficMessage *ntm;
739   const struct GNUNET_MessageHeader *em;
740   const struct SendMessageReady *smr;
741   const struct GNUNET_CORE_MessageHandler *mh;
742   GNUNET_CORE_StartupCallback init;
743   struct PeerRecord *pr;
744   struct GNUNET_CORE_TransmitHandle *th;
745   unsigned int hpos;
746   int trigger;
747   uint16_t msize;
748   uint16_t et;
749   if (NULL == msg)
750   {
751     LOG (GNUNET_ERROR_TYPE_INFO,
752          _("Client was disconnected from core service, trying to reconnect.\n"));
753     reconnect_later (h);
754     return;
755   }
756   msize = ntohs (msg->size);
757   LOG (GNUNET_ERROR_TYPE_DEBUG,
758        "Processing message of type %u and size %u from core service\n",
759        ntohs (msg->type), msize);
760   switch (ntohs (msg->type))
761   {
762   case GNUNET_MESSAGE_TYPE_CORE_INIT_REPLY:
763     if (ntohs (msg->size) != sizeof (struct InitReplyMessage))
764     {
765       GNUNET_break (0);
766       reconnect_later (h);
767       return;
768     }
769     m = (const struct InitReplyMessage *) msg;
770     GNUNET_break (0 == ntohl (m->reserved));
771     /* start our message processing loop */
772     if (GNUNET_YES == h->currently_down)
773     {
774       h->currently_down = GNUNET_NO;
775       trigger_next_request (h, GNUNET_NO);
776     }
777     h->retry_backoff = GNUNET_TIME_UNIT_MILLISECONDS;
778     h->me = m->my_identity;
779     if (NULL != (init = h->init))
780     {
781       /* mark so we don't call init on reconnect */
782       h->init = NULL;
783       LOG (GNUNET_ERROR_TYPE_DEBUG, "Connected to core service of peer `%s'.\n",
784            GNUNET_i2s (&h->me));
785       init (h->cls, &h->me);
786     }
787     else
788     {
789       LOG (GNUNET_ERROR_TYPE_DEBUG,
790            "Successfully reconnected to core service.\n");
791     }
792     /* fake 'connect to self' */
793     pr = GNUNET_CONTAINER_multihashmap_get (h->peers, &h->me.hashPubKey);
794     GNUNET_assert (NULL == pr);
795     pr = GNUNET_new (struct PeerRecord);
796     pr->peer = h->me;
797     pr->ch = h;
798     GNUNET_assert (GNUNET_YES ==
799                    GNUNET_CONTAINER_multihashmap_put (h->peers,
800                                                       &h->me.hashPubKey, pr,
801                                                       GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST));
802     if (NULL != h->connects)
803       h->connects (h->cls, &h->me);
804     break;
805   case GNUNET_MESSAGE_TYPE_CORE_NOTIFY_CONNECT:
806     if (msize < sizeof (struct ConnectNotifyMessage))
807     {
808       GNUNET_break (0);
809       reconnect_later (h);
810       return;
811     }
812     cnm = (const struct ConnectNotifyMessage *) msg;
813     if (msize !=
814         sizeof (struct ConnectNotifyMessage))
815     {
816       GNUNET_break (0);
817       reconnect_later (h);
818       return;
819     }
820     LOG (GNUNET_ERROR_TYPE_DEBUG,
821          "Received notification about connection from `%s'.\n",
822          GNUNET_i2s (&cnm->peer));
823     if (0 == memcmp (&h->me, &cnm->peer, sizeof (struct GNUNET_PeerIdentity)))
824     {
825       /* connect to self!? */
826       GNUNET_break (0);
827       return;
828     }
829     pr = GNUNET_CONTAINER_multihashmap_get (h->peers, &cnm->peer.hashPubKey);
830     if (NULL != pr)
831     {
832       GNUNET_break (0);
833       reconnect_later (h);
834       return;
835     }
836     pr = GNUNET_new (struct PeerRecord);
837     pr->peer = cnm->peer;
838     pr->ch = h;
839     GNUNET_assert (GNUNET_YES ==
840                    GNUNET_CONTAINER_multihashmap_put (h->peers,
841                                                       &cnm->peer.hashPubKey, pr,
842                                                       GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST));
843     if (NULL != h->connects)
844       h->connects (h->cls, &cnm->peer);
845     break;
846   case GNUNET_MESSAGE_TYPE_CORE_NOTIFY_DISCONNECT:
847     if (msize != sizeof (struct DisconnectNotifyMessage))
848     {
849       GNUNET_break (0);
850       reconnect_later (h);
851       return;
852     }
853     dnm = (const struct DisconnectNotifyMessage *) msg;
854     if (0 == memcmp (&h->me, &dnm->peer, sizeof (struct GNUNET_PeerIdentity)))
855     {
856       /* connection to self!? */
857       GNUNET_break (0);
858       return;
859     }
860     GNUNET_break (0 == ntohl (dnm->reserved));
861     LOG (GNUNET_ERROR_TYPE_DEBUG,
862          "Received notification about disconnect from `%s'.\n",
863          GNUNET_i2s (&dnm->peer));
864     pr = GNUNET_CONTAINER_multihashmap_get (h->peers, &dnm->peer.hashPubKey);
865     if (NULL == pr)
866     {
867       GNUNET_break (0);
868       reconnect_later (h);
869       return;
870     }
871     trigger = ((pr->prev != NULL) || (pr->next != NULL) ||
872                (h->ready_peer_head == pr));
873     disconnect_and_free_peer_entry (h, &dnm->peer.hashPubKey, pr);
874     if (trigger)
875       trigger_next_request (h, GNUNET_NO);
876     break;
877   case GNUNET_MESSAGE_TYPE_CORE_NOTIFY_INBOUND:
878     if (msize < sizeof (struct NotifyTrafficMessage))
879     {
880       GNUNET_break (0);
881       reconnect_later (h);
882       return;
883     }
884     ntm = (const struct NotifyTrafficMessage *) msg;
885     if ((msize <
886          sizeof (struct NotifyTrafficMessage) +
887          sizeof (struct GNUNET_MessageHeader)) )
888     {
889       GNUNET_break (0);
890       reconnect_later (h);
891       return;
892     }
893     em = (const struct GNUNET_MessageHeader *) &ntm[1];
894     LOG (GNUNET_ERROR_TYPE_DEBUG,
895          "Received message of type %u and size %u from peer `%4s'\n",
896          ntohs (em->type), ntohs (em->size), GNUNET_i2s (&ntm->peer));
897     if ((GNUNET_NO == h->inbound_hdr_only) &&
898         (msize !=
899          ntohs (em->size) + sizeof (struct NotifyTrafficMessage)))
900     {
901       GNUNET_break (0);
902       reconnect_later (h);
903       return;
904     }
905     et = ntohs (em->type);
906     for (hpos = 0; hpos < h->hcnt; hpos++)
907     {
908       mh = &h->handlers[hpos];
909       if (mh->type != et)
910         continue;
911       if ((mh->expected_size != ntohs (em->size)) && (mh->expected_size != 0))
912       {
913         LOG (GNUNET_ERROR_TYPE_ERROR,
914              "Unexpected message size %u for message of type %u from peer `%4s'\n",
915              htons (em->size), mh->type, GNUNET_i2s (&ntm->peer));
916         GNUNET_break_op (0);
917         continue;
918       }
919       pr = GNUNET_CONTAINER_multihashmap_get (h->peers, &ntm->peer.hashPubKey);
920       if (NULL == pr)
921       {
922         GNUNET_break (0);
923         reconnect_later (h);
924         return;
925       }
926       if (GNUNET_OK !=
927           h->handlers[hpos].callback (h->cls, &ntm->peer, em))
928       {
929         /* error in processing, do not process other messages! */
930         break;
931       }
932     }
933     if (NULL != h->inbound_notify)
934       h->inbound_notify (h->cls, &ntm->peer, em);
935     break;
936   case GNUNET_MESSAGE_TYPE_CORE_NOTIFY_OUTBOUND:
937     if (msize < sizeof (struct NotifyTrafficMessage))
938     {
939       GNUNET_break (0);
940       reconnect_later (h);
941       return;
942     }
943     ntm = (const struct NotifyTrafficMessage *) msg;
944     if ((msize <
945          sizeof (struct NotifyTrafficMessage) +
946          sizeof (struct GNUNET_MessageHeader)) )
947     {
948       GNUNET_break (0);
949       reconnect_later (h);
950       return;
951     }
952     em = (const struct GNUNET_MessageHeader *) &ntm[1];
953     LOG (GNUNET_ERROR_TYPE_DEBUG,
954          "Received notification about transmission to `%s'.\n",
955          GNUNET_i2s (&ntm->peer));
956     if ((GNUNET_NO == h->outbound_hdr_only) &&
957         (msize !=
958          ntohs (em->size) + sizeof (struct NotifyTrafficMessage)))
959     {
960       GNUNET_break (0);
961       reconnect_later (h);
962       return;
963     }
964     if (NULL == h->outbound_notify)
965     {
966       GNUNET_break (0);
967       break;
968     }
969     h->outbound_notify (h->cls, &ntm->peer, em);
970     break;
971   case GNUNET_MESSAGE_TYPE_CORE_SEND_READY:
972     if (msize != sizeof (struct SendMessageReady))
973     {
974       GNUNET_break (0);
975       reconnect_later (h);
976       return;
977     }
978     smr = (const struct SendMessageReady *) msg;
979     pr = GNUNET_CONTAINER_multihashmap_get (h->peers, &smr->peer.hashPubKey);
980     if (NULL == pr)
981     {
982       GNUNET_break (0);
983       reconnect_later (h);
984       return;
985     }
986     LOG (GNUNET_ERROR_TYPE_DEBUG,
987          "Received notification about transmission readiness to `%s'.\n",
988          GNUNET_i2s (&smr->peer));
989     if (NULL == pr->th.peer)
990     {
991       /* request must have been cancelled between the original request
992        * and the response from core, ignore core's readiness */
993       break;
994     }
995
996     th = &pr->th;
997     if (ntohs (smr->smr_id) != th->smr_id)
998     {
999       /* READY message is for expired or cancelled message,
1000        * ignore! (we should have already sent another request) */
1001       break;
1002     }
1003     if ((NULL != pr->prev) || (NULL != pr->next) || (h->ready_peer_head == pr))
1004     {
1005       /* we should not already be on the ready list... */
1006       GNUNET_break (0);
1007       reconnect_later (h);
1008       return;
1009     }
1010     GNUNET_CONTAINER_DLL_insert (h->ready_peer_head, h->ready_peer_tail, pr);
1011     trigger_next_request (h, GNUNET_NO);
1012     break;
1013   default:
1014     reconnect_later (h);
1015     return;
1016   }
1017   GNUNET_CLIENT_receive (h->client, &main_notify_handler, h,
1018                          GNUNET_TIME_UNIT_FOREVER_REL);
1019 }
1020
1021
1022 /**
1023  * Task executed once we are done transmitting the INIT message.
1024  * Starts our 'receive' loop.
1025  *
1026  * @param cls the 'struct GNUNET_CORE_Handle'
1027  * @param success were we successful
1028  */
1029 static void
1030 init_done_task (void *cls, int success)
1031 {
1032   struct GNUNET_CORE_Handle *h = cls;
1033
1034   if (GNUNET_SYSERR == success)
1035     return;                     /* shutdown */
1036   if (GNUNET_NO == success)
1037   {
1038     LOG (GNUNET_ERROR_TYPE_DEBUG,
1039          "Failed to exchange INIT with core, retrying\n");
1040     if (h->reconnect_task == GNUNET_SCHEDULER_NO_TASK)
1041       reconnect_later (h);
1042     return;
1043   }
1044   GNUNET_CLIENT_receive (h->client, &main_notify_handler, h,
1045                          GNUNET_TIME_UNIT_FOREVER_REL);
1046 }
1047
1048
1049 /**
1050  * Our current client connection went down.  Clean it up
1051  * and try to reconnect!
1052  *
1053  * @param h our handle to the core service
1054  */
1055 static void
1056 reconnect (struct GNUNET_CORE_Handle *h)
1057 {
1058   struct ControlMessage *cm;
1059   struct InitMessage *init;
1060   uint32_t opt;
1061   uint16_t msize;
1062   uint16_t *ts;
1063   unsigned int hpos;
1064
1065   GNUNET_assert (NULL == h->client);
1066   GNUNET_assert (h->currently_down == GNUNET_YES);
1067   h->client = GNUNET_CLIENT_connect ("core", h->cfg);
1068   if (NULL == h->client)
1069   {
1070     reconnect_later (h);
1071     return;
1072   }
1073   msize = h->hcnt * sizeof (uint16_t) + sizeof (struct InitMessage);
1074   cm = GNUNET_malloc (sizeof (struct ControlMessage) + msize);
1075   cm->cont = &init_done_task;
1076   cm->cont_cls = h;
1077   init = (struct InitMessage *) &cm[1];
1078   init->header.type = htons (GNUNET_MESSAGE_TYPE_CORE_INIT);
1079   init->header.size = htons (msize);
1080   opt = 0;
1081   if (h->inbound_notify != NULL)
1082   {
1083     if (h->inbound_hdr_only)
1084       opt |= GNUNET_CORE_OPTION_SEND_HDR_INBOUND;
1085     else
1086       opt |= GNUNET_CORE_OPTION_SEND_FULL_INBOUND;
1087   }
1088   if (h->outbound_notify != NULL)
1089   {
1090     if (h->outbound_hdr_only)
1091       opt |= GNUNET_CORE_OPTION_SEND_HDR_OUTBOUND;
1092     else
1093       opt |= GNUNET_CORE_OPTION_SEND_FULL_OUTBOUND;
1094   }
1095   LOG (GNUNET_ERROR_TYPE_INFO, 
1096        "(Re)connecting to CORE service, monitoring messages of type %u\n",
1097        opt);
1098
1099   init->options = htonl (opt);
1100   ts = (uint16_t *) & init[1];
1101   for (hpos = 0; hpos < h->hcnt; hpos++)
1102     ts[hpos] = htons (h->handlers[hpos].type);
1103   GNUNET_CONTAINER_DLL_insert (h->control_pending_head, h->control_pending_tail,
1104                                cm);
1105   trigger_next_request (h, GNUNET_YES);
1106 }
1107
1108
1109
1110 /**
1111  * Connect to the core service.  Note that the connection may
1112  * complete (or fail) asynchronously.
1113  *
1114  * @param cfg configuration to use
1115  * @param cls closure for the various callbacks that follow (including handlers in the handlers array)
1116  * @param init callback to call once we have successfully
1117  *        connected to the core service
1118  * @param connects function to call on peer connect, can be NULL
1119  * @param disconnects function to call on peer disconnect / timeout, can be NULL
1120  * @param inbound_notify function to call for all inbound messages, can be NULL
1121  * @param inbound_hdr_only set to #GNUNET_YES if inbound_notify will only read the
1122  *                GNUNET_MessageHeader and hence we do not need to give it the full message;
1123  *                can be used to improve efficiency, ignored if @a inbound_notify is NULLL
1124  * @param outbound_notify function to call for all outbound messages, can be NULL
1125  * @param outbound_hdr_only set to #GNUNET_YES if outbound_notify will only read the
1126  *                GNUNET_MessageHeader and hence we do not need to give it the full message
1127  *                can be used to improve efficiency, ignored if @a outbound_notify is NULLL
1128  * @param handlers callbacks for messages we care about, NULL-terminated
1129  * @return handle to the core service (only useful for disconnect until 'init' is called);
1130  *                NULL on error (in this case, init is never called)
1131  */
1132 struct GNUNET_CORE_Handle *
1133 GNUNET_CORE_connect (const struct GNUNET_CONFIGURATION_Handle *cfg,
1134                      void *cls,
1135                      GNUNET_CORE_StartupCallback init,
1136                      GNUNET_CORE_ConnectEventHandler connects,
1137                      GNUNET_CORE_DisconnectEventHandler disconnects,
1138                      GNUNET_CORE_MessageCallback inbound_notify,
1139                      int inbound_hdr_only,
1140                      GNUNET_CORE_MessageCallback outbound_notify,
1141                      int outbound_hdr_only,
1142                      const struct GNUNET_CORE_MessageHandler *handlers)
1143 {
1144   struct GNUNET_CORE_Handle *h;
1145
1146   GNUNET_assert (NULL != cfg);
1147   h = GNUNET_new (struct GNUNET_CORE_Handle);
1148   h->cfg = cfg;
1149   h->cls = cls;
1150   h->init = init;
1151   h->connects = connects;
1152   h->disconnects = disconnects;
1153   h->inbound_notify = inbound_notify;
1154   h->outbound_notify = outbound_notify;
1155   h->inbound_hdr_only = inbound_hdr_only;
1156   h->outbound_hdr_only = outbound_hdr_only;
1157   h->handlers = handlers;
1158   h->hcnt = 0;
1159   h->currently_down = GNUNET_YES;
1160   h->peers = GNUNET_CONTAINER_multihashmap_create (128, GNUNET_NO);
1161   if (NULL != handlers)
1162     while (handlers[h->hcnt].callback != NULL)
1163       h->hcnt++;
1164   GNUNET_assert (h->hcnt <
1165                  (GNUNET_SERVER_MAX_MESSAGE_SIZE -
1166                   sizeof (struct InitMessage)) / sizeof (uint16_t));
1167   LOG (GNUNET_ERROR_TYPE_DEBUG, "Connecting to CORE service\n");
1168   reconnect (h);
1169   return h;
1170 }
1171
1172
1173 /**
1174  * Disconnect from the core service.  This function can only
1175  * be called *after* all pending 'GNUNET_CORE_notify_transmit_ready'
1176  * requests have been explicitly canceled.
1177  *
1178  * @param handle connection to core to disconnect
1179  */
1180 void
1181 GNUNET_CORE_disconnect (struct GNUNET_CORE_Handle *handle)
1182 {
1183   struct ControlMessage *cm;
1184
1185   GNUNET_assert (NULL != handle);
1186
1187   LOG (GNUNET_ERROR_TYPE_DEBUG, "Disconnecting from CORE service\n");
1188   if (NULL != handle->cth)
1189   {
1190     GNUNET_CLIENT_notify_transmit_ready_cancel (handle->cth);
1191     handle->cth = NULL;
1192   }
1193   while (NULL != (cm = handle->control_pending_head))
1194   {
1195     GNUNET_CONTAINER_DLL_remove (handle->control_pending_head,
1196                                  handle->control_pending_tail, cm);
1197     if (NULL != cm->th)
1198       cm->th->cm = NULL;
1199     if (NULL != cm->cont)
1200       cm->cont (cm->cont_cls, GNUNET_SYSERR);
1201     GNUNET_free (cm);
1202   }
1203   if (NULL != handle->client)
1204   {
1205     GNUNET_CLIENT_disconnect (handle->client);
1206     handle->client = NULL;
1207   }
1208   GNUNET_CONTAINER_multihashmap_iterate (handle->peers,
1209                                          &disconnect_and_free_peer_entry,
1210                                          handle);
1211   if (handle->reconnect_task != GNUNET_SCHEDULER_NO_TASK)
1212   {
1213     GNUNET_SCHEDULER_cancel (handle->reconnect_task);
1214     handle->reconnect_task = GNUNET_SCHEDULER_NO_TASK;
1215   }
1216   GNUNET_CONTAINER_multihashmap_destroy (handle->peers);
1217   handle->peers = NULL;
1218   GNUNET_break (handle->ready_peer_head == NULL);
1219   GNUNET_free (handle);
1220 }
1221
1222
1223 /**
1224  * Task that calls 'request_next_transmission'.
1225  *
1226  * @param cls the 'struct PeerRecord *'
1227  * @param tc scheduler context
1228  */
1229 static void
1230 run_request_next_transmission (void *cls,
1231                                const struct GNUNET_SCHEDULER_TaskContext *tc)
1232 {
1233   struct PeerRecord *pr = cls;
1234
1235   pr->ntr_task = GNUNET_SCHEDULER_NO_TASK;
1236   request_next_transmission (pr);
1237 }
1238
1239
1240 /**
1241  * Ask the core to call @a notify once it is ready to transmit the
1242  * given number of bytes to the specified @a target.  Must only be
1243  * called after a connection to the respective peer has been
1244  * established (and the client has been informed about this).  You may
1245  * have one request of this type pending for each connected peer at
1246  * any time.  If a peer disconnects, the application MUST call
1247  * #GNUNET_CORE_notify_transmit_ready_cancel on the respective
1248  * transmission request, if one such request is pending.
1249  *
1250  * @param handle connection to core service
1251  * @param cork is corking allowed for this transmission?
1252  * @param priority how important is the message?
1253  * @param maxdelay how long can the message wait?
1254  * @param target who should receive the message, never NULL (can be this peer's identity for loopback)
1255  * @param notify_size how many bytes of buffer space does @a notify want?
1256  * @param notify function to call when buffer space is available;
1257  *        will be called with NULL on timeout; clients MUST cancel
1258  *        all pending transmission requests DURING the disconnect
1259  *        handler
1260  * @param notify_cls closure for notify
1261  * @return non-NULL if the notify callback was queued,
1262  *         NULL if we can not even queue the request (request already pending);
1263  *         if NULL is returned, @a notify will NOT be called.
1264  */
1265 struct GNUNET_CORE_TransmitHandle *
1266 GNUNET_CORE_notify_transmit_ready (struct GNUNET_CORE_Handle *handle, int cork,
1267                                    uint32_t priority,
1268                                    struct GNUNET_TIME_Relative maxdelay,
1269                                    const struct GNUNET_PeerIdentity *target,
1270                                    size_t notify_size,
1271                                    GNUNET_CONNECTION_TransmitReadyNotify notify,
1272                                    void *notify_cls)
1273 {
1274   struct PeerRecord *pr;
1275   struct GNUNET_CORE_TransmitHandle *th;
1276
1277   GNUNET_assert (NULL != handle);
1278   GNUNET_assert (NULL != target);
1279
1280   if (notify_size > GNUNET_CONSTANTS_MAX_ENCRYPTED_MESSAGE_SIZE)
1281   {
1282      GNUNET_break (0);
1283      return NULL;
1284   }
1285   GNUNET_assert (NULL != notify);
1286   LOG (GNUNET_ERROR_TYPE_DEBUG,
1287        "Asking core for transmission of %u bytes to `%s'\n",
1288        (unsigned int) notify_size,
1289        GNUNET_i2s (target));
1290   pr = GNUNET_CONTAINER_multihashmap_get (handle->peers, &target->hashPubKey);
1291   if (NULL == pr)
1292   {
1293     /* attempt to send to peer that is not connected */
1294     GNUNET_break (0);
1295     return NULL;
1296   }
1297   if (NULL != pr->th.peer)
1298   {
1299     /* attempting to queue a second request for the same destination */
1300     GNUNET_break (0);
1301     return NULL;
1302   }
1303   GNUNET_assert (notify_size + sizeof (struct SendMessage) <
1304                  GNUNET_SERVER_MAX_MESSAGE_SIZE);
1305   th = &pr->th;
1306   memset (th, 0, sizeof (struct GNUNET_CORE_TransmitHandle));
1307   th->peer = pr;
1308   th->get_message = notify;
1309   th->get_message_cls = notify_cls;
1310   th->timeout = GNUNET_TIME_relative_to_absolute (maxdelay);
1311   th->priority = priority;
1312   th->msize = notify_size;
1313   th->cork = cork;
1314   GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == pr->ntr_task);
1315   pr->ntr_task =
1316     GNUNET_SCHEDULER_add_now (&run_request_next_transmission, pr);
1317   LOG (GNUNET_ERROR_TYPE_DEBUG, "Transmission request added to queue\n");
1318   return th;
1319 }
1320
1321
1322 /**
1323  * Cancel the specified transmission-ready notification.
1324  *
1325  * @param th handle that was returned by "notify_transmit_ready".
1326  */
1327 void
1328 GNUNET_CORE_notify_transmit_ready_cancel (struct GNUNET_CORE_TransmitHandle *th)
1329 {
1330   struct PeerRecord *pr = th->peer;
1331   struct GNUNET_CORE_Handle *h;
1332
1333   GNUNET_assert (NULL != th);
1334   GNUNET_assert (NULL != pr);
1335   LOG (GNUNET_ERROR_TYPE_DEBUG,
1336        "Aborting transmission request to core for %u bytes to `%s'\n",
1337        (unsigned int) th->msize,
1338        GNUNET_i2s (&pr->peer));
1339   th->peer = NULL;
1340   h = pr->ch;
1341   if (NULL != th->cm)
1342   {
1343     /* we're currently in the control queue, remove */
1344     GNUNET_CONTAINER_DLL_remove (h->control_pending_head,
1345                                  h->control_pending_tail, th->cm);
1346     GNUNET_free (th->cm);
1347     th->cm = NULL;
1348   }
1349   if ((NULL != pr->prev) || (NULL != pr->next) || (pr == h->ready_peer_head))
1350   {
1351     /* the request that was 'approved' by core was
1352      * canceled before it could be transmitted; remove
1353      * us from the 'ready' list */
1354     GNUNET_CONTAINER_DLL_remove (h->ready_peer_head, h->ready_peer_tail, pr);
1355   }
1356   if (GNUNET_SCHEDULER_NO_TASK != pr->ntr_task)
1357   {
1358     GNUNET_SCHEDULER_cancel (pr->ntr_task);
1359     pr->ntr_task = GNUNET_SCHEDULER_NO_TASK;
1360   }
1361 }
1362
1363
1364 /**
1365  * Check if the given peer is currently connected. This function is for special
1366  * cirumstances (GNUNET_TESTBED uses it), normal users of the CORE API are
1367  * expected to track which peers are connected based on the connect/disconnect
1368  * callbacks from GNUNET_CORE_connect.  This function is NOT part of the
1369  * 'versioned', 'official' API. The difference between this function and the
1370  * function GNUNET_CORE_is_peer_connected() is that this one returns
1371  * synchronously after looking in the CORE API cache. The function
1372  * GNUNET_CORE_is_peer_connected() sends a message to the CORE service and hence
1373  * its response is given asynchronously.
1374  *
1375  * @param h the core handle
1376  * @param pid the identity of the peer to check if it has been connected to us
1377  * @return GNUNET_YES if the peer is connected to us; GNUNET_NO if not
1378  */
1379 int
1380 GNUNET_CORE_is_peer_connected_sync (const struct GNUNET_CORE_Handle *h,
1381                                     const struct GNUNET_PeerIdentity *pid)
1382 {
1383   GNUNET_assert (NULL != h);
1384   GNUNET_assert (NULL != pid);
1385   return GNUNET_CONTAINER_multihashmap_contains (h->peers, &pid->hashPubKey);
1386 }
1387
1388
1389 /* end of core_api.c */