-fix compiler warning
[oweals/gnunet.git] / src / transport / transport_api_core.c
1 /*
2      This file is part of GNUnet.
3      Copyright (C) 2009-2013, 2016 GNUnet e.V.
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 3, 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., 51 Franklin Street, Fifth Floor,
18      Boston, MA 02110-1301, USA.
19 */
20
21 /**
22  * @file transport/transport_api_core.c
23  * @brief library to access the transport service for message exchange
24  * @author Christian Grothoff
25  */
26 #include "platform.h"
27 #include "gnunet_util_lib.h"
28 #include "gnunet_constants.h"
29 #include "gnunet_arm_service.h"
30 #include "gnunet_hello_lib.h"
31 #include "gnunet_protocols.h"
32 #include "gnunet_transport_core_service.h"
33 #include "transport.h"
34
35 #define LOG(kind,...) GNUNET_log_from (kind, "transport-api-core",__VA_ARGS__)
36
37 /**
38  * If we could not send any payload to a peer for this amount of
39  * time, we print a warning.
40  */
41 #define UNREADY_WARN_TIME GNUNET_TIME_UNIT_MINUTES
42
43 /**
44  * How large to start with for the hashmap of neighbours.
45  */
46 #define STARTING_NEIGHBOURS_SIZE 16
47
48
49 /**
50  * Entry in hash table of all of our current (connected) neighbours.
51  */
52 struct Neighbour
53 {
54   /**
55    * Overall transport handle.
56    */
57   struct GNUNET_TRANSPORT_CoreHandle *h;
58
59   /**
60    * Active message queue for the peer.
61    */
62   struct GNUNET_MQ_Handle *mq;
63
64   /**
65    * Envelope with the message we are currently transmitting (or NULL).
66    */
67   struct GNUNET_MQ_Envelope *env;
68
69   /**
70    * Closure for @e mq handlers.
71    */
72   void *handlers_cls;
73
74   /**
75    * Identity of this neighbour.
76    */
77   struct GNUNET_PeerIdentity id;
78
79   /**
80    * Outbound bandwidh tracker.
81    */
82   struct GNUNET_BANDWIDTH_Tracker out_tracker;
83
84   /**
85    * Entry in our readyness heap (which is sorted by @e next_ready
86    * value).  NULL if there is no pending transmission request for
87    * this neighbour or if we're waiting for @e is_ready to become
88    * true AFTER the @e out_tracker suggested that this peer's quota
89    * has been satisfied (so once @e is_ready goes to #GNUNET_YES,
90    * we should immediately go back into the heap).
91    */
92   struct GNUNET_CONTAINER_HeapNode *hn;
93
94   /**
95    * Task to trigger MQ when we have enough bandwidth for the
96    * next transmission.
97    */
98   struct GNUNET_SCHEDULER_Task *timeout_task;
99
100   /**
101    * Sending consumed more bytes on wire than payload was announced
102    * This overhead is added to the delay of next sending operation
103    */
104   unsigned long long traffic_overhead;
105
106   /**
107    * Is this peer currently ready to receive a message?
108    */
109   int is_ready;
110
111   /**
112    * Size of the message in @e env.
113    */
114   uint16_t env_size;
115
116 };
117
118
119
120 /**
121  * Handle for the transport service (includes all of the
122  * state for the transport service).
123  */
124 struct GNUNET_TRANSPORT_CoreHandle
125 {
126
127   /**
128    * Closure for the callbacks.
129    */
130   void *cls;
131
132   /**
133    * Functions to call for received data (template for
134    * new message queues).
135    */
136   struct GNUNET_MQ_MessageHandler *handlers;
137
138   /**
139    * function to call on connect events
140    */
141   GNUNET_TRANSPORT_NotifyConnecT nc_cb;
142
143   /**
144    * function to call on disconnect events
145    */
146   GNUNET_TRANSPORT_NotifyDisconnecT nd_cb;
147
148   /**
149    * function to call on excess bandwidth events
150    */
151   GNUNET_TRANSPORT_NotifyExcessBandwidtH neb_cb;
152
153   /**
154    * My client connection to the transport service.
155    */
156   struct GNUNET_MQ_Handle *mq;
157
158   /**
159    * My configuration.
160    */
161   const struct GNUNET_CONFIGURATION_Handle *cfg;
162
163   /**
164    * Hash map of the current connected neighbours of this peer.
165    * Maps peer identities to `struct Neighbour` entries.
166    */
167   struct GNUNET_CONTAINER_MultiPeerMap *neighbours;
168
169   /**
170    * Peer identity as assumed by this process, or all zeros.
171    */
172   struct GNUNET_PeerIdentity self;
173
174   /**
175    * ID of the task trying to reconnect to the service.
176    */
177   struct GNUNET_SCHEDULER_Task *reconnect_task;
178
179   /**
180    * Delay until we try to reconnect.
181    */
182   struct GNUNET_TIME_Relative reconnect_delay;
183
184   /**
185    * Should we check that @e self matches what the service thinks?
186    * (if #GNUNET_NO, then @e self is all zeros!).
187    */
188   int check_self;
189
190 };
191
192
193 /**
194  * Function that will schedule the job that will try
195  * to connect us again to the client.
196  *
197  * @param h transport service to reconnect
198  */
199 static void
200 disconnect_and_schedule_reconnect (struct GNUNET_TRANSPORT_CoreHandle *h);
201
202
203 /**
204  * Get the neighbour list entry for the given peer
205  *
206  * @param h our context
207  * @param peer peer to look up
208  * @return NULL if no such peer entry exists
209  */
210 static struct Neighbour *
211 neighbour_find (struct GNUNET_TRANSPORT_CoreHandle *h,
212                 const struct GNUNET_PeerIdentity *peer)
213 {
214   return GNUNET_CONTAINER_multipeermap_get (h->neighbours,
215                                             peer);
216 }
217
218
219 /**
220  * Function called by the bandwidth tracker if we have excess
221  * bandwidth.
222  *
223  * @param cls the `struct Neighbour` that has excess bandwidth
224  */
225 static void
226 notify_excess_cb (void *cls)
227 {
228   struct Neighbour *n = cls;
229   struct GNUNET_TRANSPORT_CoreHandle *h = n->h;
230
231   LOG (GNUNET_ERROR_TYPE_DEBUG,
232        "Notifying CORE that more bandwidth is available for %s\n",
233        GNUNET_i2s (&n->id));
234
235   if (NULL != h->neb_cb)
236     h->neb_cb (h->cls,
237                &n->id,
238                n->handlers_cls);
239 }
240
241
242 /**
243  * Iterator over hash map entries, for deleting state of a neighbour.
244  *
245  * @param cls the `struct GNUNET_TRANSPORT_CoreHandle *`
246  * @param key peer identity
247  * @param value value in the hash map, the neighbour entry to delete
248  * @return #GNUNET_YES if we should continue to
249  *         iterate,
250  *         #GNUNET_NO if not.
251  */
252 static int
253 neighbour_delete (void *cls,
254                   const struct GNUNET_PeerIdentity *key,
255                   void *value)
256 {
257   struct GNUNET_TRANSPORT_CoreHandle *handle = cls;
258   struct Neighbour *n = value;
259
260   LOG (GNUNET_ERROR_TYPE_DEBUG,
261        "Dropping entry for neighbour `%s'.\n",
262        GNUNET_i2s (key));
263   GNUNET_BANDWIDTH_tracker_notification_stop (&n->out_tracker);
264   if (NULL != handle->nd_cb)
265     handle->nd_cb (handle->cls,
266                    &n->id,
267                    n->handlers_cls);
268   if (NULL != n->timeout_task)
269   {
270     GNUNET_SCHEDULER_cancel (n->timeout_task);
271     n->timeout_task = NULL;
272   }
273   if (NULL != n->env)
274   {
275     GNUNET_MQ_send_cancel (n->env);
276     n->env = NULL;
277   }
278   GNUNET_MQ_destroy (n->mq);
279   GNUNET_assert (NULL == n->mq);
280   GNUNET_assert (GNUNET_YES ==
281                  GNUNET_CONTAINER_multipeermap_remove (handle->neighbours,
282                                                        key,
283                                                        n));
284   GNUNET_free (n);
285   return GNUNET_YES;
286 }
287
288
289 /**
290  * Generic error handler, called with the appropriate
291  * error code and the same closure specified at the creation of
292  * the message queue.
293  * Not every message queue implementation supports an error handler.
294  *
295  * @param cls closure with the `struct GNUNET_TRANSPORT_CoreHandle *`
296  * @param error error code
297  */
298 static void
299 mq_error_handler (void *cls,
300                   enum GNUNET_MQ_Error error)
301 {
302   struct GNUNET_TRANSPORT_CoreHandle *h = cls;
303
304   LOG (GNUNET_ERROR_TYPE_DEBUG,
305        "Error receiving from transport service, disconnecting temporarily.\n");
306   disconnect_and_schedule_reconnect (h);
307 }
308
309
310 /**
311  * Function we use for checking incoming HELLO messages.
312  *
313  * @param cls closure, a `struct GNUNET_TRANSPORT_CoreHandle *`
314  * @param msg message received
315  * @return #GNUNET_OK if message is well-formed
316  */
317 static int
318 check_hello (void *cls,
319              const struct GNUNET_MessageHeader *msg)
320 {
321   struct GNUNET_PeerIdentity me;
322
323   if (GNUNET_OK !=
324       GNUNET_HELLO_get_id ((const struct GNUNET_HELLO_Message *) msg,
325                            &me))
326   {
327     GNUNET_break (0);
328     return GNUNET_SYSERR;
329   }
330   return GNUNET_OK;
331 }
332
333
334 /**
335  * Function we use for handling incoming HELLO messages.
336  *
337  * @param cls closure, a `struct GNUNET_TRANSPORT_CoreHandle *`
338  * @param msg message received
339  */
340 static void
341 handle_hello (void *cls,
342               const struct GNUNET_MessageHeader *msg)
343 {
344   /* we do not care => FIXME: signal in options to NEVER send HELLOs! */
345 }
346
347
348 /**
349  * A message from the handler's message queue to a neighbour was
350  * transmitted.  Now trigger (possibly delayed) notification of the
351  * neighbour's message queue that we are done and thus ready for
352  * the next message.
353  *
354  * @param cls the `struct Neighbour` where the message was sent
355  */
356 static void
357 notify_send_done (void *cls)
358 {
359   struct Neighbour *n = cls;
360   struct GNUNET_TIME_Relative delay;
361
362   n->timeout_task = NULL;
363   if (NULL != n->env)
364   {
365     GNUNET_BANDWIDTH_tracker_consume (&n->out_tracker,
366                                       n->env_size + n->traffic_overhead);
367     n->traffic_overhead = 0;
368     n->env = NULL;
369   }
370   delay = GNUNET_BANDWIDTH_tracker_get_delay (&n->out_tracker,
371                                               128);
372   if (0 == delay.rel_value_us)
373   {
374     n->is_ready = GNUNET_YES;
375     GNUNET_MQ_impl_send_continue (n->mq);
376     return;
377   }
378   /* cannot send even a small message without violating
379      quota, wait a before notifying MQ */
380   n->timeout_task = GNUNET_SCHEDULER_add_delayed (delay,
381                                                   &notify_send_done,
382                                                   n);
383 }
384
385
386 /**
387  * Implement sending functionality of a message queue.
388  * Called one message at a time. Should send the @a msg
389  * to the transport service and then notify the queue
390  * once we are ready for the next one.
391  *
392  * @param mq the message queue
393  * @param msg the message to send
394  * @param impl_state state of the implementation
395  */
396 static void
397 mq_send_impl (struct GNUNET_MQ_Handle *mq,
398               const struct GNUNET_MessageHeader *msg,
399               void *impl_state)
400 {
401   struct Neighbour *n = impl_state;
402   struct GNUNET_TRANSPORT_CoreHandle *h = n->h;
403   struct OutboundMessage *obm;
404   uint16_t msize;
405
406   GNUNET_assert (GNUNET_YES == n->is_ready);
407   msize = ntohs (msg->size);
408   if (msize >= GNUNET_SERVER_MAX_MESSAGE_SIZE - sizeof (*obm))
409   {
410     GNUNET_break (0);
411     GNUNET_MQ_impl_send_continue (mq);
412     return;
413   }
414   n->env = GNUNET_MQ_msg_nested_mh (obm,
415                                     GNUNET_MESSAGE_TYPE_TRANSPORT_SEND,
416                                     msg);
417   obm->reserved = htonl (0);
418   obm->timeout = GNUNET_TIME_relative_hton (GNUNET_TIME_UNIT_MINUTES); /* FIXME: to be removed */
419   obm->peer = n->id;
420   GNUNET_assert (NULL == n->timeout_task);
421   n->is_ready = GNUNET_NO;
422   n->env_size = ntohs (msg->size);
423   GNUNET_MQ_notify_sent (n->env,
424                          &notify_send_done,
425                          n);
426   GNUNET_MQ_send (h->mq,
427                   n->env);
428   LOG (GNUNET_ERROR_TYPE_DEBUG,
429        "Queued message for neighbour `%s'.\n",
430        GNUNET_i2s (&n->id));
431 }
432
433
434 /**
435  * Handle destruction of a message queue.  Implementations must not
436  * free @a mq, but should take care of @a impl_state.
437  *
438  * @param mq the message queue to destroy
439  * @param impl_state state of the implementation
440  */
441 static void
442 mq_destroy_impl (struct GNUNET_MQ_Handle *mq,
443                  void *impl_state)
444 {
445   struct Neighbour *n = impl_state;
446
447   GNUNET_assert (mq == n->mq);
448   n->mq = NULL;
449 }
450
451
452 /**
453  * Implementation function that cancels the currently sent message.
454  * Should basically undo whatever #mq_send_impl() did.
455  *
456  * @param mq message queue
457  * @param impl_state state specific to the implementation
458  */
459 static void
460 mq_cancel_impl (struct GNUNET_MQ_Handle *mq,
461                 void *impl_state)
462 {
463   struct Neighbour *n = impl_state;
464
465   GNUNET_assert (GNUNET_NO == n->is_ready);
466   if (NULL != n->env)
467   {
468     GNUNET_MQ_send_cancel (n->env);
469     n->env = NULL;
470   }
471
472   n->is_ready = GNUNET_YES;
473 }
474
475
476 /**
477  * We had an error processing a message we forwarded from a peer to
478  * the CORE service.  We should just complain about it but otherwise
479  * continue processing.
480  *
481  * @param cls closure
482  * @param error error code
483  */
484 static void
485 peer_mq_error_handler (void *cls,
486                        enum GNUNET_MQ_Error error)
487 {
488   /* struct Neighbour *n = cls; */
489
490   GNUNET_break_op (0);
491 }
492
493
494 /**
495  * The outbound quota has changed in a way that may require
496  * us to reset the timeout.  Update the timeout.
497  *
498  * @param cls the `struct Neighbour` for which the timeout changed
499  */
500 static void
501 outbound_bw_tracker_update (void *cls)
502 {
503   struct Neighbour *n = cls;
504   struct GNUNET_TIME_Relative delay;
505
506   if (NULL == n->timeout_task)
507     return;
508   delay = GNUNET_BANDWIDTH_tracker_get_delay (&n->out_tracker,
509                                               128);
510   GNUNET_SCHEDULER_cancel (n->timeout_task);
511   n->timeout_task = GNUNET_SCHEDULER_add_delayed (delay,
512                                                   &notify_send_done,
513                                                   n);
514 }
515
516
517 /**
518  * Function we use for handling incoming connect messages.
519  *
520  * @param cls closure, a `struct GNUNET_TRANSPORT_Handle *`
521  * @param cim message received
522  */
523 static void
524 handle_connect (void *cls,
525                 const struct ConnectInfoMessage *cim)
526 {
527   struct GNUNET_TRANSPORT_CoreHandle *h = cls;
528   struct Neighbour *n;
529
530   LOG (GNUNET_ERROR_TYPE_DEBUG,
531        "Receiving CONNECT message for `%s' with quota %u\n",
532        GNUNET_i2s (&cim->id),
533        ntohl (cim->quota_out.value__));
534   n = neighbour_find (h, &cim->id);
535   if (NULL != n)
536   {
537     GNUNET_break (0);
538     disconnect_and_schedule_reconnect (h);
539     return;
540   }
541   n = GNUNET_new (struct Neighbour);
542   n->id = cim->id;
543   n->h = h;
544   n->is_ready = GNUNET_YES;
545   n->traffic_overhead = 0;
546   GNUNET_BANDWIDTH_tracker_init2 (&n->out_tracker,
547                                   &outbound_bw_tracker_update,
548                                   n,
549                                   GNUNET_CONSTANTS_DEFAULT_BW_IN_OUT,
550                                   MAX_BANDWIDTH_CARRY_S,
551                                   &notify_excess_cb,
552                                   n);
553   GNUNET_assert (GNUNET_OK ==
554                  GNUNET_CONTAINER_multipeermap_put (h->neighbours,
555                                                     &n->id,
556                                                     n,
557                                                     GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
558
559   GNUNET_BANDWIDTH_tracker_update_quota (&n->out_tracker,
560                                          cim->quota_out);
561   n->mq = GNUNET_MQ_queue_for_callbacks (&mq_send_impl,
562                                          &mq_destroy_impl,
563                                          &mq_cancel_impl,
564                                          n,
565                                          h->handlers,
566                                          &peer_mq_error_handler,
567                                          n);
568   if (NULL != h->nc_cb)
569   {
570     n->handlers_cls = h->nc_cb (h->cls,
571                                 &n->id,
572                                 n->mq);
573     GNUNET_MQ_set_handlers_closure (n->mq,
574                                     n->handlers_cls);
575   }
576 }
577
578
579 /**
580  * Function we use for handling incoming disconnect messages.
581  *
582  * @param cls closure, a `struct GNUNET_TRANSPORT_CoreHandle *`
583  * @param dim message received
584  */
585 static void
586 handle_disconnect (void *cls,
587                    const struct DisconnectInfoMessage *dim)
588 {
589   struct GNUNET_TRANSPORT_CoreHandle *h = cls;
590   struct Neighbour *n;
591
592   GNUNET_break (ntohl (dim->reserved) == 0);
593   LOG (GNUNET_ERROR_TYPE_DEBUG,
594        "Receiving DISCONNECT message for `%s'.\n",
595        GNUNET_i2s (&dim->peer));
596   n = neighbour_find (h, &dim->peer);
597   if (NULL == n)
598   {
599     GNUNET_break (0);
600     disconnect_and_schedule_reconnect (h);
601     return;
602   }
603   GNUNET_assert (GNUNET_YES ==
604                  neighbour_delete (h,
605                                    &dim->peer,
606                                    n));
607 }
608
609
610 /**
611  * Function we use for handling incoming send-ok messages.
612  *
613  * @param cls closure, a `struct GNUNET_TRANSPORT_CoreHandle *`
614  * @param okm message received
615  */
616 static void
617 handle_send_ok (void *cls,
618                 const struct SendOkMessage *okm)
619 {
620   struct GNUNET_TRANSPORT_CoreHandle *h = cls;
621   struct Neighbour *n;
622   uint32_t bytes_msg;
623   uint32_t bytes_physical;
624
625   bytes_msg = ntohl (okm->bytes_msg);
626   bytes_physical = ntohl (okm->bytes_physical);
627   LOG (GNUNET_ERROR_TYPE_DEBUG,
628        "Receiving SEND_OK message, transmission to %s %s.\n",
629        GNUNET_i2s (&okm->peer),
630        ntohl (okm->success) == GNUNET_OK ? "succeeded" : "failed");
631   n = neighbour_find (h,
632                       &okm->peer);
633   if (NULL == n)
634   {
635     /* We should never get a 'SEND_OK' for a peer that we are not
636        connected to */
637     GNUNET_break (0);
638     disconnect_and_schedule_reconnect (h);
639     return;
640   }
641   if (bytes_physical > bytes_msg)
642   {
643     LOG (GNUNET_ERROR_TYPE_DEBUG,
644          "Overhead for %u byte message was %u\n",
645          bytes_msg,
646          bytes_physical - bytes_msg);
647     n->traffic_overhead += bytes_physical - bytes_msg;
648   }
649 }
650
651
652 /**
653  * Function we use for checking incoming "inbound" messages.
654  *
655  * @param cls closure, a `struct GNUNET_TRANSPORT_CoreHandle *`
656  * @param im message received
657  */
658 static int
659 check_recv (void *cls,
660              const struct InboundMessage *im)
661 {
662   const struct GNUNET_MessageHeader *imm;
663   uint16_t size;
664
665   size = ntohs (im->header.size) - sizeof (*im);
666   if (size < sizeof (struct GNUNET_MessageHeader))
667   {
668     GNUNET_break (0);
669     return GNUNET_SYSERR;
670   }
671   imm = (const struct GNUNET_MessageHeader *) &im[1];
672   if (ntohs (imm->size) != size)
673   {
674     GNUNET_break (0);
675     return GNUNET_SYSERR;
676   }
677   return GNUNET_OK;
678 }
679
680
681 /**
682  * Function we use for handling incoming messages.
683  *
684  * @param cls closure, a `struct GNUNET_TRANSPORT_CoreHandle *`
685  * @param im message received
686  */
687 static void
688 handle_recv (void *cls,
689              const struct InboundMessage *im)
690 {
691   struct GNUNET_TRANSPORT_CoreHandle *h = cls;
692   const struct GNUNET_MessageHeader *imm
693     = (const struct GNUNET_MessageHeader *) &im[1];
694   struct Neighbour *n;
695
696   LOG (GNUNET_ERROR_TYPE_DEBUG,
697        "Received message of type %u with %u bytes from `%s'.\n",
698        (unsigned int) ntohs (imm->type),
699        (unsigned int) ntohs (imm->size),
700        GNUNET_i2s (&im->peer));
701   n = neighbour_find (h, &im->peer);
702   if (NULL == n)
703   {
704     GNUNET_break (0);
705     disconnect_and_schedule_reconnect (h);
706     return;
707   }
708   GNUNET_MQ_inject_message (n->mq,
709                             imm);
710 }
711
712
713 /**
714  * Function we use for handling incoming set quota messages.
715  *
716  * @param cls closure, a `struct GNUNET_TRANSPORT_CoreHandle *`
717  * @param msg message received
718  */
719 static void
720 handle_set_quota (void *cls,
721                   const struct QuotaSetMessage *qm)
722 {
723   struct GNUNET_TRANSPORT_CoreHandle *h = cls;
724   struct Neighbour *n;
725
726   n = neighbour_find (h,
727                       &qm->peer);
728   if (NULL == n)
729   {
730     GNUNET_break (0);
731     disconnect_and_schedule_reconnect (h);
732     return;
733   }
734   LOG (GNUNET_ERROR_TYPE_DEBUG,
735        "Receiving SET_QUOTA message for `%s' with quota %u\n",
736        GNUNET_i2s (&qm->peer),
737        ntohl (qm->quota.value__));
738   GNUNET_BANDWIDTH_tracker_update_quota (&n->out_tracker,
739                                          qm->quota);
740 }
741
742
743 /**
744  * Try again to connect to transport service.
745  *
746  * @param cls the handle to the transport service
747  */
748 static void
749 reconnect (void *cls)
750 {
751   struct GNUNET_TRANSPORT_CoreHandle *h = cls;
752   struct GNUNET_MQ_MessageHandler handlers[] = {
753     GNUNET_MQ_hd_var_size (hello,
754                            GNUNET_MESSAGE_TYPE_HELLO,
755                            struct GNUNET_MessageHeader,
756                            h),
757     GNUNET_MQ_hd_fixed_size (connect,
758                              GNUNET_MESSAGE_TYPE_TRANSPORT_CONNECT,
759                              struct ConnectInfoMessage,
760                              h),
761     GNUNET_MQ_hd_fixed_size (disconnect,
762                              GNUNET_MESSAGE_TYPE_TRANSPORT_DISCONNECT,
763                              struct DisconnectInfoMessage,
764                              h),
765     GNUNET_MQ_hd_fixed_size (send_ok,
766                              GNUNET_MESSAGE_TYPE_TRANSPORT_SEND_OK,
767                              struct SendOkMessage,
768                              h),
769     GNUNET_MQ_hd_var_size (recv,
770                            GNUNET_MESSAGE_TYPE_TRANSPORT_RECV,
771                            struct InboundMessage,
772                            h),
773     GNUNET_MQ_hd_fixed_size (set_quota,
774                              GNUNET_MESSAGE_TYPE_TRANSPORT_SET_QUOTA,
775                              struct QuotaSetMessage,
776                              h),
777     GNUNET_MQ_handler_end ()
778   };
779   struct GNUNET_MQ_Envelope *env;
780   struct StartMessage *s;
781   uint32_t options;
782
783   h->reconnect_task = NULL;
784   LOG (GNUNET_ERROR_TYPE_DEBUG,
785        "Connecting to transport service.\n");
786   GNUNET_assert (NULL == h->mq);
787   h->mq = GNUNET_CLIENT_connecT (h->cfg,
788                                  "transport",
789                                  handlers,
790                                  &mq_error_handler,
791                                  h);
792   if (NULL == h->mq)
793     return;
794   env = GNUNET_MQ_msg (s,
795                        GNUNET_MESSAGE_TYPE_TRANSPORT_START);
796   options = 0;
797   if (h->check_self)
798     options |= 1;
799   if (NULL != h->handlers)
800     options |= 2;
801   s->options = htonl (options);
802   s->self = h->self;
803   GNUNET_MQ_send (h->mq,
804                   env);
805 }
806
807
808 /**
809  * Function that will schedule the job that will try
810  * to connect us again to the client.
811  *
812  * @param h transport service to reconnect
813  */
814 static void
815 disconnect_and_schedule_reconnect (struct GNUNET_TRANSPORT_CoreHandle *h)
816 {
817   GNUNET_assert (NULL == h->reconnect_task);
818   /* Forget about all neighbours that we used to be connected to */
819   GNUNET_CONTAINER_multipeermap_iterate (h->neighbours,
820                                          &neighbour_delete,
821                                          h);
822   if (NULL != h->mq)
823   {
824     GNUNET_MQ_destroy (h->mq);
825     h->mq = NULL;
826   }
827   LOG (GNUNET_ERROR_TYPE_DEBUG,
828        "Scheduling task to reconnect to transport service in %s.\n",
829        GNUNET_STRINGS_relative_time_to_string (h->reconnect_delay,
830                                                GNUNET_YES));
831   h->reconnect_task =
832       GNUNET_SCHEDULER_add_delayed (h->reconnect_delay,
833                                     &reconnect,
834                                     h);
835   h->reconnect_delay = GNUNET_TIME_STD_BACKOFF (h->reconnect_delay);
836 }
837
838
839 /**
840  * Checks if a given peer is connected to us and get the message queue.
841  *
842  * @param handle connection to transport service
843  * @param peer the peer to check
844  * @return NULL if disconnected, otherwise message queue for @a peer
845  */
846 struct GNUNET_MQ_Handle *
847 GNUNET_TRANSPORT_core_get_mq (struct GNUNET_TRANSPORT_CoreHandle *handle,
848                               const struct GNUNET_PeerIdentity *peer)
849 {
850   struct Neighbour *n;
851
852   n = neighbour_find (handle,
853                       peer);
854   if (NULL == n)
855     return NULL;
856   return n->mq;
857 }
858
859
860 /**
861  * Connect to the transport service.  Note that the connection may
862  * complete (or fail) asynchronously.
863  *
864  * @param cfg configuration to use
865  * @param self our own identity (API should check that it matches
866  *             the identity found by transport), or NULL (no check)
867  * @param cls closure for the callbacks
868  * @param rec receive function to call
869  * @param nc function to call on connect events
870  * @param nd function to call on disconnect events
871  * @param neb function to call if we have excess bandwidth to a peer
872  * @return NULL on error
873  */
874 struct GNUNET_TRANSPORT_CoreHandle *
875 GNUNET_TRANSPORT_core_connect (const struct GNUNET_CONFIGURATION_Handle *cfg,
876                                const struct GNUNET_PeerIdentity *self,
877                                const struct GNUNET_MQ_MessageHandler *handlers,
878                                void *cls,
879                                GNUNET_TRANSPORT_NotifyConnecT nc,
880                                GNUNET_TRANSPORT_NotifyDisconnecT nd,
881                                GNUNET_TRANSPORT_NotifyExcessBandwidtH neb)
882 {
883   struct GNUNET_TRANSPORT_CoreHandle *h;
884   unsigned int i;
885
886   h = GNUNET_new (struct GNUNET_TRANSPORT_CoreHandle);
887   if (NULL != self)
888   {
889     h->self = *self;
890     h->check_self = GNUNET_YES;
891   }
892   h->cfg = cfg;
893   h->cls = cls;
894   h->nc_cb = nc;
895   h->nd_cb = nd;
896   h->neb_cb = neb;
897   h->reconnect_delay = GNUNET_TIME_UNIT_ZERO;
898   if (NULL != handlers)
899   {
900     for (i=0;NULL != handlers[i].cb; i++) ;
901     h->handlers = GNUNET_new_array (i + 1,
902                                     struct GNUNET_MQ_MessageHandler);
903     GNUNET_memcpy (h->handlers,
904                    handlers,
905                    i * sizeof (struct GNUNET_MQ_MessageHandler));
906   }
907   LOG (GNUNET_ERROR_TYPE_DEBUG,
908        "Connecting to transport service\n");
909   reconnect (h);
910   if (NULL == h->mq)
911   {
912     GNUNET_free_non_null (h->handlers);
913     GNUNET_free (h);
914     return NULL;
915   }
916   h->neighbours =
917     GNUNET_CONTAINER_multipeermap_create (STARTING_NEIGHBOURS_SIZE,
918                                           GNUNET_YES);
919   return h;
920 }
921
922
923 /**
924  * Disconnect from the transport service.
925  *
926  * @param handle handle to the service as returned from #GNUNET_TRANSPORT_core_connect()
927  */
928 void
929 GNUNET_TRANSPORT_core_disconnect (struct GNUNET_TRANSPORT_CoreHandle *handle)
930 {
931   LOG (GNUNET_ERROR_TYPE_DEBUG,
932        "Transport disconnect called!\n");
933   /* this disconnects all neighbours... */
934   if (NULL == handle->reconnect_task)
935     disconnect_and_schedule_reconnect (handle);
936   /* and now we stop trying to connect again... */
937   if (NULL != handle->reconnect_task)
938   {
939     GNUNET_SCHEDULER_cancel (handle->reconnect_task);
940     handle->reconnect_task = NULL;
941   }
942   GNUNET_CONTAINER_multipeermap_destroy (handle->neighbours);
943   handle->neighbours = NULL;
944   GNUNET_free_non_null (handle->handlers);
945   handle->handlers = NULL;
946   GNUNET_free (handle);
947 }
948
949
950 /* end of transport_api_core.c */