REST/NAMESTORE: rework API
[oweals/gnunet.git] / src / cadet / gnunet-service-cadet_tunnels.c
index dbd84a818553a06459a9bdd17692a092438eb7a5..a2a493ebde00236f9c586c95f2457712f2a316e3 100644 (file)
@@ -2,20 +2,20 @@
      This file is part of GNUnet.
      Copyright (C) 2013, 2017, 2018 GNUnet e.V.
 
-     GNUnet is free software; you can redistribute it and/or modify
-     it under the terms of the GNU General Public License as published
-     by the Free Software Foundation; either version 3, or (at your
-     option) any later version.
+     GNUnet is free software: you can redistribute it and/or modify it
+     under the terms of the GNU Affero General Public License as published
+     by the Free Software Foundation, either version 3 of the License,
+     or (at your option) any later version.
 
      GNUnet is distributed in the hope that it will be useful, but
      WITHOUT ANY WARRANTY; without even the implied warranty of
      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
-     General Public License for more details.
+     Affero General Public License for more details.
+    
+     You should have received a copy of the GNU Affero General Public License
+     along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
-     You should have received a copy of the GNU General Public License
-     along with GNUnet; see the file COPYING.  If not, write to the
-     Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
-     Boston, MA 02110-1301, USA.
+     SPDX-License-Identifier: AGPL3.0-or-later
 */
 /**
  * @file cadet/gnunet-service-cadet_tunnels.c
@@ -459,18 +459,18 @@ struct CadetTunnel
 
 
 /**
- * Am I Alice or Bob, or talking to myself?
+ * Am I Alice or Betty (some call her Bob), or talking to myself?
  *
  * @param other the other peer
- * @return #GNUNET_YES for Alice, #GNUNET_NO for Bob, #GNUNET_SYSERR if talking to myself
+ * @return #GNUNET_YES for Alice, #GNUNET_NO for Betty, #GNUNET_SYSERR if talking to myself
  */
 static int
