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