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
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()
46 static instanceCleaner c
;
55 progname_
= "anytun-controld";
60 chroot_dir_
= "/var/run/anytun-controld";
62 bind_to_addr_
= "127.0.0.1";
70 #define PARSE_BOOL_PARAM(SHORT, LONG, VALUE) \
71 else if(str == SHORT || str == LONG) \
74 #define PARSE_INVERSE_BOOL_PARAM(SHORT, LONG, VALUE) \
75 else if(str == SHORT || str == LONG) \
78 #define PARSE_SCALAR_PARAM(SHORT, LONG, VALUE) \
79 else if(str == SHORT || str == LONG) \
81 if(argc < 1 || argv[i+1][0] == '-') \
83 std::stringstream tmp; \
90 #define PARSE_SCALAR_PARAM2(SHORT, LONG, VALUE1, VALUE2) \
91 else if(str == SHORT || str == LONG) \
94 argv[i+1][0] == '-' || argv[i+2][0] == '-') \
96 std::stringstream tmp; \
97 tmp << argv[i+1] << " " << argv[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] == '-') \
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] = '#'; \
116 #define PARSE_CSLIST_PARAM(SHORT, LONG, LIST) \
117 else if(str == SHORT || str == LONG) \
119 if(argc < 1 || argv[i+1][0] == '-') \
121 std::stringstream tmp(argv[i+1]); \
124 std::string tmp_line; \
125 getline(tmp,tmp_line,','); \
126 LIST.push(tmp_line); \
132 bool Options::parse(int argc
, char* argv
[])
139 std::string
control_host("");
140 for(int i
=1; argc
> 0; ++i
)
142 std::string
str(argv
[i
]);
145 if(str
== "-h" || str
== "--help")
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
)
158 if(control_host
!= "") {
159 std::stringstream
tmp_stream(control_host
);
160 getline(tmp_stream
,bind_to_addr_
,':');
161 if(!tmp_stream
.good())
163 tmp_stream
>> bind_to_port_
;
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()
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()
201 Options
& Options::setProgname(std::string p
)
208 bool Options::getDaemonize()
213 Options
& Options::setDaemonize(bool d
)
219 bool Options::getChroot()
224 Options
& Options::setChroot(bool c
)
230 std::string
Options::getUsername()
236 Options
& Options::setUsername(std::string u
)
243 std::string
Options::getChrootDir()
249 Options
& Options::setChrootDir(std::string c
)
256 std::string
Options::getPidFile()
262 Options
& Options::setPidFile(std::string p
)
269 std::string
Options::getFileName()
275 Options
& Options::setFileName(std::string f
)
282 std::string
Options::getBindToAddr()
285 return bind_to_addr_
;
288 Options
& Options::setBindToAddr(std::string b
)
295 uint16_t Options::getBindToPort()
297 return bind_to_port_
;
300 Options
& Options::setBindToPort(uint16_t b
)