added licence header to wireshark lua script
[anytun.git] / src / options.cpp
blob91bab7a42caa9040cc1d3095417bf0f78778085f
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-2008 Othmar Gsenger, Erwin Nindl,
15 * Christian Pointner <satp@wirdorange.org>
17 * This file is part of Anytun.
19 * Anytun is free software: you can redistribute it and/or modify
20 * it under the terms of the GNU General Public License version 3 as
21 * published by the Free Software Foundation.
23 * Anytun is distributed in the hope that it will be useful,
24 * but WITHOUT ANY WARRANTY; without even the implied warranty of
25 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
26 * GNU General Public License for more details.
28 * You should have received a copy of the GNU General Public License
29 * along with anytun. If not, see <http://www.gnu.org/licenses/>.
32 #include <iostream>
33 #include <queue>
34 #include <string>
35 #include <sstream>
37 #include "datatypes.h"
38 #include "options.h"
40 Options* Options::inst = NULL;
41 Mutex Options::instMutex;
42 Options& gOpt = Options::instance();
44 Options& Options::instance()
46 Lock lock(instMutex);
47 static instanceCleaner c;
48 if(!inst)
49 inst = new Options();
51 return *inst;
54 Options::Options() : key_(u_int32_t(0)), salt_(u_int32_t(0))
56 progname_ = "anytun";
57 daemonize_ = true;
58 chroot_ = false;
59 username_ = "nobody";
60 chroot_dir_ = "/var/run/anytun";
61 pid_file_ = "";
62 sender_id_ = 0;
63 local_addr_ = "";
64 local_port_ = 4444;
65 local_sync_port_ = 0;
66 remote_sync_port_ = 0;
67 remote_sync_addr_ = "";
68 remote_addr_ = "";
69 remote_port_ = 4444;
70 dev_name_ = "";
71 dev_type_ = "";
72 ifconfig_param_local_ = "";
73 ifconfig_param_remote_netmask_ = "";
74 post_up_script_ = "";
75 seq_window_size_ = 100;
76 cipher_ = "aes-ctr";
77 kd_prf_ = "aes-ctr";
78 auth_algo_ = "sha1";
79 mux_ = 0;
82 Options::~Options()
86 #define PARSE_BOOL_PARAM(SHORT, LONG, VALUE) \
87 else if(str == SHORT || str == LONG) \
88 VALUE = true;
90 #define PARSE_INVERSE_BOOL_PARAM(SHORT, LONG, VALUE) \
91 else if(str == SHORT || str == LONG) \
92 VALUE = false;
94 #define PARSE_SCALAR_PARAM(SHORT, LONG, VALUE) \
95 else if(str == SHORT || str == LONG) \
96 { \
97 if(argc < 1 || argv[i+1][0] == '-') \
98 return false; \
99 std::stringstream tmp; \
100 tmp << argv[i+1]; \
101 tmp >> VALUE; \
102 argc--; \
103 i++; \
106 #define PARSE_SCALAR_PARAM2(SHORT, LONG, VALUE1, VALUE2) \
107 else if(str == SHORT || str == LONG) \
109 if(argc < 2 || \
110 argv[i+1][0] == '-' || argv[i+2][0] == '-') \
111 return false; \
112 std::stringstream tmp; \
113 tmp << argv[i+1] << " " << argv[i+2]; \
114 tmp >> VALUE1; \
115 tmp >> VALUE2; \
116 argc-=2; \
117 i+=2; \
120 #define PARSE_HEXSTRING_PARAM_SEC(SHORT, LONG, VALUE) \
121 else if(str == SHORT || str == LONG) \
123 if(argc < 1 || argv[i+1][0] == '-') \
124 return false; \
125 VALUE = Buffer(std::string(argv[i+1])); \
126 for(size_t j=0; j < strlen(argv[i+1]); ++j) \
127 argv[i+1][j] = '#'; \
128 argc--; \
129 i++; \
132 #define PARSE_CSLIST_PARAM(SHORT, LONG, LIST) \
133 else if(str == SHORT || str == LONG) \
135 if(argc < 1 || argv[i+1][0] == '-') \
136 return false; \
137 std::stringstream tmp(argv[i+1]); \
138 while (tmp.good()) \
140 std::string tmp_line; \
141 getline(tmp,tmp_line,','); \
142 LIST.push(tmp_line); \
144 argc--; \
145 i++; \
148 bool Options::parse(int argc, char* argv[])
150 Lock lock(mutex);
152 progname_ = argv[0];
153 argc--;
154 std::queue<std::string> host_port_queue;
155 for(int i=1; argc > 0; ++i)
157 std::string str(argv[i]);
158 argc--;
160 if(str == "-h" || str == "--help")
161 return false;
162 PARSE_INVERSE_BOOL_PARAM("-D","--nodaemonize", daemonize_)
163 PARSE_BOOL_PARAM("-C","--chroot", chroot_)
164 PARSE_SCALAR_PARAM("-u","--username", username_)
165 PARSE_SCALAR_PARAM("-H","--chroot-dir", chroot_dir_)
166 PARSE_SCALAR_PARAM("-P","--write-pid", pid_file_)
167 PARSE_SCALAR_PARAM("-s","--sender-id", sender_id_)
168 PARSE_SCALAR_PARAM("-i","--interface", local_addr_)
169 PARSE_SCALAR_PARAM("-p","--port", local_port_)
170 PARSE_SCALAR_PARAM("-S","--sync-port", local_sync_port_)
171 PARSE_SCALAR_PARAM("-I","--sync-interface", local_sync_addr_)
172 PARSE_SCALAR_PARAM("-R","--remote-sync-host", remote_sync_addr_)
173 PARSE_SCALAR_PARAM("-O","--remote-sync-port", remote_sync_port_)
174 PARSE_SCALAR_PARAM("-r","--remote-host", remote_addr_)
175 PARSE_SCALAR_PARAM("-o","--remote-port", remote_port_)
176 PARSE_SCALAR_PARAM("-d","--dev", dev_name_)
177 PARSE_SCALAR_PARAM("-t","--type", dev_type_)
178 PARSE_SCALAR_PARAM2("-n","--ifconfig", ifconfig_param_local_, ifconfig_param_remote_netmask_)
179 PARSE_SCALAR_PARAM("-x","--post-up-script", post_up_script_)
180 PARSE_SCALAR_PARAM("-w","--window-size", seq_window_size_)
181 PARSE_SCALAR_PARAM("-m","--mux", mux_)
182 PARSE_SCALAR_PARAM("-c","--cipher", cipher_)
183 PARSE_HEXSTRING_PARAM_SEC("-K","--key", key_)
184 PARSE_HEXSTRING_PARAM_SEC("-A","--salt", salt_)
185 PARSE_SCALAR_PARAM("-k","--kd-prf", kd_prf_)
186 PARSE_SCALAR_PARAM("-a","--auth-algo", auth_algo_)
187 PARSE_CSLIST_PARAM("-M","--sync-hosts", host_port_queue)
188 PARSE_CSLIST_PARAM("-X","--control-host", host_port_queue)
189 else
190 return false;
193 if(cipher_ == "null" && auth_algo_ == "null")
194 kd_prf_ = "null";
195 if((cipher_ != "null" || auth_algo_ != "null") && kd_prf_ == "null")
196 kd_prf_ = "aes-ctr";
198 if(dev_name_ == "" && dev_type_ == "")
199 dev_type_ = "tun";
201 while(!host_port_queue.empty())
203 std::stringstream tmp_stream(host_port_queue.front());
204 OptionConnectTo oct;
205 getline(tmp_stream,oct.host,':');
206 if(!tmp_stream.good())
207 return false;
208 tmp_stream >> oct.port;
209 host_port_queue.pop();
210 connect_to_.push_back(oct);
212 return true;
215 void Options::printUsage()
217 std::cout << "USAGE:" << std::endl;
218 std::cout << "anytun [-h|--help] prints this..." << std::endl;
219 // std::cout << " [-f|--config] <file> the config file" << std::endl;
220 std::cout << " [-D|--nodaemonize] don't run in background" << std::endl;
221 std::cout << " [-C|--chroot] chroot and drop privileges" << std::endl;
222 std::cout << " [-u|--username] <username> if chroot change to this user" << std::endl;
223 std::cout << " [-H|--chroot-dir] <path> chroot to this directory" << std::endl;
224 std::cout << " [-P|--write-pid] <path> write pid to this file" << std::endl;
225 std::cout << " [-s|--sender-id ] <sender id> the sender id to use" << std::endl;
226 std::cout << " [-i|--interface] <ip-address> local anycast ip address to bind to" << std::endl;
227 std::cout << " [-p|--port] <port> local anycast(data) port to bind to" << std::endl;
228 std::cout << " [-I|--sync-interface] <ip-address> local unicast(sync) ip address to bind to" << std::endl;
229 std::cout << " [-S|--sync-port] <port> local unicast(sync) port to bind to" << std::endl;
230 std::cout << " [-M|--sync-hosts] <hostname|ip>:<port>[,<hostname|ip>:<port>[...]]"<< std::endl;
231 std::cout << " remote hosts to sync with" << std::endl;
232 std::cout << " [-X|--control-host] <hostname|ip>:<port>"<< std::endl;
233 std::cout << " fetch the config from this host" << std::endl;
234 std::cout << " [-r|--remote-host] <hostname|ip> remote host" << std::endl;
235 std::cout << " [-o|--remote-port] <port> remote port" << std::endl;
236 std::cout << " [-d|--dev] <name> device name" << std::endl;
237 std::cout << " [-t|--type] <tun|tap> device type" << std::endl;
238 std::cout << " [-n|--ifconfig] <local> the local address for the tun/tap device" << std::endl
239 << " <remote|netmask> the remote address(tun) or netmask(tap)" << std::endl;
240 std::cout << " [-x|--post-up-script] <script> script gets called after interface is created" << std::endl;
241 std::cout << " [-w|--window-size] <window size> seqence number window size" << std::endl;
242 std::cout << " [-m|--mux] <mux-id> the multiplex id to use" << std::endl;
243 std::cout << " [-c|--cipher] <cipher type> payload encryption algorithm" << std::endl;
244 std::cout << " [-K|--key] <master key> master key to use for encryption" << std::endl;
245 std::cout << " [-A|--salt] <master salt> master salt to use for encryption" << std::endl;
246 // std::cout << " [-k|--kd-prf] <kd-prf type> key derivation pseudo random function" << std::endl;
247 std::cout << " [-a|--auth-algo] <algo type> message authentication algorithm" << std::endl;
250 void Options::printOptions()
252 Lock lock(mutex);
253 std::cout << "Options:" << std::endl;
254 std::cout << "daemonize=" << daemonize_ << std::endl;
255 std::cout << "chroot=" << chroot_ << std::endl;
256 std::cout << "username='" << username_ << "'" << std::endl;
257 std::cout << "chroot_dir='" << chroot_dir_ << "'" << std::endl;
258 std::cout << "pid_file='" << pid_file_ << "'" << std::endl;
259 std::cout << "sender_id='" << sender_id_ << "'" << std::endl;
260 std::cout << "local_addr='" << local_addr_ << "'" << std::endl;
261 std::cout << "local_port='" << local_port_ << "'" << std::endl;
262 std::cout << "local_sync_addr='" << local_sync_addr_ << "'" << std::endl;
263 std::cout << "local_sync_port='" << local_sync_port_ << "'" << std::endl;
264 std::cout << "remote_addr='" << remote_addr_ << "'" << std::endl;
265 std::cout << "remote_port='" << remote_port_ << "'" << std::endl;
266 std::cout << "dev_name='" << dev_name_ << "'" << std::endl;
267 std::cout << "dev_type='" << dev_type_ << "'" << std::endl;
268 std::cout << "ifconfig_param_local='" << ifconfig_param_local_ << "'" << std::endl;
269 std::cout << "ifconfig_param_remote_netmask='" << ifconfig_param_remote_netmask_ << "'" << std::endl;
270 std::cout << "post_up_script='" << post_up_script_ << "'" << std::endl;
271 std::cout << "seq_window_size='" << seq_window_size_ << "'" << std::endl;
272 std::cout << "mux_id='" << mux_ << "'" << std::endl;
273 std::cout << "cipher='" << cipher_ << "'" << std::endl;
274 std::cout << "key=" << key_.getHexDumpOneLine() << std::endl;
275 std::cout << "salt=" << salt_.getHexDumpOneLine() << std::endl;
276 std::cout << "kd_prf='" << kd_prf_ << "'" << std::endl;
277 std::cout << "auth_algo='" << auth_algo_ << "'" << std::endl;
280 std::string Options::getProgname()
282 Lock lock(mutex);
283 return progname_;
287 Options& Options::setProgname(std::string p)
289 Lock lock(mutex);
290 progname_ = p;
291 return *this;
294 bool Options::getDaemonize()
296 return daemonize_;
299 Options& Options::setDaemonize(bool d)
301 daemonize_ = d;
302 return *this;
305 bool Options::getChroot()
307 return chroot_;
310 Options& Options::setChroot(bool c)
312 chroot_ = c;
313 return *this;
316 std::string Options::getUsername()
318 Lock lock(mutex);
319 return username_;
322 Options& Options::setUsername(std::string u)
324 Lock lock(mutex);
325 username_ = u;
326 return *this;
329 std::string Options::getChrootDir()
331 Lock lock(mutex);
332 return chroot_dir_;
335 Options& Options::setChrootDir(std::string c)
337 Lock lock(mutex);
338 chroot_dir_ = c;
339 return *this;
342 std::string Options::getPidFile()
344 Lock lock(mutex);
345 return pid_file_;
348 Options& Options::setPidFile(std::string p)
350 Lock lock(mutex);
351 pid_file_ = p;
352 return *this;
355 ConnectToList Options::getConnectTo()
357 Lock lock(mutex);
358 return connect_to_;
361 sender_id_t Options::getSenderId()
363 return sender_id_;
366 Options& Options::setSenderId(sender_id_t s)
368 sender_id_ = s;
369 return *this;
372 std::string Options::getLocalAddr()
374 Lock lock(mutex);
375 return local_addr_;
378 Options& Options::setLocalAddr(std::string l)
380 Lock lock(mutex);
381 local_addr_ = l;
382 return *this;
385 std::string Options::getLocalSyncAddr()
387 Lock lock(mutex);
388 return local_sync_addr_;
391 Options& Options::setLocalSyncAddr(std::string l)
393 Lock lock(mutex);
394 local_sync_addr_ = l;
395 return *this;
398 u_int16_t Options::getLocalPort()
400 return local_port_;
403 Options& Options::setLocalPort(u_int16_t l)
405 local_port_ = l;
406 return *this;
409 u_int16_t Options::getLocalSyncPort()
411 return local_sync_port_;
414 Options& Options::setLocalSyncPort(u_int16_t l)
416 local_sync_port_ = l;
417 return *this;
420 u_int16_t Options::getRemoteSyncPort()
422 return remote_sync_port_;
425 Options& Options::setRemoteSyncPort(u_int16_t l)
427 remote_sync_port_ = l;
428 return *this;
431 std::string Options::getRemoteSyncAddr()
433 Lock lock(mutex);
434 return remote_sync_addr_;
437 Options& Options::setRemoteSyncAddr(std::string r)
439 Lock lock(mutex);
440 remote_sync_addr_ = r;
441 return *this;
444 std::string Options::getRemoteAddr()
446 Lock lock(mutex);
447 return remote_addr_;
450 Options& Options::setRemoteAddr(std::string r)
452 Lock lock(mutex);
453 remote_addr_ = r;
454 return *this;
457 u_int16_t Options::getRemotePort()
459 return remote_port_;
462 Options& Options::setRemotePort(u_int16_t r)
464 remote_port_ = r;
465 return *this;
468 Options& Options::setRemoteAddrPort(std::string addr, u_int16_t port)
470 Lock lock(mutex);
471 remote_addr_ = addr;
472 remote_port_ = port;
473 return *this;
476 std::string Options::getDevName()
478 Lock lock(mutex);
479 return dev_name_;
482 std::string Options::getDevType()
484 Lock lock(mutex);
485 return dev_type_;
488 Options& Options::setDevName(std::string d)
490 Lock lock(mutex);
491 dev_name_ = d;
492 return *this;
495 Options& Options::setDevType(std::string d)
497 Lock lock(mutex);
498 dev_type_ = d;
499 return *this;
502 std::string Options::getIfconfigParamLocal()
504 Lock lock(mutex);
505 return ifconfig_param_local_;
508 Options& Options::setIfconfigParamLocal(std::string i)
510 Lock lock(mutex);
511 ifconfig_param_local_ = i;
512 return *this;
515 std::string Options::getIfconfigParamRemoteNetmask()
517 Lock lock(mutex);
518 return ifconfig_param_remote_netmask_;
521 Options& Options::setIfconfigParamRemoteNetmask(std::string i)
523 Lock lock(mutex);
524 ifconfig_param_remote_netmask_ = i;
525 return *this;
528 std::string Options::getPostUpScript()
530 Lock lock(mutex);
531 return post_up_script_;
534 Options& Options::setPostUpScript(std::string p)
536 Lock lock(mutex);
537 post_up_script_ = p;
538 return *this;
541 window_size_t Options::getSeqWindowSize()
543 return seq_window_size_;
546 Options& Options::setSeqWindowSize(window_size_t s)
548 seq_window_size_ = s;
549 return *this;
552 std::string Options::getCipher()
554 Lock lock(mutex);
555 return cipher_;
558 Options& Options::setCipher(std::string c)
560 Lock lock(mutex);
561 cipher_ = c;
562 return *this;
565 std::string Options::getKdPrf()
567 Lock lock(mutex);
568 return kd_prf_;
571 Options& Options::setKdPrf(std::string k)
573 Lock lock(mutex);
574 kd_prf_ = k;
575 return *this;
578 std::string Options::getAuthAlgo()
580 Lock lock(mutex);
581 return auth_algo_;
584 Options& Options::setAuthAlgo(std::string a)
586 Lock lock(mutex);
587 auth_algo_ = a;
588 return *this;
591 u_int16_t Options::getMux()
593 Lock lock(mutex);
594 return mux_;
597 Options& Options::setMux(u_int16_t m)
599 Lock lock(mutex);
600 mux_ = m;
601 return *this;
604 Buffer Options::getKey()
606 Lock lock(mutex);
607 return key_;
610 Options& Options::setKey(std::string k)
612 Lock lock(mutex);
613 key_ = k;
614 return *this;
617 Buffer Options::getSalt()
619 Lock lock(mutex);
620 return salt_;
623 Options& Options::setSalt(std::string s)
625 Lock lock(mutex);
626 salt_ = s;
627 return *this;