inline
[oweals/gnunet.git] / src / transport / transport_api.c
1 /*
2      This file is part of GNUnet.
3      (C) 2009 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 2, 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  * - set_quota with low bandwidth should cause peer
28  *   disconnects (currently never does that) (MINOR)
29  */
30 #include "platform.h"
31 #include "gnunet_client_lib.h"
32 #include "gnunet_arm_service.h"
33 #include "gnunet_hello_lib.h"
34 #include "gnunet_protocols.h"
35 #include "gnunet_server_lib.h"
36 #include "gnunet_time_lib.h"
37 #include "gnunet_transport_service.h"
38 #include "transport.h"
39
40 /**
41  * After how long do we give up on transmitting a HELLO
42  * to the service?
43  */
44 #define OFFER_HELLO_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 30)
45
46 /**
47  * After how long do we give automatically retry an unsuccessful
48  * CONNECT request?
49  */
50 #define CONNECT_RETRY_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS, 750)
51
52 /**
53  * How long should ARM wait when starting up the
54  * transport service before reporting back?
55  */
56 #define START_SERVICE_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 5)
57
58 /**
59  * How long should ARM wait when stopping the
60  * transport service before reporting back?
61  */
62 #define STOP_SERVICE_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 5)
63
64 /**
65  * Entry in linked list of all of our current neighbours.
66  */
67 struct NeighbourList
68 {
69
70   /**
71    * This is a linked list.
72    */
73   struct NeighbourList *next;
74
75   /**
76    * Active transmit handle, can be NULL.  Used to move
77    * from ready to wait list on disconnect and to block
78    * two transmissions to the same peer from being scheduled
79    * at the same time.
80    */
81   struct GNUNET_TRANSPORT_TransmitHandle *transmit_handle;
82
83   /**
84    * Identity of this neighbour.
85    */
86   struct GNUNET_PeerIdentity id;
87
88   /**
89    * At what time did we reset last_sent last?
90    */
91   struct GNUNET_TIME_Absolute last_quota_update;
92
93   /**
94    * How many bytes have we sent since the "last_quota_update"
95    * timestamp?
96    */
97   uint64_t last_sent;
98
99   /**
100    * Quota for outbound traffic to the neighbour in bytes/ms.
101    */
102   uint32_t quota_out;
103
104   /**
105    * Set to GNUNET_YES if we are currently allowed to
106    * transmit a message to the transport service for this
107    * peer, GNUNET_NO otherwise.
108    */
109   int transmit_ok;
110
111   /**
112    * Set to GNUNET_YES if we have received an ACK for the
113    * given peer.  Peers that receive our HELLO always respond
114    * with an ACK to let us know that we are successfully
115    * communicating.  Note that a PING can not be used for this
116    * since PINGs are only send if a HELLO address requires
117    * confirmation (and also, PINGs are not passed to the
118    * transport API itself).
119    */
120   int received_ack;
121
122 };
123
124
125 /**
126  * Linked list of requests from clients for our HELLO
127  * that were deferred.
128  */
129 struct HelloWaitList
130 {
131
132   /**
133    * This is a linked list.
134    */
135   struct HelloWaitList *next;
136
137   /**
138    * Reference back to our transport handle.
139    */
140   struct GNUNET_TRANSPORT_Handle *handle;
141
142   /**
143    * Callback to call once we got our HELLO.
144    */
145   GNUNET_TRANSPORT_ReceiveCallback rec;
146
147   /**
148    * Closure for rec.
149    */
150   void *rec_cls;
151
152   /**
153    * When to time out (call rec with NULL).
154    */
155   struct GNUNET_TIME_Absolute timeout;
156
157   /**
158    * Timeout task (used to trigger timeout,
159    * cancel if we get the HELLO in time).
160    */
161   GNUNET_SCHEDULER_TaskIdentifier task;
162
163
164 };
165
166
167 /**
168  * Opaque handle for a transmission-ready request.
169  */
170 struct GNUNET_TRANSPORT_TransmitHandle
171 {
172
173   /**
174    * We keep the transmit handles that are waiting for
175    * a transport-level connection in a doubly linked list.
176    */
177   struct GNUNET_TRANSPORT_TransmitHandle *next;
178
179   /**
180    * We keep the transmit handles that are waiting for
181    * a transport-level connection in a doubly linked list.
182    */
183   struct GNUNET_TRANSPORT_TransmitHandle *prev;
184
185   /**
186    * Handle of the main transport data structure.
187    */
188   struct GNUNET_TRANSPORT_Handle *handle;
189
190   /**
191    * Neighbour for this handle, can be NULL if the service
192    * is not yet connected to the target.
193    */
194   struct NeighbourList *neighbour;
195
196   /**
197    * Which peer is this transmission going to be for?  All
198    * zeros if it is control-traffic to the service.
199    */
200   struct GNUNET_PeerIdentity target;
201
202   /**
203    * Function to call when notify_size bytes are available
204    * for transmission.
205    */
206   GNUNET_CONNECTION_TransmitReadyNotify notify;
207
208   /**
209    * Closure for notify.
210    */
211   void *notify_cls;
212
213   /**
214    * transmit_ready task Id.  The task is used to introduce the
215    * artificial delay that may be required to maintain the bandwidth
216    * limits.  Later, this will be the ID of the "transmit_timeout"
217    * task which is used to signal a timeout if the transmission could
218    * not be done in a timely fashion.
219    */
220   GNUNET_SCHEDULER_TaskIdentifier notify_delay_task;
221
222   /**
223    * Timeout for this request.
224    */
225   struct GNUNET_TIME_Absolute timeout;
226
227   /**
228    * How many bytes is our notify callback waiting for?
229    */
230   size_t notify_size;
231
232   /**
233    * How important is this message?
234    */
235   unsigned int priority;
236
237 };
238
239
240 /**
241  * Handle for the transport service (includes all of the
242  * state for the transport service).
243  */
244 struct GNUNET_TRANSPORT_Handle
245 {
246
247   /**
248    * Closure for the callbacks.
249    */
250   void *cls;
251
252   /**
253    * Function to call for received data.
254    */
255   GNUNET_TRANSPORT_ReceiveCallback rec;
256
257   /**
258    * function to call on connect events
259    */
260   GNUNET_TRANSPORT_NotifyConnect nc_cb;
261
262   /**
263    * function to call on disconnect events
264    */
265   GNUNET_TRANSPORT_NotifyDisconnect nd_cb;
266
267   /**
268    * The current HELLO message for this peer.  Updated
269    * whenever transports change their addresses.
270    */
271   struct GNUNET_HELLO_Message *my_hello;
272
273   /**
274    * My client connection to the transport service.
275    */
276   struct GNUNET_CLIENT_Connection *client;
277
278   /**
279    * Handle to our registration with the client for notification.
280    */
281   struct GNUNET_CLIENT_TransmitHandle *network_handle;
282
283   /**
284    * Linked list of transmit handles that are waiting for the
285    * transport to connect to the respective peer.  When we
286    * receive notification that the transport connected to a
287    * peer, we go over this list and check if someone has already
288    * requested a transmission to the new peer; if so, we trigger
289    * the next step.
290    */
291   struct GNUNET_TRANSPORT_TransmitHandle *connect_wait_head;
292
293   /**
294    * Linked list of transmit handles that are waiting for the
295    * transport to be ready for transmission to the respective
296    * peer.  When we
297    * receive notification that the transport disconnected from
298    * a peer, we go over this list and move the entry back to
299    * the connect_wait list.
300    */
301   struct GNUNET_TRANSPORT_TransmitHandle *connect_ready_head;
302
303   /**
304    * Linked list of pending requests for our HELLO.
305    */
306   struct HelloWaitList *hwl_head;
307
308   /**
309    * My scheduler.
310    */
311   struct GNUNET_SCHEDULER_Handle *sched;
312
313   /**
314    * My configuration.
315    */
316   const struct GNUNET_CONFIGURATION_Handle *cfg;
317
318   /**
319    * Linked list of the current neighbours of this peer.
320    */
321   struct NeighbourList *neighbours;
322
323   /**
324    * ID of the task trying to reconnect to the
325    * service.
326    */
327   GNUNET_SCHEDULER_TaskIdentifier reconnect_task;
328
329   /**
330    * Delay until we try to reconnect.
331    */
332   struct GNUNET_TIME_Relative reconnect_delay;
333
334   /**
335    * Do we currently have a transmission pending?
336    * (schedule transmission was called but has not
337    * yet succeeded)?
338    */
339   int transmission_scheduled;
340 };
341
342
343 static struct NeighbourList *
344 find_neighbour (struct GNUNET_TRANSPORT_Handle *h,
345                 const struct GNUNET_PeerIdentity *peer)
346 {
347   struct NeighbourList *pos;
348
349   pos = h->neighbours;
350   while ((pos != NULL) &&
351          (0 != memcmp (peer, &pos->id, sizeof (struct GNUNET_PeerIdentity))))
352     pos = pos->next;
353   return pos;
354 }
355
356
357 /**
358  * Schedule the task to send one message from the
359  * connect_ready list to the service.
360  */
361 static void schedule_transmission (struct GNUNET_TRANSPORT_Handle *h);
362
363
364 /**
365  * Transmit message to client...
366  */
367 static size_t
368 transport_notify_ready (void *cls, size_t size, void *buf)
369 {
370   struct GNUNET_TRANSPORT_Handle *h = cls;
371   struct GNUNET_TRANSPORT_TransmitHandle *th;
372   struct NeighbourList *n;
373   size_t ret;
374   char *cbuf;
375
376   h->network_handle = NULL;
377   h->transmission_scheduled = GNUNET_NO;
378   if (buf == NULL)
379     {
380 #if DEBUG_TRANSPORT
381       GNUNET_log (GNUNET_ERROR_TYPE_INFO,
382                   "Could not transmit to transport service, cancelling pending requests\n");
383 #endif
384       th = h->connect_ready_head;
385       if (th->next != NULL)
386         th->next->prev = NULL;
387       h->connect_ready_head = th->next;
388       if (NULL != (n = th->neighbour))
389         {
390           GNUNET_assert (n->transmit_handle == th);
391           n->transmit_handle = NULL;
392         }
393       if (th->notify_delay_task != GNUNET_SCHEDULER_NO_TASK)
394         {
395           GNUNET_SCHEDULER_cancel (h->sched,
396                                    th->notify_delay_task);
397           th->notify_delay_task = GNUNET_SCHEDULER_NO_TASK;
398         }
399       GNUNET_assert (0 == th->notify (th->notify_cls, 0, NULL));
400       GNUNET_free (th);
401       if (h->connect_ready_head != NULL) schedule_transmission (h); /* FIXME: is this ok? */
402       return 0;
403     } 
404 #if DEBUG_TRANSPORT
405   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
406               "Ready to transmit %u bytes to transport service\n", size);
407 #endif
408   cbuf = buf;
409   ret = 0;
410   h->network_handle = NULL;
411   h->transmission_scheduled = GNUNET_NO;
412   do
413     {
414       th = h->connect_ready_head;
415       if (th->notify_delay_task != GNUNET_SCHEDULER_NO_TASK)
416         {
417           GNUNET_SCHEDULER_cancel (h->sched,
418                                    th->notify_delay_task);
419           th->notify_delay_task = GNUNET_SCHEDULER_NO_TASK;
420         }
421       GNUNET_assert (th->notify_size <= size);
422       if (th->next != NULL)
423         th->next->prev = NULL;
424       h->connect_ready_head = th->next;
425       if (NULL != (n = th->neighbour))
426         {
427           GNUNET_assert (n->transmit_handle == th);
428           n->transmit_handle = NULL;
429         }
430       ret += th->notify (th->notify_cls, size, &cbuf[ret]);
431       GNUNET_free (th);
432       if (n != NULL)
433         n->last_sent += ret;
434       size -= ret;
435     }
436   while ((h->connect_ready_head != NULL) &&
437          (h->connect_ready_head->notify_size <= size));
438   if (h->connect_ready_head != NULL)
439     schedule_transmission (h);
440 #if DEBUG_TRANSPORT
441   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
442               "Transmitting %u bytes to transport service\n", ret);
443 #endif
444   return ret;
445 }
446
447
448 /**
449  * Schedule the task to send one message from the
450  * connect_ready list to the service.
451  */
452 static void
453 schedule_transmission (struct GNUNET_TRANSPORT_Handle *h)
454 {
455   struct GNUNET_TRANSPORT_TransmitHandle *th;
456
457   GNUNET_assert (NULL == h->network_handle);
458   if (h->client == NULL)
459     {
460       GNUNET_log (GNUNET_ERROR_TYPE_INFO,
461                   "Could not yet schedule transmission: we are not yet connected to the transport service!\n");
462       return; /* not yet connected */
463     }
464   th = h->connect_ready_head;
465   if (th == NULL)
466     return; /* no request pending */
467   if (th->notify_delay_task != GNUNET_SCHEDULER_NO_TASK)
468     {
469       /* remove existing time out task, will be integrated
470          with transmit_ready notification! */
471       GNUNET_SCHEDULER_cancel (h->sched,
472                                th->notify_delay_task);
473       th->notify_delay_task = GNUNET_SCHEDULER_NO_TASK;
474     }
475   h->transmission_scheduled = GNUNET_YES;
476   h->network_handle = GNUNET_CLIENT_notify_transmit_ready (h->client,
477                                                            th->notify_size,
478                                                            GNUNET_TIME_absolute_get_remaining
479                                                            (th->timeout),
480                                                            GNUNET_NO,
481                                                            &transport_notify_ready,
482                                                            h);
483   GNUNET_assert (NULL != h->network_handle);
484 }
485
486
487 /**
488  * Insert the given transmit handle in the given sorted
489  * doubly linked list based on timeout.
490  *
491  * @param head pointer to the head of the linked list
492  * @param th element to insert into the list
493  */
494 static void
495 insert_transmit_handle (struct GNUNET_TRANSPORT_TransmitHandle **head,
496                         struct GNUNET_TRANSPORT_TransmitHandle *th)
497 {
498   struct GNUNET_TRANSPORT_TransmitHandle *pos;
499   struct GNUNET_TRANSPORT_TransmitHandle *prev;
500
501   pos = *head;
502   prev = NULL;
503   while ((pos != NULL) && (pos->timeout.value < th->timeout.value))
504     {
505       prev = pos;
506       pos = pos->next;
507     }
508   if (prev == NULL)
509     {
510       th->next = *head;
511       if (th->next != NULL)
512         th->next->prev = th;
513       *head = th;
514     }
515   else
516     {
517       th->next = pos;
518       th->prev = prev;
519       prev->next = th;
520       if (pos != NULL)
521         pos->prev = th;
522     }
523 }
524
525
526 /**
527  * Cancel a pending notify delay task (if pending) and also remove the
528  * given transmit handle from whatever list is on.
529  *
530  * @param th handle for the transmission request to manipulate
531  */
532 static void
533 remove_from_any_list (struct GNUNET_TRANSPORT_TransmitHandle *th)
534 {
535   struct GNUNET_TRANSPORT_Handle *h;
536
537   h = th->handle;
538   if (th->notify_delay_task != GNUNET_SCHEDULER_NO_TASK)
539     {
540       GNUNET_SCHEDULER_cancel (h->sched, th->notify_delay_task);
541       th->notify_delay_task = GNUNET_SCHEDULER_NO_TASK;
542     }
543   if (th->prev == NULL)
544     {
545       if (th == h->connect_wait_head)
546         h->connect_wait_head = th->next;
547       else
548         h->connect_ready_head = th->next;
549     }
550   else
551     {
552       th->prev->next = th->next;
553     }
554   if (th->next != NULL)
555     th->next->prev = th->prev;
556 }
557
558
559 /**
560  * Schedule a request to connect to the given
561  * neighbour (and if successful, add the specified
562  * handle to the wait list).
563  *
564  * @param th handle for a request to transmit once we
565  *        have connected
566  */
567 static void
568 try_connect (struct GNUNET_TRANSPORT_TransmitHandle *th);
569
570
571 /**
572  * Called when our transmit request timed out before any transport
573  * reported success connecting to the desired peer or before the
574  * transport was ready to receive.  Signal error and free
575  * TransmitHandle.
576  */
577 static void
578 peer_transmit_timeout (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
579 {
580   struct GNUNET_TRANSPORT_TransmitHandle *th = cls;
581
582   th->notify_delay_task = GNUNET_SCHEDULER_NO_TASK;
583   if (th->neighbour != NULL)
584     th->neighbour->transmit_handle = NULL;
585 #if DEBUG_TRANSPORT
586   GNUNET_log (GNUNET_ERROR_TYPE_INFO, 
587               "Request for transmission to peer `%s' timed out.\n",
588               GNUNET_i2s(&th->target));
589 #endif
590   remove_from_any_list (th);
591   th->notify (th->notify_cls, 0, NULL);
592   GNUNET_free (th);
593 }
594
595
596
597
598 /**
599  * Queue control request for transmission to the transport
600  * service.
601  *
602  * @param h handle to the transport service
603  * @param size number of bytes to be transmitted
604  * @param at_head request must be added to the head of the queue
605  *        (otherwise request will be appended)
606  * @param timeout how long this transmission can wait (at most)
607  * @param notify function to call to get the content
608  * @param notify_cls closure for notify
609  */
610 static void
611 schedule_control_transmit (struct GNUNET_TRANSPORT_Handle *h,
612                            size_t size,
613                            int at_head,
614                            struct GNUNET_TIME_Relative timeout,
615                            GNUNET_CONNECTION_TransmitReadyNotify notify,
616                            void *notify_cls)
617 {
618   struct GNUNET_TRANSPORT_TransmitHandle *th;
619
620   th = GNUNET_malloc (sizeof (struct GNUNET_TRANSPORT_TransmitHandle));
621   th->handle = h;
622   th->notify = notify;
623   th->notify_cls = notify_cls;
624   th->timeout = GNUNET_TIME_relative_to_absolute (timeout);
625   th->notify_size = size;
626   th->notify_delay_task 
627     = GNUNET_SCHEDULER_add_delayed (h->sched,
628                                     GNUNET_NO,
629                                     GNUNET_SCHEDULER_PRIORITY_KEEP,
630                                     GNUNET_SCHEDULER_NO_TASK,
631                                     timeout,
632                                     &peer_transmit_timeout, th);    
633   if (at_head)
634     {
635       th->next = h->connect_ready_head;
636       h->connect_ready_head = th;
637       if (th->next != NULL)
638         th->next->prev = th;
639     }
640   else
641     {
642       insert_transmit_handle (&h->connect_ready_head, th);
643     }
644   if (GNUNET_NO == h->transmission_scheduled)
645     schedule_transmission (h);
646 }
647
648
649 /**
650  * Update the quota values for the given neighbour now.
651  */
652 static void
653 update_quota (struct NeighbourList *n)
654 {
655   struct GNUNET_TIME_Relative delta;
656   uint64_t allowed;
657   uint64_t remaining;
658
659   delta = GNUNET_TIME_absolute_get_duration (n->last_quota_update);
660   allowed = delta.value * n->quota_out;
661   if (n->last_sent < allowed)
662     {
663       remaining = allowed - n->last_sent;
664       if (n->quota_out > 0)
665         remaining /= n->quota_out;
666       else
667         remaining = 0;
668       if (remaining > MAX_BANDWIDTH_CARRY)
669         remaining = MAX_BANDWIDTH_CARRY;
670       n->last_sent = 0;
671       n->last_quota_update = GNUNET_TIME_absolute_get ();
672       n->last_quota_update.value -= remaining;
673     }
674   else
675     {
676       n->last_sent -= allowed;
677       n->last_quota_update = GNUNET_TIME_absolute_get ();
678     }
679 }
680
681
682 struct SetQuotaContext
683 {
684   struct GNUNET_TRANSPORT_Handle *handle;
685
686   struct GNUNET_PeerIdentity target;
687
688   GNUNET_SCHEDULER_Task cont;
689
690   void *cont_cls;
691
692   struct GNUNET_TIME_Absolute timeout;
693
694   uint32_t quota_in;
695 };
696
697
698 static size_t
699 send_set_quota (void *cls, size_t size, void *buf)
700 {
701   struct SetQuotaContext *sqc = cls;
702   struct QuotaSetMessage *msg;
703
704   if (buf == NULL)
705     {
706       GNUNET_SCHEDULER_add_continuation (sqc->handle->sched,
707                                          GNUNET_NO,
708                                          sqc->cont,
709                                          sqc->cont_cls,
710                                          GNUNET_SCHEDULER_REASON_TIMEOUT);
711       GNUNET_free (sqc);
712       return 0;
713     }
714 #if DEBUG_TRANSPORT
715   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
716               "Transmitting `%s' request with respect to `%4s'.\n",
717               "SET_QUOTA", GNUNET_i2s (&sqc->target));
718 #endif
719   GNUNET_assert (size >= sizeof (struct QuotaSetMessage));
720   msg = buf;
721   msg->header.size = htons (sizeof (struct QuotaSetMessage));
722   msg->header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_SET_QUOTA);
723   msg->quota_in = htonl (sqc->quota_in);
724   memcpy (&msg->peer, &sqc->target, sizeof (struct GNUNET_PeerIdentity));
725   if (sqc->cont != NULL)
726     GNUNET_SCHEDULER_add_continuation (sqc->handle->sched,
727                                        GNUNET_NO,
728                                        sqc->cont,
729                                        sqc->cont_cls,
730                                        GNUNET_SCHEDULER_REASON_PREREQ_DONE);
731   GNUNET_free (sqc);
732   return sizeof (struct QuotaSetMessage);
733 }
734
735
736 /**
737  * Set the share of incoming bandwidth for the given
738  * peer to the specified amount.
739  *
740  * @param handle connection to transport service
741  * @param target who's bandwidth quota is being changed
742  * @param quota_in incoming bandwidth quota in bytes per ms; 0 can
743  *        be used to force all traffic to be discarded
744  * @param quota_out outgoing bandwidth quota in bytes per ms; 0 can
745  *        be used to force all traffic to be discarded
746  * @param timeout how long to wait until signaling failure if
747  *        we can not communicate the quota change
748  * @param cont continuation to call when done, will be called
749  *        either with reason "TIMEOUT" or with reason "PREREQ_DONE"
750  * @param cont_cls closure for continuation
751  */
752 void
753 GNUNET_TRANSPORT_set_quota (struct GNUNET_TRANSPORT_Handle *handle,
754                             const struct GNUNET_PeerIdentity *target,
755                             uint32_t quota_in,
756                             uint32_t quota_out,
757                             struct GNUNET_TIME_Relative timeout,
758                             GNUNET_SCHEDULER_Task cont, void *cont_cls)
759 {
760   struct NeighbourList *n;
761   struct SetQuotaContext *sqc;
762
763   n = find_neighbour (handle, target);
764   if (n != NULL)
765     {
766       update_quota (n);
767       if (n->quota_out < quota_out)
768         n->last_quota_update = GNUNET_TIME_absolute_get ();
769       n->quota_out = quota_out;
770     }
771   sqc = GNUNET_malloc (sizeof (struct SetQuotaContext));
772   sqc->handle = handle;
773   sqc->target = *target;
774   sqc->cont = cont;
775   sqc->cont_cls = cont_cls;
776   sqc->timeout = GNUNET_TIME_relative_to_absolute (timeout);
777   sqc->quota_in = quota_in;
778   schedule_control_transmit (handle,
779                              sizeof (struct QuotaSetMessage),
780                              GNUNET_NO, timeout, &send_set_quota, sqc);
781 }
782
783
784 /**
785  * A "get_hello" request has timed out.  Signal the client
786  * and clean up.
787  */
788 static void
789 hello_wait_timeout (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
790 {
791   struct HelloWaitList *hwl = cls;
792   struct HelloWaitList *pos;
793   struct HelloWaitList *prev;
794
795   hwl->task = GNUNET_SCHEDULER_NO_TASK;
796   if (GNUNET_TIME_absolute_get_remaining (hwl->timeout).value > 0)
797     {
798 #if DEBUG_TRANSPORT
799       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
800                   _("First attempt to obtain `%s' from transport service failed, will try again for %llums.\n"),
801                   "HELLO",
802                   GNUNET_TIME_absolute_get_remaining (hwl->timeout).value);
803 #endif
804       hwl->task = GNUNET_SCHEDULER_add_delayed (hwl->handle->sched,
805                                                 GNUNET_YES,
806                                                 GNUNET_SCHEDULER_PRIORITY_KEEP,
807                                                 GNUNET_SCHEDULER_NO_TASK,
808                                                 GNUNET_TIME_absolute_get_remaining (hwl->timeout),
809                                                 &hello_wait_timeout, hwl);
810       return;      
811     }
812   /* signal timeout */
813   GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
814               _("Timeout trying to obtain `%s' from transport service.\n"),
815               "HELLO");
816   prev = NULL;
817   pos = hwl->handle->hwl_head;
818   while (pos != hwl)
819     {
820       GNUNET_assert (pos != NULL);
821       prev = pos;
822       pos = pos->next;
823     }
824   if (prev == NULL)
825     hwl->handle->hwl_head = hwl->next;
826   else
827     prev->next = hwl->next;
828   if (hwl->rec != NULL)
829     hwl->rec (hwl->rec_cls, GNUNET_TIME_UNIT_ZERO, NULL, NULL);
830   GNUNET_free (hwl);
831 }
832
833
834 /**
835  * Obtain the HELLO message for this peer.
836  *
837  * @param handle connection to transport service
838  * @param timeout how long to wait for the HELLO
839  * @param rec function to call with the HELLO, sender will be our peer
840  *            identity; message and sender will be NULL on timeout
841  *            (handshake with transport service pending/failed).
842  *             cost estimate will be 0.
843  * @param rec_cls closure for rec
844  */
845 void
846 GNUNET_TRANSPORT_get_hello (struct GNUNET_TRANSPORT_Handle *handle,
847                             struct GNUNET_TIME_Relative timeout,
848                             GNUNET_TRANSPORT_ReceiveCallback rec,
849                             void *rec_cls)
850 {
851   struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded pk;
852   struct GNUNET_PeerIdentity me;
853   struct HelloWaitList *hwl;
854
855   if (handle->my_hello == NULL)
856     {
857       hwl = GNUNET_malloc (sizeof (struct HelloWaitList));
858       hwl->next = handle->hwl_head;
859       handle->hwl_head = hwl;
860       hwl->handle = handle;
861       hwl->rec = rec;
862       hwl->rec_cls = rec_cls;
863       hwl->timeout = GNUNET_TIME_relative_to_absolute (timeout);
864       hwl->task = GNUNET_SCHEDULER_add_delayed (handle->sched,
865                                                 GNUNET_YES,
866                                                 GNUNET_SCHEDULER_PRIORITY_KEEP,
867                                                 GNUNET_SCHEDULER_NO_TASK,
868                                                 timeout,
869                                                 &hello_wait_timeout, hwl);
870       return;
871     }
872   GNUNET_assert (GNUNET_OK == GNUNET_HELLO_get_key (handle->my_hello, &pk));
873   GNUNET_CRYPTO_hash (&pk,
874                       sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
875                       &me.hashPubKey);
876
877   rec (rec_cls,
878        GNUNET_TIME_UNIT_ZERO,
879        &me, (const struct GNUNET_MessageHeader *) handle->my_hello);
880 }
881
882
883 static size_t
884 send_hello (void *cls, size_t size, void *buf)
885 {
886   struct GNUNET_MessageHeader *hello = cls;
887   uint16_t msize;
888
889   if (buf == NULL)
890     {
891 #if DEBUG_TRANSPORT
892       GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
893                   "Timeout while trying to transmit `%s' request.\n",
894                   "HELLO");
895 #endif
896       GNUNET_free (hello);
897       return 0;
898     }
899 #if DEBUG_TRANSPORT
900   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
901               "Transmitting `%s' request.\n", "HELLO");
902 #endif
903   msize = ntohs (hello->size);
904   GNUNET_assert (size >= msize);
905   memcpy (buf, hello, msize);
906   GNUNET_free (hello);
907   return msize;
908 }
909
910
911 /**
912  * Offer the transport service the HELLO of another peer.  Note that
913  * the transport service may just ignore this message if the HELLO is
914  * malformed or useless due to our local configuration.
915  *
916  * @param handle connection to transport service
917  * @param hello the hello message
918  */
919 void
920 GNUNET_TRANSPORT_offer_hello (struct GNUNET_TRANSPORT_Handle *handle,
921                               const struct GNUNET_MessageHeader *hello)
922 {
923   struct GNUNET_MessageHeader *hc;
924   uint16_t size;
925
926   if (handle->client == NULL)
927     {
928 #if DEBUG_TRANSPORT
929       GNUNET_log (GNUNET_ERROR_TYPE_INFO,
930                   "Not connected to transport service, dropping offered HELLO\n");
931 #endif
932       return;
933     }
934   GNUNET_break (ntohs (hello->type) == GNUNET_MESSAGE_TYPE_HELLO);
935   size = ntohs (hello->size);
936   GNUNET_break (size >= sizeof (struct GNUNET_MessageHeader));
937   hc = GNUNET_malloc (size);
938   memcpy (hc, hello, size);
939   schedule_control_transmit (handle,
940                              size,
941                              GNUNET_NO, OFFER_HELLO_TIMEOUT, &send_hello, hc);
942 }
943
944
945 /**
946  * Function we use for handling incoming messages.
947  */
948 static void demultiplexer (void *cls, const struct GNUNET_MessageHeader *msg);
949
950
951 static size_t
952 send_start (void *cls, size_t size, void *buf)
953 {
954   struct GNUNET_MessageHeader *s = buf;
955
956   if (buf == NULL)
957     {
958 #if DEBUG_TRANSPORT
959       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
960                   "Timeout while trying to transmit `%s' request.\n",
961                   "START");
962 #endif
963       return 0;
964     }
965 #if DEBUG_TRANSPORT
966   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
967               "Transmitting `%s' request.\n", "START");
968 #endif
969   GNUNET_assert (size >= sizeof (struct GNUNET_MessageHeader));
970   s->size = htons (sizeof (struct GNUNET_MessageHeader));
971   s->type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_START);
972   return sizeof (struct GNUNET_MessageHeader);
973 }
974
975
976 /**
977  * We're ready to transmit the request that the transport service
978  * should connect to a new peer.  In addition to sending the
979  * request, schedule the next phase for the transmission processing
980  * that caused the connect request in the first place.
981  */
982 static size_t
983 request_connect (void *cls, size_t size, void *buf)
984 {
985   struct GNUNET_TRANSPORT_TransmitHandle *th = cls;
986   struct TryConnectMessage *tcm;
987   struct GNUNET_TRANSPORT_Handle *h;
988
989   GNUNET_assert (th->notify_delay_task == GNUNET_SCHEDULER_NO_TASK);
990   h = th->handle;
991   if (buf == NULL)
992     {
993 #if DEBUG_TRANSPORT
994       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
995                   "Failed to transmit `%s' request for `%4s' to service.\n",
996                   "TRY_CONNECT",
997                   GNUNET_i2s(&th->target));
998 #endif
999       if (th->notify_delay_task != GNUNET_SCHEDULER_NO_TASK)
1000         {
1001           GNUNET_SCHEDULER_cancel (h->sched, th->notify_delay_task);
1002           th->notify_delay_task = GNUNET_SCHEDULER_NO_TASK;
1003         }
1004       th->notify (th->notify_cls, 0, NULL);
1005       GNUNET_free (th);
1006       return 0;
1007     }
1008 #if DEBUG_TRANSPORT
1009   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1010               "Transmitting `%s' message for `%4s'.\n",
1011               "TRY_CONNECT", GNUNET_i2s (&th->target));
1012 #endif
1013   GNUNET_assert (size >= sizeof (struct TryConnectMessage));
1014   tcm = buf;
1015   tcm->header.size = htons (sizeof (struct TryConnectMessage));
1016   tcm->header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_TRY_CONNECT);
1017   tcm->reserved = htonl (0);
1018   memcpy (&tcm->peer, &th->target, sizeof (struct GNUNET_PeerIdentity));
1019   th->notify_delay_task
1020     = GNUNET_SCHEDULER_add_delayed (h->sched,
1021                                     GNUNET_NO,
1022                                     GNUNET_SCHEDULER_PRIORITY_KEEP,
1023                                     GNUNET_SCHEDULER_NO_TASK,
1024                                     GNUNET_TIME_absolute_get_remaining
1025                                     (th->timeout),
1026                                     &peer_transmit_timeout, th);
1027   insert_transmit_handle (&h->connect_wait_head, th);
1028   return sizeof (struct TryConnectMessage);
1029 }
1030
1031
1032 /**
1033  * Schedule a request to connect to the given
1034  * neighbour (and if successful, add the specified
1035  * handle to the wait list).
1036  *
1037  * @param th handle for a request to transmit once we
1038  *        have connected
1039  */
1040 static void
1041 try_connect (struct GNUNET_TRANSPORT_TransmitHandle *th)
1042 {
1043   GNUNET_assert (th->notify_delay_task == GNUNET_SCHEDULER_NO_TASK);  
1044   schedule_control_transmit (th->handle,
1045                              sizeof (struct TryConnectMessage),
1046                              GNUNET_NO,
1047                              GNUNET_TIME_absolute_get_remaining (th->timeout),
1048                              &request_connect, th);
1049 }
1050
1051
1052 /**
1053  * Task for delayed attempts to reconnect to a peer.
1054  *
1055  * @param cls must be a transmit handle that determines the peer
1056  *        to which we will try to connect
1057  * @param tc scheduler information about why we were triggered (not used)
1058  */
1059 static void
1060 try_connect_task (void *cls,
1061                   const struct GNUNET_SCHEDULER_TaskContext *tc)
1062 {
1063   struct GNUNET_TRANSPORT_TransmitHandle *th = cls;  
1064
1065   th->notify_delay_task = GNUNET_SCHEDULER_NO_TASK;
1066   try_connect (th);
1067 }
1068
1069
1070 /**
1071  * Remove neighbour from our list.  Will automatically
1072  * trigger a re-connect attempt if we have messages pending
1073  * for this peer.
1074  * 
1075  * @param h our state
1076  * @param peer the peer to remove
1077  */
1078 static void
1079 remove_neighbour (struct GNUNET_TRANSPORT_Handle *h,
1080                   const struct GNUNET_PeerIdentity *peer)
1081 {
1082   struct NeighbourList *prev;
1083   struct NeighbourList *pos;
1084   struct GNUNET_TRANSPORT_TransmitHandle *th;
1085
1086 #if DEBUG_TRANSPORT
1087   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1088               "Removing neighbour `%s' from list of connected peers.\n",
1089               GNUNET_i2s (peer));
1090 #endif
1091   prev = NULL;
1092   pos = h->neighbours;
1093   while ((pos != NULL) &&
1094          (0 != memcmp (peer, 
1095                        &pos->id, 
1096                        sizeof (struct GNUNET_PeerIdentity))))
1097     {
1098       prev = pos;
1099       pos = pos->next;
1100     }
1101   if (pos == NULL)
1102     {
1103       GNUNET_break (0);
1104       return;
1105     }
1106   if (prev == NULL)
1107     h->neighbours = pos->next;
1108   else
1109     prev->next = pos->next;
1110   if (NULL != (th = pos->transmit_handle))
1111     {
1112       pos->transmit_handle = NULL;
1113       th->neighbour = NULL;
1114       remove_from_any_list (th);
1115       if (GNUNET_TIME_absolute_get_remaining (th->timeout).value <= CONNECT_RETRY_TIMEOUT.value)
1116         {
1117           /* signal error */
1118           GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == th->notify_delay_task);
1119           peer_transmit_timeout (th, NULL);       
1120         }
1121       else
1122         {
1123           GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1124                       _("Connection with `%4s' failed, will keep trying for %llu ms to deliver message\n"),
1125                       GNUNET_i2s (peer),
1126                       GNUNET_TIME_absolute_get_remaining (th->timeout).value);
1127           /* try again in a bit */
1128           GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == th->notify_delay_task);
1129           th->notify_delay_task 
1130             = GNUNET_SCHEDULER_add_delayed (h->sched,
1131                                             GNUNET_NO,
1132                                             GNUNET_SCHEDULER_PRIORITY_KEEP,
1133                                             GNUNET_SCHEDULER_NO_TASK,
1134                                             CONNECT_RETRY_TIMEOUT,
1135                                             &try_connect_task,
1136                                             th);
1137         }
1138     }
1139   if (h->nc_cb != NULL)
1140     h->nd_cb (h->cls, peer);
1141   GNUNET_free (pos);
1142 }
1143
1144
1145 /**
1146  * Try again to connect to transport service.
1147  */
1148 static void
1149 reconnect (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1150 {
1151   struct GNUNET_TRANSPORT_Handle *h = cls;
1152   struct GNUNET_TRANSPORT_TransmitHandle *pos;
1153   struct NeighbourList *n;
1154
1155   /* Forget about all neighbours that we used to be connected
1156      to */
1157   while (NULL != (n = h->neighbours))
1158     remove_neighbour (h, &n->id);
1159 #if DEBUG_TRANSPORT
1160   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Connecting to transport service.\n");
1161 #endif
1162   GNUNET_assert (h->client == NULL);
1163   h->reconnect_task = GNUNET_SCHEDULER_NO_TASK;
1164   h->client = GNUNET_CLIENT_connect (h->sched, "transport", h->cfg);
1165   GNUNET_assert (h->client != NULL);
1166   /* make sure we don't send "START" twice,
1167      remove existing entry from queue (if present) */
1168   pos = h->connect_ready_head;
1169   while (pos != NULL)
1170     {
1171       if (pos->notify == &send_start)
1172         {
1173           if (pos->prev == NULL)
1174             h->connect_ready_head = pos->next;
1175           else
1176             pos->prev->next = pos->next;
1177           if (pos->next != NULL)
1178             pos->next->prev = pos->prev;
1179           GNUNET_assert (pos->neighbour == NULL);
1180           if (GNUNET_SCHEDULER_NO_TASK != pos->notify_delay_task)
1181             {
1182               GNUNET_SCHEDULER_cancel (h->sched,
1183                                        pos->notify_delay_task);
1184               pos->notify_delay_task = GNUNET_SCHEDULER_NO_TASK;
1185             }
1186           GNUNET_free (pos);
1187           break;
1188         }
1189       pos = pos->next;
1190     }
1191   schedule_control_transmit (h,
1192                              sizeof (struct GNUNET_MessageHeader),
1193                              GNUNET_YES,
1194                              GNUNET_TIME_UNIT_FOREVER_REL, 
1195                              &send_start, NULL);
1196   GNUNET_CLIENT_receive (h->client,
1197                          &demultiplexer, h, GNUNET_TIME_UNIT_FOREVER_REL);
1198 }
1199
1200
1201 /**
1202  * Function that will schedule the job that will try
1203  * to connect us again to the client.
1204  */
1205 static void
1206 schedule_reconnect (struct GNUNET_TRANSPORT_Handle *h)
1207 {
1208 #if DEBUG_TRANSPORT
1209   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1210               "Scheduling task to reconnect to transport service in %llu ms.\n",
1211               h->reconnect_delay.value);
1212 #endif
1213   GNUNET_assert (h->client == NULL);
1214   GNUNET_assert (h->reconnect_task == GNUNET_SCHEDULER_NO_TASK);
1215   h->reconnect_task
1216     = GNUNET_SCHEDULER_add_delayed (h->sched,
1217                                     GNUNET_NO,
1218                                     GNUNET_SCHEDULER_PRIORITY_DEFAULT,
1219                                     GNUNET_SCHEDULER_NO_TASK,
1220                                     h->reconnect_delay, &reconnect, h);
1221   h->reconnect_delay = GNUNET_TIME_UNIT_SECONDS;
1222 }
1223
1224
1225 /**
1226  * We are connected to the respective peer, check the
1227  * bandwidth limits and schedule the transmission.
1228  */
1229 static void schedule_request (struct GNUNET_TRANSPORT_TransmitHandle *th);
1230
1231
1232 /**
1233  * Function called by the scheduler when the timeout
1234  * for bandwidth availablility for the target
1235  * neighbour is reached.
1236  */
1237 static void
1238 transmit_ready (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1239 {
1240   struct GNUNET_TRANSPORT_TransmitHandle *th = cls;
1241
1242   th->notify_delay_task = GNUNET_SCHEDULER_NO_TASK;
1243   schedule_request (th);
1244 }
1245
1246
1247 /**
1248  * Remove the given transmit handle from the wait list.  Does NOT free
1249  * it.
1250  */
1251 static void
1252 remove_from_wait_list (struct GNUNET_TRANSPORT_TransmitHandle *th)
1253 {
1254   if (th->prev == NULL)
1255     th->handle->connect_wait_head = th->next;
1256   else
1257     th->prev->next = th->next;
1258   if (th->next != NULL)
1259     th->next->prev = th->prev;
1260 }
1261
1262
1263 /**
1264  * We are connected to the respective peer, check the
1265  * bandwidth limits and schedule the transmission.
1266  */
1267 static void
1268 schedule_request (struct GNUNET_TRANSPORT_TransmitHandle *th)
1269 {
1270   struct GNUNET_TRANSPORT_Handle *h;
1271   struct GNUNET_TIME_Relative duration;
1272   struct NeighbourList *n;
1273   uint64_t available;
1274
1275   h = th->handle;
1276   n = th->neighbour;
1277   if (th->notify_delay_task != GNUNET_SCHEDULER_NO_TASK)
1278     {
1279       GNUNET_SCHEDULER_cancel (h->sched, th->notify_delay_task);
1280       th->notify_delay_task = GNUNET_SCHEDULER_NO_TASK;
1281     }
1282   /* check outgoing quota */
1283   duration = GNUNET_TIME_absolute_get_duration (n->last_quota_update);
1284   if (duration.value > MIN_QUOTA_REFRESH_TIME)
1285     {
1286       update_quota (n);
1287       duration = GNUNET_TIME_absolute_get_duration (n->last_quota_update);
1288     }
1289   available = duration.value * n->quota_out;
1290   if (available < n->last_sent + th->notify_size)
1291     {
1292       /* calculate how much bandwidth we'd still need to
1293          accumulate and based on that how long we'll have
1294          to wait... */
1295       available = n->last_sent + th->notify_size - available;
1296       duration = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS,
1297                                                 available / n->quota_out);
1298       if (th->timeout.value <
1299           GNUNET_TIME_relative_to_absolute (duration).value)
1300         {
1301           /* signal timeout! */
1302 #if DEBUG_TRANSPORT
1303           GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1304                       "Would need %llu ms before bandwidth is available for delivery to `%4s', that is too long.  Signaling timeout.\n",
1305                       duration.value,
1306                       GNUNET_i2s(&th->target));
1307 #endif
1308           remove_from_wait_list (th);
1309           th->notify (th->notify_cls, 0, NULL);
1310           GNUNET_free (th);
1311           return;
1312         }
1313 #if DEBUG_TRANSPORT
1314       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1315                   "Need more bandwidth, delaying delivery to `%4s' by %llu ms\n",
1316                   GNUNET_i2s(&th->target),
1317                   duration.value);
1318 #endif
1319       th->notify_delay_task
1320         = GNUNET_SCHEDULER_add_delayed (h->sched,
1321                                         GNUNET_NO,
1322                                         GNUNET_SCHEDULER_PRIORITY_KEEP,
1323                                         GNUNET_SCHEDULER_NO_TASK,
1324                                         duration, &transmit_ready, th);
1325       return;
1326     }
1327 #if DEBUG_TRANSPORT
1328   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1329               "Bandwidth available for transmission to `%4s'\n",
1330               GNUNET_i2s (&n->id));
1331 #endif
1332   if (GNUNET_NO == n->transmit_ok)
1333     {
1334       /* we may be ready, but transport service is not;
1335          wait for SendOkMessage or timeout */
1336 #if DEBUG_TRANSPORT
1337       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1338                   "Need to wait for transport service `%s' message\n",
1339                   "SEND_OK");
1340 #endif
1341       th->notify_delay_task
1342         = GNUNET_SCHEDULER_add_delayed (h->sched,
1343                                         GNUNET_NO,
1344                                         GNUNET_SCHEDULER_PRIORITY_KEEP,
1345                                         GNUNET_SCHEDULER_NO_TASK,
1346                                         GNUNET_TIME_absolute_get_remaining
1347                                         (th->timeout), &peer_transmit_timeout, th);
1348       return;
1349     }
1350   n->transmit_ok = GNUNET_NO;
1351   remove_from_wait_list (th);
1352 #if DEBUG_TRANSPORT
1353   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Moving message for `%4s' to ready list\n",
1354               GNUNET_i2s(&n->id));
1355 #endif
1356   insert_transmit_handle (&h->connect_ready_head, th);
1357   if (GNUNET_NO == h->transmission_scheduled)
1358     schedule_transmission (h);
1359 }
1360
1361
1362 /**
1363  * Add neighbour to our list
1364  */
1365 static void
1366 add_neighbour (struct GNUNET_TRANSPORT_Handle *h,
1367                uint32_t quota_out,
1368                struct GNUNET_TIME_Relative latency,
1369                const struct GNUNET_PeerIdentity *pid)
1370 {
1371   struct NeighbourList *n;
1372   struct GNUNET_TRANSPORT_TransmitHandle *prev;
1373   struct GNUNET_TRANSPORT_TransmitHandle *pos;
1374   struct GNUNET_TRANSPORT_TransmitHandle *next;
1375
1376   /* check for duplicates */
1377   if (NULL != find_neighbour (h, pid))
1378     {
1379       GNUNET_break (0);
1380       return;
1381     }
1382 #if DEBUG_TRANSPORT
1383   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1384               "Creating entry for new neighbour `%4s'.\n", GNUNET_i2s (pid));
1385 #endif
1386   n = GNUNET_malloc (sizeof (struct NeighbourList));
1387   n->id = *pid;
1388   n->last_quota_update = GNUNET_TIME_absolute_get ();
1389   n->quota_out = quota_out;
1390   n->next = h->neighbours;
1391   n->transmit_ok = GNUNET_YES;
1392   h->neighbours = n;
1393   if (h->nc_cb != NULL)
1394     h->nc_cb (h->cls, &n->id, latency);
1395   prev = NULL;
1396   pos = h->connect_wait_head;
1397   while (pos != NULL)
1398     {
1399       next = pos->next;
1400       if (0 == memcmp (pid,
1401                        &pos->target, sizeof (struct GNUNET_PeerIdentity)))
1402         {
1403           pos->neighbour = n;
1404           GNUNET_assert (NULL == n->transmit_handle);
1405           n->transmit_handle = pos;
1406           if (prev == NULL)
1407             h->connect_wait_head = next;
1408           else
1409             prev->next = next;
1410           if (GNUNET_YES == n->received_ack)
1411             {
1412 #if DEBUG_TRANSPORT
1413               GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1414                           "Found pending request for `%4s' will trigger it now.\n",
1415                           GNUNET_i2s (&pos->target));
1416 #endif
1417               if (pos->notify_delay_task != GNUNET_SCHEDULER_NO_TASK)
1418                 {
1419                   GNUNET_SCHEDULER_cancel (h->sched, pos->notify_delay_task);
1420                   pos->notify_delay_task = GNUNET_SCHEDULER_NO_TASK;
1421                 }
1422               schedule_request (pos);
1423             }
1424           else
1425             {
1426 #if DEBUG_TRANSPORT
1427               GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1428                           "Found pending request for `%4s' but still need `%s' before proceeding.\n",
1429                           GNUNET_i2s (&pos->target),
1430                           "ACK");
1431 #endif
1432             }
1433           break;
1434         }
1435       prev = pos;
1436       pos = next;
1437     }
1438 }
1439
1440
1441 /**
1442  * Connect to the transport service.  Note that the connection may
1443  * complete (or fail) asynchronously.
1444  *
1445
1446  * @param sched scheduler to use
1447  * @param cfg configuration to use
1448  * @param cls closure for the callbacks
1449  * @param rec receive function to call
1450  * @param nc function to call on connect events
1451  * @param nd function to call on disconnect events
1452  */
1453 struct GNUNET_TRANSPORT_Handle *
1454 GNUNET_TRANSPORT_connect (struct GNUNET_SCHEDULER_Handle *sched,
1455                           const struct GNUNET_CONFIGURATION_Handle *cfg,
1456                           void *cls,
1457                           GNUNET_TRANSPORT_ReceiveCallback rec,
1458                           GNUNET_TRANSPORT_NotifyConnect nc,
1459                           GNUNET_TRANSPORT_NotifyDisconnect nd)
1460 {
1461   struct GNUNET_TRANSPORT_Handle *ret;
1462
1463   GNUNET_ARM_start_services (cfg, sched, "peerinfo", "transport", NULL);
1464   ret = GNUNET_malloc (sizeof (struct GNUNET_TRANSPORT_Handle));
1465   ret->sched = sched;
1466   ret->cfg = cfg;
1467   ret->cls = cls;
1468   ret->rec = rec;
1469   ret->nc_cb = nc;
1470   ret->nd_cb = nd;
1471   ret->reconnect_delay = GNUNET_TIME_UNIT_ZERO;
1472   schedule_reconnect (ret);
1473   return ret;
1474 }
1475
1476
1477 /**
1478  * Disconnect from the transport service.
1479  */
1480 void
1481 GNUNET_TRANSPORT_disconnect (struct GNUNET_TRANSPORT_Handle *handle)
1482 {
1483   struct GNUNET_TRANSPORT_TransmitHandle *th;
1484   struct NeighbourList *n;
1485   struct HelloWaitList *hwl;
1486   struct GNUNET_CLIENT_Connection *client;
1487
1488 #if DEBUG_TRANSPORT
1489   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 
1490               "Transport disconnect called!\n");
1491 #endif
1492   while (NULL != (th = handle->connect_ready_head))
1493     {
1494       handle->connect_ready_head = th->next;
1495       if (th->notify_delay_task != GNUNET_SCHEDULER_NO_TASK)
1496         {
1497           GNUNET_SCHEDULER_cancel (handle->sched, th->notify_delay_task);
1498           th->notify_delay_task = GNUNET_SCHEDULER_NO_TASK;
1499         }
1500       th->notify (th->notify_cls, 0, NULL);
1501       GNUNET_free (th);
1502     }
1503   while (NULL != (th = handle->connect_wait_head))
1504     {
1505       handle->connect_wait_head = th->next;
1506       if (th->notify_delay_task != GNUNET_SCHEDULER_NO_TASK)
1507         {
1508           GNUNET_SCHEDULER_cancel (handle->sched, th->notify_delay_task);
1509           th->notify_delay_task = GNUNET_SCHEDULER_NO_TASK;
1510         }
1511       th->notify (th->notify_cls, 0, NULL);
1512       GNUNET_free (th);
1513     }
1514   while (NULL != (n = handle->neighbours))
1515     {
1516       handle->neighbours = n->next;
1517       GNUNET_free (n);
1518     }
1519   while (NULL != (hwl = handle->hwl_head))
1520     {
1521       handle->hwl_head = hwl->next;
1522       GNUNET_SCHEDULER_cancel (handle->sched, hwl->task);
1523       GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1524                   _("Disconnect while trying to obtain `%s' from transport service.\n"),
1525                   "HELLO");
1526       if (hwl->rec != NULL)
1527         hwl->rec (hwl->rec_cls, GNUNET_TIME_UNIT_ZERO, NULL, NULL);
1528       GNUNET_free (hwl);
1529     }
1530   if (handle->reconnect_task != GNUNET_SCHEDULER_NO_TASK)
1531     {
1532       GNUNET_SCHEDULER_cancel (handle->sched, handle->reconnect_task);
1533       handle->reconnect_task = GNUNET_SCHEDULER_NO_TASK;
1534     }
1535   GNUNET_free_non_null (handle->my_hello);
1536   handle->my_hello = NULL;
1537   GNUNET_ARM_stop_services (handle->cfg, handle->sched, "transport", "peerinfo", NULL);
1538   if (NULL != (client = handle->client))
1539     {
1540 #if DEBUG_TRANSPORT
1541       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1542                   "Disconnecting from transport service for good.\n");
1543 #endif
1544       handle->client = NULL;
1545       GNUNET_CLIENT_disconnect (client);
1546     }
1547   if (client == NULL)
1548     GNUNET_free (handle);
1549 }
1550
1551
1552 /**
1553  * Type of a function to call when we receive a message
1554  * from the service.
1555  *
1556  * @param cls closure
1557  * @param msg message received, NULL on timeout or fatal error
1558  */
1559 static void
1560 demultiplexer (void *cls, const struct GNUNET_MessageHeader *msg)
1561 {
1562   struct GNUNET_TRANSPORT_Handle *h = cls;
1563   const struct DisconnectInfoMessage *dim;
1564   const struct ConnectInfoMessage *cim;
1565   const struct InboundMessage *im;
1566   const struct GNUNET_MessageHeader *imm;
1567   const struct SendOkMessage *okm;
1568   struct HelloWaitList *hwl;
1569   struct NeighbourList *n;
1570   struct GNUNET_PeerIdentity me;
1571   struct GNUNET_TRANSPORT_TransmitHandle *th;
1572   uint16_t size;
1573
1574   if ((msg == NULL) || (h->client == NULL))
1575     {
1576       if (h->client != NULL)
1577         {
1578 #if DEBUG_TRANSPORT
1579           GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1580                       "Error receiving from transport service, disconnecting temporarily.\n");
1581 #endif
1582           if (h->network_handle != NULL)
1583             {
1584               GNUNET_CLIENT_notify_transmit_ready_cancel (h->network_handle);
1585               h->network_handle = NULL;
1586               h->transmission_scheduled = GNUNET_NO;
1587               th = h->connect_ready_head;
1588               /* add timeout again, we cancelled the transmit_ready task! */
1589               GNUNET_assert (th->notify_delay_task == GNUNET_SCHEDULER_NO_TASK);
1590               th->notify_delay_task 
1591                 = GNUNET_SCHEDULER_add_delayed (h->sched,
1592                                                 GNUNET_NO,
1593                                                 GNUNET_SCHEDULER_PRIORITY_KEEP,
1594                                                 GNUNET_SCHEDULER_NO_TASK,
1595                                                 GNUNET_TIME_absolute_get_remaining(th->timeout),
1596                                                 &peer_transmit_timeout, 
1597                                                 th);    
1598             }
1599           GNUNET_CLIENT_disconnect (h->client);
1600           h->client = NULL;
1601           schedule_reconnect (h);
1602         }
1603       else
1604         {
1605           /* shutdown initiated from 'GNUNET_TRANSPORT_disconnect',
1606              finish clean up work! */
1607           GNUNET_free (h);
1608         }
1609       return;
1610     }
1611   GNUNET_CLIENT_receive (h->client,
1612                          &demultiplexer, h, GNUNET_TIME_UNIT_FOREVER_REL);
1613   size = ntohs (msg->size);
1614   switch (ntohs (msg->type))
1615     {
1616     case GNUNET_MESSAGE_TYPE_HELLO:
1617       if (GNUNET_OK !=
1618           GNUNET_HELLO_get_id ((const struct GNUNET_HELLO_Message *) msg,
1619                                &me))
1620         {
1621           GNUNET_break (0);
1622           break;
1623         }
1624 #if DEBUG_TRANSPORT
1625       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1626                   "Receiving (my own) `%s' message, I am `%4s'.\n",
1627                   "HELLO", GNUNET_i2s (&me));
1628 #endif
1629       GNUNET_free_non_null (h->my_hello);
1630       h->my_hello = NULL;
1631       if (size < sizeof (struct GNUNET_MessageHeader))
1632         {
1633           GNUNET_break (0);
1634           break;
1635         }
1636       h->my_hello = GNUNET_malloc (size);
1637       memcpy (h->my_hello, msg, size);
1638       while (NULL != (hwl = h->hwl_head))
1639         {
1640           h->hwl_head = hwl->next;
1641           GNUNET_SCHEDULER_cancel (h->sched, hwl->task);
1642           GNUNET_TRANSPORT_get_hello (h,
1643                                       GNUNET_TIME_UNIT_ZERO,
1644                                       hwl->rec, hwl->rec_cls);
1645           GNUNET_free (hwl);
1646         }
1647       break;
1648     case GNUNET_MESSAGE_TYPE_TRANSPORT_CONNECT:
1649       if (size != sizeof (struct ConnectInfoMessage))
1650         {
1651           GNUNET_break (0);
1652           break;
1653         }
1654       cim = (const struct ConnectInfoMessage *) msg;
1655 #if DEBUG_TRANSPORT
1656       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1657                   "Receiving `%s' message for `%4s'.\n",
1658                   "CONNECT", GNUNET_i2s (&cim->id));
1659 #endif
1660       add_neighbour (h,
1661                      ntohl (cim->quota_out),
1662                      GNUNET_TIME_relative_ntoh (cim->latency), &cim->id);
1663       break;
1664     case GNUNET_MESSAGE_TYPE_TRANSPORT_DISCONNECT:
1665       if (size != sizeof (struct DisconnectInfoMessage))
1666         {
1667           GNUNET_break (0);
1668           break;
1669         }
1670       dim = (const struct DisconnectInfoMessage *) msg;
1671 #if DEBUG_TRANSPORT
1672       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1673                   "Receiving `%s' message for `%4s'.\n",
1674                   "DISCONNECT", GNUNET_i2s (&dim->peer));
1675 #endif
1676       remove_neighbour (h, &dim->peer);
1677       break;
1678     case GNUNET_MESSAGE_TYPE_TRANSPORT_SEND_OK:
1679       if (size != sizeof (struct SendOkMessage))
1680         {
1681           GNUNET_break (0);
1682           break;
1683         }
1684       okm = (const struct SendOkMessage *) msg;
1685 #if DEBUG_TRANSPORT
1686       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1687                   "Receiving `%s' message, transmission %s.\n", "SEND_OK",
1688                   ntohl(okm->success) == GNUNET_OK ? "succeeded" : "failed");
1689 #endif
1690       n = find_neighbour (h, &okm->peer);
1691       GNUNET_assert (n != NULL);
1692       n->transmit_ok = GNUNET_YES;
1693       if (n->transmit_handle != NULL)
1694         {
1695 #if DEBUG_TRANSPORT
1696           GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1697                       "Processing pending message for `%4s'\n",
1698                       GNUNET_i2s(&n->id));
1699 #endif
1700           GNUNET_SCHEDULER_cancel (h->sched,
1701                                    n->transmit_handle->notify_delay_task);
1702           n->transmit_handle->notify_delay_task =
1703             GNUNET_SCHEDULER_NO_TASK;
1704           GNUNET_assert (GNUNET_YES == n->received_ack);
1705           schedule_request (n->transmit_handle);
1706         }
1707       break;
1708     case GNUNET_MESSAGE_TYPE_TRANSPORT_RECV:
1709 #if DEBUG_TRANSPORT
1710       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1711                   "Receiving `%s' message.\n", "RECV");
1712 #endif
1713       if (size <
1714           sizeof (struct InboundMessage) +
1715           sizeof (struct GNUNET_MessageHeader))
1716         {
1717           GNUNET_break (0);
1718           break;
1719         }
1720       im = (const struct InboundMessage *) msg;
1721       imm = (const struct GNUNET_MessageHeader *) &im[1];
1722       if (ntohs (imm->size) + sizeof (struct InboundMessage) != size)
1723         {
1724           GNUNET_break (0);
1725           break;
1726         }
1727       switch (ntohs (imm->type))
1728         {
1729         case GNUNET_MESSAGE_TYPE_TRANSPORT_ACK:
1730 #if DEBUG_TRANSPORT
1731           GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1732                       "Receiving `%s' message from `%4s'.\n",
1733                       "ACK", GNUNET_i2s (&im->peer));
1734 #endif
1735           n = find_neighbour (h, &im->peer);
1736           if (n == NULL)
1737             {
1738               GNUNET_break (0);
1739               break;
1740             }
1741           if (n->received_ack == GNUNET_NO)
1742             {
1743               n->received_ack = GNUNET_YES;
1744               if (NULL != n->transmit_handle)
1745                 {
1746 #if DEBUG_TRANSPORT
1747                   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1748                               "Peer connected, scheduling delayed message for deliverery now.\n");
1749 #endif
1750                   schedule_request (n->transmit_handle);
1751                 }
1752             }
1753           break;
1754         default:
1755 #if DEBUG_TRANSPORT
1756           GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1757                       "Received message of type %u from `%4s'.\n",
1758                       ntohs (imm->type), GNUNET_i2s (&im->peer));
1759 #endif
1760           if (h->rec != NULL)
1761             h->rec (h->cls,
1762                     GNUNET_TIME_relative_ntoh (im->latency), &im->peer, imm);
1763           break;
1764         }
1765       break;
1766     default:
1767       GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1768                   _
1769                   ("Received unexpected message of type %u in %s:%u\n"),
1770                   ntohs (msg->type), __FILE__, __LINE__);
1771       GNUNET_break (0);
1772       break;
1773     }
1774 }
1775
1776
1777 struct ClientTransmitWrapper
1778 {
1779   GNUNET_CONNECTION_TransmitReadyNotify notify;
1780   void *notify_cls;
1781   struct GNUNET_TRANSPORT_TransmitHandle *th;
1782 };
1783
1784
1785 /**
1786  * Transmit message of a client destined for another
1787  * peer to the service.
1788  */
1789 static size_t
1790 client_notify_wrapper (void *cls, size_t size, void *buf)
1791 {
1792   struct ClientTransmitWrapper *ctw = cls;
1793   struct OutboundMessage *obm;
1794   struct GNUNET_MessageHeader *hdr;
1795   size_t ret;
1796
1797   if (size == 0)
1798     {
1799 #if DEBUG_TRANSPORT
1800       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1801                   "Transmission request could not be satisfied.\n");
1802 #endif
1803       ret = ctw->notify (ctw->notify_cls, 0, NULL);
1804       GNUNET_assert (ret == 0);
1805       GNUNET_free (ctw);
1806       return 0;
1807     }
1808   GNUNET_assert (size >= sizeof (struct OutboundMessage));
1809   obm = buf;
1810   ret = ctw->notify (ctw->notify_cls,
1811                      size - sizeof (struct OutboundMessage),
1812                      (void *) &obm[1]);
1813   if (ret == 0)
1814     {
1815       /* Need to reset flag, no SEND means no SEND_OK! */
1816       ctw->th->neighbour->transmit_ok = GNUNET_YES;
1817       GNUNET_free (ctw);
1818       return 0;
1819     }
1820   GNUNET_assert (ret >= sizeof (struct GNUNET_MessageHeader));
1821   hdr = (struct GNUNET_MessageHeader *) &obm[1];
1822   GNUNET_assert (ntohs (hdr->size) == ret);
1823   GNUNET_assert (ret + sizeof (struct OutboundMessage) <
1824                  GNUNET_SERVER_MAX_MESSAGE_SIZE);
1825 #if DEBUG_TRANSPORT
1826   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1827               "Transmitting `%s' message with data for `%4s'\n",
1828               "SEND", GNUNET_i2s (&ctw->th->target));
1829 #endif
1830   ret += sizeof (struct OutboundMessage);
1831   obm->header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_SEND);
1832   obm->header.size = htons (ret);
1833   obm->priority = htonl (ctw->th->priority);
1834   obm->peer = ctw->th->target;
1835   GNUNET_free (ctw);
1836   return ret;
1837 }
1838
1839
1840
1841 /**
1842  * Check if we could queue a message of the given size for
1843  * transmission.  The transport service will take both its
1844  * internal buffers and bandwidth limits imposed by the
1845  * other peer into consideration when answering this query.
1846  *
1847  * @param handle connection to transport service
1848  * @param target who should receive the message
1849  * @param size how big is the message we want to transmit?
1850  * @param priority how important is the message?
1851  * @param timeout after how long should we give up (and call
1852  *        notify with buf NULL and size 0)?
1853  * @param notify function to call when we are ready to
1854  *        send such a message
1855  * @param notify_cls closure for notify
1856  * @return NULL if someone else is already waiting to be notified
1857  *         non-NULL if the notify callback was queued (can be used to cancel
1858  *         using GNUNET_TRANSPORT_notify_transmit_ready_cancel)
1859  */
1860 struct GNUNET_TRANSPORT_TransmitHandle *
1861 GNUNET_TRANSPORT_notify_transmit_ready (struct GNUNET_TRANSPORT_Handle
1862                                         *handle,
1863                                         const struct GNUNET_PeerIdentity
1864                                         *target, size_t size,
1865                                         unsigned int priority,
1866                                         struct GNUNET_TIME_Relative timeout,
1867                                         GNUNET_CONNECTION_TransmitReadyNotify
1868                                         notify, void *notify_cls)
1869 {
1870   struct GNUNET_TRANSPORT_TransmitHandle *pos;
1871   struct GNUNET_TRANSPORT_TransmitHandle *th;
1872   struct NeighbourList *n;
1873   struct ClientTransmitWrapper *ctw;
1874
1875   if (size + sizeof (struct OutboundMessage) >=
1876       GNUNET_SERVER_MAX_MESSAGE_SIZE)
1877     {
1878       GNUNET_break (0);
1879       return NULL;
1880     }
1881 #if DEBUG_TRANSPORT
1882   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1883               "Asking transport service for transmission of %u bytes to peer `%4s'.\n",
1884               size, GNUNET_i2s (target));
1885 #endif
1886   n = find_neighbour (handle, target);
1887   if ( (n != NULL) &&
1888        (n->transmit_handle != NULL) )
1889     return NULL; /* already have a request pending for this peer! */
1890   ctw = GNUNET_malloc (sizeof (struct ClientTransmitWrapper));
1891   th = GNUNET_malloc (sizeof (struct GNUNET_TRANSPORT_TransmitHandle));
1892   ctw->notify = notify;
1893   ctw->notify_cls = notify_cls;
1894   ctw->th = th;
1895   th->handle = handle;
1896   th->neighbour = n;
1897   th->target = *target;
1898   th->notify = &client_notify_wrapper;
1899   th->notify_cls = ctw;
1900   th->timeout = GNUNET_TIME_relative_to_absolute (timeout);
1901   th->notify_size = size + sizeof (struct OutboundMessage);
1902   th->priority = priority;
1903   if (NULL == n)
1904     {
1905       pos = handle->connect_wait_head;
1906       while (pos != NULL)
1907         {
1908           GNUNET_assert (0 != memcmp (target,
1909                                       &pos->target,
1910                                       sizeof (struct GNUNET_PeerIdentity)));
1911           pos = pos->next;
1912         }
1913 #if DEBUG_TRANSPORT
1914       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1915                   "Will now try to connect to `%4s'.\n", GNUNET_i2s (target));
1916 #endif
1917       try_connect (th);
1918       return th;
1919     }
1920
1921 #if DEBUG_TRANSPORT
1922   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1923               "Transmission request queued for transmission to transport service.\n");
1924 #endif
1925   GNUNET_assert (NULL == n->transmit_handle);
1926   n->transmit_handle = th;
1927   if (GNUNET_YES != n->received_ack)
1928     {
1929 #if DEBUG_TRANSPORT
1930       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1931                   "Connection to `%4s' is not yet confirmed connected, scheduling timeout (%llu ms) only.\n",
1932                   GNUNET_i2s (target), timeout.value);
1933 #endif
1934       th->notify_delay_task
1935         = GNUNET_SCHEDULER_add_delayed (handle->sched,
1936                                         GNUNET_NO,
1937                                         GNUNET_SCHEDULER_PRIORITY_KEEP,
1938                                         GNUNET_SCHEDULER_NO_TASK,
1939                                         timeout, &peer_transmit_timeout, th);
1940       return th;
1941     }
1942   
1943 #if DEBUG_TRANSPORT
1944   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1945               "Peer `%4s' is ready to receive, scheduling message for delivery now.\n",
1946               GNUNET_i2s (target));
1947 #endif
1948   schedule_request (th);
1949   return th;
1950 }
1951
1952
1953 /**
1954  * Cancel the specified transmission-ready notification.
1955  */
1956 void
1957 GNUNET_TRANSPORT_notify_transmit_ready_cancel (struct
1958                                                GNUNET_TRANSPORT_TransmitHandle
1959                                                *th)
1960 {
1961   struct GNUNET_TRANSPORT_Handle *h;
1962
1963 #if DEBUG_TRANSPORT
1964   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1965               "Transmission request of %u bytes to `%4s' was cancelled.\n",
1966               th->notify_size - sizeof(struct OutboundMessage),
1967               GNUNET_i2s (&th->target));
1968 #endif
1969   GNUNET_assert (th->notify == &client_notify_wrapper);
1970   remove_from_any_list (th);
1971   h = th->handle;
1972   if ((h->connect_ready_head == NULL) && (h->network_handle != NULL))
1973     {
1974       GNUNET_CLIENT_notify_transmit_ready_cancel (h->network_handle);
1975       h->network_handle = NULL;
1976       h->transmission_scheduled = GNUNET_NO;
1977     }
1978   GNUNET_free (th->notify_cls);
1979   GNUNET_assert (th->notify_delay_task == GNUNET_SCHEDULER_NO_TASK);
1980   GNUNET_free (th);
1981 }
1982
1983
1984 /* end of transport_api.c */