created eps files for black-wight logo
[anytun.git] / options.cpp
blob4aedd43239fc4c1bb2dedabe06492cfd88fd57cd
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 "datatypes.h"
37 #include "options.h"
39 Options* Options::inst = NULL;
40 Mutex Options::instMutex;
41 Options& gOpt = Options::instance();
43 Options& Options::instance()
45 Lock lock(instMutex);
46 static instanceCleaner c;
47 if(!inst)
48 inst = new Options();
50 return *inst;
53 Options::Options() : key_(u_int32_t(0)), salt_(u_int32_t(0))
55 progname_ = "anytun";
56 daemonize_ = true;
57 sender_id_ = 0;
58 local_addr_ = "";
59 local_port_ = 4444;
60 local_sync_port_ = 0;
61 remote_sync_port_ = 0;
62 remote_sync_addr_ = "";
63 remote_addr_ = "";
64 remote_port_ = 4444;
65 dev_name_ = "tap";
66 dev_type_ = "";
67 ifconfig_param_local_ = "192.168.200.1";
68 ifconfig_param_remote_netmask_ = "255.255.255.0";
69 seq_window_size_ = 100;
70 cipher_ = "aes-ctr";
71 kd_prf_ = "aes-ctr";
72 auth_algo_ = "sha1";
73 mux_ = 0;
76 Options::~Options()
80 #define PARSE_BOOL_PARAM(SHORT, LONG, VALUE) \
81 else if(str == SHORT || str == LONG) \
82 VALUE = true;
84 #define PARSE_INVERSE_BOOL_PARAM(SHORT, LONG, VALUE) \
85 else if(str == SHORT || str == LONG) \
86 VALUE = false;
88 #define PARSE_SCALAR_PARAM(SHORT, LONG, VALUE) \
89 else if(str == SHORT || str == LONG) \
90 { \
91 if(argc < 1 || argv[i+1][0] == '-') \
92 return false; \
93 std::stringstream tmp; \
94 tmp << argv[i+1]; \
95 tmp >> VALUE; \
96 argc--; \
97 i++; \
100 #define PARSE_SCALAR_PARAM2(SHORT, LONG, VALUE1, VALUE2) \
101 else if(str == SHORT || str == LONG) \
103 if(argc < 2 || \
104 argv[i+1][0] == '-' || argv[i+2][0] == '-') \
105 return false; \
106 std::stringstream tmp; \
107 tmp << argv[i+1] << " " << argv[i+2]; \
108 tmp >> VALUE1; \
109 tmp >> VALUE2; \
110 argc-=2; \
111 i+=2; \
114 #define PARSE_HEXSTRING_PARAM(SHORT, LONG, VALUE) \
115 else if(str == SHORT || str == LONG) \
117 if(argc < 1 || argv[i+1][0] == '-') \
118 return false; \
119 VALUE = Buffer(std::string(argv[i+1])); \
120 argc--; \
121 i++; \
124 #define PARSE_CSLIST_PARAM(SHORT, LONG, LIST) \
125 else if(str == SHORT || str == LONG) \
127 if(argc < 1 || argv[i+1][0] == '-') \
128 return false; \
129 std::stringstream tmp(argv[i+1]); \
130 while (tmp.good()) \
132 std::string tmp_line; \
133 getline(tmp,tmp_line,','); \
134 LIST.push(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 std::queue<std::string> host_port_queue;
147 for(int i=1; argc > 0; ++i)
149 std::string str(argv[i]);
150 argc--;
152 if(str == "-h" || str == "--help")
153 return false;
154 PARSE_INVERSE_BOOL_PARAM("-D","--nodaemonize", daemonize_)
155 PARSE_SCALAR_PARAM("-s","--sender-id", sender_id_)
156 PARSE_SCALAR_PARAM("-i","--interface", local_addr_)
157 PARSE_SCALAR_PARAM("-p","--port", local_port_)
158 PARSE_SCALAR_PARAM("-S","--sync-port", local_sync_port_)
159 PARSE_SCALAR_PARAM("-I","--sync-interface", local_sync_addr_)
160 PARSE_SCALAR_PARAM("-R","--remote-sync-host", remote_sync_addr_)
161 PARSE_SCALAR_PARAM("-O","--remote-sync-port", remote_sync_port_)
162 PARSE_SCALAR_PARAM("-r","--remote-host", remote_addr_)
163 PARSE_SCALAR_PARAM("-o","--remote-port", remote_port_)
164 PARSE_SCALAR_PARAM("-d","--dev", dev_name_)
165 PARSE_SCALAR_PARAM("-t","--type", dev_type_)
166 PARSE_SCALAR_PARAM2("-n","--ifconfig", ifconfig_param_local_, ifconfig_param_remote_netmask_)
167 PARSE_SCALAR_PARAM("-w","--window-size", seq_window_size_)
168 PARSE_SCALAR_PARAM("-m","--mux", mux_)
169 PARSE_SCALAR_PARAM("-c","--cipher", cipher_)
170 PARSE_HEXSTRING_PARAM("-K","--key", key_)
171 PARSE_HEXSTRING_PARAM("-A","--salt", salt_)
172 PARSE_SCALAR_PARAM("-k","--kd-prf", kd_prf_)
173 PARSE_SCALAR_PARAM("-a","--auth-algo", auth_algo_)
174 PARSE_CSLIST_PARAM("-M","--sync-hosts", host_port_queue)
175 else
176 return false;
179 if(cipher_ == "null" && auth_algo_ == "null")
180 kd_prf_ = "null";
181 if((cipher_ != "null" || auth_algo_ != "null") && kd_prf_ == "null")
182 kd_prf_ = "aes-ctr";
184 while(!host_port_queue.empty())
186 std::stringstream tmp_stream(host_port_queue.front());
187 OptionConnectTo oct;
188 getline(tmp_stream,oct.host,':');
189 if(!tmp_stream.good())
190 return false;
191 tmp_stream >> oct.port;
192 host_port_queue.pop();
193 connect_to_.push_back(oct);
195 return true;
198 void Options::printUsage()
200 std::cout << "USAGE:" << std::endl;
201 std::cout << "anytun [-h|--help] prints this..." << std::endl;
202 // std::cout << " [-f|--config] <file> the config file" << std::endl;
203 std::cout << " [-D|--nodaemonize] don't run in background" << std::endl;
204 std::cout << " [-s|--sender-id ] <sender id> the sender id to use" << std::endl;
205 std::cout << " [-i|--interface] <ip-address> local anycast ip address to bind to" << std::endl;
206 std::cout << " [-p|--port] <port> local anycast(data) port to bind to" << std::endl;
207 std::cout << " [-I|--sync-interface] <ip-address> local unicast(sync) ip address to bind to" << std::endl;
208 std::cout << " [-S|--sync-port] <port> local unicast(sync) port to bind to" << std::endl;
209 std::cout << " [-M|--sync-hosts] <hostname|ip>:<port>[,<hostname|ip>:<port>[...]]"<< std::endl;
210 std::cout << " remote hosts to sync with" << std::endl;
211 std::cout << " [-r|--remote-host] <hostname|ip> remote host" << std::endl;
212 std::cout << " [-o|--remote-port] <port> remote port" << std::endl;
213 std::cout << " [-d|--dev] <name> device name" << std::endl;
214 std::cout << " [-t|--type] <tun|tap> device type" << std::endl;
215 std::cout << " [-n|--ifconfig] <local> the local address for the tun/tap device" << std::endl
216 << " <remote|netmask> the remote address(tun) or netmask(tap)" << std::endl;
217 std::cout << " [-w|--window-size] <window size> seqence number window size" << std::endl;
218 std::cout << " [-m|--mux] <mux-id> the multiplex id to use" << std::endl;
219 std::cout << " [-c|--cipher] <cipher type> payload encryption algorithm" << std::endl;
220 std::cout << " [-K|--key] <master key> master key to use for encryption" << std::endl;
221 std::cout << " [-A|--salt] <master salt> master salt to use for encryption" << std::endl;
222 std::cout << " [-k|--kd-prf] <kd-prf type> key derivation pseudo random function" << std::endl;
223 std::cout << " [-a|--auth-algo] <algo type> message authentication algorithm" << std::endl;
226 void Options::printOptions()
228 Lock lock(mutex);
229 std::cout << "Options:" << std::endl;
230 std::cout << "daemonize=" << daemonize_ << std::endl;
231 std::cout << "sender_id='" << sender_id_ << "'" << std::endl;
232 std::cout << "local_addr='" << local_addr_ << "'" << std::endl;
233 std::cout << "local_port='" << local_port_ << "'" << std::endl;
234 std::cout << "local_sync_addr='" << local_sync_addr_ << "'" << std::endl;
235 std::cout << "local_sync_port='" << local_sync_port_ << "'" << std::endl;
236 std::cout << "remote_addr='" << remote_addr_ << "'" << std::endl;
237 std::cout << "remote_port='" << remote_port_ << "'" << std::endl;
238 std::cout << "dev_name='" << dev_name_ << "'" << std::endl;
239 std::cout << "dev_type='" << dev_type_ << "'" << std::endl;
240 std::cout << "ifconfig_param_local='" << ifconfig_param_local_ << "'" << std::endl;
241 std::cout << "ifconfig_param_remote_netmask='" << ifconfig_param_remote_netmask_ << "'" << std::endl;
242 std::cout << "seq_window_size='" << seq_window_size_ << "'" << std::endl;
243 std::cout << "mux_id='" << mux_ << "'" << std::endl;
244 std::cout << "cipher='" << cipher_ << "'" << std::endl;
245 std::cout << "key=" << key_.getHexDumpOneLine() << std::endl;
246 std::cout << "salt=" << salt_.getHexDumpOneLine() << std::endl;
247 std::cout << "kd_prf='" << kd_prf_ << "'" << std::endl;
248 std::cout << "auth_algo='" << auth_algo_ << "'" << std::endl;
251 std::string Options::getProgname()
253 Lock lock(mutex);
254 return progname_;
258 Options& Options::setProgname(std::string p)
260 Lock lock(mutex);
261 progname_ = p;
262 return *this;
265 bool Options::getDaemonize()
267 return daemonize_;
270 Options& Options::setDaemonize(bool d)
272 daemonize_ = d;
273 return *this;
276 ConnectToList Options::getConnectTo()
278 Lock lock(mutex);
279 return connect_to_;
282 sender_id_t Options::getSenderId()
284 return sender_id_;
287 Options& Options::setSenderId(sender_id_t s)
289 sender_id_ = s;
290 return *this;
293 std::string Options::getLocalAddr()
295 Lock lock(mutex);
296 return local_addr_;
299 Options& Options::setLocalAddr(std::string l)
301 Lock lock(mutex);
302 local_addr_ = l;
303 return *this;
306 std::string Options::getLocalSyncAddr()
308 Lock lock(mutex);
309 return local_sync_addr_;
312 Options& Options::setLocalSyncAddr(std::string l)
314 Lock lock(mutex);
315 local_sync_addr_ = l;
316 return *this;
319 u_int16_t Options::getLocalPort()
321 return local_port_;
324 Options& Options::setLocalPort(u_int16_t l)
326 local_port_ = l;
327 return *this;
330 u_int16_t Options::getLocalSyncPort()
332 return local_sync_port_;
335 Options& Options::setLocalSyncPort(u_int16_t l)
337 local_sync_port_ = l;
338 return *this;
341 u_int16_t Options::getRemoteSyncPort()
343 return remote_sync_port_;
346 Options& Options::setRemoteSyncPort(u_int16_t l)
348 remote_sync_port_ = l;
349 return *this;
352 std::string Options::getRemoteSyncAddr()
354 Lock lock(mutex);
355 return remote_sync_addr_;
358 Options& Options::setRemoteSyncAddr(std::string r)
360 Lock lock(mutex);
361 remote_sync_addr_ = r;
362 return *this;
365 std::string Options::getRemoteAddr()
367 Lock lock(mutex);
368 return remote_addr_;
371 Options& Options::setRemoteAddr(std::string r)
373 Lock lock(mutex);
374 remote_addr_ = r;
375 return *this;
378 u_int16_t Options::getRemotePort()
380 return remote_port_;
383 Options& Options::setRemotePort(u_int16_t r)
385 remote_port_ = r;
386 return *this;
389 Options& Options::setRemoteAddrPort(std::string addr, u_int16_t port)
391 Lock lock(mutex);
392 remote_addr_ = addr;
393 remote_port_ = port;
394 return *this;
397 std::string Options::getDevName()
399 Lock lock(mutex);
400 return dev_name_;
403 std::string Options::getDevType()
405 Lock lock(mutex);
406 return dev_type_;
409 Options& Options::setDevName(std::string d)
411 Lock lock(mutex);
412 dev_name_ = d;
413 return *this;
416 Options& Options::setDevType(std::string d)
418 Lock lock(mutex);
419 dev_type_ = d;
420 return *this;
423 std::string Options::getIfconfigParamLocal()
425 Lock lock(mutex);
426 return ifconfig_param_local_;
429 Options& Options::setIfconfigParamLocal(std::string i)
431 Lock lock(mutex);
432 ifconfig_param_local_ = i;
433 return *this;
436 std::string Options::getIfconfigParamRemoteNetmask()
438 Lock lock(mutex);
439 return ifconfig_param_remote_netmask_;
442 Options& Options::setIfconfigParamRemoteNetmask(std::string i)
444 Lock lock(mutex);
445 ifconfig_param_remote_netmask_ = i;
446 return *this;
449 window_size_t Options::getSeqWindowSize()
451 return seq_window_size_;
454 Options& Options::setSeqWindowSize(window_size_t s)
456 seq_window_size_ = s;
457 return *this;
460 std::string Options::getCipher()
462 Lock lock(mutex);
463 return cipher_;
466 Options& Options::setCipher(std::string c)
468 Lock lock(mutex);
469 cipher_ = c;
470 return *this;
473 std::string Options::getKdPrf()
475 Lock lock(mutex);
476 return kd_prf_;
479 Options& Options::setKdPrf(std::string k)
481 Lock lock(mutex);
482 kd_prf_ = k;
483 return *this;
486 std::string Options::getAuthAlgo()
488 Lock lock(mutex);
489 return auth_algo_;
492 Options& Options::setAuthAlgo(std::string a)
494 Lock lock(mutex);
495 auth_algo_ = a;
496 return *this;
499 u_int16_t Options::getMux()
501 Lock lock(mutex);
502 return mux_;
505 Options& Options::setMux(u_int16_t m)
507 Lock lock(mutex);
508 mux_ = m;
509 return *this;
512 Buffer Options::getKey()
514 Lock lock(mutex);
515 return key_;
518 Options& Options::setKey(std::string k)
520 Lock lock(mutex);
521 key_ = k;
522 return *this;
525 Buffer Options::getSalt()
527 Lock lock(mutex);
528 return salt_;
531 Options& Options::setSalt(std::string s)
533 Lock lock(mutex);
534 salt_ = s;
535 return *this;