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