cleaning up set handlers, eliminating 2nd level demultiplexing and improving use...
[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_fin (void *cls)
358 {
359   struct Neighbour *n = cls;
360
361   n->timeout_task = NULL;
362   n->is_ready = GNUNET_YES;
363   GNUNET_MQ_impl_send_continue (n->mq);
364 }
365
366
367 /**
368  * A message from the handler's message queue to a neighbour was
369  * transmitted.  Now trigger (possibly delayed) notification of the
370  * neighbour's message queue that we are done and thus ready for
371  * the next message.
372  *
373  * @param cls the `struct Neighbour` where the message was sent
374  */
375 static void
376 notify_send_done (void *cls)
377 {
378   struct Neighbour *n = cls;
379   struct GNUNET_TIME_Relative delay;
380
381   n->timeout_task = NULL;
382   if (NULL != n->env)
383   {
384     GNUNET_BANDWIDTH_tracker_consume (&n->out_tracker,
385                                       n->env_size + n->traffic_overhead);
386     n->env = NULL;
387     n->traffic_overhead = 0;
388   }
389   delay = GNUNET_BANDWIDTH_tracker_get_delay (&n->out_tracker,
390                                               128);
391   if (0 == delay.rel_value_us)
392   {
393     n->is_ready = GNUNET_YES;
394     GNUNET_MQ_impl_send_continue (n->mq);
395     return;
396   }
397   GNUNET_MQ_impl_send_in_flight (n->mq);
398   /* cannot send even a small message without violating
399      quota, wait a before allowing MQ to send next message */
400   n->timeout_task = GNUNET_SCHEDULER_add_delayed (delay,
401                                                   &notify_send_done_fin,
402                                                   n);
403 }
404
405
406 /**
407  * Implement sending functionality of a message queue.
408  * Called one message at a time. Should send the @a msg
409  * to the transport service and then notify the queue
410  * once we are ready for the next one.
411  *
412  * @param mq the message queue
413  * @param msg the message to send
414  * @param impl_state state of the implementation
415  */
416 static void
417 mq_send_impl (struct GNUNET_MQ_Handle *mq,
418               const struct GNUNET_MessageHeader *msg,
419               void *impl_state)
420 {
421   struct Neighbour *n = impl_state;
422   struct GNUNET_TRANSPORT_CoreHandle *h = n->h;
423   struct OutboundMessage *obm;
424   uint16_t msize;
425
426   GNUNET_assert (GNUNET_YES == n->is_ready);
427   msize = ntohs (msg->size);
428   if (msize >= GNUNET_SERVER_MAX_MESSAGE_SIZE - sizeof (*obm))
429   {
430     GNUNET_break (0);
431     GNUNET_MQ_impl_send_continue (mq);
432     return;
433   }
434   GNUNET_assert (NULL == n->env);
435   n->env = GNUNET_MQ_msg_nested_mh (obm,
436                                     GNUNET_MESSAGE_TYPE_TRANSPORT_SEND,
437                                     msg);
438   obm->reserved = htonl (0);
439   obm->timeout = GNUNET_TIME_relative_hton (GNUNET_TIME_UNIT_MINUTES); /* FIXME: to be removed */
440   obm->peer = n->id;
441   GNUNET_assert (NULL == n->timeout_task);
442   n->is_ready = GNUNET_NO;
443   n->env_size = ntohs (msg->size);
444   GNUNET_MQ_notify_sent (n->env,
445                          &notify_send_done,
446                          n);
447   GNUNET_MQ_send (h->mq,
448                   n->env);
449   LOG (GNUNET_ERROR_TYPE_DEBUG,
450        "Queued message of type %u for neighbour `%s'.\n",
451        ntohs (msg->type),
452        GNUNET_i2s (&n->id));
453 }
454
455
456 /**
457  * Handle destruction of a message queue.  Implementations must not
458  * free @a mq, but should take care of @a impl_state.
459  *
460  * @param mq the message queue to destroy
461  * @param impl_state state of the implementation
462  */
463 static void
464 mq_destroy_impl (struct GNUNET_MQ_Handle *mq,
465                  void *impl_state)
466 {
467   struct Neighbour *n = impl_state;
468
469   GNUNET_assert (mq == n->mq);
470   n->mq = NULL;
471 }
472
473
474 /**
475  * Implementation function that cancels the currently sent message.
476  * Should basically undo whatever #mq_send_impl() did.
477  *
478  * @param mq message queue
479  * @param impl_state state specific to the implementation
480  */
481 static void
482 mq_cancel_impl (struct GNUNET_MQ_Handle *mq,
483                 void *impl_state)
484 {
485   struct Neighbour *n = impl_state;
486
487   GNUNET_assert (GNUNET_NO == n->is_ready);
488   if (NULL != n->env)
489   {
490     GNUNET_MQ_send_cancel (n->env);
491     n->env = NULL;
492   }
493
494   n->is_ready = GNUNET_YES;
495 }
496
497
498 /**
499  * We had an error processing a message we forwarded from a peer to
500  * the CORE service.  We should just complain about it but otherwise
501  * continue processing.
502  *
503  * @param cls closure
504  * @param error error code
505  */
506 static void
507 peer_mq_error_handler (void *cls,
508                        enum GNUNET_MQ_Error error)
509 {
510   /* struct Neighbour *n = cls; */
511
512   GNUNET_break_op (0);
513 }
514
515
516 /**
517  * The outbound quota has changed in a way that may require
518  * us to reset the timeout.  Update the timeout.
519  *
520  * @param cls the `struct Neighbour` for which the timeout changed
521  */
522 static void
523 outbound_bw_tracker_update (void *cls)
524 {
525   struct Neighbour *n = cls;
526   struct GNUNET_TIME_Relative delay;
527
528   if (NULL == n->timeout_task)
529     return;
530   delay = GNUNET_BANDWIDTH_tracker_get_delay (&n->out_tracker,
531                                               128);
532   GNUNET_SCHEDULER_cancel (n->timeout_task);
533   n->timeout_task = GNUNET_SCHEDULER_add_delayed (delay,
534                                                   &notify_send_done,
535                                                   n);
536 }
537
538
539 /**
540  * Function we use for handling incoming connect messages.
541  *
542  * @param cls closure, a `struct GNUNET_TRANSPORT_Handle *`
543  * @param cim message received
544  */
545 static void
546 handle_connect (void *cls,
547                 const struct ConnectInfoMessage *cim)
548 {
549   struct GNUNET_TRANSPORT_CoreHandle *h = cls;
550   struct Neighbour *n;
551
552   LOG (GNUNET_ERROR_TYPE_DEBUG,
553        "Receiving CONNECT message for `%s' with quota %u\n",
554        GNUNET_i2s (&cim->id),
555        ntohl (cim->quota_out.value__));
556   n = neighbour_find (h, &cim->id);
557   if (NULL != n)
558   {
559     GNUNET_break (0);
560     disconnect_and_schedule_reconnect (h);
561     return;
562   }
563   n = GNUNET_new (struct Neighbour);
564   n->id = cim->id;
565   n->h = h;
566   n->is_ready = GNUNET_YES;
567   n->traffic_overhead = 0;
568   GNUNET_BANDWIDTH_tracker_init2 (&n->out_tracker,
569                                   &outbound_bw_tracker_update,
570                                   n,
571                                   GNUNET_CONSTANTS_DEFAULT_BW_IN_OUT,
572                                   MAX_BANDWIDTH_CARRY_S,
573                                   &notify_excess_cb,
574                                   n);
575   GNUNET_assert (GNUNET_OK ==
576                  GNUNET_CONTAINER_multipeermap_put (h->neighbours,
577                                                     &n->id,
578                                                     n,
579                                                     GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
580
581   GNUNET_BANDWIDTH_tracker_update_quota (&n->out_tracker,
582                                          cim->quota_out);
583   n->mq = GNUNET_MQ_queue_for_callbacks (&mq_send_impl,
584                                          &mq_destroy_impl,
585                                          &mq_cancel_impl,
586                                          n,
587                                          h->handlers,
588                                          &peer_mq_error_handler,
589                                          n);
590   if (NULL != h->nc_cb)
591   {
592     n->handlers_cls = h->nc_cb (h->cls,
593                                 &n->id,
594                                 n->mq);
595     GNUNET_MQ_set_handlers_closure (n->mq,
596                                     n->handlers_cls);
597   }
598 }
599
600
601 /**
602  * Function we use for handling incoming disconnect messages.
603  *
604  * @param cls closure, a `struct GNUNET_TRANSPORT_CoreHandle *`
605  * @param dim message received
606  */
607 static void
608 handle_disconnect (void *cls,
609                    const struct DisconnectInfoMessage *dim)
610 {
611   struct GNUNET_TRANSPORT_CoreHandle *h = cls;
612   struct Neighbour *n;
613
614   GNUNET_break (ntohl (dim->reserved) == 0);
615   LOG (GNUNET_ERROR_TYPE_DEBUG,
616        "Receiving DISCONNECT message for `%s'.\n",
617        GNUNET_i2s (&dim->peer));
618   n = neighbour_find (h, &dim->peer);
619   if (NULL == n)
620   {
621     GNUNET_break (0);
622     disconnect_and_schedule_reconnect (h);
623     return;
624   }
625   GNUNET_assert (GNUNET_YES ==
626                  neighbour_delete (h,
627                                    &dim->peer,
628                                    n));
629 }
630
631
632 /**
633  * Function we use for handling incoming send-ok messages.
634  *
635  * @param cls closure, a `struct GNUNET_TRANSPORT_CoreHandle *`
636  * @param okm message received
637  */
638 static void
639 handle_send_ok (void *cls,
640                 const struct SendOkMessage *okm)
641 {
642   struct GNUNET_TRANSPORT_CoreHandle *h = cls;
643   struct Neighbour *n;
644   uint32_t bytes_msg;
645   uint32_t bytes_physical;
646
647   bytes_msg = ntohl (okm->bytes_msg);
648   bytes_physical = ntohl (okm->bytes_physical);
649   LOG (GNUNET_ERROR_TYPE_DEBUG,
650        "Receiving SEND_OK message, transmission to %s %s.\n",
651        GNUNET_i2s (&okm->peer),
652        ntohl (okm->success) == GNUNET_OK ? "succeeded" : "failed");
653   n = neighbour_find (h,
654                       &okm->peer);
655   if (NULL == n)
656   {
657     /* We should never get a 'SEND_OK' for a peer that we are not
658        connected to */
659     GNUNET_break (0);
660     disconnect_and_schedule_reconnect (h);
661     return;
662   }
663   if (bytes_physical > bytes_msg)
664   {
665     LOG (GNUNET_ERROR_TYPE_DEBUG,
666          "Overhead for %u byte message was %u\n",
667          bytes_msg,
668          bytes_physical - bytes_msg);
669     n->traffic_overhead += bytes_physical - bytes_msg;
670   }
671 }
672
673
674 /**
675  * Function we use for checking incoming "inbound" messages.
676  *
677  * @param cls closure, a `struct GNUNET_TRANSPORT_CoreHandle *`
678  * @param im message received
679  */
680 static int
681 check_recv (void *cls,
682              const struct InboundMessage *im)
683 {
684   const struct GNUNET_MessageHeader *imm;
685   uint16_t size;
686
687   size = ntohs (im->header.size) - sizeof (*im);
688   if (size < sizeof (struct GNUNET_MessageHeader))
689   {
690     GNUNET_break (0);
691     return GNUNET_SYSERR;
692   }
693   imm = (const struct GNUNET_MessageHeader *) &im[1];
694   if (ntohs (imm->size) != size)
695   {
696     GNUNET_break (0);
697     return GNUNET_SYSERR;
698   }
699   return GNUNET_OK;
700 }
701
702
703 /**
704  * Function we use for handling incoming messages.
705  *
706  * @param cls closure, a `struct GNUNET_TRANSPORT_CoreHandle *`
707  * @param im message received
708  */
709 static void
710 handle_recv (void *cls,
711              const struct InboundMessage *im)
712 {
713   struct GNUNET_TRANSPORT_CoreHandle *h = cls;
714   const struct GNUNET_MessageHeader *imm
715     = (const struct GNUNET_MessageHeader *) &im[1];
716   struct Neighbour *n;
717
718   LOG (GNUNET_ERROR_TYPE_DEBUG,
719        "Received message of type %u with %u bytes from `%s'.\n",
720        (unsigned int) ntohs (imm->type),
721        (unsigned int) ntohs (imm->size),
722        GNUNET_i2s (&im->peer));
723   n = neighbour_find (h, &im->peer);
724   if (NULL == n)
725   {
726     GNUNET_break (0);
727     disconnect_and_schedule_reconnect (h);
728     return;
729   }
730   GNUNET_MQ_inject_message (n->mq,
731                             imm);
732 }
733
734
735 /**
736  * Function we use for handling incoming set quota messages.
737  *
738  * @param cls closure, a `struct GNUNET_TRANSPORT_CoreHandle *`
739  * @param msg message received
740  */
741 static void
742 handle_set_quota (void *cls,
743                   const struct QuotaSetMessage *qm)
744 {
745   struct GNUNET_TRANSPORT_CoreHandle *h = cls;
746   struct Neighbour *n;
747
748   n = neighbour_find (h,
749                       &qm->peer);
750   if (NULL == n)
751   {
752     GNUNET_break (0);
753     disconnect_and_schedule_reconnect (h);
754     return;
755   }
756   LOG (GNUNET_ERROR_TYPE_DEBUG,
757        "Receiving SET_QUOTA message for `%s' with quota %u\n",
758        GNUNET_i2s (&qm->peer),
759        ntohl (qm->quota.value__));
760   GNUNET_BANDWIDTH_tracker_update_quota (&n->out_tracker,
761                                          qm->quota);
762 }
763
764
765 /**
766  * Try again to connect to transport service.
767  *
768  * @param cls the handle to the transport service
769  */
770 static void
771 reconnect (void *cls)
772 {
773   struct GNUNET_TRANSPORT_CoreHandle *h = cls;
774   struct GNUNET_MQ_MessageHandler handlers[] = {
775     GNUNET_MQ_hd_var_size (hello,
776                            GNUNET_MESSAGE_TYPE_HELLO,
777                            struct GNUNET_MessageHeader,
778                            h),
779     GNUNET_MQ_hd_fixed_size (connect,
780                              GNUNET_MESSAGE_TYPE_TRANSPORT_CONNECT,
781                              struct ConnectInfoMessage,
782                              h),
783     GNUNET_MQ_hd_fixed_size (disconnect,
784                              GNUNET_MESSAGE_TYPE_TRANSPORT_DISCONNECT,
785                              struct DisconnectInfoMessage,
786                              h),
787     GNUNET_MQ_hd_fixed_size (send_ok,
788                              GNUNET_MESSAGE_TYPE_TRANSPORT_SEND_OK,
789                              struct SendOkMessage,
790                              h),
791     GNUNET_MQ_hd_var_size (recv,
792                            GNUNET_MESSAGE_TYPE_TRANSPORT_RECV,
793                            struct InboundMessage,
794                            h),
795     GNUNET_MQ_hd_fixed_size (set_quota,
796                              GNUNET_MESSAGE_TYPE_TRANSPORT_SET_QUOTA,
797                              struct QuotaSetMessage,
798                              h),
799     GNUNET_MQ_handler_end ()
800   };
801   struct GNUNET_MQ_Envelope *env;
802   struct StartMessage *s;
803   uint32_t options;
804
805   h->reconnect_task = NULL;
806   LOG (GNUNET_ERROR_TYPE_DEBUG,
807        "Connecting to transport service.\n");
808   GNUNET_assert (NULL == h->mq);
809   h->mq = GNUNET_CLIENT_connect (h->cfg,
810                                  "transport",
811                                  handlers,
812                                  &mq_error_handler,
813                                  h);
814   if (NULL == h->mq)
815     return;
816   env = GNUNET_MQ_msg (s,
817                        GNUNET_MESSAGE_TYPE_TRANSPORT_START);
818   options = 0;
819   if (h->check_self)
820     options |= 1;
821   if (NULL != h->handlers)
822     options |= 2;
823   s->options = htonl (options);
824   s->self = h->self;
825   GNUNET_MQ_send (h->mq,
826                   env);
827 }
828
829
830 /**
831  * Function that will schedule the job that will try
832  * to connect us again to the client.
833  *
834  * @param h transport service to reconnect
835  */
836 static void
837 disconnect_and_schedule_reconnect (struct GNUNET_TRANSPORT_CoreHandle *h)
838 {
839   GNUNET_assert (NULL == h->reconnect_task);
840   /* Forget about all neighbours that we used to be connected to */
841   GNUNET_CONTAINER_multipeermap_iterate (h->neighbours,
842                                          &neighbour_delete,
843                                          h);
844   if (NULL != h->mq)
845   {
846     GNUNET_MQ_destroy (h->mq);
847     h->mq = NULL;
848   }
849   LOG (GNUNET_ERROR_TYPE_DEBUG,
850        "Scheduling task to reconnect to transport service in %s.\n",
851        GNUNET_STRINGS_relative_time_to_string (h->reconnect_delay,
852                                                GNUNET_YES));
853   h->reconnect_task =
854       GNUNET_SCHEDULER_add_delayed (h->reconnect_delay,
855                                     &reconnect,
856                                     h);
857   h->reconnect_delay = GNUNET_TIME_STD_BACKOFF (h->reconnect_delay);
858 }
859
860
861 /**
862  * Checks if a given peer is connected to us and get the message queue.
863  *
864  * @param handle connection to transport service
865  * @param peer the peer to check
866  * @return NULL if disconnected, otherwise message queue for @a peer
867  */
868 struct GNUNET_MQ_Handle *
869 GNUNET_TRANSPORT_core_get_mq (struct GNUNET_TRANSPORT_CoreHandle *handle,
870                               const struct GNUNET_PeerIdentity *peer)
871 {
872   struct Neighbour *n;
873
874   n = neighbour_find (handle,
875                       peer);
876   if (NULL == n)
877     return NULL;
878   return n->mq;
879 }
880
881
882 /**
883  * Connect to the transport service.  Note that the connection may
884  * complete (or fail) asynchronously.
885  *
886  * @param cfg configuration to use
887  * @param self our own identity (API should check that it matches
888  *             the identity found by transport), or NULL (no check)
889  * @param cls closure for the callbacks
890  * @param rec receive function to call
891  * @param nc function to call on connect events
892  * @param nd function to call on disconnect events
893  * @param neb function to call if we have excess bandwidth to a peer
894  * @return NULL on error
895  */
896 struct GNUNET_TRANSPORT_CoreHandle *
897 GNUNET_TRANSPORT_core_connect (const struct GNUNET_CONFIGURATION_Handle *cfg,
898                                const struct GNUNET_PeerIdentity *self,
899                                const struct GNUNET_MQ_MessageHandler *handlers,
900                                void *cls,
901                                GNUNET_TRANSPORT_NotifyConnecT nc,
902                                GNUNET_TRANSPORT_NotifyDisconnecT nd,
903                                GNUNET_TRANSPORT_NotifyExcessBandwidtH neb)
904 {
905   struct GNUNET_TRANSPORT_CoreHandle *h;
906   unsigned int i;
907
908   h = GNUNET_new (struct GNUNET_TRANSPORT_CoreHandle);
909   if (NULL != self)
910   {
911     h->self = *self;
912     h->check_self = GNUNET_YES;
913   }
914   h->cfg = cfg;
915   h->cls = cls;
916   h->nc_cb = nc;
917   h->nd_cb = nd;
918   h->neb_cb = neb;
919   h->reconnect_delay = GNUNET_TIME_UNIT_ZERO;
920   if (NULL != handlers)
921   {
922     for (i=0;NULL != handlers[i].cb; i++) ;
923     h->handlers = GNUNET_new_array (i + 1,
924                                     struct GNUNET_MQ_MessageHandler);
925     GNUNET_memcpy (h->handlers,
926                    handlers,
927                    i * sizeof (struct GNUNET_MQ_MessageHandler));
928   }
929   LOG (GNUNET_ERROR_TYPE_DEBUG,
930        "Connecting to transport service\n");
931   reconnect (h);
932   if (NULL == h->mq)
933   {
934     GNUNET_free_non_null (h->handlers);
935     GNUNET_free (h);
936     return NULL;
937   }
938   h->neighbours =
939     GNUNET_CONTAINER_multipeermap_create (STARTING_NEIGHBOURS_SIZE,
940                                           GNUNET_YES);
941   return h;
942 }
943
944
945 /**
946  * Disconnect from the transport service.
947  *
948  * @param handle handle to the service as returned from #GNUNET_TRANSPORT_core_connect()
949  */
950 void
951 GNUNET_TRANSPORT_core_disconnect (struct GNUNET_TRANSPORT_CoreHandle *handle)
952 {
953   LOG (GNUNET_ERROR_TYPE_DEBUG,
954        "Transport disconnect called!\n");
955   /* this disconnects all neighbours... */
956   if (NULL == handle->reconnect_task)
957     disconnect_and_schedule_reconnect (handle);
958   /* and now we stop trying to connect again... */
959   if (NULL != handle->reconnect_task)
960   {
961     GNUNET_SCHEDULER_cancel (handle->reconnect_task);
962     handle->reconnect_task = NULL;
963   }
964   GNUNET_CONTAINER_multipeermap_destroy (handle->neighbours);
965   handle->neighbours = NULL;
966   GNUNET_free_non_null (handle->handlers);
967   handle->handlers = NULL;
968   GNUNET_free (handle);
969 }
970
971
972 /* end of transport_api_core.c */