introduce GNUNET_memcmp() and GNUNET_is_zero()
authorChristian Grothoff <christian@grothoff.org>
Sat, 6 Apr 2019 12:37:07 +0000 (14:37 +0200)
committerChristian Grothoff <christian@grothoff.org>
Sat, 6 Apr 2019 12:37:07 +0000 (14:37 +0200)
13 files changed:
ChangeLog
src/cadet/gnunet-service-cadet_tunnels.c
src/fs/gnunet-service-fs.c
src/fs/gnunet-service-fs_cp.c
src/include/gnunet_cadet_service.h
src/include/gnunet_common.h
src/include/gnunet_crypto_lib.h
src/rps/gnunet-service-rps.c
src/rps/gnunet-service-rps_sampler_elem.c
src/rps/rps-sampler_common.c
src/util/crypto_ecc.c
src/util/service.c
src/util/test_peer.c

index 31b0b637e7a7b29e3a2e40f2f2d568c184314b52..9d2d27a05374f456adea24d41a2fc267bd80de49 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,7 @@
+Sat 06 Apr 2019 02:36:17 PM CEST
+  Introducing GNUNET_memcmp() and GNUNET_is_zero() for better typing
+  when comparing memory areas. -CG
+
 Fri 05 Apr 2019 11:38:53 PM CEST
   Use paths of sysctl, ip, ifconfig and ip(6)tables from configure
   if they work. (#5352). -CG
index ad4ed6e96fc36c862ac5211fd2a01f29006825b8..57a6d1adfe15112157d2585d57abc14abbaf89ec 100644 (file)
@@ -467,10 +467,10 @@ struct CadetTunnel
 static int
 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
@@ -2077,7 +2077,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;
index b9ccf7c55bc81e82bbc5206f62d1d9846355ef94..691242ce32ead429075da9091c889bb315410b88 100644 (file)
@@ -1224,7 +1224,7 @@ static void
 peer_init_handler (void *cls,
                    const struct GNUNET_PeerIdentity *my_identity)
 {
-  if (0 != GNUNET_CRYPTO_cmp_peer_identity (&GSF_my_id,
+  if (0 != GNUNET_memcmp (&GSF_my_id,
                                             my_identity))
   {
     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
index 6486732a603a4ad9d0ee58a8667e613e4ff4a550..a3a52122171614610ff38bc187af2732911e9110 100644 (file)
@@ -588,7 +588,7 @@ GSF_peer_connect_handler (void *cls,
   struct GSF_ConnectedPeer *cp;
 
   if (0 ==
-      GNUNET_CRYPTO_cmp_peer_identity (&GSF_my_id,
+      GNUNET_memcmp (&GSF_my_id,
                                        peer))
     return NULL;
   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
index b8326657dfc8b4d6a9b384f7483a29e84da94cad..da914ac9e0dd5b07416ae20bc38667e8fa7a030a 100644 (file)
@@ -89,7 +89,7 @@ struct GNUNET_CADET_ChannelTunnelNumber
    * Given two peers, both may initiate channels over the same tunnel.
    * The @e cn must be greater or equal to 0x80000000 (high-bit set)
    * for tunnels initiated with the peer that has the larger peer
-   * identity as compared using #GNUNET_CRYPTO_cmp_peer_identity().
+   * identity as compared using #GNUNET_memcmp().
    */
   uint32_t cn GNUNET_PACKED;
 };
index ca49d31093cd06ab961eea7c94b0d2c8066a91cb..773ff8f3e7e810b72da9494a8fa42e8decda1046 100644 (file)
@@ -913,6 +913,32 @@ GNUNET_ntoh_double (double d);
  */
 #define GNUNET_new(type) (type *) GNUNET_malloc (sizeof (type))
 
+
+/**
+ * Compare memory in @a a and @a b, where both must be of
+ * the same pointer type.
+ */
+#define GNUNET_memcmp(a,b) ({ \
+  typeof(b) _a = (a); \
+  typeof(a) _b = (b); \
+  memcmp(_a, \
+         _b, \
+         sizeof (*a)); })
+
+
+/**
+ * Check that memory in @a a is all zeros. @a a must be a pointer.
+ *
+ * @param a pointer to a struct which should be tested for the
+ *          entire memory being zero'ed out.
+ */
+#define GNUNET_is_zero(a) ({ \
+  typeof(*a) _z = { 0 };      \
+  memcmp(a, \
+         &_z, \
+         sizeof (_z)); })
+
+
 /**
  * Call memcpy() but check for @a n being 0 first. In the latter
  * case, it is now safe to pass NULL for @a src or @a dst.
index 6a9eddd7cb8fbc19f611c00f02abef7ff613d055..c6ae943b9615480a09205a5e61fc56240080b865 100644 (file)
@@ -1354,20 +1354,6 @@ GNUNET_CRYPTO_get_peer_identity (const struct GNUNET_CONFIGURATION_Handle *cfg,
                                  struct GNUNET_PeerIdentity *dst);
 
 
-/**
- * Compare two Peer Identities.
- *
- * @param first first peer identity
- * @param second second peer identity
- * @return bigger than 0 if first > second,
- *         0 if they are the same
- *         smaller than 0 if second > first
- */
-int
-GNUNET_CRYPTO_cmp_peer_identity (const struct GNUNET_PeerIdentity *first,
-                                 const struct GNUNET_PeerIdentity *second);
-
-
 /**
  * Internal structure used to cache pre-calculated values for DLOG calculation.
  */
@@ -1397,7 +1383,7 @@ struct GNUNET_CRYPTO_EccPoint
  */
 struct GNUNET_CRYPTO_EccDlogContext *
 GNUNET_CRYPTO_ecc_dlog_prepare (unsigned int max,
-                               unsigned int mem);
+                                unsigned int mem);
 
 
 /**
index 1871ef006cfd004073bba660fc519439089d5292..f6fe1758925e62677ac750f9a33fac150ffd7545 100644 (file)
@@ -2161,7 +2161,7 @@ rem_from_list (struct GNUNET_PeerIdentity **peer_list,
 
   for ( i = 0 ; i < *list_size ; i++ )
   {
-    if (0 == GNUNET_CRYPTO_cmp_peer_identity (&tmp[i], peer))
+    if (0 == GNUNET_memcmp (&tmp[i], peer))
     {
       if (i < *list_size -1)
       { /* Not at the last entry -- shift peers left */
@@ -2742,7 +2742,7 @@ clean_peer (struct Sub *sub,
         "Going to remove send channel to peer %s\n",
         GNUNET_i2s (peer));
     #if ENABLE_MALICIOUS
-    if (0 != GNUNET_CRYPTO_cmp_peer_identity (&attacked_peer,
+    if (0 != GNUNET_memcmp (&attacked_peer,
                                               peer))
       (void) destroy_sending_channel (get_peer_ctx (sub->peer_map,
                                                     peer));
@@ -3596,7 +3596,7 @@ handle_peer_pull_request (void *cls,
 
   else if (2 == mal_type)
   { /* Try to partition network */
-    if (0 == GNUNET_CRYPTO_cmp_peer_identity (&attacked_peer, peer))
+    if (0 == GNUNET_memcmp (&attacked_peer, peer))
     {
       send_pull_reply (peer_ctx, mal_peers, num_mal_peers);
     }
index 737b7ee7fa9d56fec4415b63d603886be1ea4a42..9487c5f2e4be03e8ab10e2e8924eb40ed20a7820 100644 (file)
@@ -111,7 +111,7 @@ RPS_sampler_elem_next (struct RPS_SamplerElement *sampler_elem,
 
   sampler_elem->num_peers++;
 
-  if (0 == GNUNET_CRYPTO_cmp_peer_identity (new_ID, &(sampler_elem->peer_id)))
+  if (0 == GNUNET_memcmp (new_ID, &(sampler_elem->peer_id)))
   {
     LOG (GNUNET_ERROR_TYPE_DEBUG, "Have already PeerID %s\n",
         GNUNET_i2s (&(sampler_elem->peer_id)));
index 3ed4ef989133dba9a480b7bf5628e04586344cea..f54de90142ad6037c41c7091b1872a21078f4844 100644 (file)
@@ -286,7 +286,7 @@ RPS_sampler_reinitialise_by_value (struct RPS_Sampler *sampler,
 
   for (i = 0; i < sampler->sampler_size; i++)
   {
-    if (0 == GNUNET_CRYPTO_cmp_peer_identity(id,
+    if (0 == GNUNET_memcmp(id,
           &(sampler->sampler_elements[i]->peer_id)) )
     {
       LOG (GNUNET_ERROR_TYPE_DEBUG, "Reinitialising sampler\n");
@@ -314,7 +314,7 @@ RPS_sampler_count_id (struct RPS_Sampler *sampler,
   count = 0;
   for ( i = 0 ; i < sampler->sampler_size ; i++ )
   {
-    if ( 0 == GNUNET_CRYPTO_cmp_peer_identity (&sampler->sampler_elements[i]->peer_id, id)
+    if ( 0 == GNUNET_memcmp (&sampler->sampler_elements[i]->peer_id, id)
         && EMPTY != sampler->sampler_elements[i]->is_empty)
       count++;
   }
index cf40522d641bab0d14881fc8b1b2b96a8596482f..339180dffbf91a12cf5dc4ed455a203aa9b18edf 100644 (file)
@@ -11,7 +11,7 @@
      WITHOUT ANY WARRANTY; without even the implied warranty of
      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
      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/>.
 
@@ -762,23 +762,6 @@ GNUNET_CRYPTO_ecdsa_key_get_anonymous ()
 }
 
 
-/**
- * Compare two Peer Identities.
- *
- * @param first first peer identity
- * @param second second peer identity
- * @return bigger than 0 if first > second,
- *         0 if they are the same
- *         smaller than 0 if second > first
- */
-int
-GNUNET_CRYPTO_cmp_peer_identity (const struct GNUNET_PeerIdentity *first,
-                                 const struct GNUNET_PeerIdentity *second)
-{
-  return memcmp (first, second, sizeof (struct GNUNET_PeerIdentity));
-}
-
-
 /**
  * Convert the data specified in the given purpose argument to an
  * S-expression suitable for signature operations.
@@ -795,7 +778,7 @@ data_to_eddsa_value (const struct GNUNET_CRYPTO_EccSignaturePurpose *purpose)
 /* SEE #5398 */
 #if 1
   struct GNUNET_HashCode hc;
-  
+
   GNUNET_CRYPTO_hash (purpose,
                      ntohl (purpose->size),
                      &hc);
@@ -823,7 +806,7 @@ data_to_eddsa_value (const struct GNUNET_CRYPTO_EccSignaturePurpose *purpose)
              rc);
     return NULL;
   }
-#endif    
+#endif
   return data;
 }
 
index b2e3e89cc8f22395537053d74bc35d0dcd242f51..4fd16f93d8386b1624f80e763167c8b1c642ee9b 100644 (file)
@@ -473,18 +473,12 @@ check_ipv6_listed (const struct GNUNET_STRINGS_IPv6NetworkPolicy *list,
 {
   unsigned int i;
   unsigned int j;
-  struct in6_addr zero;
 
   if (NULL == list)
     return GNUNET_NO;
-  memset (&zero,
-         0,
-         sizeof (struct in6_addr));
   i = 0;
 NEXT:
-  while (0 != memcmp (&zero,
-                     &list[i].network,
-                     sizeof (struct in6_addr)))
+  while (0 != GNUNET_is_zero (&list[i].network))
   {
     for (j = 0; j < sizeof (struct in6_addr) / sizeof (int); j++)
       if (((((int *) ip)[j] & ((int *) &list[i].netmask)[j])) !=
index 9fbedb15c4cd57713f36032cb494ef0bb722ed58..248836b4c842f1706fceafb931b5ff6b770062e7 100644 (file)
@@ -11,7 +11,7 @@
      WITHOUT ANY WARRANTY; without even the implied warranty of
      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
      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/>.
 
@@ -38,15 +38,15 @@ static struct GNUNET_PeerIdentity pidArr[NUMBER_OF_PEERS];
 static void
 generatePeerIdList ()
 {
-  int i;
-
-  for (i = 0; i < NUMBER_OF_PEERS; i++)
+  for (unsigned int i = 0; i < NUMBER_OF_PEERS; i++)
   {
     gcry_randomize (&pidArr[i],
                     sizeof (struct GNUNET_PeerIdentity),
                     GCRY_STRONG_RANDOM);
     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
-               "Peer %d: %s\n", i, GNUNET_i2s (&pidArr[i]));
+                "Peer %u: %s\n",
+                i,
+                GNUNET_i2s (&pidArr[i]));
   }
 }
 
@@ -54,15 +54,13 @@ generatePeerIdList ()
 static int
 check ()
 {
-  int i;
   GNUNET_PEER_Id pid;
   struct GNUNET_PeerIdentity res;
-  struct GNUNET_PeerIdentity zero;
   GNUNET_PEER_Id ids[] = { 1, 2, 3 };
 
   GNUNET_assert (0 == GNUNET_PEER_intern (NULL));
   /* Insert Peers into PeerEntry table and hashmap */
-  for (i = 0; i < NUMBER_OF_PEERS; i++)
+  for (unsigned int i = 0; i < NUMBER_OF_PEERS; i++)
   {
     pid = GNUNET_PEER_intern (&pidArr[i]);
     if (pid != (i + 1))
@@ -73,7 +71,7 @@ check ()
   }
 
   /* Referencing the first 3 peers once again */
-  for (i = 0; i < 3; i++)
+  for (unsigned int i = 0; i < 3; i++)
   {
     pid = GNUNET_PEER_intern (&pidArr[i]);
     if (pid != (i + 1))
@@ -87,25 +85,28 @@ check ()
   GNUNET_PEER_decrement_rcs (ids, 3);
 
   /* re-referencing the first 3 peers using the change_rc function */
-  for (i = 1; i <= 3; i++)
+  for (unsigned int i = 1; i <= 3; i++)
     GNUNET_PEER_change_rc (i, 1);
 
   /* Removing the second Peer from the PeerEntry hash map */
   GNUNET_PEER_change_rc (2, -2);
 
   /* convert the pid of the first PeerEntry into that of the third */
-  GNUNET_PEER_resolve (1, &res);
-  GNUNET_assert (0 == memcmp (&res, &pidArr[0], sizeof (res)));
+  GNUNET_PEER_resolve (1,
+                       &res);
+  GNUNET_assert (0 ==
+                 GNUNET_memcmp (&res,
+                                &pidArr[0]));
 
   /*
    * Attempt to convert pid = 0 (which is reserved)
    * into a peer identity object, the peer identity memory
    * is expected to be set to zero
    */
-  memset (&zero, 0, sizeof (struct GNUNET_PeerIdentity));
   GNUNET_log_skip (1, GNUNET_YES);
   GNUNET_PEER_resolve (0, &res);
-  GNUNET_assert (0 == memcmp (&res, &zero, sizeof (res)));
+  GNUNET_assert (0 ==
+                 GNUNET_is_zero (&res));
 
   /* Removing peer entries 1 and 3 from table using the list decrement function */
   /* If count = 0, nothing should be done whatsoever */
@@ -122,10 +123,10 @@ check ()
 int
 main ()
 {
-  unsigned int i;
-
-  GNUNET_log_setup ("test-peer", "ERROR", NULL);
-  for (i = 0; i < 1; i++)
+  GNUNET_log_setup ("test-peer",
+                    "ERROR",
+                    NULL);
+  for (unsigned int i = 0; i < 1; i++)
   {
     generatePeerIdList ();
     if (0 != check ())