[or-cvs] greatly simplify this notion of "roles":

Roger Dingledine arma at seul.org
Tue Mar 18 01:49:58 UTC 2003


Update of /home/or/cvsroot/src/or
In directory moria.mit.edu:/home/arma/work/onion/cvs/src/or

Modified Files:
	config.c connection.c connection_ap.c directory.c main.c 
	onion.c or.h routers.c 
Log Message:
greatly simplify this notion of 'roles':
if your ORPort is non-zero then you must connect to all nodes
if your DirPort is non-zero then you must act like a directory server


Index: config.c
===================================================================
RCS file: /home/or/cvsroot/src/or/config.c,v
retrieving revision 1.26
retrieving revision 1.27
diff -u -d -r1.26 -r1.27
--- config.c	17 Mar 2003 02:27:19 -0000	1.26
+++ config.c	18 Mar 2003 01:49:55 -0000	1.27
@@ -149,8 +149,8 @@
     case CONFIG_TYPE_BOOL:
       i = atoi(c->value);
       if (i != 0 && i != 1) {
-	log(LOG_ERR, "Boolean keyword '%s' expects 0 or 1", c->key);
-	return 0;
+        log(LOG_ERR, "Boolean keyword '%s' expects 0 or 1", c->key);
+        return 0;
       }
       *(int *)arg = i;
       break;
@@ -179,13 +179,11 @@
     config_compare(list, "RouterFile",     CONFIG_TYPE_STRING, &options->RouterFile) ||
 
     /* int options */
-    config_compare(list, "Role",            CONFIG_TYPE_INT, &options->Role) ||
     config_compare(list, "MaxConn",         CONFIG_TYPE_INT, &options->MaxConn) ||
     config_compare(list, "APPort",          CONFIG_TYPE_INT, &options->APPort) ||
     config_compare(list, "OPPort",          CONFIG_TYPE_INT, &options->OPPort) ||
     config_compare(list, "ORPort",          CONFIG_TYPE_INT, &options->ORPort) ||
     config_compare(list, "DirPort",         CONFIG_TYPE_INT, &options->DirPort) ||
-    config_compare(list, "DirRebuildPeriod",CONFIG_TYPE_INT, &options->DirRebuildPeriod) ||
     config_compare(list, "DirFetchPeriod",  CONFIG_TYPE_INT, &options->DirFetchPeriod) ||
     config_compare(list, "KeepalivePeriod", CONFIG_TYPE_INT, &options->KeepalivePeriod) ||
     config_compare(list, "MaxOnionsPending",CONFIG_TYPE_INT, &options->MaxOnionsPending) ||
@@ -223,12 +221,10 @@
   options->loglevel = LOG_DEBUG;
   options->CoinWeight = 0.8;
   options->LinkPadding = 0;
-  options->DirRebuildPeriod = 300;
   options->DirFetchPeriod = 600;
   options->KeepalivePeriod = 300;
   options->MaxOnionsPending = 10;
 //  options->ReconnectPeriod = 6001;
-  options->Role = ROLE_OR_LISTEN | ROLE_OR_CONNECT_ALL | ROLE_OP_LISTEN | ROLE_AP_LISTEN;
 
 /* get config lines from /etc/torrc and assign them */
   cmd = basename(argv[0]);
@@ -270,9 +266,8 @@
 
 /* print config */
   if (options->loglevel == LOG_DEBUG) {
-    printf("LogLevel=%s, Role=%d\n",
-           options->LogLevel,
-           options->Role);
+    printf("LogLevel=%s\n",
+           options->LogLevel);
     printf("RouterFile=%s, PrivateKeyFile=%s\n",
            options->RouterFile ? options->RouterFile : "(undefined)",
            options->PrivateKeyFile ? options->PrivateKeyFile : "(undefined)");
@@ -284,8 +279,7 @@
            options->MaxConn,
            options->TrafficShaping,
            options->LinkPadding);
