small cleanup
[anytun.git] / src / anyCtrOptions.cpp
blob7543206cda8d6dcd9f62dbb4b4c4faad912e5ac0
1 /*
2 * anytun
4 * The secure anycast tunneling protocol (satp) defines a protocol used
5 * for communication between any combination of unicast and anycast
6 * tunnel endpoints. It has less protocol overhead than IPSec in Tunnel
7 * mode and allows tunneling of every ETHER TYPE protocol (e.g.
8 * ethernet, ip, arp ...). satp directly includes cryptography and
9 * message authentication based on the methodes used by SRTP. It is
10 * intended to deliver a generic, scaleable and secure solution for
11 * tunneling and relaying of packets of any protocol.
14 * Copyright (C) 2007-2008 Othmar Gsenger, Erwin Nindl,
15 * Christian Pointner <satp@wirdorange.org>
17 * This file is part of Anytun.
19 * Anytun is free software: you can redistribute it and/or modify
20 * it under the terms of the GNU General Public License version 3 as
21 * published by the Free Software Foundation.
23 * Anytun is distributed in the hope that it will be useful,
24 * but WITHOUT ANY WARRANTY; without even the implied warranty of
25 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
26 * GNU General Public License for more details.
28 * You should have received a copy of the GNU General Public License
29 * along with anytun. If not, see <http://www.gnu.org/licenses/>.
32 #include <iostream>
33 #include <queue>
34 #include <string>
35 #include <sstream>
37 #include "datatypes.h"
38 #include "anyCtrOptions.h"
40 Options* Options::inst = NULL;
41 Mutex Options::instMutex;
42 Options& gOpt = Options::instance();
44 Options& Options::instance()
46 Lock lock(instMutex);
47 static instanceCleaner c;
48 if(!inst)
49 inst = new Options();
51 return *inst;
54 Options::Options()
56 progname_ = "anytun-controld";
57 file_name_ = "";
58 daemonize_ = true;
59 chroot_ = false;
60 username_ = "nobody";
61 chroot_dir_ = "/var/run/anytun-controld";
62 pid_file_ = "";
63 bind_to_addr_ = "127.0.0.1";
64 bind_to_port_ = 4445;
67 Options::~Options()
71 #define PARSE_BOOL_PARAM(SHORT, LONG, VALUE) \
72 else if(str == SHORT || str == LONG) \
73 VALUE = true;
75 #define PARSE_INVERSE_BOOL_PARAM(SHORT, LONG, VALUE) \
76 else if(str == SHORT || str == LONG) \
77 VALUE = false;
79 #define PARSE_SCALAR_PARAM(SHORT, LONG, VALUE) \
80 else if(str == SHORT || str == LONG) \
81 { \
82 if(argc < 1 || argv[i+1][0] == '-') \
83 return false; \
84 std::stringstream tmp; \
85 tmp << argv[i+1]; \
86 tmp >> VALUE; \
87 argc--; \
88 i++; \
91 #define PARSE_SCALAR_PARAM2(SHORT, LONG, VALUE1, VALUE2) \
92 else if(str == SHORT || str == LONG) \
93 { \
94 if(argc < 2 || \
95 argv[i+1][0] == '-' || argv[i+2][0] == '-') \
96 return false; \
97 std::stringstream tmp; \
98 tmp << argv[i+1] << " " << argv[i+2]; \
99 tmp >> VALUE1; \
100 tmp >> VALUE2; \
101 argc-=2; \
102 i+=2; \
105 #define PARSE_HEXSTRING_PARAM_SEC(SHORT, LONG, VALUE) \
106 else if(str == SHORT || str == LONG) \
108 if(argc < 1 || argv[i+1][0] == '-') \
109 return false; \
110 VALUE = Buffer(std::string(argv[i+1])); \
111 for(size_t j=0; j < strlen(argv[i+1]); ++j) \
112 argv[i+1][j] = '#'; \
113 argc--; \
114 i++; \
117 #define PARSE_CSLIST_PARAM(SHORT, LONG, LIST) \
118 else if(str == SHORT || str == LONG) \
120 if(argc < 1 || argv[i+1][0] == '-') \
121 return false; \
122 std::stringstream tmp(argv[i+1]); \
123 while (tmp.good()) \
125 std::string tmp_line; \
126 getline(tmp,tmp_line,','); \
127 LIST.push(tmp_line); \
129 argc--; \
130 i++; \
133 bool Options::parse(int argc, char* argv[])
135 Lock lock(mutex);
137 progname_ = argv[0];
138 argc--;
140 std::string control_host("");
141 for(int i=1; argc > 0; ++i)
143 std::string str(argv[i]);
144 argc--;
146 if(str == "-h" || str == "--help")
147 return false;
148 PARSE_SCALAR_PARAM("-f","--file", file_name_)
149 PARSE_INVERSE_BOOL_PARAM("-D","--nodaemonize", daemonize_)
150 PARSE_BOOL_PARAM("-C","--chroot", chroot_)
151 PARSE_SCALAR_PARAM("-u","--username", username_)
152 PARSE_SCALAR_PARAM("-H","--chroot-dir", chroot_dir_)
153 PARSE_SCALAR_PARAM("-P","--write-pid", pid_file_)
154 PARSE_SCALAR_PARAM("-X","--control-host", control_host)
155 else
156 return false;
159 if(control_host != "") {
160 std::stringstream tmp_stream(control_host);
161 getline(tmp_stream,bind_to_addr_,':');
162 if(!tmp_stream.good())
163 return false;
164 tmp_stream >> bind_to_port_;
167 return true;
170 void Options::printUsage()
172 std::cout << "USAGE:" << std::endl;
173 std::cout << "anytun-controld [-h|--help] prints this..." << std::endl;
174 std::cout << " [-D|--nodaemonize] don't run in background" << std::endl;
175 std::cout << " [-C|--chroot] chroot and drop privileges" << std::endl;
176 std::cout << " [-u|--username] <username> if chroot change to this user" << std::endl;
177 std::cout << " [-H|--chroot-dir] <path> chroot to this directory" << std::endl;
178 std::cout << " [-P|--write-pid] <path> write pid to this file" << std::endl;
179 std::cout << " [-f|--file] <path> path to file" << std::endl;
180 std::cout << " [-X|--control-host] <host:port> local tcp port to bind to" << std::endl;
184 void Options::printOptions()
186 Lock lock(mutex);
187 std::cout << "Options:" << std::endl;
188 std::cout << "daemonize=" << daemonize_ << std::endl;
189 std::cout << "chroot=" << chroot_ << std::endl;
190 std::cout << "username='" << username_ << "'" << std::endl;
191 std::cout << "chroot_dir='" << chroot_dir_ << "'" << std::endl;
192 std::cout << "pid_file='" << pid_file_ << "'" << std::endl;
195 std::string Options::getProgname()
197 Lock lock(mutex);
198 return progname_;
202 Options& Options::setProgname(std::string p)
204 Lock lock(mutex);
205 progname_ = p;
206 return *this;
209 bool Options::getDaemonize()
211 return daemonize_;
214 Options& Options::setDaemonize(bool d)
216 daemonize_ = d;
217 return *this;
220 bool Options::getChroot()
222 return chroot_;
225 Options& Options::setChroot(bool c)
227 chroot_ = c;
228 return *this;
231 std::string Options::getUsername()
233 Lock lock(mutex);
234 return username_;
237 Options& Options::setUsername(std::string u)
239 Lock lock(mutex);
240 username_ = u;
241 return *this;
244 std::string Options::getChrootDir()
246 Lock lock(mutex);
247 return chroot_dir_;
250 Options& Options::setChrootDir(std::string c)
252 Lock lock(mutex);
253 chroot_dir_ = c;
254 return *this;
257 std::string Options::getPidFile()
259 Lock lock(mutex);
260 return pid_file_;
263 Options& Options::setPidFile(std::string p)
265 Lock lock(mutex);
266 pid_file_ = p;
267 return *this;
270 std::string Options::getFileName()
272 Lock lock(mutex);
273 return file_name_;
276 Options& Options::setFileName(std::string f)
278 Lock lock(mutex);
279 file_name_ = f;
280 return *this;
283 std::string Options::getBindToAddr()
285 Lock lock(mutex);
286 return bind_to_addr_;
289 Options& Options::setBindToAddr(std::string b)
291 Lock lock(mutex);
292 bind_to_addr_ = b;
293 return *this;
296 uint16_t Options::getBindToPort()
298 return bind_to_port_;
301 Options& Options::setBindToPort(uint16_t b)
303 bind_to_port_ = b;
304 return *this;