major bugfix: is_ready must only be set to GNUNET_NO if we actually did transmit...
[oweals/gnunet.git] / src / transport / transport_api.c
1 /*
2      This file is part of GNUnet.
3      Copyright (C) 2009-2013 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., 51 Franklin Street, Fifth Floor,
18      Boston, MA 02110-1301, 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  * - test test test
28  */
29 #include "platform.h"
30 #include "gnunet_util_lib.h"
31 #include "gnunet_constants.h"
32 #include "gnunet_arm_service.h"
33 #include "gnunet_hello_lib.h"
34 #include "gnunet_protocols.h"
35 #include "gnunet_transport_service.h"
36 #include "transport.h"
37
38 #define LOG(kind,...) GNUNET_log_from (kind, "transport-api",__VA_ARGS__)
39
40 /**
41  * If we could not send any payload to a peer for this amount of
42  * time, we print a warning.
43  */
44 #define UNREADY_WARN_TIME GNUNET_TIME_UNIT_MINUTES
45
46 /**
47  * How large to start with for the hashmap of neighbours.
48  */
49 #define STARTING_NEIGHBOURS_SIZE 16
50
51 /**
52  * Handle for a message that should be transmitted to the service.
53  * Used for both control messages and normal messages.
54  */
55 struct GNUNET_TRANSPORT_TransmitHandle
56 {
57
58   /**
59    * We keep all requests in a DLL.
60    */
61   struct GNUNET_TRANSPORT_TransmitHandle *next;
62
63   /**
64    * We keep all requests in a DLL.
65    */
66   struct GNUNET_TRANSPORT_TransmitHandle *prev;
67
68   /**
69    * Neighbour for this handle, NULL for control messages.
70    */
71   struct Neighbour *neighbour;
72
73   /**
74    * Function to call when @e notify_size bytes are available
75    * for transmission.
76    */
77   GNUNET_TRANSPORT_TransmitReadyNotify notify;
78
79   /**
80    * Closure for @e notify.
81    */
82   void *notify_cls;
83
84   /**
85    * Time at which this request was originally scheduled.
86    */
87   struct GNUNET_TIME_Absolute request_start;
88
89   /**
90    * Timeout for this request, 0 for control messages.
91    */
92   struct GNUNET_TIME_Absolute timeout;
93
94   /**
95    * Task to trigger request timeout if the request is stalled due to
96    * congestion.
97    */
98   struct GNUNET_SCHEDULER_Task *timeout_task;
99
100   /**
101    * How many bytes is our notify callback waiting for?
102    */
103   size_t notify_size;
104
105 };
106
107
108 /**
109  * Entry in hash table of all of our current (connected) neighbours.
110  */
111 struct Neighbour
112 {
113   /**
114    * Overall transport handle.
115    */
116   struct GNUNET_TRANSPORT_Handle *h;
117
118   /**
119    * Active transmit handle or NULL.
120    */
121   struct GNUNET_TRANSPORT_TransmitHandle *th;
122
123   /**
124    * Identity of this neighbour.
125    */
126   struct GNUNET_PeerIdentity id;
127
128   /**
129    * Outbound bandwidh tracker.
130    */
131   struct GNUNET_BANDWIDTH_Tracker out_tracker;
132
133   /**
134    * Entry in our readyness heap (which is sorted by @e next_ready
135    * value).  NULL if there is no pending transmission request for
136    * this neighbour or if we're waiting for @e is_ready to become
137    * true AFTER the @e out_tracker suggested that this peer's quota
138    * has been satisfied (so once @e is_ready goes to #GNUNET_YES,
139    * we should immediately go back into the heap).
140    */
141   struct GNUNET_CONTAINER_HeapNode *hn;
142
143   /**
144    * Last time when this peer received payload from us.
145    */
146   struct GNUNET_TIME_Absolute last_payload;
147
148   /**
149    * Task to trigger warnings if we do not get SEND_OK after a while.
150    */
151   struct GNUNET_SCHEDULER_Task *unready_warn_task;
152
153   /**
154    * Is this peer currently ready to receive a message?
155    */
156   int is_ready;
157
158   /**
159    * Sending consumed more bytes on wire than payload was announced
160    * This overhead is added to the delay of next sending operation
161    */
162   size_t traffic_overhead;
163 };
164
165
166 /**
167  * Linked list of functions to call whenever our HELLO is updated.
168  */
169 struct GNUNET_TRANSPORT_GetHelloHandle
170 {
171
172   /**
173    * This is a doubly linked list.
174    */
175   struct GNUNET_TRANSPORT_GetHelloHandle *next;
176
177   /**
178    * This is a doubly linked list.
179    */
180   struct GNUNET_TRANSPORT_GetHelloHandle *prev;
181
182   /**
183    * Transport handle.
184    */
185   struct GNUNET_TRANSPORT_Handle *handle;
186
187   /**
188    * Callback to call once we got our HELLO.
189    */
190   GNUNET_TRANSPORT_HelloUpdateCallback rec;
191
192   /**
193    * Task for calling the HelloUpdateCallback when we already have a HELLO
194    */
195   struct GNUNET_SCHEDULER_Task *notify_task;
196
197   /**
198    * Closure for @e rec.
199    */
200   void *rec_cls;
201
202 };
203
204
205 /**
206  * Entry in linked list for a try-connect request.
207  */
208 struct GNUNET_TRANSPORT_TryConnectHandle
209 {
210   /**
211    * For the DLL.
212    */
213   struct GNUNET_TRANSPORT_TryConnectHandle *prev;
214
215   /**
216    * For the DLL.
217    */
218   struct GNUNET_TRANSPORT_TryConnectHandle *next;
219
220   /**
221    * Peer we should try to connect to.
222    */
223   struct GNUNET_PeerIdentity pid;
224
225   /**
226    * Transport service handle this request is part of.
227    */
228   struct GNUNET_TRANSPORT_Handle *th;
229
230   /**
231    * Message transmission request to communicate to service.
232    */
233   struct GNUNET_TRANSPORT_TransmitHandle *tth;
234
235   /**
236    * Function to call upon completion (of request transmission).
237    */
238   GNUNET_TRANSPORT_TryConnectCallback cb;
239
240   /**
241    * Closure for @e cb.
242    */
243   void *cb_cls;
244
245 };
246
247
248 /**
249  * Entry in linked list for all try-disconnect requests
250  */
251 struct GNUNET_TRANSPORT_TryDisconnectHandle
252 {
253   /**
254    * For the DLL.
255    */
256   struct GNUNET_TRANSPORT_TryDisconnectHandle *prev;
257
258   /**
259    * For the DLL.
260    */
261   struct GNUNET_TRANSPORT_TryDisconnectHandle *next;
262
263   /**
264    * Peer we should try to connect to.
265    */
266   struct GNUNET_PeerIdentity pid;
267
268   /**
269    * Transport service handle this request is part of.
270    */
271   struct GNUNET_TRANSPORT_Handle *th;
272
273   /**
274    * Message transmission request to communicate to service.
275    */
276   struct GNUNET_TRANSPORT_TransmitHandle *tth;
277
278   /**
279    * Function to call upon completion (of request transmission).
280    */
281   GNUNET_TRANSPORT_TryDisconnectCallback cb;
282
283   /**
284    * Closure for @e cb.
285    */
286   void *cb_cls;
287
288 };
289
290
291 /**
292  * Entry in linked list for all offer-HELLO requests.
293  */
294 struct GNUNET_TRANSPORT_OfferHelloHandle
295 {
296   /**
297    * For the DLL.
298    */
299   struct GNUNET_TRANSPORT_OfferHelloHandle *prev;
300
301   /**
302    * For the DLL.
303    */
304   struct GNUNET_TRANSPORT_OfferHelloHandle *next;
305
306   /**
307    * Transport service handle we use for transmission.
308    */
309   struct GNUNET_TRANSPORT_Handle *th;
310
311   /**
312    * Transmission handle for this request.
313    */
314   struct GNUNET_TRANSPORT_TransmitHandle *tth;
315
316   /**
317    * Function to call once we are done.
318    */
319   GNUNET_SCHEDULER_TaskCallback cont;
320
321   /**
322    * Closure for @e cont
323    */
324   void *cls;
325
326   /**
327    * The HELLO message to be transmitted.
328    */
329   struct GNUNET_MessageHeader *msg;
330 };
331
332
333 /**
334  * Handle for the transport service (includes all of the
335  * state for the transport service).
336  */
337 struct GNUNET_TRANSPORT_Handle
338 {
339
340   /**
341    * Closure for the callbacks.
342    */
343   void *cls;
344
345   /**
346    * Function to call for received data.
347    */
348   GNUNET_TRANSPORT_ReceiveCallback rec;
349
350   /**
351    * function to call on connect events
352    */
353   GNUNET_TRANSPORT_NotifyConnect nc_cb;
354
355   /**
356    * function to call on disconnect events
357    */
358   GNUNET_TRANSPORT_NotifyDisconnect nd_cb;
359
360   /**
361    * function to call on excess bandwidth events
362    */
363   GNUNET_TRANSPORT_NotifyExcessBandwidth neb_cb;
364
365   /**
366    * Head of DLL of control messages.
367    */
368   struct GNUNET_TRANSPORT_TransmitHandle *control_head;
369
370   /**
371    * Tail of DLL of control messages.
372    */
373   struct GNUNET_TRANSPORT_TransmitHandle *control_tail;
374
375   /**
376    * The current HELLO message for this peer.  Updated
377    * whenever transports change their addresses.
378    */
379   struct GNUNET_MessageHeader *my_hello;
380
381   /**
382    * My client connection to the transport service.
383    */
384   struct GNUNET_CLIENT_Connection *client;
385
386   /**
387    * Handle to our registration with the client for notification.
388    */
389   struct GNUNET_CLIENT_TransmitHandle *cth;
390
391   /**
392    * Linked list of pending requests for our HELLO.
393    */
394   struct GNUNET_TRANSPORT_GetHelloHandle *hwl_head;
395
396   /**
397    * Linked list of pending requests for our HELLO.
398    */
399   struct GNUNET_TRANSPORT_GetHelloHandle *hwl_tail;
400
401   /**
402    * Linked list of pending try connect requests head
403    */
404   struct GNUNET_TRANSPORT_TryConnectHandle *tc_head;
405
406   /**
407    * Linked list of pending try connect requests tail
408    */
409   struct GNUNET_TRANSPORT_TryConnectHandle *tc_tail;
410
411   /**
412    * Linked list of pending try disconnect requests head
413    */
414   struct GNUNET_TRANSPORT_TryDisconnectHandle *td_head;
415
416   /**
417    * Linked list of pending try connect requests tail
418    */
419   struct GNUNET_TRANSPORT_TryDisconnectHandle *td_tail;
420
421   /**
422    * Linked list of pending offer HELLO requests head
423    */
424   struct GNUNET_TRANSPORT_OfferHelloHandle *oh_head;
425
426   /**
427    * Linked list of pending offer HELLO requests tail
428    */
429   struct GNUNET_TRANSPORT_OfferHelloHandle *oh_tail;
430
431   /**
432    * My configuration.
433    */
434   const struct GNUNET_CONFIGURATION_Handle *cfg;
435
436   /**
437    * Hash map of the current connected neighbours of this peer.
438    * Maps peer identities to `struct Neighbour` entries.
439    */
440   struct GNUNET_CONTAINER_MultiPeerMap *neighbours;
441
442   /**
443    * Heap sorting peers with pending messages by the timestamps that
444    * specify when we could next send a message to the respective peer.
445    * Excludes control messages (which can always go out immediately).
446    * Maps time stamps to `struct Neighbour` entries.
447    */
448   struct GNUNET_CONTAINER_Heap *ready_heap;
449
450   /**
451    * Peer identity as assumed by this process, or all zeros.
452    */
453   struct GNUNET_PeerIdentity self;
454
455   /**
456    * ID of the task trying to reconnect to the service.
457    */
458   struct GNUNET_SCHEDULER_Task *reconnect_task;
459
460   /**
461    * ID of the task trying to trigger transmission for a peer while
462    * maintaining bandwidth quotas.  In use if there are no control
463    * messages and the smallest entry in the @e ready_heap has a time
464    * stamp in the future.
465    */
466   struct GNUNET_SCHEDULER_Task *quota_task;
467
468   /**
469    * Delay until we try to reconnect.
470    */
471   struct GNUNET_TIME_Relative reconnect_delay;
472
473   /**
474    * Should we check that @e self matches what the service thinks?
475    * (if #GNUNET_NO, then @e self is all zeros!).
476    */
477   int check_self;
478
479   /**
480    * Reconnect in progress
481    */
482   int reconnecting;
483 };
484
485
486 /**
487  * Schedule the task to send one message, either from the control
488  * list or the peer message queues  to the service.
489  *
490  * @param h transport service to schedule a transmission for
491  */
492 static void
493 schedule_transmission (struct GNUNET_TRANSPORT_Handle *h);
494
495
496 /**
497  * Function that will schedule the job that will try
498  * to connect us again to the client.
499  *
500  * @param h transport service to reconnect
501  */
502 static void
503 disconnect_and_schedule_reconnect (struct GNUNET_TRANSPORT_Handle *h);
504
505
506 /**
507  * A neighbour has not gotten a SEND_OK in a  while. Print a warning.
508  *
509  * @param cls the `struct Neighbour`
510  * @param tc scheduler context
511  */
512 static void
513 do_warn_unready (void *cls,
514                  const struct GNUNET_SCHEDULER_TaskContext *tc)
515 {
516   struct Neighbour *n = cls;
517   struct GNUNET_TIME_Relative delay;
518
519   delay = GNUNET_TIME_absolute_get_duration (n->last_payload);
520   GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
521               "Lacking SEND_OK, no payload could be send to %s for %s\n",
522               GNUNET_i2s (&n->id),
523               GNUNET_STRINGS_relative_time_to_string (delay,
524                                                       GNUNET_YES));
525   n->unready_warn_task
526     = GNUNET_SCHEDULER_add_delayed (UNREADY_WARN_TIME,
527                                     &do_warn_unready,
528                                     n);
529 }
530
531
532 /**
533  * Get the neighbour list entry for the given peer
534  *
535  * @param h our context
536  * @param peer peer to look up
537  * @return NULL if no such peer entry exists
538  */
539 static struct Neighbour *
540 neighbour_find (struct GNUNET_TRANSPORT_Handle *h,
541                 const struct GNUNET_PeerIdentity *peer)
542 {
543   return GNUNET_CONTAINER_multipeermap_get (h->neighbours,
544                                             peer);
545 }
546
547
548 /**
549  * The outbound quota has changed in a way that may require
550  * us to reset the timeout.  Update the timeout.
551  *
552  * @param cls the `struct Neighbour` for which the timeout changed
553  */
554 static void
555 outbound_bw_tracker_update (void *cls)
556 {
557   struct Neighbour *n = cls;
558   struct GNUNET_TIME_Relative delay;
559
560   if (NULL == n->hn)
561     return;
562   delay = GNUNET_BANDWIDTH_tracker_get_delay (&n->out_tracker,
563                                               n->th->notify_size + n->traffic_overhead);
564   LOG (GNUNET_ERROR_TYPE_DEBUG,
565        "New outbound delay %s us\n",
566        GNUNET_STRINGS_relative_time_to_string (delay,
567                                                GNUNET_NO));
568   GNUNET_CONTAINER_heap_update_cost (n->h->ready_heap,
569       n->hn, delay.rel_value_us);
570   schedule_transmission (n->h);
571 }
572
573
574 /**
575  * Function called by the bandwidth tracker if we have excess
576  * bandwidth.
577  *
578  * @param cls the `struct Neighbour` that has excess bandwidth
579  */
580 static void
581 notify_excess_cb (void *cls)
582 {
583   struct Neighbour *n = cls;
584   struct GNUNET_TRANSPORT_Handle *h = n->h;
585
586   if (NULL != h->neb_cb)
587     h->neb_cb (h->cls,
588                &n->id);
589 }
590
591
592 /**
593  * Add neighbour to our list
594  *
595  * @return NULL if this API is currently disconnecting from the service
596  */
597 static struct Neighbour *
598 neighbour_add (struct GNUNET_TRANSPORT_Handle *h,
599                const struct GNUNET_PeerIdentity *pid)
600 {
601   struct Neighbour *n;
602
603   LOG (GNUNET_ERROR_TYPE_DEBUG,
604        "Creating entry for neighbour `%s'.\n",
605        GNUNET_i2s (pid));
606   n = GNUNET_new (struct Neighbour);
607   n->id = *pid;
608   n->h = h;
609   n->is_ready = GNUNET_YES;
610   n->traffic_overhead = 0;
611   GNUNET_BANDWIDTH_tracker_init2 (&n->out_tracker,
612                                   &outbound_bw_tracker_update,
613                                   n,
614                                   GNUNET_CONSTANTS_DEFAULT_BW_IN_OUT,
615                                   MAX_BANDWIDTH_CARRY_S,
616                                   &notify_excess_cb,
617                                   n);
618   GNUNET_assert (GNUNET_OK ==
619                  GNUNET_CONTAINER_multipeermap_put (h->neighbours,
620                                                     &n->id,
621                                                     n,
622                                                     GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
623   return n;
624 }
625
626
627 /**
628  * Iterator over hash map entries, for deleting state of a neighbour.
629  *
630  * @param cls the `struct GNUNET_TRANSPORT_Handle *`
631  * @param key peer identity
632  * @param value value in the hash map, the neighbour entry to delete
633  * @return #GNUNET_YES if we should continue to
634  *         iterate,
635  *         #GNUNET_NO if not.
636  */
637 static int
638 neighbour_delete (void *cls,
639                   const struct GNUNET_PeerIdentity *key,
640                   void *value)
641 {
642   struct GNUNET_TRANSPORT_Handle *handle = cls;
643   struct Neighbour *n = value;
644
645   LOG (GNUNET_ERROR_TYPE_DEBUG,
646        "Dropping entry for neighbour `%s'.\n",
647        GNUNET_i2s (key));
648   GNUNET_BANDWIDTH_tracker_notification_stop (&n->out_tracker);
649   if (NULL != handle->nd_cb)
650     handle->nd_cb (handle->cls,
651                    &n->id);
652   if (NULL != n->unready_warn_task)
653   {
654     GNUNET_SCHEDULER_cancel (n->unready_warn_task);
655     n->unready_warn_task = NULL;
656   }
657   GNUNET_assert (NULL == n->th);
658   GNUNET_assert (NULL == n->hn);
659   GNUNET_assert (GNUNET_YES ==
660                  GNUNET_CONTAINER_multipeermap_remove (handle->neighbours,
661                                                        key,
662                                                        n));
663   GNUNET_free (n);
664   return GNUNET_YES;
665 }
666
667
668 /**
669  * Function we use for handling incoming messages.
670  *
671  * @param cls closure, a `struct GNUNET_TRANSPORT_Handle *`
672  * @param msg message received, NULL on timeout or fatal error
673  */
674 static void
675 demultiplexer (void *cls,
676                const struct GNUNET_MessageHeader *msg)
677 {
678   struct GNUNET_TRANSPORT_Handle *h = cls;
679   const struct DisconnectInfoMessage *dim;
680   const struct ConnectInfoMessage *cim;
681   const struct InboundMessage *im;
682   const struct GNUNET_MessageHeader *imm;
683   const struct SendOkMessage *okm;
684   const struct QuotaSetMessage *qm;
685   struct GNUNET_TRANSPORT_GetHelloHandle *hwl;
686   struct GNUNET_TRANSPORT_GetHelloHandle *next_hwl;
687   struct Neighbour *n;
688   struct GNUNET_PeerIdentity me;
689   uint16_t size;
690   uint32_t bytes_msg;
691   uint32_t bytes_physical;
692
693   GNUNET_assert (NULL != h->client);
694   if (GNUNET_YES == h->reconnecting)
695   {
696     return;
697   }
698   if (NULL == msg)
699   {
700     LOG (GNUNET_ERROR_TYPE_DEBUG,
701          "Error receiving from transport service, disconnecting temporarily.\n");
702     h->reconnecting = GNUNET_YES;
703     disconnect_and_schedule_reconnect (h);
704     return;
705   }
706   GNUNET_CLIENT_receive (h->client,
707                          &demultiplexer,
708                          h,
709                          GNUNET_TIME_UNIT_FOREVER_REL);
710   size = ntohs (msg->size);
711   switch (ntohs (msg->type))
712   {
713   case GNUNET_MESSAGE_TYPE_HELLO:
714     if (GNUNET_OK !=
715         GNUNET_HELLO_get_id ((const struct GNUNET_HELLO_Message *) msg,
716                              &me))
717     {
718       GNUNET_break (0);
719       break;
720     }
721     LOG (GNUNET_ERROR_TYPE_DEBUG,
722          "Receiving (my own) HELLO message (%u bytes), I am `%s'.\n",
723          (unsigned int) size,
724          GNUNET_i2s (&me));
725     GNUNET_free_non_null (h->my_hello);
726     h->my_hello = NULL;
727     if (size < sizeof (struct GNUNET_MessageHeader))
728     {
729       GNUNET_break (0);
730       break;
731     }
732     h->my_hello = GNUNET_copy_message (msg);
733     hwl = h->hwl_head;
734     while (NULL != hwl)
735     {
736       next_hwl = hwl->next;
737       hwl->rec (hwl->rec_cls,
738                 h->my_hello);
739       hwl = next_hwl;
740     }
741     break;
742   case GNUNET_MESSAGE_TYPE_TRANSPORT_CONNECT:
743     if (size < sizeof (struct ConnectInfoMessage))
744     {
745       GNUNET_break (0);
746       break;
747     }
748     cim = (const struct ConnectInfoMessage *) msg;
749     if (size !=
750         sizeof (struct ConnectInfoMessage))
751     {
752       GNUNET_break (0);
753       break;
754     }
755     LOG (GNUNET_ERROR_TYPE_DEBUG,
756          "Receiving CONNECT message for `%s'.\n",
757          GNUNET_i2s (&cim->id));
758     n = neighbour_find (h, &cim->id);
759     if (NULL != n)
760     {
761       GNUNET_break (0);
762       break;
763     }
764     n = neighbour_add (h,
765                        &cim->id);
766     LOG (GNUNET_ERROR_TYPE_DEBUG,
767          "Receiving CONNECT message for `%s' with quota %u\n",
768          GNUNET_i2s (&cim->id),
769          ntohl (cim->quota_out.value__));
770     GNUNET_BANDWIDTH_tracker_update_quota (&n->out_tracker,
771                                            cim->quota_out);
772     if (NULL != h->nc_cb)
773       h->nc_cb (h->cls,
774                 &n->id);
775     break;
776   case GNUNET_MESSAGE_TYPE_TRANSPORT_DISCONNECT:
777     if (size != sizeof (struct DisconnectInfoMessage))
778     {
779       GNUNET_break (0);
780       break;
781     }
782     dim = (const struct DisconnectInfoMessage *) msg;
783     GNUNET_break (ntohl (dim->reserved) == 0);
784     LOG (GNUNET_ERROR_TYPE_DEBUG,
785          "Receiving DISCONNECT message for `%s'.\n",
786          GNUNET_i2s (&dim->peer));
787     n = neighbour_find (h, &dim->peer);
788     if (NULL == n)
789     {
790       GNUNET_break (0);
791       break;
792     }
793     neighbour_delete (h,
794                       &dim->peer,
795                       n);
796     break;
797   case GNUNET_MESSAGE_TYPE_TRANSPORT_SEND_OK:
798     if (size != sizeof (struct SendOkMessage))
799     {
800       GNUNET_break (0);
801       break;
802     }
803     okm = (const struct SendOkMessage *) msg;
804     bytes_msg = ntohl (okm->bytes_msg);
805     bytes_physical = ntohl (okm->bytes_physical);
806     LOG (GNUNET_ERROR_TYPE_DEBUG,
807          "Receiving SEND_OK message, transmission %s.\n",
808          ntohl (okm->success) == GNUNET_OK ? "succeeded" : "failed");
809
810     n = neighbour_find (h,
811                         &okm->peer);
812     if (NULL == n)
813     {
814       /* we should never get a 'SEND_OK' for a peer that we are not
815          connected to */
816       GNUNET_break (0);
817       break;
818     }
819     if (bytes_physical >= bytes_msg)
820     {
821       LOG (GNUNET_ERROR_TYPE_DEBUG,
822            "Overhead for %u byte message: %u\n",
823            bytes_msg,
824            bytes_physical - bytes_msg);
825       n->traffic_overhead += bytes_physical - bytes_msg;
826     }
827     GNUNET_break (GNUNET_NO == n->is_ready);
828     n->is_ready = GNUNET_YES;
829     GNUNET_SCHEDULER_cancel (n->unready_warn_task);
830     n->unready_warn_task = NULL;
831     if ((NULL != n->th) && (NULL == n->hn))
832     {
833       GNUNET_assert (NULL != n->th->timeout_task);
834       GNUNET_SCHEDULER_cancel (n->th->timeout_task);
835       n->th->timeout_task = NULL;
836       /* we've been waiting for this (congestion, not quota,
837        * caused delayed transmission) */
838       n->hn = GNUNET_CONTAINER_heap_insert (h->ready_heap, n, 0);
839       schedule_transmission (h);
840     }
841     break;
842   case GNUNET_MESSAGE_TYPE_TRANSPORT_RECV:
843     if (size <
844         sizeof (struct InboundMessage) + sizeof (struct GNUNET_MessageHeader))
845     {
846       GNUNET_break (0);
847       break;
848     }
849     im = (const struct InboundMessage *) msg;
850     imm = (const struct GNUNET_MessageHeader *) &im[1];
851     if (ntohs (imm->size) + sizeof (struct InboundMessage) != size)
852     {
853       GNUNET_break (0);
854       break;
855     }
856     LOG (GNUNET_ERROR_TYPE_DEBUG,
857          "Received message of type %u from `%s'.\n",
858          ntohs (imm->type), GNUNET_i2s (&im->peer));
859     n = neighbour_find (h, &im->peer);
860     if (NULL == n)
861     {
862       GNUNET_break (0);
863       break;
864     }
865     if (NULL != h->rec)
866       h->rec (h->cls,
867               &im->peer,
868               imm);
869     break;
870   case GNUNET_MESSAGE_TYPE_TRANSPORT_SET_QUOTA:
871     if (size != sizeof (struct QuotaSetMessage))
872     {
873       GNUNET_break (0);
874       break;
875     }
876     qm = (const struct QuotaSetMessage *) msg;
877     n = neighbour_find (h, &qm->peer);
878     if (NULL == n)
879       break;
880     LOG (GNUNET_ERROR_TYPE_DEBUG,
881          "Receiving SET_QUOTA message for `%s' with quota %u\n",
882          GNUNET_i2s (&qm->peer),
883          ntohl (qm->quota.value__));
884     GNUNET_BANDWIDTH_tracker_update_quota (&n->out_tracker,
885                                            qm->quota);
886     break;
887   default:
888     LOG (GNUNET_ERROR_TYPE_ERROR,
889          _("Received unexpected message of type %u in %s:%u\n"),
890          ntohs (msg->type),
891          __FILE__,
892          __LINE__);
893     GNUNET_break (0);
894     break;
895   }
896 }
897
898
899 /**
900  * A transmission request could not be satisfied because of
901  * network congestion.  Notify the initiator and clean up.
902  *
903  * @param cls the `struct GNUNET_TRANSPORT_TransmitHandle`
904  * @param tc scheduler context
905  */
906 static void
907 timeout_request_due_to_congestion (void *cls,
908                                    const struct GNUNET_SCHEDULER_TaskContext *tc)
909 {
910   struct GNUNET_TRANSPORT_TransmitHandle *th = cls;
911   struct Neighbour *n = th->neighbour;
912   struct GNUNET_TIME_Relative delay;
913
914   n->th->timeout_task = NULL;
915   delay = GNUNET_TIME_absolute_get_duration (th->request_start);
916   LOG (GNUNET_ERROR_TYPE_WARNING,
917        "Discarding %u bytes of payload message after %s delay due to congestion\n",
918        th->notify_size,
919        GNUNET_STRINGS_relative_time_to_string (delay,
920                                                GNUNET_YES));
921   GNUNET_assert (th == n->th);
922   GNUNET_assert (NULL == n->hn);
923   n->th = NULL;
924   th->notify (th->notify_cls,
925               0,
926               NULL);
927   GNUNET_free (th);
928 }
929
930
931 /**
932  * Transmit message(s) to service.
933  *
934  * @param cls handle to transport
935  * @param size number of bytes available in @a buf
936  * @param buf where to copy the message
937  * @return number of bytes copied to @a buf
938  */
939 static size_t
940 transport_notify_ready (void *cls,
941                         size_t size,
942                         void *buf)
943 {
944   struct GNUNET_TRANSPORT_Handle *h = cls;
945   struct GNUNET_TRANSPORT_TransmitHandle *th;
946   struct GNUNET_TIME_Relative delay;
947   struct Neighbour *n;
948   char *cbuf;
949   struct OutboundMessage obm;
950   size_t ret;
951   size_t nret;
952   size_t mret;
953
954   GNUNET_assert (NULL != h->client);
955   h->cth = NULL;
956   if (NULL == buf)
957   {
958     /* transmission failed */
959     disconnect_and_schedule_reconnect (h);
960     return 0;
961   }
962
963   cbuf = buf;
964   ret = 0;
965   /* first send control messages */
966   while ( (NULL != (th = h->control_head)) &&
967           (th->notify_size <= size) )
968   {
969     GNUNET_CONTAINER_DLL_remove (h->control_head,
970                                  h->control_tail,
971                                  th);
972     nret = th->notify (th->notify_cls,
973                        size,
974                        &cbuf[ret]);
975     delay = GNUNET_TIME_absolute_get_duration (th->request_start);
976     if (delay.rel_value_us > 1000 * 1000)
977       LOG (GNUNET_ERROR_TYPE_WARNING,
978            "Added %u bytes of control message at %u after %s delay\n",
979            nret,
980            ret,
981            GNUNET_STRINGS_relative_time_to_string (delay,
982                                                    GNUNET_YES));
983     else
984       LOG (GNUNET_ERROR_TYPE_DEBUG,
985            "Added %u bytes of control message at %u after %s delay\n",
986            nret,
987            ret,
988            GNUNET_STRINGS_relative_time_to_string (delay,
989                                                    GNUNET_YES));
990     GNUNET_free (th);
991     ret += nret;
992     size -= nret;
993   }
994
995   /* then, if possible and no control messages pending, send data messages */
996   while ( (NULL == h->control_head) &&
997           (NULL != (n = GNUNET_CONTAINER_heap_peek (h->ready_heap))) )
998   {
999     if (GNUNET_YES != n->is_ready)
1000     {
1001       /* peer not ready, wait for notification! */
1002       GNUNET_assert (n == GNUNET_CONTAINER_heap_remove_root (h->ready_heap));
1003       n->hn = NULL;
1004       GNUNET_assert (NULL == n->th->timeout_task);
1005       n->th->timeout_task
1006         = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_absolute_get_remaining
1007                                         (n->th->timeout),
1008                                         &timeout_request_due_to_congestion,
1009                                         n->th);
1010       continue;
1011     }
1012     th = n->th;
1013     if (th->notify_size + sizeof (struct OutboundMessage) > size)
1014       break;                    /* does not fit */
1015     if (GNUNET_BANDWIDTH_tracker_get_delay
1016         (&n->out_tracker, th->notify_size).rel_value_us > 0)
1017       break;                    /* too early */
1018     GNUNET_assert (n == GNUNET_CONTAINER_heap_remove_root (h->ready_heap));
1019     n->hn = NULL;
1020     n->th = NULL;
1021     GNUNET_assert (size >= sizeof (struct OutboundMessage));
1022     mret =
1023         th->notify (th->notify_cls, size - sizeof (struct OutboundMessage),
1024                     &cbuf[ret + sizeof (struct OutboundMessage)]);
1025     GNUNET_assert (mret <= size - sizeof (struct OutboundMessage));
1026     if (0 != mret)
1027     {
1028       if (NULL != n->unready_warn_task)
1029         n->unready_warn_task
1030           = GNUNET_SCHEDULER_add_delayed (UNREADY_WARN_TIME,
1031                                           &do_warn_unready,
1032                                           n);
1033       n->last_payload = GNUNET_TIME_absolute_get ();
1034       n->is_ready = GNUNET_NO;
1035       GNUNET_assert (mret + sizeof (struct OutboundMessage) <
1036                      GNUNET_SERVER_MAX_MESSAGE_SIZE);
1037       obm.header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_SEND);
1038       obm.header.size = htons (mret + sizeof (struct OutboundMessage));
1039       obm.reserved = htonl (0);
1040       obm.timeout =
1041           GNUNET_TIME_relative_hton (GNUNET_TIME_absolute_get_remaining
1042                                      (th->timeout));
1043       obm.peer = n->id;
1044       memcpy (&cbuf[ret],
1045               &obm,
1046               sizeof (struct OutboundMessage));
1047       ret += (mret + sizeof (struct OutboundMessage));
1048       size -= (mret + sizeof (struct OutboundMessage));
1049       GNUNET_BANDWIDTH_tracker_consume (&n->out_tracker,
1050                                         mret);
1051       delay = GNUNET_TIME_absolute_get_duration (th->request_start);
1052       if (delay.rel_value_us > 1000 * 1000)
1053         LOG (GNUNET_ERROR_TYPE_WARNING,
1054              "Added %u bytes of payload message at %u after %s delay\n",
1055              mret,
1056              ret,
1057              GNUNET_STRINGS_relative_time_to_string (delay,
1058                                                      GNUNET_YES));
1059       else
1060         LOG (GNUNET_ERROR_TYPE_DEBUG,
1061              "Added %u bytes of payload message at %u after %s delay\n",
1062              mret,
1063              ret,
1064              GNUNET_STRINGS_relative_time_to_string (delay,
1065                                                      GNUNET_YES));
1066     }
1067     GNUNET_free (th);
1068   }
1069   /* if there are more pending messages, try to schedule those */
1070   schedule_transmission (h);
1071   LOG (GNUNET_ERROR_TYPE_DEBUG,
1072        "Transmitting %u bytes to transport service\n",
1073        ret);
1074   return ret;
1075 }
1076
1077
1078 /**
1079  * Schedule the task to send one message, either from the control
1080  * list or the peer message queues  to the service.
1081  *
1082  * @param cls transport service to schedule a transmission for
1083  * @param tc scheduler context
1084  */
1085 static void
1086 schedule_transmission_task (void *cls,
1087                             const struct GNUNET_SCHEDULER_TaskContext *tc)
1088 {
1089   struct GNUNET_TRANSPORT_Handle *h = cls;
1090   size_t size;
1091   struct GNUNET_TRANSPORT_TransmitHandle *th;
1092   struct Neighbour *n;
1093
1094   h->quota_task = NULL;
1095   GNUNET_assert (NULL != h->client);
1096   /* destroy all requests that have timed out */
1097   while ( (NULL != (n = GNUNET_CONTAINER_heap_peek (h->ready_heap))) &&
1098           (0 == GNUNET_TIME_absolute_get_remaining (n->th->timeout).rel_value_us) )
1099   {
1100     /* notify client that the request could not be satisfied within
1101      * the given time constraints */
1102     th = n->th;
1103     n->th = NULL;
1104     GNUNET_assert (n == GNUNET_CONTAINER_heap_remove_root (h->ready_heap));
1105     n->hn = NULL;
1106     LOG (GNUNET_ERROR_TYPE_DEBUG,
1107          "Signalling timeout for transmission to peer %s due to congestion\n",
1108          GNUNET_i2s (&n->id));
1109     GNUNET_assert (0 == th->notify (th->notify_cls,
1110                                     0,
1111                                     NULL));
1112     GNUNET_free (th);
1113   }
1114   if (NULL != h->cth)
1115     return;
1116   if (NULL != h->control_head)
1117   {
1118     size = h->control_head->notify_size;
1119   }
1120   else
1121   {
1122     n = GNUNET_CONTAINER_heap_peek (h->ready_heap);
1123     if (NULL == n)
1124       return;                   /* no pending messages */
1125     size = n->th->notify_size + sizeof (struct OutboundMessage);
1126   }
1127   LOG (GNUNET_ERROR_TYPE_DEBUG,
1128        "Calling notify_transmit_ready\n");
1129   h->cth
1130     = GNUNET_CLIENT_notify_transmit_ready (h->client,
1131                                            size,
1132                                            GNUNET_TIME_UNIT_FOREVER_REL,
1133                                            GNUNET_NO,
1134                                            &transport_notify_ready,
1135                                            h);
1136   GNUNET_assert (NULL != h->cth);
1137 }
1138
1139
1140 /**
1141  * Schedule the task to send one message, either from the control
1142  * list or the peer message queues  to the service.
1143  *
1144  * @param h transport service to schedule a transmission for
1145  */
1146 static void
1147 schedule_transmission (struct GNUNET_TRANSPORT_Handle *h)
1148 {
1149   struct GNUNET_TIME_Relative delay;
1150   struct Neighbour *n;
1151
1152   GNUNET_assert (NULL != h->client);
1153   if (NULL != h->quota_task)
1154   {
1155     GNUNET_SCHEDULER_cancel (h->quota_task);
1156     h->quota_task = NULL;
1157   }
1158   if (NULL != h->control_head)
1159     delay = GNUNET_TIME_UNIT_ZERO;
1160   else if (NULL != (n = GNUNET_CONTAINER_heap_peek (h->ready_heap)))
1161   {
1162     delay =
1163         GNUNET_BANDWIDTH_tracker_get_delay (&n->out_tracker,
1164                                             n->th->notify_size + n->traffic_overhead);
1165     n->traffic_overhead = 0;
1166   }
1167   else
1168     return;                     /* no work to be done */
1169   LOG (GNUNET_ERROR_TYPE_DEBUG,
1170        "Scheduling next transmission to service in %s\n",
1171        GNUNET_STRINGS_relative_time_to_string (delay,
1172                                                GNUNET_YES));
1173   h->quota_task =
1174       GNUNET_SCHEDULER_add_delayed (delay,
1175                                     &schedule_transmission_task,
1176                                     h);
1177 }
1178
1179
1180 /**
1181  * Queue control request for transmission to the transport
1182  * service.
1183  *
1184  * @param h handle to the transport service
1185  * @param size number of bytes to be transmitted
1186  * @param notify function to call to get the content
1187  * @param notify_cls closure for @a notify
1188  * @return a `struct GNUNET_TRANSPORT_TransmitHandle`
1189  */
1190 static struct GNUNET_TRANSPORT_TransmitHandle *
1191 schedule_control_transmit (struct GNUNET_TRANSPORT_Handle *h,
1192                            size_t size,
1193                            GNUNET_TRANSPORT_TransmitReadyNotify notify,
1194                            void *notify_cls)
1195 {
1196   struct GNUNET_TRANSPORT_TransmitHandle *th;
1197
1198   LOG (GNUNET_ERROR_TYPE_DEBUG,
1199        "Control transmit of %u bytes requested\n",
1200        size);
1201   th = GNUNET_new (struct GNUNET_TRANSPORT_TransmitHandle);
1202   th->notify = notify;
1203   th->notify_cls = notify_cls;
1204   th->notify_size = size;
1205   th->request_start = GNUNET_TIME_absolute_get ();
1206   GNUNET_CONTAINER_DLL_insert_tail (h->control_head,
1207                                     h->control_tail,
1208                                     th);
1209   schedule_transmission (h);
1210   return th;
1211 }
1212
1213
1214 /**
1215  * Transmit START message to service.
1216  *
1217  * @param cls unused
1218  * @param size number of bytes available in @a buf
1219  * @param buf where to copy the message
1220  * @return number of bytes copied to @a buf
1221  */
1222 static size_t
1223 send_start (void *cls,
1224             size_t size,
1225             void *buf)
1226 {
1227   struct GNUNET_TRANSPORT_Handle *h = cls;
1228   struct StartMessage s;
1229   uint32_t options;
1230
1231   if (NULL == buf)
1232   {
1233     /* Can only be shutdown, just give up */
1234     LOG (GNUNET_ERROR_TYPE_DEBUG,
1235          "Shutdown while trying to transmit START request.\n");
1236     return 0;
1237   }
1238   LOG (GNUNET_ERROR_TYPE_DEBUG,
1239        "Transmitting START request.\n");
1240   GNUNET_assert (size >= sizeof (struct StartMessage));
1241   s.header.size = htons (sizeof (struct StartMessage));
1242   s.header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_START);
1243   options = 0;
1244   if (h->check_self)
1245     options |= 1;
1246   if (NULL != h->rec)
1247     options |= 2;
1248   s.options = htonl (options);
1249   s.self = h->self;
1250   memcpy (buf, &s, sizeof (struct StartMessage));
1251   GNUNET_CLIENT_receive (h->client, &demultiplexer, h,
1252                          GNUNET_TIME_UNIT_FOREVER_REL);
1253   return sizeof (struct StartMessage);
1254 }
1255
1256
1257 /**
1258  * Try again to connect to transport service.
1259  *
1260  * @param cls the handle to the transport service
1261  * @param tc scheduler context
1262  */
1263 static void
1264 reconnect (void *cls,
1265            const struct GNUNET_SCHEDULER_TaskContext *tc)
1266 {
1267   struct GNUNET_TRANSPORT_Handle *h = cls;
1268
1269   h->reconnect_task = NULL;
1270   if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1271   {
1272     /* shutdown, just give up */
1273     return;
1274   }
1275   LOG (GNUNET_ERROR_TYPE_DEBUG,
1276        "Connecting to transport service.\n");
1277   GNUNET_assert (NULL == h->client);
1278   GNUNET_assert (NULL == h->control_head);
1279   GNUNET_assert (NULL == h->control_tail);
1280   h->reconnecting = GNUNET_NO;
1281   h->client = GNUNET_CLIENT_connect ("transport", h->cfg);
1282
1283   GNUNET_assert (NULL != h->client);
1284   schedule_control_transmit (h, sizeof (struct StartMessage),
1285                              &send_start, h);
1286 }
1287
1288
1289 /**
1290  * Function that will schedule the job that will try
1291  * to connect us again to the client.
1292  *
1293  * @param h transport service to reconnect
1294  */
1295 static void
1296 disconnect_and_schedule_reconnect (struct GNUNET_TRANSPORT_Handle *h)
1297 {
1298   struct GNUNET_TRANSPORT_TransmitHandle *th;
1299
1300   GNUNET_assert (h->reconnect_task == NULL);
1301   if (NULL != h->cth)
1302   {
1303     GNUNET_CLIENT_notify_transmit_ready_cancel (h->cth);
1304     h->cth = NULL;
1305   }
1306   if (NULL != h->client)
1307   {
1308     GNUNET_CLIENT_disconnect (h->client);
1309     h->client = NULL;
1310 /*    LOG (GNUNET_ERROR_TYPE_ERROR,
1311          "Client disconnect done \n");*/
1312   }
1313   /* Forget about all neighbours that we used to be connected to */
1314   GNUNET_CONTAINER_multipeermap_iterate (h->neighbours,
1315                                          &neighbour_delete,
1316                                          h);
1317   if (NULL != h->quota_task)
1318   {
1319     GNUNET_SCHEDULER_cancel (h->quota_task);
1320     h->quota_task = NULL;
1321   }
1322   while ((NULL != (th = h->control_head)))
1323   {
1324     GNUNET_CONTAINER_DLL_remove (h->control_head,
1325                                  h->control_tail,
1326                                  th);
1327     th->notify (th->notify_cls,
1328                 0,
1329                 NULL);
1330     GNUNET_free (th);
1331   }
1332   LOG (GNUNET_ERROR_TYPE_DEBUG,
1333        "Scheduling task to reconnect to transport service in %s.\n",
1334        GNUNET_STRINGS_relative_time_to_string (h->reconnect_delay,
1335                                                GNUNET_YES));
1336   h->reconnect_task =
1337       GNUNET_SCHEDULER_add_delayed (h->reconnect_delay,
1338                                     &reconnect,
1339                                     h);
1340   h->reconnect_delay = GNUNET_TIME_STD_BACKOFF (h->reconnect_delay);
1341 }
1342
1343
1344 /**
1345  * Cancel control request for transmission to the transport service.
1346  *
1347  * @param th handle to the transport service
1348  * @param tth transmit handle to cancel
1349  */
1350 static void
1351 cancel_control_transmit (struct GNUNET_TRANSPORT_Handle *th,
1352                          struct GNUNET_TRANSPORT_TransmitHandle *tth)
1353 {
1354   LOG (GNUNET_ERROR_TYPE_DEBUG,
1355        "Canceling transmit of contral transmission requested\n");
1356   GNUNET_CONTAINER_DLL_remove (th->control_head,
1357                                th->control_tail,
1358                                tth);
1359   GNUNET_free (tth);
1360 }
1361
1362
1363 /**
1364  * Send #GNUNET_MESSAGE_TYPE_TRANSPORT_REQUEST_CONNECT message to the
1365  * service.
1366  *
1367  * @param cls the `struct GNUNET_TRANSPORT_TryConnectHandle`
1368  * @param size number of bytes available in @a buf
1369  * @param buf where to copy the message
1370  * @return number of bytes copied to @a buf
1371  */
1372 static size_t
1373 send_try_connect (void *cls,
1374                   size_t size,
1375                   void *buf)
1376 {
1377   struct GNUNET_TRANSPORT_TryConnectHandle *tch = cls;
1378   struct TransportRequestConnectMessage msg;
1379
1380   tch->tth = NULL;
1381   if (NULL == buf)
1382   {
1383     LOG (GNUNET_ERROR_TYPE_DEBUG,
1384          "Discarding  `%s' request to `%s' due to error in transport service connection.\n",
1385          "REQUEST_CONNECT",
1386          GNUNET_i2s (&tch->pid));
1387     if (NULL != tch->cb)
1388       tch->cb (tch->cb_cls,
1389                GNUNET_SYSERR);
1390     GNUNET_TRANSPORT_try_connect_cancel (tch);
1391     return 0;
1392   }
1393   LOG (GNUNET_ERROR_TYPE_DEBUG,
1394        "Transmitting `%s' request with respect to `%s'.\n",
1395        "REQUEST_CONNECT",
1396        GNUNET_i2s (&tch->pid));
1397   GNUNET_assert (size >= sizeof (struct TransportRequestConnectMessage));
1398   msg.header.size = htons (sizeof (struct TransportRequestConnectMessage));
1399   msg.header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_REQUEST_CONNECT);
1400   msg.reserved = htonl (0);
1401   msg.peer = tch->pid;
1402   memcpy (buf, &msg, sizeof (msg));
1403   if (NULL != tch->cb)
1404     tch->cb (tch->cb_cls, GNUNET_OK);
1405   GNUNET_TRANSPORT_try_connect_cancel (tch);
1406   return sizeof (struct TransportRequestConnectMessage);
1407 }
1408
1409
1410 /**
1411  * Ask the transport service to establish a connection to
1412  * the given peer.
1413  *
1414  * @param handle connection to transport service
1415  * @param target who we should try to connect to
1416  * @param cb callback to be called when request was transmitted to transport
1417  *         service
1418  * @param cb_cls closure for the callback
1419  * @return a `struct GNUNET_TRANSPORT_TryConnectHandle` handle or
1420  *         NULL on failure (cb will not be called)
1421  */
1422 struct GNUNET_TRANSPORT_TryConnectHandle *
1423 GNUNET_TRANSPORT_try_connect (struct GNUNET_TRANSPORT_Handle *handle,
1424                               const struct GNUNET_PeerIdentity *target,
1425                               GNUNET_TRANSPORT_TryConnectCallback cb,
1426                               void *cb_cls)
1427 {
1428   struct GNUNET_TRANSPORT_TryConnectHandle *tch;
1429
1430   if (NULL == handle->client)
1431     return NULL;
1432   tch = GNUNET_new (struct GNUNET_TRANSPORT_TryConnectHandle);
1433   tch->th = handle;
1434   tch->pid = *target;
1435   tch->cb = cb;
1436   tch->cb_cls = cb_cls;
1437   tch->tth = schedule_control_transmit (handle,
1438                                         sizeof (struct TransportRequestConnectMessage),
1439                                         &send_try_connect, tch);
1440   GNUNET_CONTAINER_DLL_insert (handle->tc_head,
1441                                handle->tc_tail,
1442                                tch);
1443   return tch;
1444 }
1445
1446
1447 /**
1448  * Cancel the request to transport to try a connect
1449  * Callback will not be called
1450  *
1451  * @param tch the handle to cancel
1452  */
1453 void
1454 GNUNET_TRANSPORT_try_connect_cancel (struct GNUNET_TRANSPORT_TryConnectHandle *tch)
1455 {
1456   struct GNUNET_TRANSPORT_Handle *th;
1457
1458   th = tch->th;
1459   if (NULL != tch->tth)
1460     cancel_control_transmit (th, tch->tth);
1461   GNUNET_CONTAINER_DLL_remove (th->tc_head,
1462                                th->tc_tail,
1463                                tch);
1464   GNUNET_free (tch);
1465 }
1466
1467
1468 /**
1469  * Send #GNUNET_MESSAGE_TYPE_TRANSPORT_REQUEST_DISCONNECT message to the
1470  * service.
1471  *
1472  * @param cls the `struct GNUNET_TRANSPORT_TryDisconnectHandle`
1473  * @param size number of bytes available in @a buf
1474  * @param buf where to copy the message
1475  * @return number of bytes copied to @a buf
1476  */
1477 static size_t
1478 send_try_disconnect (void *cls,
1479                      size_t size,
1480                      void *buf)
1481 {
1482   struct GNUNET_TRANSPORT_TryDisconnectHandle *tdh = cls;
1483   struct TransportRequestConnectMessage msg;
1484
1485   tdh->th = NULL;
1486   if (NULL == buf)
1487   {
1488     LOG (GNUNET_ERROR_TYPE_DEBUG,
1489          "Discarding  `%s' request to `%s' due to error in transport service connection.\n",
1490          "REQUEST_DISCONNECT",
1491          GNUNET_i2s (&tdh->pid));
1492     if (NULL != tdh->cb)
1493       tdh->cb (tdh->cb_cls,
1494                GNUNET_SYSERR);
1495     GNUNET_TRANSPORT_try_disconnect_cancel (tdh);
1496     return 0;
1497   }
1498   LOG (GNUNET_ERROR_TYPE_DEBUG,
1499        "Transmitting `%s' request with respect to `%s'.\n",
1500        "REQUEST_DISCONNECT",
1501        GNUNET_i2s (&tdh->pid));
1502   GNUNET_assert (size >= sizeof (struct TransportRequestDisconnectMessage));
1503   msg.header.size = htons (sizeof (struct TransportRequestDisconnectMessage));
1504   msg.header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_REQUEST_DISCONNECT);
1505   msg.reserved = htonl (0);
1506   msg.peer = tdh->pid;
1507   memcpy (buf, &msg, sizeof (msg));
1508   if (NULL != tdh->cb)
1509     tdh->cb (tdh->cb_cls, GNUNET_OK);
1510   GNUNET_TRANSPORT_try_disconnect_cancel (tdh);
1511   return sizeof (struct TransportRequestDisconnectMessage);
1512 }
1513
1514
1515 /**
1516  * Ask the transport service to shutdown a connection to
1517  * the given peer.
1518  *
1519  * @param handle connection to transport service
1520  * @param target who we should try to connect to
1521  * @param cb callback to be called when request was transmitted to transport
1522  *         service
1523  * @param cb_cls closure for the callback @a cb
1524  * @return a `struct GNUNET_TRANSPORT_TryDisconnectHandle` handle or
1525  *         NULL on failure (cb will not be called)
1526  */
1527 struct GNUNET_TRANSPORT_TryDisconnectHandle *
1528 GNUNET_TRANSPORT_try_disconnect (struct GNUNET_TRANSPORT_Handle *handle,
1529                                  const struct GNUNET_PeerIdentity *target,
1530                                  GNUNET_TRANSPORT_TryDisconnectCallback cb,
1531                                  void *cb_cls)
1532 {
1533   struct GNUNET_TRANSPORT_TryDisconnectHandle *tdh;
1534
1535   if (NULL == handle->client)
1536     return NULL;
1537   tdh = GNUNET_new (struct GNUNET_TRANSPORT_TryDisconnectHandle);
1538   tdh->th = handle;
1539   tdh->pid = *target;
1540   tdh->cb = cb;
1541   tdh->cb_cls = cb_cls;
1542   tdh->tth = schedule_control_transmit (handle,
1543                                         sizeof (struct TransportRequestDisconnectMessage),
1544                                         &send_try_disconnect, tdh);
1545   GNUNET_CONTAINER_DLL_insert (handle->td_head,
1546                                handle->td_tail,
1547                                tdh);
1548   return tdh;
1549 }
1550
1551
1552 /**
1553  * Cancel the request to transport to try a disconnect
1554  * Callback will not be called
1555  *
1556  * @param tdh the handle to cancel
1557  */
1558 void
1559 GNUNET_TRANSPORT_try_disconnect_cancel (struct GNUNET_TRANSPORT_TryDisconnectHandle *tdh)
1560 {
1561   struct GNUNET_TRANSPORT_Handle *th;
1562
1563   th = tdh->th;
1564   if (NULL != tdh->tth)
1565     cancel_control_transmit (th, tdh->tth);
1566   GNUNET_CONTAINER_DLL_remove (th->td_head,
1567                                th->td_tail,
1568                                tdh);
1569   GNUNET_free (tdh);
1570 }
1571
1572
1573 /**
1574  * Send HELLO message to the service.
1575  *
1576  * @param cls the HELLO message to send
1577  * @param size number of bytes available in @a buf
1578  * @param buf where to copy the message
1579  * @return number of bytes copied to @a buf
1580  */
1581 static size_t
1582 send_hello (void *cls,
1583             size_t size,
1584             void *buf)
1585 {
1586   struct GNUNET_TRANSPORT_OfferHelloHandle *ohh = cls;
1587   struct GNUNET_MessageHeader *msg = ohh->msg;
1588   uint16_t ssize;
1589   struct GNUNET_SCHEDULER_TaskContext tc;
1590
1591   tc.read_ready = NULL;
1592   tc.write_ready = NULL;
1593   tc.reason = GNUNET_SCHEDULER_REASON_TIMEOUT;
1594   if (NULL == buf)
1595   {
1596     LOG (GNUNET_ERROR_TYPE_DEBUG,
1597          "Timeout while trying to transmit `%s' request.\n",
1598          "HELLO");
1599     if (NULL != ohh->cont)
1600       ohh->cont (ohh->cls,
1601                  &tc);
1602     GNUNET_free (msg);
1603     GNUNET_CONTAINER_DLL_remove (ohh->th->oh_head,
1604                                  ohh->th->oh_tail,
1605                                  ohh);
1606     GNUNET_free (ohh);
1607     return 0;
1608   }
1609   LOG (GNUNET_ERROR_TYPE_DEBUG,
1610        "Transmitting `%s' request.\n",
1611        "HELLO");
1612   ssize = ntohs (msg->size);
1613   GNUNET_assert (size >= ssize);
1614   memcpy (buf,
1615           msg,
1616           ssize);
1617   GNUNET_free (msg);
1618   tc.reason = GNUNET_SCHEDULER_REASON_READ_READY;
1619   if (NULL != ohh->cont)
1620     ohh->cont (ohh->cls,
1621                &tc);
1622   GNUNET_CONTAINER_DLL_remove (ohh->th->oh_head,
1623                                ohh->th->oh_tail,
1624                                ohh);
1625   GNUNET_free (ohh);
1626   return ssize;
1627 }
1628
1629
1630 /**
1631  * Send traffic metric message to the service.
1632  *
1633  * @param cls the message to send
1634  * @param size number of bytes available in @a buf
1635  * @param buf where to copy the message
1636  * @return number of bytes copied to @a buf
1637  */
1638 static size_t
1639 send_metric (void *cls,
1640              size_t size,
1641              void *buf)
1642 {
1643   struct TrafficMetricMessage *msg = cls;
1644   uint16_t ssize;
1645
1646   if (NULL == buf)
1647   {
1648     LOG (GNUNET_ERROR_TYPE_DEBUG,
1649          "Timeout while trying to transmit TRAFFIC_METRIC request.\n");
1650     GNUNET_free (msg);
1651     return 0;
1652   }
1653   LOG (GNUNET_ERROR_TYPE_DEBUG,
1654        "Transmitting TRAFFIC_METRIC request.\n");
1655   ssize = ntohs (msg->header.size);
1656   GNUNET_assert (size >= ssize);
1657   memcpy (buf, msg, ssize);
1658   GNUNET_free (msg);
1659   return ssize;
1660 }
1661
1662
1663 /**
1664  * Set transport metrics for a peer and a direction
1665  *
1666  * @param handle transport handle
1667  * @param peer the peer to set the metric for
1668  * @param prop the performance metrics to set
1669  * @param delay_in inbound delay to introduce
1670  * @param delay_out outbound delay to introduce
1671  *
1672  * Note: Delay restrictions in receiving direction will be enforced
1673  * with one message delay.
1674  */
1675 void
1676 GNUNET_TRANSPORT_set_traffic_metric (struct GNUNET_TRANSPORT_Handle *handle,
1677                                      const struct GNUNET_PeerIdentity *peer,
1678                                      const struct GNUNET_ATS_Properties *prop,
1679                                      struct GNUNET_TIME_Relative delay_in,
1680                                      struct GNUNET_TIME_Relative delay_out)
1681 {
1682   struct TrafficMetricMessage *msg;
1683
1684   msg = GNUNET_new (struct TrafficMetricMessage);
1685   msg->header.size = htons (sizeof (struct TrafficMetricMessage));
1686   msg->header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_TRAFFIC_METRIC);
1687   msg->reserved = htonl (0);
1688   msg->peer = *peer;
1689   GNUNET_ATS_properties_hton (&msg->properties,
1690                               prop);
1691   msg->delay_in = GNUNET_TIME_relative_hton (delay_in);
1692   msg->delay_out = GNUNET_TIME_relative_hton (delay_out);
1693   schedule_control_transmit (handle,
1694                              sizeof (struct TrafficMetricMessage),
1695                              &send_metric,
1696                              msg);
1697 }
1698
1699
1700 /**
1701  * Offer the transport service the HELLO of another peer.  Note that
1702  * the transport service may just ignore this message if the HELLO is
1703  * malformed or useless due to our local configuration.
1704  *
1705  * @param handle connection to transport service
1706  * @param hello the hello message
1707  * @param cont continuation to call when HELLO has been sent,
1708  *      tc reason #GNUNET_SCHEDULER_REASON_TIMEOUT for fail
1709  *      tc reasong #GNUNET_SCHEDULER_REASON_READ_READY for success
1710  * @param cls closure for continuation
1711  * @return a `struct GNUNET_TRANSPORT_OfferHelloHandle` handle or NULL on failure,
1712  *      in case of failure cont will not be called
1713  *
1714  */
1715 struct GNUNET_TRANSPORT_OfferHelloHandle *
1716 GNUNET_TRANSPORT_offer_hello (struct GNUNET_TRANSPORT_Handle *handle,
1717                               const struct GNUNET_MessageHeader *hello,
1718                               GNUNET_SCHEDULER_TaskCallback cont,
1719                               void *cls)
1720 {
1721   struct GNUNET_TRANSPORT_OfferHelloHandle *ohh;
1722   struct GNUNET_MessageHeader *msg;
1723   struct GNUNET_PeerIdentity peer;
1724   uint16_t size;
1725
1726   if (NULL == handle->client)
1727     return NULL;
1728   GNUNET_break (ntohs (hello->type) == GNUNET_MESSAGE_TYPE_HELLO);
1729   size = ntohs (hello->size);
1730   GNUNET_break (size >= sizeof (struct GNUNET_MessageHeader));
1731   if (GNUNET_OK !=
1732       GNUNET_HELLO_get_id ((const struct GNUNET_HELLO_Message *) hello,
1733                            &peer))
1734   {
1735     GNUNET_break (0);
1736     return NULL;
1737   }
1738
1739   msg = GNUNET_malloc (size);
1740   memcpy (msg, hello, size);
1741   LOG (GNUNET_ERROR_TYPE_DEBUG,
1742        "Offering HELLO message of `%s' to transport for validation.\n",
1743        GNUNET_i2s (&peer));
1744
1745   ohh = GNUNET_new (struct GNUNET_TRANSPORT_OfferHelloHandle);
1746   ohh->th = handle;
1747   ohh->cont = cont;
1748   ohh->cls = cls;
1749   ohh->msg = msg;
1750   ohh->tth = schedule_control_transmit (handle, size,
1751                                         &send_hello, ohh);
1752   GNUNET_CONTAINER_DLL_insert (handle->oh_head, handle->oh_tail, ohh);
1753   return ohh;
1754 }
1755
1756
1757 /**
1758  * Cancel the request to transport to offer the HELLO message
1759  *
1760  * @param ohh the handle for the operation to cancel
1761  */
1762 void
1763 GNUNET_TRANSPORT_offer_hello_cancel (struct GNUNET_TRANSPORT_OfferHelloHandle *ohh)
1764 {
1765   struct GNUNET_TRANSPORT_Handle *th = ohh->th;
1766
1767   cancel_control_transmit (ohh->th, ohh->tth);
1768   GNUNET_CONTAINER_DLL_remove (th->oh_head,
1769                                th->oh_tail,
1770                                ohh);
1771   GNUNET_free (ohh->msg);
1772   GNUNET_free (ohh);
1773 }
1774
1775
1776 /**
1777  * Checks if a given peer is connected to us
1778  *
1779  * @param handle connection to transport service
1780  * @param peer the peer to check
1781  * @return #GNUNET_YES (connected) or #GNUNET_NO (disconnected)
1782  */
1783 int
1784 GNUNET_TRANSPORT_check_peer_connected (struct GNUNET_TRANSPORT_Handle *handle,
1785                                        const struct GNUNET_PeerIdentity *peer)
1786 {
1787   if (GNUNET_YES ==
1788       GNUNET_CONTAINER_multipeermap_contains (handle->neighbours,
1789                                               peer))
1790     return GNUNET_YES;
1791   return GNUNET_NO;
1792 }
1793
1794
1795 /**
1796  * Task to call the HelloUpdateCallback of the GetHelloHandle
1797  *
1798  * @param cls the `struct GNUNET_TRANSPORT_GetHelloHandle`
1799  * @param tc the scheduler task context
1800  */
1801 static void
1802 call_hello_update_cb_async (void *cls,
1803                             const struct GNUNET_SCHEDULER_TaskContext *tc)
1804 {
1805   struct GNUNET_TRANSPORT_GetHelloHandle *ghh = cls;
1806
1807   GNUNET_assert (NULL != ghh->handle->my_hello);
1808   GNUNET_assert (NULL != ghh->notify_task);
1809   ghh->notify_task = NULL;
1810   ghh->rec (ghh->rec_cls,
1811             ghh->handle->my_hello);
1812 }
1813
1814
1815 /**
1816  * Obtain the HELLO message for this peer.  The callback given in this function
1817  * is never called synchronously.
1818  *
1819  * @param handle connection to transport service
1820  * @param rec function to call with the HELLO, sender will be our peer
1821  *            identity; message and sender will be NULL on timeout
1822  *            (handshake with transport service pending/failed).
1823  *             cost estimate will be 0.
1824  * @param rec_cls closure for @a rec
1825  * @return handle to cancel the operation
1826  */
1827 struct GNUNET_TRANSPORT_GetHelloHandle *
1828 GNUNET_TRANSPORT_get_hello (struct GNUNET_TRANSPORT_Handle *handle,
1829                             GNUNET_TRANSPORT_HelloUpdateCallback rec,
1830                             void *rec_cls)
1831 {
1832   struct GNUNET_TRANSPORT_GetHelloHandle *hwl;
1833
1834   hwl = GNUNET_new (struct GNUNET_TRANSPORT_GetHelloHandle);
1835   hwl->rec = rec;
1836   hwl->rec_cls = rec_cls;
1837   hwl->handle = handle;
1838   GNUNET_CONTAINER_DLL_insert (handle->hwl_head,
1839                                handle->hwl_tail,
1840                                hwl);
1841   if (NULL != handle->my_hello)
1842     hwl->notify_task = GNUNET_SCHEDULER_add_now (&call_hello_update_cb_async,
1843                                                  hwl);
1844   return hwl;
1845 }
1846
1847
1848 /**
1849  * Stop receiving updates about changes to our HELLO message.
1850  *
1851  * @param ghh handle to cancel
1852  */
1853 void
1854 GNUNET_TRANSPORT_get_hello_cancel (struct GNUNET_TRANSPORT_GetHelloHandle *ghh)
1855 {
1856   struct GNUNET_TRANSPORT_Handle *handle = ghh->handle;
1857
1858   if (NULL != ghh->notify_task)
1859     GNUNET_SCHEDULER_cancel (ghh->notify_task);
1860   GNUNET_CONTAINER_DLL_remove (handle->hwl_head, handle->hwl_tail, ghh);
1861   GNUNET_free (ghh);
1862 }
1863
1864
1865 /**
1866  * Connect to the transport service.  Note that the connection may
1867  * complete (or fail) asynchronously.
1868  *
1869  * @param cfg configuration to use
1870  * @param self our own identity (API should check that it matches
1871  *             the identity found by transport), or NULL (no check)
1872  * @param cls closure for the callbacks
1873  * @param rec receive function to call
1874  * @param nc function to call on connect events
1875  * @param nd function to call on disconnect events
1876  * @return NULL on error
1877  */
1878 struct GNUNET_TRANSPORT_Handle *
1879 GNUNET_TRANSPORT_connect (const struct GNUNET_CONFIGURATION_Handle *cfg,
1880                           const struct GNUNET_PeerIdentity *self,
1881                           void *cls,
1882                           GNUNET_TRANSPORT_ReceiveCallback rec,
1883                           GNUNET_TRANSPORT_NotifyConnect nc,
1884                           GNUNET_TRANSPORT_NotifyDisconnect nd)
1885 {
1886   return GNUNET_TRANSPORT_connect2 (cfg,
1887                                     self,
1888                                     cls,
1889                                     rec,
1890                                     nc,
1891                                     nd,
1892                                     NULL);
1893 }
1894
1895
1896 /**
1897  * Connect to the transport service.  Note that the connection may
1898  * complete (or fail) asynchronously.
1899  *
1900  * @param cfg configuration to use
1901  * @param self our own identity (API should check that it matches
1902  *             the identity found by transport), or NULL (no check)
1903  * @param cls closure for the callbacks
1904  * @param rec receive function to call
1905  * @param nc function to call on connect events
1906  * @param nd function to call on disconnect events
1907  * @param neb function to call if we have excess bandwidth to a peer
1908  * @return NULL on error
1909  */
1910 struct GNUNET_TRANSPORT_Handle *
1911 GNUNET_TRANSPORT_connect2 (const struct GNUNET_CONFIGURATION_Handle *cfg,
1912                            const struct GNUNET_PeerIdentity *self,
1913                            void *cls,
1914                            GNUNET_TRANSPORT_ReceiveCallback rec,
1915                            GNUNET_TRANSPORT_NotifyConnect nc,
1916                            GNUNET_TRANSPORT_NotifyDisconnect nd,
1917                            GNUNET_TRANSPORT_NotifyExcessBandwidth neb)
1918 {
1919   struct GNUNET_TRANSPORT_Handle *ret;
1920
1921   ret = GNUNET_new (struct GNUNET_TRANSPORT_Handle);
1922   if (NULL != self)
1923   {
1924     ret->self = *self;
1925     ret->check_self = GNUNET_YES;
1926   }
1927   ret->cfg = cfg;
1928   ret->cls = cls;
1929   ret->rec = rec;
1930   ret->nc_cb = nc;
1931   ret->nd_cb = nd;
1932   ret->neb_cb = neb;
1933   ret->reconnect_delay = GNUNET_TIME_UNIT_ZERO;
1934   LOG (GNUNET_ERROR_TYPE_DEBUG,
1935        "Connecting to transport service.\n");
1936   ret->client = GNUNET_CLIENT_connect ("transport",
1937                                        cfg);
1938   if (NULL == ret->client)
1939   {
1940     GNUNET_free (ret);
1941     return NULL;
1942   }
1943   ret->neighbours =
1944     GNUNET_CONTAINER_multipeermap_create (STARTING_NEIGHBOURS_SIZE,
1945                                           GNUNET_YES);
1946   ret->ready_heap =
1947       GNUNET_CONTAINER_heap_create (GNUNET_CONTAINER_HEAP_ORDER_MIN);
1948   schedule_control_transmit (ret,
1949                              sizeof (struct StartMessage),
1950                              &send_start,
1951                              ret);
1952   return ret;
1953 }
1954
1955
1956 /**
1957  * Disconnect from the transport service.
1958  *
1959  * @param handle handle to the service as returned from #GNUNET_TRANSPORT_connect()
1960  */
1961 void
1962 GNUNET_TRANSPORT_disconnect (struct GNUNET_TRANSPORT_Handle *handle)
1963 {
1964   LOG (GNUNET_ERROR_TYPE_DEBUG,
1965        "Transport disconnect called!\n");
1966   /* this disconnects all neighbours... */
1967   if (NULL == handle->reconnect_task)
1968     disconnect_and_schedule_reconnect (handle);
1969   /* and now we stop trying to connect again... */
1970   if (NULL != handle->reconnect_task)
1971   {
1972     GNUNET_SCHEDULER_cancel (handle->reconnect_task);
1973     handle->reconnect_task = NULL;
1974   }
1975   GNUNET_CONTAINER_multipeermap_destroy (handle->neighbours);
1976   handle->neighbours = NULL;
1977   if (NULL != handle->quota_task)
1978   {
1979     GNUNET_SCHEDULER_cancel (handle->quota_task);
1980     handle->quota_task = NULL;
1981   }
1982   GNUNET_free_non_null (handle->my_hello);
1983   handle->my_hello = NULL;
1984   GNUNET_assert (NULL == handle->tc_head);
1985   GNUNET_assert (NULL == handle->tc_tail);
1986   GNUNET_assert (NULL == handle->hwl_head);
1987   GNUNET_assert (NULL == handle->hwl_tail);
1988   GNUNET_CONTAINER_heap_destroy (handle->ready_heap);
1989   handle->ready_heap = NULL;
1990   GNUNET_free (handle);
1991 }
1992
1993
1994 /**
1995  * Check if we could queue a message of the given size for
1996  * transmission.  The transport service will take both its
1997  * internal buffers and bandwidth limits imposed by the
1998  * other peer into consideration when answering this query.
1999  *
2000  * @param handle connection to transport service
2001  * @param target who should receive the message
2002  * @param size how big is the message we want to transmit?
2003  * @param timeout after how long should we give up (and call
2004  *        notify with buf NULL and size 0)?
2005  * @param notify function to call when we are ready to
2006  *        send such a message
2007  * @param notify_cls closure for @a notify
2008  * @return NULL if someone else is already waiting to be notified
2009  *         non-NULL if the notify callback was queued (can be used to cancel
2010  *         using #GNUNET_TRANSPORT_notify_transmit_ready_cancel)
2011  */
2012 struct GNUNET_TRANSPORT_TransmitHandle *
2013 GNUNET_TRANSPORT_notify_transmit_ready (struct GNUNET_TRANSPORT_Handle *handle,
2014                                         const struct GNUNET_PeerIdentity *target,
2015                                         size_t size,
2016                                         struct GNUNET_TIME_Relative timeout,
2017                                         GNUNET_TRANSPORT_TransmitReadyNotify notify,
2018                                         void *notify_cls)
2019 {
2020   struct Neighbour *n;
2021   struct GNUNET_TRANSPORT_TransmitHandle *th;
2022   struct GNUNET_TIME_Relative delay;
2023
2024   n = neighbour_find (handle, target);
2025   if (NULL == n)
2026   {
2027     /* use GNUNET_TRANSPORT_try_connect first, only use this function
2028      * once a connection has been established */
2029     GNUNET_assert (0);
2030     return NULL;
2031   }
2032   if (NULL != n->th)
2033   {
2034     /* attempt to send two messages at the same time to the same peer */
2035     GNUNET_assert (0);
2036     return NULL;
2037   }
2038   GNUNET_assert (NULL == n->hn);
2039   th = GNUNET_new (struct GNUNET_TRANSPORT_TransmitHandle);
2040   th->neighbour = n;
2041   th->notify = notify;
2042   th->notify_cls = notify_cls;
2043   th->request_start = GNUNET_TIME_absolute_get ();
2044   th->timeout = GNUNET_TIME_relative_to_absolute (timeout);
2045   th->notify_size = size;
2046   n->th = th;
2047   /* calculate when our transmission should be ready */
2048   delay = GNUNET_BANDWIDTH_tracker_get_delay (&n->out_tracker,
2049                                               size + n->traffic_overhead);
2050   n->traffic_overhead = 0;
2051   if (delay.rel_value_us > timeout.rel_value_us)
2052     delay.rel_value_us = 0;        /* notify immediately (with failure) */
2053   if (delay.rel_value_us > GNUNET_TIME_UNIT_SECONDS.rel_value_us)
2054     LOG (GNUNET_ERROR_TYPE_WARNING,
2055          "At bandwidth %u byte/s next transmission to %s in %s\n",
2056          (unsigned int) n->out_tracker.available_bytes_per_s__,
2057          GNUNET_i2s (target),
2058          GNUNET_STRINGS_relative_time_to_string (delay,
2059                                                  GNUNET_YES));
2060   else
2061     LOG (GNUNET_ERROR_TYPE_DEBUG,
2062          "At bandwidth %u byte/s next transmission to %s in %s\n",
2063          (unsigned int) n->out_tracker.available_bytes_per_s__,
2064          GNUNET_i2s (target),
2065          GNUNET_STRINGS_relative_time_to_string (delay,
2066                                                  GNUNET_YES));
2067   n->hn = GNUNET_CONTAINER_heap_insert (handle->ready_heap,
2068                                         n,
2069                                         delay.rel_value_us);
2070   schedule_transmission (handle);
2071   return th;
2072 }
2073
2074
2075 /**
2076  * Cancel the specified transmission-ready notification.
2077  *
2078  * @param th handle returned from #GNUNET_TRANSPORT_notify_transmit_ready()
2079  */
2080 void
2081 GNUNET_TRANSPORT_notify_transmit_ready_cancel (struct GNUNET_TRANSPORT_TransmitHandle *th)
2082 {
2083   struct Neighbour *n;
2084
2085   GNUNET_assert (NULL == th->next);
2086   GNUNET_assert (NULL == th->prev);
2087   n = th->neighbour;
2088   GNUNET_assert (th == n->th);
2089   n->th = NULL;
2090   if (NULL != n->hn)
2091   {
2092     GNUNET_CONTAINER_heap_remove_node (n->hn);
2093     n->hn = NULL;
2094   }
2095   else
2096   {
2097     GNUNET_assert (NULL != th->timeout_task);
2098     GNUNET_SCHEDULER_cancel (th->timeout_task);
2099     th->timeout_task = NULL;
2100   }
2101   GNUNET_free (th);
2102 }
2103
2104
2105 /* end of transport_api.c */