Imported Upstream version 0.3
[anytun.git] / src / anyrtpproxy / options.cpp
1 /*
2  *  anytun
3  *
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.
12  *
13  *
14  *  Copyright (C) 2007-2008 Othmar Gsenger, Erwin Nindl, 
15  *                          Christian Pointner <satp@wirdorange.org>
16  *
17  *  This file is part of Anytun.
18  *
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.
22  *
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.
27  *
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/>.
30  */
31
32 #include <iostream>
33 #include <queue>
34 #include <string>
35 #include <sstream>
36
37 #include "options.h"
38
39 Options* Options::inst = NULL;
40 Mutex Options::instMutex;
41 Options& gOpt = Options::instance();
42
43 Options& Options::instance()
44 {
45   Lock lock(instMutex);
46   static instanceCleaner c;
47   if(!inst)
48     inst = new Options();
49   
50   return *inst;
51 }
52
53 void Host::splitAndSetAddrPort(std::string addr_port)
54 {
55   if(addr_port.length() >= 2 && addr_port[0] == ':' && addr_port[1] != ':') {
56     addr_ = "";
57     addr_port.erase(0,1);
58     std::stringstream tmp_stream(addr_port);    
59     tmp_stream >> port_;
60     return;
61   }
62
63   size_t pos = addr_port.find_first_of("[");
64
65   if(pos != std::string::npos && pos != 0)
66     return; // an [ was found but not at the beginning
67
68   bool hasPort = false;
69   if(pos != std::string::npos) {
70     addr_port.erase(pos, 1);
71     pos = addr_port.find_first_of("]");
72
73     if(pos == std::string::npos)
74       return; // no trailing ] although an leading [ was found
75
76     if(pos < addr_port.length()-2) {
77
78       if(addr_port[pos+1] != ':')
79         return; // wrong port delimieter
80
81       addr_port[pos+1] = '/';
82       hasPort = true;
83     }
84     else if(pos != addr_port.length()-1)
85       return; // to few characters left
86
87     addr_port.erase(pos, 1);
88   }
89
90   if(hasPort) {
91     std::stringstream tmp_stream(addr_port);
92
93     getline(tmp_stream, addr_, '/');
94     if(!tmp_stream.good())
95       return;
96
97     tmp_stream >> port_;
98   }
99   else {
100     addr_ = addr_port;
101     port_ = "2323"; // default sync port
102   }
103 }
104
105
106 Options::Options() : control_interface_("0.0.0.0", "22222")
107
108 {
109   progname_ = "anyrtpproxy";
110   chroot_ = false;
111   username_ = "nobody";
112   chroot_dir_ = "/var/run";
113   daemonize_ = true;
114   pid_file_ = "";
115   local_addr_ = "";
116         local_sync_port_ = "";
117         rtp_start_port_ = 34000;
118         rtp_end_port_ = 35000;
119         no_nat_once_ = false;
120         nat_ = false;
121 }
122
123 Options::~Options()
124 {
125 }
126
127 #define PARSE_BOOL_PARAM(SHORT, LONG, VALUE)             \
128     else if(str == SHORT || str == LONG)                 \
129       VALUE = true;
130
131 #define PARSE_INVERSE_BOOL_PARAM(SHORT, LONG, VALUE)     \
132     else if(str == SHORT || str == LONG)                 \
133       VALUE = false;
134
135 #define PARSE_SCALAR_PARAM(SHORT, LONG, VALUE)           \
136     else if(str == SHORT || str == LONG)                 \
137     {                                                    \
138       if(argc < 1 || argv[i+1][0] == '-')                \
139         return false;                                    \
140       std::stringstream tmp;                             \
141       tmp << argv[i+1];                                  \
142       tmp >> VALUE;                                      \
143       argc--;                                            \
144       i++;                                               \
145     }
146
147 #define PARSE_SCALAR_PARAM2(SHORT, LONG, VALUE1, VALUE2) \
148     else if(str == SHORT || str == LONG)                 \
149     {                                                    \
150       if(argc < 2 ||                                     \
151          argv[i+1][0] == '-' || argv[i+2][0] == '-')     \
152         return false;                                    \
153       std::stringstream tmp;                             \
154       tmp << argv[i+1] << " " << argv[i+2];              \
155       tmp >> VALUE1;                                     \
156       tmp >> VALUE2;                                     \
157       argc-=2;                                           \
158       i+=2;                                              \
159     }
160
161 #define PARSE_STRING_PARAM(SHORT, LONG, VALUE)           \
162     else if(str == SHORT || str == LONG)                 \
163     {                                                    \
164       if(argc < 1 || argv[i+1][0] == '-')                \
165         return false;                                    \
166       VALUE = std::string(argv[i+1]);                    \
167       argc--;                                            \
168       i++;                                               \
169     }
170
171 #define PARSE_HEXSTRING_PARAM_SEC(SHORT, LONG, VALUE)    \
172     else if(str == SHORT || str == LONG)                 \
173     {                                                    \
174       if(argc < 1 || argv[i+1][0] == '-')                \
175         return false;                                    \
176       VALUE = Buffer(std::string(argv[i+1]));            \
177       for(size_t j=0; j < strlen(argv[i+1]); ++j)        \
178         argv[i+1][j] = '#';                              \
179       argc--;                                            \
180       i++;                                               \
181     }
182
183 #define PARSE_CSLIST_PARAM(SHORT, LONG, LIST)            \
184     else if(str == SHORT || str == LONG)                 \
185     {                                                    \
186       if(argc < 1 || argv[i+1][0] == '-')                \
187         return false;                                    \
188       std::stringstream tmp(argv[i+1]);                  \
189       /* LIST.clear(); */                                \
190                         while (tmp.good())                                 \
191                         {                                                  \
192                                 std::string tmp_line;                            \
193                                 getline(tmp,tmp_line,',');                       \
194                                 LIST.push(tmp_line);                        \
195                         }                                                  \
196       argc--;                                            \
197       i++;                                               \
198     }
199
200 bool Options::parse(int argc, char* argv[])
201 {
202   Lock lock(mutex);
203
204   progname_ = argv[0];
205   std::queue<std::string> host_port_queue;
206   argc--;
207   for(int i=1; argc > 0; ++i)
208   {
209     std::string str(argv[i]);
210     argc--;
211
212     if(str == "-h" || str == "--help")
213       return false;
214     PARSE_BOOL_PARAM("-t","--chroot", chroot_)
215     PARSE_BOOL_PARAM("-n","--nat", nat_)
216     PARSE_BOOL_PARAM("-o","--no-nat-once", no_nat_once_)
217     PARSE_SCALAR_PARAM("-u","--user", username_)
218     PARSE_SCALAR_PARAM("-c","--chroot-dir", chroot_dir_)
219     PARSE_INVERSE_BOOL_PARAM("-d","--nodaemonize", daemonize_)
220     PARSE_SCALAR_PARAM("-P","--write-pid", pid_file_)
221     PARSE_SCALAR_PARAM("-i","--interface", local_addr_)
222     PARSE_STRING_PARAM("-s","--control", control_interface_)
223     PARSE_SCALAR_PARAM2("-p","--port-range", rtp_start_port_, rtp_end_port_)
224                 PARSE_CSLIST_PARAM("-M","--sync-hosts", host_port_queue)
225     PARSE_SCALAR_PARAM("-S","--sync-port", local_sync_port_)
226     PARSE_SCALAR_PARAM("-I","--sync-interface", local_sync_addr_)
227     else 
228       return false;
229   }
230   while(!host_port_queue.empty())
231   {
232     std::stringstream tmp_stream(host_port_queue.front());
233     OptionConnectTo oct;
234     getline(tmp_stream,oct.host,':');
235     if(!tmp_stream.good())
236       return false;
237     tmp_stream >> oct.port;
238     host_port_queue.pop();
239     connect_to_.push_back(oct);
240   }
241   
242   return sanityCheck();
243 }
244
245 bool Options::sanityCheck()
246 {
247   if(control_interface_.port_ == "") control_interface_.port_ = "22222";
248   return true;
249 }
250
251 void Options::printUsage()
252 {
253   std::cout << "USAGE: anyrtpproxy" << std::endl;
254   std::cout << "  [-h|--help]                      prints this..." << std::endl;
255   std::cout << "  [-t|--chroot]                    chroot and drop priviledges" << std::endl;
256   std::cout << "  [-u|--username] <username>       in case of chroot run as this user" << std::endl;
257   std::cout << "  [-c|--chroot-dir] <directory>    directory to make a chroot to" << std::endl;
258   std::cout << "  [-d|--nodaemonize]               don't run in background" << std::endl;
259   std::cout << "  [-P|--write-pid] <path>          write pid to this file" << std::endl;
260   std::cout << "  [-i|--interface] <ip-address>    local ip address to listen to for RTP packets" << std::endl;
261   std::cout << "  [-s|--control] <addr>[:<port>]   the address/port to listen on for control commands" << std::endl;
262   std::cout << "  [-p|--port-range] <start> <end>  port range used to relay rtp connections" << std::endl;
263   std::cout << "  [-n|--nat]                       enable permantent automatic nat detection(use only with anytun)" << std::endl;
264   std::cout << "  [-o|--no-nat-once]               disable automatic nat detection for new connections" << std::endl;
265   std::cout << "  [-I|--sync-interface] <ip-address>  local unicast(sync) ip address to bind to" << std::endl;
266   std::cout << "  [-S|--sync-port] <port>          local unicast(sync) port to bind to" << std::endl;
267   std::cout << "  [-M|--sync-hosts] <hostname|ip>:<port>[,<hostname|ip>:<port>[...]]"<< std::endl;
268   std::cout << "                                   List of Remote Sync Hosts/Ports"<< std::endl;
269 }
270
271 void Options::printOptions()
272 {
273   Lock lock(mutex);
274   std::cout << "Options:" << std::endl;
275   std::cout << "chroot='" << chroot_ << "'" << std::endl;
276   std::cout << "username='" << username_ << "'" << std::endl;
277   std::cout << "chroot-dir='" << chroot_dir_ << "'" << std::endl;
278   std::cout << "daemonize='" << daemonize_ << "'" << std::endl;
279   std::cout << "pid_file='" << pid_file_ << "'" << std::endl;
280   std::cout << "control-interface='" << control_interface_.toString() << "'" << std::endl;
281   std::cout << "local_addr='" << local_addr_ << "'" << std::endl;
282   std::cout << "rtp_start_port=" << rtp_start_port_ << std::endl;
283   std::cout << "rtp_end_port=" << rtp_end_port_ << std::endl;
284   std::cout << "local_sync_addr='" << local_sync_addr_ << "'" << std::endl;
285   std::cout << "local_sync_port='" << local_sync_port_ << "'" << std::endl;
286 }
287
288 std::string Options::getProgname()
289 {
290   Lock lock(mutex);
291   return progname_;
292 }
293
294 bool Options::getChroot()
295 {
296   Lock lock(mutex);
297   return chroot_;
298 }
299
300 bool Options::getNat()
301 {
302   Lock lock(mutex);
303   return nat_;
304 }
305
306 bool Options::getNoNatOnce()
307 {
308   Lock lock(mutex);
309   return no_nat_once_;
310 }
311
312 std::string Options::getUsername()
313 {
314   Lock lock(mutex);
315   return username_;
316 }
317
318 std::string Options::getChrootDir()
319 {
320   Lock lock(mutex);
321   return chroot_dir_;
322 }
323
324 bool Options::getDaemonize()
325 {
326   Lock lock(mutex);
327   return daemonize_;
328 }
329
330 std::string Options::getPidFile()
331 {
332   Lock lock(mutex);
333   return pid_file_;
334 }
335
336 Host Options::getControlInterface()
337 {
338   Lock lock(mutex);
339   return control_interface_;
340 }
341
342 std::string Options::getLocalAddr()
343 {
344   Lock lock(mutex);
345   return local_addr_;
346 }
347
348 Options& Options::setLocalAddr(std::string l)
349 {
350   Lock lock(mutex);
351   local_addr_ = l;
352   return *this;
353 }
354
355 std::string Options::getLocalSyncAddr()
356 {
357   Lock lock(mutex);
358   return local_sync_addr_;
359 }
360
361 Options& Options::setLocalSyncAddr(std::string l)
362 {
363   Lock lock(mutex);
364   local_sync_addr_ = l;
365   return *this;
366 }
367
368 std::string Options::getLocalSyncPort()
369 {
370   Lock lock(mutex);
371   return local_sync_port_;
372 }
373
374 Options& Options::setLocalSyncPort(std::string l)
375 {
376   Lock lock(mutex);
377   local_sync_port_ = l;
378   return *this;
379 }
380
381 u_int16_t Options::getRtpStartPort()
382 {
383   return rtp_start_port_;
384 }
385
386 Options& Options::setRtpStartPort(u_int16_t l)
387 {
388   rtp_start_port_ = l;
389   return *this;
390 }
391
392 u_int16_t Options::getRtpEndPort()
393 {
394   return rtp_end_port_;
395 }
396
397 Options& Options::setRtpEndPort(u_int16_t l)
398 {
399   rtp_end_port_ = l;
400   return *this;
401 }
402
403 ConnectToList Options::getConnectTo()
404 {
405   Lock lock(mutex);
406   return connect_to_;
407 }
408