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