redone the bad anyrtpproxy
[anytun.git] / anyrtpproxy / options.cpp
blob2acc597c806b88fe7c33e0cded6bf8adecd52bbe
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", 22220)
55 progname_ = "anyrtpproxy";
56 chroot_ = false;
57 username_ = "nobody";
58 chroot_dir_ = "/var/run";
59 daemonize_ = true;
60 send_port_ = 22221;
61 remote_hosts_.push_back(Host("127.0.0.1", 22222));
64 Options::~Options()
68 #define PARSE_BOOL_PARAM(SHORT, LONG, VALUE) \
69 else if(str == SHORT || str == LONG) \
70 VALUE = true;
72 #define PARSE_INVERSE_BOOL_PARAM(SHORT, LONG, VALUE) \
73 else if(str == SHORT || str == LONG) \
74 VALUE = false;
76 #define PARSE_SCALAR_PARAM(SHORT, LONG, VALUE) \
77 else if(str == SHORT || str == LONG) \
78 { \
79 if(argc < 1 || argv[i+1][0] == '-') \
80 return false; \
81 std::stringstream tmp; \
82 tmp << argv[i+1]; \
83 tmp >> VALUE; \
84 argc--; \
85 i++; \
88 #define PARSE_SCALAR_PARAM2(SHORT, LONG, VALUE1, VALUE2) \
89 else if(str == SHORT || str == LONG) \
90 { \
91 if(argc < 2 || \
92 argv[i+1][0] == '-' || argv[i+2][0] == '-') \
93 return false; \
94 std::stringstream tmp; \
95 tmp << argv[i+1] << " " << argv[i+2]; \
96 tmp >> VALUE1; \
97 tmp >> VALUE2; \
98 argc-=2; \
99 i+=2; \
102 #define PARSE_STRING_PARAM(SHORT, LONG, VALUE) \
103 else if(str == SHORT || str == LONG) \
105 if(argc < 1 || argv[i+1][0] == '-') \
106 return false; \
107 VALUE = std::string(argv[i+1]); \
108 argc--; \
109 i++; \
112 #define PARSE_HEXSTRING_PARAM(SHORT, LONG, VALUE) \
113 else if(str == SHORT || str == LONG) \
115 if(argc < 1 || argv[i+1][0] == '-') \
116 return false; \
117 VALUE = Buffer(std::string(argv[i+1])); \
118 argc--; \
119 i++; \
123 #define PARSE_CSLIST_PARAM(SHORT, LONG, LIST) \
124 else if(str == SHORT || str == LONG) \
126 if(argc < 1 || argv[i+1][0] == '-') \
127 return false; \
128 std::stringstream tmp(argv[i+1]); \
129 LIST.clear(); \
130 while (tmp.good()) \
132 std::string tmp_line; \
133 getline(tmp,tmp_line,','); \
134 LIST.push_back(tmp_line); \
136 argc--; \
137 i++; \
140 bool Options::parse(int argc, char* argv[])
142 Lock lock(mutex);
144 progname_ = argv[0];
145 argc--;
146 for(int i=1; argc > 0; ++i)
148 std::string str(argv[i]);
149 argc--;
151 if(str == "-h" || str == "--help")
152 return false;
153 PARSE_BOOL_PARAM("-t","--chroot", chroot_)
154 PARSE_SCALAR_PARAM("-u","--user", username_)
155 PARSE_SCALAR_PARAM("-c","--chroot-dir", chroot_dir_)
156 PARSE_INVERSE_BOOL_PARAM("-d","--nodaemonize", daemonize_)
157 PARSE_STRING_PARAM("-s","--control", control_interface_)
158 PARSE_SCALAR_PARAM("-p","--port", send_port_)
159 PARSE_CSLIST_PARAM("-r","--remote-hosts", remote_hosts_)
160 else
161 return false;
164 return sanityCheck();
167 bool Options::sanityCheck()
169 if(control_interface_.port_) control_interface_.port_ = 22220;
171 HostList::iterator it=remote_hosts_.begin();
172 for(u_int32_t i=0; it != remote_hosts_.end(); ++it, ++i)
173 if(!it->port_) it->port_ = 22222;
175 return true;
178 void Options::printUsage()
180 std::cout << "USAGE:" << std::endl;
181 std::cout << "anyrtpproxy [-h|--help] prints this..." << std::endl;
182 std::cout << " [-t|--chroot] chroot and drop priviledges" << std::endl;
183 std::cout << " [-u|--username] <username> in case of chroot run as this user" << std::endl;
184 std::cout << " [-c|--chroot-dir] <directory> directory to make a chroot to" << std::endl;
185 std::cout << " [-d|--nodaemonize] don't run in background" << std::endl;
186 std::cout << " [-s|--control] <addr[:port]> the address/port to listen on for control commands" << std::endl;
187 std::cout << " [-p|--port] <port> use this port to send out packets to remote hosts" << std::endl;
188 std::cout << " [-r|--remote-hosts] <addr[:port]>[,<addr[:<port]> .. ] a list of remote hosts to send duplicates to" << std::endl;
191 void Options::printOptions()
193 Lock lock(mutex);
194 std::cout << "Options:" << std::endl;
195 std::cout << "chroot='" << chroot_ << "'" << std::endl;
196 std::cout << "username='" << username_ << "'" << std::endl;
197 std::cout << "chroot-dir='" << chroot_dir_ << "'" << std::endl;
198 std::cout << "daemonize='" << daemonize_ << "'" << std::endl;
199 std::cout << "control-interface='" << control_interface_.toString() << "'" << std::endl;
200 std::cout << "send-port='" << send_port_ << "'" << std::endl;
201 std::cout << "remote hosts='";
202 HostList::const_iterator it=remote_hosts_.begin();
203 for(u_int32_t i=0; it != remote_hosts_.end(); ++it, ++i)
205 if(i) std::cout << "','";
206 std::cout << it->toString();
208 std::cout << "'" << std::endl;
211 std::string Options::getProgname()
213 Lock lock(mutex);
214 return progname_;
217 bool Options::getChroot()
219 Lock lock(mutex);
220 return chroot_;
223 std::string Options::getUsername()
225 Lock lock(mutex);
226 return username_;
229 std::string Options::getChrootDir()
231 Lock lock(mutex);
232 return chroot_dir_;
235 bool Options::getDaemonize()
237 Lock lock(mutex);
238 return daemonize_;
241 u_int16_t Options::getSendPort()
243 Lock lock(mutex);
244 return send_port_;
247 Host Options::getControlInterface()
249 Lock lock(mutex);
250 return control_interface_;
253 HostList Options::getRemoteHosts()
255 Lock lock(mutex);
256 return remote_hosts_;