Merge branch 'master' of ssh://gnunet.org/gnunet
[oweals/gnunet.git] / src / dht / gnunet-service-dht_neighbours.c
index 94844374de23a7c95485cfcbff1e2eeecf792ed6..56639448f5ecc702d50384a0bbf1187b9785a133 100644 (file)
@@ -2,20 +2,18 @@
      This file is part of GNUnet.
      Copyright (C) 2009-2017 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.
-
-     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.
+     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/>.
 */
 
 /**
@@ -470,6 +468,7 @@ free_connect_info (void *cls,
 {
   struct ConnectInfo *ci = value;
 
+ (void) cls;
   GNUNET_assert (GNUNET_YES ==
                  GNUNET_CONTAINER_multipeermap_remove (all_desired_peers,
                                                        peer,
@@ -581,7 +580,10 @@ update_desire_strength (void *cls,
                         const struct GNUNET_PeerIdentity *pid,
                         void *value)
 {
-  try_connect (pid, NULL);
+  (void) cls;
+  (void) value;
+  try_connect (pid,
+              NULL);
   return GNUNET_YES;
 }
 
@@ -618,6 +620,8 @@ add_known_to_bloom (void *cls,
   struct GNUNET_BLOCK_Group *bg = cls;
   struct GNUNET_HashCode key_hash;
 
+ (void) cls;
+ (void) value;
   GNUNET_CRYPTO_hash (key,
                       sizeof (struct GNUNET_PeerIdentity),
                       &key_hash);
@@ -645,7 +649,8 @@ send_find_peer_message (void *cls)
   struct GNUNET_BLOCK_Group *bg;
   struct GNUNET_CONTAINER_BloomFilter *peer_bf;
 
-  find_peer_task = NULL;
+ (void) cls;
+ find_peer_task = NULL;
   if (newly_found_peers > bucket_size)
   {
     /* If we are finding many peers already, no need to send out our request right now! */
