working example server and client
[anytun.git] / anymuxOptions.cpp
blob418eebd7746c875117e52f5fedac7f28aea76bd9
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 "anymuxOptions.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(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 argc--; \
111 i++; \
114 #define PARSE_CSLIST_PARAM(SHORT, LONG, LIST) \
115 else if(str == SHORT || str == LONG) \
117 if(argc < 1 || argv[i+1][0] == '-') \
118 return false; \
119 std::stringstream tmp(argv[i+1]); \
120 while (tmp.good()) \
122 std::string tmp_line; \
123 getline(tmp,tmp_line,','); \
124 LIST.push(tmp_line); \
126 argc--; \
127 i++; \
130 bool Options::parse(int argc, char* argv[])
132 Lock lock(mutex);
134 progname_ = argv[0];
135 argc--;
137 std::string control_host("");
138 for(int i=1; argc > 0; ++i)
140 std::string str(argv[i]);
141 argc--;
143 if(str == "-h" || str == "--help")
144 return false;
145 PARSE_SCALAR_PARAM("-f","--file", file_name_)
146 PARSE_INVERSE_BOOL_PARAM("-D","--nodaemonize", daemonize_)
147 PARSE_BOOL_PARAM("-C","--chroot", chroot_)
148 PARSE_SCALAR_PARAM("-u","--username", username_)
149 PARSE_SCALAR_PARAM("-H","--chroot-dir", chroot_dir_)
150 PARSE_SCALAR_PARAM("-P","--write-pid", pid_file_)
151 PARSE_SCALAR_PARAM("-X","--control-host", control_host)
152 else
153 return false;
156 if(control_host != "") {
157 std::stringstream tmp_stream(control_host);
158 getline(tmp_stream,bind_to_addr_,':');
159 if(!tmp_stream.good())
160 return false;
161 tmp_stream >> bind_to_port_;
164 return true;
167 void Options::printUsage()
169 std::cout << "USAGE:" << std::endl;
170 std::cout << "anytun-controld [-h|--help] prints this..." << std::endl;
171 std::cout << " [-D|--nodaemonize] don't run in background" << std::endl;
172 std::cout << " [-C|--chroot] chroot and drop privileges" << std::endl;
173 std::cout << " [-u|--username] if chroot change to this user" << std::endl;
174 std::cout << " [-H|--chroot-dir] chroot to this directory" << std::endl;
175 std::cout << " [-P|--write-pid] write pid to this file" << std::endl;
176 std::cout << " [-f|--file] <path> path to file" << std::endl;
180 void Options::printOptions()
182 Lock lock(mutex);
183 std::cout << "Options:" << std::endl;
184 std::cout << "daemonize=" << daemonize_ << std::endl;
185 std::cout << "chroot=" << chroot_ << std::endl;
186 std::cout << "username='" << username_ << "'" << std::endl;
187 std::cout << "chroot_dir='" << chroot_dir_ << "'" << std::endl;
188 std::cout << "pid_file='" << pid_file_ << "'" << std::endl;
191 std::string Options::getProgname()
193 Lock lock(mutex);
194 return progname_;
198 Options& Options::setProgname(std::string p)
200 Lock lock(mutex);
201 progname_ = p;
202 return *this;
205 bool Options::getDaemonize()
207 return daemonize_;
210 Options& Options::setDaemonize(bool d)
212 daemonize_ = d;
213 return *this;
216 bool Options::getChroot()
218 return chroot_;
221 Options& Options::setChroot(bool c)
223 chroot_ = c;
224 return *this;
227 std::string Options::getUsername()
229 Lock lock(mutex);
230 return username_;
233 Options& Options::setUsername(std::string u)
235 Lock lock(mutex);
236 username_ = u;
237 return *this;
240 std::string Options::getChrootDir()
242 Lock lock(mutex);
243 return chroot_dir_;
246 Options& Options::setChrootDir(std::string c)
248 Lock lock(mutex);
249 chroot_dir_ = c;
250 return *this;
253 std::string Options::getPidFile()
255 Lock lock(mutex);
256 return pid_file_;
259 Options& Options::setPidFile(std::string p)
261 Lock lock(mutex);
262 pid_file_ = p;
263 return *this;
266 std::string Options::getFileName()
268 Lock lock(mutex);
269 return file_name_;
272 Options& Options::setFileName(std::string f)
274 Lock lock(mutex);
275 file_name_ = f;
276 return *this;
279 std::string Options::getBindToAddr()
281 Lock lock(mutex);
282 return bind_to_addr_;
285 Options& Options::setBindToAddr(std::string b)
287 Lock lock(mutex);
288 bind_to_addr_ = b;
289 return *this;
292 uint16_t Options::getBindToPort()
294 return bind_to_port_;
297 Options& Options::setBindToPort(uint16_t b)
299 bind_to_port_ = b;
300 return *this;