rename
[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_CONNECTION_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                                                            &transport_notify_ready,
481                                                            h);
482   GNUNET_assert (NULL != h->network_handle);
483 }
484
485
486 /**
487  * Insert the given transmit handle in the given sorted
488  * doubly linked list based on timeout.
489  *
490  * @param head pointer to the head of the linked list
491  * @param th element to insert into the list
492  */
493 static void
494 insert_transmit_handle (struct GNUNET_TRANSPORT_TransmitHandle **head,
495                         struct GNUNET_TRANSPORT_TransmitHandle *th)
496 {
497   struct GNUNET_TRANSPORT_TransmitHandle *pos;
498   struct GNUNET_TRANSPORT_TransmitHandle *prev;
499
500   pos = *head;
501   prev = NULL;
502   while ((pos != NULL) && (pos->timeout.value < th->timeout.value))
503     {
504       prev = pos;
505       pos = pos->next;
506     }
507   if (prev == NULL)
508     {
509       th->next = *head;
510       if (th->next != NULL)
511         th->next->prev = th;
512       *head = th;
513     }
514   else
515     {
516       th->next = pos;
517       th->prev = prev;
518       prev->next = th;
519       if (pos != NULL)
520         pos->prev = th;
521     }
522 }
523
524
525 /**
526  * Cancel a pending notify delay task (if pending) and also remove the
527  * given transmit handle from whatever list is on.
528  *
529  * @param th handle for the transmission request to manipulate
530  */
531 static void
532 remove_from_any_list (struct GNUNET_TRANSPORT_TransmitHandle *th)
533 {
534   struct GNUNET_TRANSPORT_Handle *h;
535
536   h = th->handle;
537   if (th->notify_delay_task != GNUNET_SCHEDULER_NO_TASK)
538     {
539       GNUNET_SCHEDULER_cancel (h->sched, th->notify_delay_task);
540       th->notify_delay_task = GNUNET_SCHEDULER_NO_TASK;
541     }
542   if (th->prev == NULL)
543     {
544       if (th == h->connect_wait_head)
545         h->connect_wait_head = th->next;
546       else
547         h->connect_ready_head = th->next;
548     }
549   else
550     {
551       th->prev->next = th->next;
552     }
553   if (th->next != NULL)
554     th->next->prev = th->prev;
555 }
556
557
558 /**
559  * Schedule a request to connect to the given
560  * neighbour (and if successful, add the specified
561  * handle to the wait list).
562  *
563  * @param th handle for a request to transmit once we
564  *        have connected
565  */
566 static void
567 try_connect (struct GNUNET_TRANSPORT_TransmitHandle *th);
568
569
570 /**
571  * Called when our transmit request timed out before any transport
572  * reported success connecting to the desired peer or before the
573  * transport was ready to receive.  Signal error and free
574  * TransmitHandle.
575  */
576 static void
577 peer_transmit_timeout (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
578 {
579   struct GNUNET_TRANSPORT_TransmitHandle *th = cls;
580
581   th->notify_delay_task = GNUNET_SCHEDULER_NO_TASK;
582   if (th->neighbour != NULL)
583     th->neighbour->transmit_handle = NULL;
584 #if DEBUG_TRANSPORT
585   GNUNET_log (GNUNET_ERROR_TYPE_INFO, 
586               "Request for transmission to peer `%s' timed out.\n",
587               GNUNET_i2s(&th->target));
588 #endif
589   remove_from_any_list (th);
590   th->notify (th->notify_cls, 0, NULL);
591   GNUNET_free (th);
592 }
593
594
595
596
597 /**
598  * Queue control request for transmission to the transport
599  * service.
600  *
601  * @param size number of bytes to be transmitted
602  * @param at_head request must be added to the head of the queue
603  *        (otherwise request will be appended)
604  * @param timeout how long this transmission can wait (at most)
605  * @param notify function to call to get the content
606  * @param notify_cls closure for notify
607  */
608 static void
609 schedule_control_transmit (struct GNUNET_TRANSPORT_Handle *h,
610                            size_t size,
611                            int at_head,
612                            struct GNUNET_TIME_Relative timeout,
613                            GNUNET_CONNECTION_TransmitReadyNotify notify,
614                            void *notify_cls)
615 {
616   struct GNUNET_TRANSPORT_TransmitHandle *th;
617
618   th = GNUNET_malloc (sizeof (struct GNUNET_TRANSPORT_TransmitHandle));
619   th->handle = h;
620   th->notify = notify;
621   th->notify_cls = notify_cls;
622   th->timeout = GNUNET_TIME_relative_to_absolute (timeout);
623   th->notify_size = size;
624   th->notify_delay_task 
625     = GNUNET_SCHEDULER_add_delayed (h->sched,
626                                     GNUNET_NO,
627                                     GNUNET_SCHEDULER_PRIORITY_KEEP,
628                                     GNUNET_SCHEDULER_NO_TASK,
629                                     timeout,
630                                     &peer_transmit_timeout, th);    
631   if (at_head)
632     {
633       th->next = h->connect_ready_head;
634       h->connect_ready_head = th;
635       if (th->next != NULL)
636         th->next->prev = th;
637     }
638   else
639     {
640       insert_transmit_handle (&h->connect_ready_head, th);
641     }
642   if (GNUNET_NO == h->transmission_scheduled)
643     schedule_transmission (h);
644 }
645
646
647 /**
648  * Update the quota values for the given neighbour now.
649  */
650 static void
651 update_quota (struct NeighbourList *n)
652 {
653   struct GNUNET_TIME_Relative delta;
654   uint64_t allowed;
655   uint64_t remaining;
656
657   delta = GNUNET_TIME_absolute_get_duration (n->last_quota_update);
658   allowed = delta.value * n->quota_out;
659   if (n->last_sent < allowed)
660     {
661       remaining = allowed - n->last_sent;
662       if (n->quota_out > 0)
663         remaining /= n->quota_out;
664       else
665         remaining = 0;
666       if (remaining > MAX_BANDWIDTH_CARRY)
667         remaining = MAX_BANDWIDTH_CARRY;
668       n->last_sent = 0;
669       n->last_quota_update = GNUNET_TIME_absolute_get ();
670       n->last_quota_update.value -= remaining;
671     }
672   else
673     {
674       n->last_sent -= allowed;
675       n->last_quota_update = GNUNET_TIME_absolute_get ();
676     }
677 }
678
679
680 struct SetQuotaContext
681 {
682   struct GNUNET_TRANSPORT_Handle *handle;
683
684   struct GNUNET_PeerIdentity target;
685
686   GNUNET_SCHEDULER_Task cont;
687
688   void *cont_cls;
689
690   struct GNUNET_TIME_Absolute timeout;
691
692   uint32_t quota_in;
693 };
694
695
696 static size_t
697 send_set_quota (void *cls, size_t size, void *buf)
698 {
699   struct SetQuotaContext *sqc = cls;
700   struct QuotaSetMessage *msg;
701
702   if (buf == NULL)
703     {
704       GNUNET_SCHEDULER_add_continuation (sqc->handle->sched,
705                                          GNUNET_NO,
706                                          sqc->cont,
707                                          sqc->cont_cls,
708                                          GNUNET_SCHEDULER_REASON_TIMEOUT);
709       GNUNET_free (sqc);
710       return 0;
711     }
712 #if DEBUG_TRANSPORT
713   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
714               "Transmitting `%s' request with respect to `%4s'.\n",
715               "SET_QUOTA", GNUNET_i2s (&sqc->target));
716 #endif
717   GNUNET_assert (size >= sizeof (struct QuotaSetMessage));
718   msg = buf;
719   msg->header.size = htons (sizeof (struct QuotaSetMessage));
720   msg->header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_SET_QUOTA);
721   msg->quota_in = htonl (sqc->quota_in);
722   memcpy (&msg->peer, &sqc->target, sizeof (struct GNUNET_PeerIdentity));
723   if (sqc->cont != NULL)
724     GNUNET_SCHEDULER_add_continuation (sqc->handle->sched,
725                                        GNUNET_NO,
726                                        sqc->cont,
727                                        sqc->cont_cls,
728                                        GNUNET_SCHEDULER_REASON_PREREQ_DONE);
729   GNUNET_free (sqc);
730   return sizeof (struct QuotaSetMessage);
731 }
732
733
734 /**
735  * Set the share of incoming bandwidth for the given
736  * peer to the specified amount.
737  *
738  * @param handle connection to transport service
739  * @param target who's bandwidth quota is being changed
740  * @param quota_in incoming bandwidth quota in bytes per ms; 0 can
741  *        be used to force all traffic to be discarded
742  * @param quota_out outgoing bandwidth quota in bytes per ms; 0 can
743  *        be used to force all traffic to be discarded
744  * @param timeout how long to wait until signaling failure if
745  *        we can not communicate the quota change
746  * @param cont continuation to call when done, will be called
747  *        either with reason "TIMEOUT" or with reason "PREREQ_DONE"
748  * @param cont_cls closure for continuation
749  */
750 void
751 GNUNET_TRANSPORT_set_quota (struct GNUNET_TRANSPORT_Handle *handle,
752                             const struct GNUNET_PeerIdentity *target,
753                             uint32_t quota_in,
754                             uint32_t quota_out,
755                             struct GNUNET_TIME_Relative timeout,
756                             GNUNET_SCHEDULER_Task cont, void *cont_cls)
757 {
758   struct NeighbourList *n;
759   struct SetQuotaContext *sqc;
760
761   n = find_neighbour (handle, target);
762   if (n != NULL)
763     {
764       update_quota (n);
765       if (n->quota_out < quota_out)
766         n->last_quota_update = GNUNET_TIME_absolute_get ();
767       n->quota_out = quota_out;
768     }
769   sqc = GNUNET_malloc (sizeof (struct SetQuotaContext));
770   sqc->handle = handle;
771   sqc->target = *target;
772   sqc->cont = cont;
773   sqc->cont_cls = cont_cls;
774   sqc->timeout = GNUNET_TIME_relative_to_absolute (timeout);
775   sqc->quota_in = quota_in;
776   schedule_control_transmit (handle,
777                              sizeof (struct QuotaSetMessage),
778                              GNUNET_NO, timeout, &send_set_quota, sqc);
779 }
780
781
782 /**
783  * A "get_hello" request has timed out.  Signal the client
784  * and clean up.
785  */
786 static void
787 hello_wait_timeout (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
788 {
789   struct HelloWaitList *hwl = cls;
790   struct HelloWaitList *pos;
791   struct HelloWaitList *prev;
792
793   hwl->task = GNUNET_SCHEDULER_NO_TASK;
794   if (GNUNET_TIME_absolute_get_remaining (hwl->timeout).value > 0)
795     {
796 #if DEBUG_TRANSPORT
797       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
798                   _("First attempt to obtain `%s' from transport service failed, will try again for %llums.\n"),
799                   "HELLO",
800                   GNUNET_TIME_absolute_get_remaining (hwl->timeout).value);
801 #endif
802       hwl->task = GNUNET_SCHEDULER_add_delayed (hwl->handle->sched,
803                                                 GNUNET_YES,
804                                                 GNUNET_SCHEDULER_PRIORITY_KEEP,
805                                                 GNUNET_SCHEDULER_NO_TASK,
806                                                 GNUNET_TIME_absolute_get_remaining (hwl->timeout),
807                                                 &hello_wait_timeout, hwl);
808       return;      
809     }
810   /* signal timeout */
811   GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
812               _("Timeout trying to obtain `%s' from transport service.\n"),
813               "HELLO");
814   prev = NULL;
815   pos = hwl->handle->hwl_head;
816   while (pos != hwl)
817     {
818       GNUNET_assert (pos != NULL);
819       prev = pos;
820       pos = pos->next;
821     }
822   if (prev == NULL)
823     hwl->handle->hwl_head = hwl->next;
824   else
825     prev->next = hwl->next;
826   if (hwl->rec != NULL)
827     hwl->rec (hwl->rec_cls, GNUNET_TIME_UNIT_ZERO, NULL, NULL);
828   GNUNET_free (hwl);
829 }
830
831
832 /**
833  * Obtain the HELLO message for this peer.
834  *
835  * @param handle connection to transport service
836  * @param timeout how long to wait for the HELLO
837  * @param rec function to call with the HELLO, sender will be our peer
838  *            identity; message and sender will be NULL on timeout
839  *            (handshake with transport service pending/failed).
840  *             cost estimate will be 0.
841  * @param rec_cls closure for rec
842  */
843 void
844 GNUNET_TRANSPORT_get_hello (struct GNUNET_TRANSPORT_Handle *handle,
845                             struct GNUNET_TIME_Relative timeout,
846                             GNUNET_TRANSPORT_ReceiveCallback rec,
847                             void *rec_cls)
848 {
849   struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded pk;
850   struct GNUNET_PeerIdentity me;
851   struct HelloWaitList *hwl;
852
853   if (handle->my_hello == NULL)
854     {
855       hwl = GNUNET_malloc (sizeof (struct HelloWaitList));
856       hwl->next = handle->hwl_head;
857       handle->hwl_head = hwl;
858       hwl->handle = handle;
859       hwl->rec = rec;
860       hwl->rec_cls = rec_cls;
861       hwl->timeout = GNUNET_TIME_relative_to_absolute (timeout);
862       hwl->task = GNUNET_SCHEDULER_add_delayed (handle->sched,
863                                                 GNUNET_YES,
864                                                 GNUNET_SCHEDULER_PRIORITY_KEEP,
865                                                 GNUNET_SCHEDULER_NO_TASK,
866                                                 timeout,
867                                                 &hello_wait_timeout, hwl);
868       return;
869     }
870   GNUNET_assert (GNUNET_OK == GNUNET_HELLO_get_key (handle->my_hello, &pk));
871   GNUNET_CRYPTO_hash (&pk,
872                       sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
873                       &me.hashPubKey);
874
875   rec (rec_cls,
876        GNUNET_TIME_UNIT_ZERO,
877        &me, (const struct GNUNET_MessageHeader *) handle->my_hello);
878 }
879
880
881 static size_t
882 send_hello (void *cls, size_t size, void *buf)
883 {
884   struct GNUNET_MessageHeader *hello = cls;
885   uint16_t msize;
886
887   if (buf == NULL)
888     {
889 #if DEBUG_TRANSPORT
890       GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
891                   "Timeout while trying to transmit `%s' request.\n",
892                   "HELLO");
893 #endif
894       GNUNET_free (hello);
895       return 0;
896     }
897 #if DEBUG_TRANSPORT
898   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
899               "Transmitting `%s' request.\n", "HELLO");
900 #endif
901   msize = ntohs (hello->size);
902   GNUNET_assert (size >= msize);
903   memcpy (buf, hello, msize);
904   GNUNET_free (hello);
905   return msize;
906 }
907
908
909 /**
910  * Offer the transport service the HELLO of another peer.  Note that
911  * the transport service may just ignore this message if the HELLO is
912  * malformed or useless due to our local configuration.
913  *
914  * @param handle connection to transport service
915  * @param hello the hello message
916  */
917 void
918 GNUNET_TRANSPORT_offer_hello (struct GNUNET_TRANSPORT_Handle *handle,
919                               const struct GNUNET_MessageHeader *hello)
920 {
921   struct GNUNET_MessageHeader *hc;
922   uint16_t size;
923
924   if (handle->client == NULL)
925     {
926 #if DEBUG_TRANSPORT
927       GNUNET_log (GNUNET_ERROR_TYPE_INFO,
928                   "Not connected to transport service, dropping offered HELLO\n");
929 #endif
930       return;
931     }
932   GNUNET_break (ntohs (hello->type) == GNUNET_MESSAGE_TYPE_HELLO);
933   size = ntohs (hello->size);
934   GNUNET_break (size >= sizeof (struct GNUNET_MessageHeader));
935   hc = GNUNET_malloc (size);
936   memcpy (hc, hello, size);
937   schedule_control_transmit (handle,
938                              size,
939                              GNUNET_NO, OFFER_HELLO_TIMEOUT, &send_hello, hc);
940 }
941
942
943 /**
944  * Function we use for handling incoming messages.
945  */
946 static void demultiplexer (void *cls, const struct GNUNET_MessageHeader *msg);
947
948
949 static size_t
950 send_start (void *cls, size_t size, void *buf)
951 {
952   struct GNUNET_MessageHeader *s = buf;
953
954   if (buf == NULL)
955     {
956 #if DEBUG_TRANSPORT
957       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
958                   "Timeout while trying to transmit `%s' request.\n",
959                   "START");
960 #endif
961       return 0;
962     }
963 #if DEBUG_TRANSPORT
964   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
965               "Transmitting `%s' request.\n", "START");
966 #endif
967   GNUNET_assert (size >= sizeof (struct GNUNET_MessageHeader));
968   s->size = htons (sizeof (struct GNUNET_MessageHeader));
969   s->type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_START);
970   return sizeof (struct GNUNET_MessageHeader);
971 }
972
973
974 /**
975  * We're ready to transmit the request that the transport service
976  * should connect to a new peer.  In addition to sending the
977  * request, schedule the next phase for the transmission processing
978  * that caused the connect request in the first place.
979  */
980 static size_t
981 request_connect (void *cls, size_t size, void *buf)
982 {
983   struct GNUNET_TRANSPORT_TransmitHandle *th = cls;
984   struct TryConnectMessage *tcm;
985   struct GNUNET_TRANSPORT_Handle *h;
986
987   GNUNET_assert (th->notify_delay_task == GNUNET_SCHEDULER_NO_TASK);
988   h = th->handle;
989   if (buf == NULL)
990     {
991 #if DEBUG_TRANSPORT
992       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
993                   "Failed to transmit `%s' request for `%4s' to service.\n",
994                   "TRY_CONNECT",
995                   GNUNET_i2s(&th->target));
996 #endif
997       if (th->notify_delay_task != GNUNET_SCHEDULER_NO_TASK)
998         {
999           GNUNET_SCHEDULER_cancel (h->sched, th->notify_delay_task);
1000           th->notify_delay_task = GNUNET_SCHEDULER_NO_TASK;
1001         }
1002       th->notify (th->notify_cls, 0, NULL);
1003       GNUNET_free (th);
1004       return 0;
1005     }
1006 #if DEBUG_TRANSPORT
1007   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1008               "Transmitting `%s' message for `%4s'.\n",
1009               "TRY_CONNECT", GNUNET_i2s (&th->target));
1010 #endif
1011   GNUNET_assert (size >= sizeof (struct TryConnectMessage));
1012   tcm = buf;
1013   tcm->header.size = htons (sizeof (struct TryConnectMessage));
1014   tcm->header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_TRY_CONNECT);
1015   tcm->reserved = htonl (0);
1016   memcpy (&tcm->peer, &th->target, sizeof (struct GNUNET_PeerIdentity));
1017   th->notify_delay_task
1018     = GNUNET_SCHEDULER_add_delayed (h->sched,
1019                                     GNUNET_NO,
1020                                     GNUNET_SCHEDULER_PRIORITY_KEEP,
1021                                     GNUNET_SCHEDULER_NO_TASK,
1022                                     GNUNET_TIME_absolute_get_remaining
1023                                     (th->timeout),
1024                                     &peer_transmit_timeout, th);
1025   insert_transmit_handle (&h->connect_wait_head, th);
1026   return sizeof (struct TryConnectMessage);
1027 }
1028
1029
1030 /**
1031  * Schedule a request to connect to the given
1032  * neighbour (and if successful, add the specified
1033  * handle to the wait list).
1034  *
1035  * @param th handle for a request to transmit once we
1036  *        have connected
1037  */
1038 static void
1039 try_connect (struct GNUNET_TRANSPORT_TransmitHandle *th)
1040 {
1041   GNUNET_assert (th->notify_delay_task == GNUNET_SCHEDULER_NO_TASK);  
1042   schedule_control_transmit (th->handle,
1043                              sizeof (struct TryConnectMessage),
1044                              GNUNET_NO,
1045                              GNUNET_TIME_absolute_get_remaining (th->timeout),
1046                              &request_connect, th);
1047 }
1048
1049
1050 /**
1051  * Task for delayed attempts to reconnect to a peer.
1052  *
1053  * @param cls must be a transmit handle that determines the peer
1054  *        to which we will try to connect
1055  * @param tc scheduler information about why we were triggered (not used)
1056  */
1057 static void
1058 try_connect_task (void *cls,
1059                   const struct GNUNET_SCHEDULER_TaskContext *tc)
1060 {
1061   struct GNUNET_TRANSPORT_TransmitHandle *th = cls;  
1062
1063   th->notify_delay_task = GNUNET_SCHEDULER_NO_TASK;
1064   try_connect (th);
1065 }
1066
1067
1068 /**
1069  * Remove neighbour from our list.  Will automatically
1070  * trigger a re-connect attempt if we have messages pending
1071  * for this peer.
1072  * 
1073  * @param h our state
1074  * @param peer the peer to remove
1075  */
1076 static void
1077 remove_neighbour (struct GNUNET_TRANSPORT_Handle *h,
1078                   const struct GNUNET_PeerIdentity *peer)
1079 {
1080   struct NeighbourList *prev;
1081   struct NeighbourList *pos;
1082   struct GNUNET_TRANSPORT_TransmitHandle *th;
1083
1084 #if DEBUG_TRANSPORT
1085   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1086               "Removing neighbour `%s' from list of connected peers.\n",
1087               GNUNET_i2s (peer));
1088 #endif
1089   prev = NULL;
1090   pos = h->neighbours;
1091   while ((pos != NULL) &&
1092          (0 != memcmp (peer, 
1093                        &pos->id, 
1094                        sizeof (struct GNUNET_PeerIdentity))))
1095     {
1096       prev = pos;
1097       pos = pos->next;
1098     }
1099   if (pos == NULL)
1100     {
1101       GNUNET_break (0);
1102       return;
1103     }
1104   if (prev == NULL)
1105     h->neighbours = pos->next;
1106   else
1107     prev->next = pos->next;
1108   if (NULL != (th = pos->transmit_handle))
1109     {
1110       pos->transmit_handle = NULL;
1111       th->neighbour = NULL;
1112       remove_from_any_list (th);
1113       if (GNUNET_TIME_absolute_get_remaining (th->timeout).value <= CONNECT_RETRY_TIMEOUT.value)
1114         {
1115           /* signal error */
1116           GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == th->notify_delay_task);
1117           peer_transmit_timeout (th, NULL);       
1118         }
1119       else
1120         {
1121           GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1122                       _("Connection with `%4s' failed, will keep trying for %llu ms to deliver message\n"),
1123                       GNUNET_i2s (peer),
1124                       GNUNET_TIME_absolute_get_remaining (th->timeout).value);
1125           /* try again in a bit */
1126           GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == th->notify_delay_task);
1127           th->notify_delay_task 
1128             = GNUNET_SCHEDULER_add_delayed (h->sched,
1129                                             GNUNET_NO,
1130                                             GNUNET_SCHEDULER_PRIORITY_KEEP,
1131                                             GNUNET_SCHEDULER_NO_TASK,
1132                                             CONNECT_RETRY_TIMEOUT,
1133                                             &try_connect_task,
1134                                             th);
1135         }
1136     }
1137   if (h->nc_cb != NULL)
1138     h->nd_cb (h->cls, peer);
1139   GNUNET_free (pos);
1140 }
1141
1142
1143 /**
1144  * Try again to connect to transport service.
1145  */
1146 static void
1147 reconnect (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1148 {
1149   struct GNUNET_TRANSPORT_Handle *h = cls;
1150   struct GNUNET_TRANSPORT_TransmitHandle *pos;
1151   struct NeighbourList *n;
1152
1153   /* Forget about all neighbours that we used to be connected
1154      to */
1155   while (NULL != (n = h->neighbours))
1156     remove_neighbour (h, &n->id);
1157 #if DEBUG_TRANSPORT
1158   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Connecting to transport service.\n");
1159 #endif
1160   GNUNET_assert (h->client == NULL);
1161   h->reconnect_task = GNUNET_SCHEDULER_NO_TASK;
1162   h->client = GNUNET_CLIENT_connect (h->sched, "transport", h->cfg);
1163   GNUNET_assert (h->client != NULL);
1164   /* make sure we don't send "START" twice,
1165      remove existing entry from queue (if present) */
1166   pos = h->connect_ready_head;
1167   while (pos != NULL)
1168     {
1169       if (pos->notify == &send_start)
1170         {
1171           if (pos->prev == NULL)
1172             h->connect_ready_head = pos->next;
1173           else
1174             pos->prev->next = pos->next;
1175           if (pos->next != NULL)
1176             pos->next->prev = pos->prev;
1177           GNUNET_assert (pos->neighbour == NULL);
1178           GNUNET_free (pos);
1179           break;
1180         }
1181       pos = pos->next;
1182     }
1183   schedule_control_transmit (h,
1184                              sizeof (struct GNUNET_MessageHeader),
1185                              GNUNET_YES,
1186                              GNUNET_TIME_UNIT_FOREVER_REL, 
1187                              &send_start, NULL);
1188   GNUNET_CLIENT_receive (h->client,
1189                          &demultiplexer, h, GNUNET_TIME_UNIT_FOREVER_REL);
1190 }
1191
1192
1193 /**
1194  * Function that will schedule the job that will try
1195  * to connect us again to the client.
1196  */
1197 static void
1198 schedule_reconnect (struct GNUNET_TRANSPORT_Handle *h)
1199 {
1200 #if DEBUG_TRANSPORT
1201   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1202               "Scheduling task to reconnect to transport service in %llu ms.\n",
1203               h->reconnect_delay.value);
1204 #endif
1205   GNUNET_assert (h->client == NULL);
1206   GNUNET_assert (h->reconnect_task == GNUNET_SCHEDULER_NO_TASK);
1207   h->reconnect_task
1208     = GNUNET_SCHEDULER_add_delayed (h->sched,
1209                                     GNUNET_NO,
1210                                     GNUNET_SCHEDULER_PRIORITY_DEFAULT,
1211                                     GNUNET_SCHEDULER_NO_TASK,
1212                                     h->reconnect_delay, &reconnect, h);
1213   h->reconnect_delay = GNUNET_TIME_UNIT_SECONDS;
1214 }
1215
1216
1217 /**
1218  * We are connected to the respective peer, check the
1219  * bandwidth limits and schedule the transmission.
1220  */
1221 static void schedule_request (struct GNUNET_TRANSPORT_TransmitHandle *th);
1222
1223
1224 /**
1225  * Function called by the scheduler when the timeout
1226  * for bandwidth availablility for the target
1227  * neighbour is reached.
1228  */
1229 static void
1230 transmit_ready (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1231 {
1232   struct GNUNET_TRANSPORT_TransmitHandle *th = cls;
1233
1234   th->notify_delay_task = GNUNET_SCHEDULER_NO_TASK;
1235   schedule_request (th);
1236 }
1237
1238
1239 /**
1240  * Remove the given transmit handle from the wait list.  Does NOT free
1241  * it.
1242  */
1243 static void
1244 remove_from_wait_list (struct GNUNET_TRANSPORT_TransmitHandle *th)
1245 {
1246   if (th->prev == NULL)
1247     th->handle->connect_wait_head = th->next;
1248   else
1249     th->prev->next = th->next;
1250   if (th->next != NULL)
1251     th->next->prev = th->prev;
1252 }
1253
1254
1255 /**
1256  * We are connected to the respective peer, check the
1257  * bandwidth limits and schedule the transmission.
1258  */
1259 static void
1260 schedule_request (struct GNUNET_TRANSPORT_TransmitHandle *th)
1261 {
1262   struct GNUNET_TRANSPORT_Handle *h;
1263   struct GNUNET_TIME_Relative duration;
1264   struct NeighbourList *n;
1265   uint64_t available;
1266
1267   h = th->handle;
1268   n = th->neighbour;
1269   if (th->notify_delay_task != GNUNET_SCHEDULER_NO_TASK)
1270     {
1271       GNUNET_SCHEDULER_cancel (h->sched, th->notify_delay_task);
1272       th->notify_delay_task = GNUNET_SCHEDULER_NO_TASK;
1273     }
1274   /* check outgoing quota */
1275   duration = GNUNET_TIME_absolute_get_duration (n->last_quota_update);
1276   if (duration.value > MIN_QUOTA_REFRESH_TIME)
1277     {
1278       update_quota (n);
1279       duration = GNUNET_TIME_absolute_get_duration (n->last_quota_update);
1280     }
1281   available = duration.value * n->quota_out;
1282   if (available < n->last_sent + th->notify_size)
1283     {
1284       /* calculate how much bandwidth we'd still need to
1285          accumulate and based on that how long we'll have
1286          to wait... */
1287       available = n->last_sent + th->notify_size - available;
1288       duration = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS,
1289                                                 available / n->quota_out);
1290       if (th->timeout.value <
1291           GNUNET_TIME_relative_to_absolute (duration).value)
1292         {
1293           /* signal timeout! */
1294 #if DEBUG_TRANSPORT
1295           GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1296                       "Would need %llu ms before bandwidth is available for delivery to `%4s', that is too long.  Signaling timeout.\n",
1297                       duration.value,
1298                       GNUNET_i2s(&th->target));
1299 #endif
1300           remove_from_wait_list (th);
1301           th->notify (th->notify_cls, 0, NULL);
1302           GNUNET_free (th);
1303           return;
1304         }
1305 #if DEBUG_TRANSPORT
1306       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1307                   "Need more bandwidth, delaying delivery to `%4s' by %llu ms\n",
1308                   GNUNET_i2s(&th->target),
1309                   duration.value);
1310 #endif
1311       th->notify_delay_task
1312         = GNUNET_SCHEDULER_add_delayed (h->sched,
1313                                         GNUNET_NO,
1314                                         GNUNET_SCHEDULER_PRIORITY_KEEP,
1315                                         GNUNET_SCHEDULER_NO_TASK,
1316                                         duration, &transmit_ready, th);
1317       return;
1318     }
1319 #if DEBUG_TRANSPORT
1320   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1321               "Bandwidth available for transmission to `%4s'\n",
1322               GNUNET_i2s (&n->id));
1323 #endif
1324   if (GNUNET_NO == n->transmit_ok)
1325     {
1326       /* we may be ready, but transport service is not;
1327          wait for SendOkMessage or timeout */
1328 #if DEBUG_TRANSPORT
1329       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1330                   "Need to wait for transport service `%s' message\n",
1331                   "SEND_OK");
1332 #endif
1333       th->notify_delay_task
1334         = GNUNET_SCHEDULER_add_delayed (h->sched,
1335                                         GNUNET_NO,
1336                                         GNUNET_SCHEDULER_PRIORITY_KEEP,
1337                                         GNUNET_SCHEDULER_NO_TASK,
1338                                         GNUNET_TIME_absolute_get_remaining
1339                                         (th->timeout), &peer_transmit_timeout, th);
1340       return;
1341     }
1342   n->transmit_ok = GNUNET_NO;
1343   remove_from_wait_list (th);
1344 #if DEBUG_TRANSPORT
1345   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Moving message for `%4s' to ready list\n",
1346               GNUNET_i2s(&n->id));
1347 #endif
1348   insert_transmit_handle (&h->connect_ready_head, th);
1349   if (GNUNET_NO == h->transmission_scheduled)
1350     schedule_transmission (h);
1351 }
1352
1353
1354 /**
1355  * Add neighbour to our list
1356  */
1357 static void
1358 add_neighbour (struct GNUNET_TRANSPORT_Handle *h,
1359                uint32_t quota_out,
1360                struct GNUNET_TIME_Relative latency,
1361                const struct GNUNET_PeerIdentity *pid)
1362 {
1363   struct NeighbourList *n;
1364   struct GNUNET_TRANSPORT_TransmitHandle *prev;
1365   struct GNUNET_TRANSPORT_TransmitHandle *pos;
1366   struct GNUNET_TRANSPORT_TransmitHandle *next;
1367
1368   /* check for duplicates */
1369   if (NULL != find_neighbour (h, pid))
1370     {
1371       GNUNET_break (0);
1372       return;
1373     }
1374 #if DEBUG_TRANSPORT
1375   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1376               "Creating entry for new neighbour `%4s'.\n", GNUNET_i2s (pid));
1377 #endif
1378   n = GNUNET_malloc (sizeof (struct NeighbourList));
1379   n->id = *pid;
1380   n->last_quota_update = GNUNET_TIME_absolute_get ();
1381   n->quota_out = quota_out;
1382   n->next = h->neighbours;
1383   n->transmit_ok = GNUNET_YES;
1384   h->neighbours = n;
1385   if (h->nc_cb != NULL)
1386     h->nc_cb (h->cls, &n->id, latency);
1387   prev = NULL;
1388   pos = h->connect_wait_head;
1389   while (pos != NULL)
1390     {
1391       next = pos->next;
1392       if (0 == memcmp (pid,
1393                        &pos->target, sizeof (struct GNUNET_PeerIdentity)))
1394         {
1395           pos->neighbour = n;
1396           GNUNET_assert (NULL == n->transmit_handle);
1397           n->transmit_handle = pos;
1398           if (prev == NULL)
1399             h->connect_wait_head = next;
1400           else
1401             prev->next = next;
1402           if (GNUNET_YES == n->received_ack)
1403             {
1404 #if DEBUG_TRANSPORT
1405               GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1406                           "Found pending request for `%4s' will trigger it now.\n",
1407                           GNUNET_i2s (&pos->target));
1408 #endif
1409               if (pos->notify_delay_task != GNUNET_SCHEDULER_NO_TASK)
1410                 {
1411                   GNUNET_SCHEDULER_cancel (h->sched, pos->notify_delay_task);
1412                   pos->notify_delay_task = GNUNET_SCHEDULER_NO_TASK;
1413                 }
1414               schedule_request (pos);
1415             }
1416           else
1417             {
1418 #if DEBUG_TRANSPORT
1419               GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1420                           "Found pending request for `%4s' but still need `%s' before proceeding.\n",
1421                           GNUNET_i2s (&pos->target),
1422                           "ACK");
1423 #endif
1424             }
1425           break;
1426         }
1427       prev = pos;
1428       pos = next;
1429     }
1430 }
1431
1432
1433 /**
1434  * Connect to the transport service.  Note that the connection may
1435  * complete (or fail) asynchronously.
1436  *
1437
1438  * @param sched scheduler to use
1439  * @param cfg configuration to use
1440  * @param cls closure for the callbacks
1441  * @param rec receive function to call
1442  * @param nc function to call on connect events
1443  * @param dc function to call on disconnect events
1444  */
1445 struct GNUNET_TRANSPORT_Handle *
1446 GNUNET_TRANSPORT_connect (struct GNUNET_SCHEDULER_Handle *sched,
1447                           const struct GNUNET_CONFIGURATION_Handle *cfg,
1448                           void *cls,
1449                           GNUNET_TRANSPORT_ReceiveCallback rec,
1450                           GNUNET_TRANSPORT_NotifyConnect nc,
1451                           GNUNET_TRANSPORT_NotifyDisconnect nd)
1452 {
1453   struct GNUNET_TRANSPORT_Handle *ret;
1454
1455   GNUNET_ARM_start_service ("peerinfo",
1456                             cfg, sched, START_SERVICE_TIMEOUT, NULL, NULL);
1457   GNUNET_ARM_start_service ("transport",
1458                             cfg, sched, START_SERVICE_TIMEOUT, NULL, NULL);
1459   ret = GNUNET_malloc (sizeof (struct GNUNET_TRANSPORT_Handle));
1460   ret->sched = sched;
1461   ret->cfg = cfg;
1462   ret->cls = cls;
1463   ret->rec = rec;
1464   ret->nc_cb = nc;
1465   ret->nd_cb = nd;
1466   ret->reconnect_delay = GNUNET_TIME_UNIT_ZERO;
1467   schedule_reconnect (ret);
1468   return ret;
1469 }
1470
1471
1472 /**
1473  * These stop activities must be run in a fresh
1474  * scheduler that is NOT in shutdown mode.
1475  */
1476 static void
1477 stop_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1478 {
1479   struct GNUNET_TRANSPORT_Handle *handle = cls;
1480   GNUNET_ARM_stop_service ("transport",
1481                            handle->cfg,
1482                            tc->sched, STOP_SERVICE_TIMEOUT, NULL, NULL);
1483   GNUNET_ARM_stop_service ("peerinfo",
1484                            handle->cfg,
1485                            tc->sched, STOP_SERVICE_TIMEOUT, NULL, NULL);
1486 }
1487
1488
1489 /**
1490  * Disconnect from the transport service.
1491  */
1492 void
1493 GNUNET_TRANSPORT_disconnect (struct GNUNET_TRANSPORT_Handle *handle)
1494 {
1495   struct GNUNET_TRANSPORT_TransmitHandle *th;
1496   struct NeighbourList *n;
1497   struct HelloWaitList *hwl;
1498   struct GNUNET_CLIENT_Connection *client;
1499
1500 #if DEBUG_TRANSPORT
1501   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 
1502               "Transport disconnect called!\n");
1503 #endif
1504   while (NULL != (th = handle->connect_ready_head))
1505     {
1506       handle->connect_ready_head = th->next;
1507       GNUNET_assert (th->notify_delay_task == GNUNET_SCHEDULER_NO_TASK);
1508       th->notify (th->notify_cls, 0, NULL);
1509       GNUNET_free (th);
1510     }
1511   while (NULL != (th = handle->connect_wait_head))
1512     {
1513       handle->connect_wait_head = th->next;
1514       if (th->notify_delay_task != GNUNET_SCHEDULER_NO_TASK)
1515         {
1516           GNUNET_SCHEDULER_cancel (handle->sched, th->notify_delay_task);
1517           th->notify_delay_task = GNUNET_SCHEDULER_NO_TASK;
1518         }
1519       th->notify (th->notify_cls, 0, NULL);
1520       GNUNET_free (th);
1521     }
1522   while (NULL != (n = handle->neighbours))
1523     {
1524       handle->neighbours = n->next;
1525       GNUNET_free (n);
1526     }
1527   while (NULL != (hwl = handle->hwl_head))
1528     {
1529       handle->hwl_head = hwl->next;
1530       GNUNET_SCHEDULER_cancel (handle->sched, hwl->task);
1531       GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1532                   _("Disconnect while trying to obtain `%s' from transport service.\n"),
1533                   "HELLO");
1534       if (hwl->rec != NULL)
1535         hwl->rec (hwl->rec_cls, GNUNET_TIME_UNIT_ZERO, NULL, NULL);
1536       GNUNET_free (hwl);
1537     }
1538   if (handle->reconnect_task != GNUNET_SCHEDULER_NO_TASK)
1539     {
1540       GNUNET_SCHEDULER_cancel (handle->sched, handle->reconnect_task);
1541       handle->reconnect_task = GNUNET_SCHEDULER_NO_TASK;
1542     }
1543   GNUNET_free_non_null (handle->my_hello);
1544   handle->my_hello = NULL;
1545   GNUNET_SCHEDULER_run (&stop_task, handle);
1546   if (NULL != (client = handle->client))
1547     {
1548 #if DEBUG_TRANSPORT
1549       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1550                   "Disconnecting from transport service for good.\n");
1551 #endif
1552       handle->client = NULL;
1553       GNUNET_CLIENT_disconnect (client);
1554     }
1555   if (client == NULL)
1556     GNUNET_free (handle);
1557 }
1558
1559
1560 /**
1561  * Type of a function to call when we receive a message
1562  * from the service.
1563  *
1564  * @param cls closure
1565  * @param msg message received, NULL on timeout or fatal error
1566  */
1567 static void
1568 demultiplexer (void *cls, const struct GNUNET_MessageHeader *msg)
1569 {
1570   struct GNUNET_TRANSPORT_Handle *h = cls;
1571   const struct DisconnectInfoMessage *dim;
1572   const struct ConnectInfoMessage *cim;
1573   const struct InboundMessage *im;
1574   const struct GNUNET_MessageHeader *imm;
1575   const struct SendOkMessage *okm;
1576   struct HelloWaitList *hwl;
1577   struct NeighbourList *n;
1578   struct GNUNET_PeerIdentity me;
1579   struct GNUNET_TRANSPORT_TransmitHandle *th;
1580   uint16_t size;
1581
1582   if ((msg == NULL) || (h->client == NULL))
1583     {
1584       if (h->client != NULL)
1585         {
1586 #if DEBUG_TRANSPORT
1587           GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1588                       "Error receiving from transport service, disconnecting temporarily.\n");
1589 #endif
1590           if (h->network_handle != NULL)
1591             {
1592               GNUNET_CONNECTION_notify_transmit_ready_cancel (h->network_handle);
1593               h->network_handle = NULL;
1594               h->transmission_scheduled = GNUNET_NO;
1595               th = h->connect_ready_head;
1596               /* add timeout again, we cancelled the transmit_ready task! */
1597               GNUNET_assert (th->notify_delay_task == GNUNET_SCHEDULER_NO_TASK);
1598               th->notify_delay_task 
1599                 = GNUNET_SCHEDULER_add_delayed (h->sched,
1600                                                 GNUNET_NO,
1601                                                 GNUNET_SCHEDULER_PRIORITY_KEEP,
1602                                                 GNUNET_SCHEDULER_NO_TASK,
1603                                                 GNUNET_TIME_absolute_get_remaining(th->timeout),
1604                                                 &peer_transmit_timeout, 
1605                                                 th);    
1606             }
1607           GNUNET_CLIENT_disconnect (h->client);
1608           h->client = NULL;
1609           schedule_reconnect (h);
1610         }
1611       else
1612         {
1613           /* shutdown initiated from 'GNUNET_TRANSPORT_disconnect',
1614              finish clean up work! */
1615           GNUNET_free (h);
1616         }
1617       return;
1618     }
1619   GNUNET_CLIENT_receive (h->client,
1620                          &demultiplexer, h, GNUNET_TIME_UNIT_FOREVER_REL);
1621   size = ntohs (msg->size);
1622   switch (ntohs (msg->type))
1623     {
1624     case GNUNET_MESSAGE_TYPE_HELLO:
1625       if (GNUNET_OK !=
1626           GNUNET_HELLO_get_id ((const struct GNUNET_HELLO_Message *) msg,
1627                                &me))
1628         {
1629           GNUNET_break (0);
1630           break;
1631         }
1632 #if DEBUG_TRANSPORT
1633       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1634                   "Receiving (my own) `%s' message, I am `%4s'.\n",
1635                   "HELLO", GNUNET_i2s (&me));
1636 #endif
1637       GNUNET_free_non_null (h->my_hello);
1638       h->my_hello = NULL;
1639       if (size < sizeof (struct GNUNET_MessageHeader))
1640         {
1641           GNUNET_break (0);
1642           break;
1643         }
1644       h->my_hello = GNUNET_malloc (size);
1645       memcpy (h->my_hello, msg, size);
1646       while (NULL != (hwl = h->hwl_head))
1647         {
1648           h->hwl_head = hwl->next;
1649           GNUNET_SCHEDULER_cancel (h->sched, hwl->task);
1650           GNUNET_TRANSPORT_get_hello (h,
1651                                       GNUNET_TIME_UNIT_ZERO,
1652                                       hwl->rec, hwl->rec_cls);
1653           GNUNET_free (hwl);
1654         }
1655       break;
1656     case GNUNET_MESSAGE_TYPE_TRANSPORT_CONNECT:
1657       if (size != sizeof (struct ConnectInfoMessage))
1658         {
1659           GNUNET_break (0);
1660           break;
1661         }
1662       cim = (const struct ConnectInfoMessage *) msg;
1663 #if DEBUG_TRANSPORT
1664       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1665                   "Receiving `%s' message for `%4s'.\n",
1666                   "CONNECT", GNUNET_i2s (&cim->id));
1667 #endif
1668       add_neighbour (h,
1669                      ntohl (cim->quota_out),
1670                      GNUNET_TIME_relative_ntoh (cim->latency), &cim->id);
1671       break;
1672     case GNUNET_MESSAGE_TYPE_TRANSPORT_DISCONNECT:
1673       if (size != sizeof (struct DisconnectInfoMessage))
1674         {
1675           GNUNET_break (0);
1676           break;
1677         }
1678       dim = (const struct DisconnectInfoMessage *) msg;
1679 #if DEBUG_TRANSPORT
1680       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1681                   "Receiving `%s' message for `%4s'.\n",
1682                   "DISCONNECT", GNUNET_i2s (&dim->peer));
1683 #endif
1684       remove_neighbour (h, &dim->peer);
1685       break;
1686     case GNUNET_MESSAGE_TYPE_TRANSPORT_SEND_OK:
1687       if (size != sizeof (struct SendOkMessage))
1688         {
1689           GNUNET_break (0);
1690           break;
1691         }
1692       okm = (const struct SendOkMessage *) msg;
1693 #if DEBUG_TRANSPORT
1694       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1695                   "Receiving `%s' message, transmission %s.\n", "SEND_OK",
1696                   ntohl(okm->success) == GNUNET_OK ? "succeeded" : "failed");
1697 #endif
1698       n = find_neighbour (h, &okm->peer);
1699       GNUNET_assert (n != NULL);
1700       n->transmit_ok = GNUNET_YES;
1701       if (n->transmit_handle != NULL)
1702         {
1703 #if DEBUG_TRANSPORT
1704           GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1705                       "Processing pending message for `%4s'\n",
1706                       GNUNET_i2s(&n->id));
1707 #endif
1708           GNUNET_SCHEDULER_cancel (h->sched,
1709                                    n->transmit_handle->notify_delay_task);
1710           n->transmit_handle->notify_delay_task =
1711             GNUNET_SCHEDULER_NO_TASK;
1712           GNUNET_assert (GNUNET_YES == n->received_ack);
1713           schedule_request (n->transmit_handle);
1714         }
1715       break;
1716     case GNUNET_MESSAGE_TYPE_TRANSPORT_RECV:
1717 #if DEBUG_TRANSPORT
1718       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1719                   "Receiving `%s' message.\n", "RECV");
1720 #endif
1721       if (size <
1722           sizeof (struct InboundMessage) +
1723           sizeof (struct GNUNET_MessageHeader))
1724         {
1725           GNUNET_break (0);
1726           break;
1727         }
1728       im = (const struct InboundMessage *) msg;
1729       imm = (const struct GNUNET_MessageHeader *) &im[1];
1730       if (ntohs (imm->size) + sizeof (struct InboundMessage) != size)
1731         {
1732           GNUNET_break (0);
1733           break;
1734         }
1735       switch (ntohs (imm->type))
1736         {
1737         case GNUNET_MESSAGE_TYPE_TRANSPORT_ACK:
1738 #if DEBUG_TRANSPORT
1739           GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1740                       "Receiving `%s' message from `%4s'.\n",
1741                       "ACK", GNUNET_i2s (&im->peer));
1742 #endif
1743           n = find_neighbour (h, &im->peer);
1744           if (n == NULL)
1745             {
1746               GNUNET_break (0);
1747               break;
1748             }
1749           if (n->received_ack == GNUNET_NO)
1750             {
1751               n->received_ack = GNUNET_YES;
1752               if (NULL != n->transmit_handle)
1753                 {
1754 #if DEBUG_TRANSPORT
1755                   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1756                               "Peer connected, scheduling delayed message for deliverery now.\n");
1757 #endif
1758                   schedule_request (n->transmit_handle);
1759                 }
1760             }
1761           break;
1762         default:
1763 #if DEBUG_TRANSPORT
1764           GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1765                       "Received message of type %u from `%4s'.\n",
1766                       ntohs (imm->type), GNUNET_i2s (&im->peer));
1767 #endif
1768           if (h->rec != NULL)
1769             h->rec (h->cls,
1770                     GNUNET_TIME_relative_ntoh (im->latency), &im->peer, imm);
1771           break;
1772         }
1773       break;
1774     default:
1775       GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1776                   _
1777                   ("Received unexpected message of type %u in %s:%u\n"),
1778                   ntohs (msg->type), __FILE__, __LINE__);
1779       GNUNET_break (0);
1780       break;
1781     }
1782 }
1783
1784
1785 struct ClientTransmitWrapper
1786 {
1787   GNUNET_CONNECTION_TransmitReadyNotify notify;
1788   void *notify_cls;
1789   struct GNUNET_TRANSPORT_TransmitHandle *th;
1790 };
1791
1792
1793 /**
1794  * Transmit message of a client destined for another
1795  * peer to the service.
1796  */
1797 static size_t
1798 client_notify_wrapper (void *cls, size_t size, void *buf)
1799 {
1800   struct ClientTransmitWrapper *ctw = cls;
1801   struct OutboundMessage *obm;
1802   struct GNUNET_MessageHeader *hdr;
1803   size_t ret;
1804
1805   if (size == 0)
1806     {
1807 #if DEBUG_TRANSPORT
1808       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1809                   "Transmission request could not be satisfied.\n");
1810 #endif
1811       ret = ctw->notify (ctw->notify_cls, 0, NULL);
1812       GNUNET_assert (ret == 0);
1813       GNUNET_free (ctw);
1814       return 0;
1815     }
1816   GNUNET_assert (size >= sizeof (struct OutboundMessage));
1817   obm = buf;
1818   ret = ctw->notify (ctw->notify_cls,
1819                      size - sizeof (struct OutboundMessage),
1820                      (void *) &obm[1]);
1821   if (ret == 0)
1822     {
1823       /* Need to reset flag, no SEND means no SEND_OK! */
1824       ctw->th->neighbour->transmit_ok = GNUNET_YES;
1825       GNUNET_free (ctw);
1826       return 0;
1827     }
1828   GNUNET_assert (ret >= sizeof (struct GNUNET_MessageHeader));
1829   hdr = (struct GNUNET_MessageHeader *) &obm[1];
1830   GNUNET_assert (ntohs (hdr->size) == ret);
1831   GNUNET_assert (ret + sizeof (struct OutboundMessage) <
1832                  GNUNET_SERVER_MAX_MESSAGE_SIZE);
1833 #if DEBUG_TRANSPORT
1834   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1835               "Transmitting `%s' message with data for `%4s'\n",
1836               "SEND", GNUNET_i2s (&ctw->th->target));
1837 #endif
1838   ret += sizeof (struct OutboundMessage);
1839   obm->header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_SEND);
1840   obm->header.size = htons (ret);
1841   obm->priority = htonl (ctw->th->priority);
1842   obm->peer = ctw->th->target;
1843   GNUNET_free (ctw);
1844   return ret;
1845 }
1846
1847
1848
1849 /**
1850  * Check if we could queue a message of the given size for
1851  * transmission.  The transport service will take both its
1852  * internal buffers and bandwidth limits imposed by the
1853  * other peer into consideration when answering this query.
1854  *
1855  * @param handle connection to transport service
1856  * @param target who should receive the message
1857  * @param size how big is the message we want to transmit?
1858  * @param priority how important is the message?
1859  * @param timeout after how long should we give up (and call
1860  *        notify with buf NULL and size 0)?
1861  * @param notify function to call when we are ready to
1862  *        send such a message
1863  * @param notify_cls closure for notify
1864  * @return NULL if someone else is already waiting to be notified
1865  *         non-NULL if the notify callback was queued (can be used to cancel
1866  *         using GNUNET_TRANSPORT_notify_transmit_ready_cancel)
1867  */
1868 struct GNUNET_TRANSPORT_TransmitHandle *
1869 GNUNET_TRANSPORT_notify_transmit_ready (struct GNUNET_TRANSPORT_Handle
1870                                         *handle,
1871                                         const struct GNUNET_PeerIdentity
1872                                         *target, size_t size,
1873                                         unsigned int priority,
1874                                         struct GNUNET_TIME_Relative timeout,
1875                                         GNUNET_CONNECTION_TransmitReadyNotify
1876                                         notify, void *notify_cls)
1877 {
1878   struct GNUNET_TRANSPORT_TransmitHandle *pos;
1879   struct GNUNET_TRANSPORT_TransmitHandle *th;
1880   struct NeighbourList *n;
1881   struct ClientTransmitWrapper *ctw;
1882
1883   if (size + sizeof (struct OutboundMessage) >=
1884       GNUNET_SERVER_MAX_MESSAGE_SIZE)
1885     {
1886       GNUNET_break (0);
1887       return NULL;
1888     }
1889 #if DEBUG_TRANSPORT
1890   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1891               "Asking transport service for transmission of %u bytes to peer `%4s'.\n",
1892               size, GNUNET_i2s (target));
1893 #endif
1894   n = find_neighbour (handle, target);
1895   if ( (n != NULL) &&
1896        (n->transmit_handle != NULL) )
1897     return NULL; /* already have a request pending for this peer! */
1898   ctw = GNUNET_malloc (sizeof (struct ClientTransmitWrapper));
1899   th = GNUNET_malloc (sizeof (struct GNUNET_TRANSPORT_TransmitHandle));
1900   ctw->notify = notify;
1901   ctw->notify_cls = notify_cls;
1902   ctw->th = th;
1903   th->handle = handle;
1904   th->neighbour = n;
1905   th->target = *target;
1906   th->notify = &client_notify_wrapper;
1907   th->notify_cls = ctw;
1908   th->timeout = GNUNET_TIME_relative_to_absolute (timeout);
1909   th->notify_size = size + sizeof (struct OutboundMessage);
1910   th->priority = priority;
1911   if (NULL == n)
1912     {
1913       pos = handle->connect_wait_head;
1914       while (pos != NULL)
1915         {
1916           GNUNET_assert (0 != memcmp (target,
1917                                       &pos->target,
1918                                       sizeof (struct GNUNET_PeerIdentity)));
1919           pos = pos->next;
1920         }
1921 #if DEBUG_TRANSPORT
1922       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1923                   "Will now try to connect to `%4s'.\n", GNUNET_i2s (target));
1924 #endif
1925       try_connect (th);
1926       return th;
1927     }
1928
1929 #if DEBUG_TRANSPORT
1930   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1931               "Transmission request queued for transmission to transport service.\n");
1932 #endif
1933   GNUNET_assert (NULL == n->transmit_handle);
1934   n->transmit_handle = th;
1935   if (GNUNET_YES != n->received_ack)
1936     {
1937 #if DEBUG_TRANSPORT
1938       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1939                   "Connection to `%4s' is not yet confirmed connected, scheduling timeout (%llu ms) only.\n",
1940                   GNUNET_i2s (target), timeout.value);
1941 #endif
1942       th->notify_delay_task
1943         = GNUNET_SCHEDULER_add_delayed (handle->sched,
1944                                         GNUNET_NO,
1945                                         GNUNET_SCHEDULER_PRIORITY_KEEP,
1946                                         GNUNET_SCHEDULER_NO_TASK,
1947                                         timeout, &peer_transmit_timeout, th);
1948       return th;
1949     }
1950   
1951 #if DEBUG_TRANSPORT
1952   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1953               "Peer `%4s' is ready to receive, scheduling message for delivery now.\n",
1954               GNUNET_i2s (target));
1955 #endif
1956   schedule_request (th);
1957   return th;
1958 }
1959
1960
1961 /**
1962  * Cancel the specified transmission-ready notification.
1963  */
1964 void
1965 GNUNET_TRANSPORT_notify_transmit_ready_cancel (struct
1966                                                GNUNET_TRANSPORT_TransmitHandle
1967                                                *th)
1968 {
1969   struct GNUNET_TRANSPORT_Handle *h;
1970
1971 #if DEBUG_TRANSPORT
1972   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1973               "Transmission request of %u bytes to `%4s' was cancelled.\n",
1974               th->notify_size - sizeof(struct OutboundMessage),
1975               GNUNET_i2s (&th->target));
1976 #endif
1977   GNUNET_assert (th->notify == &client_notify_wrapper);
1978   remove_from_any_list (th);
1979   h = th->handle;
1980   if ((h->connect_ready_head == NULL) && (h->network_handle != NULL))
1981     {
1982       GNUNET_CONNECTION_notify_transmit_ready_cancel (h->network_handle);
1983       h->network_handle = NULL;
1984       h->transmission_scheduled = GNUNET_NO;
1985     }
1986   GNUNET_free (th->notify_cls);
1987   GNUNET_assert (th->notify_delay_task == GNUNET_SCHEDULER_NO_TASK);
1988   GNUNET_free (th);
1989 }
1990
1991
1992 /* end of transport_api.c */