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