From 5869e779ee108a63b71798b50beee13ec2480d67 Mon Sep 17 00:00:00 2001 From: Alex Borisov Date: Sun, 20 Nov 2011 03:22:49 +0200 Subject: Parsing firewall rules --- client/Config.cpp | 60 +++++++++++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 54 insertions(+), 6 deletions(-) (limited to 'client/Config.cpp') diff --git a/client/Config.cpp b/client/Config.cpp index abf090a..a4a6c07 100644 --- a/client/Config.cpp +++ b/client/Config.cpp @@ -10,6 +10,39 @@ using namespace std; +/* + * Firewall Entry class + */ +void Config::FirewallEntry::Parse(string entry) +{ + Logger::Trace("Parsing firewall string: %s\n", entry.c_str()); + + size_t lp1 = 0, lp2 = 0; + //extract action part + lp2 = entry.find(" ", lp1); + string action = entry.substr(lp1, lp2-lp1); + if (action.compare("block") == 0) + { + block = true; + } + else if (action.compare("allow") == 0) + { + block = false; + } + else + { + Logger::Error("Unknown firewall action! Skipping.\n"); + return; + } + //extract host part + lp1 = lp2 + 1; + host.clear(); + host = entry.substr(lp1); +} + +/* + * Config class + */ Config::Config(): StaticProxySpeedLow(50) { Logger::Info("Parsing config.cfg to determine initial configuration\n"); @@ -240,9 +273,9 @@ void Config::ParseGenericProxies(string data) if (proxies.eof()) break; string entry = str; - ProxyEntryGeneric *proxy = new ProxyEntryGeneric; - proxy->Parse(entry); - genericProxy.push_back(*proxy); + ProxyEntryGeneric proxy; + proxy.Parse(entry); + genericProxy.push_back(proxy); } } @@ -261,15 +294,30 @@ void Config::ParseStaticPorxies(string data) if (proxies.eof()) break; string entry = str; - ProxyEntryStatic *proxy = new ProxyEntryStatic; - proxy->Parse(entry); - staticProxy.push_back(*proxy); + ProxyEntryStatic proxy; + proxy.Parse(entry); + staticProxy.push_back(proxy); } } void Config::ParseFirewalls(string data) { + firewalls.clear(); + stringstream rules(data, ios_base::in); + const int str_size = 512; + char str[str_size] = {0}; + while (! rules.eof()) + { + rules.getline(str, str_size, ';'); + rules.ignore(2, '\n'); + if (rules.eof()) + break; + string entry = str; + FirewallEntry rule; + rule.Parse(entry); + firewalls.push_back(rule); + } } -- cgit v1.2.3