From 4c65f3134019400105a9927b12b5a6f2eae4d362 Mon Sep 17 00:00:00 2001 From: Bruce Montrose Date: Thu, 11 Jul 2002 14:50:26 +0000 Subject: [PATCH] cleaned up some, added validation to getoptions() svn:r40 --- src/or/config.c | 146 ++++++++++++++++++++++++++++++++++++++++-------- src/or/or.h | 1 + 2 files changed, 124 insertions(+), 23 deletions(-) diff --git a/src/or/config.c b/src/or/config.c index b0283e1505..b408cccd1b 100644 --- a/src/or/config.c +++ b/src/or/config.c @@ -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 getconfig(char *conf_filename, config_opt_t *options) int getoptions(int argc, char **argv, or_options_t *options) /** + A replacement for getargs() and getconfig() which uses the 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/rc and ~/.rc will be loaded in that order (so -user preferences will override the ones specified in /etc. Note: 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/rc and ~/.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. 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 @@ RETURN VALUE: 0 on success, non-zero on error char *cmd; struct poptOption opt_tab[] = { - { "APPort", 'a', POPT_ARG_INT, &options->APPort, 0, "application proxy port", "" }, - { "CoinWeight", 'w', POPT_ARG_FLOAT, &options->CoinWeight, 0, "coin weight used in determining routes", "" }, - { "ConfigFile", 'f', POPT_ARG_STRING, &ConfigFile, 0, "user specified configuration file", "" }, - { "LogLevel", 'l', POPT_ARG_STRING, &options->LogLevel, 0, "emerg|alert|crit|err|warning|notice|info|debug", "" }, - { "MaxConn", 'm', POPT_ARG_INT, &options->MaxConn, 0, "maximum number of incoming connections", "" }, - { "OPPort", 'o', POPT_ARG_INT, &options->OPPort, 0, "onion proxy port", "" }, - { "ORPort", 'p', POPT_ARG_INT, &options->ORPort, 0, "onion router port", "" }, - { "PrivateKeyFile", 'k', POPT_ARG_STRING, &options->PrivateKeyFile, 0, "maximum number of incoming connections", "" }, - { "RouterFile", 'r', POPT_ARG_STRING, &options->RouterFile, 0, "local port on which the onion proxy is running", "" }, - { "TrafficShaping", 't', POPT_ARG_INT, &options->TrafficShaping, 0, "which traffic shaping policy to use", "" }, - { "Verbose", 'v', POPT_ARG_NONE, &Verbose, 0, "display options selected before execution", NULL }, + { "APPort", 'a', POPT_ARG_INT, &options->APPort, + 0, "application proxy port", "" }, + { "CoinWeight", 'w', POPT_ARG_FLOAT, &options->CoinWeight, + 0, "coin weight used in determining routes", "" }, + { "ConfigFile", 'f', POPT_ARG_STRING, &ConfigFile, + 0, "user specified configuration file", "" }, + { "LogLevel", 'l', POPT_ARG_STRING, &options->LogLevel, + 0, "emerg|alert|crit|err|warning|notice|info|debug", "" }, + { "MaxConn", 'm', POPT_ARG_INT, &options->MaxConn, + 0, "maximum number of incoming connections", "" }, + { "OPPort", 'o', POPT_ARG_INT, &options->OPPort, + 0, "onion proxy port", "" }, + { "ORPort", 'p', POPT_ARG_INT, &options->ORPort, + 0, "onion router port", "" }, + { "PrivateKeyFile", 'k', POPT_ARG_STRING, &options->PrivateKeyFile, + 0, "maximum number of incoming connections", "" }, + { "RouterFile", 'r', POPT_ARG_STRING, &options->RouterFile, + 0, "local port on which the onion proxy is running", "" }, + { "TrafficShaping", 't', POPT_ARG_INT, &options->TrafficShaping, + 0, "which traffic shaping policy to use", "" }, + { "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 @@ RETURN VALUE: 0 on success, non-zero on error 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,26 +134,110 @@ RETURN VALUE: 0 on success, non-zero on error 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; } diff --git a/src/or/or.h b/src/or/or.h index c7cc2b63d9..610f760d22 100644 --- a/src/or/or.h +++ b/src/or/or.h @@ -295,6 +295,7 @@ typedef struct int APPort; int MaxConn; int TrafficShaping; + int loglevel; } or_options_t;