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