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