added linuxtage presentation
[anytun.git] / src / anyCtrOptions.cpp
blob6f53a78d3b8adc932697671e8191d3150fd13e90
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 anytun.org <satp@wirdorange.org>
16 * This program is free software; you can redistribute it and/or modify
17 * it under the terms of the GNU General Public License version 2
18 * as published by the Free Software Foundation.
20 * This program is distributed in the hope that it will be useful,
21 * but WITHOUT ANY WARRANTY; without even the implied warranty of
22 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 * GNU General Public License for more details.
25 * You should have received a copy of the GNU General Public License
26 * along with this program (see the file COPYING included with this
27 * distribution); if not, write to the Free Software Foundation, Inc.,
28 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
31 #include <iostream>
32 #include <queue>
33 #include <string>
34 #include <sstream>
36 #include "datatypes.h"
37 #include "anyCtrOptions.h"
39 Options* Options::inst = NULL;
40 Mutex Options::instMutex;
41 Options& gOpt = Options::instance();
43 Options& Options::instance()
45 Lock lock(instMutex);
46 static instanceCleaner c;
47 if(!inst)
48 inst = new Options();
50 return *inst;
53 Options::Options()
55 progname_ = "anytun-controld";
56 file_name_ = "";
57 daemonize_ = true;
58 chroot_ = false;
59 username_ = "nobody";
60 chroot_dir_ = "/var/run/anytun-controld";
61 pid_file_ = "";
62 bind_to_addr_ = "127.0.0.1";
63 bind_to_port_ = 4445;
66 Options::~Options()
70 #define PARSE_BOOL_PARAM(SHORT, LONG, VALUE) \
71 else if(str == SHORT || str == LONG) \
72 VALUE = true;
74 #define PARSE_INVERSE_BOOL_PARAM(SHORT, LONG, VALUE) \
75 else if(str == SHORT || str == LONG) \
76 VALUE = false;
78 #define PARSE_SCALAR_PARAM(SHORT, LONG, VALUE) \
79 else if(str == SHORT || str == LONG) \
80 { \
81 if(argc < 1 || argv[i+1][0] == '-') \
82 return false; \
83 std::stringstream tmp; \
84 tmp << argv[i+1]; \
85 tmp >> VALUE; \
86 argc--; \
87 i++; \
90 #define PARSE_SCALAR_PARAM2(SHORT, LONG, VALUE1, VALUE2) \
91 else if(str == SHORT || str == LONG) \
92 { \
93 if(argc < 2 || \
94 argv[i+1][0] == '-' || argv[i+2][0] == '-') \
95 return false; \
96 std::stringstream tmp; \
97 tmp << argv[i+1] << " " << argv[i+2]; \
98 tmp >> VALUE1; \
99 tmp >> VALUE2; \
100 argc-=2; \
101 i+=2; \
104 #define PARSE_HEXSTRING_PARAM_SEC(SHORT, LONG, VALUE) \
105 else if(str == SHORT || str == LONG) \
107 if(argc < 1 || argv[i+1][0] == '-') \
108 return false; \
109 VALUE = Buffer(std::string(argv[i+1])); \
110 for(size_t j=0; j < strlen(argv[i+1]); ++j) \
111 argv[i+1][j] = '#'; \
112 argc--; \
113 i++; \
116 #define PARSE_CSLIST_PARAM(SHORT, LONG, LIST) \
117 else if(str == SHORT || str == LONG) \
119 if(argc < 1 || argv[i+1][0] == '-') \
120 return false; \
121 std::stringstream tmp(argv[i+1]); \
122 while (tmp.good()) \
124 std::string tmp_line; \
125 getline(tmp,tmp_line,','); \
126 LIST.push(tmp_line); \
128 argc--; \
129 i++; \
132 bool Options::parse(int argc, char* argv[])
134 Lock lock(mutex);
136 progname_ = argv[0];
137 argc--;
139 std::string control_host("");
140 for(int i=1; argc > 0; ++i)
142 std::string str(argv[i]);
143 argc--;
145 if(str == "-h" || str == "--help")
146 return false;
147 PARSE_SCALAR_PARAM("-f","--file", file_name_)
148 PARSE_INVERSE_BOOL_PARAM("-D","--nodaemonize", daemonize_)
149 PARSE_BOOL_PARAM("-C","--chroot", chroot_)
150 PARSE_SCALAR_PARAM("-u","--username", username_)
151 PARSE_SCALAR_PARAM("-H","--chroot-dir", chroot_dir_)
152 PARSE_SCALAR_PARAM("-P","--write-pid", pid_file_)
153 PARSE_SCALAR_PARAM("-X","--control-host", control_host)
154 else
155 return false;
158 if(control_host != "") {
159 std::stringstream tmp_stream(control_host);
160 getline(tmp_stream,bind_to_addr_,':');
161 if(!tmp_stream.good())
162 return false;
163 tmp_stream >> bind_to_port_;
166 return true;
169 void Options::printUsage()
171 std::cout << "USAGE:" << std::endl;
172 std::cout << "anytun-controld [-h|--help] prints this..." << std::endl;
173 std::cout << " [-D|--nodaemonize] don't run in background" << std::endl;
174 std::cout << " [-C|--chroot] chroot and drop privileges" << std::endl;
175 std::cout << " [-u|--username] <username> if chroot change to this user" << std::endl;
176 std::cout << " [-H|--chroot-dir] <path> chroot to this directory" << std::endl;
177 std::cout << " [-P|--write-pid] <path> write pid to this file" << std::endl;
178 std::cout << " [-f|--file] <path> path to file" << std::endl;
179 std::cout << " [-X|--control-host] <host:port> local tcp port to bind to" << std::endl;
183 void Options::printOptions()
185 Lock lock(mutex);
186 std::cout << "Options:" << std::endl;
187 std::cout << "daemonize=" << daemonize_ << std::endl;
188 std::cout << "chroot=" << chroot_ << std::endl;
189 std::cout << "username='" << username_ << "'" << std::endl;
190 std::cout << "chroot_dir='" << chroot_dir_ << "'" << std::endl;
191 std::cout << "pid_file='" << pid_file_ << "'" << std::endl;
194 std::string Options::getProgname()
196 Lock lock(mutex);
197 return progname_;
201 Options& Options::setProgname(std::string p)
203 Lock lock(mutex);
204 progname_ = p;
205 return *this;
208 bool Options::getDaemonize()
210 return daemonize_;
213 Options& Options::setDaemonize(bool d)
215 daemonize_ = d;
216 return *this;
219 bool Options::getChroot()
221 return chroot_;
224 Options& Options::setChroot(bool c)
226 chroot_ = c;
227 return *this;
230 std::string Options::getUsername()
232 Lock lock(mutex);
233 return username_;
236 Options& Options::setUsername(std::string u)
238 Lock lock(mutex);
239 username_ = u;
240 return *this;
243 std::string Options::getChrootDir()
245 Lock lock(mutex);
246 return chroot_dir_;
249 Options& Options::setChrootDir(std::string c)
251 Lock lock(mutex);
252 chroot_dir_ = c;
253 return *this;
256 std::string Options::getPidFile()
258 Lock lock(mutex);
259 return pid_file_;
262 Options& Options::setPidFile(std::string p)
264 Lock lock(mutex);
265 pid_file_ = p;
266 return *this;
269 std::string Options::getFileName()
271 Lock lock(mutex);
272 return file_name_;
275 Options& Options::setFileName(std::string f)
277 Lock lock(mutex);
278 file_name_ = f;
279 return *this;
282 std::string Options::getBindToAddr()
284 Lock lock(mutex);
285 return bind_to_addr_;
288 Options& Options::setBindToAddr(std::string b)
290 Lock lock(mutex);
291 bind_to_addr_ = b;
292 return *this;
295 uint16_t Options::getBindToPort()
297 return bind_to_port_;
300 Options& Options::setBindToPort(uint16_t b)
302 bind_to_port_ = b;
303 return *this;