From 085da0cea748096ee89f9da9a2ab7f28b030e08c Mon Sep 17 00:00:00 2001
From: mpc <mpc>
Date: Thu, 24 Jun 2004 11:47:01 +0000
Subject: [PATCH] Started work on a configuration options object

---
 apps/enclave/Makefile         |  1 +
 apps/enclave/cfg/enclave.cfg  | 30 +++++++++++
 apps/enclave/src/chk.hpp      |  2 +-
 apps/enclave/src/config.cpp   | 93 +++++++++++++++++++++++++++++++++++
 apps/enclave/src/config.hpp   | 52 ++++++++++++++++++++
 apps/enclave/src/main.cpp     |  3 +-
 apps/enclave/src/platform.hpp |  2 +
 7 files changed, 181 insertions(+), 2 deletions(-)
 create mode 100644 apps/enclave/cfg/enclave.cfg
 create mode 100644 apps/enclave/src/config.cpp
 create mode 100644 apps/enclave/src/config.hpp

diff --git a/apps/enclave/Makefile b/apps/enclave/Makefile
index 34b45f0a19..3805927028 100644
--- a/apps/enclave/Makefile
+++ b/apps/enclave/Makefile
@@ -42,6 +42,7 @@ LIBS = -lsam -ltomcrypt
 
 OBJS =	$(OBJDIR)/bigint.o \
 		$(OBJDIR)/chk.o \
+		$(OBJDIR)/config.o \
 		$(OBJDIR)/logger.o \
 		$(OBJDIR)/main.o \
 		$(OBJDIR)/peers.o \
