[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[or-cvs] cleaned up some, added validation to getoptions()



Update of /home/or/cvsroot/src/or
In directory moria.seul.org:/tmp/cvs-serv8192/src/or

Modified Files:
	config.c or.h 
Log Message:
cleaned up some, added validation to getoptions()


Index: config.c
===================================================================
RCS file: /home/or/cvsroot/src/or/config.c,v
retrieving revision 1.6
retrieving revision 1.7
diff -u -d -r1.6 -r1.7
--- config.c	10 Jul 2002 12:37:49 -0000	1.6
+++ config.c	11 Jul 2002 14:50:26 -0000	1.7
@@ -8,6 +8,9 @@
 /*
  * Changes :
  * $Log$
+ * Revision 1.7  2002/07/11 14:50:26  montrose
+ * cleaned up some, added validation to getoptions()
+ *
  * Revision 1.6  2002/07/10 12:37:49  montrose
  * Added usage display on error.
  *
@@ -65,16 +68,18 @@
 
 int getoptions(int argc, char **argv, or_options_t *options)
 /**
+
 A replacement for getargs() and getconfig() which uses the <popt> library to parse
 both command-line arguments and configuration files. A specific configuration file
 may be specified using the --ConfigFile option. If one is not specified, then the
-configuration files at /etc/<cmd>rc and ~/.<cmd>rc will be loaded in that order (so
-user preferences will override the ones specified in /etc. Note: <cmd> is the
-basename() or argv[0] so one could run the same executeable through soft links to
-get different configuration files loaded for different instances of the same program.
-The ConfigFile option may only be used on the command-line. All other command-line
+configuration files at /etc/<cmd>rc and ~/.<cmd>rc will be loaded in that order so
+user preferences will override the ones specified in /etc.
+
+The --ConfigFile (-f) option may only be used on the command-line. All other command-line
 options may also be specified in configuration files. <popt> aliases are enabled
-here so a user can define their own options in the /etc/popt or ~/.popt files.
+so a user can define their own options in the /etc/popt or ~/.popt files as outlined
+in "man popt" pages.
+
 RETURN VALUE: 0 on success, non-zero on error
 **/
 {
@@ -85,17 +90,28 @@
    char *cmd;
    struct poptOption opt_tab[] =
    {
-      { "APPort", 'a', POPT_ARG_INT, &options->APPort, 0, "application proxy port", "<port>" },
-      { "CoinWeight", 'w', POPT_ARG_FLOAT, &options->CoinWeight, 0, "coin weight used in determining routes", "<weight>" },
-      { "ConfigFile", 'f', POPT_ARG_STRING, &ConfigFile, 0, "user specified configuration file", "<file>" },
-      { "LogLevel", 'l', POPT_ARG_STRING, &options->LogLevel, 0, "emerg|alert|crit|err|warning|notice|info|debug", "<level>" },
-      { "MaxConn", 'm', POPT_ARG_INT, &options->MaxConn, 0, "maximum number of incoming connections", "<max>" },
-      { "OPPort", 'o', POPT_ARG_INT, &options->OPPort, 0, "onion proxy port", "<port>" },
-      { "ORPort", 'p', POPT_ARG_INT, &options->ORPort, 0, "onion router port", "<port>" },
-      { "PrivateKeyFile", 'k', POPT_ARG_STRING, &options->PrivateKeyFile, 0, "maximum number of incoming connections", "<max>" },
-      { "RouterFile", 'r', POPT_ARG_STRING, &options->RouterFile, 0, "local port on which the onion proxy is running", "<port>" },
-      { "TrafficShaping", 't', POPT_ARG_INT, &options->TrafficShaping, 0, "which traffic shaping policy to use", "<policy>" },
-      { "Verbose", 'v', POPT_ARG_NONE, &Verbose, 0, "display options selected before execution", NULL },
+      { "APPort",          'a', POPT_ARG_INT,      &options->APPort,
+         0, "application proxy port",                          "<port>" },
+      { "CoinWeight",      'w', POPT_ARG_FLOAT,    &options->CoinWeight,
+         0, "coin weight used in determining routes",          "<weight>" },
+      { "ConfigFile",      'f', POPT_ARG_STRING,   &ConfigFile,
+         0, "user specified configuration file",               "<file>" },
+      { "LogLevel",        'l', POPT_ARG_STRING,   &options->LogLevel,
+         0, "emerg|alert|crit|err|warning|notice|info|debug",  "<level>" },
+      { "MaxConn",         'm', POPT_ARG_INT,      &options->MaxConn,
+         0, "maximum number of incoming connections",          "<max>" },
+      { "OPPort",          'o', POPT_ARG_INT,      &options->OPPort,
+         0, "onion proxy port",                                "<port>" },
+      { "ORPort",          'p', POPT_ARG_INT,      &options->ORPort,
+         0, "onion router port",                               "<port>" },
+      { "PrivateKeyFile",  'k', POPT_ARG_STRING,   &options->PrivateKeyFile,
+         0, "maximum number of incoming connections",          "<max>" },
+      { "RouterFile",      'r', POPT_ARG_STRING,   &options->RouterFile,
+         0, "local port on which the onion proxy is running",  "<port>" },
+      { "TrafficShaping",  't', POPT_ARG_INT,      &options->TrafficShaping,
+         0, "which traffic shaping policy to use",             "<policy>" },
+      { "Verbose",         'v', POPT_ARG_NONE,     &Verbose,
+         0, "display options selected before execution",       NULL },
       POPT_AUTOHELP  /* handles --usage and --help automatically */
       POPT_TABLEEND  /* marks end of table */
    };
@@ -109,7 +125,7 @@
    code = poptGetNextOpt(optCon);         /* first we handle command-line args */
    if ( code == -1 )
    {
-      if ( ConfigFile )                   /* handle user-specified config file if any */
+      if ( ConfigFile )                   /* handle user-specified config file */
          code = poptReadOptions(optCon,ConfigFile);
       else                                /* load Default configuration files */
          code = poptReadDefaultOptions(cmd,optCon);
@@ -118,25 +134,109 @@
    switch(code)                           /* error checking */
    {
    case INT_MIN:
-      fprintf(stderr, "%s: Unable to open configuration file.\n", ConfigFile);
+      log(LOG_ERR, "%s: Unable to open configuration file.\n", ConfigFile);
       break;
    case -1:
       if ( Verbose )                      /* display options upon user request */
       {
          printf("LogLevel=%s\n",options->LogLevel);
-         printf("RouterFile=%s, PrivateKeyFile=%s\n",options->RouterFile,options->PrivateKeyFile);
-         printf("ORPort=%d, OPPort=%d, APPort=%d\n",options->ORPort,options->OPPort,options->APPort);
-         printf("CoinWeight=%6.4f, MaxConn=%d, TrafficShaping=%d\n",options->CoinWeight,options->MaxConn,options->TrafficShaping);
+         printf("RouterFile=%s, PrivateKeyFile=%s\n",
+                options->RouterFile,
+                options->PrivateKeyFile);
+         printf("ORPort=%d, OPPort=%d, APPort=%d\n",
+                options->ORPort,options->OPPort,
+                options->APPort);
+         printf("CoinWeight=%6.4f, MaxConn=%d, TrafficShaping=%d\n",
+                options->CoinWeight,
+                options->MaxConn,
+                options->TrafficShaping);
       }
       code = 0;
       break;
    default:
       poptPrintUsage(optCon, stderr, 0);
-      fprintf(stderr, "%s: %s\n", poptBadOption(optCon, POPT_BADOPTION_NOALIAS), poptStrerror(code));
+      log(LOG_ERR, "%s: %s\n", poptBadOption(optCon, POPT_BADOPTION_NOALIAS), poptStrerror(code));
       break;
    }
 
    poptFreeContext(optCon);
+
+   /* Validate options */
+
+   if ( options->LogLevel )
+   {
+      if (!strcmp(options->LogLevel,"emerg"))
+         options->loglevel = LOG_EMERG;
+      else if (!strcmp(options->LogLevel,"alert"))
+         options->loglevel = LOG_ALERT;
+      else if (!strcmp(options->LogLevel,"crit"))
+         options->loglevel = LOG_CRIT;
+      else if (!strcmp(options->LogLevel,"err"))
+         options->loglevel = LOG_ERR;
+      else if (!strcmp(options->LogLevel,"warning"))
+         options->loglevel = LOG_WARNING;
+      else if (!strcmp(options->LogLevel,"notice"))
+         options->loglevel = LOG_NOTICE;
+      else if (!strcmp(options->LogLevel,"info"))
+         options->loglevel = LOG_INFO;
+      else if (!strcmp(options->LogLevel,"debug"))
+         options->loglevel = LOG_DEBUG;
+      else
+      {
+         log(LOG_ERR,"LogLevel must be one of emerg|alert|crit|err|warning|notice|info|debug.");
+         code = -1;
+      }
+   }
+   else
+      options->loglevel = LOG_DEBUG;   /* default value */
+
+   if ( options->RouterFile == NULL )
+   {
+      log(LOG_ERR,"RouterFile option required, but not found.");
+      code = -1;
+   }
+
+   if ( options->PrivateKeyFile == NULL )
+   {
+      log(LOG_ERR,"PrivateKeyFile option required, but not found.");
+      code = -1;
+   }
+
+   if ( options->ORPort < 1 )
+   {
+      log(LOG_ERR,"ORPort option required and must be a positive integer value.");
+      code = -1;
+   }
+
+   if ( options->OPPort < 1 )
+   {
+      log(LOG_ERR,"OPPort option required and must be a positive integer value.");
+      code = -1;
+   }
+
+   if ( options->APPort < 1 )
+   {
+      log(LOG_ERR,"APPort option required and must be a positive integer value.");
+      code = -1;
+   }
+
+   if ( options->CoinWeight < 0.0 || options->CoinWeight >= 1.0 )
+   {
+      log(LOG_ERR,"CoinWeight option must a value from 0.0 upto 1.0, but not including 1.0.");
+      code = -1;
+   }
+
+   if ( options->MaxConn <= 0 )
+   {
+      log(LOG_ERR,"MaxConn option must be a non-zero positive integer.");
+      code = -1;
+   }
+
+   if ( options->TrafficShaping != 0 && options->TrafficShaping != 1 )
+   {
+      log(LOG_ERR,"TrafficShaping option must be either 0 or 1.");
+      code = -1;
+   }
 
    return code;
 }

Index: or.h
===================================================================
RCS file: /home/or/cvsroot/src/or/or.h,v
retrieving revision 1.7
retrieving revision 1.8
diff -u -d -r1.7 -r1.8
--- or.h	10 Jul 2002 12:35:59 -0000	1.7
+++ or.h	11 Jul 2002 14:50:26 -0000	1.8
@@ -295,6 +295,7 @@
    int APPort;
    int MaxConn;
    int TrafficShaping;
+   int loglevel;
 } or_options_t;