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/>.
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()
47 static instanceCleaner c
;
56 progname_
= "anytun-controld";
61 chroot_dir_
= "/var/run/anytun-controld";
63 bind_to_addr_
= "127.0.0.1";
64 bind_to_port_
= "2323";
71 #define PARSE_BOOL_PARAM(SHORT, LONG, VALUE) \
72 else if(str == SHORT || str == LONG) \
75 #define PARSE_INVERSE_BOOL_PARAM(SHORT, LONG, VALUE) \
76 else if(str == SHORT || str == LONG) \
79 #define PARSE_SCALAR_PARAM(SHORT, LONG, VALUE) \
80 else if(str == SHORT || str == LONG) \
82 if(argc < 1 || argv[i+1][0] == '-') \
84 std::stringstream tmp; \
91 #define PARSE_SCALAR_PARAM2(SHORT, LONG, VALUE1, VALUE2) \
92 else if(str == SHORT || str == LONG) \
95 argv[i+1][0] == '-' || argv[i+2][0] == '-') \
97 std::stringstream tmp; \
98 tmp << argv[i+1] << " " << argv[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] == '-') \
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] = '#'; \
117 #define PARSE_CSLIST_PARAM(SHORT, LONG, LIST) \
118 else if(str == SHORT || str == LONG) \
120 if(argc < 1 || argv[i+1][0] == '-') \
122 std::stringstream tmp(argv[i+1]); \
125 std::string tmp_line; \
126 getline(tmp,tmp_line,','); \
127 LIST.push(tmp_line); \
133 bool Options::parse(int argc
, char* argv
[])
140 std::string
control_host("");
141 for(int i
=1; argc
> 0; ++i
)
143 std::string
str(argv
[i
]);
146 if(str
== "-h" || str
== "--help")
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
)
159 if(control_host
!= "")
160 return splitAndSetHostPort(control_host
);
165 bool Options::splitAndSetHostPort(std::string hostPort
)
167 if(hostPort
.length() >= 2 && hostPort
[0] == ':' && hostPort
[1] != ':') {
170 std::stringstream
tmp_stream(hostPort
);
171 tmp_stream
>> bind_to_port_
;
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] = '/';
196 else if(pos
!= hostPort
.length()-1)
197 return false; // to few characters left
199 hostPort
.erase(pos
, 1);
203 std::stringstream
tmp_stream(hostPort
);
205 getline(tmp_stream
, bind_to_addr_
, '/');
206 if(!tmp_stream
.good())
209 tmp_stream
>> bind_to_port_
;
212 bind_to_addr_
= hostPort
;
213 bind_to_port_
= "2323"; // default sync port
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()
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()
253 Options
& Options::setProgname(std::string p
)
260 bool Options::getDaemonize()
265 Options
& Options::setDaemonize(bool d
)
271 bool Options::getChroot()
276 Options
& Options::setChroot(bool c
)
282 std::string
Options::getUsername()
288 Options
& Options::setUsername(std::string u
)
295 std::string
Options::getChrootDir()
301 Options
& Options::setChrootDir(std::string c
)
308 std::string
Options::getPidFile()
314 Options
& Options::setPidFile(std::string p
)
321 std::string
Options::getFileName()
327 Options
& Options::setFileName(std::string f
)
334 std::string
Options::getBindToAddr()
337 return bind_to_addr_
;
340 Options
& Options::setBindToAddr(std::string b
)
347 std::string
Options::getBindToPort()
349 return bind_to_port_
;
352 Options
& Options::setBindToPort(std::string b
)