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