[or-cvs] Check if our IP address has changed every 5 minutes. If it...

Peter Palfrader weasel at seul.org
Wed Oct 12 22:41:18 UTC 2005


Update of /home/or/cvsroot/tor/src/or
In directory moria:/tmp/cvs-serv6602/src/or

Modified Files:
	main.c or.h router.c 
Log Message:
Check if our IP address has changed every 5 minutes.  If it has, update our server descriptor, but not too often

Index: main.c
===================================================================
RCS file: /home/or/cvsroot/tor/src/or/main.c,v
retrieving revision 1.575
retrieving revision 1.576
diff -u -d -r1.575 -r1.576
--- main.c	6 Oct 2005 22:22:22 -0000	1.575
+++ main.c	12 Oct 2005 22:41:16 -0000	1.576
@@ -94,6 +94,7 @@
 
 #define FORCE_REGENERATE_DESCRIPTOR_INTERVAL 18*60*60 /* 18 hours */
 #define CHECK_DESCRIPTOR_INTERVAL 60 /* one minute */
+#define CHECK_IPADDRESS_INTERVAL (5*60) /* five minutes */
 #define BUF_SHRINK_INTERVAL 60 /* one minute */
 #define DESCRIPTOR_RETRY_INTERVAL 10
 #define DESCRIPTOR_FAILURE_RESET_INTERVAL 60*60
@@ -637,6 +638,7 @@
   static time_t last_rotated_certificate = 0;
   static time_t time_to_check_listeners = 0;
   static time_t time_to_check_descriptor = 0;
+  static time_t time_to_check_ipaddress = 0;
   static time_t time_to_shrink_buffers = 0;
   static time_t time_to_try_getting_descriptors = 0;
   static time_t time_to_reset_descriptor_failures = 0;
@@ -744,6 +746,10 @@
   if (time_to_check_descriptor < now) {
     time_to_check_descriptor = now + CHECK_DESCRIPTOR_INTERVAL;
     check_descriptor_bandwidth_changed(now);
+    if (time_to_check_ipaddress < now) {
+      time_to_check_ipaddress = now + CHECK_IPADDRESS_INTERVAL;
+      check_descriptor_ipaddress_changed(now);
+    }
     mark_my_descriptor_dirty_if_older_than(
                                     now - FORCE_REGENERATE_DESCRIPTOR_INTERVAL);
     consider_publishable_server(now, 0);

Index: or.h
===================================================================
RCS file: /home/or/cvsroot/tor/src/or/or.h,v
retrieving revision 1.712
retrieving revision 1.713
diff -u -d -r1.712 -r1.713
--- or.h	12 Oct 2005 13:49:13 -0000	1.712
+++ or.h	12 Oct 2005 22:41:16 -0000	1.713
@@ -2058,6 +2058,7 @@
 void mark_my_descriptor_dirty_if_older_than(time_t when);
 void mark_my_descriptor_dirty(void);
 void check_descriptor_bandwidth_changed(time_t now);
+void check_descriptor_ipaddress_changed(time_t now);
 int router_compare_to_my_exit_policy(connection_t *conn);
 routerinfo_t *router_get_my_routerinfo(void);
 const char *router_get_my_descriptor(void);

Index: router.c
===================================================================
RCS file: /home/or/cvsroot/tor/src/or/router.c,v
retrieving revision 1.219
retrieving revision 1.220
diff -u -d -r1.219 -r1.220
--- router.c	12 Oct 2005 04:07:10 -0000	1.219
+++ router.c	12 Oct 2005 22:41:16 -0000	1.220
@@ -909,6 +909,55 @@
   }
 }
 
+#define MAX_IPADDRESS_CHANGE_FREQ 60*60
+/** Check whether our own address as defined by the Address configuration
+ * has changed.  This is for routers that get their address from a service
+ * like dyndns.  If our address has changed, mark our descriptor dirty.*/
+void
+check_descriptor_ipaddress_changed(time_t now)
+{
+  static time_t last_changed = 0;
+  static time_t last_warned_lastchangetime = 0;
+  uint32_t prev, cur;
+  or_options_t *options = get_options();
+
+  if (!desc_routerinfo)
+    return;
+
+  prev = desc_routerinfo->addr;
+  if (resolve_my_address(options, &cur, NULL) < 0) {
+    log_fn(LOG_WARN,"options->Address didn't resolve into an IP.");
+    return;
+  }
+
+  if (prev != cur) {
+    char addrbuf_prev[INET_NTOA_BUF_LEN];
+    char addrbuf_cur[INET_NTOA_BUF_LEN];
+    struct in_addr in_prev;
+    struct in_addr in_cur;
+
+    in_prev.s_addr = htonl(prev);
+    tor_inet_ntoa(&in_prev, addrbuf_prev, sizeof(addrbuf_prev));
+
+    in_cur.s_addr = htonl(cur);
+    tor_inet_ntoa(&in_cur, addrbuf_cur, sizeof(addrbuf_cur));
+
+    if (last_changed+MAX_IPADDRESS_CHANGE_FREQ < now) {
+      log_fn(LOG_INFO,"Our IP Address has changed from %s to %s; rebuilding descriptor.", addrbuf_prev, addrbuf_cur);
+      mark_my_descriptor_dirty();
+      last_changed = now;
+      last_warned_lastchangetime = 0;
+    }
+    else
+    {
+      if (last_warned_lastchangetime != last_changed) {
+        log_fn(LOG_WARN,"Our IP Address seems to be flapping.  It has changed twice within one hour (from %s to %s this time).  Ignoring for now.", addrbuf_prev, addrbuf_cur);
+        last_warned_lastchangetime = last_changed;
+      }
+    }
+  }
+}
+
 /** Set <b>platform</b> (max length <b>len</b>) to a NUL-terminated short
  * string describing the version of Tor and the operating system we're
  * currently running on.



More information about the tor-commits mailing list