summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorChristian Pointner <equinox@anytun.org>2009-01-17 22:01:56 +0000
committerChristian Pointner <equinox@anytun.org>2009-01-17 22:01:56 +0000
commit8a96f4e8d7df1aff57ed4cbb708fb4a81e00299f (patch)
tree62904ad7476deaf49a29758b020c9962a12c945b /src
parentoptions parser refactoring (diff)
options parser cleanup
Diffstat (limited to 'src')
-rw-r--r--src/options.cpp255
-rw-r--r--src/options.h11
2 files changed, 132 insertions, 134 deletions
diff --git a/src/options.cpp b/src/options.cpp
index 8b76906..40182c4 100644
--- a/src/options.cpp
+++ b/src/options.cpp
@@ -152,12 +152,12 @@ Options::Options() : key_(u_int32_t(0)), salt_(u_int32_t(0))
bind_to_.addr = "127.0.0.1";
bind_to_.port = "2323";
- local_addr_ = "";
- local_port_ = "4444";
- remote_addr_ = "";
- remote_port_ = "4444";
- local_sync_addr_ = "";
- local_sync_port_ = "";
+ local_.addr = "";
+ local_.port = "4444";
+ remote_.addr = "";
+ remote_.port = "4444";
+ local_sync_.addr = "";
+ local_sync_.port = "";
dev_name_ = "";
dev_type_ = "";
@@ -254,7 +254,7 @@ Options::~Options()
i++; \
}
-#define PARSE_CSLIST_PARAM(SHORT, LONG, LIST) \
+#define PARSE_CSLIST_PARAM(SHORT, LONG, LIST, TYPE) \
else if(str == SHORT || str == LONG) \
{ \
if(argc < 1) \
@@ -268,7 +268,7 @@ Options::~Options()
{ \
std::string tmp_line; \
getline(tmp,tmp_line,','); \
- LIST.push(tmp_line); \
+ LIST.push_back(TYPE(tmp_line)); \
} \
argc--; \
i++; \
@@ -276,13 +276,11 @@ Options::~Options()
bool Options::parse(int argc, char* argv[])
{
- Lock lock(mutex);
+ WritersLock lock(mutex);
progname_ = argv[0];
argc--;
- std::queue<std::string> host_port_queue;
int32_t ld_kdr_tmp = ld_kdr_;
- std::queue<std::string> route_queue;
for(int i=1; argc > 0; ++i)
{
std::string str(argv[i]);
@@ -299,20 +297,20 @@ bool Options::parse(int argc, char* argv[])
// PARSE_SCALAR_PARAM("-f","--file", file_name_)
// PARSE_SCALAR_PARAM("-X","--control-host", bind_to_)
- PARSE_SCALAR_PARAM("-i","--interface", local_addr_)
- PARSE_SCALAR_PARAM("-p","--port", local_port_)
- PARSE_SCALAR_PARAM("-r","--remote-host", remote_addr_)
- PARSE_SCALAR_PARAM("-o","--remote-port", remote_port_)
- PARSE_SCALAR_PARAM("-I","--sync-interface", local_sync_addr_)
- PARSE_SCALAR_PARAM("-S","--sync-port", local_sync_port_)
- PARSE_CSLIST_PARAM("-M","--sync-hosts", host_port_queue)
- PARSE_CSLIST_PARAM("-X","--control-host", host_port_queue)
+ PARSE_SCALAR_PARAM("-i","--interface", local_.addr)
+ PARSE_SCALAR_PARAM("-p","--port", local_.port)
+ PARSE_SCALAR_PARAM("-r","--remote-host", remote_.addr)
+ PARSE_SCALAR_PARAM("-o","--remote-port", remote_.port)
+ PARSE_SCALAR_PARAM("-I","--sync-interface", local_sync_.addr)
+ PARSE_SCALAR_PARAM("-S","--sync-port", local_sync_.port)
+ PARSE_CSLIST_PARAM("-M","--sync-hosts", remote_sync_hosts_, OptionHost)
+ PARSE_CSLIST_PARAM("-X","--control-host", remote_sync_hosts_, OptionHost)
PARSE_SCALAR_PARAM("-d","--dev", dev_name_)
PARSE_SCALAR_PARAM("-t","--type", dev_type_)
PARSE_SCALAR_PARAM2("-n","--ifconfig", ifconfig_param_local_, ifconfig_param_remote_netmask_)
PARSE_SCALAR_PARAM("-x","--post-up-script", post_up_script_)
- PARSE_CSLIST_PARAM("-R","--route", route_queue)
+ PARSE_CSLIST_PARAM("-R","--route", routes_, OptionRoute)
PARSE_SCALAR_PARAM("-s","--sender-id", sender_id_)
PARSE_SCALAR_PARAM("-m","--mux", mux_)
@@ -327,7 +325,6 @@ bool Options::parse(int argc, char* argv[])
else
throw syntax_error(str, 0);
}
-
ld_kdr_ = static_cast<int8_t>(ld_kdr_tmp);
if(cipher_ == "null" && auth_algo_ == "null")
@@ -338,18 +335,6 @@ bool Options::parse(int argc, char* argv[])
if(dev_name_ == "" && dev_type_ == "")
dev_type_ = "tun";
- while(!host_port_queue.empty()) {
- OptionHost sh(host_port_queue.front());
- remote_sync_hosts_.push_back(sh);
- host_port_queue.pop();
- }
-
- while(!route_queue.empty()) {
- OptionRoute rt(route_queue.front());
- routes_.push_back(rt);
- route_queue.pop();
- }
-
return true;
}
@@ -367,7 +352,7 @@ void Options::printUsage()
// std::cout << " [-f|--file] <path> path to input file" << std::endl;
// std::cout << " [-X|--control-host] < <hostname|ip>[:<port>] | :<port> >" << std::endl;
-// std::cout << " local tcp port and or ip address to bind to" << std::endl;
+// std::cout << " local tcp port and or ip address to bind to" << std::endl;
std::cout << " [-i|--interface] <hostname|ip> local anycast ip address to bind to" << std::endl;
std::cout << " [-p|--port] <port> local anycast(data) port to bind to" << std::endl;
@@ -401,125 +386,129 @@ void Options::printUsage()
void Options::printOptions()
{
- Lock lock(mutex);
+ ReadersLock lock(mutex);
+
std::cout << "Options:" << std::endl;
std::cout << std::endl;
- std::cout << "daemonize=" << daemonize_ << std::endl;
- std::cout << "chroot=" << chroot_ << std::endl;
- std::cout << "username='" << username_ << "'" << std::endl;
- std::cout << "chroot_dir='" << chroot_dir_ << "'" << std::endl;
- std::cout << "pid_file='" << pid_file_ << "'" << std::endl;
+ std::cout << "daemonize = " << daemonize_ << std::endl;
+ std::cout << "chroot = " << chroot_ << std::endl;
+ std::cout << "username = '" << username_ << "'" << std::endl;
+ std::cout << "chroot_dir = '" << chroot_dir_ << "'" << std::endl;
+ std::cout << "pid_file = '" << pid_file_ << "'" << std::endl;
std::cout << std::endl;
-// std::cout << "file_name='" << file_name_ << "'" << std::endl;
-// std::cout << "bind_to.addr='" << bind_to_.addr << "'" << std::endl;
-// std::cout << "bind_to.port='" << bind_to_.port << "'" << std::endl;
+// std::cout << "file_name = '" << file_name_ << "'" << std::endl;
+// std::cout << "bind_to.addr = '" << bind_to_.addr << "'" << std::endl;
+// std::cout << "bind_to.port = '" << bind_to_.port << "'" << std::endl;
// std::cout << std::endl;
- std::cout << "local_addr='" << local_addr_ << "'" << std::endl;
- std::cout << "local_port='" << local_port_ << "'" << std::endl;
- std::cout << "remote_addr='" << remote_addr_ << "'" << std::endl;
- std::cout << "remote_port='" << remote_port_ << "'" << std::endl;
- std::cout << "local_sync_addr='" << local_sync_addr_ << "'" << std::endl;
- std::cout << "local_sync_port='" << local_sync_port_ << "'" << std::endl;
- std::cout << "remote_sync_hosts=";
+ std::cout << "local.addr = '" << local_.addr << "'" << std::endl;
+ std::cout << "local.port = '" << local_.port << "'" << std::endl;
+ std::cout << "remote.addr = '" << remote_.addr << "'" << std::endl;
+ std::cout << "remote.port = '" << remote_.port << "'" << std::endl;
+ std::cout << "local_sync.addr = '" << local_sync_.addr << "'" << std::endl;
+ std::cout << "local_sync.port = '" << local_sync_.port << "'" << std::endl;
+ std::cout << "remote_sync_hosts:" << std::endl;
HostList::const_iterator it = remote_sync_hosts_.begin();
for(; it != remote_sync_hosts_.end(); ++it)
- std::cout << "'" << it->addr << "','" << it->port << "'; ";
- std::cout << std::endl;
+ std::cout << " '" << it->addr << "','" << it->port << "'" << std::endl;
std::cout << std::endl;
- std::cout << "dev_name='" << dev_name_ << "'" << std::endl;
- std::cout << "dev_type='" << dev_type_ << "'" << std::endl;
- std::cout << "ifconfig_param_local='" << ifconfig_param_local_ << "'" << std::endl;
- std::cout << "ifconfig_param_remote_netmask='" << ifconfig_param_remote_netmask_ << "'" << std::endl;
- std::cout << "post_up_script='" << post_up_script_ << "'" << std::endl;
+ std::cout << "dev_name = '" << dev_name_ << "'" << std::endl;
+ std::cout << "dev_type = '" << dev_type_ << "'" << std::endl;
+ std::cout << "ifconfig_param_local = '" << ifconfig_param_local_ << "'" << std::endl;
+ std::cout << "ifconfig_param_remote_netmask = '" << ifconfig_param_remote_netmask_ << "'" << std::endl;
+ std::cout << "post_up_script = '" << post_up_script_ << "'" << std::endl;
std::cout << "routes:" << std::endl;
RouteList::const_iterator rit;
for(rit = routes_.begin(); rit != routes_.end(); ++rit)
std::cout << " " << rit->net_addr << "/" << rit->prefix_length << std::endl;
std::cout << std::endl;
- std::cout << "sender_id='" << sender_id_ << "'" << std::endl;
- std::cout << "mux_id=" << mux_ << std::endl;
- std::cout << "seq_window_size='" << seq_window_size_ << "'" << std::endl;
+ std::cout << "sender_id = '" << sender_id_ << "'" << std::endl;
+ std::cout << "mux_id = " << mux_ << std::endl;
+ std::cout << "seq_window_size = '" << seq_window_size_ << "'" << std::endl;
std::cout << std::endl;
- std::cout << "cipher='" << cipher_ << "'" << std::endl;
- std::cout << "auth_algo='" << auth_algo_ << "'" << std::endl;
- std::cout << "kd_prf='" << kd_prf_ << "'" << std::endl;
- std::cout << "ld_kdr=" << static_cast<int32_t>(ld_kdr_) << std::endl;
- std::cout << "key=" << key_.getHexDumpOneLine() << std::endl;
- std::cout << "salt=" << salt_.getHexDumpOneLine() << std::endl;
+ std::cout << "cipher = '" << cipher_ << "'" << std::endl;
+ std::cout << "auth_algo = '" << auth_algo_ << "'" << std::endl;
+ std::cout << "kd_prf = '" << kd_prf_ << "'" << std::endl;
+ std::cout << "ld_kdr = " << static_cast<int32_t>(ld_kdr_) << std::endl;
+ std::cout << "key = " << key_.getHexDumpOneLine() << std::endl;
+ std::cout << "salt = " << salt_.getHexDumpOneLine() << std::endl;
}
std::string Options::getProgname()
{
- Lock lock(mutex);
+ ReadersLock lock(mutex);
return progname_;
}
Options& Options::setProgname(std::string p)
{
- Lock lock(mutex);
+ WritersLock lock(mutex);
progname_ = p;
return *this;
}
bool Options::getDaemonize()
{
+ ReadersLock lock(mutex);
return daemonize_;
}
Options& Options::setDaemonize(bool d)
{
+ WritersLock lock(mutex);
daemonize_ = d;
return *this;
}
bool Options::getChroot()
{
+ ReadersLock lock(mutex);
return chroot_;
}
Options& Options::setChroot(bool c)
{
+ WritersLock lock(mutex);
chroot_ = c;
return *this;
}
std::string Options::getUsername()
{
- Lock lock(mutex);
+ ReadersLock lock(mutex);
return username_;
}
Options& Options::setUsername(std::string u)
{
- Lock lock(mutex);
+ WritersLock lock(mutex);
username_ = u;
return *this;
}
std::string Options::getChrootDir()
{
- Lock lock(mutex);
+ ReadersLock lock(mutex);
return chroot_dir_;
}
Options& Options::setChrootDir(std::string c)
{
- Lock lock(mutex);
+ WritersLock lock(mutex);
chroot_dir_ = c;
return *this;
}
std::string Options::getPidFile()
{
- Lock lock(mutex);
+ ReadersLock lock(mutex);
return pid_file_;
}
Options& Options::setPidFile(std::string p)
{
- Lock lock(mutex);
+ WritersLock lock(mutex);
pid_file_ = p;
return *this;
}
@@ -528,37 +517,39 @@ Options& Options::setPidFile(std::string p)
std::string Options::getFileName()
{
- Lock lock(mutex);
+ ReadersLock lock(mutex);
return file_name_;
}
Options& Options::setFileName(std::string f)
{
- Lock lock(mutex);
+ WritersLock lock(mutex);
file_name_ = f;
return *this;
}
std::string Options::getBindToAddr()
{
- Lock lock(mutex);
+ ReadersLock lock(mutex);
return bind_to_.addr;
}
Options& Options::setBindToAddr(std::string b)
{
- Lock lock(mutex);
+ WritersLock lock(mutex);
bind_to_.addr = b;
return *this;
}
std::string Options::getBindToPort()
{
+ ReadersLock lock(mutex);
return bind_to_.port;
}
Options& Options::setBindToPort(std::string b)
{
+ WritersLock lock(mutex);
bind_to_.port = b;
return *this;
}
@@ -567,79 +558,85 @@ Options& Options::setBindToPort(std::string b)
std::string Options::getLocalAddr()
{
- Lock lock(mutex);
- return local_addr_;
+ ReadersLock lock(mutex);
+ return local_.addr;
}
Options& Options::setLocalAddr(std::string l)
{
- Lock lock(mutex);
- local_addr_ = l;
+ WritersLock lock(mutex);
+ local_.addr = l;
return *this;
}
std::string Options::getLocalPort()
{
- return local_port_;
+ ReadersLock lock(mutex);
+ return local_.port;
}
Options& Options::setLocalPort(std::string l)
{
- local_port_ = l;
+ WritersLock lock(mutex);
+ local_.port = l;
return *this;
}
std::string Options::getRemoteAddr()
{
- Lock lock(mutex);
- return remote_addr_;
+ ReadersLock lock(mutex);
+ return remote_.addr;
}
Options& Options::setRemoteAddr(std::string r)
{
- Lock lock(mutex);
- remote_addr_ = r;
+ WritersLock lock(mutex);
+ remote_.addr = r;
return *this;
}
std::string Options::getRemotePort()
{
- return remote_port_;
+ ReadersLock lock(mutex);
+ return remote_.port;
}
Options& Options::setRemotePort(std::string r)
{
- remote_port_ = r;
+ WritersLock lock(mutex);
+ remote_.port = r;
return *this;
}
std::string Options::getLocalSyncAddr()
{
- Lock lock(mutex);
- return local_sync_addr_;
+ ReadersLock lock(mutex);
+ return local_sync_.addr;
}
Options& Options::setLocalSyncAddr(std::string l)
{
- Lock lock(mutex);
- local_sync_addr_ = l;
+ WritersLock lock(mutex);
+ local_sync_.addr = l;
return *this;
}
std::string Options::getLocalSyncPort()
{
- return local_sync_port_;
+ ReadersLock lock(mutex);
+ return local_sync_.port;
}
Options& Options::setLocalSyncPort(std::string l)
{
- local_sync_port_ = l;
+ WritersLock lock(mutex);
+ local_sync_.port = l;
return *this;
}
HostList Options::getRemoteSyncHosts()
{
- Lock lock(mutex);
+ ReadersLock lock(mutex);
return remote_sync_hosts_;
}
@@ -647,72 +644,72 @@ HostList Options::getRemoteSyncHosts()
std::string Options::getDevName()
{
- Lock lock(mutex);
+ ReadersLock lock(mutex);
return dev_name_;
}
-std::string Options::getDevType()
-{
- Lock lock(mutex);
- return dev_type_;
-}
-
Options& Options::setDevName(std::string d)
{
- Lock lock(mutex);
+ WritersLock lock(mutex);
dev_name_ = d;
return *this;
}
+std::string Options::getDevType()
+{
+ ReadersLock lock(mutex);
+ return dev_type_;
+}
+
Options& Options::setDevType(std::string d)
{
- Lock lock(mutex);
+ WritersLock lock(mutex);
dev_type_ = d;
return *this;
}
std::string Options::getIfconfigParamLocal()
{
- Lock lock(mutex);
+ ReadersLock lock(mutex);
return ifconfig_param_local_;
}
Options& Options::setIfconfigParamLocal(std::string i)
{
- Lock lock(mutex);
+ WritersLock lock(mutex);
ifconfig_param_local_ = i;
return *this;
}
std::string Options::getIfconfigParamRemoteNetmask()
{
- Lock lock(mutex);
+ ReadersLock lock(mutex);
return ifconfig_param_remote_netmask_;
}
Options& Options::setIfconfigParamRemoteNetmask(std::string i)
{
- Lock lock(mutex);
+ WritersLock lock(mutex);
ifconfig_param_remote_netmask_ = i;
return *this;
}
std::string Options::getPostUpScript()
{
- Lock lock(mutex);
+ ReadersLock lock(mutex);
return post_up_script_;
}
Options& Options::setPostUpScript(std::string p)
{
- Lock lock(mutex);
+ WritersLock lock(mutex);
post_up_script_ = p;
return *this;
}
RouteList Options::getRoutes()
{
- Lock lock(mutex);
+ ReadersLock lock(mutex);
return routes_;
}
@@ -720,35 +717,39 @@ RouteList Options::getRoutes()
sender_id_t Options::getSenderId()
{
+ ReadersLock lock(mutex);
return sender_id_;
}
Options& Options::setSenderId(sender_id_t s)
{
+ WritersLock lock(mutex);
sender_id_ = s;
return *this;
}
mux_t Options::getMux()
{
- Lock lock(mutex);
+ ReadersLock lock(mutex);
return mux_;
}
Options& Options::setMux(mux_t m)
{
- Lock lock(mutex);
+ WritersLock lock(mutex);
mux_ = m;
return *this;
}
window_size_t Options::getSeqWindowSize()
{
+ ReadersLock lock(mutex);
return seq_window_size_;
}
Options& Options::setSeqWindowSize(window_size_t s)
{
+ WritersLock lock(mutex);
seq_window_size_ = s;
return *this;
}
@@ -757,78 +758,78 @@ Options& Options::setSeqWindowSize(window_size_t s)
std::string Options::getCipher()
{
- Lock lock(mutex);
+ ReadersLock lock(mutex);
return cipher_;
}
Options& Options::setCipher(std::string c)
{
- Lock lock(mutex);
+ WritersLock lock(mutex);
cipher_ = c;
return *this;
}
std::string Options::getAuthAlgo()
{
- Lock lock(mutex);
+ ReadersLock lock(mutex);
return auth_algo_;
}
Options& Options::setAuthAlgo(std::string a)
{
- Lock lock(mutex);
+ WritersLock lock(mutex);
auth_algo_ = a;
return *this;
}
std::string Options::getKdPrf()
{
- Lock lock(mutex);
+ ReadersLock lock(mutex);
return kd_prf_;
}
Options& Options::setKdPrf(std::string k)
{
- Lock lock(mutex);
+ WritersLock lock(mutex);
kd_prf_ = k;
return *this;
}
int8_t Options::getLdKdr()
{
- Lock lock(mutex);
+ ReadersLock lock(mutex);
return ld_kdr_;
}
Options& Options::setLdKdr(int8_t l)
{
- Lock lock(mutex);
+ WritersLock lock(mutex);
ld_kdr_ = l;
return *this;
}
Buffer Options::getKey()
{
- Lock lock(mutex);
+ ReadersLock lock(mutex);
return key_;
}
Options& Options::setKey(std::string k)
{
- Lock lock(mutex);
+ WritersLock lock(mutex);
key_ = k;
return *this;
}
Buffer Options::getSalt()
{
- Lock lock(mutex);
+ ReadersLock lock(mutex);
return salt_;
}
Options& Options::setSalt(std::string s)
{
- Lock lock(mutex);
+ WritersLock lock(mutex);
salt_ = s;
return *this;
}
diff --git a/src/options.h b/src/options.h
index 6cd8b8a..5167878 100644
--- a/src/options.h
+++ b/src/options.h
@@ -168,7 +168,7 @@ private:
};
friend class instanceCleaner;
- ::Mutex mutex;
+ ::SharedMutex mutex;
std::string progname_;
bool daemonize_;
@@ -180,13 +180,10 @@ private:
std::string file_name_;
OptionHost bind_to_;
- std::string local_addr_;
- std::string local_port_;
- std::string remote_addr_;
- std::string remote_port_;
+ OptionHost local_;
+ OptionHost remote_;
- std::string local_sync_addr_;
- std::string local_sync_port_;
+ OptionHost local_sync_;
HostList remote_sync_hosts_;
std::string dev_name_;