-alice_or_bob (const struct GNUNET_PeerIdentity *other)
+alice_or_betty (const struct GNUNET_PeerIdentity *other)
 {
-  if (0 > GNUNET_CRYPTO_cmp_peer_identity (&my_full_id,
+  if (0 > GNUNET_memcmp (&my_full_id,
                                            other))
     return GNUNET_YES;
-  else if (0 < GNUNET_CRYPTO_cmp_peer_identity (&my_full_id,
+  else if (0 < GNUNET_memcmp (&my_full_id,
                                                 other))
     return GNUNET_NO;
   else
@@ -1013,9 +1013,8 @@ try_old_ax_keys (struct CadetTunnelAxolotl *ax,
             0,
             &key->HK,
             hmac);
-    if (0 == memcmp (hmac,
-                     &src->hmac,
-                     sizeof (*hmac)))
+    if (0 == GNUNET_memcmp (hmac,
+                     &src->hmac))
     {
       valid_HK = &key->HK;
       break;
@@ -1047,9 +1046,8 @@ try_old_ax_keys (struct CadetTunnelAxolotl *ax,
           (N != key->Kn) )
     key = key->next;
   if ( (NULL == key) ||
-       (0 != memcmp (&key->HK,
-                     valid_HK,
-                     sizeof (*valid_HK))) )
+       (0 != GNUNET_memcmp (&key->HK,
+                     valid_HK)) )
     return -1;
 
   /* Decrypt payload */
@@ -1181,9 +1179,8 @@ t_ax_decrypt_and_validate (struct CadetTunnelAxolotl *ax,
           sizeof (struct GNUNET_CADET_AxHeader) + esize,
           0, &ax->HKr,
           &msg_hmac);
-  if (0 != memcmp (&msg_hmac,
-                   &src->hmac,
-                   sizeof (msg_hmac)))
+  if (0 != GNUNET_memcmp (&msg_hmac,
+                   &src->hmac))
   {
     static const char ctx[] = "axolotl ratchet";
     struct GNUNET_CRYPTO_SymmetricSessionKey keys[3]; /* RKp, NHKp, CKp */
@@ -1197,9 +1194,8 @@ t_ax_decrypt_and_validate (struct CadetTunnelAxolotl *ax,
             0,
             &ax->NHKr,
             &msg_hmac);
-    if (0 != memcmp (&msg_hmac,
-                     &src->hmac,
-                     sizeof (msg_hmac)))
+    if (0 != GNUNET_memcmp (&msg_hmac,
+                     &src->hmac))
     {
       /* Try the skipped keys, if that fails, we're out of luck. */
       return try_old_ax_keys (ax,
@@ -1347,7 +1343,7 @@ send_kx (struct CadetTunnel *t,
   struct GNUNET_CADET_TunnelKeyExchangeMessage *msg;
   enum GNUNET_CADET_KX_Flags flags;
 
-  if (GNUNET_YES != alice_or_bob (GCP_get_id (t->destination)))
+  if (GNUNET_YES != alice_or_betty (GCP_get_id (t->destination)))
     return; /* only Alice may send KX */
   if ( (NULL == ct) ||
        (GNUNET_NO == ct->is_ready) )
@@ -1523,14 +1519,13 @@ update_ax_by_kx (struct CadetTunnelAxolotl *ax,
   const char salt[] = "CADET Axolotl salt";
   int am_I_alice;
 
-  if (GNUNET_SYSERR == (am_I_alice = alice_or_bob (pid)))
+  if (GNUNET_SYSERR == (am_I_alice = alice_or_betty (pid)))
   {
     GNUNET_break_op (0);
     return GNUNET_SYSERR;
   }
-  if (0 == memcmp (&ax->DHRr,
-                   ratchet_key,
-                   sizeof (*ratchet_key)))
+  if (0 == GNUNET_memcmp (&ax->DHRr,
+                     ratchet_key))
   {
     GNUNET_STATISTICS_update (stats,
                               "# Ratchet key already known",
@@ -1726,9 +1721,9 @@ GCT_handle_kx (struct CadetTConnection *ct,
                             1,
                             GNUNET_NO);
   if (GNUNET_YES ==
-      alice_or_bob (GCP_get_id (t->destination)))
+      alice_or_betty (GCP_get_id (t->destination)))
   {
-    /* Bob is not allowed to send KX! */
+    /* Betty/Bob is not allowed to send KX! */
     GNUNET_break_op (0);
     return;
   }
@@ -1872,9 +1867,8 @@ check_ee (const struct GNUNET_CRYPTO_EcdhePrivateKey *e1,
                  GNUNET_CRYPTO_ecc_ecdh (e2,
                                          &p1,
                                          &hc2));
-  GNUNET_break (0 == memcmp (&hc1,
-                             &hc2,
-                             sizeof (hc1)));
+  GNUNET_break (0 == GNUNET_memcmp (&hc1,
+                             &hc2));
 }
 
 
@@ -1899,9 +1893,8 @@ check_ed (const struct GNUNET_CRYPTO_EcdhePrivateKey *e1,
                  GNUNET_CRYPTO_eddsa_ecdh (e2,
                                            &p1,
                                            &hc2));
-  GNUNET_break (0 == memcmp (&hc1,
-                             &hc2,
-                             sizeof (hc1)));
+  GNUNET_break (0 == GNUNET_memcmp (&hc1,
+                             &hc2));
 }
 
 
@@ -1974,9 +1967,8 @@ GCT_handle_kx_auth (struct CadetTConnection *ct,
   GNUNET_CRYPTO_hash (&ax_tmp.RK,
                       sizeof (ax_tmp.RK),
                       &kx_auth);
-  if (0 != memcmp (&kx_auth,
-                   &msg->auth,
-                   sizeof (kx_auth)))
+  if (0 != GNUNET_memcmp (&kx_auth,
+                   &msg->auth))
   {
     /* This KX_AUTH is not using the latest KX/KX_AUTH data
        we transmitted to the sender, refuse it, try KX again. */
@@ -1985,16 +1977,15 @@ GCT_handle_kx_auth (struct CadetTConnection *ct,
                               1,
                               GNUNET_NO);
     LOG (GNUNET_ERROR_TYPE_WARNING,
-         "KX AUTH missmatch!\n");
+         "KX AUTH mismatch!\n");
 #if DEBUG_KX
     {
       struct GNUNET_CRYPTO_EcdhePublicKey ephemeral_key;
 
       GNUNET_CRYPTO_ecdhe_key_get_public (&ax_tmp.kx_0,
                                           &ephemeral_key);
-      if (0 != memcmp (&ephemeral_key,
-                       &msg->r_ephemeral_key_XXX,
-                       sizeof (ephemeral_key)))
+      if (0 != GNUNET_memcmp (&ephemeral_key,
+                       &msg->r_ephemeral_key_XXX))
       {
         LOG (GNUNET_ERROR_TYPE_WARNING,
            "My ephemeral is %s!\n",
@@ -2048,6 +2039,13 @@ GCT_handle_kx_auth (struct CadetTConnection *ct,
        Nothing to do here. */
     break;
   }
+  if (0 != (GNUNET_CADET_KX_FLAG_FORCE_REPLY & ntohl (msg->kx.flags)))
+  {
+    send_kx_auth (t,
+                 NULL,
+                 &t->ax,
+                 GNUNET_NO);
+  }
 }
 
 
@@ -2070,7 +2068,7 @@ get_next_free_ctn (struct CadetTunnel *t)
   int cmp;
   uint32_t highbit;
 
-  cmp = GNUNET_CRYPTO_cmp_peer_identity (&my_full_id,
+  cmp = GNUNET_memcmp (&my_full_id,
                                          GCP_get_id (GCT_get_destination (t)));
   if (0 < cmp)
     highbit = HIGH_BIT;
@@ -2422,8 +2420,6 @@ connection_ready_cb (void *cls,
   {
   case CADET_TUNNEL_KEY_UNINITIALIZED:
     /* Do not begin KX if WE have no channels waiting! */
-    if (0 == GCT_count_channels (t))
-      return;
     if (0 != GNUNET_TIME_absolute_get_remaining (t->next_kx_attempt).rel_value_us)
       return; /* wait for timeout before retrying */
     /* We are uninitialized, just transmit immediately,
@@ -2436,6 +2432,15 @@ connection_ready_cb (void *cls,
     send_kx (t,
              ct,
              &t->ax);
+    if ( (0 ==
+         GCT_count_channels (t)) &&
+        (NULL == t->destroy_task) )
+    {
+      t->destroy_task
+       = GNUNET_SCHEDULER_add_delayed (IDLE_DESTROY_DELAY,
+                                       &destroy_tunnel,
+                                       t);
+    }
     break;
   case CADET_TUNNEL_KEY_AX_RECV:
   case CADET_TUNNEL_KEY_AX_SENT: