From 88edae94076cc39d40a39f80b2d6ddadc88fe324 Mon Sep 17 00:00:00 2001
From: Roger Dingledine <arma@torproject.org>
Date: Thu, 14 Aug 2003 03:52:51 +0000
Subject: [PATCH] start refactoring dnsworker so testing won't be so darn hard
 add NumCpus config variable in preparation for cpuworkers hardcode /etc/torrc
 path for config (simplifies win32 port) improve exit policy debugging during
 router entry parsing

svn:r397
---
 src/or/config.c          | 17 ++++-------------
 src/or/connection_exit.c | 16 ++++++++++------
 src/or/dns.c             | 38 +++++++++++++++++++++++---------------
 src/or/onion.c           |  4 ++--
 src/or/or.h              |  3 +--
 src/or/routers.c         |  5 +++--
 6 files changed, 43 insertions(+), 40 deletions(-)

diff --git a/src/or/config.c b/src/or/config.c
index 6d7a31190d..59113ef862 100644
--- a/src/or/config.c
+++ b/src/or/config.c
@@ -4,16 +4,6 @@
 
 #include "or.h"
 
-const char *basename(const char *filename) {
-  char *result;
-  /* XXX This won't work on windows. */
-  result = strrchr(filename, '/');
-  if (result)
-    return result;
-  else
-    return filename;
-}
-
 /* open configuration file for reading */
 FILE *config_open(const unsigned char *filename) {
   assert(filename);
@@ -189,6 +179,7 @@ void config_assign(or_options_t *options, struct config_line *list) {
     config_compare(list, "MaxOnionsPending",CONFIG_TYPE_INT, &options->MaxOnionsPending) ||
     config_compare(list, "NewCircuitPeriod",CONFIG_TYPE_INT, &options->NewCircuitPeriod) ||
     config_compare(list, "TotalBandwidth",  CONFIG_TYPE_INT, &options->TotalBandwidth) ||
+    config_compare(list, "NumCpus",         CONFIG_TYPE_INT, &options->NumCpus) ||
 
     config_compare(list, "OnionRouter",     CONFIG_TYPE_BOOL, &options->OnionRouter) ||
     config_compare(list, "Daemon",          CONFIG_TYPE_BOOL, &options->Daemon) ||
@@ -214,7 +205,6 @@ int getconfig(int argc, char **argv, or_options_t *options) {
   FILE *cf;
   char fname[256];
   int i;
-  const char *cmd;
   int result = 0;
 
 /* give reasonable values for each option. Defaults to zero. */
@@ -228,11 +218,12 @@ int getconfig(int argc, char **argv, or_options_t *options) {
   options->MaxOnionsPending = 10;
   options->NewCircuitPeriod = 60; /* once a minute */
   options->TotalBandwidth = 800000; /* at most 800kB/s total sustained incoming */
+  options->NumCpus = 1;
 //  options->ReconnectPeriod = 6001;
 
 /* get config lines from /etc/torrc and assign them */
-  cmd = basename(argv[0]);
-  snprintf(fname,256,"/etc/%src",cmd);
+#define rcfile "torrc"
+  snprintf(fname,256,"/etc/%s",rcfile);
 
   cf = config_open(fname);
   if(cf) {
diff --git a/src/or/connection_exit.c b/src/or/connection_exit.c
index f674404028..1daf9ad8f9 100644
--- a/src/or/connection_exit.c
+++ b/src/or/connection_exit.c
@@ -51,13 +51,17 @@ int connection_exit_begin_conn(cell_t *cell, circuit_t *circ) {
   circ->n_streams = n_stream;
 
   /* send it off to the gethostbyname farm */
-  if(dns_resolve(n_stream) < 0) {
-    log_fn(LOG_DEBUG,"Couldn't queue resolve request.");
-    connection_remove(n_stream);
-    connection_free(n_stream);
-    return 0;
+  switch(dns_resolve(n_stream)) {
+    case 1: /* resolve worked */
+      if(connection_exit_connect(n_stream) >= 0)
+        return 0;
+      /* else fall through */
+    case -1: /* resolve failed */
+      log_fn(LOG_DEBUG,"Couldn't queue resolve request.");
+      connection_remove(n_stream);
+      connection_free(n_stream);
+    case 0: /* resolve added to pending list */
   }
-
   return 0;
 }
 
diff --git a/src/or/dns.c b/src/or/dns.c
index 9571475344..704ffc5c28 100644
--- a/src/or/dns.c
+++ b/src/or/dns.c
@@ -19,6 +19,7 @@
 int num_workers=0;
 int num_workers_busy=0;
 
+static void purge_expired_resolves(uint32_t now);
 static int dns_assign_to_worker(connection_t *exitconn);
 static void dns_found_answer(char *question, uint32_t answer);
 int dnsworker_main(void *data);
@@ -65,9 +66,26 @@ void dns_init(void) {
 static struct cached_resolve *oldest_cached_resolve = NULL; /* linked list, */
 static struct cached_resolve *newest_cached_resolve = NULL; /* oldest to newest */
 
+static void purge_expired_resolves(uint32_t now) {
+  struct cached_resolve *resolve;
+
+  /* this is fast because the linked list
+   * oldest_cached_resolve is ordered by when they came in.
+   */
+  while(oldest_cached_resolve && (oldest_cached_resolve->expire < now)) {
+    resolve = oldest_cached_resolve;
+    log(LOG_DEBUG,"Forgetting old cached resolve (expires %d)", resolve->expire);
+    oldest_cached_resolve = resolve->next;
+    if(!oldest_cached_resolve) /* if there are no more, */
+      newest_cached_resolve = NULL; /* then make sure the list's tail knows that too */
+    SPLAY_REMOVE(cache_tree, &cache_root, resolve);
+    free(resolve);
+  }
+}
+
 /* See if the question 'exitconn->address' has been answered. if so,
- * if resolve valid, put it into exitconn->addr and exec to
- * connection_exit_connect. If resolve failed, return -1.
+ * if resolve valid, put it into exitconn->addr and return 1.
+ * If resolve failed, return -1.
  *
  * Else, if seen before and pending, add conn to the pending list,
  * and return 0.
@@ -82,18 +100,8 @@ int dns_resolve(connection_t *exitconn) {
   uint32_t now = time(NULL);
 
   /* first take this opportunity to see if there are any expired
-   * resolves in the tree. this is fast because the linked list
-   * oldest_cached_resolve is ordered by when they came in.
-   */
-  while(oldest_cached_resolve && (oldest_cached_resolve->expire < now)) {
-    resolve = oldest_cached_resolve;
-    log(LOG_DEBUG,"Forgetting old cached resolve (expires %d)", resolve->expire);
-    oldest_cached_resolve = resolve->next;
-    if(!oldest_cached_resolve) /* if there are no more, */
-      newest_cached_resolve = NULL; /* then make sure the list's tail knows that too */
-    SPLAY_REMOVE(cache_tree, &cache_root, resolve);
-    free(resolve);
-  }
+     resolves in the tree.*/
+  purge_expired_resolves(now);
 
   /* now check the tree to see if 'question' is already there. */
   strncpy(search.question, exitconn->address, MAX_ADDRESSLEN);
@@ -109,7 +117,7 @@ int dns_resolve(connection_t *exitconn) {
         return 0;
       case CACHE_STATE_VALID:
         exitconn->addr = resolve->answer;
-        return connection_exit_connect(exitconn);
+        return 1;
       case CACHE_STATE_FAILED:
         return -1;
     }
diff --git a/src/or/onion.c b/src/or/onion.c
index ea457c5b4a..7bab89befd 100644
--- a/src/or/onion.c
+++ b/src/or/onion.c
@@ -226,7 +226,7 @@ unsigned int *new_route(double cw, routerinfo_t **rarray, int rarray_len, int *r
   }
 
   if(num_acceptable_routers < *routelen) {
-    log(LOG_DEBUG,"new_route(): Cutting routelen from %d to %d.",*routelen, num_acceptable_routers);
+    log(LOG_NOTICE,"new_route(): Cutting routelen from %d to %d.",*routelen, num_acceptable_routers);
     *routelen = num_acceptable_routers;
   }
 
@@ -246,7 +246,7 @@ unsigned int *new_route(double cw, routerinfo_t **rarray, int rarray_len, int *r
       return NULL;
     }
 
-    choice = choice % (rarray_len);
+    choice = choice % rarray_len;
     log(LOG_DEBUG,"new_route(): Contemplating router %u.",choice);
     if(choice == oldchoice ||
       (oldchoice < rarray_len && !crypto_pk_cmp_keys(rarray[choice]->pkey, rarray[oldchoice]->pkey)) ||
diff --git a/src/or/or.h b/src/or/or.h
index 6f0165487c..72bdf0bbba 100644
--- a/src/or/or.h
+++ b/src/or/or.h
@@ -456,6 +456,7 @@ typedef struct {
    int MaxOnionsPending;
    int NewCircuitPeriod;
    int TotalBandwidth;
+   int NumCpus;
    int Role;
    int loglevel;
 } or_options_t;
@@ -558,8 +559,6 @@ void command_process_connected_cell(cell_t *cell, connection_t *conn);
 
 /********************************* config.c ***************************/
 
-const char *basename(const char *filename);
-
 /* open configuration file for reading */
 FILE *config_open(const unsigned char *filename);
 
diff --git a/src/or/routers.c b/src/or/routers.c
index e9cee1ef7c..4ed858732c 100644
--- a/src/or/routers.c
+++ b/src/or/routers.c
@@ -842,8 +842,9 @@ static int router_add_exit_policy(routerinfo_t *router,
   newe->address = strdup(arg);
   newe->port = strdup(colon+1);
 
-  log(LOG_DEBUG,"router_add_exit_policy(): type %d, address '%s', port '%s'.",
-      newe->policy_type, newe->address, newe->port);
+  log(LOG_DEBUG,"router_add_exit_policy(): %s %s:%s",
+      newe->policy_type == EXIT_POLICY_REJECT ? "reject" : "accept",
+      newe->address, newe->port);
 
   /* now link newe onto the end of exit_policy */
 
-- 
GitLab