diff --git a/apps/enclave/cfg/enclave.cfg b/apps/enclave/cfg/enclave.cfg
new file mode 100644
index 0000000000..cb118d48e2
--- /dev/null
+++ b/apps/enclave/cfg/enclave.cfg
@@ -0,0 +1,30 @@
+#
+# This is the Enclave configuration file.  Lines starting with # and blank lines
+# are ignored.
+#
+
+# The DNS name or IP address of the SAM server you will be using
+samhost=localhost
+
+# The TCP port the SAM server is listening on
+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
+
+# 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
+
+# Record every log message at or above this priority level
+# debug = 0, minor = 1, info = 2, warn = 3, error = 4
+loglevel=1
+
+# The location of the Enclave log file
+logfile=/home/Administrator/cvs/i2p/apps/enclave/log/enclave.log
diff --git a/apps/enclave/src/chk.hpp b/apps/enclave/src/chk.hpp
index 8767d5ea47..c6f2cfca11 100644
--- a/apps/enclave/src/chk.hpp
+++ b/apps/enclave/src/chk.hpp
@@ -35,7 +35,7 @@ class Chk {
 	public:
 		//Chk(const uchar_t* cypertext, size_t size);
 		Chk(const uchar_t* plaintext, size_t size, const string& mime_type);
-		~Chk(void) { delete ct; }
+		~Chk(void) { delete[] ct; }
 
 	private:
 		static const size_t CRYPT_BLOCK_SIZE = 16;
diff --git a/apps/enclave/src/config.cpp b/apps/enclave/src/config.cpp
new file mode 100644
index 0000000000..5aa9daaffe
--- /dev/null
+++ b/apps/enclave/src/config.cpp
@@ -0,0 +1,93 @@
+/*
+ * Copyright (c) 2004, Matthew P. Cashdollar <mpc@innographx.com>
+ * All rights reserved.
+ * 
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are
+ * met:
+ * 
+ *     * Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ *     * Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ *     * Neither the name of the author nor the names of any contributors
+ * may be used to endorse or promote products derived from this software
+ * without specific prior written permission.
+ * 
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
+ * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
+ * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
+ * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
+ * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
+ * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
+ * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
+ * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
+ * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
+ * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
+ * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#include "platform.hpp"
+#include "bigint.hpp"
+
+Config::Config(const string& file)
+	: file(file)
+{
+	set_defaults();
+	parse();
+	configf.close();
+}
+
+void Config::parse(void)
+{
+	configf.open(file.c_str());
+	if (!configf) {
+		cerr << "Error opening configuration file (" << file.c_str() << ")\n";
+		throw runtime_error("Error opening configuration file");
+	}
+	size_t line = 0;
+	string s;
+	for (getline(configf, s); configf; getline(configf, s)) {
+		line++;
+		if (s[0] == '#')  // comment
+			continue;
+		size_t eqpos = s.find("=");
+		if (eqpos == string::npos) {
+			LERROR << "Error parsing line #" << line << " in " << file << ": " << s << '\n';
+			continue;
+		}
+		string key = s.substr(0, eqpos - 1);
+		string value = s.substr(eqpos + 1);
+		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
+ */
+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("tunneldepth", "2"));
+	cfgmap.insert(make_pair("references", "cfg/peers.ref"));
+	cfgmap.insert(make_pair("loglevel", "1"));
+	cfgmap.insert(make_pair("logfile", "log/enclave.log"));
+}
diff --git a/apps/enclave/src/config.hpp b/apps/enclave/src/config.hpp
new file mode 100644
index 0000000000..6c207600d9
--- /dev/null
+++ b/apps/enclave/src/config.hpp
@@ -0,0 +1,52 @@
+/*
+ * Copyright (c) 2004, Matthew P. Cashdollar <mpc@innographx.com>
+ * All rights reserved.
+ * 
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are
+ * met:
+ * 
+ *     * Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ *     * Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ *     * Neither the name of the author nor the names of any contributors
+ * may be used to endorse or promote products derived from this software
+ * without specific prior written permission.
+ * 
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
+ * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
+ * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
+ * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
+ * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
+ * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
+ * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
+ * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
+ * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
+ * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
+ * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#ifndef CONFIG_HPP
+#define CONFIG_HPP
+
+class Config {
+	public:
+		Config(const string& file);
+
+		const string* get_property(const string& key) const;
+
+	private:
+		typedef map<const string, string>::const_iterator cfgmap_ci;
+		typedef map<const string, string>::iterator cfgmap_i;
+
+		void parse(void);
+		void set_defaults(void);
+
+		ifstream configf;
+		const string file;
+		map<const string, string> cfgmap;
+};
+
+#endif  // CONFIG_HPP
diff --git a/apps/enclave/src/main.cpp b/apps/enclave/src/main.cpp
index 07c1b2c19a..a85f7d1f57 100644
--- a/apps/enclave/src/main.cpp
+++ b/apps/enclave/src/main.cpp
@@ -31,6 +31,7 @@
 #include "platform.hpp"
 #include "main.hpp"
 
+Config *config;  // Configuration options
 Logger logger(LOG_FILE);  // Logging mechanism
 Random prng;  // Random number generator
 Sam *sam;  // SAM connection
@@ -42,7 +43,7 @@ int main(int argc, char* argv[])
 	if (argc != 2) {  // put some getopts stuff in here later
 		LERROR << "Please specify your destination name.  e.g. 'bin/enclave " \
 			"enclave'\n";
-		return -1;
+		return 1;
 	}
 
 	LINFO << "Enclave DHT - Built on " << __DATE__ << ' ' << __TIME__ << '\n';
diff --git a/apps/enclave/src/platform.hpp b/apps/enclave/src/platform.hpp
index 23a9ee0f85..178b88a1a4 100644
--- a/apps/enclave/src/platform.hpp
+++ b/apps/enclave/src/platform.hpp
@@ -77,6 +77,7 @@ using namespace std;
  * Local includes
  */
 #include "logger.hpp"  // Logger
+#include "config.hpp"  // Config
 #include "sam_error.hpp"  // for sam.hpp
 #include "bigint.hpp"  // for sha1.hpp
 #include "sha1.hpp"  // for peers.hpp
@@ -89,6 +90,7 @@ using namespace std;
 /*
  * Global variables
  */
+extern Config *config;  // Configuration options
 extern Logger logger;  // Logging mechanism
 extern Random prng;  // Random number generator
 extern Sam *sam;  // Sam connection
-- 
GitLab