-remove debug message
[oweals/gnunet.git] / src / cadet / gnunet-service-cadet_connection.c
1 /*
2      This file is part of GNUnet.
3      Copyright (C) 2001-2017 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      You should have received a copy of the GNU Affero General Public License
16      along with this program.  If not, see <http://www.gnu.org/licenses/>.
17
18      SPDX-License-Identifier: AGPL3.0-or-later
19  */
20
21 /**
22  * @file cadet/gnunet-service-cadet_connection.c
23  * @brief management of CORE-level end-to-end connections; establishes
24  *        end-to-end routes and transmits messages along the route
25  * @author Bartlomiej Polot
26  * @author Christian Grothoff
27  */
28 #include "platform.h"
29 #include "gnunet_signatures.h"
30 #include "gnunet-service-cadet_connection.h"
31 #include "gnunet-service-cadet_channel.h"
32 #include "gnunet-service-cadet_paths.h"
33 #include "gnunet-service-cadet_tunnels.h"
34 #include "gnunet_cadet_service.h"
35 #include "gnunet_statistics_service.h"
36 #include "cadet_protocol.h"
37
38
39 #define LOG(level, ...) GNUNET_log_from (level, "cadet-con", __VA_ARGS__)
40
41
42 /**
43  * How long do we wait initially before retransmitting the KX?
44  * TODO: replace by 2 RTT if/once we have connection-level RTT data!
45  */
46 #define INITIAL_CONNECTION_CREATE_RETRY_DELAY \
47   GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS, 200)
48
49
50 /**
51  * All the states a connection can be in.
52  */
53 enum CadetConnectionState
54 {
55   /**
56    * Uninitialized status, we have not yet even gotten the message queue.
57    */
58   CADET_CONNECTION_NEW,
59
60   /**
61    * Connection create message in queue, awaiting transmission by CORE.
62    */
63   CADET_CONNECTION_SENDING_CREATE,
64
65   /**
66    * Connection create message sent, waiting for ACK.
67    */
68   CADET_CONNECTION_SENT,
69
70   /**
71    * We are an inbound connection, and received a CREATE. Need to
72    * send an CREATE_ACK back.
73    */
74   CADET_CONNECTION_CREATE_RECEIVED,
75
76   /**
77    * Connection confirmed, ready to carry traffic.
78    */
79   CADET_CONNECTION_READY
80 };
81
82 /**
83  * Low-level connection to a destination.
84  */
85 struct CadetConnection
86 {
87   /**
88    * ID of the connection.
89    */
90   struct GNUNET_CADET_ConnectionTunnelIdentifier cid;
91
92   /**
93    * To which peer does this connection go?
94    */
95   struct CadetPeer *destination;
96
97   /**
98    * Which tunnel is using this connection?
99    */
100   struct CadetTConnection *ct;
101
102   /**
103    * Path we are using to our destination.
104    */
105   struct CadetPeerPath *path;
106
107   /**
108    * Pending message, NULL if we are ready to transmit.
109    */
110   struct GNUNET_MQ_Envelope *env;
111
112   /**
113    * Handle for calling #GCP_request_mq_cancel() once we are finished.
114    */
115   struct GCP_MessageQueueManager *mq_man;
116
117   /**
118    * Task for connection maintenance.
119    */
120   struct GNUNET_SCHEDULER_Task *task;
121
122   /**
123    * Queue entry for keepalive messages.
124    */
125   struct CadetTunnelQueueEntry *keepalive_qe;
126
127   /**
128    * Function to call once we are ready to transmit.
129    */
130   GCC_ReadyCallback ready_cb;
131
132   /**
133    * Closure for @e ready_cb.
134    */
135   void *ready_cb_cls;
136
137   /**
138    * How long do we wait before we try again with a CREATE message?
139    */
140   struct GNUNET_TIME_Relative retry_delay;
141
142   /**
143    * Earliest time for re-trying CREATE
144    */
145   struct GNUNET_TIME_Absolute create_at;
146
147   /**
148    * Earliest time for re-trying CREATE_ACK
149    */
150   struct GNUNET_TIME_Absolute create_ack_at;
151
152   /**
153    * Performance metrics for this connection.
154    */
155   struct CadetConnectionMetrics metrics;
156
157   /**
158    * State of the connection.
159    */
160   enum CadetConnectionState state;
161
162   /**
163    * How many latency observations did we make for this connection?
164    */
165   unsigned int latency_datapoints;
166
167   /**
168    * Offset of our @e destination in @e path.
169    */
170   unsigned int off;
171
172   /**
173    * Are we ready to transmit via @e mq_man right now?
174    */
175   int mqm_ready;
176 };
177
178
179 /**
180  * Lookup a connection by its identifier.
181  *
182  * @param cid identifier to resolve
183  * @return NULL if connection was not found
184  */
185 struct CadetConnection *
186 GCC_lookup (const struct GNUNET_CADET_ConnectionTunnelIdentifier *cid)
187 {
188   return GNUNET_CONTAINER_multishortmap_get (connections,
189                                              &cid->connection_of_tunnel);
190 }
191
192
193 /**
194  * Update the connection state. Also triggers the necessary
195  * MQM notifications.
196  *
197  * @param cc connection to update the state for
198  * @param new_state new state for @a cc
199  * @param new_mqm_ready new `mqm_ready` state for @a cc
200  */
201 static void
202 update_state (struct CadetConnection *cc,
203               enum CadetConnectionState new_state,
204               int new_mqm_ready)
205 {
206   int old_ready;
207   int new_ready;
208
209   LOG (GNUNET_ERROR_TYPE_DEBUG,
210        "Trying to update connection state for %s having old state %d to new %d and mqm_ready old %d to mqm_ready new %d\n",
211        GCT_2s (cc->ct->t),
212        cc->state,
213        new_state,
214        cc->mqm_ready,
215        new_mqm_ready);
216
217   if ((new_state == cc->state) && (new_mqm_ready == cc->mqm_ready))
218     return; /* no change, nothing to do */
219   old_ready =
220     ((CADET_CONNECTION_READY == cc->state) && (GNUNET_YES == cc->mqm_ready));
221   new_ready =
222     ((CADET_CONNECTION_READY == new_state) && (GNUNET_YES == new_mqm_ready));
223   cc->state = new_state;
224   cc->mqm_ready = new_mqm_ready;
225
226   LOG (GNUNET_ERROR_TYPE_DEBUG,
227        "Updating connection state for %s having old_ready %d and new_rady %d\n",
228        GCT_2s (cc->ct->t),
229        old_ready,
230        new_ready);
231
232   if (old_ready != new_ready)
233     cc->ready_cb (cc->ready_cb_cls, new_ready);
234 }
235
236
237 /**
238  * Destroy a connection, part of the internal implementation.  Called
239  * only from #GCC_destroy_from_core() or #GCC_destroy_from_tunnel().
240  *
241  * @param cc connection to destroy
242  */
243 static void
244 GCC_destroy (struct CadetConnection *cc)
245 {
246   LOG (GNUNET_ERROR_TYPE_DEBUG, "Destroying %s\n", GCC_2s (cc));
247   if (NULL != cc->mq_man)
248   {
249     GCP_request_mq_cancel (cc->mq_man, NULL);
250     cc->mq_man = NULL;
251   }
252   if (NULL != cc->task)
253   {
254     GNUNET_SCHEDULER_cancel (cc->task);
255     cc->task = NULL;
256   }
257   if (NULL != cc->keepalive_qe)
258   {
259     GCT_send_cancel (cc->keepalive_qe);
260     cc->keepalive_qe = NULL;
261   }
262   GCPP_del_connection (cc->path, cc->off, cc);
263   for (unsigned int i = 0; i < cc->off; i++)
264     GCP_remove_connection (GCPP_get_peer_at_offset (cc->path, i), cc);
265   GNUNET_assert (
266     GNUNET_YES ==
267     GNUNET_CONTAINER_multishortmap_remove (connections,
268                                            &GCC_get_id (cc)
269                                            ->connection_of_tunnel,
270                                            cc));
271   GNUNET_free (cc);
272 }
273
274
275 /**
276  * Destroy a connection, called when the CORE layer is already done
277  * (i.e. has received a BROKEN message), but if we still have to
278  * communicate the destruction of the connection to the tunnel (if one
279  * exists).
280  *
281  * @param cc connection to destroy
282  */
283 void
284 GCC_destroy_without_core (struct CadetConnection *cc)
285 {
286   if (NULL != cc->ct)
287   {
288     GCT_connection_lost (cc->ct);
289     cc->ct = NULL;
290   }
291   GCC_destroy (cc);
292 }
293
294
295 /**
296  * Destroy a connection, called if the tunnel association with the
297  * connection was already broken, but we still need to notify the CORE
298  * layer about the breakage.
299  *
300  * @param cc connection to destroy
301  */
302 void
303 GCC_destroy_without_tunnel (struct CadetConnection *cc)
304 {
305   cc->ct = NULL;
306   if ((CADET_CONNECTION_SENDING_CREATE != cc->state) && (NULL != cc->mq_man))
307   {
308     struct GNUNET_MQ_Envelope *env;
309     struct GNUNET_CADET_ConnectionDestroyMessage *destroy_msg;
310
311     /* Need to notify next hop that we are down. */
312     env =
313       GNUNET_MQ_msg (destroy_msg, GNUNET_MESSAGE_TYPE_CADET_CONNECTION_DESTROY);
314     destroy_msg->cid = cc->cid;
315     GCP_request_mq_cancel (cc->mq_man, env);
316     cc->mq_man = NULL;
317   }
318   GCC_destroy (cc);
319 }
320
321
322 /**
323  * Return the tunnel associated with this connection.
324  *
325  * @param cc connection to query
326  * @return corresponding entry in the tunnel's connection list
327  */
328 struct CadetTConnection *
329 GCC_get_ct (struct CadetConnection *cc)
330 {
331   return cc->ct;
332 }
333
334
335 /**
336  * Obtain performance @a metrics from @a cc.
337  *
338  * @param cc connection to query
339  * @return the metrics
340  */
341 const struct CadetConnectionMetrics *
342 GCC_get_metrics (struct CadetConnection *cc)
343 {
344   return &cc->metrics;
345 }
346
347
348 /**
349  * Send a #GNUNET_MESSAGE_TYPE_CADET_CHANNEL_KEEPALIVE through the
350  * tunnel to prevent it from timing out.
351  *
352  * @param cls the `struct CadetConnection` to keep alive.
353  */
354 static void
355 send_keepalive (void *cls);
356
357
358 /**
359  * Keepalive was transmitted.  Remember this, and possibly
360  * schedule the next one.
361  *
362  * @param cls the `struct CadetConnection` to keep alive.
363  * @param cid identifier of the connection within the tunnel, NULL
364  *            if transmission failed
365  */
366 static void
367 keepalive_done (void *cls,
368                 const struct GNUNET_CADET_ConnectionTunnelIdentifier *cid)
369 {
370   struct CadetConnection *cc = cls;
371
372   cc->keepalive_qe = NULL;
373   if ((GNUNET_YES == cc->mqm_ready) && (NULL == cc->task))
374     cc->task =
375       GNUNET_SCHEDULER_add_delayed (keepalive_period, &send_keepalive, cc);
376 }
377
378
379 /**
380  * Send a #GNUNET_MESSAGE_TYPE_CADET_CHANNEL_KEEPALIVE through the
381  * tunnel to prevent it from timing out.
382  *
383  * @param cls the `struct CadetConnection` to keep alive.
384  */
385 static void
386 send_keepalive (void *cls)
387 {
388   struct CadetConnection *cc = cls;
389   struct GNUNET_MessageHeader msg;
390
391   cc->task = NULL;
392   if (CADET_TUNNEL_KEY_OK != GCT_get_estate (cc->ct->t))
393   {
394     /* Tunnel not yet ready, wait with keepalives... */
395     cc->task =
396       GNUNET_SCHEDULER_add_delayed (keepalive_period, &send_keepalive, cc);
397     return;
398   }
399   GNUNET_assert (NULL != cc->ct);
400   GNUNET_assert (GNUNET_YES == cc->mqm_ready);
401   GNUNET_assert (NULL == cc->keepalive_qe);
402   LOG (GNUNET_ERROR_TYPE_INFO,
403        "Sending KEEPALIVE on behalf of %s via %s\n",
404        GCC_2s (cc),
405        GCT_2s (cc->ct->t));
406   GNUNET_STATISTICS_update (stats, "# keepalives sent", 1, GNUNET_NO);
407   msg.size = htons (sizeof(msg));
408   msg.type = htons (GNUNET_MESSAGE_TYPE_CADET_CHANNEL_KEEPALIVE);
409
410   cc->keepalive_qe = GCT_send (cc->ct->t, &msg, &keepalive_done, cc, NULL);
411 }
412
413
414 /**
415  * We sent a message for which we expect to receive an ACK via
416  * the connection identified by @a cti.
417  *
418  * @param cid connection identifier where we expect an ACK
419  */
420 void
421 GCC_ack_expected (const struct GNUNET_CADET_ConnectionTunnelIdentifier *cid)
422 {
423   struct CadetConnection *cc;
424
425   cc = GCC_lookup (cid);
426   if (NULL == cc)
427     return; /* whopise, connection alredy down? */
428   cc->metrics.num_acked_transmissions++;
429 }
430
431
432 /**
433  * We observed an ACK for a message that was originally sent via
434  * the connection identified by @a cti.
435  *
436  * @param cti connection identifier where we got an ACK for a message
437  *            that was originally sent via this connection (the ACK
438  *            may have gotten back to us via a different connection).
439  */
440 void
441 GCC_ack_observed (const struct GNUNET_CADET_ConnectionTunnelIdentifier *cid)
442 {
443   struct CadetConnection *cc;
444
445   cc = GCC_lookup (cid);
446   if (NULL == cc)
447     return; /* whopise, connection alredy down? */
448   cc->metrics.num_successes++;
449 }
450
451
452 /**
453  * We observed some the given @a latency on the connection
454  * identified by @a cti.  (The same connection was taken
455  * in both directions.)
456  *
457  * @param cid connection identifier where we measured latency
458  * @param latency the observed latency
459  */
460 void
461 GCC_latency_observed (const struct GNUNET_CADET_ConnectionTunnelIdentifier *cid,
462                       struct GNUNET_TIME_Relative latency)
463 {
464   struct CadetConnection *cc;
465   double weight;
466   double result;
467
468   cc = GCC_lookup (cid);
469   if (NULL == cc)
470     return; /* whopise, connection alredy down? */
471   GNUNET_STATISTICS_update (stats, "# latencies observed", 1, GNUNET_NO);
472   cc->latency_datapoints++;
473   if (cc->latency_datapoints >= 7)
474     weight = 7.0;
475   else
476     weight = cc->latency_datapoints;
477   /* Compute weighted average, giving at MOST weight 7 to the
478      existing values, or less if that value is based on fewer than 7
479      measurements. */
480   result = (weight * cc->metrics.aged_latency.rel_value_us)
481            + 1.0 * latency.rel_value_us;
482   result /= (weight + 1.0);
483   cc->metrics.aged_latency.rel_value_us = (uint64_t) result;
484 }
485
486
487 /**
488  * A #GNUNET_MESSAGE_TYPE_CADET_CONNECTION_CREATE_ACK was received for
489  * this connection, implying that the end-to-end connection is up.
490  * Process it.
491  *
492  * @param cc the connection that got the ACK.
493  */
494 void
495 GCC_handle_connection_create_ack (struct CadetConnection *cc)
496 {
497   LOG (GNUNET_ERROR_TYPE_DEBUG,
498        "Received CADET_CONNECTION_CREATE_ACK for %s in state %d (%s)\n",
499        GCC_2s (cc),
500        cc->state,
501        (GNUNET_YES == cc->mqm_ready) ? "MQM ready" : "MQM busy");
502   if (CADET_CONNECTION_READY == cc->state)
503     return; /* Duplicate ACK, ignore */
504   if (NULL != cc->task)
505   {
506     GNUNET_SCHEDULER_cancel (cc->task);
507     cc->task = NULL;
508   }
509   cc->metrics.age = GNUNET_TIME_absolute_get ();
510   update_state (cc, CADET_CONNECTION_READY, cc->mqm_ready);
511   if ((NULL == cc->keepalive_qe) && (GNUNET_YES == cc->mqm_ready) &&
512       (NULL == cc->task))
513     cc->task =
514       GNUNET_SCHEDULER_add_delayed (keepalive_period, &send_keepalive, cc);
515 }
516
517
518 /**
519  * Handle KX message.
520  *
521  * @param cc connection that received encrypted message
522  * @param msg the key exchange message
523  */
524 void
525 GCC_handle_kx (struct CadetConnection *cc,
526                const struct GNUNET_CADET_TunnelKeyExchangeMessage *msg)
527 {
528   LOG (GNUNET_ERROR_TYPE_DEBUG,
529        "Received KX message with ephermal %s on CC %s in state %d\n",
530        GNUNET_e2s (&msg->ephemeral_key),
531        GNUNET_sh2s (&cc->cid.connection_of_tunnel),
532        cc->state);
533   if (CADET_CONNECTION_SENT == cc->state)
534   {
535     /* We didn't get the CADET_CONNECTION_CREATE_ACK, but instead got payload. That's fine,
536        clearly something is working, so pretend we got an ACK. */
537     LOG (GNUNET_ERROR_TYPE_DEBUG,
538          "Faking connection CADET_CONNECTION_CREATE_ACK for %s due to KX\n",
539          GCC_2s (cc));
540     GCC_handle_connection_create_ack (cc);
541   }
542   GCT_handle_kx (cc->ct, msg);
543 }
544
545
546 /**
547  * Handle KX_AUTH message.
548  *
549  * @param cc connection that received encrypted message
550  * @param msg the key exchange message
551  */
552 void
553 GCC_handle_kx_auth (struct CadetConnection *cc,
554                     const struct GNUNET_CADET_TunnelKeyExchangeAuthMessage *msg)
555 {
556   LOG (GNUNET_ERROR_TYPE_DEBUG,
557        "Received KX AUTH message with ephermal %s on CC %s in state %d\n",
558        GNUNET_e2s (&msg->kx.ephemeral_key),
559        GNUNET_sh2s (&cc->cid.connection_of_tunnel),
560        cc->state);
561   if (CADET_CONNECTION_SENT == cc->state)
562   {
563     /* We didn't get the CADET_CONNECTION_CREATE_ACK, but instead got payload. That's fine,
564        clearly something is working, so pretend we got an ACK. */
565     LOG (GNUNET_ERROR_TYPE_DEBUG,
566          "Faking connection CADET_CONNECTION_CREATE_ACK for %s due to KX\n",
567          GCC_2s (cc));
568     GCC_handle_connection_create_ack (cc);
569   }
570   GCT_handle_kx_auth (cc->ct, msg);
571 }
572
573
574 /**
575  * Handle encrypted message.
576  *
577  * @param cc connection that received encrypted message
578  * @param msg the encrypted message to decrypt
579  */
580 void
581 GCC_handle_encrypted (struct CadetConnection *cc,
582                       const struct GNUNET_CADET_TunnelEncryptedMessage *msg)
583 {
584   if (CADET_CONNECTION_SENT == cc->state)
585   {
586     /* We didn't get the CREATE_ACK, but instead got payload. That's fine,
587        clearly something is working, so pretend we got an ACK. */
588     LOG (GNUNET_ERROR_TYPE_DEBUG,
589          "Faking connection ACK for %s due to ENCRYPTED payload\n",
590          GCC_2s (cc));
591     GCC_handle_connection_create_ack (cc);
592   }
593   cc->metrics.last_use = GNUNET_TIME_absolute_get ();
594   GCT_handle_encrypted (cc->ct, msg);
595 }
596
597
598 /**
599  * Set the signature for a monotime value on a GNUNET_CADET_ConnectionCreateMessage.
600  *
601  * @param msg The GNUNET_CADET_ConnectionCreateMessage.
602  */
603 void
604 set_monotime_sig (struct GNUNET_CADET_ConnectionCreateMessage *msg)
605 {
606
607   struct CadetConnectionCreatePS cp = { .purpose.purpose = htonl (
608                                           GNUNET_SIGNATURE_PURPOSE_CADET_CONNECTION_INITIATOR),
609                                         .purpose.size = htonl (sizeof(cp)),
610                                         .monotonic_time = msg->monotime};
611
612   GNUNET_CRYPTO_eddsa_sign (my_private_key, &cp,
613                             &msg->monotime_sig);
614
615 }
616
617 /**
618  * Send a #GNUNET_MESSAGE_TYPE_CADET_CONNECTION_CREATE message to the
619  * first hop.
620  *
621  * @param cls the `struct CadetConnection` to initiate
622  */
623 static void
624 send_create (void *cls)
625 {
626   struct CadetConnection *cc = cls;
627   struct GNUNET_CADET_ConnectionCreateMessage *create_msg;
628   struct GNUNET_PeerIdentity *pids;
629   struct GNUNET_MQ_Envelope *env;
630   struct CadetTunnel *t;
631
632   cc->task = NULL;
633   GNUNET_assert (GNUNET_YES == cc->mqm_ready);
634   env =
635     GNUNET_MQ_msg_extra (create_msg,
636                          (2 + cc->off) * sizeof(struct GNUNET_PeerIdentity),
637                          GNUNET_MESSAGE_TYPE_CADET_CONNECTION_CREATE);
638   // TODO This will be removed in a major release, because this will be a protocol breaking change. We set the deprecated 'reliable' bit here that was removed.
639   create_msg->options = 2;
640   create_msg->cid = cc->cid;
641
642   // check for tunnel state and set signed monotime (xrs,t3ss)
643   t = GCP_get_tunnel (cc->destination, GNUNET_YES);
644   if ((NULL != t)&& (GCT_get_estate (t) == CADET_TUNNEL_KEY_UNINITIALIZED) &&
645       (GCT_alice_or_betty (GCP_get_id (cc->destination)) == GNUNET_NO))
646   {
647     create_msg->has_monotime = GNUNET_YES;
648     create_msg->monotime = GNUNET_TIME_absolute_hton (
649       GNUNET_TIME_absolute_get_monotonic (cfg));
650     set_monotime_sig (create_msg);
651   }
652
653   pids = (struct GNUNET_PeerIdentity *) &create_msg[1];
654   pids[0] = my_full_id;
655   for (unsigned int i = 0; i <= cc->off; i++)
656     pids[i + 1] = *GCP_get_id (GCPP_get_peer_at_offset (cc->path, i));
657   LOG (GNUNET_ERROR_TYPE_DEBUG,
658        "Sending CADET_CONNECTION_CREATE message for %s with %u hops\n",
659        GCC_2s (cc),
660        cc->off + 2);
661   cc->env = env;
662   cc->retry_delay = GNUNET_TIME_STD_BACKOFF (cc->retry_delay);
663   cc->create_at = GNUNET_TIME_relative_to_absolute (cc->retry_delay);
664   update_state (cc, CADET_CONNECTION_SENT, GNUNET_NO);
665   GCP_send (cc->mq_man, env);
666 }
667
668
669 /**
670  * Send a CREATE_ACK message towards the origin.
671  *
672  * @param cls the `struct CadetConnection` to initiate
673  */
674 static void
675 send_create_ack (void *cls)
676 {
677   struct CadetConnection *cc = cls;
678   struct GNUNET_CADET_ConnectionCreateAckMessage *ack_msg;
679   struct GNUNET_MQ_Envelope *env;
680
681   cc->task = NULL;
682   LOG (GNUNET_ERROR_TYPE_DEBUG,
683        "Sending CONNECTION_CREATE_ACK message for %s\n",
684        GCC_2s (cc));
685   GNUNET_assert (GNUNET_YES == cc->mqm_ready);
686   env =
687     GNUNET_MQ_msg (ack_msg, GNUNET_MESSAGE_TYPE_CADET_CONNECTION_CREATE_ACK);
688   ack_msg->cid = cc->cid;
689   cc->env = env;
690   cc->retry_delay = GNUNET_TIME_STD_BACKOFF (cc->retry_delay);
691   cc->create_ack_at = GNUNET_TIME_relative_to_absolute (cc->retry_delay);
692   if (CADET_CONNECTION_CREATE_RECEIVED == cc->state)
693     update_state (cc, CADET_CONNECTION_READY, GNUNET_NO);
694   if (CADET_CONNECTION_READY == cc->state)
695     cc->task =
696       GNUNET_SCHEDULER_add_delayed (keepalive_period, &send_keepalive, cc);
697   GCP_send (cc->mq_man, env);
698 }
699
700
701 /**
702  * We got a #GNUNET_MESSAGE_TYPE_CADET_CONNECTION_CREATE for a
703  * connection that we already have.  Either our ACK got lost
704  * or something is fishy.  Consider retransmitting the ACK.
705  *
706  * @param cc connection that got the duplicate CREATE
707  */
708 void
709 GCC_handle_duplicate_create (struct CadetConnection *cc)
710 {
711   if (GNUNET_YES == cc->mqm_ready)
712   {
713     LOG (GNUNET_ERROR_TYPE_DEBUG,
714          "Got duplicate CREATE for %s, scheduling another ACK (%s)\n",
715          GCC_2s (cc),
716          (GNUNET_YES == cc->mqm_ready) ? "MQM ready" : "MQM busy");
717     /* Revert back to the state of having only received the 'CREATE',
718        and immediately proceed to send the CREATE_ACK. */
719     update_state (cc, CADET_CONNECTION_CREATE_RECEIVED, cc->mqm_ready);
720     if (NULL != cc->task)
721       GNUNET_SCHEDULER_cancel (cc->task);
722     cc->task =
723       GNUNET_SCHEDULER_add_at (cc->create_ack_at, &send_create_ack, cc);
724   }
725   else
726   {
727     /* We are currently sending something else back, which
728        can only be an ACK or payload, either of which would
729        do. So actually no need to do anything. */
730     LOG (GNUNET_ERROR_TYPE_DEBUG,
731          "Got duplicate CREATE for %s. MQ is busy, not queueing another ACK\n",
732          GCC_2s (cc));
733   }
734 }
735
736
737 /**
738  * There has been a change in the message queue existence for our
739  * peer at the first hop.  Adjust accordingly.
740  *
741  * @param cls the `struct CadetConnection`
742  * @param available #GNUNET_YES if sending is now possible,
743  *                  #GNUNET_NO if sending is no longer possible
744  *                  #GNUNET_SYSERR if sending is no longer possible
745  *                                 and the last envelope was discarded
746  */
747 static void
748 manage_first_hop_mq (void *cls, int available)
749 {
750   struct CadetConnection *cc = cls;
751
752   if (GNUNET_YES != available)
753   {
754     /* Connection is down, for now... */
755     LOG (GNUNET_ERROR_TYPE_DEBUG, "Core MQ for %s went down\n", GCC_2s (cc));
756     update_state (cc, CADET_CONNECTION_NEW, GNUNET_NO);
757     cc->retry_delay = INITIAL_CONNECTION_CREATE_RETRY_DELAY;
758     if (NULL != cc->task)
759     {
760       GNUNET_SCHEDULER_cancel (cc->task);
761       cc->task = NULL;
762     }
763     return;
764   }
765
766   update_state (cc, cc->state, GNUNET_YES);
767   LOG (GNUNET_ERROR_TYPE_DEBUG,
768        "Core MQ for %s became available in state %d\n",
769        GCC_2s (cc),
770        cc->state);
771   switch (cc->state)
772   {
773   case CADET_CONNECTION_NEW:
774     /* Transmit immediately */
775     cc->task = GNUNET_SCHEDULER_add_at (cc->create_at, &send_create, cc);
776     break;
777
778   case CADET_CONNECTION_SENDING_CREATE:
779     /* Should not be possible to be called in this state. */
780     GNUNET_assert (0);
781     break;
782
783   case CADET_CONNECTION_SENT:
784     /* Retry a bit later... */
785     cc->task = GNUNET_SCHEDULER_add_at (cc->create_at, &send_create, cc);
786     break;
787
788   case CADET_CONNECTION_CREATE_RECEIVED:
789     /* We got the 'CREATE' (incoming connection), should send the CREATE_ACK */
790     cc->metrics.age = GNUNET_TIME_absolute_get ();
791     cc->task =
792       GNUNET_SCHEDULER_add_at (cc->create_ack_at, &send_create_ack, cc);
793     break;
794
795   case CADET_CONNECTION_READY:
796     if ((NULL == cc->keepalive_qe) && (GNUNET_YES == cc->mqm_ready) &&
797         (NULL == cc->task))
798     {
799       LOG (GNUNET_ERROR_TYPE_DEBUG,
800            "Scheduling keepalive for %s in %s\n",
801            GCC_2s (cc),
802            GNUNET_STRINGS_relative_time_to_string (keepalive_period,
803                                                    GNUNET_YES));
804       cc->task =
805         GNUNET_SCHEDULER_add_delayed (keepalive_period, &send_keepalive, cc);
806     }
807     break;
808   }
809 }
810
811
812 /**
813  * Create a connection to @a destination via @a path and notify @a cb
814  * whenever we are ready for more data.  Shared logic independent of
815  * who is initiating the connection.
816  *
817  * @param destination where to go
818  * @param path which path to take (may not be the full path)
819  * @param off offset of @a destination on @a path
820  * @param ct which tunnel uses this connection
821  * @param init_state initial state for the connection
822  * @param ready_cb function to call when ready to transmit
823  * @param ready_cb_cls closure for @a cb
824  * @return handle to the connection
825  */
826 static struct CadetConnection *
827 connection_create (struct CadetPeer *destination,
828                    struct CadetPeerPath *path,
829                    unsigned int off,
830                    struct CadetTConnection *ct,
831                    const struct GNUNET_CADET_ConnectionTunnelIdentifier *cid,
832                    enum CadetConnectionState init_state,
833                    GCC_ReadyCallback ready_cb,
834                    void *ready_cb_cls)
835 {
836   struct CadetConnection *cc;
837   struct CadetPeer *first_hop;
838
839   cc = GNUNET_new (struct CadetConnection);
840   cc->state = init_state;
841   cc->ct = ct;
842   cc->destination = destination; /* xrs,t3ss,lurchi*/
843   cc->cid = *cid;
844   cc->retry_delay =
845     GNUNET_TIME_relative_multiply (INITIAL_CONNECTION_CREATE_RETRY_DELAY, off);
846   GNUNET_assert (GNUNET_OK ==
847                  GNUNET_CONTAINER_multishortmap_put (
848                    connections,
849                    &GCC_get_id (cc)->connection_of_tunnel,
850                    cc,
851                    GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
852   cc->ready_cb = ready_cb;
853   cc->ready_cb_cls = ready_cb_cls;
854   cc->path = path;
855   cc->off = off;
856   LOG (GNUNET_ERROR_TYPE_DEBUG,
857        "Creating %s using path %s (offset: %u)\n",
858        GCC_2s (cc),
859        GCPP_2s (path),
860        off);
861   GCPP_add_connection (path, off, cc);
862   for (unsigned int i = 0; i < off; i++)
863     GCP_add_connection (GCPP_get_peer_at_offset (path, i), cc);
864   first_hop = GCPP_get_peer_at_offset (path, 0);
865   cc->mq_man = GCP_request_mq (first_hop, &manage_first_hop_mq, cc);
866   return cc;
867 }
868
869
870 /**
871  * Create a connection to @a destination via @a path and
872  * notify @a cb whenever we are ready for more data.  This
873  * is an inbound tunnel, so we must use the existing @a cid
874  *
875  * @param destination where to go
876  * @param path which path to take (may not be the full path)
877  * @param ct which tunnel uses this connection
878  * @param ready_cb function to call when ready to transmit
879  * @param ready_cb_cls closure for @a cb
880  * @return handle to the connection, NULL if we already have
881  *         a connection that takes precedence on @a path
882  */
883 struct CadetConnection *
884 GCC_create_inbound (struct CadetPeer *destination,
885                     struct CadetPeerPath *path,
886                     struct CadetTConnection *ct,
887                     const struct GNUNET_CADET_ConnectionTunnelIdentifier *cid,
888                     GCC_ReadyCallback ready_cb,
889                     void *ready_cb_cls)
890 {
891   struct CadetConnection *cc;
892   unsigned int off;
893
894   off = GCPP_find_peer (path, destination);
895   GNUNET_assert (UINT_MAX != off);
896   cc = GCPP_get_connection (path, destination, off);
897   if (NULL != cc)
898   {
899     int cmp;
900
901     cmp = GNUNET_memcmp (cid, &cc->cid);
902     if (0 == cmp)
903     {
904       /* Two peers picked the SAME random connection identifier at the
905          same time for the same path? Must be malicious.  Drop
906          connection (existing and inbound), even if it is the only
907          one. */
908       GNUNET_break_op (0);
909       GCT_connection_lost (cc->ct);
910       GCC_destroy_without_tunnel (cc);
911       return NULL;
912     }
913     if (0 < cmp)
914     {
915       /* drop existing */
916       LOG (GNUNET_ERROR_TYPE_DEBUG,
917            "Got two connections on %s, dropping my existing %s\n",
918            GCPP_2s (path),
919            GCC_2s (cc));
920       GCT_connection_lost (cc->ct);
921       GCC_destroy_without_tunnel (cc);
922     }
923     else
924     {
925       /* keep existing */
926       LOG (GNUNET_ERROR_TYPE_DEBUG,
927            "Got two connections on %s, keeping my existing %s\n",
928            GCPP_2s (path),
929            GCC_2s (cc));
930       return NULL;
931     }
932   }
933
934   return connection_create (destination,
935                             path,
936                             off,
937                             ct,
938                             cid,
939                             CADET_CONNECTION_CREATE_RECEIVED,
940                             ready_cb,
941                             ready_cb_cls);
942 }
943
944
945 /**
946  * Create a connection to @a destination via @a path and
947  * notify @a cb whenever we are ready for more data.
948  *
949  * @param destination where to go
950  * @param path which path to take (may not be the full path)
951  * @param off offset of @a destination on @a path
952  * @param ct tunnel that uses the connection
953  * @param ready_cb function to call when ready to transmit
954  * @param ready_cb_cls closure for @a cb
955  * @return handle to the connection
956  */
957 struct CadetConnection *
958 GCC_create (struct CadetPeer *destination,
959             struct CadetPeerPath *path,
960             unsigned int off,
961             struct CadetTConnection *ct,
962             GCC_ReadyCallback ready_cb,
963             void *ready_cb_cls)
964 {
965   struct GNUNET_CADET_ConnectionTunnelIdentifier cid;
966
967   GNUNET_CRYPTO_random_block (GNUNET_CRYPTO_QUALITY_NONCE, &cid, sizeof(cid));
968   return connection_create (destination,
969                             path,
970                             off,
971                             ct,
972                             &cid,
973                             CADET_CONNECTION_NEW,
974                             ready_cb,
975                             ready_cb_cls);
976 }
977
978
979 /**
980  * Transmit message @a msg via connection @a cc.  Must only be called
981  * (once) after the connection has signalled that it is ready via the
982  * `ready_cb`.  Clients can also use #GCC_is_ready() to check if the
983  * connection is right now ready for transmission.
984  *
985  * @param cc connection identification
986  * @param env envelope with message to transmit; must NOT
987  *            yet have a #GNUNET_MQ_notify_sent() callback attached to it
988  */
989 void
990 GCC_transmit (struct CadetConnection *cc, struct GNUNET_MQ_Envelope *env)
991 {
992   LOG (GNUNET_ERROR_TYPE_DEBUG,
993        "Scheduling message for transmission on %s\n",
994        GCC_2s (cc));
995   GNUNET_assert (GNUNET_YES == cc->mqm_ready);
996   GNUNET_assert (CADET_CONNECTION_READY == cc->state);
997   cc->metrics.last_use = GNUNET_TIME_absolute_get ();
998   cc->mqm_ready = GNUNET_NO;
999   if (NULL != cc->task)
1000   {
1001     GNUNET_SCHEDULER_cancel (cc->task);
1002     cc->task = NULL;
1003   }
1004   GCP_send (cc->mq_man, env);
1005 }
1006
1007
1008 /**
1009  * Obtain the path used by this connection.
1010  *
1011  * @param cc connection
1012  * @param off[out] set to the length of the path we use
1013  * @return path to @a cc
1014  */
1015 struct CadetPeerPath *
1016 GCC_get_path (struct CadetConnection *cc, unsigned int *off)
1017 {
1018   *off = cc->off;
1019   return cc->path;
1020 }
1021
1022
1023 /**
1024  * Obtain unique ID for the connection.
1025  *
1026  * @param cc connection.
1027  * @return unique number of the connection
1028  */
1029 const struct GNUNET_CADET_ConnectionTunnelIdentifier *
1030 GCC_get_id (struct CadetConnection *cc)
1031 {
1032   return &cc->cid;
1033 }
1034
1035
1036 /**
1037  * Get a (static) string for a connection.
1038  *
1039  * @param cc Connection.
1040  */
1041 const char *
1042 GCC_2s (const struct CadetConnection *cc)
1043 {
1044   static char buf[128];
1045
1046   if (NULL == cc)
1047     return "Connection(NULL)";
1048
1049   if (NULL != cc->ct)
1050   {
1051     GNUNET_snprintf (buf,
1052                      sizeof(buf),
1053                      "Connection %s (%s)",
1054                      GNUNET_sh2s (&cc->cid.connection_of_tunnel),
1055                      GCT_2s (cc->ct->t));
1056     return buf;
1057   }
1058   GNUNET_snprintf (buf,
1059                    sizeof(buf),
1060                    "Connection %s",
1061                    GNUNET_sh2s (&cc->cid.connection_of_tunnel));
1062   return buf;
1063 }
1064
1065
1066 #define LOG2(level, ...) \
1067   GNUNET_log_from_nocheck (level, "cadet-con", __VA_ARGS__)
1068
1069
1070 /**
1071  * Log connection info.
1072  *
1073  * @param cc connection
1074  * @param level Debug level to use.
1075  */
1076 void
1077 GCC_debug (struct CadetConnection *cc, enum GNUNET_ErrorType level)
1078 {
1079 #if ! defined(GNUNET_CULL_LOGGING)
1080   int do_log;
1081
1082   do_log = GNUNET_get_log_call_status (level & (~GNUNET_ERROR_TYPE_BULK),
1083                                        "cadet-con",
1084                                        __FILE__,
1085                                        __FUNCTION__,
1086                                        __LINE__);
1087   if (0 == do_log)
1088     return;
1089   if (NULL == cc)
1090   {
1091     LOG2 (level, "Connection (NULL)\n");
1092     return;
1093   }
1094   LOG2 (level,
1095         "%s to %s via path %s in state %d is %s\n",
1096         GCC_2s (cc),
1097         GCP_2s (cc->destination),
1098         GCPP_2s (cc->path),
1099         cc->state,
1100         (GNUNET_YES == cc->mqm_ready) ? "ready" : "busy");
1101 #endif
1102 }
1103
1104
1105 /* end of gnunet-service-cadet_connection.c */