receive set_quota messages
[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 #if DEBUG_TRANSPORT_API
346   LOG (GNUNET_ERROR_TYPE_DEBUG, "Creating entry for neighbour `%4s'.\n",
347        GNUNET_i2s (pid));
348 #endif
349   n = GNUNET_malloc (sizeof (struct Neighbour));
350   n->id = *pid;
351   n->h = h;
352   n->is_ready = GNUNET_YES;
353   GNUNET_BANDWIDTH_tracker_init (&n->out_tracker,
354                                  GNUNET_CONSTANTS_DEFAULT_BW_IN_OUT,
355                                  MAX_BANDWIDTH_CARRY_S);
356   GNUNET_assert (GNUNET_OK ==
357                  GNUNET_CONTAINER_multihashmap_put (h->neighbours,
358                                                     &pid->hashPubKey, n,
359                                                     GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
360   return n;
361 }
362
363
364 /**
365  * Iterator over hash map entries, for deleting state of a neighbour.
366  *
367  * @param cls the 'struct GNUNET_TRANSPORT_Handle*'
368  * @param key peer identity
369  * @param value value in the hash map, the neighbour entry to delete
370  * @return GNUNET_YES if we should continue to
371  *         iterate,
372  *         GNUNET_NO if not.
373  */
374 static int
375 neighbour_delete (void *cls, const GNUNET_HashCode * key, void *value)
376 {
377   struct GNUNET_TRANSPORT_Handle *handle = cls;
378   struct Neighbour *n = value;
379
380   if (NULL != handle->nd_cb)
381     handle->nd_cb (handle->cls, &n->id);
382   GNUNET_assert (NULL == n->th);
383   GNUNET_assert (NULL == n->hn);
384   GNUNET_assert (GNUNET_YES ==
385                  GNUNET_CONTAINER_multihashmap_remove (handle->neighbours, key,
386                                                        n));
387   GNUNET_free (n);
388   return GNUNET_YES;
389 }
390
391
392 /**
393  * Function we use for handling incoming messages.
394  *
395  * @param cls closure (struct GNUNET_TRANSPORT_Handle *)
396  * @param msg message received, NULL on timeout or fatal error
397  */
398 static void
399 demultiplexer (void *cls, const struct GNUNET_MessageHeader *msg)
400 {
401   struct GNUNET_TRANSPORT_Handle *h = cls;
402   const struct DisconnectInfoMessage *dim;
403   const struct ConnectInfoMessage *cim;
404   const struct InboundMessage *im;
405   const struct GNUNET_MessageHeader *imm;
406   const struct SendOkMessage *okm;
407   const struct QuotaSetMessage *qm;
408   const struct GNUNET_ATS_Information *ats;
409   struct GNUNET_TRANSPORT_GetHelloHandle *hwl;
410   struct GNUNET_TRANSPORT_GetHelloHandle *next_hwl;
411   struct Neighbour *n;
412   struct GNUNET_PeerIdentity me;
413   uint16_t size;
414   uint32_t ats_count;
415
416   GNUNET_assert (h->client != NULL);
417   if (msg == NULL)
418   {
419 #if DEBUG_TRANSPORT_API
420     LOG (GNUNET_ERROR_TYPE_INFO,
421          "Error receiving from transport service, disconnecting temporarily.\n");
422 #endif
423     disconnect_and_schedule_reconnect (h);
424     return;
425   }
426   GNUNET_CLIENT_receive (h->client, &demultiplexer, h,
427                          GNUNET_TIME_UNIT_FOREVER_REL);
428   size = ntohs (msg->size);
429   switch (ntohs (msg->type))
430   {
431   case GNUNET_MESSAGE_TYPE_HELLO:
432     if (GNUNET_OK !=
433         GNUNET_HELLO_get_id ((const struct GNUNET_HELLO_Message *) msg, &me))
434     {
435       GNUNET_break (0);
436       break;
437     }
438 #if DEBUG_TRANSPORT_API
439     LOG (GNUNET_ERROR_TYPE_DEBUG,
440          "Receiving (my own) `%s' message, I am `%4s'.\n", "HELLO",
441          GNUNET_i2s (&me));
442 #endif
443     GNUNET_free_non_null (h->my_hello);
444     h->my_hello = NULL;
445     if (size < sizeof (struct GNUNET_MessageHeader))
446     {
447       GNUNET_break (0);
448       break;
449     }
450     h->my_hello = GNUNET_malloc (size);
451     memcpy (h->my_hello, msg, size);
452     hwl = h->hwl_head;
453     while (NULL != hwl)
454     {
455       next_hwl = hwl->next;
456       hwl->rec (hwl->rec_cls,
457                 (const struct GNUNET_MessageHeader *) h->my_hello);
458       hwl = next_hwl;
459     }
460     break;
461   case GNUNET_MESSAGE_TYPE_TRANSPORT_CONNECT:
462     if (size < sizeof (struct ConnectInfoMessage))
463     {
464       GNUNET_break (0);
465       break;
466     }
467     cim = (const struct ConnectInfoMessage *) msg;
468     ats_count = ntohl (cim->ats_count);
469     if (size !=
470         sizeof (struct ConnectInfoMessage) +
471         ats_count * sizeof (struct GNUNET_ATS_Information))
472     {
473       GNUNET_break (0);
474       break;
475     }
476     ats = (const struct GNUNET_ATS_Information *) &cim[1];
477 #if DEBUG_TRANSPORT_API
478     LOG (GNUNET_ERROR_TYPE_DEBUG, "Receiving `%s' message for `%4s'.\n",
479          "CONNECT", GNUNET_i2s (&cim->id));
480 #endif
481     n = neighbour_find (h, &cim->id);
482     if (n != NULL)
483     {
484       GNUNET_break (0);
485       break;
486     }
487     n = neighbour_add (h, &cim->id);
488     if (h->nc_cb != NULL)
489       h->nc_cb (h->cls, &n->id, ats, ats_count);
490     break;
491   case GNUNET_MESSAGE_TYPE_TRANSPORT_DISCONNECT:
492     if (size != sizeof (struct DisconnectInfoMessage))
493     {
494       GNUNET_break (0);
495       break;
496     }
497     dim = (const struct DisconnectInfoMessage *) msg;
498     GNUNET_break (ntohl (dim->reserved) == 0);
499 #if DEBUG_TRANSPORT_API_DISCONNECT
500     LOG (GNUNET_ERROR_TYPE_DEBUG, "Receiving `%s' message for `%4s'.\n",
501          "DISCONNECT", GNUNET_i2s (&dim->peer));
502 #endif
503     n = neighbour_find (h, &dim->peer);
504     if (n == NULL)
505     {
506       GNUNET_break (0);
507       break;
508     }
509     neighbour_delete (h, &dim->peer.hashPubKey, n);
510     break;
511   case GNUNET_MESSAGE_TYPE_TRANSPORT_SEND_OK:
512     if (size != sizeof (struct SendOkMessage))
513     {
514       GNUNET_break (0);
515       break;
516     }
517     okm = (const struct SendOkMessage *) msg;
518 #if DEBUG_TRANSPORT_API
519     LOG (GNUNET_ERROR_TYPE_DEBUG, "Receiving `%s' message, transmission %s.\n",
520          "SEND_OK", ntohl (okm->success) == GNUNET_OK ? "succeeded" : "failed");
521 #endif
522     n = neighbour_find (h, &okm->peer);
523     if (n == NULL)
524       break;
525     GNUNET_break (GNUNET_NO == n->is_ready);
526     n->is_ready = GNUNET_YES;
527     if ((n->th != NULL) && (n->hn == NULL))
528     {
529       GNUNET_assert (GNUNET_SCHEDULER_NO_TASK != n->th->timeout_task);
530       GNUNET_SCHEDULER_cancel (n->th->timeout_task);
531       n->th->timeout_task = GNUNET_SCHEDULER_NO_TASK;
532       /* we've been waiting for this (congestion, not quota,
533        * caused delayed transmission) */
534       n->hn = GNUNET_CONTAINER_heap_insert (h->ready_heap, n, 0);
535       schedule_transmission (h);
536     }
537     break;
538   case GNUNET_MESSAGE_TYPE_TRANSPORT_RECV:
539 #if DEBUG_TRANSPORT_API
540     LOG (GNUNET_ERROR_TYPE_DEBUG, "Receiving `%s' message.\n", "RECV");
541 #endif
542     if (size <
543         sizeof (struct InboundMessage) + sizeof (struct GNUNET_MessageHeader))
544     {
545       GNUNET_break (0);
546       break;
547     }
548     im = (const struct InboundMessage *) msg;
549     ats_count = ntohl (im->ats_count);
550     ats = (const struct GNUNET_ATS_Information *) &im[1];
551     imm = (const struct GNUNET_MessageHeader *) &ats[ats_count];
552     if (ntohs (imm->size) + sizeof (struct InboundMessage) +
553         ats_count * sizeof (struct GNUNET_ATS_Information) != size)
554     {
555       GNUNET_break (0);
556       break;
557     }
558 #if DEBUG_TRANSPORT_API
559     LOG (GNUNET_ERROR_TYPE_DEBUG, "Received message of type %u from `%4s'.\n",
560          ntohs (imm->type), GNUNET_i2s (&im->peer));
561 #endif
562     n = neighbour_find (h, &im->peer);
563     if (n == NULL)
564     {
565       GNUNET_break (0);
566       break;
567     }
568     if (h->rec != NULL)
569       h->rec (h->cls, &im->peer, imm, ats, ats_count);
570     break;
571   case GNUNET_MESSAGE_TYPE_TRANSPORT_SET_QUOTA:
572 #if DEBUG_TRANSPORT_API
573     LOG (GNUNET_ERROR_TYPE_DEBUG, "Receiving `%s' message.\n", "SET_QUOTA");
574 #endif
575     if (size != sizeof (struct QuotaSetMessage))
576     {
577       GNUNET_break (0);
578       break;
579     }
580     qm = (const struct QuotaSetMessage *) msg;
581     n = neighbour_find (h, &qm->peer);
582     if (n == NULL)
583       break;
584     LOG (GNUNET_ERROR_TYPE_ERROR, "Received `%s' message, setting outbound quoto to %u\n",
585         "SET_QUOTA",
586         ntohl(qm->quota.value__));
587     GNUNET_BANDWIDTH_tracker_update_quota (&n->out_tracker, qm->quota);
588     break;
589   default:
590     LOG (GNUNET_ERROR_TYPE_ERROR,
591          _("Received unexpected message of type %u in %s:%u\n"),
592          ntohs (msg->type), __FILE__, __LINE__);
593     GNUNET_break (0);
594     break;
595   }
596 }
597
598
599 /**
600  * A transmission request could not be satisfied because of
601  * network congestion.  Notify the initiator and clean up.
602  *
603  * @param cls the 'struct GNUNET_TRANSPORT_TransmitHandle'
604  * @param tc scheduler context
605  */
606 static void
607 timeout_request_due_to_congestion (void *cls,
608                                    const struct GNUNET_SCHEDULER_TaskContext
609                                    *tc)
610 {
611   struct GNUNET_TRANSPORT_TransmitHandle *th = cls;
612   struct Neighbour *n = th->neighbour;
613
614   n->th->timeout_task = GNUNET_SCHEDULER_NO_TASK;
615   GNUNET_assert (th == n->th);
616   GNUNET_assert (NULL == n->hn);
617   n->th = NULL;
618   th->notify (th->notify_cls, 0, NULL);
619   GNUNET_free (th);
620 }
621
622
623 /**
624  * Transmit message(s) to service.
625  *
626  * @param cls handle to transport
627  * @param size number of bytes available in buf
628  * @param buf where to copy the message
629  * @return number of bytes copied to buf
630  */
631 static size_t
632 transport_notify_ready (void *cls, size_t size, void *buf)
633 {
634   struct GNUNET_TRANSPORT_Handle *h = cls;
635   struct GNUNET_TRANSPORT_TransmitHandle *th;
636   struct Neighbour *n;
637   char *cbuf;
638   struct OutboundMessage obm;
639   size_t ret;
640   size_t nret;
641   size_t mret;
642
643   GNUNET_assert (NULL != h->client);
644   h->cth = NULL;
645   if (NULL == buf)
646   {
647     /* transmission failed */
648     disconnect_and_schedule_reconnect (h);
649     return 0;
650   }
651
652   cbuf = buf;
653   ret = 0;
654   /* first send control messages */
655   while ((NULL != (th = h->control_head)) && (th->notify_size <= size))
656   {
657     GNUNET_CONTAINER_DLL_remove (h->control_head, h->control_tail, th);
658     nret = th->notify (th->notify_cls, size, &cbuf[ret]);
659 #if DEBUG_TRANSPORT_API
660     LOG (GNUNET_ERROR_TYPE_DEBUG, "Added %u bytes of control message at %u\n",
661          nret, ret);
662 #endif
663     GNUNET_free (th);
664     ret += nret;
665     size -= nret;
666   }
667
668   /* then, if possible and no control messages pending, send data messages */
669   while ((NULL == h->control_head) &&
670          (NULL != (n = GNUNET_CONTAINER_heap_peek (h->ready_heap))))
671   {
672     if (GNUNET_YES != n->is_ready)
673     {
674       /* peer not ready, wait for notification! */
675       GNUNET_assert (n == GNUNET_CONTAINER_heap_remove_root (h->ready_heap));
676       n->hn = NULL;
677       GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == n->th->timeout_task);
678       n->th->timeout_task =
679           GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_absolute_get_remaining
680                                         (n->th->timeout),
681                                         &timeout_request_due_to_congestion,
682                                         n->th);
683       continue;
684     }
685     th = n->th;
686     if (th->notify_size + sizeof (struct OutboundMessage) > size)
687       break;                    /* does not fit */
688     if (GNUNET_BANDWIDTH_tracker_get_delay
689         (&n->out_tracker, th->notify_size).rel_value > 0)
690       break;                    /* too early */
691     GNUNET_assert (n == GNUNET_CONTAINER_heap_remove_root (h->ready_heap));
692     n->hn = NULL;
693     n->th = NULL;
694     n->is_ready = GNUNET_NO;
695     GNUNET_assert (size >= sizeof (struct OutboundMessage));
696     mret =
697         th->notify (th->notify_cls, size - sizeof (struct OutboundMessage),
698                     &cbuf[ret + sizeof (struct OutboundMessage)]);
699     GNUNET_assert (mret <= size - sizeof (struct OutboundMessage));
700     if (mret != 0)
701     {
702       GNUNET_assert (mret + sizeof (struct OutboundMessage) <
703                      GNUNET_SERVER_MAX_MESSAGE_SIZE);
704       obm.header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_SEND);
705       obm.header.size = htons (mret + sizeof (struct OutboundMessage));
706       obm.priority = htonl (th->priority);
707       obm.timeout =
708           GNUNET_TIME_relative_hton (GNUNET_TIME_absolute_get_remaining
709                                      (th->timeout));
710       obm.peer = n->id;
711       memcpy (&cbuf[ret], &obm, sizeof (struct OutboundMessage));
712       ret += (mret + sizeof (struct OutboundMessage));
713       size -= (mret + sizeof (struct OutboundMessage));
714       GNUNET_BANDWIDTH_tracker_consume (&n->out_tracker, mret);
715     }
716     GNUNET_free (th);
717   }
718   /* if there are more pending messages, try to schedule those */
719   schedule_transmission (h);
720 #if DEBUG_TRANSPORT_API
721   LOG (GNUNET_ERROR_TYPE_DEBUG, "Transmitting %u bytes to transport service\n",
722        ret);
723 #endif
724   return ret;
725 }
726
727
728 /**
729  * Schedule the task to send one message, either from the control
730  * list or the peer message queues  to the service.
731  *
732  * @param cls transport service to schedule a transmission for
733  * @param tc scheduler context
734  */
735 static void
736 schedule_transmission_task (void *cls,
737                             const struct GNUNET_SCHEDULER_TaskContext *tc)
738 {
739   struct GNUNET_TRANSPORT_Handle *h = cls;
740   size_t size;
741   struct GNUNET_TRANSPORT_TransmitHandle *th;
742   struct Neighbour *n;
743
744   h->quota_task = GNUNET_SCHEDULER_NO_TASK;
745   GNUNET_assert (NULL != h->client);
746   /* destroy all requests that have timed out */
747   while ((NULL != (n = GNUNET_CONTAINER_heap_peek (h->ready_heap))) &&
748          (GNUNET_TIME_absolute_get_remaining (n->th->timeout).rel_value == 0))
749   {
750     /* notify client that the request could not be satisfied within
751      * the given time constraints */
752     th = n->th;
753     n->th = NULL;
754     GNUNET_assert (n == GNUNET_CONTAINER_heap_remove_root (h->ready_heap));
755     n->hn = NULL;
756 #if DEBUG_TRANSPORT_API
757     LOG (GNUNET_ERROR_TYPE_DEBUG,
758          "Signalling timeout for transmission to peer %s due to congestion\n",
759          GNUNET_i2s (&n->id));
760 #endif
761     GNUNET_assert (0 == th->notify (th->notify_cls, 0, NULL));
762     GNUNET_free (th);
763   }
764   if (NULL != h->cth)
765     return;
766   if (NULL != h->control_head)
767   {
768     size = h->control_head->notify_size;
769   }
770   else
771   {
772     n = GNUNET_CONTAINER_heap_peek (h->ready_heap);
773     if (NULL == n)
774       return;                   /* no pending messages */
775     size = n->th->notify_size + sizeof (struct OutboundMessage);
776   }
777 #if DEBUG_TRANSPORT_API
778   LOG (GNUNET_ERROR_TYPE_DEBUG, "Calling notify_transmit_ready\n");
779 #endif
780   h->cth =
781       GNUNET_CLIENT_notify_transmit_ready (h->client, size,
782                                            GNUNET_TIME_UNIT_FOREVER_REL,
783                                            GNUNET_NO, &transport_notify_ready,
784                                            h);
785   GNUNET_assert (NULL != h->cth);
786 }
787
788
789 /**
790  * Schedule the task to send one message, either from the control
791  * list or the peer message queues  to the service.
792  *
793  * @param h transport service to schedule a transmission for
794  */
795 static void
796 schedule_transmission (struct GNUNET_TRANSPORT_Handle *h)
797 {
798   struct GNUNET_TIME_Relative delay;
799   struct Neighbour *n;
800
801   GNUNET_assert (NULL != h->client);
802   if (h->quota_task != GNUNET_SCHEDULER_NO_TASK)
803   {
804     GNUNET_SCHEDULER_cancel (h->quota_task);
805     h->quota_task = GNUNET_SCHEDULER_NO_TASK;
806   }
807   if (NULL != h->control_head)
808     delay = GNUNET_TIME_UNIT_ZERO;
809   else if (NULL != (n = GNUNET_CONTAINER_heap_peek (h->ready_heap)))
810     delay =
811         GNUNET_BANDWIDTH_tracker_get_delay (&n->out_tracker,
812                                             n->th->notify_size);
813   else
814     return;                     /* no work to be done */
815 #if DEBUG_TRANSPORT_API
816   LOG (GNUNET_ERROR_TYPE_DEBUG,
817        "Scheduling next transmission to service in %llu ms\n",
818        (unsigned long long) delay.rel_value);
819 #endif
820   h->quota_task =
821       GNUNET_SCHEDULER_add_delayed (delay, &schedule_transmission_task, h);
822 }
823
824
825 /**
826  * Queue control request for transmission to the transport
827  * service.
828  *
829  * @param h handle to the transport service
830  * @param size number of bytes to be transmitted
831  * @param notify function to call to get the content
832  * @param notify_cls closure for notify
833  */
834 static void
835 schedule_control_transmit (struct GNUNET_TRANSPORT_Handle *h, size_t size,
836                            GNUNET_CONNECTION_TransmitReadyNotify notify,
837                            void *notify_cls)
838 {
839   struct GNUNET_TRANSPORT_TransmitHandle *th;
840
841 #if DEBUG_TRANSPORT_API
842   LOG (GNUNET_ERROR_TYPE_DEBUG, "Control transmit of %u bytes requested\n",
843        size);
844 #endif
845   th = GNUNET_malloc (sizeof (struct GNUNET_TRANSPORT_TransmitHandle));
846   th->notify = notify;
847   th->notify_cls = notify_cls;
848   th->notify_size = size;
849   GNUNET_CONTAINER_DLL_insert_tail (h->control_head, h->control_tail, th);
850   schedule_transmission (h);
851 }
852
853
854 /**
855  * Transmit START message to service.
856  *
857  * @param cls unused
858  * @param size number of bytes available in buf
859  * @param buf where to copy the message
860  * @return number of bytes copied to buf
861  */
862 static size_t
863 send_start (void *cls, size_t size, void *buf)
864 {
865   struct GNUNET_TRANSPORT_Handle *h = cls;
866   struct StartMessage s;
867   uint32_t options;
868
869   if (buf == NULL)
870   {
871     /* Can only be shutdown, just give up */
872 #if DEBUG_TRANSPORT_API
873     LOG (GNUNET_ERROR_TYPE_DEBUG,
874          "Shutdown while trying to transmit `%s' request.\n", "START");
875 #endif
876     return 0;
877   }
878 #if DEBUG_TRANSPORT_API
879   LOG (GNUNET_ERROR_TYPE_DEBUG, "Transmitting `%s' request.\n", "START");
880 #endif
881   GNUNET_assert (size >= sizeof (struct StartMessage));
882   s.header.size = htons (sizeof (struct StartMessage));
883   s.header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_START);
884   options = 0;
885   if (h->check_self)
886     options |= 1;
887   if (h->rec != NULL)
888     options |= 2;
889   s.options = htonl (options);
890   s.self = h->self;
891   memcpy (buf, &s, sizeof (struct StartMessage));
892   GNUNET_CLIENT_receive (h->client, &demultiplexer, h,
893                          GNUNET_TIME_UNIT_FOREVER_REL);
894   return sizeof (struct StartMessage);
895 }
896
897
898 /**
899  * Try again to connect to transport service.
900  *
901  * @param cls the handle to the transport service
902  * @param tc scheduler context
903  */
904 static void
905 reconnect (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
906 {
907   struct GNUNET_TRANSPORT_Handle *h = cls;
908
909   h->reconnect_task = GNUNET_SCHEDULER_NO_TASK;
910   if ((tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) != 0)
911   {
912     /* shutdown, just give up */
913     return;
914   }
915 #if DEBUG_TRANSPORT_API
916   LOG (GNUNET_ERROR_TYPE_DEBUG, "Connecting to transport service.\n");
917 #endif
918   GNUNET_assert (h->client == NULL);
919   GNUNET_assert (h->control_head == NULL);
920   GNUNET_assert (h->control_tail == NULL);
921   h->client = GNUNET_CLIENT_connect ("transport", h->cfg);
922   GNUNET_assert (h->client != NULL);
923   schedule_control_transmit (h, sizeof (struct StartMessage), &send_start, h);
924 }
925
926
927 /**
928  * Function that will schedule the job that will try
929  * to connect us again to the client.
930  *
931  * @param h transport service to reconnect
932  */
933 static void
934 disconnect_and_schedule_reconnect (struct GNUNET_TRANSPORT_Handle *h)
935 {
936   struct GNUNET_TRANSPORT_TransmitHandle *th;
937
938   GNUNET_assert (h->reconnect_task == GNUNET_SCHEDULER_NO_TASK);
939   /* Forget about all neighbours that we used to be connected to */
940   GNUNET_CONTAINER_multihashmap_iterate (h->neighbours, &neighbour_delete, h);
941   if (NULL != h->cth)
942   {
943     GNUNET_CLIENT_notify_transmit_ready_cancel (h->cth);
944     h->cth = NULL;
945   }
946   if (NULL != h->client)
947   {
948     GNUNET_CLIENT_disconnect (h->client, GNUNET_YES);
949     h->client = NULL;
950   }
951   if (h->quota_task != GNUNET_SCHEDULER_NO_TASK)
952   {
953     GNUNET_SCHEDULER_cancel (h->quota_task);
954     h->quota_task = GNUNET_SCHEDULER_NO_TASK;
955   }
956   while ((NULL != (th = h->control_head)))
957   {
958     GNUNET_CONTAINER_DLL_remove (h->control_head, h->control_tail, th);
959     th->notify (th->notify_cls, 0, NULL);
960     GNUNET_free (th);
961   }
962 #if DEBUG_TRANSPORT_API
963   LOG (GNUNET_ERROR_TYPE_DEBUG,
964        "Scheduling task to reconnect to transport service in %llu ms.\n",
965        h->reconnect_delay.rel_value);
966 #endif
967   h->reconnect_task =
968       GNUNET_SCHEDULER_add_delayed (h->reconnect_delay, &reconnect, h);
969   if (h->reconnect_delay.rel_value == 0)
970   {
971     h->reconnect_delay = GNUNET_TIME_UNIT_MILLISECONDS;
972   }
973   else
974   {
975     h->reconnect_delay = GNUNET_TIME_relative_multiply (h->reconnect_delay, 2);
976     h->reconnect_delay =
977         GNUNET_TIME_relative_min (GNUNET_TIME_UNIT_SECONDS, h->reconnect_delay);
978   }
979 }
980
981
982 /**
983  * Send REQUEST_CONNECT message to the service.
984  *
985  * @param cls the 'struct GNUNET_PeerIdentity'
986  * @param size number of bytes available in buf
987  * @param buf where to copy the message
988  * @return number of bytes copied to buf
989  */
990 static size_t
991 send_try_connect (void *cls, size_t size, void *buf)
992 {
993   struct GNUNET_PeerIdentity *pid = cls;
994   struct TransportRequestConnectMessage msg;
995
996   if (buf == NULL)
997   {
998     GNUNET_free (pid);
999     return 0;
1000   }
1001 #if DEBUG_TRANSPORT_API
1002   LOG (GNUNET_ERROR_TYPE_DEBUG,
1003        "Transmitting `%s' request with respect to `%4s'.\n", "REQUEST_CONNECT",
1004        GNUNET_i2s (pid));
1005 #endif
1006   GNUNET_assert (size >= sizeof (struct TransportRequestConnectMessage));
1007   msg.header.size = htons (sizeof (struct TransportRequestConnectMessage));
1008   msg.header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_REQUEST_CONNECT);
1009   msg.reserved = htonl (0);
1010   msg.peer = *pid;
1011   memcpy (buf, &msg, sizeof (msg));
1012   GNUNET_free (pid);
1013   return sizeof (struct TransportRequestConnectMessage);
1014 }
1015
1016
1017 /**
1018  * Ask the transport service to establish a connection to
1019  * the given peer.
1020  *
1021  * @param handle connection to transport service
1022  * @param target who we should try to connect to
1023  */
1024 void
1025 GNUNET_TRANSPORT_try_connect (struct GNUNET_TRANSPORT_Handle *handle,
1026                               const struct GNUNET_PeerIdentity *target)
1027 {
1028   struct GNUNET_PeerIdentity *pid;
1029
1030   if (NULL == handle->client)
1031     return;
1032   pid = GNUNET_malloc (sizeof (struct GNUNET_PeerIdentity));
1033   *pid = *target;
1034   schedule_control_transmit (handle,
1035                              sizeof (struct TransportRequestConnectMessage),
1036                              &send_try_connect, pid);
1037 }
1038
1039
1040 /**
1041  * Send HELLO message to the service.
1042  *
1043  * @param cls the HELLO message to send
1044  * @param size number of bytes available in buf
1045  * @param buf where to copy the message
1046  * @return number of bytes copied to buf
1047  */
1048 static size_t
1049 send_hello (void *cls, size_t size, void *buf)
1050 {
1051   struct GNUNET_MessageHeader *msg = cls;
1052   uint16_t ssize;
1053
1054   if (buf == NULL)
1055   {
1056 #if DEBUG_TRANSPORT_TIMEOUT
1057     LOG (GNUNET_ERROR_TYPE_DEBUG,
1058          "Timeout while trying to transmit `%s' request.\n", "HELLO");
1059 #endif
1060     GNUNET_free (msg);
1061     return 0;
1062   }
1063 #if DEBUG_TRANSPORT_API
1064   LOG (GNUNET_ERROR_TYPE_DEBUG, "Transmitting `%s' request.\n", "HELLO");
1065 #endif
1066   ssize = ntohs (msg->size);
1067   GNUNET_assert (size >= ssize);
1068   memcpy (buf, msg, ssize);
1069   GNUNET_free (msg);
1070   return ssize;
1071 }
1072
1073
1074 /**
1075  * Offer the transport service the HELLO of another peer.  Note that
1076  * the transport service may just ignore this message if the HELLO is
1077  * malformed or useless due to our local configuration.
1078  *
1079  * @param handle connection to transport service
1080  * @param hello the hello message
1081  * @param cont continuation to call when HELLO has been sent
1082  * @param cls closure for continuation
1083  *
1084  */
1085 void
1086 GNUNET_TRANSPORT_offer_hello (struct GNUNET_TRANSPORT_Handle *handle,
1087                               const struct GNUNET_MessageHeader *hello,
1088                               GNUNET_SCHEDULER_Task cont, void *cls)
1089 {
1090   uint16_t size;
1091   struct GNUNET_PeerIdentity peer;
1092   struct GNUNET_MessageHeader *msg;
1093
1094   if (NULL == handle->client)
1095     return;
1096   GNUNET_break (ntohs (hello->type) == GNUNET_MESSAGE_TYPE_HELLO);
1097   size = ntohs (hello->size);
1098   GNUNET_break (size >= sizeof (struct GNUNET_MessageHeader));
1099   if (GNUNET_OK !=
1100       GNUNET_HELLO_get_id ((const struct GNUNET_HELLO_Message *) hello, &peer))
1101   {
1102     GNUNET_break (0);
1103     return;
1104   }
1105   msg = GNUNET_malloc (size);
1106   memcpy (msg, hello, size);
1107 #if DEBUG_TRANSPORT_API
1108   LOG (GNUNET_ERROR_TYPE_DEBUG,
1109        "Offering `%s' message of `%4s' to transport for validation.\n", "HELLO",
1110        GNUNET_i2s (&peer));
1111 #endif
1112   schedule_control_transmit (handle, size, &send_hello, msg);
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   ret->reconnect_task = GNUNET_SCHEDULER_add_now (&reconnect, ret);
1198   return ret;
1199 }
1200
1201
1202 /**
1203  * Disconnect from the transport service.
1204  *
1205  * @param handle handle to the service as returned from GNUNET_TRANSPORT_connect
1206  */
1207 void
1208 GNUNET_TRANSPORT_disconnect (struct GNUNET_TRANSPORT_Handle *handle)
1209 {
1210 #if DEBUG_TRANSPORT_API
1211   LOG (GNUNET_ERROR_TYPE_DEBUG, "Transport disconnect called!\n");
1212 #endif
1213   /* this disconnects all neighbours... */
1214   if (handle->reconnect_task == GNUNET_SCHEDULER_NO_TASK)
1215     disconnect_and_schedule_reconnect (handle);
1216   /* and now we stop trying to connect again... */
1217   if (handle->reconnect_task != GNUNET_SCHEDULER_NO_TASK)
1218   {
1219     GNUNET_SCHEDULER_cancel (handle->reconnect_task);
1220     handle->reconnect_task = GNUNET_SCHEDULER_NO_TASK;
1221   }
1222   GNUNET_CONTAINER_multihashmap_destroy (handle->neighbours);
1223   handle->neighbours = NULL;
1224   if (handle->quota_task != GNUNET_SCHEDULER_NO_TASK)
1225   {
1226     GNUNET_SCHEDULER_cancel (handle->quota_task);
1227     handle->quota_task = GNUNET_SCHEDULER_NO_TASK;
1228   }
1229   GNUNET_free_non_null (handle->my_hello);
1230   handle->my_hello = NULL;
1231   GNUNET_assert (handle->hwl_head == NULL);
1232   GNUNET_assert (handle->hwl_tail == NULL);
1233   GNUNET_CONTAINER_heap_destroy (handle->ready_heap);
1234   handle->ready_heap = NULL;
1235   GNUNET_free (handle);
1236 }
1237
1238
1239 /**
1240  * Check if we could queue a message of the given size for
1241  * transmission.  The transport service will take both its
1242  * internal buffers and bandwidth limits imposed by the
1243  * other peer into consideration when answering this query.
1244  *
1245  * @param handle connection to transport service
1246  * @param target who should receive the message
1247  * @param size how big is the message we want to transmit?
1248  * @param priority how important is the message?
1249  * @param timeout after how long should we give up (and call
1250  *        notify with buf NULL and size 0)?
1251  * @param notify function to call when we are ready to
1252  *        send such a message
1253  * @param notify_cls closure for notify
1254  * @return NULL if someone else is already waiting to be notified
1255  *         non-NULL if the notify callback was queued (can be used to cancel
1256  *         using GNUNET_TRANSPORT_notify_transmit_ready_cancel)
1257  */
1258 struct GNUNET_TRANSPORT_TransmitHandle *
1259 GNUNET_TRANSPORT_notify_transmit_ready (struct GNUNET_TRANSPORT_Handle *handle,
1260                                         const struct GNUNET_PeerIdentity
1261                                         *target, size_t size, uint32_t priority,
1262                                         struct GNUNET_TIME_Relative timeout,
1263                                         GNUNET_CONNECTION_TransmitReadyNotify
1264                                         notify, void *notify_cls)
1265 {
1266   struct Neighbour *n;
1267   struct GNUNET_TRANSPORT_TransmitHandle *th;
1268   struct GNUNET_TIME_Relative delay;
1269
1270   n = neighbour_find (handle, target);
1271   if (NULL == n)
1272   {
1273     /* use GNUNET_TRANSPORT_try_connect first, only use this function
1274      * once a connection has been established */
1275     GNUNET_assert (0);
1276     return NULL;
1277   }
1278   if (NULL != n->th)
1279   {
1280     /* attempt to send two messages at the same time to the same peer */
1281     GNUNET_assert (0);
1282     return NULL;
1283   }
1284   GNUNET_assert (NULL == n->hn);
1285   th = GNUNET_malloc (sizeof (struct GNUNET_TRANSPORT_TransmitHandle));
1286   th->neighbour = n;
1287   th->notify = notify;
1288   th->notify_cls = notify_cls;
1289   th->timeout = GNUNET_TIME_relative_to_absolute (timeout);
1290   th->notify_size = size;
1291   th->priority = priority;
1292   n->th = th;
1293   /* calculate when our transmission should be ready */
1294   delay = GNUNET_BANDWIDTH_tracker_get_delay (&n->out_tracker, size);
1295   if (delay.rel_value > timeout.rel_value)
1296     delay.rel_value = 0;        /* notify immediately (with failure) */
1297 #if DEBUG_TRANSPORT_API
1298   LOG (GNUNET_ERROR_TYPE_DEBUG,
1299        "Bandwidth tracker allows next transmission to peer %s in %llu ms\n",
1300        GNUNET_i2s (target), (unsigned long long) delay.rel_value);
1301 #endif
1302   n->hn = GNUNET_CONTAINER_heap_insert (handle->ready_heap, n, delay.rel_value);
1303   schedule_transmission (handle);
1304   return th;
1305 }
1306
1307
1308 /**
1309  * Cancel the specified transmission-ready notification.
1310  *
1311  * @param th handle returned from GNUNET_TRANSPORT_notify_transmit_ready
1312  */
1313 void
1314 GNUNET_TRANSPORT_notify_transmit_ready_cancel (struct
1315                                                GNUNET_TRANSPORT_TransmitHandle
1316                                                *th)
1317 {
1318   struct Neighbour *n;
1319
1320   GNUNET_assert (NULL == th->next);
1321   GNUNET_assert (NULL == th->prev);
1322   n = th->neighbour;
1323   GNUNET_assert (th == n->th);
1324   n->th = NULL;
1325   if (n->hn != NULL)
1326   {
1327     GNUNET_CONTAINER_heap_remove_node (n->hn);
1328     n->hn = NULL;
1329   }
1330   else
1331   {
1332     GNUNET_assert (GNUNET_SCHEDULER_NO_TASK != th->timeout_task);
1333     GNUNET_SCHEDULER_cancel (th->timeout_task);
1334     th->timeout_task = GNUNET_SCHEDULER_NO_TASK;
1335   }
1336   GNUNET_free (th);
1337 }
1338
1339
1340 /* end of transport_api.c */