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