-some fixes for monkey
[oweals/gnunet.git] / src / transport / transport_api.c
1 /*
2      This file is part of GNUnet.
3      (C) 2009, 2010, 2011 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 transport/transport_api.c
23  * @brief library to access the low-level P2P IO service
24  * @author Christian Grothoff
25  *
26  * TODO:
27  * - adjust testcases to use new 'try connect' style (should be easy, breaks API compatibility!)
28  * - adjust core service to use new 'try connect' style (should be MUCH nicer there as well!)
29  * - test test test
30  */
31 #include "platform.h"
32 #include "gnunet_constants.h"
33 #include "gnunet_bandwidth_lib.h"
34 #include "gnunet_client_lib.h"
35 #include "gnunet_constants.h"
36 #include "gnunet_container_lib.h"
37 #include "gnunet_arm_service.h"
38 #include "gnunet_hello_lib.h"
39 #include "gnunet_protocols.h"
40 #include "gnunet_server_lib.h"
41 #include "gnunet_time_lib.h"
42 #include "gnunet_transport_service.h"
43 #include "transport.h"
44
45 #define LOG(kind,...) GNUNET_log_from (kind, "transport-api",__VA_ARGS__)
46
47 /**
48  * How large to start with for the hashmap of neighbours.
49  */
50 #define STARTING_NEIGHBOURS_SIZE 16
51
52 /**
53  * Handle for a message that should be transmitted to the service.
54  * Used for both control messages and normal messages.
55  */
56 struct GNUNET_TRANSPORT_TransmitHandle
57 {
58
59   /**
60    * We keep all requests in a DLL.
61    */
62   struct GNUNET_TRANSPORT_TransmitHandle *next;
63
64   /**
65    * We keep all requests in a DLL.
66    */
67   struct GNUNET_TRANSPORT_TransmitHandle *prev;
68
69   /**
70    * Neighbour for this handle, NULL for control messages.
71    */
72   struct Neighbour *neighbour;
73
74   /**
75    * Function to call when notify_size bytes are available
76    * for transmission.
77    */
78   GNUNET_CONNECTION_TransmitReadyNotify notify;
79
80   /**
81    * Closure for notify.
82    */
83   void *notify_cls;
84
85   /**
86    * Timeout for this request, 0 for control messages.
87    */
88   struct GNUNET_TIME_Absolute timeout;
89
90   /**
91    * Task to trigger request timeout if the request is stalled due to
92    * congestion.
93    */
94   GNUNET_SCHEDULER_TaskIdentifier timeout_task;
95
96   /**
97    * How many bytes is our notify callback waiting for?
98    */
99   size_t notify_size;
100
101   /**
102    * How important is this message? Not used for control messages.
103    */
104   uint32_t priority;
105
106 };
107
108
109 /**
110  * Entry in hash table of all of our current neighbours.
111  */
112 struct Neighbour
113 {
114   /**
115    * Overall transport handle.
116    */
117   struct GNUNET_TRANSPORT_Handle *h;
118
119   /**
120    * Active transmit handle or NULL.
121    */
122   struct GNUNET_TRANSPORT_TransmitHandle *th;
123
124   /**
125    * Identity of this neighbour.
126    */
127   struct GNUNET_PeerIdentity id;
128
129   /**
130    * Outbound bandwidh tracker.
131    */
132   struct GNUNET_BANDWIDTH_Tracker out_tracker;
133
134   /**
135    * Entry in our readyness heap (which is sorted by 'next_ready'
136    * value).  NULL if there is no pending transmission request for
137    * this neighbour or if we're waiting for 'is_ready' to become
138    * true AFTER the 'out_tracker' suggested that this peer's quota
139    * has been satisfied (so once 'is_ready' goes to GNUNET_YES,
140    * we should immediately go back into the heap).
141    */
142   struct GNUNET_CONTAINER_HeapNode *hn;
143
144   /**
145    * Is this peer currently ready to receive a message?
146    */
147   int is_ready;
148
149 };
150
151
152 /**
153  * Linked list of functions to call whenever our HELLO is updated.
154  */
155 struct GNUNET_TRANSPORT_GetHelloHandle
156 {
157
158   /**
159    * This is a doubly linked list.
160    */
161   struct GNUNET_TRANSPORT_GetHelloHandle *next;
162
163   /**
164    * This is a doubly linked list.
165    */
166   struct GNUNET_TRANSPORT_GetHelloHandle *prev;
167
168   /**
169    * Transport handle.
170    */
171   struct GNUNET_TRANSPORT_Handle *handle;
172
173   /**
174    * Callback to call once we got our HELLO.
175    */
176   GNUNET_TRANSPORT_HelloUpdateCallback rec;
177
178   /**
179    * Closure for rec.
180    */
181   void *rec_cls;
182
183 };
184
185
186 /**
187  * Handle for the transport service (includes all of the
188  * state for the transport service).
189  */
190 struct GNUNET_TRANSPORT_Handle
191 {
192
193   /**
194    * Closure for the callbacks.
195    */
196   void *cls;
197
198   /**
199    * Function to call for received data.
200    */
201   GNUNET_TRANSPORT_ReceiveCallback rec;
202
203   /**
204    * function to call on connect events
205    */
206   GNUNET_TRANSPORT_NotifyConnect nc_cb;
207
208   /**
209    * function to call on disconnect events
210    */
211   GNUNET_TRANSPORT_NotifyDisconnect nd_cb;
212
213   /**
214    * Head of DLL of control messages.
215    */
216   struct GNUNET_TRANSPORT_TransmitHandle *control_head;
217
218   /**
219    * Tail of DLL of control messages.
220    */
221   struct GNUNET_TRANSPORT_TransmitHandle *control_tail;
222
223   /**
224    * The current HELLO message for this peer.  Updated
225    * whenever transports change their addresses.
226    */
227   struct GNUNET_HELLO_Message *my_hello;
228
229   /**
230    * My client connection to the transport service.
231    */
232   struct GNUNET_CLIENT_Connection *client;
233
234   /**
235    * Handle to our registration with the client for notification.
236    */
237   struct GNUNET_CLIENT_TransmitHandle *cth;
238
239   /**
240    * Linked list of pending requests for our HELLO.
241    */
242   struct GNUNET_TRANSPORT_GetHelloHandle *hwl_head;
243
244   /**
245    * Linked list of pending requests for our HELLO.
246    */
247   struct GNUNET_TRANSPORT_GetHelloHandle *hwl_tail;
248
249   /**
250    * My configuration.
251    */
252   const struct GNUNET_CONFIGURATION_Handle *cfg;
253
254   /**
255    * Hash map of the current connected neighbours of this peer.
256    * Maps peer identities to 'struct Neighbour' entries.
257    */
258   struct GNUNET_CONTAINER_MultiHashMap *neighbours;
259
260   /**
261    * Heap sorting peers with pending messages by the timestamps that
262    * specify when we could next send a message to the respective peer.
263    * Excludes control messages (which can always go out immediately).
264    * Maps time stamps to 'struct Neighbour' entries.
265    */
266   struct GNUNET_CONTAINER_Heap *ready_heap;
267
268   /**
269    * Peer identity as assumed by this process, or all zeros.
270    */
271   struct GNUNET_PeerIdentity self;
272
273   /**
274    * ID of the task trying to reconnect to the service.
275    */
276   GNUNET_SCHEDULER_TaskIdentifier reconnect_task;
277
278   /**
279    * ID of the task trying to trigger transmission for a peer while
280    * maintaining bandwidth quotas.  In use if there are no control
281    * messages and the smallest entry in the 'ready_heap' has a time
282    * stamp in the future.
283    */
284   GNUNET_SCHEDULER_TaskIdentifier quota_task;
285
286   /**
287    * Delay until we try to reconnect.
288    */
289   struct GNUNET_TIME_Relative reconnect_delay;
290
291   /**
292    * Should we check that 'self' matches what the service thinks?
293    * (if GNUNET_NO, then 'self' is all zeros!).
294    */
295   int check_self;
296 };
297
298
299 /**
300  * Schedule the task to send one message, either from the control
301  * list or the peer message queues  to the service.
302  *
303  * @param h transport service to schedule a transmission for
304  */
305 static void
306 schedule_transmission (struct GNUNET_TRANSPORT_Handle *h);
307
308
309 /**
310  * Function that will schedule the job that will try
311  * to connect us again to the client.
312  *
313  * @param h transport service to reconnect
314  */
315 static void
316 disconnect_and_schedule_reconnect (struct GNUNET_TRANSPORT_Handle *h);
317
318
319 /**
320  * Get the neighbour list entry for the given peer
321  *
322  * @param h our context
323  * @param peer peer to look up
324  * @return NULL if no such peer entry exists
325  */
326 static struct Neighbour *
327 neighbour_find (struct GNUNET_TRANSPORT_Handle *h,
328                 const struct GNUNET_PeerIdentity *peer)
329 {
330   return GNUNET_CONTAINER_multihashmap_get (h->neighbours, &peer->hashPubKey);
331 }
332
333
334 /**
335  * Add neighbour to our list
336  *
337  * @return NULL if this API is currently disconnecting from the service
338  */
339 static struct Neighbour *
340 neighbour_add (struct GNUNET_TRANSPORT_Handle *h,
341                const struct GNUNET_PeerIdentity *pid)
342 {
343   struct Neighbour *n;
344
345   LOG (GNUNET_ERROR_TYPE_DEBUG, "Creating entry for neighbour `%4s'.\n",
346        GNUNET_i2s (pid));
347   n = GNUNET_malloc (sizeof (struct Neighbour));
348   n->id = *pid;
349   n->h = h;
350   n->is_ready = GNUNET_YES;
351   GNUNET_BANDWIDTH_tracker_init (&n->out_tracker,
352                                  GNUNET_CONSTANTS_DEFAULT_BW_IN_OUT,
353                                  MAX_BANDWIDTH_CARRY_S);
354   GNUNET_assert (GNUNET_OK ==
355                  GNUNET_CONTAINER_multihashmap_put (h->neighbours,
356                                                     &pid->hashPubKey, n,
357                                                     GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
358   return n;
359 }
360
361
362 /**
363  * Iterator over hash map entries, for deleting state of a neighbour.
364  *
365  * @param cls the 'struct GNUNET_TRANSPORT_Handle*'
366  * @param key peer identity
367  * @param value value in the hash map, the neighbour entry to delete
368  * @return GNUNET_YES if we should continue to
369  *         iterate,
370  *         GNUNET_NO if not.
371  */
372 static int
373 neighbour_delete (void *cls, const struct GNUNET_HashCode * key, void *value)
374 {
375   struct GNUNET_TRANSPORT_Handle *handle = cls;
376   struct Neighbour *n = value;
377
378   if (NULL != handle->nd_cb)
379     handle->nd_cb (handle->cls, &n->id);
380   GNUNET_assert (NULL == n->th);
381   GNUNET_assert (NULL == n->hn);
382   GNUNET_assert (GNUNET_YES ==
383                  GNUNET_CONTAINER_multihashmap_remove (handle->neighbours, key,
384                                                        n));
385   GNUNET_free (n);
386   return GNUNET_YES;
387 }
388
389
390 /**
391  * Function we use for handling incoming messages.
392  *
393  * @param cls closure (struct GNUNET_TRANSPORT_Handle *)
394  * @param msg message received, NULL on timeout or fatal error
395  */
396 static void
397 demultiplexer (void *cls, const struct GNUNET_MessageHeader *msg)
398 {
399   struct GNUNET_TRANSPORT_Handle *h = cls;
400   const struct DisconnectInfoMessage *dim;
401   const struct ConnectInfoMessage *cim;
402   const struct InboundMessage *im;
403   const struct GNUNET_MessageHeader *imm;
404   const struct SendOkMessage *okm;
405   const struct QuotaSetMessage *qm;
406   const struct GNUNET_ATS_Information *ats;
407   struct GNUNET_TRANSPORT_GetHelloHandle *hwl;
408   struct GNUNET_TRANSPORT_GetHelloHandle *next_hwl;
409   struct Neighbour *n;
410   struct GNUNET_PeerIdentity me;
411   uint16_t size;
412   uint32_t ats_count;
413
414   GNUNET_assert (h->client != NULL);
415   if (msg == NULL)
416   {
417     LOG (GNUNET_ERROR_TYPE_DEBUG,
418          "Error receiving from transport service, disconnecting temporarily.\n");
419     disconnect_and_schedule_reconnect (h);
420     return;
421   }
422   GNUNET_CLIENT_receive (h->client, &demultiplexer, h,
423                          GNUNET_TIME_UNIT_FOREVER_REL);
424   size = ntohs (msg->size);
425   switch (ntohs (msg->type))
426   {
427   case GNUNET_MESSAGE_TYPE_HELLO:
428     if (GNUNET_OK !=
429         GNUNET_HELLO_get_id ((const struct GNUNET_HELLO_Message *) msg, &me))
430     {
431       GNUNET_break (0);
432       break;
433     }
434     LOG (GNUNET_ERROR_TYPE_DEBUG,
435          "Receiving (my own) `%s' message, I am `%4s'.\n", "HELLO",
436          GNUNET_i2s (&me));
437     GNUNET_free_non_null (h->my_hello);
438     h->my_hello = NULL;
439     if (size < sizeof (struct GNUNET_MessageHeader))
440     {
441       GNUNET_break (0);
442       break;
443     }
444     h->my_hello = GNUNET_malloc (size);
445     memcpy (h->my_hello, msg, size);
446     hwl = h->hwl_head;
447     while (NULL != hwl)
448     {
449       next_hwl = hwl->next;
450       hwl->rec (hwl->rec_cls,
451                 (const struct GNUNET_MessageHeader *) h->my_hello);
452       hwl = next_hwl;
453     }
454     break;
455   case GNUNET_MESSAGE_TYPE_TRANSPORT_CONNECT:
456     if (size < sizeof (struct ConnectInfoMessage))
457     {
458       GNUNET_break (0);
459       break;
460     }
461     cim = (const struct ConnectInfoMessage *) msg;
462     ats_count = ntohl (cim->ats_count);
463     if (size !=
464         sizeof (struct ConnectInfoMessage) +
465         ats_count * sizeof (struct GNUNET_ATS_Information))
466     {
467       GNUNET_break (0);
468       break;
469     }
470     ats = (const struct GNUNET_ATS_Information *) &cim[1];
471     LOG (GNUNET_ERROR_TYPE_DEBUG, "Receiving `%s' message for `%4s'.\n",
472          "CONNECT", GNUNET_i2s (&cim->id));
473     n = neighbour_find (h, &cim->id);
474     if (n != NULL)
475     {
476       GNUNET_break (0);
477       break;
478     }
479     n = neighbour_add (h, &cim->id);
480     LOG (GNUNET_ERROR_TYPE_DEBUG, "Receiving `%s' message for `%4s' with quota %u\n",
481          "CONNECT", GNUNET_i2s (&cim->id), ntohl (cim->quota_out.value__));
482     GNUNET_BANDWIDTH_tracker_update_quota (&n->out_tracker, cim->quota_out);
483     if (h->nc_cb != NULL)
484       h->nc_cb (h->cls, &n->id, ats, ats_count);
485     break;
486   case GNUNET_MESSAGE_TYPE_TRANSPORT_DISCONNECT:
487     if (size != sizeof (struct DisconnectInfoMessage))
488     {
489       GNUNET_break (0);
490       break;
491     }
492     dim = (const struct DisconnectInfoMessage *) msg;
493     GNUNET_break (ntohl (dim->reserved) == 0);
494     LOG (GNUNET_ERROR_TYPE_DEBUG, "Receiving `%s' message for `%4s'.\n",
495          "DISCONNECT", GNUNET_i2s (&dim->peer));
496     n = neighbour_find (h, &dim->peer);
497     if (n == NULL)
498     {
499       GNUNET_break (0);
500       break;
501     }
502     neighbour_delete (h, &dim->peer.hashPubKey, n);
503     break;
504   case GNUNET_MESSAGE_TYPE_TRANSPORT_SEND_OK:
505     if (size != sizeof (struct SendOkMessage))
506     {
507       GNUNET_break (0);
508       break;
509     }
510     okm = (const struct SendOkMessage *) msg;
511     LOG (GNUNET_ERROR_TYPE_DEBUG, "Receiving `%s' message, transmission %s.\n",
512          "SEND_OK", ntohl (okm->success) == GNUNET_OK ? "succeeded" : "failed");
513     n = neighbour_find (h, &okm->peer);
514     if (n == NULL)
515       break;
516     GNUNET_break (GNUNET_NO == n->is_ready);
517     n->is_ready = GNUNET_YES;
518     if ((n->th != NULL) && (n->hn == NULL))
519     {
520       GNUNET_assert (GNUNET_SCHEDULER_NO_TASK != n->th->timeout_task);
521       GNUNET_SCHEDULER_cancel (n->th->timeout_task);
522       n->th->timeout_task = GNUNET_SCHEDULER_NO_TASK;
523       /* we've been waiting for this (congestion, not quota,
524        * caused delayed transmission) */
525       n->hn = GNUNET_CONTAINER_heap_insert (h->ready_heap, n, 0);
526       schedule_transmission (h);
527     }
528     break;
529   case GNUNET_MESSAGE_TYPE_TRANSPORT_RECV:
530     LOG (GNUNET_ERROR_TYPE_DEBUG, "Receiving `%s' message.\n", "RECV");
531     if (size <
532         sizeof (struct InboundMessage) + sizeof (struct GNUNET_MessageHeader))
533     {
534       GNUNET_break (0);
535       break;
536     }
537     im = (const struct InboundMessage *) msg;
538     ats_count = ntohl (im->ats_count);
539     ats = (const struct GNUNET_ATS_Information *) &im[1];
540     imm = (const struct GNUNET_MessageHeader *) &ats[ats_count];
541     if (ntohs (imm->size) + sizeof (struct InboundMessage) +
542         ats_count * sizeof (struct GNUNET_ATS_Information) != size)
543     {
544       GNUNET_break (0);
545       break;
546     }
547     LOG (GNUNET_ERROR_TYPE_DEBUG, "Received message of type %u from `%4s'.\n",
548          ntohs (imm->type), GNUNET_i2s (&im->peer));
549     n = neighbour_find (h, &im->peer);
550     if (n == NULL)
551     {
552       GNUNET_break (0);
553       break;
554     }
555     if (h->rec != NULL)
556       h->rec (h->cls, &im->peer, imm, ats, ats_count);
557     break;
558   case GNUNET_MESSAGE_TYPE_TRANSPORT_SET_QUOTA:
559     LOG (GNUNET_ERROR_TYPE_DEBUG, "Receiving `%s' message.\n", "SET_QUOTA");
560     if (size != sizeof (struct QuotaSetMessage))
561     {
562       GNUNET_break (0);
563       break;
564     }
565     qm = (const struct QuotaSetMessage *) msg;
566     n = neighbour_find (h, &qm->peer);
567     if (n == NULL)
568       break;
569     LOG (GNUNET_ERROR_TYPE_DEBUG, "Receiving `%s' message for `%4s' with quota %u\n",
570          "SET_QUOTA", GNUNET_i2s (&qm->peer), ntohl (qm->quota.value__));
571     GNUNET_BANDWIDTH_tracker_update_quota (&n->out_tracker, qm->quota);
572     break;
573   default:
574     LOG (GNUNET_ERROR_TYPE_ERROR,
575          _("Received unexpected message of type %u in %s:%u\n"),
576          ntohs (msg->type), __FILE__, __LINE__);
577     GNUNET_break (0);
578     break;
579   }
580 }
581
582
583 /**
584  * A transmission request could not be satisfied because of
585  * network congestion.  Notify the initiator and clean up.
586  *
587  * @param cls the 'struct GNUNET_TRANSPORT_TransmitHandle'
588  * @param tc scheduler context
589  */
590 static void
591 timeout_request_due_to_congestion (void *cls,
592                                    const struct GNUNET_SCHEDULER_TaskContext
593                                    *tc)
594 {
595   struct GNUNET_TRANSPORT_TransmitHandle *th = cls;
596   struct Neighbour *n = th->neighbour;
597
598   n->th->timeout_task = GNUNET_SCHEDULER_NO_TASK;
599   GNUNET_assert (th == n->th);
600   GNUNET_assert (NULL == n->hn);
601   n->th = NULL;
602   th->notify (th->notify_cls, 0, NULL);
603   GNUNET_free (th);
604 }
605
606
607 /**
608  * Transmit message(s) to service.
609  *
610  * @param cls handle to transport
611  * @param size number of bytes available in buf
612  * @param buf where to copy the message
613  * @return number of bytes copied to buf
614  */
615 static size_t
616 transport_notify_ready (void *cls, size_t size, void *buf)
617 {
618   struct GNUNET_TRANSPORT_Handle *h = cls;
619   struct GNUNET_TRANSPORT_TransmitHandle *th;
620   struct Neighbour *n;
621   char *cbuf;
622   struct OutboundMessage obm;
623   size_t ret;
624   size_t nret;
625   size_t mret;
626
627   GNUNET_assert (NULL != h->client);
628   h->cth = NULL;
629   if (NULL == buf)
630   {
631     /* transmission failed */
632     disconnect_and_schedule_reconnect (h);
633     return 0;
634   }
635
636   cbuf = buf;
637   ret = 0;
638   /* first send control messages */
639   while ((NULL != (th = h->control_head)) && (th->notify_size <= size))
640   {
641     GNUNET_CONTAINER_DLL_remove (h->control_head, h->control_tail, th);
642     nret = th->notify (th->notify_cls, size, &cbuf[ret]);
643     LOG (GNUNET_ERROR_TYPE_DEBUG, "Added %u bytes of control message at %u\n",
644          nret, ret);
645     GNUNET_free (th);
646     ret += nret;
647     size -= nret;
648   }
649
650   /* then, if possible and no control messages pending, send data messages */
651   while ((NULL == h->control_head) &&
652          (NULL != (n = GNUNET_CONTAINER_heap_peek (h->ready_heap))))
653   {
654     if (GNUNET_YES != n->is_ready)
655     {
656       /* peer not ready, wait for notification! */
657       GNUNET_assert (n == GNUNET_CONTAINER_heap_remove_root (h->ready_heap));
658       n->hn = NULL;
659       GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == n->th->timeout_task);
660       n->th->timeout_task =
661           GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_absolute_get_remaining
662                                         (n->th->timeout),
663                                         &timeout_request_due_to_congestion,
664                                         n->th);
665       continue;
666     }
667     th = n->th;
668     if (th->notify_size + sizeof (struct OutboundMessage) > size)
669       break;                    /* does not fit */
670     if (GNUNET_BANDWIDTH_tracker_get_delay
671         (&n->out_tracker, th->notify_size).rel_value > 0)
672       break;                    /* too early */
673     GNUNET_assert (n == GNUNET_CONTAINER_heap_remove_root (h->ready_heap));
674     n->hn = NULL;
675     n->th = NULL;
676     n->is_ready = GNUNET_NO;
677     GNUNET_assert (size >= sizeof (struct OutboundMessage));
678     mret =
679         th->notify (th->notify_cls, size - sizeof (struct OutboundMessage),
680                     &cbuf[ret + sizeof (struct OutboundMessage)]);
681     GNUNET_assert (mret <= size - sizeof (struct OutboundMessage));
682     if (mret != 0)
683     {
684       GNUNET_assert (mret + sizeof (struct OutboundMessage) <
685                      GNUNET_SERVER_MAX_MESSAGE_SIZE);
686       obm.header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_SEND);
687       obm.header.size = htons (mret + sizeof (struct OutboundMessage));
688       obm.priority = htonl (th->priority);
689       obm.timeout =
690           GNUNET_TIME_relative_hton (GNUNET_TIME_absolute_get_remaining
691                                      (th->timeout));
692       obm.peer = n->id;
693       memcpy (&cbuf[ret], &obm, sizeof (struct OutboundMessage));
694       ret += (mret + sizeof (struct OutboundMessage));
695       size -= (mret + sizeof (struct OutboundMessage));
696       GNUNET_BANDWIDTH_tracker_consume (&n->out_tracker, mret);
697     }
698     GNUNET_free (th);
699   }
700   /* if there are more pending messages, try to schedule those */
701   schedule_transmission (h);
702   LOG (GNUNET_ERROR_TYPE_DEBUG, "Transmitting %u bytes to transport service\n",
703        ret);
704   return ret;
705 }
706
707
708 /**
709  * Schedule the task to send one message, either from the control
710  * list or the peer message queues  to the service.
711  *
712  * @param cls transport service to schedule a transmission for
713  * @param tc scheduler context
714  */
715 static void
716 schedule_transmission_task (void *cls,
717                             const struct GNUNET_SCHEDULER_TaskContext *tc)
718 {
719   struct GNUNET_TRANSPORT_Handle *h = cls;
720   size_t size;
721   struct GNUNET_TRANSPORT_TransmitHandle *th;
722   struct Neighbour *n;
723
724   h->quota_task = GNUNET_SCHEDULER_NO_TASK;
725   GNUNET_assert (NULL != h->client);
726   /* destroy all requests that have timed out */
727   while ((NULL != (n = GNUNET_CONTAINER_heap_peek (h->ready_heap))) &&
728          (GNUNET_TIME_absolute_get_remaining (n->th->timeout).rel_value == 0))
729   {
730     /* notify client that the request could not be satisfied within
731      * the given time constraints */
732     th = n->th;
733     n->th = NULL;
734     GNUNET_assert (n == GNUNET_CONTAINER_heap_remove_root (h->ready_heap));
735     n->hn = NULL;
736     LOG (GNUNET_ERROR_TYPE_DEBUG,
737          "Signalling timeout for transmission to peer %s due to congestion\n",
738          GNUNET_i2s (&n->id));
739     GNUNET_assert (0 == th->notify (th->notify_cls, 0, NULL));
740     GNUNET_free (th);
741   }
742   if (NULL != h->cth)
743     return;
744   if (NULL != h->control_head)
745   {
746     size = h->control_head->notify_size;
747   }
748   else
749   {
750     n = GNUNET_CONTAINER_heap_peek (h->ready_heap);
751     if (NULL == n)
752       return;                   /* no pending messages */
753     size = n->th->notify_size + sizeof (struct OutboundMessage);
754   }
755   LOG (GNUNET_ERROR_TYPE_DEBUG, "Calling notify_transmit_ready\n");
756   h->cth =
757       GNUNET_CLIENT_notify_transmit_ready (h->client, size,
758                                            GNUNET_TIME_UNIT_FOREVER_REL,
759                                            GNUNET_NO, &transport_notify_ready,
760                                            h);
761   GNUNET_assert (NULL != h->cth);
762 }
763
764
765 /**
766  * Schedule the task to send one message, either from the control
767  * list or the peer message queues  to the service.
768  *
769  * @param h transport service to schedule a transmission for
770  */
771 static void
772 schedule_transmission (struct GNUNET_TRANSPORT_Handle *h)
773 {
774   struct GNUNET_TIME_Relative delay;
775   struct Neighbour *n;
776
777   GNUNET_assert (NULL != h->client);
778   if (h->quota_task != GNUNET_SCHEDULER_NO_TASK)
779   {
780     GNUNET_SCHEDULER_cancel (h->quota_task);
781     h->quota_task = GNUNET_SCHEDULER_NO_TASK;
782   }
783   if (NULL != h->control_head)
784     delay = GNUNET_TIME_UNIT_ZERO;
785   else if (NULL != (n = GNUNET_CONTAINER_heap_peek (h->ready_heap)))
786     delay =
787         GNUNET_BANDWIDTH_tracker_get_delay (&n->out_tracker,
788                                             n->th->notify_size);
789   else
790     return;                     /* no work to be done */
791   LOG (GNUNET_ERROR_TYPE_DEBUG,
792        "Scheduling next transmission to service in %llu ms\n",
793        (unsigned long long) delay.rel_value);
794   h->quota_task =
795       GNUNET_SCHEDULER_add_delayed (delay, &schedule_transmission_task, h);
796 }
797
798
799 /**
800  * Queue control request for transmission to the transport
801  * service.
802  *
803  * @param h handle to the transport service
804  * @param size number of bytes to be transmitted
805  * @param notify function to call to get the content
806  * @param notify_cls closure for notify
807  */
808 static void
809 schedule_control_transmit (struct GNUNET_TRANSPORT_Handle *h, size_t size,
810                            GNUNET_CONNECTION_TransmitReadyNotify notify,
811                            void *notify_cls)
812 {
813   struct GNUNET_TRANSPORT_TransmitHandle *th;
814
815   LOG (GNUNET_ERROR_TYPE_DEBUG, "Control transmit of %u bytes requested\n",
816        size);
817   th = GNUNET_malloc (sizeof (struct GNUNET_TRANSPORT_TransmitHandle));
818   th->notify = notify;
819   th->notify_cls = notify_cls;
820   th->notify_size = size;
821   GNUNET_CONTAINER_DLL_insert_tail (h->control_head, h->control_tail, th);
822   schedule_transmission (h);
823 }
824
825
826 /**
827  * Transmit START message to service.
828  *
829  * @param cls unused
830  * @param size number of bytes available in buf
831  * @param buf where to copy the message
832  * @return number of bytes copied to buf
833  */
834 static size_t
835 send_start (void *cls, size_t size, void *buf)
836 {
837   struct GNUNET_TRANSPORT_Handle *h = cls;
838   struct StartMessage s;
839   uint32_t options;
840
841   if (buf == NULL)
842   {
843     /* Can only be shutdown, just give up */
844     LOG (GNUNET_ERROR_TYPE_DEBUG,
845          "Shutdown while trying to transmit `%s' request.\n", "START");
846     return 0;
847   }
848   LOG (GNUNET_ERROR_TYPE_DEBUG, "Transmitting `%s' request.\n", "START");
849   GNUNET_assert (size >= sizeof (struct StartMessage));
850   s.header.size = htons (sizeof (struct StartMessage));
851   s.header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_START);
852   options = 0;
853   if (h->check_self)
854     options |= 1;
855   if (h->rec != NULL)
856     options |= 2;
857   s.options = htonl (options);
858   s.self = h->self;
859   memcpy (buf, &s, sizeof (struct StartMessage));
860   GNUNET_CLIENT_receive (h->client, &demultiplexer, h,
861                          GNUNET_TIME_UNIT_FOREVER_REL);
862   return sizeof (struct StartMessage);
863 }
864
865
866 /**
867  * Try again to connect to transport service.
868  *
869  * @param cls the handle to the transport service
870  * @param tc scheduler context
871  */
872 static void
873 reconnect (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
874 {
875   struct GNUNET_TRANSPORT_Handle *h = cls;
876
877   h->reconnect_task = GNUNET_SCHEDULER_NO_TASK;
878   if ((tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) != 0)
879   {
880     /* shutdown, just give up */
881     return;
882   }
883   LOG (GNUNET_ERROR_TYPE_DEBUG, "Connecting to transport service.\n");
884   GNUNET_assert (h->client == NULL);
885   GNUNET_assert (h->control_head == NULL);
886   GNUNET_assert (h->control_tail == NULL);
887   h->client = GNUNET_CLIENT_connect ("transport", h->cfg);
888   GNUNET_assert (h->client != NULL);
889   schedule_control_transmit (h, sizeof (struct StartMessage), &send_start, h);
890 }
891
892
893 /**
894  * Function that will schedule the job that will try
895  * to connect us again to the client.
896  *
897  * @param h transport service to reconnect
898  */
899 static void
900 disconnect_and_schedule_reconnect (struct GNUNET_TRANSPORT_Handle *h)
901 {
902   struct GNUNET_TRANSPORT_TransmitHandle *th;
903
904   GNUNET_assert (h->reconnect_task == GNUNET_SCHEDULER_NO_TASK);
905   if (NULL != h->cth)
906   {
907     GNUNET_CLIENT_notify_transmit_ready_cancel (h->cth);
908     h->cth = NULL;
909   }
910   if (NULL != h->client)
911   {
912     GNUNET_CLIENT_disconnect (h->client);
913     h->client = NULL;
914   }
915   /* Forget about all neighbours that we used to be connected to */
916   GNUNET_CONTAINER_multihashmap_iterate (h->neighbours, &neighbour_delete, h);
917   if (h->quota_task != GNUNET_SCHEDULER_NO_TASK)
918   {
919     GNUNET_SCHEDULER_cancel (h->quota_task);
920     h->quota_task = GNUNET_SCHEDULER_NO_TASK;
921   }
922   while ((NULL != (th = h->control_head)))
923   {
924     GNUNET_CONTAINER_DLL_remove (h->control_head, h->control_tail, th);
925     th->notify (th->notify_cls, 0, NULL);
926     GNUNET_free (th);
927   }
928   LOG (GNUNET_ERROR_TYPE_DEBUG,
929        "Scheduling task to reconnect to transport service in %llu ms.\n",
930        h->reconnect_delay.rel_value);
931   h->reconnect_task =
932       GNUNET_SCHEDULER_add_delayed (h->reconnect_delay, &reconnect, h);
933   if (h->reconnect_delay.rel_value == 0)
934   {
935     h->reconnect_delay = GNUNET_TIME_UNIT_MILLISECONDS;
936   }
937   else
938   {
939     h->reconnect_delay = GNUNET_TIME_relative_multiply (h->reconnect_delay, 2);
940     h->reconnect_delay =
941         GNUNET_TIME_relative_min (GNUNET_TIME_UNIT_SECONDS, h->reconnect_delay);
942   }
943 }
944
945
946 /**
947  * Send REQUEST_CONNECT message to the service.
948  *
949  * @param cls the 'struct GNUNET_PeerIdentity'
950  * @param size number of bytes available in buf
951  * @param buf where to copy the message
952  * @return number of bytes copied to buf
953  */
954 static size_t
955 send_try_connect (void *cls, size_t size, void *buf)
956 {
957   struct GNUNET_PeerIdentity *pid = cls;
958   struct TransportRequestConnectMessage msg;
959
960   if (buf == NULL)
961   {
962     GNUNET_free (pid);
963     return 0;
964   }
965   LOG (GNUNET_ERROR_TYPE_DEBUG,
966        "Transmitting `%s' request with respect to `%4s'.\n", "REQUEST_CONNECT",
967        GNUNET_i2s (pid));
968   GNUNET_assert (size >= sizeof (struct TransportRequestConnectMessage));
969   msg.header.size = htons (sizeof (struct TransportRequestConnectMessage));
970   msg.header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_REQUEST_CONNECT);
971   msg.reserved = htonl (0);
972   msg.peer = *pid;
973   memcpy (buf, &msg, sizeof (msg));
974   GNUNET_free (pid);
975   return sizeof (struct TransportRequestConnectMessage);
976 }
977
978
979 /**
980  * Ask the transport service to establish a connection to
981  * the given peer.
982  *
983  * @param handle connection to transport service
984  * @param target who we should try to connect to
985  */
986 void
987 GNUNET_TRANSPORT_try_connect (struct GNUNET_TRANSPORT_Handle *handle,
988                               const struct GNUNET_PeerIdentity *target)
989 {
990   struct GNUNET_PeerIdentity *pid;
991   if (NULL == handle->client)
992   {
993       /* FIXME: handle->client can be NULL when transport api is reconnecting */
994       GNUNET_break (0);
995       return;
996   }
997
998   pid = GNUNET_malloc (sizeof (struct GNUNET_PeerIdentity));
999   *pid = *target;
1000   schedule_control_transmit (handle,
1001                              sizeof (struct TransportRequestConnectMessage),
1002                              &send_try_connect, pid);
1003 }
1004
1005
1006 struct SendHelloContext
1007 {
1008         GNUNET_SCHEDULER_Task cont;
1009
1010         void *cls;
1011
1012         struct GNUNET_MessageHeader *msg;
1013 };
1014
1015 /**
1016  * Send HELLO message to the service.
1017  *
1018  * @param cls the HELLO message to send
1019  * @param size number of bytes available in buf
1020  * @param buf where to copy the message
1021  * @return number of bytes copied to buf
1022  */
1023 static size_t
1024 send_hello (void *cls, size_t size, void *buf)
1025 {
1026   struct SendHelloContext *shc = cls;
1027   struct GNUNET_MessageHeader *msg = shc->msg;
1028   uint16_t ssize;
1029   struct GNUNET_SCHEDULER_TaskContext tc;
1030   tc.read_ready = NULL;
1031   tc.write_ready = NULL;
1032   tc.reason = GNUNET_SCHEDULER_REASON_TIMEOUT;
1033
1034   if (buf == NULL)
1035   {
1036     LOG (GNUNET_ERROR_TYPE_DEBUG,
1037          "Timeout while trying to transmit `%s' request.\n", "HELLO");
1038     if (NULL != shc->cont)
1039       shc->cont (shc->cls, &tc);
1040     GNUNET_free (msg);
1041     GNUNET_free (shc);
1042     return 0;
1043   }
1044   LOG (GNUNET_ERROR_TYPE_DEBUG, "Transmitting `%s' request.\n", "HELLO");
1045   ssize = ntohs (msg->size);
1046   GNUNET_assert (size >= ssize);
1047   memcpy (buf, msg, ssize);
1048   GNUNET_free (msg);
1049   tc.reason = GNUNET_SCHEDULER_REASON_READ_READY;
1050   if (NULL != shc->cont)
1051     shc->cont (shc->cls, &tc);
1052   GNUNET_free (shc);
1053   return ssize;
1054 }
1055
1056
1057 /**
1058  * Offer the transport service the HELLO of another peer.  Note that
1059  * the transport service may just ignore this message if the HELLO is
1060  * malformed or useless due to our local configuration.
1061  *
1062  * @param handle connection to transport service
1063  * @param hello the hello message
1064  * @param cont continuation to call when HELLO has been sent,
1065  *      tc reason GNUNET_SCHEDULER_REASON_TIMEOUT for fail
1066  *      tc reasong GNUNET_SCHEDULER_REASON_READY for success
1067  * @param cls closure for continuation
1068  *
1069  */
1070 void
1071 GNUNET_TRANSPORT_offer_hello (struct GNUNET_TRANSPORT_Handle *handle,
1072                               const struct GNUNET_MessageHeader *hello,
1073                               GNUNET_SCHEDULER_Task cont, void *cls)
1074 {
1075   uint16_t size;
1076   struct GNUNET_PeerIdentity peer;
1077   struct GNUNET_MessageHeader *msg;
1078   struct SendHelloContext * shc;
1079   struct GNUNET_SCHEDULER_TaskContext tc;
1080
1081   tc.read_ready = NULL;
1082   tc.write_ready = NULL;
1083   tc.reason = GNUNET_SCHEDULER_REASON_TIMEOUT;
1084
1085   if (NULL == handle->client)
1086   {
1087     if (NULL != cont)
1088       cont (cls, &tc);
1089     return;
1090   }
1091   GNUNET_break (ntohs (hello->type) == GNUNET_MESSAGE_TYPE_HELLO);
1092   size = ntohs (hello->size);
1093   GNUNET_break (size >= sizeof (struct GNUNET_MessageHeader));
1094   if (GNUNET_OK !=
1095       GNUNET_HELLO_get_id ((const struct GNUNET_HELLO_Message *) hello, &peer))
1096   {
1097     GNUNET_break (0);
1098     if (NULL != cont)
1099       if (NULL != cont)
1100         cont (cls, &tc);
1101     return;
1102   }
1103   msg = GNUNET_malloc (size);
1104   memcpy (msg, hello, size);
1105   LOG (GNUNET_ERROR_TYPE_DEBUG,
1106        "Offering `%s' message of `%4s' to transport for validation.\n", "HELLO",
1107        GNUNET_i2s (&peer));
1108   shc = GNUNET_malloc (sizeof (struct SendHelloContext));
1109   shc->msg = msg;
1110   shc->cont = cont;
1111   shc->cls = cls;
1112   schedule_control_transmit (handle, size, &send_hello, shc);
1113 }
1114
1115
1116 /**
1117  * Obtain the HELLO message for this peer.
1118  *
1119  * @param handle connection to transport service
1120  * @param rec function to call with the HELLO, sender will be our peer
1121  *            identity; message and sender will be NULL on timeout
1122  *            (handshake with transport service pending/failed).
1123  *             cost estimate will be 0.
1124  * @param rec_cls closure for rec
1125  * @return handle to cancel the operation
1126  */
1127 struct GNUNET_TRANSPORT_GetHelloHandle *
1128 GNUNET_TRANSPORT_get_hello (struct GNUNET_TRANSPORT_Handle *handle,
1129                             GNUNET_TRANSPORT_HelloUpdateCallback rec,
1130                             void *rec_cls)
1131 {
1132   struct GNUNET_TRANSPORT_GetHelloHandle *hwl;
1133
1134   hwl = GNUNET_malloc (sizeof (struct GNUNET_TRANSPORT_GetHelloHandle));
1135   hwl->rec = rec;
1136   hwl->rec_cls = rec_cls;
1137   hwl->handle = handle;
1138   GNUNET_CONTAINER_DLL_insert (handle->hwl_head, handle->hwl_tail, hwl);
1139   if (handle->my_hello != NULL)
1140     rec (rec_cls, (const struct GNUNET_MessageHeader *) handle->my_hello);
1141   return hwl;
1142 }
1143
1144
1145 /**
1146  * Stop receiving updates about changes to our HELLO message.
1147  *
1148  * @param ghh handle to cancel
1149  */
1150 void
1151 GNUNET_TRANSPORT_get_hello_cancel (struct GNUNET_TRANSPORT_GetHelloHandle *ghh)
1152 {
1153   struct GNUNET_TRANSPORT_Handle *handle = ghh->handle;
1154
1155   GNUNET_CONTAINER_DLL_remove (handle->hwl_head, handle->hwl_tail, ghh);
1156   GNUNET_free (ghh);
1157 }
1158
1159
1160 /**
1161  * Connect to the transport service.  Note that the connection may
1162  * complete (or fail) asynchronously.
1163  *
1164  * @param cfg configuration to use
1165  * @param self our own identity (API should check that it matches
1166  *             the identity found by transport), or NULL (no check)
1167  * @param cls closure for the callbacks
1168  * @param rec receive function to call
1169  * @param nc function to call on connect events
1170  * @param nd function to call on disconnect events
1171  */
1172 struct GNUNET_TRANSPORT_Handle *
1173 GNUNET_TRANSPORT_connect (const struct GNUNET_CONFIGURATION_Handle *cfg,
1174                           const struct GNUNET_PeerIdentity *self, void *cls,
1175                           GNUNET_TRANSPORT_ReceiveCallback rec,
1176                           GNUNET_TRANSPORT_NotifyConnect nc,
1177                           GNUNET_TRANSPORT_NotifyDisconnect nd)
1178 {
1179   struct GNUNET_TRANSPORT_Handle *ret;
1180
1181   ret = GNUNET_malloc (sizeof (struct GNUNET_TRANSPORT_Handle));
1182   if (self != NULL)
1183   {
1184     ret->self = *self;
1185     ret->check_self = GNUNET_YES;
1186   }
1187   ret->cfg = cfg;
1188   ret->cls = cls;
1189   ret->rec = rec;
1190   ret->nc_cb = nc;
1191   ret->nd_cb = nd;
1192   ret->reconnect_delay = GNUNET_TIME_UNIT_ZERO;
1193   ret->neighbours =
1194       GNUNET_CONTAINER_multihashmap_create (STARTING_NEIGHBOURS_SIZE);
1195   ret->ready_heap =
1196       GNUNET_CONTAINER_heap_create (GNUNET_CONTAINER_HEAP_ORDER_MIN);
1197   LOG (GNUNET_ERROR_TYPE_DEBUG, "Connecting to transport service.\n");
1198   ret->client = GNUNET_CLIENT_connect ("transport", cfg);
1199   if (ret->client == NULL)
1200   {
1201     GNUNET_free (ret);
1202     return NULL;
1203   }
1204   schedule_control_transmit (ret, sizeof (struct StartMessage), &send_start, ret);
1205   return ret;
1206 }
1207
1208
1209 /**
1210  * Disconnect from the transport service.
1211  *
1212  * @param handle handle to the service as returned from GNUNET_TRANSPORT_connect
1213  */
1214 void
1215 GNUNET_TRANSPORT_disconnect (struct GNUNET_TRANSPORT_Handle *handle)
1216 {
1217   LOG (GNUNET_ERROR_TYPE_DEBUG, "Transport disconnect called!\n");
1218   /* this disconnects all neighbours... */
1219   if (handle->reconnect_task == GNUNET_SCHEDULER_NO_TASK)
1220     disconnect_and_schedule_reconnect (handle);
1221   /* and now we stop trying to connect again... */
1222   if (handle->reconnect_task != GNUNET_SCHEDULER_NO_TASK)
1223   {
1224     GNUNET_SCHEDULER_cancel (handle->reconnect_task);
1225     handle->reconnect_task = GNUNET_SCHEDULER_NO_TASK;
1226   }
1227   GNUNET_CONTAINER_multihashmap_destroy (handle->neighbours);
1228   handle->neighbours = NULL;
1229   if (handle->quota_task != GNUNET_SCHEDULER_NO_TASK)
1230   {
1231     GNUNET_SCHEDULER_cancel (handle->quota_task);
1232     handle->quota_task = GNUNET_SCHEDULER_NO_TASK;
1233   }
1234   GNUNET_free_non_null (handle->my_hello);
1235   handle->my_hello = NULL;
1236   GNUNET_assert (handle->hwl_head == NULL);
1237   GNUNET_assert (handle->hwl_tail == NULL);
1238   GNUNET_CONTAINER_heap_destroy (handle->ready_heap);
1239   handle->ready_heap = NULL;
1240   GNUNET_free (handle);
1241 }
1242
1243
1244 /**
1245  * Check if we could queue a message of the given size for
1246  * transmission.  The transport service will take both its
1247  * internal buffers and bandwidth limits imposed by the
1248  * other peer into consideration when answering this query.
1249  *
1250  * @param handle connection to transport service
1251  * @param target who should receive the message
1252  * @param size how big is the message we want to transmit?
1253  * @param priority how important is the message?
1254  * @param timeout after how long should we give up (and call
1255  *        notify with buf NULL and size 0)?
1256  * @param notify function to call when we are ready to
1257  *        send such a message
1258  * @param notify_cls closure for notify
1259  * @return NULL if someone else is already waiting to be notified
1260  *         non-NULL if the notify callback was queued (can be used to cancel
1261  *         using GNUNET_TRANSPORT_notify_transmit_ready_cancel)
1262  */
1263 struct GNUNET_TRANSPORT_TransmitHandle *
1264 GNUNET_TRANSPORT_notify_transmit_ready (struct GNUNET_TRANSPORT_Handle *handle,
1265                                         const struct GNUNET_PeerIdentity
1266                                         *target, size_t size, uint32_t priority,
1267                                         struct GNUNET_TIME_Relative timeout,
1268                                         GNUNET_CONNECTION_TransmitReadyNotify
1269                                         notify, void *notify_cls)
1270 {
1271   struct Neighbour *n;
1272   struct GNUNET_TRANSPORT_TransmitHandle *th;
1273   struct GNUNET_TIME_Relative delay;
1274
1275   n = neighbour_find (handle, target);
1276   if (NULL == n)
1277   {
1278     /* use GNUNET_TRANSPORT_try_connect first, only use this function
1279      * once a connection has been established */
1280     GNUNET_assert (0);
1281     return NULL;
1282   }
1283   if (NULL != n->th)
1284   {
1285     /* attempt to send two messages at the same time to the same peer */
1286     GNUNET_assert (0);
1287     return NULL;
1288   }
1289   GNUNET_assert (NULL == n->hn);
1290   th = GNUNET_malloc (sizeof (struct GNUNET_TRANSPORT_TransmitHandle));
1291   th->neighbour = n;
1292   th->notify = notify;
1293   th->notify_cls = notify_cls;
1294   th->timeout = GNUNET_TIME_relative_to_absolute (timeout);
1295   th->notify_size = size;
1296   th->priority = priority;
1297   n->th = th;
1298   /* calculate when our transmission should be ready */
1299   delay = GNUNET_BANDWIDTH_tracker_get_delay (&n->out_tracker, size);
1300   if (delay.rel_value > timeout.rel_value)
1301     delay.rel_value = 0;        /* notify immediately (with failure) */
1302   LOG (GNUNET_ERROR_TYPE_DEBUG,
1303        "Bandwidth tracker allows next transmission to peer %s in %llu ms\n",
1304        GNUNET_i2s (target), (unsigned long long) delay.rel_value);
1305   n->hn = GNUNET_CONTAINER_heap_insert (handle->ready_heap, n, delay.rel_value);
1306   schedule_transmission (handle);
1307   return th;
1308 }
1309
1310
1311 /**
1312  * Cancel the specified transmission-ready notification.
1313  *
1314  * @param th handle returned from GNUNET_TRANSPORT_notify_transmit_ready
1315  */
1316 void
1317 GNUNET_TRANSPORT_notify_transmit_ready_cancel (struct
1318                                                GNUNET_TRANSPORT_TransmitHandle
1319                                                *th)
1320 {
1321   struct Neighbour *n;
1322
1323   GNUNET_assert (NULL == th->next);
1324   GNUNET_assert (NULL == th->prev);
1325   n = th->neighbour;
1326   GNUNET_assert (th == n->th);
1327   n->th = NULL;
1328   if (n->hn != NULL)
1329   {
1330     GNUNET_CONTAINER_heap_remove_node (n->hn);
1331     n->hn = NULL;
1332   }
1333   else
1334   {
1335     GNUNET_assert (GNUNET_SCHEDULER_NO_TASK != th->timeout_task);
1336     GNUNET_SCHEDULER_cancel (th->timeout_task);
1337     th->timeout_task = GNUNET_SCHEDULER_NO_TASK;
1338   }
1339   GNUNET_free (th);
1340 }
1341
1342
1343 /* end of transport_api.c */