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