aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorBruce Montrose <montrose@itd.nrl.navy.mil>2002-07-03 16:31:22 +0000
committerBruce Montrose <montrose@itd.nrl.navy.mil>2002-07-03 16:31:22 +0000
commita3609f4d5d66ce4eeb4492422e7a0ec2d623a43a (patch)
treec1f5d2f144c45c1b3017b97f8bb3fcbe550e411f /src
parent419781685cc7fcccb18634cdd23b639ea73ba39f (diff)
downloadtor-a3609f4d5d66ce4eeb4492422e7a0ec2d623a43a.tar
tor-a3609f4d5d66ce4eeb4492422e7a0ec2d623a43a.tar.gz
Added getoptions() and made minor adjustment to poptReadDefaultOptions()
svn:r25
Diffstat (limited to 'src')
-rw-r--r--src/common/config.c7
-rw-r--r--src/common/config.h15
-rw-r--r--src/or/config.c77
-rw-r--r--src/or/or.h16
4 files changed, 108 insertions, 7 deletions
diff --git a/src/common/config.c b/src/common/config.c
index 1d2f1a166..3efcbe207 100644
--- a/src/common/config.c
+++ b/src/common/config.c
@@ -8,6 +8,9 @@
/*
* Changes :
* $Log$
+ * Revision 1.3 2002/07/03 16:31:22 montrose
+ * Added getoptions() and made minor adjustment to poptReadDefaultOptions()
+ *
* Revision 1.2 2002/06/28 18:14:55 montrose
* Added poptReadOptions() and poptReadDefaultOptions()
*
@@ -360,7 +363,7 @@ RETURN VALUE: INT_MIN = problem opening config file, else standard poptGetNextOp
FILE *fp;
int argc, c;
char **argv;
- char line[256];
+ char line[1024];
line[0] = line[1] = '-'; /* prepend expected long name option flag */
fp = open_config(fname);
if ( fp == NULL ) return INT_MIN;
@@ -402,6 +405,6 @@ RETURN VALUE: same as poptReadOptions()
sprintf(fname,"~/.%src",cmd);
c = poptReadOptions(optCon,fname);
}
- return c;
+ return (c == INT_MIN) ? -1 : c;
}
diff --git a/src/common/config.h b/src/common/config.h
index 15d9b1e5d..7642086b1 100644
--- a/src/common/config.h
+++ b/src/common/config.h
@@ -8,8 +8,11 @@
/*
* Changes :
* $Log$
- * Revision 1.1 2002/06/26 22:45:50 arma
- * Initial revision
+ * Revision 1.2 2002/07/03 16:31:22 montrose
+ * Added getoptions() and made minor adjustment to poptReadDefaultOptions()
+ *
+ * Revision 1.1.1.1 2002/06/26 22:45:50 arma
+ * initial commit: current code
*
* Revision 1.7 2002/04/02 14:27:11 badbytes
* Final finishes.
@@ -39,6 +42,8 @@
# include <stdio.h>
+#include <popt.h>
+
/* enumeration of types which option values can take */
#define CONFIG_TYPE_STRING 0
#define CONFIG_TYPE_CHAR 1
@@ -84,5 +89,11 @@ int close_config(FILE *f);
/* parse the config file and obtain required option values */
int parse_config(FILE *f, config_opt_t *option);
+/* parse popt-style options in a config file */
+int poptReadOptions(poptContext optCon, const unsigned char *fname);
+
+/* parse popt-style options from /etc/<cmd>rc and ~/.<cmd>rc */
+int poptReadDefaultOptions(const char *cmd, poptContext optCon);
+
#define __CONFIG_H
#endif
diff --git a/src/or/config.c b/src/or/config.c
index 535f7638d..e1d534538 100644
--- a/src/or/config.c
+++ b/src/or/config.c
@@ -8,8 +8,11 @@
/*
* Changes :
* $Log$
- * Revision 1.1 2002/06/26 22:45:50 arma
- * Initial revision
+ * Revision 1.2 2002/07/03 16:31:22 montrose
+ * Added getoptions() and made minor adjustment to poptReadDefaultOptions()
+ *
+ * Revision 1.1.1.1 2002/06/26 22:45:50 arma
+ * initial commit: current code
*
* Revision 1.3 2002/04/02 14:28:24 badbytes
* Final finishes.
@@ -23,6 +26,7 @@
*/
#include "or.h"
+#include <libgen.h>
/* loads the configuration file */
int getconfig(char *conf_filename, config_opt_t *options)
@@ -47,3 +51,72 @@ int getconfig(char *conf_filename, config_opt_t *options)
return 0;
}
+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
+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.
+RETURN VALUE: 0 on success, non-zero on error
+**/
+{
+ char *ConfigFile;
+ int Verbose;
+ int code;
+ poptContext optCon;
+ 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 },
+ POPT_AUTOHELP /* handles --usage and --help automatically */
+ POPT_TABLEEND /* marks end of table */
+ };
+ cmd = basename(argv[0]);
+ optCon = poptGetContext(cmd,argc,(const char **)argv,opt_tab,0);
+
+ poptReadDefaultConfig(optCon,0); /* read <popt> alias definitions */
+
+ bzero(options,sizeof(or_options_t)); /* zero out options initially */
+
+ code = poptGetNextOpt(optCon); /* first we handle command-line args */
+
+ if ( ConfigFile ) /* handle user-specified config file if any */
+ {
+ code = poptReadOptions(optCon,ConfigFile);
+ if ( code < -1 ) return code;
+ }
+ else /* load Default configuration files */
+ {
+ code = poptReadDefaultOptions(cmd,optCon);
+ if ( code < -1 ) return code;
+ }
+
+ if ( Verbose ) /* display options upon user request */
+ {
+ printf("\nLogLevel=%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\n",options->CoinWeight,options->MaxConn,options->TrafficShaping);
+ }
+
+ poptFreeContext(optCon);
+
+ return 0;
+}
+
diff --git a/src/or/or.h b/src/or/or.h
index 0f4647fb2..e09b154f5 100644
--- a/src/or/or.h
+++ b/src/or/or.h
@@ -284,7 +284,18 @@ typedef struct
void *next;
} tracked_onion_t;
-
+typedef struct
+{
+ char *LogLevel;
+ char *RouterFile;
+ char *PrivateKeyFile;
+ float CoinWeight;
+ int ORPort;
+ int OPPort;
+ int APPort;
+ int MaxConn;
+ int TrafficShaping;
+} or_options_t;
/* all the function prototypes go here */
@@ -366,6 +377,9 @@ void command_process_destroy_cell(cell_t *cell, connection_t *conn);
/* loads the configuration file */
int getconfig(char *filename, config_opt_t *options);
+/* create or_options_t from command-line args and config files(s) */
+int getoptions(int argc, char **argv, or_options_t *options);
+
/********************************* connection.c ***************************/
connection_t *connection_new(int type);