fix
[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 #if DEBUG_TRANSPORT
621   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
622               "Control transmit of %u bytes within %llums requested\n", 
623               size,
624               (unsigned long long) timeout.value);
625 #endif
626   th = GNUNET_malloc (sizeof (struct GNUNET_TRANSPORT_TransmitHandle));
627   th->handle = h;
628   th->notify = notify;
629   th->notify_cls = notify_cls;
630   th->timeout = GNUNET_TIME_relative_to_absolute (timeout);
631   th->notify_size = size;
632   th->notify_delay_task 
633     = GNUNET_SCHEDULER_add_delayed (h->sched,
634                                     timeout,
635                                     &peer_transmit_timeout, th);    
636   if (at_head)
637     {
638       th->next = h->connect_ready_head;
639       h->connect_ready_head = th;
640       if (th->next != NULL)
641         th->next->prev = th;
642     }
643   else
644     {
645       insert_transmit_handle (&h->connect_ready_head, th);
646     }
647   if (GNUNET_NO == h->transmission_scheduled)
648     schedule_transmission (h);
649 }
650
651
652 /**
653  * Update the quota values for the given neighbour now.
654  */
655 static void
656 update_quota (struct NeighbourList *n)
657 {
658   struct GNUNET_TIME_Relative delta;
659   uint64_t allowed;
660   uint64_t remaining;
661
662   delta = GNUNET_TIME_absolute_get_duration (n->last_quota_update);
663   allowed = delta.value * n->quota_out;
664   if (n->last_sent < allowed)
665     {
666       remaining = allowed - n->last_sent;
667       if (n->quota_out > 0)
668         remaining /= n->quota_out;
669       else
670         remaining = 0;
671       if (remaining > MAX_BANDWIDTH_CARRY)
672         remaining = MAX_BANDWIDTH_CARRY;
673       n->last_sent = 0;
674       n->last_quota_update = GNUNET_TIME_absolute_get ();
675       n->last_quota_update.value -= remaining;
676     }
677   else
678     {
679       n->last_sent -= allowed;
680       n->last_quota_update = GNUNET_TIME_absolute_get ();
681     }
682 }
683
684
685 struct SetQuotaContext
686 {
687   struct GNUNET_TRANSPORT_Handle *handle;
688
689   struct GNUNET_PeerIdentity target;
690
691   GNUNET_SCHEDULER_Task cont;
692
693   void *cont_cls;
694
695   struct GNUNET_TIME_Absolute timeout;
696
697   uint32_t quota_in;
698 };
699
700
701 static size_t
702 send_set_quota (void *cls, size_t size, void *buf)
703 {
704   struct SetQuotaContext *sqc = cls;
705   struct QuotaSetMessage *msg;
706
707   if (buf == NULL)
708     {
709       GNUNET_SCHEDULER_add_continuation (sqc->handle->sched,
710                                          sqc->cont,
711                                          sqc->cont_cls,
712                                          GNUNET_SCHEDULER_REASON_TIMEOUT);
713       GNUNET_free (sqc);
714       return 0;
715     }
716 #if DEBUG_TRANSPORT
717   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
718               "Transmitting `%s' request with respect to `%4s'.\n",
719               "SET_QUOTA", GNUNET_i2s (&sqc->target));
720 #endif
721   GNUNET_assert (size >= sizeof (struct QuotaSetMessage));
722   msg = buf;
723   msg->header.size = htons (sizeof (struct QuotaSetMessage));
724   msg->header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_SET_QUOTA);
725   msg->quota_in = htonl (sqc->quota_in);
726   memcpy (&msg->peer, &sqc->target, sizeof (struct GNUNET_PeerIdentity));
727   if (sqc->cont != NULL)
728     GNUNET_SCHEDULER_add_continuation (sqc->handle->sched,
729                                        sqc->cont,
730                                        sqc->cont_cls,
731                                        GNUNET_SCHEDULER_REASON_PREREQ_DONE);
732   GNUNET_free (sqc);
733   return sizeof (struct QuotaSetMessage);
734 }
735
736
737 /**
738  * Set the share of incoming bandwidth for the given
739  * peer to the specified amount.
740  *
741  * @param handle connection to transport service
742  * @param target who's bandwidth quota is being changed
743  * @param quota_in incoming bandwidth quota in bytes per ms; 0 can
744  *        be used to force all traffic to be discarded
745  * @param quota_out outgoing bandwidth quota in bytes per ms; 0 can
746  *        be used to force all traffic to be discarded
747  * @param timeout how long to wait until signaling failure if
748  *        we can not communicate the quota change
749  * @param cont continuation to call when done, will be called
750  *        either with reason "TIMEOUT" or with reason "PREREQ_DONE"
751  * @param cont_cls closure for continuation
752  */
753 void
754 GNUNET_TRANSPORT_set_quota (struct GNUNET_TRANSPORT_Handle *handle,
755                             const struct GNUNET_PeerIdentity *target,
756                             uint32_t quota_in,
757                             uint32_t quota_out,
758                             struct GNUNET_TIME_Relative timeout,
759                             GNUNET_SCHEDULER_Task cont, void *cont_cls)
760 {
761   struct NeighbourList *n;
762   struct SetQuotaContext *sqc;
763
764   n = find_neighbour (handle, target);
765   if (n != NULL)
766     {
767       update_quota (n);
768       if (n->quota_out < quota_out)
769         n->last_quota_update = GNUNET_TIME_absolute_get ();
770       n->quota_out = quota_out;
771     }
772   sqc = GNUNET_malloc (sizeof (struct SetQuotaContext));
773   sqc->handle = handle;
774   sqc->target = *target;
775   sqc->cont = cont;
776   sqc->cont_cls = cont_cls;
777   sqc->timeout = GNUNET_TIME_relative_to_absolute (timeout);
778   sqc->quota_in = quota_in;
779   schedule_control_transmit (handle,
780                              sizeof (struct QuotaSetMessage),
781                              GNUNET_NO, timeout, &send_set_quota, sqc);
782 }
783
784
785 /**
786  * A "get_hello" request has timed out.  Signal the client
787  * and clean up.
788  */
789 static void
790 hello_wait_timeout (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
791 {
792   struct HelloWaitList *hwl = cls;
793   struct HelloWaitList *pos;
794   struct HelloWaitList *prev;
795
796   hwl->task = GNUNET_SCHEDULER_NO_TASK;
797   if (GNUNET_TIME_absolute_get_remaining (hwl->timeout).value > 0)
798     {
799 #if DEBUG_TRANSPORT
800       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
801                   _("First attempt to obtain `%s' from transport service failed, will try again for %llums.\n"),
802                   "HELLO",
803                   GNUNET_TIME_absolute_get_remaining (hwl->timeout).value);
804 #endif
805       hwl->task = GNUNET_SCHEDULER_add_delayed (hwl->handle->sched,
806                                                 GNUNET_TIME_absolute_get_remaining (hwl->timeout),
807                                                 &hello_wait_timeout, hwl);
808       return;      
809     }
810   /* signal timeout */
811   GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
812               _("Timeout trying to obtain `%s' from transport service.\n"),
813               "HELLO");
814   prev = NULL;
815   pos = hwl->handle->hwl_head;
816   while (pos != hwl)
817     {
818       GNUNET_assert (pos != NULL);
819       prev = pos;
820       pos = pos->next;
821     }
822   if (prev == NULL)
823     hwl->handle->hwl_head = hwl->next;
824   else
825     prev->next = hwl->next;
826   if (hwl->rec != NULL)
827     hwl->rec (hwl->rec_cls, GNUNET_TIME_UNIT_ZERO, NULL, NULL);
828   GNUNET_free (hwl);
829 }
830
831
832 /**
833  * Obtain the HELLO message for this peer.
834  *
835  * @param handle connection to transport service
836  * @param timeout how long to wait for the HELLO
837  * @param rec function to call with the HELLO, sender will be our peer
838  *            identity; message and sender will be NULL on timeout
839  *            (handshake with transport service pending/failed).
840  *             cost estimate will be 0.
841  * @param rec_cls closure for rec
842  */
843 void
844 GNUNET_TRANSPORT_get_hello (struct GNUNET_TRANSPORT_Handle *handle,
845                             struct GNUNET_TIME_Relative timeout,
846                             GNUNET_TRANSPORT_ReceiveCallback rec,
847                             void *rec_cls)
848 {
849   struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded pk;
850   struct GNUNET_PeerIdentity me;
851   struct HelloWaitList *hwl;
852
853   if (handle->my_hello == NULL)
854     {
855       hwl = GNUNET_malloc (sizeof (struct HelloWaitList));
856       hwl->next = handle->hwl_head;
857       handle->hwl_head = hwl;
858       hwl->handle = handle;
859       hwl->rec = rec;
860       hwl->rec_cls = rec_cls;
861       hwl->timeout = GNUNET_TIME_relative_to_absolute (timeout);
862       hwl->task = GNUNET_SCHEDULER_add_delayed (handle->sched,
863                                                 timeout,
864                                                 &hello_wait_timeout, hwl);
865       return;
866     }
867   GNUNET_assert (GNUNET_OK == GNUNET_HELLO_get_key (handle->my_hello, &pk));
868   GNUNET_CRYPTO_hash (&pk,
869                       sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
870                       &me.hashPubKey);
871
872   rec (rec_cls,
873        GNUNET_TIME_UNIT_ZERO,
874        &me, (const struct GNUNET_MessageHeader *) handle->my_hello);
875 }
876
877
878 static size_t
879 send_hello (void *cls, size_t size, void *buf)
880 {
881   struct GNUNET_MessageHeader *hello = cls;
882   uint16_t msize;
883
884   if (buf == NULL)
885     {
886 #if DEBUG_TRANSPORT
887       GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
888                   "Timeout while trying to transmit `%s' request.\n",
889                   "HELLO");
890 #endif
891       GNUNET_free (hello);
892       return 0;
893     }
894 #if DEBUG_TRANSPORT
895   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
896               "Transmitting `%s' request.\n", "HELLO");
897 #endif
898   msize = ntohs (hello->size);
899   GNUNET_assert (size >= msize);
900   memcpy (buf, hello, msize);
901   GNUNET_free (hello);
902   return msize;
903 }
904
905
906 /**
907  * Offer the transport service the HELLO of another peer.  Note that
908  * the transport service may just ignore this message if the HELLO is
909  * malformed or useless due to our local configuration.
910  *
911  * @param handle connection to transport service
912  * @param hello the hello message
913  */
914 void
915 GNUNET_TRANSPORT_offer_hello (struct GNUNET_TRANSPORT_Handle *handle,
916                               const struct GNUNET_MessageHeader *hello)
917 {
918   struct GNUNET_MessageHeader *hc;
919   uint16_t size;
920
921   if (handle->client == NULL)
922     {
923 #if DEBUG_TRANSPORT
924       GNUNET_log (GNUNET_ERROR_TYPE_INFO,
925                   "Not connected to transport service, dropping offered HELLO\n");
926 #endif
927       return;
928     }
929   GNUNET_break (ntohs (hello->type) == GNUNET_MESSAGE_TYPE_HELLO);
930   size = ntohs (hello->size);
931   GNUNET_break (size >= sizeof (struct GNUNET_MessageHeader));
932   hc = GNUNET_malloc (size);
933   memcpy (hc, hello, size);
934   schedule_control_transmit (handle,
935                              size,
936                              GNUNET_NO, OFFER_HELLO_TIMEOUT, &send_hello, hc);
937 }
938
939
940 /**
941  * Function we use for handling incoming messages.
942  */
943 static void demultiplexer (void *cls, const struct GNUNET_MessageHeader *msg);
944
945
946 static size_t
947 send_start (void *cls, size_t size, void *buf)
948 {
949   struct GNUNET_MessageHeader *s = buf;
950
951   if (buf == NULL)
952     {
953 #if DEBUG_TRANSPORT
954       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
955                   "Timeout while trying to transmit `%s' request.\n",
956                   "START");
957 #endif
958       return 0;
959     }
960 #if DEBUG_TRANSPORT
961   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
962               "Transmitting `%s' request.\n", "START");
963 #endif
964   GNUNET_assert (size >= sizeof (struct GNUNET_MessageHeader));
965   s->size = htons (sizeof (struct GNUNET_MessageHeader));
966   s->type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_START);
967   return sizeof (struct GNUNET_MessageHeader);
968 }
969
970
971 /**
972  * We're ready to transmit the request that the transport service
973  * should connect to a new peer.  In addition to sending the
974  * request, schedule the next phase for the transmission processing
975  * that caused the connect request in the first place.
976  */
977 static size_t
978 request_connect (void *cls, size_t size, void *buf)
979 {
980   struct GNUNET_TRANSPORT_TransmitHandle *th = cls;
981   struct TryConnectMessage *tcm;
982   struct GNUNET_TRANSPORT_Handle *h;
983
984   GNUNET_assert (th->notify_delay_task == GNUNET_SCHEDULER_NO_TASK);
985   h = th->handle;
986   if (buf == NULL)
987     {
988 #if DEBUG_TRANSPORT
989       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
990                   "Failed to transmit `%s' request for `%4s' to service.\n",
991                   "TRY_CONNECT",
992                   GNUNET_i2s(&th->target));
993 #endif
994       if (th->notify_delay_task != GNUNET_SCHEDULER_NO_TASK)
995         {
996           GNUNET_SCHEDULER_cancel (h->sched, th->notify_delay_task);
997           th->notify_delay_task = GNUNET_SCHEDULER_NO_TASK;
998         }
999       th->notify (th->notify_cls, 0, NULL);
1000       GNUNET_free (th);
1001       return 0;
1002     }
1003 #if DEBUG_TRANSPORT
1004   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1005               "Transmitting `%s' message for `%4s' (need connection in %llu ms).\n",
1006               "TRY_CONNECT", GNUNET_i2s (&th->target),
1007               GNUNET_TIME_absolute_get_remaining (th->timeout).value);
1008 #endif
1009   GNUNET_assert (size >= sizeof (struct TryConnectMessage));
1010   tcm = buf;
1011   tcm->header.size = htons (sizeof (struct TryConnectMessage));
1012   tcm->header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_TRY_CONNECT);
1013   tcm->reserved = htonl (0);
1014   memcpy (&tcm->peer, &th->target, sizeof (struct GNUNET_PeerIdentity));
1015   th->notify_delay_task
1016     = GNUNET_SCHEDULER_add_delayed (h->sched,
1017                                     GNUNET_TIME_absolute_get_remaining
1018                                     (th->timeout),
1019                                     &peer_transmit_timeout, th);
1020   insert_transmit_handle (&h->connect_wait_head, th);
1021   return sizeof (struct TryConnectMessage);
1022 }
1023
1024
1025 /**
1026  * Schedule a request to connect to the given
1027  * neighbour (and if successful, add the specified
1028  * handle to the wait list).
1029  *
1030  * @param th handle for a request to transmit once we
1031  *        have connected
1032  */
1033 static void
1034 try_connect (struct GNUNET_TRANSPORT_TransmitHandle *th)
1035 {
1036   GNUNET_assert (th->notify_delay_task == GNUNET_SCHEDULER_NO_TASK);  
1037   schedule_control_transmit (th->handle,
1038                              sizeof (struct TryConnectMessage),
1039                              GNUNET_NO,
1040                              GNUNET_TIME_absolute_get_remaining (th->timeout),
1041                              &request_connect, th);
1042 }
1043
1044
1045 /**
1046  * Task for delayed attempts to reconnect to a peer.
1047  *
1048  * @param cls must be a transmit handle that determines the peer
1049  *        to which we will try to connect
1050  * @param tc scheduler information about why we were triggered (not used)
1051  */
1052 static void
1053 try_connect_task (void *cls,
1054                   const struct GNUNET_SCHEDULER_TaskContext *tc)
1055 {
1056   struct GNUNET_TRANSPORT_TransmitHandle *th = cls;  
1057
1058   th->notify_delay_task = GNUNET_SCHEDULER_NO_TASK;
1059   try_connect (th);
1060 }
1061
1062
1063 /**
1064  * Remove neighbour from our list.  Will automatically
1065  * trigger a re-connect attempt if we have messages pending
1066  * for this peer.
1067  * 
1068  * @param h our state
1069  * @param peer the peer to remove
1070  */
1071 static void
1072 remove_neighbour (struct GNUNET_TRANSPORT_Handle *h,
1073                   const struct GNUNET_PeerIdentity *peer)
1074 {
1075   struct NeighbourList *prev;
1076   struct NeighbourList *pos;
1077   struct GNUNET_TRANSPORT_TransmitHandle *th;
1078
1079 #if DEBUG_TRANSPORT
1080   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1081               "Removing neighbour `%s' from list of connected peers.\n",
1082               GNUNET_i2s (peer));
1083 #endif
1084   prev = NULL;
1085   pos = h->neighbours;
1086   while ((pos != NULL) &&
1087          (0 != memcmp (peer, 
1088                        &pos->id, 
1089                        sizeof (struct GNUNET_PeerIdentity))))
1090     {
1091       prev = pos;
1092       pos = pos->next;
1093     }
1094   if (pos == NULL)
1095     {
1096       GNUNET_break (0);
1097       return;
1098     }
1099   if (prev == NULL)
1100     h->neighbours = pos->next;
1101   else
1102     prev->next = pos->next;
1103   if (NULL != (th = pos->transmit_handle))
1104     {
1105       pos->transmit_handle = NULL;
1106       th->neighbour = NULL;
1107       remove_from_any_list (th);
1108       if (GNUNET_TIME_absolute_get_remaining (th->timeout).value <= CONNECT_RETRY_TIMEOUT.value)
1109         {
1110           /* signal error */
1111           GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1112                       _("Connection with `%4s' failed and timeout was in the past, giving up on message delivery.\n"),
1113                       GNUNET_i2s (peer));
1114           GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == th->notify_delay_task);
1115           peer_transmit_timeout (th, NULL);       
1116         }
1117       else
1118         {
1119           GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1120                       _("Connection with `%4s' failed, will keep trying for %llu ms to deliver message\n"),
1121                       GNUNET_i2s (peer),
1122                       GNUNET_TIME_absolute_get_remaining (th->timeout).value);
1123           /* try again in a bit */
1124           GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == th->notify_delay_task);
1125           th->notify_delay_task 
1126             = GNUNET_SCHEDULER_add_delayed (h->sched,
1127                                             CONNECT_RETRY_TIMEOUT,
1128                                             &try_connect_task,
1129                                             th);
1130         }
1131     }
1132   if (h->nc_cb != NULL)
1133     h->nd_cb (h->cls, peer);
1134   GNUNET_free (pos);
1135 }
1136
1137
1138 /**
1139  * Try again to connect to transport service.
1140  */
1141 static void
1142 reconnect (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1143 {
1144   struct GNUNET_TRANSPORT_Handle *h = cls;
1145   struct GNUNET_TRANSPORT_TransmitHandle *pos;
1146   struct NeighbourList *n;
1147
1148   /* Forget about all neighbours that we used to be connected
1149      to */
1150   while (NULL != (n = h->neighbours))
1151     remove_neighbour (h, &n->id);
1152 #if DEBUG_TRANSPORT
1153   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Connecting to transport service.\n");
1154 #endif
1155   GNUNET_assert (h->client == NULL);
1156   h->reconnect_task = GNUNET_SCHEDULER_NO_TASK;
1157   h->client = GNUNET_CLIENT_connect (h->sched, "transport", h->cfg);
1158   GNUNET_assert (h->client != NULL);
1159   /* make sure we don't send "START" twice,
1160      remove existing entry from queue (if present) */
1161   pos = h->connect_ready_head;
1162   while (pos != NULL)
1163     {
1164       if (pos->notify == &send_start)
1165         {
1166           if (pos->prev == NULL)
1167             h->connect_ready_head = pos->next;
1168           else
1169             pos->prev->next = pos->next;
1170           if (pos->next != NULL)
1171             pos->next->prev = pos->prev;
1172           GNUNET_assert (pos->neighbour == NULL);
1173           if (GNUNET_SCHEDULER_NO_TASK != pos->notify_delay_task)
1174             {
1175               GNUNET_SCHEDULER_cancel (h->sched,
1176                                        pos->notify_delay_task);
1177               pos->notify_delay_task = GNUNET_SCHEDULER_NO_TASK;
1178             }
1179           GNUNET_free (pos);
1180           break;
1181         }
1182       pos = pos->next;
1183     }
1184   schedule_control_transmit (h,
1185                              sizeof (struct GNUNET_MessageHeader),
1186                              GNUNET_YES,
1187                              GNUNET_TIME_UNIT_FOREVER_REL, 
1188                              &send_start, NULL);
1189   GNUNET_CLIENT_receive (h->client,
1190                          &demultiplexer, h, GNUNET_TIME_UNIT_FOREVER_REL);
1191 }
1192
1193
1194 /**
1195  * Function that will schedule the job that will try
1196  * to connect us again to the client.
1197  */
1198 static void
1199 schedule_reconnect (struct GNUNET_TRANSPORT_Handle *h)
1200 {
1201 #if DEBUG_TRANSPORT
1202   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1203               "Scheduling task to reconnect to transport service in %llu ms.\n",
1204               h->reconnect_delay.value);
1205 #endif
1206   GNUNET_assert (h->client == NULL);
1207   GNUNET_assert (h->reconnect_task == GNUNET_SCHEDULER_NO_TASK);
1208   h->reconnect_task
1209     = GNUNET_SCHEDULER_add_delayed (h->sched,
1210                                     h->reconnect_delay, &reconnect, h);
1211   h->reconnect_delay = GNUNET_TIME_UNIT_SECONDS;
1212 }
1213
1214
1215 /**
1216  * We are connected to the respective peer, check the
1217  * bandwidth limits and schedule the transmission.
1218  */
1219 static void schedule_request (struct GNUNET_TRANSPORT_TransmitHandle *th);
1220
1221
1222 /**
1223  * Function called by the scheduler when the timeout
1224  * for bandwidth availablility for the target
1225  * neighbour is reached.
1226  */
1227 static void
1228 transmit_ready (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1229 {
1230   struct GNUNET_TRANSPORT_TransmitHandle *th = cls;
1231
1232   th->notify_delay_task = GNUNET_SCHEDULER_NO_TASK;
1233   schedule_request (th);
1234 }
1235
1236
1237 /**
1238  * Remove the given transmit handle from the wait list.  Does NOT free
1239  * it.
1240  */
1241 static void
1242 remove_from_wait_list (struct GNUNET_TRANSPORT_TransmitHandle *th)
1243 {
1244   if (th->prev == NULL)
1245     th->handle->connect_wait_head = th->next;
1246   else
1247     th->prev->next = th->next;
1248   if (th->next != NULL)
1249     th->next->prev = th->prev;
1250 }
1251
1252
1253 /**
1254  * We are connected to the respective peer, check the
1255  * bandwidth limits and schedule the transmission.
1256  */
1257 static void
1258 schedule_request (struct GNUNET_TRANSPORT_TransmitHandle *th)
1259 {
1260   struct GNUNET_TRANSPORT_Handle *h;
1261   struct GNUNET_TIME_Relative duration;
1262   struct NeighbourList *n;
1263   uint64_t available;
1264
1265   h = th->handle;
1266   n = th->neighbour;
1267   if (th->notify_delay_task != GNUNET_SCHEDULER_NO_TASK)
1268     {
1269       GNUNET_SCHEDULER_cancel (h->sched, th->notify_delay_task);
1270       th->notify_delay_task = GNUNET_SCHEDULER_NO_TASK;
1271     }
1272   /* check outgoing quota */
1273   duration = GNUNET_TIME_absolute_get_duration (n->last_quota_update);
1274   if (duration.value > MIN_QUOTA_REFRESH_TIME)
1275     {
1276       update_quota (n);
1277       duration = GNUNET_TIME_absolute_get_duration (n->last_quota_update);
1278     }
1279   available = duration.value * n->quota_out;
1280   if (available < n->last_sent + th->notify_size)
1281     {
1282       /* calculate how much bandwidth we'd still need to
1283          accumulate and based on that how long we'll have
1284          to wait... */
1285       available = n->last_sent + th->notify_size - available;
1286       duration = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS,
1287                                                 available / n->quota_out);
1288       if (th->timeout.value <
1289           GNUNET_TIME_relative_to_absolute (duration).value)
1290         {
1291           /* signal timeout! */
1292 #if DEBUG_TRANSPORT
1293           GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1294                       "Would need %llu ms before bandwidth is available for delivery to `%4s', that is too long.  Signaling timeout.\n",
1295                       duration.value,
1296                       GNUNET_i2s(&th->target));
1297 #endif
1298           remove_from_wait_list (th);
1299           th->notify (th->notify_cls, 0, NULL);
1300           GNUNET_free (th);
1301           return;
1302         }
1303 #if DEBUG_TRANSPORT
1304       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1305                   "Need more bandwidth, delaying delivery to `%4s' by %llu ms\n",
1306                   GNUNET_i2s(&th->target),
1307                   duration.value);
1308 #endif
1309       th->notify_delay_task
1310         = GNUNET_SCHEDULER_add_delayed (h->sched,
1311                                         duration, &transmit_ready, th);
1312       return;
1313     }
1314 #if DEBUG_TRANSPORT
1315   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1316               "Bandwidth available for transmission to `%4s'\n",
1317               GNUNET_i2s (&n->id));
1318 #endif
1319   if (GNUNET_NO == n->transmit_ok)
1320     {
1321       /* we may be ready, but transport service is not;
1322          wait for SendOkMessage or timeout */
1323 #if DEBUG_TRANSPORT
1324       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1325                   "Need to wait for transport service `%s' message\n",
1326                   "SEND_OK");
1327 #endif
1328       th->notify_delay_task
1329         = GNUNET_SCHEDULER_add_delayed (h->sched,
1330                                         GNUNET_TIME_absolute_get_remaining
1331                                         (th->timeout), &peer_transmit_timeout, th);
1332       return;
1333     }
1334   n->transmit_ok = GNUNET_NO;
1335   remove_from_wait_list (th);
1336 #if DEBUG_TRANSPORT
1337   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Moving message for `%4s' to ready list\n",
1338               GNUNET_i2s(&n->id));
1339 #endif
1340   insert_transmit_handle (&h->connect_ready_head, th);
1341   if (GNUNET_NO == h->transmission_scheduled)
1342     schedule_transmission (h);
1343 }
1344
1345
1346 /**
1347  * Add neighbour to our list
1348  */
1349 static void
1350 add_neighbour (struct GNUNET_TRANSPORT_Handle *h,
1351                uint32_t quota_out,
1352                struct GNUNET_TIME_Relative latency,
1353                const struct GNUNET_PeerIdentity *pid)
1354 {
1355   struct NeighbourList *n;
1356   struct GNUNET_TRANSPORT_TransmitHandle *prev;
1357   struct GNUNET_TRANSPORT_TransmitHandle *pos;
1358   struct GNUNET_TRANSPORT_TransmitHandle *next;
1359
1360   /* check for duplicates */
1361   if (NULL != find_neighbour (h, pid))
1362     {
1363       GNUNET_break (0);
1364       return;
1365     }
1366 #if DEBUG_TRANSPORT
1367   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1368               "Creating entry for new neighbour `%4s'.\n", GNUNET_i2s (pid));
1369 #endif
1370   n = GNUNET_malloc (sizeof (struct NeighbourList));
1371   n->id = *pid;
1372   n->last_quota_update = GNUNET_TIME_absolute_get ();
1373   n->quota_out = quota_out;
1374   n->next = h->neighbours;
1375   n->transmit_ok = GNUNET_YES;
1376   h->neighbours = n;
1377   if (h->nc_cb != NULL)
1378     h->nc_cb (h->cls, &n->id, latency);
1379   prev = NULL;
1380   pos = h->connect_wait_head;
1381   while (pos != NULL)
1382     {
1383       next = pos->next;
1384       if (0 == memcmp (pid,
1385                        &pos->target, sizeof (struct GNUNET_PeerIdentity)))
1386         {
1387           pos->neighbour = n;
1388           GNUNET_assert (NULL == n->transmit_handle);
1389           n->transmit_handle = pos;
1390           if (prev == NULL)
1391             h->connect_wait_head = next;
1392           else
1393             prev->next = next;
1394           if (GNUNET_YES == n->received_ack)
1395             {
1396 #if DEBUG_TRANSPORT
1397               GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1398                           "Found pending request for `%4s' will trigger it now.\n",
1399                           GNUNET_i2s (&pos->target));
1400 #endif
1401               if (pos->notify_delay_task != GNUNET_SCHEDULER_NO_TASK)
1402                 {
1403                   GNUNET_SCHEDULER_cancel (h->sched, pos->notify_delay_task);
1404                   pos->notify_delay_task = GNUNET_SCHEDULER_NO_TASK;
1405                 }
1406               schedule_request (pos);
1407             }
1408           else
1409             {
1410 #if DEBUG_TRANSPORT
1411               GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1412                           "Found pending request for `%4s' but still need `%s' before proceeding.\n",
1413                           GNUNET_i2s (&pos->target),
1414                           "ACK");
1415 #endif
1416             }
1417           break;
1418         }
1419       prev = pos;
1420       pos = next;
1421     }
1422 }
1423
1424
1425 /**
1426  * Connect to the transport service.  Note that the connection may
1427  * complete (or fail) asynchronously.
1428  *
1429
1430  * @param sched scheduler to use
1431  * @param cfg configuration to use
1432  * @param cls closure for the callbacks
1433  * @param rec receive function to call
1434  * @param nc function to call on connect events
1435  * @param nd function to call on disconnect events
1436  */
1437 struct GNUNET_TRANSPORT_Handle *
1438 GNUNET_TRANSPORT_connect (struct GNUNET_SCHEDULER_Handle *sched,
1439                           const struct GNUNET_CONFIGURATION_Handle *cfg,
1440                           void *cls,
1441                           GNUNET_TRANSPORT_ReceiveCallback rec,
1442                           GNUNET_TRANSPORT_NotifyConnect nc,
1443                           GNUNET_TRANSPORT_NotifyDisconnect nd)
1444 {
1445   struct GNUNET_TRANSPORT_Handle *ret;
1446
1447   GNUNET_ARM_start_services (cfg, sched, "peerinfo", "transport", NULL);
1448   ret = GNUNET_malloc (sizeof (struct GNUNET_TRANSPORT_Handle));
1449   ret->sched = sched;
1450   ret->cfg = cfg;
1451   ret->cls = cls;
1452   ret->rec = rec;
1453   ret->nc_cb = nc;
1454   ret->nd_cb = nd;
1455   ret->reconnect_delay = GNUNET_TIME_UNIT_ZERO;
1456   schedule_reconnect (ret);
1457   return ret;
1458 }
1459
1460
1461 /**
1462  * Disconnect from the transport service.
1463  */
1464 void
1465 GNUNET_TRANSPORT_disconnect (struct GNUNET_TRANSPORT_Handle *handle)
1466 {
1467   struct GNUNET_TRANSPORT_TransmitHandle *th;
1468   struct NeighbourList *n;
1469   struct HelloWaitList *hwl;
1470   struct GNUNET_CLIENT_Connection *client;
1471
1472 #if DEBUG_TRANSPORT
1473   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 
1474               "Transport disconnect called!\n");
1475 #endif
1476   while (NULL != (th = handle->connect_ready_head))
1477     {
1478       handle->connect_ready_head = th->next;
1479       if (th->notify_delay_task != GNUNET_SCHEDULER_NO_TASK)
1480         {
1481           GNUNET_SCHEDULER_cancel (handle->sched, th->notify_delay_task);
1482           th->notify_delay_task = GNUNET_SCHEDULER_NO_TASK;
1483         }
1484       th->notify (th->notify_cls, 0, NULL);
1485       GNUNET_free (th);
1486     }
1487   while (NULL != (th = handle->connect_wait_head))
1488     {
1489       handle->connect_wait_head = th->next;
1490       if (th->notify_delay_task != GNUNET_SCHEDULER_NO_TASK)
1491         {
1492           GNUNET_SCHEDULER_cancel (handle->sched, th->notify_delay_task);
1493           th->notify_delay_task = GNUNET_SCHEDULER_NO_TASK;
1494         }
1495       th->notify (th->notify_cls, 0, NULL);
1496       GNUNET_free (th);
1497     }
1498   while (NULL != (n = handle->neighbours))
1499     {
1500       handle->neighbours = n->next;
1501       GNUNET_free (n);
1502     }
1503   while (NULL != (hwl = handle->hwl_head))
1504     {
1505       handle->hwl_head = hwl->next;
1506       GNUNET_SCHEDULER_cancel (handle->sched, hwl->task);
1507       GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1508                   _("Disconnect while trying to obtain `%s' from transport service.\n"),
1509                   "HELLO");
1510       if (hwl->rec != NULL)
1511         hwl->rec (hwl->rec_cls, GNUNET_TIME_UNIT_ZERO, NULL, NULL);
1512       GNUNET_free (hwl);
1513     }
1514   if (handle->reconnect_task != GNUNET_SCHEDULER_NO_TASK)
1515     {
1516       GNUNET_SCHEDULER_cancel (handle->sched, handle->reconnect_task);
1517       handle->reconnect_task = GNUNET_SCHEDULER_NO_TASK;
1518     }
1519   GNUNET_free_non_null (handle->my_hello);
1520   handle->my_hello = NULL;
1521   GNUNET_ARM_stop_services (handle->cfg, handle->sched, "transport", "peerinfo", NULL);
1522   if (NULL != handle->network_handle)
1523     {
1524       GNUNET_CLIENT_notify_transmit_ready_cancel (handle->network_handle);
1525       handle->network_handle = NULL;
1526     }
1527   if (NULL != (client = handle->client))
1528     {
1529 #if DEBUG_TRANSPORT
1530       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1531                   "Disconnecting from transport service for good.\n");
1532 #endif
1533       handle->client = NULL;
1534       GNUNET_CLIENT_disconnect (client);
1535     }
1536   GNUNET_free (handle);
1537 }
1538
1539
1540 /**
1541  * Type of a function to call when we receive a message
1542  * from the service.
1543  *
1544  * @param cls closure
1545  * @param msg message received, NULL on timeout or fatal error
1546  */
1547 static void
1548 demultiplexer (void *cls, const struct GNUNET_MessageHeader *msg)
1549 {
1550   struct GNUNET_TRANSPORT_Handle *h = cls;
1551   const struct DisconnectInfoMessage *dim;
1552   const struct ConnectInfoMessage *cim;
1553   const struct InboundMessage *im;
1554   const struct GNUNET_MessageHeader *imm;
1555   const struct SendOkMessage *okm;
1556   struct HelloWaitList *hwl;
1557   struct NeighbourList *n;
1558   struct GNUNET_PeerIdentity me;
1559   struct GNUNET_TRANSPORT_TransmitHandle *th;
1560   uint16_t size;
1561
1562   if ((msg == NULL) || (h->client == NULL))
1563     {
1564       if (h->client != NULL)
1565         {
1566 #if DEBUG_TRANSPORT
1567           GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1568                       "Error receiving from transport service, disconnecting temporarily.\n");
1569 #endif
1570           if (h->network_handle != NULL)
1571             {
1572               GNUNET_CLIENT_notify_transmit_ready_cancel (h->network_handle);
1573               h->network_handle = NULL;
1574               h->transmission_scheduled = GNUNET_NO;
1575               th = h->connect_ready_head;
1576               /* add timeout again, we cancelled the transmit_ready task! */
1577               GNUNET_assert (th->notify_delay_task == GNUNET_SCHEDULER_NO_TASK);
1578               th->notify_delay_task 
1579                 = GNUNET_SCHEDULER_add_delayed (h->sched,
1580                                                 GNUNET_TIME_absolute_get_remaining(th->timeout),
1581                                                 &peer_transmit_timeout, 
1582                                                 th);    
1583             }
1584           GNUNET_CLIENT_disconnect (h->client);
1585           h->client = NULL;
1586           schedule_reconnect (h);
1587         }
1588       else
1589         {
1590           /* shutdown initiated from 'GNUNET_TRANSPORT_disconnect',
1591              finish clean up work! */
1592           GNUNET_free (h);
1593         }
1594       return;
1595     }
1596   GNUNET_CLIENT_receive (h->client,
1597                          &demultiplexer, h, GNUNET_TIME_UNIT_FOREVER_REL);
1598   size = ntohs (msg->size);
1599   switch (ntohs (msg->type))
1600     {
1601     case GNUNET_MESSAGE_TYPE_HELLO:
1602       if (GNUNET_OK !=
1603           GNUNET_HELLO_get_id ((const struct GNUNET_HELLO_Message *) msg,
1604                                &me))
1605         {
1606           GNUNET_break (0);
1607           break;
1608         }
1609 #if DEBUG_TRANSPORT
1610       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1611                   "Receiving (my own) `%s' message, I am `%4s'.\n",
1612                   "HELLO", GNUNET_i2s (&me));
1613 #endif
1614       GNUNET_free_non_null (h->my_hello);
1615       h->my_hello = NULL;
1616       if (size < sizeof (struct GNUNET_MessageHeader))
1617         {
1618           GNUNET_break (0);
1619           break;
1620         }
1621       h->my_hello = GNUNET_malloc (size);
1622       memcpy (h->my_hello, msg, size);
1623       while (NULL != (hwl = h->hwl_head))
1624         {
1625           h->hwl_head = hwl->next;
1626           GNUNET_SCHEDULER_cancel (h->sched, hwl->task);
1627           GNUNET_TRANSPORT_get_hello (h,
1628                                       GNUNET_TIME_UNIT_ZERO,
1629                                       hwl->rec, hwl->rec_cls);
1630           GNUNET_free (hwl);
1631         }
1632       break;
1633     case GNUNET_MESSAGE_TYPE_TRANSPORT_CONNECT:
1634       if (size != sizeof (struct ConnectInfoMessage))
1635         {
1636           GNUNET_break (0);
1637           break;
1638         }
1639       cim = (const struct ConnectInfoMessage *) msg;
1640 #if DEBUG_TRANSPORT
1641       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1642                   "Receiving `%s' message for `%4s'.\n",
1643                   "CONNECT", GNUNET_i2s (&cim->id));
1644 #endif
1645       add_neighbour (h,
1646                      ntohl (cim->quota_out),
1647                      GNUNET_TIME_relative_ntoh (cim->latency), &cim->id);
1648       break;
1649     case GNUNET_MESSAGE_TYPE_TRANSPORT_DISCONNECT:
1650       if (size != sizeof (struct DisconnectInfoMessage))
1651         {
1652           GNUNET_break (0);
1653           break;
1654         }
1655       dim = (const struct DisconnectInfoMessage *) msg;
1656 #if DEBUG_TRANSPORT
1657       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1658                   "Receiving `%s' message for `%4s'.\n",
1659                   "DISCONNECT", GNUNET_i2s (&dim->peer));
1660 #endif
1661       remove_neighbour (h, &dim->peer);
1662       break;
1663     case GNUNET_MESSAGE_TYPE_TRANSPORT_SEND_OK:
1664       if (size != sizeof (struct SendOkMessage))
1665         {
1666           GNUNET_break (0);
1667           break;
1668         }
1669       okm = (const struct SendOkMessage *) msg;
1670 #if DEBUG_TRANSPORT
1671       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1672                   "Receiving `%s' message, transmission %s.\n", "SEND_OK",
1673                   ntohl(okm->success) == GNUNET_OK ? "succeeded" : "failed");
1674 #endif
1675       n = find_neighbour (h, &okm->peer);
1676       GNUNET_assert (n != NULL);
1677       n->transmit_ok = GNUNET_YES;
1678       if (n->transmit_handle != NULL)
1679         {
1680 #if DEBUG_TRANSPORT
1681           GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1682                       "Processing pending message for `%4s'\n",
1683                       GNUNET_i2s(&n->id));
1684 #endif
1685           GNUNET_SCHEDULER_cancel (h->sched,
1686                                    n->transmit_handle->notify_delay_task);
1687           n->transmit_handle->notify_delay_task =
1688             GNUNET_SCHEDULER_NO_TASK;
1689           GNUNET_assert (GNUNET_YES == n->received_ack);
1690           schedule_request (n->transmit_handle);
1691         }
1692       break;
1693     case GNUNET_MESSAGE_TYPE_TRANSPORT_RECV:
1694 #if DEBUG_TRANSPORT
1695       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1696                   "Receiving `%s' message.\n", "RECV");
1697 #endif
1698       if (size <
1699           sizeof (struct InboundMessage) +
1700           sizeof (struct GNUNET_MessageHeader))
1701         {
1702           GNUNET_break (0);
1703           break;
1704         }
1705       im = (const struct InboundMessage *) msg;
1706       imm = (const struct GNUNET_MessageHeader *) &im[1];
1707       if (ntohs (imm->size) + sizeof (struct InboundMessage) != size)
1708         {
1709           GNUNET_break (0);
1710           break;
1711         }
1712       switch (ntohs (imm->type))
1713         {
1714         case GNUNET_MESSAGE_TYPE_TRANSPORT_ACK:
1715 #if DEBUG_TRANSPORT
1716           GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1717                       "Receiving `%s' message from `%4s'.\n",
1718                       "ACK", GNUNET_i2s (&im->peer));
1719 #endif
1720           n = find_neighbour (h, &im->peer);
1721           if (n == NULL)
1722             {
1723               GNUNET_break (0);
1724               break;
1725             }
1726           if (n->received_ack == GNUNET_NO)
1727             {
1728               n->received_ack = GNUNET_YES;
1729               if (NULL != n->transmit_handle)
1730                 {
1731 #if DEBUG_TRANSPORT
1732                   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1733                               "Peer connected, scheduling delayed message for deliverery now.\n");
1734 #endif
1735                   schedule_request (n->transmit_handle);
1736                 }
1737             }
1738           break;
1739         default:
1740 #if DEBUG_TRANSPORT
1741           GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1742                       "Received message of type %u from `%4s'.\n",
1743                       ntohs (imm->type), GNUNET_i2s (&im->peer));
1744 #endif
1745           if (h->rec != NULL)
1746             h->rec (h->cls,
1747                     GNUNET_TIME_relative_ntoh (im->latency), &im->peer, imm);
1748           break;
1749         }
1750       break;
1751     default:
1752       GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1753                   _
1754                   ("Received unexpected message of type %u in %s:%u\n"),
1755                   ntohs (msg->type), __FILE__, __LINE__);
1756       GNUNET_break (0);
1757       break;
1758     }
1759 }
1760
1761
1762 struct ClientTransmitWrapper
1763 {
1764   GNUNET_CONNECTION_TransmitReadyNotify notify;
1765   void *notify_cls;
1766   struct GNUNET_TRANSPORT_TransmitHandle *th;
1767 };
1768
1769
1770 /**
1771  * Transmit message of a client destined for another
1772  * peer to the service.
1773  */
1774 static size_t
1775 client_notify_wrapper (void *cls, size_t size, void *buf)
1776 {
1777   struct ClientTransmitWrapper *ctw = cls;
1778   struct OutboundMessage *obm;
1779   struct GNUNET_MessageHeader *hdr;
1780   size_t ret;
1781
1782   if (size == 0)
1783     {
1784 #if DEBUG_TRANSPORT
1785       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1786                   "Transmission request could not be satisfied.\n");
1787 #endif
1788       ret = ctw->notify (ctw->notify_cls, 0, NULL);
1789       GNUNET_assert (ret == 0);
1790       GNUNET_free (ctw);
1791       return 0;
1792     }
1793   GNUNET_assert (size >= sizeof (struct OutboundMessage));
1794   obm = buf;
1795   ret = ctw->notify (ctw->notify_cls,
1796                      size - sizeof (struct OutboundMessage),
1797                      (void *) &obm[1]);
1798   if (ret == 0)
1799     {
1800       /* Need to reset flag, no SEND means no SEND_OK! */
1801       ctw->th->neighbour->transmit_ok = GNUNET_YES;
1802       GNUNET_free (ctw);
1803       return 0;
1804     }
1805   GNUNET_assert (ret >= sizeof (struct GNUNET_MessageHeader));
1806   hdr = (struct GNUNET_MessageHeader *) &obm[1];
1807   GNUNET_assert (ntohs (hdr->size) == ret);
1808   GNUNET_assert (ret + sizeof (struct OutboundMessage) <
1809                  GNUNET_SERVER_MAX_MESSAGE_SIZE);
1810 #if DEBUG_TRANSPORT
1811   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1812               "Transmitting `%s' message with data for `%4s'\n",
1813               "SEND", GNUNET_i2s (&ctw->th->target));
1814 #endif
1815   ret += sizeof (struct OutboundMessage);
1816   obm->header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_SEND);
1817   obm->header.size = htons (ret);
1818   obm->priority = htonl (ctw->th->priority);
1819   obm->peer = ctw->th->target;
1820   GNUNET_free (ctw);
1821   return ret;
1822 }
1823
1824
1825
1826 /**
1827  * Check if we could queue a message of the given size for
1828  * transmission.  The transport service will take both its
1829  * internal buffers and bandwidth limits imposed by the
1830  * other peer into consideration when answering this query.
1831  *
1832  * @param handle connection to transport service
1833  * @param target who should receive the message
1834  * @param size how big is the message we want to transmit?
1835  * @param priority how important is the message?
1836  * @param timeout after how long should we give up (and call
1837  *        notify with buf NULL and size 0)?
1838  * @param notify function to call when we are ready to
1839  *        send such a message
1840  * @param notify_cls closure for notify
1841  * @return NULL if someone else is already waiting to be notified
1842  *         non-NULL if the notify callback was queued (can be used to cancel
1843  *         using GNUNET_TRANSPORT_notify_transmit_ready_cancel)
1844  */
1845 struct GNUNET_TRANSPORT_TransmitHandle *
1846 GNUNET_TRANSPORT_notify_transmit_ready (struct GNUNET_TRANSPORT_Handle
1847                                         *handle,
1848                                         const struct GNUNET_PeerIdentity
1849                                         *target, size_t size,
1850                                         unsigned int priority,
1851                                         struct GNUNET_TIME_Relative timeout,
1852                                         GNUNET_CONNECTION_TransmitReadyNotify
1853                                         notify, void *notify_cls)
1854 {
1855   struct GNUNET_TRANSPORT_TransmitHandle *pos;
1856   struct GNUNET_TRANSPORT_TransmitHandle *th;
1857   struct NeighbourList *n;
1858   struct ClientTransmitWrapper *ctw;
1859
1860   if (size + sizeof (struct OutboundMessage) >=
1861       GNUNET_SERVER_MAX_MESSAGE_SIZE)
1862     {
1863       GNUNET_break (0);
1864       return NULL;
1865     }
1866 #if DEBUG_TRANSPORT
1867   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1868               "Asking transport service for transmission of %u bytes to peer `%4s'.\n",
1869               size, GNUNET_i2s (target));
1870 #endif
1871   n = find_neighbour (handle, target);
1872   if ( (n != NULL) &&
1873        (n->transmit_handle != NULL) )
1874     return NULL; /* already have a request pending for this peer! */
1875   ctw = GNUNET_malloc (sizeof (struct ClientTransmitWrapper));
1876   th = GNUNET_malloc (sizeof (struct GNUNET_TRANSPORT_TransmitHandle));
1877   ctw->notify = notify;
1878   ctw->notify_cls = notify_cls;
1879   ctw->th = th;
1880   th->handle = handle;
1881   th->neighbour = n;
1882   th->target = *target;
1883   th->notify = &client_notify_wrapper;
1884   th->notify_cls = ctw;
1885   th->timeout = GNUNET_TIME_relative_to_absolute (timeout);
1886   th->notify_size = size + sizeof (struct OutboundMessage);
1887   th->priority = priority;
1888   if (NULL == n)
1889     {
1890       pos = handle->connect_wait_head;
1891       while (pos != NULL)
1892         {
1893           GNUNET_assert (0 != memcmp (target,
1894                                       &pos->target,
1895                                       sizeof (struct GNUNET_PeerIdentity)));
1896           pos = pos->next;
1897         }
1898 #if DEBUG_TRANSPORT
1899       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1900                   "Will now try to connect to `%4s'.\n", GNUNET_i2s (target));
1901 #endif
1902       try_connect (th);
1903       return th;
1904     }
1905
1906 #if DEBUG_TRANSPORT
1907   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1908               "Transmission request queued for transmission to transport service.\n");
1909 #endif
1910   GNUNET_assert (NULL == n->transmit_handle);
1911   n->transmit_handle = th;
1912   if (GNUNET_YES != n->received_ack)
1913     {
1914 #if DEBUG_TRANSPORT
1915       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1916                   "Connection to `%4s' is not yet confirmed connected, scheduling timeout (%llu ms) only.\n",
1917                   GNUNET_i2s (target), timeout.value);
1918 #endif
1919       th->notify_delay_task
1920         = GNUNET_SCHEDULER_add_delayed (handle->sched,
1921                                         timeout, &peer_transmit_timeout, th);
1922       return th;
1923     }
1924   
1925 #if DEBUG_TRANSPORT
1926   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1927               "Peer `%4s' is ready to receive, scheduling message for delivery now.\n",
1928               GNUNET_i2s (target));
1929 #endif
1930   th->notify_delay_task
1931     = GNUNET_SCHEDULER_add_now (handle->sched,
1932                                 &transmit_ready, th);
1933   return th;
1934 }
1935
1936
1937 /**
1938  * Cancel the specified transmission-ready notification.
1939  */
1940 void
1941 GNUNET_TRANSPORT_notify_transmit_ready_cancel (struct
1942                                                GNUNET_TRANSPORT_TransmitHandle
1943                                                *th)
1944 {
1945   struct GNUNET_TRANSPORT_Handle *h;
1946
1947 #if DEBUG_TRANSPORT
1948   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1949               "Transmission request of %u bytes to `%4s' was cancelled.\n",
1950               th->notify_size - sizeof(struct OutboundMessage),
1951               GNUNET_i2s (&th->target));
1952 #endif
1953   GNUNET_assert (th->notify == &client_notify_wrapper);
1954   remove_from_any_list (th);
1955   h = th->handle;
1956   if ((h->connect_ready_head == NULL) && (h->network_handle != NULL))
1957     {
1958       GNUNET_CLIENT_notify_transmit_ready_cancel (h->network_handle);
1959       h->network_handle = NULL;
1960       h->transmission_scheduled = GNUNET_NO;
1961     }
1962   GNUNET_free (th->notify_cls);
1963   GNUNET_assert (th->notify_delay_task == GNUNET_SCHEDULER_NO_TASK);
1964   GNUNET_free (th);
1965 }
1966
1967
1968 /* end of transport_api.c */