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