working version with crypto
[anytun.git] / anyrtpproxy / options.cpp
blob6ddd9dc16b603bf85b1445232a8b50f155c39a3c
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(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 argc--; \
122 i++; \
126 #define PARSE_CSLIST_PARAM(SHORT, LONG, LIST) \
127 else if(str == SHORT || str == LONG) \
129 if(argc < 1 || argv[i+1][0] == '-') \
130 return false; \
131 std::stringstream tmp(argv[i+1]); \
132 /* LIST.clear(); */ \
133 while (tmp.good()) \
135 std::string tmp_line; \
136 getline(tmp,tmp_line,','); \
137 LIST.push(tmp_line); \
139 argc--; \
140 i++; \
143 bool Options::parse(int argc, char* argv[])
145 Lock lock(mutex);
147 progname_ = argv[0];
148 std::queue<std::string> host_port_queue;
149 argc--;
150 for(int i=1; argc > 0; ++i)
152 std::string str(argv[i]);
153 argc--;
155 if(str == "-h" || str == "--help")
156 return false;
157 PARSE_BOOL_PARAM("-t","--chroot", chroot_)
158 PARSE_BOOL_PARAM("-n","--nat", nat_)
159 PARSE_BOOL_PARAM("-o","--no-nat-once", no_nat_once_)
160 PARSE_SCALAR_PARAM("-u","--user", username_)
161 PARSE_SCALAR_PARAM("-c","--chroot-dir", chroot_dir_)
162 PARSE_INVERSE_BOOL_PARAM("-d","--nodaemonize", daemonize_)
163 PARSE_STRING_PARAM("-s","--control", control_interface_)
164 PARSE_SCALAR_PARAM2("-p","--port-range", rtp_start_port_, rtp_end_port_)
165 PARSE_CSLIST_PARAM("-M","--sync-hosts", host_port_queue)
166 PARSE_SCALAR_PARAM("-S","--sync-port", local_sync_port_)
167 // PARSE_SCALAR_PARAM("-I","--sync-interface", local_sync_addr_)
168 else
169 return false;
171 while(!host_port_queue.empty())
173 std::stringstream tmp_stream(host_port_queue.front());
174 OptionConnectTo oct;
175 getline(tmp_stream,oct.host,':');
176 if(!tmp_stream.good())
177 return false;
178 tmp_stream >> oct.port;
179 host_port_queue.pop();
180 connect_to_.push_back(oct);
183 return sanityCheck();
186 bool Options::sanityCheck()
188 if(!control_interface_.port_) control_interface_.port_ = 22220;
189 return true;
192 void Options::printUsage()
194 std::cout << "USAGE: anyrtpproxy" << std::endl;
195 std::cout << " [-h|--help] prints this..." << std::endl;
196 std::cout << " [-t|--chroot] chroot and drop priviledges" << std::endl;
197 std::cout << " [-u|--username] <username> in case of chroot run as this user" << std::endl;
198 std::cout << " [-c|--chroot-dir] <directory> directory to make a chroot to" << std::endl;
199 std::cout << " [-d|--nodaemonize] don't run in background" << std::endl;
200 std::cout << " [-s|--control] <addr[:port]> the address/port to listen on for control commands" << std::endl;
201 std::cout << " [-p|--port-range] <start> <end> port range used to relay rtp connections" << std::endl;
202 std::cout << " [-n|--nat] enable permantent automatic nat detection(use only with anytun)" << std::endl;
203 std::cout << " [-o|--no-nat-once] disable automatic nat detection for new connections" << std::endl;
204 // std::cout << " [-I|--sync-interface] <ip-address> local unicast(sync) ip address to bind to" << std::endl;
205 std::cout << " [-S|--sync-port] <port> local unicast(sync) port to bind to" << std::endl;
206 std::cout << " [-M|--sync-hosts] <hostname|ip>:<port>[,<hostname|ip>:<port>[...]]"<< std::endl;
207 std::cout << " List of Remote Sync Hosts/Ports"<< std::endl;
210 void Options::printOptions()
212 Lock lock(mutex);
213 std::cout << "Options:" << std::endl;
214 std::cout << "chroot='" << chroot_ << "'" << std::endl;
215 std::cout << "username='" << username_ << "'" << std::endl;
216 std::cout << "chroot-dir='" << chroot_dir_ << "'" << std::endl;
217 std::cout << "daemonize='" << daemonize_ << "'" << std::endl;
218 std::cout << "control-interface='" << control_interface_.toString() << "'" << std::endl;
221 std::string Options::getProgname()
223 Lock lock(mutex);
224 return progname_;
227 bool Options::getChroot()
229 Lock lock(mutex);
230 return chroot_;
233 bool Options::getNat()
235 Lock lock(mutex);
236 return nat_;
239 bool Options::getNoNatOnce()
241 Lock lock(mutex);
242 return no_nat_once_;
245 std::string Options::getUsername()
247 Lock lock(mutex);
248 return username_;
251 std::string Options::getChrootDir()
253 Lock lock(mutex);
254 return chroot_dir_;
257 bool Options::getDaemonize()
259 Lock lock(mutex);
260 return daemonize_;
263 Host Options::getControlInterface()
265 Lock lock(mutex);
266 return control_interface_;
269 u_int16_t Options::getLocalSyncPort()
271 return local_sync_port_;
274 Options& Options::setLocalSyncPort(u_int16_t l)
276 local_sync_port_ = l;
277 return *this;
280 u_int16_t Options::getRtpStartPort()
282 return rtp_start_port_;
285 Options& Options::setRtpStartPort(u_int16_t l)
287 rtp_start_port_ = l;
288 return *this;
291 u_int16_t Options::getRtpEndPort()
293 return rtp_end_port_;
296 Options& Options::setRtpEndPort(u_int16_t l)
298 rtp_end_port_ = l;
299 return *this;
302 ConnectToList Options::getConnectTo()
304 Lock lock(mutex);
305 return connect_to_;