svn cleanup
[anytun.git] / anyrtpproxy / options.cpp
blobbdddf75f4ab5140db903410b961763a512b75d67
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 "options.h"
38 Options* Options::inst = NULL;
39 Mutex Options::instMutex;
40 Options& gOpt = Options::instance();
42 Options& Options::instance()
44 Lock lock(instMutex);
45 static instanceCleaner c;
46 if(!inst)
47 inst = new Options();
49 return *inst;
52 Options::Options() : control_interface_("0.0.0.0", 22222)
55 progname_ = "anyrtpproxy";
56 chroot_ = false;
57 username_ = "nobody";
58 chroot_dir_ = "/var/run";
59 daemonize_ = true;
60 local_sync_port_ = 0;
61 rtp_start_port_ = 34000;
62 rtp_end_port_ = 35000;
63 no_nat_once_ = false;
64 nat_ = false;
67 Options::~Options()
71 #define PARSE_BOOL_PARAM(SHORT, LONG, VALUE) \
72 else if(str == SHORT || str == LONG) \
73 VALUE = true;
75 #define PARSE_INVERSE_BOOL_PARAM(SHORT, LONG, VALUE) \
76 else if(str == SHORT || str == LONG) \
77 VALUE = false;
79 #define PARSE_SCALAR_PARAM(SHORT, LONG, VALUE) \
80 else if(str == SHORT || str == LONG) \
81 { \
82 if(argc < 1 || argv[i+1][0] == '-') \
83 return false; \
84 std::stringstream tmp; \
85 tmp << argv[i+1]; \
86 tmp >> VALUE; \
87 argc--; \
88 i++; \
91 #define PARSE_SCALAR_PARAM2(SHORT, LONG, VALUE1, VALUE2) \
92 else if(str == SHORT || str == LONG) \
93 { \
94 if(argc < 2 || \
95 argv[i+1][0] == '-' || argv[i+2][0] == '-') \
96 return false; \
97 std::stringstream tmp; \
98 tmp << argv[i+1] << " " << argv[i+2]; \
99 tmp >> VALUE1; \
100 tmp >> VALUE2; \
101 argc-=2; \
102 i+=2; \
105 #define PARSE_STRING_PARAM(SHORT, LONG, VALUE) \
106 else if(str == SHORT || str == LONG) \
108 if(argc < 1 || argv[i+1][0] == '-') \
109 return false; \
110 VALUE = std::string(argv[i+1]); \
111 argc--; \
112 i++; \
115 #define PARSE_HEXSTRING_PARAM_SEC(SHORT, LONG, VALUE) \
116 else if(str == SHORT || str == LONG) \
118 if(argc < 1 || argv[i+1][0] == '-') \
119 return false; \
120 VALUE = Buffer(std::string(argv[i+1])); \
121 for(size_t j=0; j < strlen(argv[i+1]); ++j) \
122 argv[i+1][j] = '#'; \
123 argc--; \
124 i++; \
127 #define PARSE_CSLIST_PARAM(SHORT, LONG, LIST) \
128 else if(str == SHORT || str == LONG) \
130 if(argc < 1 || argv[i+1][0] == '-') \
131 return false; \
132 std::stringstream tmp(argv[i+1]); \
133 /* LIST.clear(); */ \
134 while (tmp.good()) \
136 std::string tmp_line; \
137 getline(tmp,tmp_line,','); \
138 LIST.push(tmp_line); \
140 argc--; \
141 i++; \
144 bool Options::parse(int argc, char* argv[])
146 Lock lock(mutex);
148 progname_ = argv[0];
149 std::queue<std::string> host_port_queue;
150 argc--;
151 for(int i=1; argc > 0; ++i)
153 std::string str(argv[i]);
154 argc--;
156 if(str == "-h" || str == "--help")
157 return false;
158 PARSE_BOOL_PARAM("-t","--chroot", chroot_)
159 PARSE_BOOL_PARAM("-n","--nat", nat_)
160 PARSE_BOOL_PARAM("-o","--no-nat-once", no_nat_once_)
161 PARSE_SCALAR_PARAM("-u","--user", username_)
162 PARSE_SCALAR_PARAM("-c","--chroot-dir", chroot_dir_)
163 PARSE_INVERSE_BOOL_PARAM("-d","--nodaemonize", daemonize_)
164 PARSE_STRING_PARAM("-s","--control", control_interface_)
165 PARSE_SCALAR_PARAM2("-p","--port-range", rtp_start_port_, rtp_end_port_)
166 PARSE_CSLIST_PARAM("-M","--sync-hosts", host_port_queue)
167 PARSE_SCALAR_PARAM("-S","--sync-port", local_sync_port_)
168 // PARSE_SCALAR_PARAM("-I","--sync-interface", local_sync_addr_)
169 else
170 return false;
172 while(!host_port_queue.empty())
174 std::stringstream tmp_stream(host_port_queue.front());
175 OptionConnectTo oct;
176 getline(tmp_stream,oct.host,':');
177 if(!tmp_stream.good())
178 return false;
179 tmp_stream >> oct.port;
180 host_port_queue.pop();
181 connect_to_.push_back(oct);
184 return sanityCheck();
187 bool Options::sanityCheck()
189 if(!control_interface_.port_) control_interface_.port_ = 22220;
190 return true;
193 void Options::printUsage()
195 std::cout << "USAGE: anyrtpproxy" << std::endl;
196 std::cout << " [-h|--help] prints this..." << std::endl;
197 std::cout << " [-t|--chroot] chroot and drop priviledges" << std::endl;
198 std::cout << " [-u|--username] <username> in case of chroot run as this user" << std::endl;
199 std::cout << " [-c|--chroot-dir] <directory> directory to make a chroot to" << std::endl;
200 std::cout << " [-d|--nodaemonize] don't run in background" << std::endl;
201 std::cout << " [-s|--control] <addr[:port]> the address/port to listen on for control commands" << std::endl;
202 std::cout << " [-p|--port-range] <start> <end> port range used to relay rtp connections" << std::endl;
203 std::cout << " [-n|--nat] enable permantent automatic nat detection(use only with anytun)" << std::endl;
204 std::cout << " [-o|--no-nat-once] disable automatic nat detection for new connections" << std::endl;
205 // std::cout << " [-I|--sync-interface] <ip-address> local unicast(sync) ip address to bind to" << std::endl;
206 std::cout << " [-S|--sync-port] <port> local unicast(sync) port to bind to" << std::endl;
207 std::cout << " [-M|--sync-hosts] <hostname|ip>:<port>[,<hostname|ip>:<port>[...]]"<< std::endl;
208 std::cout << " List of Remote Sync Hosts/Ports"<< std::endl;
211 void Options::printOptions()
213 Lock lock(mutex);
214 std::cout << "Options:" << std::endl;
215 std::cout << "chroot='" << chroot_ << "'" << std::endl;
216 std::cout << "username='" << username_ << "'" << std::endl;
217 std::cout << "chroot-dir='" << chroot_dir_ << "'" << std::endl;
218 std::cout << "daemonize='" << daemonize_ << "'" << std::endl;
219 std::cout << "control-interface='" << control_interface_.toString() << "'" << std::endl;
222 std::string Options::getProgname()
224 Lock lock(mutex);
225 return progname_;
228 bool Options::getChroot()
230 Lock lock(mutex);
231 return chroot_;
234 bool Options::getNat()
236 Lock lock(mutex);
237 return nat_;
240 bool Options::getNoNatOnce()
242 Lock lock(mutex);
243 return no_nat_once_;
246 std::string Options::getUsername()
248 Lock lock(mutex);
249 return username_;
252 std::string Options::getChrootDir()
254 Lock lock(mutex);
255 return chroot_dir_;
258 bool Options::getDaemonize()
260 Lock lock(mutex);
261 return daemonize_;
264 Host Options::getControlInterface()
266 Lock lock(mutex);
267 return control_interface_;
270 u_int16_t Options::getLocalSyncPort()
272 return local_sync_port_;
275 Options& Options::setLocalSyncPort(u_int16_t l)
277 local_sync_port_ = l;
278 return *this;
281 u_int16_t Options::getRtpStartPort()
283 return rtp_start_port_;
286 Options& Options::setRtpStartPort(u_int16_t l)
288 rtp_start_port_ = l;
289 return *this;
292 u_int16_t Options::getRtpEndPort()
294 return rtp_end_port_;
297 Options& Options::setRtpEndPort(u_int16_t l)
299 rtp_end_port_ = l;
300 return *this;
303 ConnectToList Options::getConnectTo()
305 Lock lock(mutex);
306 return connect_to_;