added ipv6 routing support
[anytun.git] / src / anyCtrOptions.cpp
blobcd983456922366f613f37a33cd7de431dff83514
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);
202 if(hasPort) {
203 std::stringstream tmp_stream(hostPort);
205 getline(tmp_stream, bind_to_addr_, '/');
206 if(!tmp_stream.good())
207 return false;
209 tmp_stream >> bind_to_port_;
211 else {
212 bind_to_addr_ = hostPort;
213 bind_to_port_ = "2323"; // default sync port
216 return true;
219 void Options::printUsage()
221 std::cout << "USAGE:" << std::endl;
222 std::cout << "anytun-controld [-h|--help] prints this..." << std::endl;
223 std::cout << " [-D|--nodaemonize] don't run in background" << std::endl;
224 std::cout << " [-C|--chroot] chroot and drop privileges" << std::endl;
225 std::cout << " [-u|--username] <username> if chroot change to this user" << std::endl;
226 std::cout << " [-H|--chroot-dir] <path> chroot to this directory" << std::endl;
227 std::cout << " [-P|--write-pid] <path> write pid to this file" << std::endl;
228 std::cout << " [-f|--file] <path> path to file" << std::endl;
229 std::cout << " [-X|--control-host] < <hostname|ip>[:<port>] | :<port> >" << std::endl;
230 std::cout << " local tcp port and or ip address to bind to" << std::endl;
233 void Options::printOptions()
235 Lock lock(mutex);
236 std::cout << "Options:" << std::endl;
237 std::cout << "daemonize=" << daemonize_ << std::endl;
238 std::cout << "chroot=" << chroot_ << std::endl;
239 std::cout << "username='" << username_ << "'" << std::endl;
240 std::cout << "chroot_dir='" << chroot_dir_ << "'" << std::endl;
241 std::cout << "pid_file='" << pid_file_ << "'" << std::endl;
242 std::cout << "bind_to_addr_='" << bind_to_addr_ << "'" << std::endl;
243 std::cout << "bind_to_port_='" << bind_to_port_ << "'" << std::endl;
246 std::string Options::getProgname()
248 Lock lock(mutex);
249 return progname_;
253 Options& Options::setProgname(std::string p)
255 Lock lock(mutex);
256 progname_ = p;
257 return *this;
260 bool Options::getDaemonize()
262 return daemonize_;
265 Options& Options::setDaemonize(bool d)
267 daemonize_ = d;
268 return *this;
271 bool Options::getChroot()
273 return chroot_;
276 Options& Options::setChroot(bool c)
278 chroot_ = c;
279 return *this;
282 std::string Options::getUsername()
284 Lock lock(mutex);
285 return username_;
288 Options& Options::setUsername(std::string u)
290 Lock lock(mutex);
291 username_ = u;
292 return *this;
295 std::string Options::getChrootDir()
297 Lock lock(mutex);
298 return chroot_dir_;
301 Options& Options::setChrootDir(std::string c)
303 Lock lock(mutex);
304 chroot_dir_ = c;
305 return *this;
308 std::string Options::getPidFile()
310 Lock lock(mutex);
311 return pid_file_;
314 Options& Options::setPidFile(std::string p)
316 Lock lock(mutex);
317 pid_file_ = p;
318 return *this;
321 std::string Options::getFileName()
323 Lock lock(mutex);
324 return file_name_;
327 Options& Options::setFileName(std::string f)
329 Lock lock(mutex);
330 file_name_ = f;
331 return *this;
334 std::string Options::getBindToAddr()
336 Lock lock(mutex);
337 return bind_to_addr_;
340 Options& Options::setBindToAddr(std::string b)
342 Lock lock(mutex);
343 bind_to_addr_ = b;
344 return *this;
347 std::string Options::getBindToPort()
349 return bind_to_port_;
352 Options& Options::setBindToPort(std::string b)
354 bind_to_port_ = b;
355 return *this;