@@ -718,7 +723,8 @@ handle_core_connect (void *cls,
 {
   struct PeerInfo *pi;
 
-  /* Check for connect to self message */
+ (void) cls;
+ /* Check for connect to self message */
   if (0 == memcmp (&my_identity,
                   peer,
                   sizeof (struct GNUNET_PeerIdentity)))
@@ -741,13 +747,13 @@ handle_core_connect (void *cls,
                      &pi->phash);
   pi->peer_bucket = find_bucket (&pi->phash);
   GNUNET_assert ( (pi->peer_bucket >= 0) &&
-                  (pi->peer_bucket < MAX_BUCKETS) );
+                  ((unsigned int) pi->peer_bucket < MAX_BUCKETS) );
   GNUNET_CONTAINER_DLL_insert_tail (k_buckets[pi->peer_bucket].head,
                                     k_buckets[pi->peer_bucket].tail,
                                     pi);
   k_buckets[pi->peer_bucket].peers_size++;
   closest_bucket = GNUNET_MAX (closest_bucket,
-                               pi->peer_bucket);
+                               (unsigned int) pi->peer_bucket);
   GNUNET_assert (GNUNET_OK ==
                  GNUNET_CONTAINER_multipeermap_put (all_connected_peers,
                                                     pi->id,
@@ -785,6 +791,7 @@ handle_core_disconnect (void *cls,
 {
   struct PeerInfo *to_remove = internal_cls;
 
+  (void) cls;
   /* Check for disconnect from self message */
   if (NULL == to_remove)
     return;
@@ -948,23 +955,23 @@ GDS_am_closest_peer (const struct GNUNET_HashCode *key,
   int bits;
   int other_bits;
   int bucket_num;
-  int count;
   struct PeerInfo *pos;
 
-  if (0 == memcmp (&my_identity_hash, key, sizeof (struct GNUNET_HashCode)))
+  if (0 == memcmp (&my_identity_hash,
+                  key,
+                  sizeof (struct GNUNET_HashCode)))
     return GNUNET_YES;
   bucket_num = find_bucket (key);
   GNUNET_assert (bucket_num >= 0);
   bits = GNUNET_CRYPTO_hash_matching_bits (&my_identity_hash,
                                            key);
   pos = k_buckets[bucket_num].head;
-  count = 0;
-  while ((NULL != pos) && (count < bucket_size))
+  while (NULL != pos) 
   {
-    if ((NULL != bloom) &&
-        (GNUNET_YES ==
-         GNUNET_CONTAINER_bloomfilter_test (bloom,
-                                            &pos->phash)))
+    if ( (NULL != bloom) &&
+        (GNUNET_YES ==
+         GNUNET_CONTAINER_bloomfilter_test (bloom,
+                                            &pos->phash)) )
     {
       pos = pos->next;
       continue;                 /* Skip already checked entries */
@@ -1596,6 +1603,7 @@ static void
 core_init (void *cls,
            const struct GNUNET_PeerIdentity *identity)
 {
+  (void) cls;
   GNUNET_log (GNUNET_ERROR_TYPE_INFO,
              "CORE called, I am %s\n",
               GNUNET_i2s (identity));
@@ -1621,6 +1629,7 @@ check_dht_p2p_put (void *cls,
   uint32_t putlen;
   uint16_t msize;
 
+  (void) cls;
   msize = ntohs (put->header.size);
   putlen = ntohl (put->put_path_length);
   if ((msize <
@@ -1656,7 +1665,17 @@ handle_dht_p2p_put (void *cls,
   struct GNUNET_CONTAINER_BloomFilter *bf;
   struct GNUNET_HashCode test_key;
   int forwarded;
+  struct GNUNET_TIME_Absolute exp_time;
 
+  exp_time = GNUNET_TIME_absolute_ntoh (put->expiration_time);
+  if (0 == GNUNET_TIME_absolute_get_remaining (exp_time).rel_value_us)
+  {
+    GNUNET_STATISTICS_update (GDS_stats,
+                             gettext_noop ("# Expired PUTs discarded"),
+                             1,
+                             GNUNET_NO);
+    return;
+  }
   msize = ntohs (put->header.size);
   putlen = ntohl (put->put_path_length);
   GNUNET_STATISTICS_update (GDS_stats,
@@ -1792,7 +1811,7 @@ handle_dht_p2p_put (void *cls,
       putlen = 0;
 
     /* give to local clients */
-    GDS_CLIENTS_handle_reply (GNUNET_TIME_absolute_ntoh (put->expiration_time),
+    GDS_CLIENTS_handle_reply (exp_time,
                               &put->key,
                              0,
                              NULL,
@@ -1804,7 +1823,7 @@ handle_dht_p2p_put (void *cls,
     /* store locally */
     if ((0 != (options & GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE)) ||
         (GDS_am_closest_peer (&put->key, bf)))
-      GDS_DATACACHE_handle_put (GNUNET_TIME_absolute_ntoh (put->expiration_time),
+      GDS_DATACACHE_handle_put (exp_time,
                                 &put->key,
                                 putlen,
                                 pp,
@@ -1815,7 +1834,7 @@ handle_dht_p2p_put (void *cls,
     forwarded = GDS_NEIGHBOURS_handle_put (ntohl (put->type),
                                           options,
                                            ntohl (put->desired_replication_level),
-                                           GNUNET_TIME_absolute_ntoh (put->expiration_time),
+                                           exp_time,
                                            ntohl (put->hop_count),
                                           bf,
                                            &put->key,
@@ -1832,7 +1851,7 @@ handle_dht_p2p_put (void *cls,
                              ntohl (put->hop_count),
                              ntohl (put->desired_replication_level),
                              putlen, pp,
-                             GNUNET_TIME_absolute_ntoh (put->expiration_time),
+                             exp_time,
                              &put->key,
                              payload,
                              payload_size);
@@ -1909,9 +1928,9 @@ handle_find_peer (const struct GNUNET_PeerIdentity *sender,
                   sizeof (struct GNUNET_HashCode)))
     bucket_idx = closest_bucket;
   else
-    bucket_idx = GNUNET_MIN (closest_bucket,
+    bucket_idx = GNUNET_MIN ((int) closest_bucket,
                             find_bucket (key));
-  if (bucket_idx == GNUNET_SYSERR)
+  if (bucket_idx < 0)
     return;
   bucket = &k_buckets[bucket_idx];
   if (bucket->peers_size == 0)
@@ -2018,6 +2037,7 @@ check_dht_p2p_get (void *cls,
   uint32_t xquery_size;
   uint16_t msize;
 
+  (void) cls;
   msize = ntohs (get->header.size);
   xquery_size = ntohl (get->xquery_size);
   if (msize < sizeof (struct PeerGetMessage) + xquery_size)
@@ -2205,6 +2225,7 @@ check_dht_p2p_result (void *cls,
   uint32_t put_path_length;
   uint16_t msize;
 
+  (void) cls;
   msize = ntohs (prm->header.size);
   put_path_length = ntohl (prm->put_path_length);
   get_path_length = ntohl (prm->get_path_length);
@@ -2318,8 +2339,18 @@ handle_dht_p2p_result (void *cls,
   uint16_t msize;
   size_t data_size;
   enum GNUNET_BLOCK_Type type;
+  struct GNUNET_TIME_Absolute exp_time;
 
   /* parse and validate message */
+  exp_time = GNUNET_TIME_absolute_ntoh (prm->expiration_time);
+  if (0 == GNUNET_TIME_absolute_get_remaining (exp_time).rel_value_us)
+  {
+    GNUNET_STATISTICS_update (GDS_stats,
+                             gettext_noop ("# Expired results discarded"),
+                             1,
+                             GNUNET_NO);
+    return;
+  }
   msize = ntohs (prm->header.size);
   put_path_length = ntohl (prm->put_path_length);
   get_path_length = ntohl (prm->get_path_length);
@@ -2393,7 +2424,6 @@ handle_dht_p2p_result (void *cls,
                    h);
   }
 
-
   /* First, check if 'peer' is already on the path, and if
      so, truncate it instead of expanding. */
   for (unsigned int i=0;i<=get_path_length;i++)
@@ -2401,7 +2431,7 @@ handle_dht_p2p_result (void *cls,
                      peer->id,
                      sizeof (struct GNUNET_PeerIdentity)))
     {
-      process_reply_with_path (GNUNET_TIME_absolute_ntoh (prm->expiration_time),
+      process_reply_with_path (exp_time,
                                &prm->key,
                                i,
                                get_path,
@@ -2422,7 +2452,7 @@ handle_dht_p2p_result (void *cls,
                   get_path_length * sizeof (struct GNUNET_PeerIdentity));
     xget_path[get_path_length] = *peer->id;
 
-    process_reply_with_path (GNUNET_TIME_absolute_ntoh (prm->expiration_time),
+    process_reply_with_path (exp_time,
                              &prm->key,
                              get_path_length + 1,
                              xget_path,