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