added endian.h
[anytun.git] / src / anyCtrOptions.cpp
blob5626aec5851d98e28e876f368f66036759350d47
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_ = "2323";
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 return splitAndSetHostPort(control_host);
162 return true;
165 bool Options::splitAndSetHostPort(std::string hostPort)
167 if(hostPort.length() >= 2 && hostPort[0] == ':' && hostPort[1] != ':') {
168 bind_to_addr_ = "";
169 hostPort.erase(0,1);
170 std::stringstream tmp_stream(hostPort);
171 tmp_stream >> bind_to_port_;
172 return true;
175 size_t pos = hostPort.find_first_of("[");
177 if(pos != std::string::npos && pos != 0)
178 return false; // an [ was found but not at the beginning
180 bool hasPort = false;
181 if(pos != std::string::npos) {
182 hostPort.erase(pos, 1);
183 pos = hostPort.find_first_of("]");
185 if(pos == std::string::npos)
186 return false; // no trailing ] although an leading [ was found
188 if(pos < hostPort.length()-2) {
190 if(hostPort[pos+1] != ':')
191 return false; // wrong port delimieter
193 hostPort[pos+1] = '/';
194 hasPort = true;
196 else if(pos != hostPort.length()-1)
197 return false; // to few characters left
199 hostPort.erase(pos, 1);
201 else {
202 pos = hostPort.find_first_of(":");
203 if(pos != std::string::npos && pos == hostPort.find_last_of(":")) {
204 // an ':' has been found and it is the only one -> assuming port present
205 hasPort = true;
206 hostPort[pos] = '/';
210 if(hasPort) {
211 std::stringstream tmp_stream(hostPort);
213 getline(tmp_stream, bind_to_addr_, '/');
214 if(!tmp_stream.good())
215 return false;
217 tmp_stream >> bind_to_port_;
219 else {
220 bind_to_addr_ = hostPort;
221 bind_to_port_ = "2323"; // default sync port
224 return true;
227 void Options::printUsage()
229 std::cout << "USAGE:" << std::endl;
230 std::cout << "anytun-controld [-h|--help] prints this..." << std::endl;
231 std::cout << " [-D|--nodaemonize] don't run in background" << std::endl;
232 std::cout << " [-C|--chroot] chroot and drop privileges" << std::endl;
233 std::cout << " [-u|--username] <username> if chroot change to this user" << std::endl;
234 std::cout << " [-H|--chroot-dir] <path> chroot to this directory" << std::endl;
235 std::cout << " [-P|--write-pid] <path> write pid to this file" << std::endl;
236 std::cout << " [-f|--file] <path> path to file" << std::endl;
237 std::cout << " [-X|--control-host] < <hostname|ip>[:<port>] | :<port> >" << std::endl;
238 std::cout << " local tcp port and or ip address to bind to" << std::endl;
241 void Options::printOptions()
243 Lock lock(mutex);
244 std::cout << "Options:" << std::endl;
245 std::cout << "daemonize=" << daemonize_ << std::endl;
246 std::cout << "chroot=" << chroot_ << std::endl;
247 std::cout << "username='" << username_ << "'" << std::endl;
248 std::cout << "chroot_dir='" << chroot_dir_ << "'" << std::endl;
249 std::cout << "pid_file='" << pid_file_ << "'" << std::endl;
250 std::cout << "bind_to_addr_='" << bind_to_addr_ << "'" << std::endl;
251 std::cout << "bind_to_port_='" << bind_to_port_ << "'" << std::endl;
254 std::string Options::getProgname()
256 Lock lock(mutex);
257 return progname_;
261 Options& Options::setProgname(std::string p)
263 Lock lock(mutex);
264 progname_ = p;
265 return *this;
268 bool Options::getDaemonize()
270 return daemonize_;
273 Options& Options::setDaemonize(bool d)
275 daemonize_ = d;
276 return *this;
279 bool Options::getChroot()
281 return chroot_;
284 Options& Options::setChroot(bool c)
286 chroot_ = c;
287 return *this;
290 std::string Options::getUsername()
292 Lock lock(mutex);
293 return username_;
296 Options& Options::setUsername(std::string u)
298 Lock lock(mutex);
299 username_ = u;
300 return *this;
303 std::string Options::getChrootDir()
305 Lock lock(mutex);
306 return chroot_dir_;
309 Options& Options::setChrootDir(std::string c)
311 Lock lock(mutex);
312 chroot_dir_ = c;
313 return *this;
316 std::string Options::getPidFile()
318 Lock lock(mutex);
319 return pid_file_;
322 Options& Options::setPidFile(std::string p)
324 Lock lock(mutex);
325 pid_file_ = p;
326 return *this;
329 std::string Options::getFileName()
331 Lock lock(mutex);
332 return file_name_;
335 Options& Options::setFileName(std::string f)
337 Lock lock(mutex);
338 file_name_ = f;
339 return *this;
342 std::string Options::getBindToAddr()
344 Lock lock(mutex);
345 return bind_to_addr_;
348 Options& Options::setBindToAddr(std::string b)
350 Lock lock(mutex);
351 bind_to_addr_ = b;
352 return *this;
355 std::string Options::getBindToPort()
357 return bind_to_port_;
360 Options& Options::setBindToPort(std::string b)
362 bind_to_port_ = b;
363 return *this;