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