From e95ae1c9ad38b831c62b21a95c323491c1828f96 Mon Sep 17 00:00:00 2001
From: Roger Dingledine <arma@torproject.org>
Date: Fri, 6 Aug 2004 22:15:25 +0000
Subject: [PATCH] let purging routerinfos and descriptors take an age argument

svn:r2171
---
 src/or/dirserv.c    | 10 +++++-----
 src/or/main.c       |  5 +++--
 src/or/or.h         |  4 ++--
 src/or/routerlist.c |  9 +++++----
 4 files changed, 15 insertions(+), 13 deletions(-)

diff --git a/src/or/dirserv.c b/src/or/dirserv.c
index 5aab933877..57aacced67 100644
--- a/src/or/dirserv.c
+++ b/src/or/dirserv.c
@@ -510,11 +510,11 @@ list_running_servers(char **nicknames_out)
   return 0;
 }
 
-/** Remove any descriptors from the directory that are more than ROUTER_MAX_AGE
+/** Remove any descriptors from the directory that are more than <b>age</b>
  * seconds old.
  */
 void
-dirserv_remove_old_servers(void)
+dirserv_remove_old_servers(int age)
 {
   int i;
   time_t cutoff;
@@ -522,10 +522,10 @@ dirserv_remove_old_servers(void)
   if (!descriptor_list)
     descriptor_list = smartlist_create();
 
-  cutoff = time(NULL) - ROUTER_MAX_AGE;
+  cutoff = time(NULL) - age;
   for (i = 0; i < smartlist_len(descriptor_list); ++i) {
     ent = smartlist_get(descriptor_list, i);
-    if (ent->published < cutoff) {
+    if (ent->published <= cutoff) {
       /* descriptor_list[i] is too old.  Remove it. */
       free_descriptor_entry(ent);
       smartlist_del(descriptor_list, i--);
@@ -556,7 +556,7 @@ dirserv_dump_directory_to_string(char *s, unsigned int maxlen,
 
   if (list_running_servers(&cp))
     return -1;
-  dirserv_remove_old_servers();
+  dirserv_remove_old_servers(ROUTER_MAX_AGE);
   published_on = time(NULL);
   strftime(published, 32, "%Y-%m-%d %H:%M:%S", gmtime(&published_on));
   snprintf(s, maxlen,
diff --git a/src/or/main.c b/src/or/main.c
index d4e6c6a47e..2ebd02a914 100644
--- a/src/or/main.c
+++ b/src/or/main.c
@@ -544,11 +544,12 @@ static void run_scheduled_events(time_t now) {
       server_is_advertised = 0;
     }
 
-    routerlist_remove_old_routers(); /* purge obsolete entries */
+    /* purge obsolete entries */
+    routerlist_remove_old_routers(ROUTER_MAX_AGE);
 
     if(authdir_mode()) {
       /* We're a directory; dump any old descriptors. */
-      dirserv_remove_old_servers();
+      dirserv_remove_old_servers(ROUTER_MAX_AGE);
     }
     if(server_mode()) {
       /* dirservers try to reconnect, in case connections have failed;
diff --git a/src/or/or.h b/src/or/or.h
index 167b6d86a3..df69cc28fb 100644
--- a/src/or/or.h
+++ b/src/or/or.h
@@ -1160,7 +1160,7 @@ void dirserv_free_fingerprint_list();
 int dirserv_add_descriptor(const char **desc);
 int dirserv_load_from_directory_string(const char *dir);
 void dirserv_free_descriptors();
-void dirserv_remove_old_servers(void);
+void dirserv_remove_old_servers(int age);
 int dirserv_dump_directory_to_string(char *s, unsigned int maxlen,
                                      crypto_pk_env_t *private_key);
 void directory_set_dirty(void);
@@ -1385,7 +1385,7 @@ void routerlist_clear_trusted_directories(void);
 void routerinfo_free(routerinfo_t *router);
 routerinfo_t *routerinfo_copy(const routerinfo_t *router);
 void router_mark_as_down(const char *digest);
-void routerlist_remove_old_routers(void);
+void routerlist_remove_old_routers(int age);
 int router_load_routerlist_from_file(char *routerfile, int trusted);
 int router_load_routerlist_from_string(const char *s, int trusted);
 int router_load_routerlist_from_directory(const char *s,crypto_pk_env_t *pkey);
diff --git a/src/or/routerlist.c b/src/or/routerlist.c
index e67502aa7f..79dabc4902 100644
--- a/src/or/routerlist.c
+++ b/src/or/routerlist.c
@@ -488,14 +488,14 @@ int router_add_to_routerlist(routerinfo_t *router) {
   return 0;
 }
 
-/** Remove any routers from the routerlist that are more than ROUTER_MAX_AGE
+/** Remove any routers from the routerlist that are more than <b>age</b>
  * seconds old.
  *
  * (This function is just like dirserv_remove_old_servers. One day we should
  * merge them.)
  */
 void
-routerlist_remove_old_routers(void)
+routerlist_remove_old_routers(int age)
 {
   int i;
   time_t cutoff;
@@ -503,10 +503,11 @@ routerlist_remove_old_routers(void)
   if (!routerlist)
     return;
 
-  cutoff = time(NULL) - ROUTER_MAX_AGE;
+  cutoff = time(NULL) - age;
   for (i = 0; i < smartlist_len(routerlist->routers); ++i) {
     router = smartlist_get(routerlist->routers, i);
-    if (router->published_on < cutoff &&
+    if (router->published_on <= cutoff &&
+/* XXX008 don't get fooled by cached dir ports */
       !router->dir_port) {
       /* Too old.  Remove it. But never remove dirservers! */
       log_fn(LOG_INFO,"Forgetting obsolete routerinfo for node %s.", router->nickname);
-- 
GitLab