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