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