svn cleanup
[anytun.git] / options.cpp
blob174844fe585f98a76435c5c5f7dbb091df4f0ca8
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 chroot_ = false;
58 username_ = "nobody";
59 chroot_dir_ = "/var/run/anytun";
60 pid_file_ = "";
61 sender_id_ = 0;
62 local_addr_ = "";
63 local_port_ = 4444;
64 local_sync_port_ = 0;
65 remote_sync_port_ = 0;
66 remote_sync_addr_ = "";
67 remote_addr_ = "";
68 remote_port_ = 4444;
69 dev_name_ = "tun";
70 dev_type_ = "";
71 ifconfig_param_local_ = "";
72 ifconfig_param_remote_netmask_ = "";
73 post_up_script_ = "";
74 seq_window_size_ = 100;
75 cipher_ = "aes-ctr";
76 kd_prf_ = "aes-ctr";
77 auth_algo_ = "sha1";
78 mux_ = 0;
81 Options::~Options()
85 #define PARSE_BOOL_PARAM(SHORT, LONG, VALUE) \
86 else if(str == SHORT || str == LONG) \
87 VALUE = true;
89 #define PARSE_INVERSE_BOOL_PARAM(SHORT, LONG, VALUE) \
90 else if(str == SHORT || str == LONG) \
91 VALUE = false;
93 #define PARSE_SCALAR_PARAM(SHORT, LONG, VALUE) \
94 else if(str == SHORT || str == LONG) \
95 { \
96 if(argc < 1 || argv[i+1][0] == '-') \
97 return false; \
98 std::stringstream tmp; \
99 tmp << argv[i+1]; \
100 tmp >> VALUE; \
101 argc--; \
102 i++; \
105 #define PARSE_SCALAR_PARAM2(SHORT, LONG, VALUE1, VALUE2) \
106 else if(str == SHORT || str == LONG) \
108 if(argc < 2 || \
109 argv[i+1][0] == '-' || argv[i+2][0] == '-') \
110 return false; \
111 std::stringstream tmp; \
112 tmp << argv[i+1] << " " << argv[i+2]; \
113 tmp >> VALUE1; \
114 tmp >> VALUE2; \
115 argc-=2; \
116 i+=2; \
119 #define PARSE_HEXSTRING_PARAM_SEC(SHORT, LONG, VALUE) \
120 else if(str == SHORT || str == LONG) \
122 if(argc < 1 || argv[i+1][0] == '-') \
123 return false; \
124 VALUE = Buffer(std::string(argv[i+1])); \
125 for(size_t j=0; j < strlen(argv[i+1]); ++j) \
126 argv[i+1][j] = '#'; \
127 argc--; \
128 i++; \
131 #define PARSE_CSLIST_PARAM(SHORT, LONG, LIST) \
132 else if(str == SHORT || str == LONG) \
134 if(argc < 1 || argv[i+1][0] == '-') \
135 return false; \
136 std::stringstream tmp(argv[i+1]); \
137 while (tmp.good()) \
139 std::string tmp_line; \
140 getline(tmp,tmp_line,','); \
141 LIST.push(tmp_line); \
143 argc--; \
144 i++; \
147 bool Options::parse(int argc, char* argv[])
149 Lock lock(mutex);
151 progname_ = argv[0];
152 argc--;
153 std::queue<std::string> host_port_queue;
154 for(int i=1; argc > 0; ++i)
156 std::string str(argv[i]);
157 argc--;
159 if(str == "-h" || str == "--help")
160 return false;
161 PARSE_INVERSE_BOOL_PARAM("-D","--nodaemonize", daemonize_)
162 PARSE_BOOL_PARAM("-C","--chroot", chroot_)
163 PARSE_SCALAR_PARAM("-u","--username", username_)
164 PARSE_SCALAR_PARAM("-H","--chroot-dir", chroot_dir_)
165 PARSE_SCALAR_PARAM("-P","--write-pid", pid_file_)
166 PARSE_SCALAR_PARAM("-s","--sender-id", sender_id_)
167 PARSE_SCALAR_PARAM("-i","--interface", local_addr_)
168 PARSE_SCALAR_PARAM("-p","--port", local_port_)
169 PARSE_SCALAR_PARAM("-S","--sync-port", local_sync_port_)
170 PARSE_SCALAR_PARAM("-I","--sync-interface", local_sync_addr_)
171 PARSE_SCALAR_PARAM("-R","--remote-sync-host", remote_sync_addr_)
172 PARSE_SCALAR_PARAM("-O","--remote-sync-port", remote_sync_port_)
173 PARSE_SCALAR_PARAM("-r","--remote-host", remote_addr_)
174 PARSE_SCALAR_PARAM("-o","--remote-port", remote_port_)
175 PARSE_SCALAR_PARAM("-d","--dev", dev_name_)
176 PARSE_SCALAR_PARAM("-t","--type", dev_type_)
177 PARSE_SCALAR_PARAM2("-n","--ifconfig", ifconfig_param_local_, ifconfig_param_remote_netmask_)
178 PARSE_SCALAR_PARAM("-x","--post-up-script", post_up_script_)
179 PARSE_SCALAR_PARAM("-w","--window-size", seq_window_size_)
180 PARSE_SCALAR_PARAM("-m","--mux", mux_)
181 PARSE_SCALAR_PARAM("-c","--cipher", cipher_)
182 PARSE_HEXSTRING_PARAM_SEC("-K","--key", key_)
183 PARSE_HEXSTRING_PARAM_SEC("-A","--salt", salt_)
184 PARSE_SCALAR_PARAM("-k","--kd-prf", kd_prf_)
185 PARSE_SCALAR_PARAM("-a","--auth-algo", auth_algo_)
186 PARSE_CSLIST_PARAM("-M","--sync-hosts", host_port_queue)
187 PARSE_CSLIST_PARAM("-X","--control-host", host_port_queue)
188 else
189 return false;
192 if(cipher_ == "null" && auth_algo_ == "null")
193 kd_prf_ = "null";
194 if((cipher_ != "null" || auth_algo_ != "null") && kd_prf_ == "null")
195 kd_prf_ = "aes-ctr";
197 while(!host_port_queue.empty())
199 std::stringstream tmp_stream(host_port_queue.front());
200 OptionConnectTo oct;
201 getline(tmp_stream,oct.host,':');
202 if(!tmp_stream.good())
203 return false;
204 tmp_stream >> oct.port;
205 host_port_queue.pop();
206 connect_to_.push_back(oct);
208 return true;
211 void Options::printUsage()
213 std::cout << "USAGE:" << std::endl;
214 std::cout << "anytun [-h|--help] prints this..." << std::endl;
215 // std::cout << " [-f|--config] <file> the config file" << std::endl;
216 std::cout << " [-D|--nodaemonize] don't run in background" << std::endl;
217 std::cout << " [-C|--chroot] chroot and drop privileges" << std::endl;
218 std::cout << " [-u|--username] if chroot change to this user" << std::endl;
219 std::cout << " [-H|--chroot-dir] chroot to this directory" << std::endl;
220 std::cout << " [-P|--write-pid] write pid to this file" << std::endl;
221 std::cout << " [-s|--sender-id ] <sender id> the sender id to use" << std::endl;
222 std::cout << " [-i|--interface] <ip-address> local anycast ip address to bind to" << std::endl;
223 std::cout << " [-p|--port] <port> local anycast(data) port to bind to" << std::endl;
224 std::cout << " [-I|--sync-interface] <ip-address> local unicast(sync) ip address to bind to" << std::endl;
225 std::cout << " [-S|--sync-port] <port> local unicast(sync) port to bind to" << std::endl;
226 std::cout << " [-M|--sync-hosts] <hostname|ip>:<port>[,<hostname|ip>:<port>[...]]"<< std::endl;
227 std::cout << " remote hosts to sync with" << std::endl;
228 std::cout << " [-X|--control-host] <hostname|ip>:<port>"<< std::endl;
229 std::cout << " fetch the config from this host" << std::endl;
230 std::cout << " [-r|--remote-host] <hostname|ip> remote host" << std::endl;
231 std::cout << " [-o|--remote-port] <port> remote port" << std::endl;
232 std::cout << " [-d|--dev] <name> device name" << std::endl;
233 std::cout << " [-t|--type] <tun|tap> device type" << std::endl;
234 std::cout << " [-n|--ifconfig] <local> the local address for the tun/tap device" << std::endl
235 << " <remote|netmask> the remote address(tun) or netmask(tap)" << std::endl;
236 std::cout << " [-x|--post-up-script] <script> script gets called after interface is created" << std::endl;
237 std::cout << " [-w|--window-size] <window size> seqence number window size" << std::endl;
238 std::cout << " [-m|--mux] <mux-id> the multiplex id to use" << std::endl;
239 std::cout << " [-c|--cipher] <cipher type> payload encryption algorithm" << std::endl;
240 std::cout << " [-K|--key] <master key> master key to use for encryption" << std::endl;
241 std::cout << " [-A|--salt] <master salt> master salt to use for encryption" << std::endl;
242 // std::cout << " [-k|--kd-prf] <kd-prf type> key derivation pseudo random function" << std::endl;
243 std::cout << " [-a|--auth-algo] <algo type> message authentication algorithm" << std::endl;
246 void Options::printOptions()
248 Lock lock(mutex);
249 std::cout << "Options:" << std::endl;
250 std::cout << "daemonize=" << daemonize_ << std::endl;
251 std::cout << "chroot=" << chroot_ << std::endl;
252 std::cout << "username='" << username_ << "'" << std::endl;
253 std::cout << "chroot_dir='" << chroot_dir_ << "'" << std::endl;
254 std::cout << "pid_file='" << pid_file_ << "'" << std::endl;
255 std::cout << "sender_id='" << sender_id_ << "'" << std::endl;
256 std::cout << "local_addr='" << local_addr_ << "'" << std::endl;
257 std::cout << "local_port='" << local_port_ << "'" << std::endl;
258 std::cout << "local_sync_addr='" << local_sync_addr_ << "'" << std::endl;
259 std::cout << "local_sync_port='" << local_sync_port_ << "'" << std::endl;
260 std::cout << "remote_addr='" << remote_addr_ << "'" << std::endl;
261 std::cout << "remote_port='" << remote_port_ << "'" << std::endl;
262 std::cout << "dev_name='" << dev_name_ << "'" << std::endl;
263 std::cout << "dev_type='" << dev_type_ << "'" << std::endl;
264 std::cout << "ifconfig_param_local='" << ifconfig_param_local_ << "'" << std::endl;
265 std::cout << "ifconfig_param_remote_netmask='" << ifconfig_param_remote_netmask_ << "'" << std::endl;
266 std::cout << "post_up_script='" << post_up_script_ << "'" << std::endl;
267 std::cout << "seq_window_size='" << seq_window_size_ << "'" << std::endl;
268 std::cout << "mux_id='" << mux_ << "'" << std::endl;
269 std::cout << "cipher='" << cipher_ << "'" << std::endl;
270 std::cout << "key=" << key_.getHexDumpOneLine() << std::endl;
271 std::cout << "salt=" << salt_.getHexDumpOneLine() << std::endl;
272 std::cout << "kd_prf='" << kd_prf_ << "'" << std::endl;
273 std::cout << "auth_algo='" << auth_algo_ << "'" << std::endl;
276 std::string Options::getProgname()
278 Lock lock(mutex);
279 return progname_;
283 Options& Options::setProgname(std::string p)
285 Lock lock(mutex);
286 progname_ = p;
287 return *this;
290 bool Options::getDaemonize()
292 return daemonize_;
295 Options& Options::setDaemonize(bool d)
297 daemonize_ = d;
298 return *this;
301 bool Options::getChroot()
303 return chroot_;
306 Options& Options::setChroot(bool c)
308 chroot_ = c;
309 return *this;
312 std::string Options::getUsername()
314 Lock lock(mutex);
315 return username_;
318 Options& Options::setUsername(std::string u)
320 Lock lock(mutex);
321 username_ = u;
322 return *this;
325 std::string Options::getChrootDir()
327 Lock lock(mutex);
328 return chroot_dir_;
331 Options& Options::setChrootDir(std::string c)
333 Lock lock(mutex);
334 chroot_dir_ = c;
335 return *this;
338 std::string Options::getPidFile()
340 Lock lock(mutex);
341 return pid_file_;
344 Options& Options::setPidFile(std::string p)
346 Lock lock(mutex);
347 pid_file_ = p;
348 return *this;
351 ConnectToList Options::getConnectTo()
353 Lock lock(mutex);
354 return connect_to_;
357 sender_id_t Options::getSenderId()
359 return sender_id_;
362 Options& Options::setSenderId(sender_id_t s)
364 sender_id_ = s;
365 return *this;
368 std::string Options::getLocalAddr()
370 Lock lock(mutex);
371 return local_addr_;
374 Options& Options::setLocalAddr(std::string l)
376 Lock lock(mutex);
377 local_addr_ = l;
378 return *this;
381 std::string Options::getLocalSyncAddr()
383 Lock lock(mutex);
384 return local_sync_addr_;
387 Options& Options::setLocalSyncAddr(std::string l)
389 Lock lock(mutex);
390 local_sync_addr_ = l;
391 return *this;
394 u_int16_t Options::getLocalPort()
396 return local_port_;
399 Options& Options::setLocalPort(u_int16_t l)
401 local_port_ = l;
402 return *this;
405 u_int16_t Options::getLocalSyncPort()
407 return local_sync_port_;
410 Options& Options::setLocalSyncPort(u_int16_t l)
412 local_sync_port_ = l;
413 return *this;
416 u_int16_t Options::getRemoteSyncPort()
418 return remote_sync_port_;
421 Options& Options::setRemoteSyncPort(u_int16_t l)
423 remote_sync_port_ = l;
424 return *this;
427 std::string Options::getRemoteSyncAddr()
429 Lock lock(mutex);
430 return remote_sync_addr_;
433 Options& Options::setRemoteSyncAddr(std::string r)
435 Lock lock(mutex);
436 remote_sync_addr_ = r;
437 return *this;
440 std::string Options::getRemoteAddr()
442 Lock lock(mutex);
443 return remote_addr_;
446 Options& Options::setRemoteAddr(std::string r)
448 Lock lock(mutex);
449 remote_addr_ = r;
450 return *this;
453 u_int16_t Options::getRemotePort()
455 return remote_port_;
458 Options& Options::setRemotePort(u_int16_t r)
460 remote_port_ = r;
461 return *this;
464 Options& Options::setRemoteAddrPort(std::string addr, u_int16_t port)
466 Lock lock(mutex);
467 remote_addr_ = addr;
468 remote_port_ = port;
469 return *this;
472 std::string Options::getDevName()
474 Lock lock(mutex);
475 return dev_name_;
478 std::string Options::getDevType()
480 Lock lock(mutex);
481 return dev_type_;
484 Options& Options::setDevName(std::string d)
486 Lock lock(mutex);
487 dev_name_ = d;
488 return *this;
491 Options& Options::setDevType(std::string d)
493 Lock lock(mutex);
494 dev_type_ = d;
495 return *this;
498 std::string Options::getIfconfigParamLocal()
500 Lock lock(mutex);
501 return ifconfig_param_local_;
504 Options& Options::setIfconfigParamLocal(std::string i)
506 Lock lock(mutex);
507 ifconfig_param_local_ = i;
508 return *this;
511 std::string Options::getIfconfigParamRemoteNetmask()
513 Lock lock(mutex);
514 return ifconfig_param_remote_netmask_;
517 Options& Options::setIfconfigParamRemoteNetmask(std::string i)
519 Lock lock(mutex);
520 ifconfig_param_remote_netmask_ = i;
521 return *this;
524 std::string Options::getPostUpScript()
526 Lock lock(mutex);
527 return post_up_script_;
530 Options& Options::setPostUpScript(std::string p)
532 Lock lock(mutex);
533 post_up_script_ = p;
534 return *this;
537 window_size_t Options::getSeqWindowSize()
539 return seq_window_size_;
542 Options& Options::setSeqWindowSize(window_size_t s)
544 seq_window_size_ = s;
545 return *this;
548 std::string Options::getCipher()
550 Lock lock(mutex);
551 return cipher_;
554 Options& Options::setCipher(std::string c)
556 Lock lock(mutex);
557 cipher_ = c;
558 return *this;
561 std::string Options::getKdPrf()
563 Lock lock(mutex);
564 return kd_prf_;
567 Options& Options::setKdPrf(std::string k)
569 Lock lock(mutex);
570 kd_prf_ = k;
571 return *this;
574 std::string Options::getAuthAlgo()
576 Lock lock(mutex);
577 return auth_algo_;
580 Options& Options::setAuthAlgo(std::string a)
582 Lock lock(mutex);
583 auth_algo_ = a;
584 return *this;
587 u_int16_t Options::getMux()
589 Lock lock(mutex);
590 return mux_;
593 Options& Options::setMux(u_int16_t m)
595 Lock lock(mutex);
596 mux_ = m;
597 return *this;
600 Buffer Options::getKey()
602 Lock lock(mutex);
603 return key_;
606 Options& Options::setKey(std::string k)
608 Lock lock(mutex);
609 key_ = k;
610 return *this;
613 Buffer Options::getSalt()
615 Lock lock(mutex);
616 return salt_;
619 Options& Options::setSalt(std::string s)
621 Lock lock(mutex);
622 salt_ = s;
623 return *this;