Browse Source

introduce GNUNET_memcmp() and GNUNET_is_zero()

Christian Grothoff 5 years ago
parent
commit
c57caa206c

+ 4 - 0
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

+ 3 - 3
src/cadet/gnunet-service-cadet_tunnels.c

@@ -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;

+ 1 - 1
src/fs/gnunet-service-fs.c

@@ -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,

+ 1 - 1
src/fs/gnunet-service-fs_cp.c

@@ -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,

+ 1 - 1
src/include/gnunet_cadet_service.h

@@ -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;
 };

+ 26 - 0
src/include/gnunet_common.h

@@ -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.

+ 1 - 15
src/include/gnunet_crypto_lib.h

@@ -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);
 
 
 /**

+ 3 - 3
src/rps/gnunet-service-rps.c

@@ -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);
     }

+ 1 - 1
src/rps/gnunet-service-rps_sampler_elem.c

@@ -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)));

+ 2 - 2
src/rps/rps-sampler_common.c

@@ -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++;
   }

+ 3 - 20
src/util/crypto_ecc.c

@@ -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;
 }
 

+ 1 - 7
src/util/service.c

@@ -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])) !=

+ 19 - 18
src/util/test_peer.c

@@ -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 ())