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