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-2009 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/>.
40 Options* Options::inst = NULL;
41 Mutex Options::instMutex;
42 Options& gOpt = Options::instance();
44 Options& Options::instance()
47 static instanceCleaner c;
54 void Host::splitAndSetAddrPort(std::string addr_port)
56 if(addr_port.length() >= 2 && addr_port[0] == ':' && addr_port[1] != ':') {
59 std::stringstream tmp_stream(addr_port);
64 size_t pos = addr_port.find_first_of("[");
66 if(pos != std::string::npos && pos != 0)
67 return; // an [ was found but not at the beginning
70 if(pos != std::string::npos) {
71 addr_port.erase(pos, 1);
72 pos = addr_port.find_first_of("]");
74 if(pos == std::string::npos)
75 return; // no trailing ] although an leading [ was found
77 if(pos < addr_port.length()-2) {
79 if(addr_port[pos+1] != ':')
80 return; // wrong port delimieter
82 addr_port[pos+1] = '/';
85 else if(pos != addr_port.length()-1)
86 return; // to few characters left
88 addr_port.erase(pos, 1);
92 std::stringstream tmp_stream(addr_port);
94 getline(tmp_stream, addr_, '/');
95 if(!tmp_stream.good())
102 port_ = "2323"; // default sync port
107 Options::Options() : control_interface_("0.0.0.0", "22222")
110 progname_ = "anyrtpproxy";
112 username_ = "nobody";
113 chroot_dir_ = "/var/run";
117 local_sync_port_ = "";
118 rtp_start_port_ = 34000;
119 rtp_end_port_ = 35000;
120 no_nat_once_ = false;
128 #define PARSE_BOOL_PARAM(SHORT, LONG, VALUE) \
129 else if(str == SHORT || str == LONG) \
132 #define PARSE_INVERSE_BOOL_PARAM(SHORT, LONG, VALUE) \
133 else if(str == SHORT || str == LONG) \
136 #define PARSE_SCALAR_PARAM(SHORT, LONG, VALUE) \
137 else if(str == SHORT || str == LONG) \
139 if(argc < 1 || argv[i+1][0] == '-') \
141 std::stringstream tmp; \
148 #define PARSE_SCALAR_PARAM2(SHORT, LONG, VALUE1, VALUE2) \
149 else if(str == SHORT || str == LONG) \
152 argv[i+1][0] == '-' || argv[i+2][0] == '-') \
154 std::stringstream tmp; \
155 tmp << argv[i+1] << " " << argv[i+2]; \
162 #define PARSE_STRING_PARAM(SHORT, LONG, VALUE) \
163 else if(str == SHORT || str == LONG) \
165 if(argc < 1 || argv[i+1][0] == '-') \
167 VALUE = std::string(argv[i+1]); \
172 #define PARSE_HEXSTRING_PARAM_SEC(SHORT, LONG, VALUE) \
173 else if(str == SHORT || str == LONG) \
175 if(argc < 1 || argv[i+1][0] == '-') \
177 VALUE = Buffer(std::string(argv[i+1])); \
178 for(size_t j=0; j < strlen(argv[i+1]); ++j) \
179 argv[i+1][j] = '#'; \
184 #define PARSE_CSLIST_PARAM(SHORT, LONG, LIST) \
185 else if(str == SHORT || str == LONG) \
187 if(argc < 1 || argv[i+1][0] == '-') \
189 std::stringstream tmp(argv[i+1]); \
190 /* LIST.clear(); */ \
193 std::string tmp_line; \
194 getline(tmp,tmp_line,','); \
195 LIST.push(tmp_line); \
201 bool Options::parse(int argc, char* argv[])
206 std::queue<std::string> host_port_queue;
208 for(int i=1; argc > 0; ++i)
210 std::string str(argv[i]);
213 if(str == "-h" || str == "--help")
215 PARSE_BOOL_PARAM("-t","--chroot", chroot_)
216 PARSE_BOOL_PARAM("-n","--nat", nat_)
217 PARSE_BOOL_PARAM("-o","--no-nat-once", no_nat_once_)
218 PARSE_SCALAR_PARAM("-u","--user", username_)
219 PARSE_SCALAR_PARAM("-c","--chroot-dir", chroot_dir_)
220 PARSE_INVERSE_BOOL_PARAM("-d","--nodaemonize", daemonize_)
221 PARSE_SCALAR_PARAM("-P","--write-pid", pid_file_)
222 PARSE_SCALAR_PARAM("-i","--interface", local_addr_)
223 PARSE_STRING_PARAM("-s","--control", control_interface_)
224 PARSE_SCALAR_PARAM2("-p","--port-range", rtp_start_port_, rtp_end_port_)
225 PARSE_CSLIST_PARAM("-M","--sync-hosts", host_port_queue)
226 PARSE_SCALAR_PARAM("-S","--sync-port", local_sync_port_)
227 PARSE_SCALAR_PARAM("-I","--sync-interface", local_sync_addr_)
231 while(!host_port_queue.empty())
233 std::stringstream tmp_stream(host_port_queue.front());
235 getline(tmp_stream,oct.host,':');
236 if(!tmp_stream.good())
238 tmp_stream >> oct.port;
239 host_port_queue.pop();
240 connect_to_.push_back(oct);
243 return sanityCheck();
246 bool Options::sanityCheck()
248 if(control_interface_.port_ == "") control_interface_.port_ = "22222";
252 void Options::printUsage()
254 std::cout << "USAGE: anyrtpproxy" << std::endl;
255 std::cout << " [-h|--help] prints this..." << std::endl;
256 std::cout << " [-t|--chroot] chroot and drop priviledges" << std::endl;
257 std::cout << " [-u|--username] <username> in case of chroot run as this user" << std::endl;
258 std::cout << " [-c|--chroot-dir] <directory> directory to make a chroot to" << std::endl;
259 std::cout << " [-d|--nodaemonize] don't run in background" << std::endl;
260 std::cout << " [-P|--write-pid] <path> write pid to this file" << std::endl;
261 std::cout << " [-i|--interface] <ip-address> local ip address to listen to for RTP packets" << std::endl;
262 std::cout << " [-s|--control] <addr>[:<port>] the address/port to listen on for control commands" << std::endl;
263 std::cout << " [-p|--port-range] <start> <end> port range used to relay rtp connections" << std::endl;
264 std::cout << " [-n|--nat] enable permantent automatic nat detection(use only with anytun)" << std::endl;
265 std::cout << " [-o|--no-nat-once] disable automatic nat detection for new connections" << std::endl;
266 std::cout << " [-I|--sync-interface] <ip-address> local unicast(sync) ip address to bind to" << std::endl;
267 std::cout << " [-S|--sync-port] <port> local unicast(sync) port to bind to" << std::endl;
268 std::cout << " [-M|--sync-hosts] <hostname|ip>:<port>[,<hostname|ip>:<port>[...]]"<< std::endl;
269 std::cout << " List of Remote Sync Hosts/Ports"<< std::endl;
272 void Options::printOptions()
275 std::cout << "Options:" << std::endl;
276 std::cout << "chroot='" << chroot_ << "'" << std::endl;
277 std::cout << "username='" << username_ << "'" << std::endl;
278 std::cout << "chroot-dir='" << chroot_dir_ << "'" << std::endl;
279 std::cout << "daemonize='" << daemonize_ << "'" << std::endl;
280 std::cout << "pid_file='" << pid_file_ << "'" << std::endl;
281 std::cout << "control-interface='" << control_interface_.toString() << "'" << std::endl;
282 std::cout << "local_addr='" << local_addr_ << "'" << std::endl;
283 std::cout << "rtp_start_port=" << rtp_start_port_ << std::endl;
284 std::cout << "rtp_end_port=" << rtp_end_port_ << std::endl;
285 std::cout << "local_sync_addr='" << local_sync_addr_ << "'" << std::endl;
286 std::cout << "local_sync_port='" << local_sync_port_ << "'" << std::endl;
289 std::string Options::getProgname()
295 bool Options::getChroot()
301 bool Options::getNat()
307 bool Options::getNoNatOnce()
313 std::string Options::getUsername()
319 std::string Options::getChrootDir()
325 bool Options::getDaemonize()
331 std::string Options::getPidFile()
337 Host Options::getControlInterface()
340 return control_interface_;
343 std::string Options::getLocalAddr()
349 Options& Options::setLocalAddr(std::string l)
356 std::string Options::getLocalSyncAddr()
359 return local_sync_addr_;
362 Options& Options::setLocalSyncAddr(std::string l)
365 local_sync_addr_ = l;
369 std::string Options::getLocalSyncPort()
372 return local_sync_port_;
375 Options& Options::setLocalSyncPort(std::string l)
378 local_sync_port_ = l;
382 u_int16_t Options::getRtpStartPort()
384 return rtp_start_port_;
387 Options& Options::setRtpStartPort(u_int16_t l)
393 u_int16_t Options::getRtpEndPort()
395 return rtp_end_port_;
398 Options& Options::setRtpEndPort(u_int16_t l)
404 ConnectToList Options::getConnectTo()