-    printf("DirRebuildPeriod=%d, DirFetchPeriod=%d KeepalivePeriod=%d\n",
-           options->DirRebuildPeriod,
+    printf("DirFetchPeriod=%d KeepalivePeriod=%d\n",
            options->DirFetchPeriod,
            options->KeepalivePeriod);
     printf("Daemon=%d", options->Daemon);
@@ -316,74 +310,49 @@
     }
   }
 
-  if(options->Role < 0 || options->Role > 63) {
-    log(LOG_ERR,"Role option must be an integer between 0 and 63 (inclusive).");
-    result = -1;
-  }
-
   if(options->RouterFile == NULL) {
     log(LOG_ERR,"RouterFile option required, but not found.");
     result = -1;
   }
 
-  if(ROLE_IS_OR(options->Role) && options->PrivateKeyFile == NULL) {
-    log(LOG_ERR,"PrivateKeyFile option required for OR, but not found.");
+  if(options->ORPort < 0) {
+    log(LOG_ERR,"ORPort option required and must be a positive integer value.");
     result = -1;
   }
 
-  if((options->Role & ROLE_OR_LISTEN) && options->ORPort < 1) {
-    log(LOG_ERR,"ORPort option required and must be a positive integer value.");
+  if(options->ORPort > 0 && options->PrivateKeyFile == NULL) {
+    log(LOG_ERR,"PrivateKeyFile option required for OR, but not found.");
     result = -1;
   }
 
-  if((options->Role & ROLE_OP_LISTEN) && options->OPPort < 1) {
-    log(LOG_ERR,"OPPort option required and must be a positive integer value.");
+  if(options->OPPort < 0) {
+    log(LOG_ERR,"OPPort option can't be negative.");
     result = -1;
   }
 
-  if((options->Role & ROLE_AP_LISTEN) && options->APPort < 1) {
-    log(LOG_ERR,"APPort option required and must be a positive integer value.");
+  if(options->APPort < 0) {
+    log(LOG_ERR,"APPort option can't be negative.");
     result = -1;
   }
 
-  if((options->Role & ROLE_DIR_LISTEN) && options->DirPort < 1) {
-    log(LOG_ERR,"DirPort option required and must be a positive integer value.");
+  if(options->DirPort < 0) {
+    log(LOG_ERR,"DirPort option can't be negative.");
     result = -1;
   }
 
-  if((options->Role & ROLE_AP_LISTEN) &&
+  if(options->APPort > 1 &&
      (options->CoinWeight < 0.0 || options->CoinWeight >= 1.0)) {
-    log(LOG_ERR,"CoinWeight option must be a value from 0.0 upto 1.0, but not including 1.0.");
+    log(LOG_ERR,"CoinWeight option must be >=0.0 and <1.0.");
     result = -1;
   }
 
-  if(options->MaxConn <= 0) {
+  if(options->MaxConn < 1) {
     log(LOG_ERR,"MaxConn option must be a non-zero positive integer.");
     result = -1;
   }
 
   if(options->MaxConn >= MAXCONNECTIONS) {
     log(LOG_ERR,"MaxConn option must be less than %d.", MAXCONNECTIONS);
-    result = -1;
-  }
-
-  if(options->Daemon != 0 && options->Daemon != 1) {
-    log(LOG_ERR,"TrafficShaping option must be either 0 or 1.");
-    result = -1;
-  }
-
-  if(options->TrafficShaping != 0 && options->TrafficShaping != 1) {
-    log(LOG_ERR,"TrafficShaping option must be either 0 or 1.");
-    result = -1;
-  }
-
-  if(options->LinkPadding != 0 && options->LinkPadding != 1) {
-    log(LOG_ERR,"LinkPadding option must be either 0 or 1.");
-    result = -1;
-  }
-
-  if(options->DirRebuildPeriod < 1) {
-    log(LOG_ERR,"DirRebuildPeriod option must be positive.");
     result = -1;
   }
 

Index: connection.c
===================================================================
RCS file: /home/or/cvsroot/src/or/connection.c,v
retrieving revision 1.43
retrieving revision 1.44
diff -u -d -r1.43 -r1.44
--- connection.c	17 Mar 2003 21:21:35 -0000	1.43
+++ connection.c	18 Mar 2003 01:49:55 -0000	1.44
@@ -286,14 +286,14 @@
   return 0;
 }
 
-int retry_all_connections(int role, uint16_t or_listenport,
+int retry_all_connections(uint16_t or_listenport,
   uint16_t op_listenport, uint16_t ap_listenport, uint16_t dir_listenport) {
 
   /* start all connections that should be up but aren't */
 
   struct sockaddr_in bindaddr; /* where to bind */
 
-  if(role & ROLE_OR_CONNECT_ALL) {
+  if(or_listenport) {
     router_retry_connections();
   }
 
@@ -301,28 +301,28 @@
   bindaddr.sin_family = AF_INET;
   bindaddr.sin_addr.s_addr = htonl(INADDR_ANY); /* anyone can connect */
 
-  if(role & ROLE_OR_LISTEN) {
+  if(or_listenport) {
     bindaddr.sin_port = htons(or_listenport);
     if(!connection_get_by_type(CONN_TYPE_OR_LISTENER)) {
       connection_or_create_listener(&bindaddr);
     }
   }
 
-  if(role & ROLE_OP_LISTEN) {
+  if(op_listenport) {
     bindaddr.sin_port = htons(op_listenport);
     if(!connection_get_by_type(CONN_TYPE_OP_LISTENER)) {
       connection_op_create_listener(&bindaddr);
     }
   }
 
-  if(role & ROLE_DIR_LISTEN) {
+  if(dir_listenport) {
     bindaddr.sin_port = htons(dir_listenport);
     if(!connection_get_by_type(CONN_TYPE_DIR_LISTENER)) {
       connection_dir_create_listener(&bindaddr);
     }
   }
  
-  if(role & ROLE_AP_LISTEN) {
+  if(ap_listenport) {
     bindaddr.sin_port = htons(ap_listenport);
     inet_aton("127.0.0.1", &(bindaddr.sin_addr)); /* the AP listens only on localhost! */
     if(!connection_get_by_type(CONN_TYPE_AP_LISTENER)) {

Index: connection_ap.c
===================================================================
RCS file: /home/or/cvsroot/src/or/connection_ap.c,v
retrieving revision 1.27
retrieving revision 1.28
diff -u -d -r1.27 -r1.28
--- connection_ap.c	17 Mar 2003 02:42:45 -0000	1.27
+++ connection_ap.c	18 Mar 2003 01:49:55 -0000	1.28
@@ -4,7 +4,7 @@
 
 #include "or.h"
 
-extern int global_role; /* from main.c */
+extern or_options_t options; /* command-line and config-file options */
 
 int connection_ap_process_inbuf(connection_t *conn) {
 
@@ -221,7 +221,7 @@
   if(!n_conn || n_conn->state != OR_CONN_STATE_OPEN) { /* not currently connected */
     circ->n_addr = firsthop->addr;
     circ->n_port = firsthop->or_port;
-    if(global_role & ROLE_OR_CONNECT_ALL) { /* we would be connected if he were up. but he's not. */
+    if(options.ORPort) { /* we would be connected if he were up. but he's not. */
       log(LOG_DEBUG,"ap_handshake_establish_circuit(): Route's firsthop isn't connected.");
       circuit_close(circ); 
       return -1;

Index: directory.c
===================================================================
RCS file: /home/or/cvsroot/src/or/directory.c,v
retrieving revision 1.7
retrieving revision 1.8
diff -u -d -r1.7 -r1.8
--- directory.c	11 Mar 2003 01:51:41 -0000	1.7
+++ directory.c	18 Mar 2003 01:49:55 -0000	1.8
@@ -144,7 +144,7 @@
     if(router_get_list_from_string(the_directory) < 0) {
       log(LOG_DEBUG,"connection_dir_process_inbuf(): ...but parsing failed. Ignoring.");
     }
-    if(options.Role & ROLE_OR_CONNECT_ALL) { /* connect to them all */
+    if(options.ORPort) { /* connect to them all */
       router_retry_connections();
     }
     return -1;

Index: main.c
===================================================================
RCS file: /home/or/cvsroot/src/or/main.c,v
retrieving revision 1.44
retrieving revision 1.45
diff -u -d -r1.44 -r1.45
--- main.c	17 Mar 2003 02:41:36 -0000	1.44
+++ main.c	18 Mar 2003 01:49:55 -0000	1.45
@@ -7,7 +7,6 @@
 /********* START VARIABLES **********/
 
 or_options_t options; /* command-line and config-file options */
-int global_role;
 
 static connection_t *connection_array[MAXCONNECTIONS] =
         { NULL };
@@ -311,7 +310,7 @@
 
   if(now.tv_sec > current_second) { /* the second has rolled over. check more stuff. */
 
-    if(!(options.Role & ROLE_DIR_SERVER)) {
+    if(!options.DirPort) {
       if(time_to_fetch_directory < now.tv_sec) {
         /* it's time to fetch a new directory */
         /* NOTE directory servers do not currently fetch directories.
@@ -332,7 +331,7 @@
       if(!connection_speaks_cells(tmpconn))
         continue; /* this conn type doesn't send cells */
       if(now.tv_sec >= tmpconn->timestamp_lastwritten + options.KeepalivePeriod) {
-        if((!(options.Role & ROLE_OR_CONNECT_ALL) && !circuit_get_by_conn(tmpconn)) ||
+        if((!options.ORPort && !circuit_get_by_conn(tmpconn)) ||
            (!connection_state_is_open(tmpconn))) {
           /* we're an onion proxy, with no circuits; or our handshake has expired. kill it. */
           log(LOG_DEBUG,"prepare_for_poll(): Expiring connection to %d (%s:%d).",
@@ -415,7 +414,7 @@
   }
 
   /* load the private key, if we're supposed to have one */
-  if(ROLE_IS_OR(global_role)) {
+  if(options.ORPort) {
     prkey = crypto_new_pk_env(CRYPTO_PK_RSA);
     if (!prkey) {
       log(LOG_ERR,"Error creating a crypto environment.");
@@ -429,9 +428,11 @@
     setprivatekey(prkey);
   }
 
-  /* start-up the necessary connections based on global_role. This is where we
-   * try to connect to all the other ORs, and start the listeners */
-  retry_all_connections(options.Role, options.ORPort,
+  /* start up the necessary connections based on which ports are
+   * non-zero. This is where we try to connect to all the other ORs,
+   * and start the listeners
+   */
+  retry_all_connections(options.ORPort,
                         options.OPPort, options.APPort, options.DirPort);
 
   for(;;) {
@@ -440,7 +441,7 @@
       please_dumpstats = 0;
     }
     if(please_fetch_directory) {
-      if(options.Role & ROLE_DIR_SERVER) {
+      if(options.DirPort) {
         if(router_get_list_from_file(options.RouterFile) < 0) {
           log(LOG_ERR,"Error reloading router list. Continuing with old list.");
         }
@@ -631,12 +632,11 @@
   if(getconfig(argc,argv,&options))
     exit(1);
   log(options.loglevel,NULL);         /* assign logging severity level from options */
-  global_role = options.Role;   /* assign global_role from options. FIXME: remove from global namespace later. */
 
   if (options.Daemon)
     daemonize();
 
-  if(options.Role & ROLE_OR_LISTEN) { /* only spawn dns handlers if we're a router */
+  if(options.ORPort) { /* only spawn dns handlers if we're a router */
     if(dns_master_start() < 0) {
       log(LOG_ERR,"main(): We're running without a dns handler. Bad news.");
     }

Index: onion.c
===================================================================
RCS file: /home/or/cvsroot/src/or/onion.c,v
retrieving revision 1.28
retrieving revision 1.29
diff -u -d -r1.28 -r1.29
--- onion.c	17 Mar 2003 02:42:14 -0000	1.28
+++ onion.c	18 Mar 2003 01:49:55 -0000	1.29
@@ -4,7 +4,6 @@
 
 #include "or.h"
 
-extern int global_role; /* from main.c */
 extern or_options_t options; /* command-line and config-file options */
 
 static int onion_process(circuit_t *circ);
@@ -351,7 +350,7 @@
 
   for(i=0;i<rarray_len;i++) {
     log(LOG_DEBUG,"Contemplating whether router %d is a new option...",i);
-    if( (global_role & ROLE_OR_CONNECT_ALL) &&
+    if(options.ORPort &&
       !connection_exact_get_by_addr_port(rarray[i]->addr, rarray[i]->or_port)) {
       log(LOG_DEBUG,"Nope, %d is not connected.",i);
       goto next_i_loop;
@@ -398,7 +397,7 @@
     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)) ||
-      ((global_role & ROLE_OR_CONNECT_ALL) && !connection_twin_get_by_addr_port(rarray[choice]->addr, rarray[choice]->or_port))) {
+      (options.ORPort && !connection_twin_get_by_addr_port(rarray[choice]->addr, rarray[choice]->or_port))) {
       /* Same router as last choice, or router twin,
        *   or no routers with that key are connected to us.
        * Try again. */

Index: or.h
===================================================================
RCS file: /home/or/cvsroot/src/or/or.h,v
retrieving revision 1.51
retrieving revision 1.52
diff -u -d -r1.51 -r1.52
--- or.h	17 Mar 2003 02:42:45 -0000	1.51
+++ or.h	18 Mar 2003 01:49:55 -0000	1.52
@@ -55,16 +55,6 @@
 #define ACI_TYPE_HIGHER 1
 #define ACI_TYPE_BOTH 2
 
-/* bitvector of the roles that we might want to play. You can or (|) them together */
-#define ROLE_OR_LISTEN 1
-#define ROLE_OR_CONNECT_ALL 2
-#define ROLE_OP_LISTEN 4
-#define ROLE_AP_LISTEN 8
-#define ROLE_DIR_LISTEN 16
-#define ROLE_DIR_SERVER 32
-
-#define ROLE_IS_OR(role) ((role & ROLE_OR_LISTEN) || (role & ROLE_OR_CONNECT_ALL) || (role & ROLE_OP_LISTEN))
-
 #define CONN_TYPE_OP_LISTENER 1
 #define CONN_TYPE_OP 2
 #define CONN_TYPE_OR_LISTENER 3
@@ -559,7 +549,7 @@
 int connection_handle_listener_read(connection_t *conn, int new_type, int new_state);
 
 /* start all connections that should be up but aren't */
-int retry_all_connections(int role, uint16_t or_listenport, 
+int retry_all_connections(uint16_t or_listenport,
   uint16_t op_listenport, uint16_t ap_listenport, uint16_t dir_listenport);
 
 int connection_read_to_buf(connection_t *conn);
@@ -568,9 +558,9 @@
 
 #ifdef USE_ZLIB
 int connection_compress_from_buf(char *string, int len, connection_t *conn,
-				 int flush);
+                                 int flush);
 int connection_decompress_to_buf(char *string, int len, connection_t *conn,
-				 int flush);
+                                 int flush);
 #endif
 
 int connection_outbuf_too_full(connection_t *conn);

Index: routers.c
===================================================================
RCS file: /home/or/cvsroot/src/or/routers.c,v
retrieving revision 1.17
retrieving revision 1.18
diff -u -d -r1.17 -r1.18
--- routers.c	3 Dec 2002 05:12:30 -0000	1.17
+++ routers.c	18 Mar 2003 01:49:55 -0000	1.18
@@ -19,7 +19,6 @@
 static routerinfo_t **router_array = NULL;
 static int rarray_len = 0;
 
-extern int global_role; /* from main.c */
 extern or_options_t options; /* command-line and config-file options */
 extern routerinfo_t *my_routerinfo; /* from main.c */
 
@@ -125,7 +124,7 @@
 {
   struct sockaddr_in me; /* my router identity */
 
-  if(!ROLE_IS_OR(global_role)) {
+  if(!options.ORPort) {
     /* we're not an OR. This obviously isn't us. */
     return 0;
   }



More information about the tor-commits mailing list