From 56cf51f0f9e2dc79b8054b19295eaf567dfcf4ff Mon Sep 17 00:00:00 2001 From: mpc Date: Fri, 25 Jun 2004 01:31:02 +0000 Subject: [PATCH] New configuration system --- apps/enclave/cfg/enclave.cfg | 16 +++--- apps/enclave/src/chk.cpp | 4 +- apps/enclave/src/config.cpp | 95 +++++++++++++++++++++++++++-------- apps/enclave/src/config.hpp | 4 +- apps/enclave/src/logger.cpp | 3 +- apps/enclave/src/logger.hpp | 20 ++++---- apps/enclave/src/main.cpp | 26 +++++++--- apps/enclave/src/platform.hpp | 11 ++-- apps/enclave/src/random.cpp | 2 +- apps/enclave/src/sam.cpp | 7 +-- apps/enclave/src/sam.hpp | 2 +- 11 files changed, 128 insertions(+), 62 deletions(-) diff --git a/apps/enclave/cfg/enclave.cfg b/apps/enclave/cfg/enclave.cfg index cb118d48e..dacb19a5f 100644 --- a/apps/enclave/cfg/enclave.cfg +++ b/apps/enclave/cfg/enclave.cfg @@ -11,20 +11,22 @@ samport=7656 # The destination name of this program. This can be anything. If you run # multiple copies of Enclave off the same SAM server then each one has to have a -# unique mydest. -mydest=enclave +# unique name. +samname=enclave # The depth used for incoming and outgoing I2P tunnels. Using a depth of 2 is # the default and a good choice. You can set it to 0 if you don't care about # anonymity and just want speed. tunneldepth=0 -# The location of the peer references file -references=/home/Administrator/cvs/i2p/apps/enclave/cfg/peers.ref +# The location of the peer references file. You can use an absolute or relative +# path, but absolute paths are safer. +references=cfg/peers.ref # Record every log message at or above this priority level # debug = 0, minor = 1, info = 2, warn = 3, error = 4 -loglevel=1 +loglevel=0 -# The location of the Enclave log file -logfile=/home/Administrator/cvs/i2p/apps/enclave/log/enclave.log +# The location of the Enclave log file. You can use an absolute or relative +# path, but absolute paths are safer. +logfile=log/enclave.log diff --git a/apps/enclave/src/chk.cpp b/apps/enclave/src/chk.cpp index 59b3506d2..fd8500d53 100644 --- a/apps/enclave/src/chk.cpp +++ b/apps/enclave/src/chk.cpp @@ -43,8 +43,8 @@ void Chk::encrypt(const uchar_t *pt) assert(rc != -1); uchar_t key[CRYPT_KEY_SIZE], iv[CRYPT_BLOCK_SIZE]; - prng.get_bytes(key, CRYPT_KEY_SIZE); - prng.get_bytes(iv, CRYPT_BLOCK_SIZE); + prng->get_bytes(key, CRYPT_KEY_SIZE); + prng->get_bytes(iv, CRYPT_BLOCK_SIZE); symmetric_CTR ctr; rc = ctr_start(find_cipher("twofish"), iv, key, CRYPT_KEY_SIZE, 0, &ctr); diff --git a/apps/enclave/src/config.cpp b/apps/enclave/src/config.cpp index 5aa9daaff..d5a1f51b6 100644 --- a/apps/enclave/src/config.cpp +++ b/apps/enclave/src/config.cpp @@ -39,6 +39,72 @@ Config::Config(const string& file) configf.close(); } +/* + * Looks up a configuration option in the table and returns a constant value. + * This is the same as get_property() except the value returned is a constant. + * + * key - key to lookup + * + * Returns the value associated with the key + */ +const string& Config::get_cproperty(const string& key) const +{ + for (cfgmap_ci i = cfgmap.begin(); i != cfgmap.end(); i++) { + const string s = i->first; + if (s == key) + return i->second; + } + LERROR << "Tried to lookup an invalid property: " << key << '\n'; + assert(false); + // this should never occur, it's just to silence a compiler warning + string* s = new string; + return *s; +} + +/* + * Gets a property as an integer (they are all stored as strings) + * + * key - key to lookup + * + * Returns an integer of the value associated with the key + */ +int Config::get_iproperty(const string& key) const +{ + for (cfgmap_ci i = cfgmap.begin(); i != cfgmap.end(); i++) { + const string s = i->first; + if (s == key) + return atoi(i->second.c_str()); + } + LERROR << "Tried to lookup an invalid property: " << key << '\n'; + assert(false); + return 0; +} + +/* + * Looks up a configuration option in the table and returns the value + * + * key - key to lookup + * + * Returns the value associated with the key + */ +string& Config::get_property(const string& key) +{ + for (cfgmap_i i = cfgmap.begin(); i != cfgmap.end(); i++) { + const string s = i->first; + if (s == key) + return i->second; + } + LERROR << "Tried to lookup an invalid property: " << key << '\n'; + assert(false); + // this should never occur, it's just to silence a compiler warning + string* s = new string; + return *s; +} + +/* + * Parses the configuration file, replacing default values with user defined + * values + */ void Config::parse(void) { configf.open(file.c_str()); @@ -50,42 +116,31 @@ void Config::parse(void) string s; for (getline(configf, s); configf; getline(configf, s)) { line++; - if (s[0] == '#') // comment + if (s.size() == 0 || s[0] == '#') // blank line or comment continue; size_t eqpos = s.find("="); if (eqpos == string::npos) { - LERROR << "Error parsing line #" << line << " in " << file << ": " << s << '\n'; + cerr << "Error parsing line #" << line << " in " << file << ": " + << s << '\n'; continue; } - string key = s.substr(0, eqpos - 1); + string key = s.substr(0, eqpos); string value = s.substr(eqpos + 1); + //cout << "Inserting key = " << key << " value = " << value << '\n'; + cfgmap.erase(key); // erase the default value created by set_defaults() cfgmap.insert(make_pair(key, value)); } } /* - * Looks up a configuration option in the table and returns the value - * - * key - key to lookup - * - * Returns a pointer to the value associated with the key + * If you (the programmer) add something to the config file you should also add + * it here, and vice versa */ -const string* Config::get_property(const string& key) const -{ - for (cfgmap_ci i = cfgmap.begin(); i != cfgmap.end(); i++) { - string s = i->first; - if (s == key) - return &(i->second); - } - assert(false); - return 0; -} - void Config::set_defaults(void) { cfgmap.insert(make_pair("samhost", "localhost")); cfgmap.insert(make_pair("samport", "7656")); - cfgmap.insert(make_pair("mydest", "enclave")); + cfgmap.insert(make_pair("samname", "enclave")); cfgmap.insert(make_pair("tunneldepth", "2")); cfgmap.insert(make_pair("references", "cfg/peers.ref")); cfgmap.insert(make_pair("loglevel", "1")); diff --git a/apps/enclave/src/config.hpp b/apps/enclave/src/config.hpp index 6c207600d..278db650b 100644 --- a/apps/enclave/src/config.hpp +++ b/apps/enclave/src/config.hpp @@ -35,7 +35,9 @@ class Config { public: Config(const string& file); - const string* get_property(const string& key) const; + const string& get_cproperty(const string& key) const; + int get_iproperty(const string& key) const; + string& get_property(const string& key); private: typedef map::const_iterator cfgmap_ci; diff --git a/apps/enclave/src/logger.cpp b/apps/enclave/src/logger.cpp index 44dc7e470..145845995 100644 --- a/apps/enclave/src/logger.cpp +++ b/apps/enclave/src/logger.cpp @@ -34,7 +34,8 @@ Logger::Logger(const string& file) : file(file) { - loglevel = priority = debug; + set_pri(debug); + set_loglevel(static_cast(config->get_iproperty("loglevel"))); logf.open(file.c_str(), ios::app); if (!logf) { cerr << "Error opening log file (" << file.c_str() << ")\n"; diff --git a/apps/enclave/src/logger.hpp b/apps/enclave/src/logger.hpp index 816cbc2aa..75a5c2892 100644 --- a/apps/enclave/src/logger.hpp +++ b/apps/enclave/src/logger.hpp @@ -39,17 +39,17 @@ * LERROR - major, important errors */ #if VERBOSE_LOGS - #define LDEBUG logger.set_pri(Logger::debug); logger << "(D)" << __FILE__ << ':' << __LINE__ << ':' << __func__ << ": " - #define LMINOR logger.set_pri(Logger::minor); logger << "(M)" << __FILE__ << ':' << __LINE__ << ':' << __func__ << ": " - #define LINFO logger.set_pri(Logger::info); logger << "(I)" << __FILE__ << ':' << __LINE__ << ':' << __func__ << ": " - #define LWARN logger.set_pri(Logger::warn); logger << "(W)" << __FILE__ << ':' << __LINE__ << ':' << __func__ << ": " - #define LERROR logger.set_pri(Logger::error); logger << "(E)" << __FILE__ << ':' << __LINE__ << ':' << __func__ << ": " + #define LDEBUG logger->set_pri(Logger::debug); (*logger) << "(D)" << __FILE__ << ':' << __LINE__ << ':' << __func__ << ": " + #define LMINOR logger->set_pri(Logger::minor); (*logger) << "(M)" << __FILE__ << ':' << __LINE__ << ':' << __func__ << ": " + #define LINFO logger->set_pri(Logger::info); (*logger) << "(I)" << __FILE__ << ':' << __LINE__ << ':' << __func__ << ": " + #define LWARN logger->set_pri(Logger::warn); (*logger) << "(W)" << __FILE__ << ':' << __LINE__ << ':' << __func__ << ": " + #define LERROR logger->set_pri(Logger::error); (*logger) << "(E)" << __FILE__ << ':' << __LINE__ << ':' << __func__ << ": " #else - #define LDEBUG logger.set_pri(Logger::debug); logger << "(D)" - #define LMINOR logger.set_pri(Logger::minor); logger << "(M)" - #define LINFO logger.set_pri(Logger::info); logger << "(I)" - #define LWARN logger.set_pri(Logger::warn); logger << "(W)" - #define LERROR logger.set_pri(Logger::error); logger << "(E)" + #define LDEBUG logger->set_pri(Logger::debug); (*logger) << "(D)" + #define LMINOR logger->set_pri(Logger::minor); (*logger) << "(M)" + #define LINFO logger->set_pri(Logger::info); (*logger) << "(I)" + #define LWARN logger->set_pri(Logger::warn); (*logger) << "(W)" + #define LERROR logger->set_pri(Logger::error); (*logger) << "(E)" #endif class Logger { diff --git a/apps/enclave/src/main.cpp b/apps/enclave/src/main.cpp index a85f7d1f5..b4fea4a4d 100644 --- a/apps/enclave/src/main.cpp +++ b/apps/enclave/src/main.cpp @@ -32,23 +32,30 @@ #include "main.hpp" Config *config; // Configuration options -Logger logger(LOG_FILE); // Logging mechanism -Random prng; // Random number generator +Logger *logger; // Logging mechanism +Random *prng; // Random number generator Sam *sam; // SAM connection int main(int argc, char* argv[]) { - logger.set_loglevel(Logger::debug); - if (argc != 2) { // put some getopts stuff in here later - LERROR << "Please specify your destination name. e.g. 'bin/enclave " \ - "enclave'\n"; + cerr << "Please specify the configuration file location.\n" \ + "e.g. 'bin/enclave cfg/enclave.cfg'\n"; return 1; } - LINFO << "Enclave DHT - Built on " << __DATE__ << ' ' << __TIME__ << '\n'; try { - sam = new Sam("localhost", 7656, argv[1], 0); + config = new Config(argv[1]); + } catch (const runtime_error& x) { + return 0; + } + logger = new Logger(config->get_cproperty("logfile")); + LINFO << "Enclave DHT - Built on " << __DATE__ << ' ' << __TIME__ << '\n'; + prng = new Random; + try { + sam = new Sam(config->get_cproperty("samhost"), + config->get_iproperty("samport"), config->get_cproperty("samname"), + config->get_iproperty("tunneldepth")); } catch (const Sam_error& x) { LERROR << "SAM error: " << x.what() << '\n'; cerr << "SAM error: " << x.what() << '\n'; @@ -70,5 +77,8 @@ int main(int argc, char* argv[]) sam->read_buffer(); delete sam; + delete prng; + delete logger; + delete config; return 0; } diff --git a/apps/enclave/src/platform.hpp b/apps/enclave/src/platform.hpp index 178b88a1a..bfdf40ae5 100644 --- a/apps/enclave/src/platform.hpp +++ b/apps/enclave/src/platform.hpp @@ -43,6 +43,7 @@ * System includes */ #include +#include #include #include #include @@ -61,12 +62,6 @@ using namespace std; */ #define VERBOSE_LOGS 0 -/* - * The default locations for some files - */ -#define LOG_FILE "log/main.log" -#define PEERS_REF_FILE "cfg/peers.ref" - /* * Library includes */ @@ -91,8 +86,8 @@ using namespace std; * Global variables */ extern Config *config; // Configuration options -extern Logger logger; // Logging mechanism -extern Random prng; // Random number generator +extern Logger *logger; // Logging mechanism +extern Random *prng; // Random number generator extern Sam *sam; // Sam connection #endif // PLATFORM_HPP diff --git a/apps/enclave/src/random.cpp b/apps/enclave/src/random.cpp index 1652d050e..e843e5a26 100644 --- a/apps/enclave/src/random.cpp +++ b/apps/enclave/src/random.cpp @@ -36,7 +36,7 @@ */ Random::Random(void) { - LINFO << "Initalising PRNG\n";// it could take a bit of time on some systems + LMINOR << "Initalising PRNG\n"; int rc = yarrow_start(&prng); assert(rc == CRYPT_OK); diff --git a/apps/enclave/src/sam.cpp b/apps/enclave/src/sam.cpp index 25bc99de5..8df1f7f61 100644 --- a/apps/enclave/src/sam.cpp +++ b/apps/enclave/src/sam.cpp @@ -48,7 +48,7 @@ extern "C" { */ bool Sam::exists = false; -Sam::Sam(const char* samhost, uint16_t samport, const char* destname, +Sam::Sam(const string& samhost, uint16_t samport, const string& destname, uint_t tunneldepth) { // Only allow one Sam object to exist at a time @@ -65,7 +65,7 @@ Sam::Sam(const char* samhost, uint16_t samport, const char* destname, set_connected(false); // now try to connect to SAM - connect(samhost, samport, destname, tunneldepth); + connect(samhost.c_str(), samport, destname.c_str(), tunneldepth); } Sam::~Sam(void) @@ -90,6 +90,7 @@ void Sam::connect(const char* samhost, uint16_t samport, const char* destname, uint_t tunneldepth) { assert(!get_connected()); + LMINOR << "Connecting to SAM as '" << destname << "'\n"; samerr_t rc = sam_connect(samhost, samport, destname, SAM_DGRAM, tunneldepth); if (rc == SAM_OK) set_connected(true); @@ -103,7 +104,7 @@ void Sam::connect(const char* samhost, uint16_t samport, const char* destname, */ void Sam::load_peers(void) { - peers = new Peers(PEERS_REF_FILE); + peers = new Peers(config->get_cproperty("references")); } /* diff --git a/apps/enclave/src/sam.hpp b/apps/enclave/src/sam.hpp index a669fb38d..cff2f3f80 100644 --- a/apps/enclave/src/sam.hpp +++ b/apps/enclave/src/sam.hpp @@ -33,7 +33,7 @@ class Sam { public: - Sam(const char* samhost, uint16_t samport, const char* destname, + Sam(const string& samhost, uint16_t samport, const string& destname, uint_t tunneldepth); ~Sam(void);