Imported Upstream version 0.3.2
[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-2009 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 as published by
21  *  the Free Software Foundation, either version 3 of the License, or
22  *  any later version.
23  *
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.
28  *
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/>.
31  */
32
33 #include <iostream>
34 #include <queue>
35 #include <string>
36 #include <sstream>
37
38 #include "options.h"
39
40 Options* Options::inst = NULL;
41 Mutex Options::instMutex;
42 Options& gOpt = Options::instance();
43
44 Options& Options::instance()
45 {
46   Lock lock(instMutex);
47   static instanceCleaner c;
48   if(!inst)
49     inst = new Options();
50   
51   return *inst;
52 }
53
54 void Host::splitAndSetAddrPort(std::string addr_port)
55 {
56   if(addr_port.length() >= 2 && addr_port[0] == ':' && addr_port[1] != ':') {
57     addr_ = "";
58     addr_port.erase(0,1);
59     std::stringstream tmp_stream(addr_port);    
60     tmp_stream >> port_;
61     return;
62   }
63
64   size_t pos = addr_port.find_first_of("[");
65
66   if(pos != std::string::npos && pos != 0)
67     return; // an [ was found but not at the beginning
68
69   bool hasPort = false;
70   if(pos != std::string::npos) {
71     addr_port.erase(pos, 1);
72     pos = addr_port.find_first_of("]");
73
74     if(pos == std::string::npos)
75       return; // no trailing ] although an leading [ was found
76
77     if(pos < addr_port.length()-2) {
78
79       if(addr_port[pos+1] != ':')
80         return; // wrong port delimieter
81
82       addr_port[pos+1] = '/';
83       hasPort = true;
84     }
85     else if(pos != addr_port.length()-1)
86       return; // to few characters left
87
88     addr_port.erase(pos, 1);
89   }
90
91   if(hasPort) {
92     std::stringstream tmp_stream(addr_port);
93
94     getline(tmp_stream, addr_, '/');
95     if(!tmp_stream.good())
96       return;
97
98     tmp_stream >> port_;
99   }
100   else {
101     addr_ = addr_port;
102     port_ = "2323"; // default sync port
103   }
104 }
105
106
107 Options::Options() : control_interface_("0.0.0.0", "22222")
108
109 {
110   progname_ = "anyrtpproxy";
111   chroot_ = false;
112   username_ = "nobody";
113   chroot_dir_ = "/var/run";
114   daemonize_ = true;
115   pid_file_ = "";
116   local_addr_ = "";
117         local_sync_port_ = "";
118         rtp_start_port_ = 34000;
119         rtp_end_port_ = 35000;
120         no_nat_once_ = false;
121         nat_ = false;
122 }
123
124 Options::~Options()
125 {
126 }
127
128 #define PARSE_BOOL_PARAM(SHORT, LONG, VALUE)             \
129     else if(str == SHORT || str == LONG)                 \
130       VALUE = true;
131
132 #define PARSE_INVERSE_BOOL_PARAM(SHORT, LONG, VALUE)     \
133     else if(str == SHORT || str == LONG)                 \
134       VALUE = false;
135
136 #define PARSE_SCALAR_PARAM(SHORT, LONG, VALUE)           \
137     else if(str == SHORT || str == LONG)                 \
138     {                                                    \
139       if(argc < 1 || argv[i+1][0] == '-')                \
140         return false;                                    \
141       std::stringstream tmp;                             \
142       tmp << argv[i+1];                                  \
143       tmp >> VALUE;                                      \
144       argc--;                                            \
145       i++;                                               \
146     }
147
148 #define PARSE_SCALAR_PARAM2(SHORT, LONG, VALUE1, VALUE2) \
149     else if(str == SHORT || str == LONG)                 \
150     {                                                    \
151       if(argc < 2 ||                                     \
152          argv[i+1][0] == '-' || argv[i+2][0] == '-')     \
153         return false;                                    \
154       std::stringstream tmp;                             \
155       tmp << argv[i+1] << " " << argv[i+2];              \
156       tmp >> VALUE1;                                     \
157       tmp >> VALUE2;                                     \
158       argc-=2;                                           \
159       i+=2;                                              \
160     }
161
162 #define PARSE_STRING_PARAM(SHORT, LONG, VALUE)           \
163     else if(str == SHORT || str == LONG)                 \
164     {                                                    \
165       if(argc < 1 || argv[i+1][0] == '-')                \
166         return false;                                    \
167       VALUE = std::string(argv[i+1]);                    \
168       argc--;                                            \
169       i++;                                               \
170     }
171
172 #define PARSE_HEXSTRING_PARAM_SEC(SHORT, LONG, VALUE)    \
173     else if(str == SHORT || str == LONG)                 \
174     {                                                    \
175       if(argc < 1 || argv[i+1][0] == '-')                \
176         return false;                                    \
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] = '#';                              \
180       argc--;                                            \
181       i++;                                               \
182     }
183
184 #define PARSE_CSLIST_PARAM(SHORT, LONG, LIST)            \
185     else if(str == SHORT || str == LONG)                 \
186     {                                                    \
187       if(argc < 1 || argv[i+1][0] == '-')                \
188         return false;                                    \
189       std::stringstream tmp(argv[i+1]);                  \
190       /* LIST.clear(); */                                \
191                         while (tmp.good())                                 \
192                         {                                                  \
193                                 std::string tmp_line;                            \
194                                 getline(tmp,tmp_line,',');                       \
195                                 LIST.push(tmp_line);                        \
196                         }                                                  \
197       argc--;                                            \
198       i++;                                               \
199     }
200
201 bool Options::parse(int argc, char* argv[])
202 {
203   Lock lock(mutex);
204
205   progname_ = argv[0];
206   std::queue<std::string> host_port_queue;
207   argc--;
208   for(int i=1; argc > 0; ++i)
209   {
210     std::string str(argv[i]);
211     argc--;
212
213     if(str == "-h" || str == "--help")
214       return false;
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_)
228     else 
229       return false;
230   }
231   while(!host_port_queue.empty())
232   {
233     std::stringstream tmp_stream(host_port_queue.front());
234     OptionConnectTo oct;
235     getline(tmp_stream,oct.host,':');
236     if(!tmp_stream.good())
237       return false;
238     tmp_stream >> oct.port;
239     host_port_queue.pop();
240     connect_to_.push_back(oct);
241   }
242   
243   return sanityCheck();
244 }
245
246 bool Options::sanityCheck()
247 {
248   if(control_interface_.port_ == "") control_interface_.port_ = "22222";
249   return true;
250 }
251
252 void Options::printUsage()
253 {
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;
270 }
271
272 void Options::printOptions()
273 {
274   Lock lock(mutex);
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;
287 }
288
289 std::string Options::getProgname()
290 {
291   Lock lock(mutex);
292   return progname_;
293 }
294
295 bool Options::getChroot()
296 {
297   Lock lock(mutex);
298   return chroot_;
299 }
300
301 bool Options::getNat()
302 {
303   Lock lock(mutex);
304   return nat_;
305 }
306
307 bool Options::getNoNatOnce()
308 {
309   Lock lock(mutex);
310   return no_nat_once_;
311 }
312
313 std::string Options::getUsername()
314 {
315   Lock lock(mutex);
316   return username_;
317 }
318
319 std::string Options::getChrootDir()
320 {
321   Lock lock(mutex);
322   return chroot_dir_;
323 }
324
325 bool Options::getDaemonize()
326 {
327   Lock lock(mutex);
328   return daemonize_;
329 }
330
331 std::string Options::getPidFile()
332 {
333   Lock lock(mutex);
334   return pid_file_;
335 }
336
337 Host Options::getControlInterface()
338 {
339   Lock lock(mutex);
340   return control_interface_;
341 }
342
343 std::string Options::getLocalAddr()
344 {
345   Lock lock(mutex);
346   return local_addr_;
347 }
348
349 Options& Options::setLocalAddr(std::string l)
350 {
351   Lock lock(mutex);
352   local_addr_ = l;
353   return *this;
354 }
355
356 std::string Options::getLocalSyncAddr()
357 {
358   Lock lock(mutex);
359   return local_sync_addr_;
360 }
361
362 Options& Options::setLocalSyncAddr(std::string l)
363 {
364   Lock lock(mutex);
365   local_sync_addr_ = l;
366   return *this;
367 }
368
369 std::string Options::getLocalSyncPort()
370 {
371   Lock lock(mutex);
372   return local_sync_port_;
373 }
374
375 Options& Options::setLocalSyncPort(std::string l)
376 {
377   Lock lock(mutex);
378   local_sync_port_ = l;
379   return *this;
380 }
381
382 u_int16_t Options::getRtpStartPort()
383 {
384   return rtp_start_port_;
385 }
386
387 Options& Options::setRtpStartPort(u_int16_t l)
388 {
389   rtp_start_port_ = l;
390   return *this;
391 }
392
393 u_int16_t Options::getRtpEndPort()
394 {
395   return rtp_end_port_;
396 }
397
398 Options& Options::setRtpEndPort(u_int16_t l)
399 {
400   rtp_end_port_ = l;
401   return *this;
402 }
403
404 ConnectToList Options::getConnectTo()
405 {
406   Lock lock(mutex);
407   return connect_to_;
408 }
409