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 methods 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-2014 Markus Grüneis, 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 as published by
21 * the Free Software Foundation, either version 3 of the License, or
24 * Anytun is distributed in the hope that it will be useful,
25 * but WITHOUT ANY WARRANTY; without even the implied warranty of
26 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
27 * GNU General Public License for more details.
29 * You should have received a copy of the GNU General Public License
30 * along with Anytun. If not, see <http://www.gnu.org/licenses/>.
32 * In addition, as a special exception, the copyright holders give
33 * permission to link the code of portions of this program with the
34 * OpenSSL library under certain conditions as described in each
35 * individual source file, and distribute linked combinations
37 * You must obey the GNU General Public License in all respects
38 * for all of the code used other than OpenSSL. If you modify
39 * file(s) with this exception, you may extend this exception to your
40 * version of the file(s), but you are not obligated to do so. If you
41 * do not wish to do so, delete this exception statement from your
42 * version. If you delete this exception statement from all source
43 * files in the program, then also delete it here.
53 Options* Options::inst = NULL;
54 Mutex Options::instMutex;
55 Options& gOpt = Options::instance();
57 Options& Options::instance()
60 static instanceCleaner c;
68 void Host::splitAndSetAddrPort(std::string addr_port)
70 if(addr_port.length() >= 2 && addr_port[0] == ':' && addr_port[1] != ':') {
73 std::stringstream tmp_stream(addr_port);
78 size_t pos = addr_port.find_first_of("[");
80 if(pos != std::string::npos && pos != 0) {
81 return; // an [ was found but not at the beginning
85 if(pos != std::string::npos) {
86 addr_port.erase(pos, 1);
87 pos = addr_port.find_first_of("]");
89 if(pos == std::string::npos) {
90 return; // no trailing ] although an leading [ was found
93 if(pos < addr_port.length()-2) {
95 if(addr_port[pos+1] != ':') {
96 return; // wrong port delimieter
99 addr_port[pos+1] = '/';
101 } else if(pos != addr_port.length()-1) {
102 return; // to few characters left
105 addr_port.erase(pos, 1);
109 std::stringstream tmp_stream(addr_port);
111 getline(tmp_stream, addr_, '/');
112 if(!tmp_stream.good()) {
119 port_ = "2323"; // default sync port
124 Options::Options() : control_interface_("0.0.0.0", "22222")
127 progname_ = "anyrtpproxy";
129 username_ = "nobody";
130 chroot_dir_ = "/var/run";
134 local_sync_port_ = "";
135 rtp_start_port_ = 34000;
136 rtp_end_port_ = 35000;
137 no_nat_once_ = false;
145 #define PARSE_BOOL_PARAM(SHORT, LONG, VALUE) \
146 else if(str == SHORT || str == LONG) \
149 #define PARSE_INVERSE_BOOL_PARAM(SHORT, LONG, VALUE) \
150 else if(str == SHORT || str == LONG) \
153 #define PARSE_SCALAR_PARAM(SHORT, LONG, VALUE) \
154 else if(str == SHORT || str == LONG) \
156 if(argc < 1 || argv[i+1][0] == '-') \
158 std::stringstream tmp; \
165 #define PARSE_SCALAR_PARAM2(SHORT, LONG, VALUE1, VALUE2) \
166 else if(str == SHORT || str == LONG) \
169 argv[i+1][0] == '-' || argv[i+2][0] == '-') \
171 std::stringstream tmp; \
172 tmp << argv[i+1] << " " << argv[i+2]; \
179 #define PARSE_STRING_PARAM(SHORT, LONG, VALUE) \
180 else if(str == SHORT || str == LONG) \
182 if(argc < 1 || argv[i+1][0] == '-') \
184 VALUE = std::string(argv[i+1]); \
189 #define PARSE_HEXSTRING_PARAM_SEC(SHORT, LONG, VALUE) \
190 else if(str == SHORT || str == LONG) \
192 if(argc < 1 || argv[i+1][0] == '-') \
194 VALUE = Buffer(std::string(argv[i+1])); \
195 for(size_t j=0; j < strlen(argv[i+1]); ++j) \
196 argv[i+1][j] = '#'; \
201 #define PARSE_CSLIST_PARAM(SHORT, LONG, LIST) \
202 else if(str == SHORT || str == LONG) \
204 if(argc < 1 || argv[i+1][0] == '-') \
206 std::stringstream tmp(argv[i+1]); \
207 /* LIST.clear(); */ \
210 std::string tmp_line; \
211 getline(tmp,tmp_line,','); \
212 LIST.push(tmp_line); \
218 bool Options::parse(int argc, char* argv[])
223 std::queue<std::string> host_port_queue;
225 for(int i=1; argc > 0; ++i) {
226 std::string str(argv[i]);
229 if(str == "-h" || str == "--help") {
232 PARSE_BOOL_PARAM("-t","--chroot", chroot_)
233 PARSE_BOOL_PARAM("-n","--nat", nat_)
234 PARSE_BOOL_PARAM("-o","--no-nat-once", no_nat_once_)
235 PARSE_SCALAR_PARAM("-u","--user", username_)
236 PARSE_SCALAR_PARAM("-c","--chroot-dir", chroot_dir_)
237 PARSE_INVERSE_BOOL_PARAM("-d","--nodaemonize", daemonize_)
238 PARSE_SCALAR_PARAM("-P","--write-pid", pid_file_)
239 PARSE_SCALAR_PARAM("-i","--interface", local_addr_)
240 PARSE_STRING_PARAM("-s","--control", control_interface_)
241 PARSE_SCALAR_PARAM2("-p","--port-range", rtp_start_port_, rtp_end_port_)
242 PARSE_CSLIST_PARAM("-M","--sync-hosts", host_port_queue)
243 PARSE_SCALAR_PARAM("-S","--sync-port", local_sync_port_)
244 PARSE_SCALAR_PARAM("-I","--sync-interface", local_sync_addr_)
249 while(!host_port_queue.empty()) {
250 std::stringstream tmp_stream(host_port_queue.front());
252 getline(tmp_stream,oct.host,':');
253 if(!tmp_stream.good()) {
256 tmp_stream >> oct.port;
257 host_port_queue.pop();
258 connect_to_.push_back(oct);
261 return sanityCheck();
264 bool Options::sanityCheck()
266 if(control_interface_.port_ == "") { control_interface_.port_ = "22222"; }
270 void Options::printUsage()
272 std::cout << "USAGE: anyrtpproxy" << std::endl;
273 std::cout << " [-h|--help] prints this..." << std::endl;
274 std::cout << " [-t|--chroot] chroot and drop priviledges" << std::endl;
275 std::cout << " [-u|--username] <username> in case of chroot run as this user" << std::endl;
276 std::cout << " [-c|--chroot-dir] <directory> directory to make a chroot to" << std::endl;
277 std::cout << " [-d|--nodaemonize] don't run in background" << std::endl;
278 std::cout << " [-P|--write-pid] <path> write pid to this file" << std::endl;
279 std::cout << " [-i|--interface] <ip-address> local ip address to listen to for RTP packets" << std::endl;
280 std::cout << " [-s|--control] <addr>[:<port>] the address/port to listen on for control commands" << std::endl;
281 std::cout << " [-p|--port-range] <start> <end> port range used to relay rtp connections" << std::endl;
282 std::cout << " [-n|--nat] enable permantent automatic nat detection(use only with anytun)" << std::endl;
283 std::cout << " [-o|--no-nat-once] disable automatic nat detection for new connections" << std::endl;
284 std::cout << " [-I|--sync-interface] <ip-address> local unicast(sync) ip address to bind to" << std::endl;
285 std::cout << " [-S|--sync-port] <port> local unicast(sync) port to bind to" << std::endl;
286 std::cout << " [-M|--sync-hosts] <hostname|ip>:<port>[,<hostname|ip>:<port>[...]]"<< std::endl;
287 std::cout << " List of Remote Sync Hosts/Ports"<< std::endl;
290 void Options::printOptions()
293 std::cout << "Options:" << std::endl;
294 std::cout << "chroot='" << chroot_ << "'" << std::endl;
295 std::cout << "username='" << username_ << "'" << std::endl;
296 std::cout << "chroot-dir='" << chroot_dir_ << "'" << std::endl;
297 std::cout << "daemonize='" << daemonize_ << "'" << std::endl;
298 std::cout << "pid_file='" << pid_file_ << "'" << std::endl;
299 std::cout << "control-interface='" << control_interface_.toString() << "'" << std::endl;
300 std::cout << "local_addr='" << local_addr_ << "'" << std::endl;
301 std::cout << "rtp_start_port=" << rtp_start_port_ << std::endl;
302 std::cout << "rtp_end_port=" << rtp_end_port_ << std::endl;
303 std::cout << "local_sync_addr='" << local_sync_addr_ << "'" << std::endl;
304 std::cout << "local_sync_port='" << local_sync_port_ << "'" << std::endl;
307 std::string Options::getProgname()
313 bool Options::getChroot()
319 bool Options::getNat()
325 bool Options::getNoNatOnce()
331 std::string Options::getUsername()
337 std::string Options::getChrootDir()
343 bool Options::getDaemonize()
349 std::string Options::getPidFile()
355 Host Options::getControlInterface()
358 return control_interface_;
361 std::string Options::getLocalAddr()
367 Options& Options::setLocalAddr(std::string l)
374 std::string Options::getLocalSyncAddr()
377 return local_sync_addr_;
380 Options& Options::setLocalSyncAddr(std::string l)
383 local_sync_addr_ = l;
387 std::string Options::getLocalSyncPort()
390 return local_sync_port_;
393 Options& Options::setLocalSyncPort(std::string l)
396 local_sync_port_ = l;
400 uint16_t Options::getRtpStartPort()
402 return rtp_start_port_;
405 Options& Options::setRtpStartPort(uint16_t l)
411 uint16_t Options::getRtpEndPort()
413 return rtp_end_port_;
416 Options& Options::setRtpEndPort(uint16_t l)
422 ConnectToList Options::getConnectTo()