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