Imported Upstream version 0.3.5
[anytun.git] / src / 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 <cstring>
47 #include <iostream>
48 #include <queue>
49 #include <string>
50 #include <sstream>
51
52 #include "datatypes.h"
53 #include "version.h"
54
55 #include "options.h"
56 #include "log.h"
57 #include "authAlgoFactory.h"
58
59 std::ostream& operator<<(std::ostream& stream, syntax_error const& error)
60 {
61   stream << "syntax error: " << error.what() << std::endl;
62   if(error.pos >= 0) {
63     stream << "              ";
64     for(int32_t i = 0; i < error.pos; ++i) { stream << " "; }
65     return stream << "^";
66   }
67   return stream;
68 }
69
70 std::ostream& operator<<(std::ostream& stream, role_t const& role)
71 {
72   switch(role) {
73   case ROLE_LEFT:
74     stream << "left";
75     break;
76   case ROLE_RIGHT:
77     stream << "right";
78     break;
79   default:
80     stream << "unknown";
81     break;
82   }
83   return stream;
84 }
85
86 void OptionHost::init(std::string addrPort)
87 {
88   std::string origAddrPort(addrPort);
89   size_t pos = addrPort.find_first_of("[");
90
91   if(pos != std::string::npos && pos != 0) {
92     throw syntax_error(origAddrPort, pos);  // an [ was found but not at the beginning;
93   }
94
95   bool hasPort = false;
96   if(pos != std::string::npos) {
97     addrPort.erase(pos, 1);
98     pos = addrPort.find_first_of("]");
99
100     if(pos == std::string::npos) {
101       throw syntax_error(origAddrPort, origAddrPort.length());  //no trailing ] although an leading [ was found
102     }
103
104     if(pos < addrPort.length()-2) {
105       if(addrPort[pos+1] != ':') {
106         throw syntax_error(origAddrPort, pos+2);  // wrong port delimieter
107       }
108
109       addrPort[pos+1] = '/';
110       hasPort = true;
111     } else if(pos != addrPort.length()-1) {
112       throw syntax_error(origAddrPort, pos+2);  // too few characters left
113     }
114
115     addrPort.erase(pos, 1);
116   } else {
117     pos = addrPort.find_first_of(":");
118     if(pos != std::string::npos && pos == addrPort.find_last_of(":")) {
119       // an ':' has been found and it is the only one -> assuming port present
120       hasPort = true;
121       addrPort[pos] = '/';
122     }
123   }
124
125   if(hasPort) {
126     std::stringstream tmp_stream(addrPort);
127
128     getline(tmp_stream, addr, '/');
129     if(!tmp_stream.good()) {
130       throw syntax_error(origAddrPort, addr.length());
131     }
132
133     tmp_stream >> port;
134   } else {
135     addr = addrPort;
136     port = "2323"; // default sync port
137   }
138 }
139
140 std::istream& operator>>(std::istream& stream, OptionHost& host)
141 {
142   std::string tmp;
143   stream >> tmp;
144   host.init(tmp);
145   return stream;
146 }
147
148 void OptionNetwork::init(std::string network)
149 {
150   std::stringstream tmp_stream(network);
151   getline(tmp_stream, net_addr, '/');
152   if(!tmp_stream.good()) {
153     throw syntax_error(network, net_addr.length());
154   }
155   tmp_stream >> prefix_length;
156 }
157
158 std::istream& operator>>(std::istream& stream, OptionNetwork& network)
159 {
160   std::string tmp;
161   stream >> tmp;
162   network.init(tmp);
163   return stream;
164 }
165
166 Options& gOpt = Options::instance();
167
168 Options& Options::instance()
169 {
170   static Options instance;
171   return instance;
172 }
173
174 Options::Options() : key_(uint32_t(0)), salt_(uint32_t(0))
175 {
176 #if defined(ANYCTR_OPTIONS)
177   progname_ = "anytun-controld";
178 #elif defined(ANYCONF_OPTIONS)
179   progname_ = "anytun-config";
180 #else
181   progname_ = "anytun";
182 #endif
183
184   cluster_opts = false;
185   connection_opts = false;
186
187   daemonize_ = true;
188   username_ = "";
189   groupname_ = "";
190   chroot_dir_ = "";
191   pid_file_ = "";
192
193   debug_ = false;
194
195   file_name_ = "";
196   bind_to_.addr = "127.0.0.1";
197   bind_to_.port = "2323";
198
199   resolv_addr_type_ = ANY;
200
201   local_.addr = "";
202   local_.port = "4444";
203   remote_.addr = "";
204   remote_.port = "4444";
205   local_sync_.addr = "";
206   local_sync_.port = "";
207
208   dev_name_ = "";
209   dev_type_ = "";
210   post_up_script_ = "";
211
212   sender_id_ = 0;
213   mux_ = 0;
214   seq_window_size_ = 0;
215
216 #if !defined(ANYCONF_OPTIONS)
217 #ifndef NO_CRYPT
218   cipher_ = "aes-ctr";
219   auth_algo_ = "sha1";
220   auth_tag_length_ = 10;
221   kd_prf_ = "aes-ctr";
222 #else
223   cipher_ = "null";
224   auth_algo_ = "null";
225   auth_tag_length_ = 0;
226   kd_prf_ = "null";
227 #endif
228 #else
229   cipher_ = "null";
230   auth_algo_ = "null";
231   auth_tag_length_ = 0;
232   kd_prf_ = "aes-ctr";
233 #endif
234   role_ = ROLE_LEFT;
235 }
236
237 Options::~Options()
238 {
239 }
240
241 #define NOTHING
242
243 #define PARSE_BOOL_PARAM(SHORT, LONG, VALUE, DO_POST)                           \
244     else if(str == SHORT || str == LONG) {                                      \
245       VALUE = true;                                                             \
246       DO_POST;                                                                  \
247     }
248
249 #define PARSE_INVERSE_BOOL_PARAM(SHORT, LONG, VALUE, DO_POST)                   \
250     else if(str == SHORT || str == LONG) {                                      \
251       VALUE = false;                                                            \
252       DO_POST;                                                                  \
253     }
254
255 #define PARSE_SIGNED_INT_PARAM(SHORT, LONG, VALUE, DO_POST)                     \
256     else if(str == SHORT || str == LONG)                                        \
257     {                                                                           \
258       if(argc < 1)                                                              \
259         throw syntax_error(str, str.length());                                  \
260       std::stringstream tmp;                                                    \
261       tmp << argv[i+1];                                                         \
262       tmp >> VALUE;                                                             \
263       argc--;                                                                   \
264       i++;                                                                      \
265       DO_POST;                                                                  \
266     }
267
268 #define PARSE_SCALAR_PARAM(SHORT, LONG, VALUE, DO_POST)                         \
269     else if(str == SHORT || str == LONG)                                        \
270     {                                                                           \
271       if(argc < 1)                                                              \
272         throw syntax_error(str, str.length());                                  \
273       if(argv[i+1][0] == '-') {                                                 \
274         uint32_t pos = str.length() + 1;                                       \
275         throw syntax_error(str.append(" ").append(argv[i+1]), pos);             \
276       }                                                                         \
277       std::stringstream tmp;                                                    \
278       tmp << argv[i+1];                                                         \
279       tmp >> VALUE;                                                             \
280       argc--;                                                                   \
281       i++;                                                                      \
282       DO_POST;                                                                  \
283     }
284
285 #define PARSE_SCALAR_PARAM2(SHORT, LONG, VALUE1, VALUE2, DO_POST)               \
286     else if(str == SHORT || str == LONG)                                        \
287     {                                                                           \
288       if(argc < 1)                                                              \
289         throw syntax_error(str, str.length());                                  \
290       if(argc < 2)                                                              \
291         throw syntax_error(str.append(" ").append(argv[i+1]), str.length());    \
292       if(argv[i+1][0] == '-') {                                                 \
293         uint32_t pos = str.length() + 1;                                       \
294         throw syntax_error(str.append(" ").append(argv[i+1]), pos);             \
295       }                                                                         \
296       if(argv[i+2][0] == '-') {                                                 \
297         uint32_t pos = str.length() + 1 + strlen(argv[i+1]) + 1;               \
298         throw syntax_error(str.append(" ").append(argv[i+1]).append(" ").append(argv[i+2]), pos); \
299       }                                                                         \
300       std::stringstream tmp;                                                    \
301       tmp << argv[i+1] << " " << argv[i+2];                                     \
302       tmp >> VALUE1;                                                            \
303       tmp >> VALUE2;                                                            \
304       argc-=2;                                                                  \
305       i+=2;                                                                     \
306       DO_POST;                                                                  \
307     }
308
309 #define PARSE_CSLIST_PARAM(SHORT, LONG, LIST, TYPE, DO_POST)                    \
310     else if(str == SHORT || str == LONG)                                        \
311     {                                                                           \
312       if(argc < 1)                                                              \
313         throw syntax_error(str, str.length());                                  \
314       if(argv[i+1][0] == '-') {                                                 \
315         uint32_t pos = str.length() + 1;                                       \
316         throw syntax_error(str.append(" ").append(argv[i+1]), pos);             \
317       }                                                                         \
318       std::stringstream tmp(argv[i+1]);                                         \
319       while (tmp.good())                                                        \
320       {                                                                         \
321         std::string tmp_line;                                                   \
322         getline(tmp,tmp_line,',');                                              \
323         LIST.push_back(TYPE(tmp_line));                                         \
324       }                                                                         \
325       argc--;                                                                   \
326       i++;                                                                      \
327       DO_POST;                                                                  \
328     }
329
330 #define PARSE_HEXSTRING_PARAM_SEC(SHORT, LONG, VALUE, DO_POST)                  \
331     else if(str == SHORT || str == LONG)                                        \
332     {                                                                           \
333       if(argc < 1)                                                              \
334         throw syntax_error(str, str.length());                                  \
335       if(argv[i+1][0] == '-') {                                                 \
336         uint32_t pos = str.length() + 1;                                       \
337         throw syntax_error(str.append(" ").append(argv[i+1]), pos);             \
338       }                                                                         \
339       VALUE = Buffer(std::string(argv[i+1]));                                   \
340       for(size_t j=0; j < strlen(argv[i+1]); ++j)                               \
341         argv[i+1][j] = '#';                                                     \
342       argc--;                                                                   \
343       i++;                                                                      \
344       DO_POST;                                                                  \
345     }
346
347 #define PARSE_PHRASE_PARAM_SEC(SHORT, LONG, VALUE, DO_POST)                     \
348     else if(str == SHORT || str == LONG)                                        \
349     {                                                                           \
350       if(argc < 1)                                                              \
351         throw syntax_error(str, str.length());                                  \
352       if(argv[i+1][0] == '-') {                                                 \
353         uint32_t pos = str.length() + 1;                                       \
354         throw syntax_error(str.append(" ").append(argv[i+1]), pos);             \
355       }                                                                         \
356       VALUE = argv[i+1];                                                        \
357       for(size_t j=0; j < strlen(argv[i+1]); ++j)                               \
358         argv[i+1][j] = '#';                                                     \
359       argc--;                                                                   \
360       i++;                                                                      \
361       DO_POST;                                                                  \
362     }
363
364 #define PARSE_STRING_LIST(SHORT, LONG, LIST, DO_POST)                           \
365     else if(str == SHORT || str == LONG)                                        \
366     {                                                                           \
367       if(argc < 1)                                                              \
368         throw syntax_error(str, str.length());                                  \
369       if(argv[i+1][0] == '-') {                                                 \
370         uint32_t pos = str.length() + 1;                                       \
371         throw syntax_error(str.append(" ").append(argv[i+1]), pos);             \
372       }                                                                         \
373       LIST.push_back(argv[i+1]);                                                \
374       argc--;                                                                   \
375       i++;                                                                      \
376       DO_POST;                                                                  \
377     }
378
379 bool Options::parse(int argc, char* argv[])
380 {
381   WritersLock lock(mutex);
382
383   progname_ = argv[0];
384   argc--;
385   bool ipv4_only = false, ipv6_only = false;
386   std::string role = "";
387   for(int i=1; argc > 0; ++i) {
388     std::string str(argv[i]);
389     argc--;
390
391     if(str == "-h" || str == "--help") {
392       printUsage();
393       return false;
394     } else if(str == "-v" || str == "--version") {
395       printVersion();
396       return false;
397     }
398
399 #if defined(ANYTUN_OPTIONS) || defined(ANYCTR_OPTIONS)
400
401 #if !defined(_MSC_VER) && !defined(MINGW)
402     PARSE_INVERSE_BOOL_PARAM("-D","--nodaemonize", daemonize_, NOTHING)
403     PARSE_SCALAR_PARAM("-u","--username", username_, NOTHING)
404     PARSE_SCALAR_PARAM("-g","--groupname", groupname_, NOTHING)
405     PARSE_SCALAR_PARAM("-C","--chroot", chroot_dir_, NOTHING)
406     PARSE_SCALAR_PARAM("-P","--write-pid", pid_file_, NOTHING)
407 #endif
408
409 #endif
410
411     PARSE_STRING_LIST("-L","--log", log_targets_, NOTHING)
412     PARSE_BOOL_PARAM("-U","--debug", debug_, NOTHING)
413
414 #if defined(ANYCTR_OPTIONS)
415
416     PARSE_SCALAR_PARAM("-f","--file", file_name_, NOTHING)
417     PARSE_SCALAR_PARAM("-X","--control-host", bind_to_, NOTHING)
418
419 #endif
420 #if defined(ANYTUN_OPTIONS)
421
422     PARSE_SCALAR_PARAM("-i","--interface", local_.addr, NOTHING)
423     PARSE_SCALAR_PARAM("-p","--port", local_.port, NOTHING)
424     PARSE_SCALAR_PARAM("-s","--sender-id", sender_id_, NOTHING)
425
426 #endif
427 #if defined(ANYTUN_OPTIONS) || defined(ANYCONF_OPTIONS)
428
429     PARSE_SCALAR_PARAM("-r","--remote-host", remote_.addr, connection_opts = true)
430     PARSE_SCALAR_PARAM("-o","--remote-port", remote_.port, connection_opts = true)
431     PARSE_BOOL_PARAM("-4","--ipv4-only", ipv4_only, connection_opts = true)
432     PARSE_BOOL_PARAM("-6","--ipv6-only", ipv6_only, connection_opts = true)
433
434 #endif
435 #if defined(ANYTUN_OPTIONS)
436
437     PARSE_SCALAR_PARAM("-I","--sync-interface", local_sync_.addr, cluster_opts = true)
438     PARSE_SCALAR_PARAM("-S","--sync-port", local_sync_.port, cluster_opts = true)
439     PARSE_CSLIST_PARAM("-M","--sync-hosts", remote_sync_hosts_, OptionHost, cluster_opts = true)
440     PARSE_CSLIST_PARAM("-X","--control-host", remote_sync_hosts_, OptionHost, cluster_opts = true)
441
442     PARSE_SCALAR_PARAM("-d","--dev", dev_name_, NOTHING)
443     PARSE_SCALAR_PARAM("-t","--type", dev_type_, NOTHING)
444     PARSE_SCALAR_PARAM("-n","--ifconfig", ifconfig_param_, NOTHING)
445     PARSE_SCALAR_PARAM("-x","--post-up-script", post_up_script_, NOTHING)
446
447 #endif
448 #if defined(ANYTUN_OPTIONS) || defined(ANYCONF_OPTIONS)
449
450 #ifndef NO_ROUTING
451     PARSE_CSLIST_PARAM("-R","--route", routes_, OptionNetwork, connection_opts = true)
452 #endif
453
454     PARSE_SCALAR_PARAM("-m","--mux", mux_, connection_opts = true)
455     PARSE_SCALAR_PARAM("-w","--window-size", seq_window_size_, connection_opts = true)
456
457 #ifndef NO_CRYPT
458     PARSE_SCALAR_PARAM("-k","--kd-prf", kd_prf_, connection_opts = true)
459     PARSE_SCALAR_PARAM("-e","--role", role, connection_opts = true)
460 #ifndef NO_PASSPHRASE
461     PARSE_PHRASE_PARAM_SEC("-E","--passphrase", passphrase_, connection_opts = true)
462 #endif
463     PARSE_HEXSTRING_PARAM_SEC("-K","--key", key_, connection_opts = true)
464     PARSE_HEXSTRING_PARAM_SEC("-A","--salt", salt_, connection_opts = true)
465 #endif
466
467 #endif
468 #if defined(ANYTUN_OPTIONS)
469
470 #ifndef NO_CRYPT
471     PARSE_SCALAR_PARAM("-c","--cipher", cipher_, NOTHING)
472     PARSE_SCALAR_PARAM("-a","--auth-algo", auth_algo_, NOTHING)
473     PARSE_SCALAR_PARAM("-b","--auth-tag-length", auth_tag_length_, NOTHING)
474 #endif
475
476 #endif
477     else {
478       throw syntax_error(str, 0);
479     }
480   }
481   if(ipv4_only && ipv6_only) {
482     throw syntax_error("-4 and -6 are mutual exclusive", -1);
483   }
484   if(ipv4_only) {
485     resolv_addr_type_ = IPV4_ONLY;
486   }
487   if(ipv6_only) {
488     resolv_addr_type_ = IPV6_ONLY;
489   }
490
491   if(role != "") {
492     if(role == "alice" || role == "server" || role == "left") {
493       role_ = ROLE_LEFT;
494     } else if(role == "bob" || role == "client" || role == "right") {
495       role_ = ROLE_RIGHT;
496     } else {
497       throw syntax_error("unknown role name: " + role, -1);
498     }
499   }
500
501   if(debug_) {
502     log_targets_.push_back("stdout:5");
503     daemonize_ = false;
504   }
505
506   if(log_targets_.empty()) {
507 #if !defined(_MSC_VER) && !defined(MINGW)
508 #if !defined(ANYCONF_OPTIONS)
509     log_targets_.push_back(std::string("syslog:3,").append(progname_).append(",daemon"));
510 #else
511     log_targets_.push_back("stderr:2");
512 #endif
513 #else
514 #ifdef WIN_SERVICE
515     log_targets_.push_back(std::string("eventlog:3,").append(progname_));
516 #else
517     log_targets_.push_back("stdout:3");
518 #endif
519 #endif
520   }
521
522   return true;
523 }
524
525 void Options::parse_post()
526 {
527 #if defined(ANYTUN_OPTIONS)
528   if(cluster_opts && connection_opts) {
529     cLog.msg(Log::PRIO_WARNING) << "you have provided options for cluster support as well as connection oriented options, we strongly recommend to use anytun-config and anytun-controld when building a cluster";
530   }
531
532   if(cipher_ == "null" && auth_algo_ == "null") {
533     kd_prf_ = "null";
534   }
535   if((cipher_ != "null" || auth_algo_ != "null") && kd_prf_ == "null") {
536     cLog.msg(Log::PRIO_WARNING) << "using NULL key derivation with encryption and or authentication enabled!";
537   }
538
539   uint32_t tag_len_max = AuthAlgoFactory::getDigestLength(auth_algo_);
540   if(!tag_len_max) { auth_tag_length_ = 0; }
541   else if(tag_len_max < auth_tag_length_) {
542     cLog.msg(Log::PRIO_WARNING) << auth_algo_ << " auth algo can't generate tags of length " << auth_tag_length_ << ", using maximum tag length(" << tag_len_max << ")";
543     auth_tag_length_ = tag_len_max;
544   }
545 #endif
546
547   if(dev_name_ == "" && dev_type_ == "") {
548     dev_type_ = "tun";
549   }
550 }
551
552 void Options::printVersion()
553 {
554 #if defined(ANYCTR_OPTIONS)
555   std::cout << "anytun-controld";
556 #elif defined(ANYCONF_OPTIONS)
557   std::cout << "anytun-config";
558 #else
559   std::cout << "anytun";
560 #endif
561   std::cout << VERSION_STRING_0 << std::endl;
562   std::cout << VERSION_STRING_1 << std::endl;
563 }
564
565 void Options::printUsage()
566 {
567   std::cout << "USAGE:" << std::endl;
568
569 #if defined(ANYCTR_OPTIONS)
570   std::cout << "anytun-controld " << std::endl;
571 #elif defined(ANYCONF_OPTIONS)
572   std::cout << "anytun-config " << std::endl;
573 #else
574   std::cout << "anytun " << std::endl;
575 #endif
576
577   std::cout << "   [-h|--help]                         prints this..." << std::endl;
578   std::cout << "   [-v|--version]                      print version info and exit" << std::endl;
579
580 #if defined(ANYTUN_OPTIONS) || defined(ANYCTR_OPTIONS)
581
582 #if !defined(_MSC_VER) && !defined(MINGW)
583   std::cout << "   [-D|--nodaemonize]                  don't run in background" << std::endl;
584   std::cout << "   [-u|--username] <username>          change to this user" << std::endl;
585   std::cout << "   [-g|--groupname] <groupname>        change to this group" << std::endl;
586   std::cout << "   [-C|--chroot] <path>                chroot to this directory" << std::endl;
587   std::cout << "   [-P|--write-pid] <path>             write pid to this file" << std::endl;
588 #endif
589
590 #endif
591
592   std::cout << "   [-L|--log] <target>:<level>[,<param1>[,<param2>..]]" << std::endl;
593   std::cout << "                                       add a log target, can be invoked several times" << std::endl;
594   std::cout << "                                       i.e.: stdout:5" << std::endl;
595   std::cout << "   [-U|--debug]                        don't daemonize and log to stdout with maximum log level" << std::endl;
596
597 #if defined(ANYCTR_OPTIONS)
598
599   std::cout << "   [-f|--file] <path>                  path to input file" << std::endl;
600   std::cout << "   [-X|--control-host] < <hostname|ip>[:<port>] | :<port> >" << std::endl;
601   std::cout << "                                       local tcp port and or ip address to bind to" << std::endl;
602
603 #endif
604 #if defined(ANYTUN_OPTIONS)
605
606   std::cout << "   [-i|--interface] <hostname|ip>      local anycast ip address to bind to" << std::endl;
607   std::cout << "   [-p|--port] <port>                  local anycast(data) port to bind to" << std::endl;
608   std::cout << "   [-s|--sender-id ] <sender id>       the sender id to use" << std::endl;
609
610 #endif
611 #if defined(ANYTUN_OPTIONS) || defined(ANYCONF_OPTIONS)
612
613   std::cout << "   [-r|--remote-host] <hostname|ip>    remote host" << std::endl;
614   std::cout << "   [-o|--remote-port] <port>           remote port" << std::endl;
615   std::cout << "   [-4|--ipv4-only]                    always resolv IPv4 addresses" << std::endl;
616   std::cout << "   [-6|--ipv6-only]                    always resolv IPv6 addresses" << std::endl;
617
618 #endif
619 #if defined(ANYTUN_OPTIONS)
620
621   std::cout << "   [-I|--sync-interface] <ip-address>  local unicast(sync) ip address to bind to" << std::endl;
622   std::cout << "   [-S|--sync-port] <port>             local unicast(sync) port to bind to" << std::endl;
623   std::cout << "   [-M|--sync-hosts] <hostname|ip>[:<port>][,<hostname|ip>[:<port>][...]]"<< std::endl;
624   std::cout << "                                       remote hosts to sync with" << std::endl;
625   std::cout << "   [-X|--control-host] <hostname|ip>[:<port>]"<< std::endl;
626   std::cout << "                                       fetch the config from this host" << std::endl;
627
628   std::cout << "   [-d|--dev] <name>                   device name" << std::endl;
629   std::cout << "   [-t|--type] <tun|tap>               device type" << std::endl;
630   std::cout << "   [-n|--ifconfig] <local>/<prefix>    the local address for the tun/tap device and the used prefix length" << std::endl;
631   std::cout << "   [-x|--post-up-script] <script>      script gets called after interface is created" << std::endl;
632
633 #endif
634 #if defined(ANYTUN_OPTIONS) || defined(ANYCONF_OPTIONS)
635
636 #ifndef NO_ROUTING
637   std::cout << "   [-R|--route] <net>/<prefix length>  add a route to connection, can be invoked several times" << std::endl;
638 #endif
639
640   std::cout << "   [-m|--mux] <mux-id>                 the multiplex id to use" << std::endl;
641   std::cout << "   [-w|--window-size] <window size>    seqence number window size" << std::endl;
642
643 #ifndef NO_CRYPT
644   std::cout << "   [-k|--kd-prf] <kd-prf type>         key derivation pseudo random function" << std::endl;
645   std::cout << "   [-e|--role] <role>                  left (alice) or right (bob)" << std::endl;
646 #ifndef NO_PASSPHRASE
647   std::cout << "   [-E|--passphrase] <pass phrase>     a passprhase to generate master key and salt from" << std::endl;
648 #endif
649   std::cout << "   [-K|--key] <master key>             master key to use for encryption" << std::endl;
650   std::cout << "   [-A|--salt] <master salt>           master salt to use for encryption" << std::endl;
651 #endif
652
653 #endif
654 #if defined(ANYTUN_OPTIONS)
655
656 #ifndef NO_CRYPT
657   std::cout << "   [-c|--cipher] <cipher type>         payload encryption algorithm" << std::endl;
658   std::cout << "   [-a|--auth-algo] <algo type>        message authentication algorithm" << std::endl;
659   std::cout << "   [-b|--auth-tag-length]              length of the auth tag" << std::endl;
660 #endif
661
662 #endif
663 }
664
665 void Options::printOptions()
666 {
667   ReadersLock lock(mutex);
668
669   std::cout << "Options:" << std::endl;
670   std::cout << std::endl;
671   std::cout << "daemonize = " << daemonize_ << std::endl;
672   std::cout << "username = '" << username_ << "'" << std::endl;
673   std::cout << "groupname = '" << groupname_ << "'" << std::endl;
674   std::cout << "chroot_dir = '" << chroot_dir_ << "'" << std::endl;
675   std::cout << "pid_file = '" << pid_file_ << "'" << std::endl;
676   std::cout << std::endl;
677   std::cout << "log_targets:";
678   StringList::const_iterator lit = log_targets_.begin();
679   for(; lit != log_targets_.end(); ++lit) {
680     std::cout << " '" << *lit << "',";
681   }
682   std::cout << std::endl;
683   std::cout << "debug = " << debug_ << std::endl;
684   std::cout << std::endl;
685   std::cout << "file_name = '" << file_name_ << "'" << std::endl;
686   std::cout << "bind_to.addr = '" << bind_to_.addr << "'" << std::endl;
687   std::cout << "bind_to.port = '" << bind_to_.port << "'" << std::endl;
688   std::cout << std::endl;
689   std::cout << "resolv_addr_type = ";
690   switch(resolv_addr_type_) {
691   case ANY:
692     std::cout <<  "any" << std::endl;
693     break;
694   case IPV4_ONLY:
695     std::cout <<  "ipv4-only" << std::endl;
696     break;
697   case IPV6_ONLY:
698     std::cout <<  "ipv6-only" << std::endl;
699     break;
700   default:
701     std::cout <<  "?" << std::endl;
702     break;
703   }
704   std::cout << std::endl;
705   std::cout << "local.addr = '" << local_.addr << "'" << std::endl;
706   std::cout << "local.port = '" << local_.port << "'" << std::endl;
707   std::cout << "remote.addr = '" << remote_.addr << "'" << std::endl;
708   std::cout << "remote.port = '" << remote_.port << "'" << std::endl;
709   std::cout << "local_sync.addr = '" << local_sync_.addr << "'" << std::endl;
710   std::cout << "local_sync.port = '" << local_sync_.port << "'" << std::endl;
711   std::cout << "remote_sync_hosts:" << std::endl;
712   HostList::const_iterator hit = remote_sync_hosts_.begin();
713   for(; hit != remote_sync_hosts_.end(); ++hit) {
714     std::cout << "  '" << hit->addr << "','" << hit->port << "'" << std::endl;
715   }
716   std::cout << std::endl;
717   std::cout << "dev_name = '" << dev_name_ << "'" << std::endl;
718   std::cout << "dev_type = '" << dev_type_ << "'" << std::endl;
719   std::cout << "ifconfig_param_local = '" << ifconfig_param_.net_addr << "/" << ifconfig_param_.prefix_length << "'" << std::endl;
720   std::cout << "post_up_script = '" << post_up_script_ << "'" << std::endl;
721   std::cout << "routes:" << std::endl;
722   NetworkList::const_iterator rit;
723   for(rit = routes_.begin(); rit != routes_.end(); ++rit) {
724     std::cout << "  " << rit->net_addr << "/" << rit->prefix_length << std::endl;
725   }
726   std::cout << std::endl;
727   std::cout << "sender_id = '" << sender_id_ << "'" << std::endl;
728   std::cout << "mux_id = " << mux_ << std::endl;
729   std::cout << "seq_window_size = '" << seq_window_size_ << "'" << std::endl;
730   std::cout << std::endl;
731   std::cout << "cipher = '" << cipher_ << "'" << std::endl;
732   std::cout << "auth_algo = '" << auth_algo_ << "'" << std::endl;
733   std::cout << "auth_tag_length = " << auth_tag_length_ << std::endl;
734   std::cout << "kd_prf = '" << kd_prf_ << "'" << std::endl;
735   std::cout << "role = ";
736   switch(role_) {
737   case ROLE_LEFT:
738     std::cout << "left" << std::endl;
739     break;
740   case ROLE_RIGHT:
741     std::cout << "right" << std::endl;
742     break;
743   default:
744     std::cout << "??" << std::endl;
745     break;
746   }
747   std::cout << "passphrase = '" << passphrase_ << "'" << std::endl;
748   std::cout << "key = " << key_.getHexDumpOneLine() << std::endl;
749   std::cout << "salt = " << salt_.getHexDumpOneLine() << std::endl;
750 }
751
752
753
754 std::string Options::getProgname()
755 {
756   ReadersLock lock(mutex);
757   return progname_;
758 }
759
760 Options& Options::setProgname(std::string p)
761 {
762   WritersLock lock(mutex);
763   progname_ = p;
764   return *this;
765 }
766
767 bool Options::getDaemonize()
768 {
769   ReadersLock lock(mutex);
770   return daemonize_;
771 }
772
773 Options& Options::setDaemonize(bool d)
774 {
775   WritersLock lock(mutex);
776   daemonize_ = d;
777   return *this;
778 }
779
780 std::string Options::getUsername()
781 {
782   ReadersLock lock(mutex);
783   return username_;
784 }
785
786 Options& Options::setUsername(std::string u)
787 {
788   WritersLock lock(mutex);
789   username_ = u;
790   return *this;
791 }
792
793 std::string Options::getGroupname()
794 {
795   ReadersLock lock(mutex);
796   return groupname_;
797 }
798
799 Options& Options::setGroupname(std::string g)
800 {
801   WritersLock lock(mutex);
802   groupname_ = g;
803   return *this;
804 }
805
806 std::string Options::getChrootDir()
807 {
808   ReadersLock lock(mutex);
809   return chroot_dir_;
810 }
811
812 Options& Options::setChrootDir(std::string c)
813 {
814   WritersLock lock(mutex);
815   chroot_dir_ = c;
816   return *this;
817 }
818
819 std::string Options::getPidFile()
820 {
821   ReadersLock lock(mutex);
822   return pid_file_;
823 }
824
825 Options& Options::setPidFile(std::string p)
826 {
827   WritersLock lock(mutex);
828   pid_file_ = p;
829   return *this;
830 }
831
832
833 StringList Options::getLogTargets()
834 {
835   ReadersLock lock(mutex);
836   return log_targets_;
837 }
838
839 bool Options::getDebug()
840 {
841   ReadersLock lock(mutex);
842   return debug_;
843 }
844
845 Options& Options::setDebug(bool d)
846 {
847   WritersLock lock(mutex);
848   debug_ = d;
849   return *this;
850 }
851
852
853 std::string Options::getFileName()
854 {
855   ReadersLock lock(mutex);
856   return file_name_;
857 }
858
859 Options& Options::setFileName(std::string f)
860 {
861   WritersLock lock(mutex);
862   file_name_ = f;
863   return *this;
864 }
865
866 std::string Options::getBindToAddr()
867 {
868   ReadersLock lock(mutex);
869   return bind_to_.addr;
870 }
871
872 Options& Options::setBindToAddr(std::string b)
873 {
874   WritersLock lock(mutex);
875   bind_to_.addr = b;
876   return *this;
877 }
878
879 std::string Options::getBindToPort()
880 {
881   ReadersLock lock(mutex);
882   return bind_to_.port;
883 }
884
885 Options& Options::setBindToPort(std::string b)
886 {
887   WritersLock lock(mutex);
888   bind_to_.port = b;
889   return *this;
890 }
891
892
893 ResolvAddrType Options::getResolvAddrType()
894 {
895   ReadersLock lock(mutex);
896   return resolv_addr_type_;
897 }
898
899 Options& Options::setResolvAddrType(ResolvAddrType r)
900 {
901   WritersLock lock(mutex);
902   resolv_addr_type_ = r;
903   return *this;
904 }
905
906 std::string Options::getLocalAddr()
907 {
908   ReadersLock lock(mutex);
909   return local_.addr;
910 }
911
912 Options& Options::setLocalAddr(std::string l)
913 {
914   WritersLock lock(mutex);
915   local_.addr = l;
916   return *this;
917 }
918
919 std::string Options::getLocalPort()
920 {
921   ReadersLock lock(mutex);
922   return local_.port;
923 }
924
925 Options& Options::setLocalPort(std::string l)
926 {
927   WritersLock lock(mutex);
928   local_.port = l;
929   return *this;
930 }
931
932 std::string Options::getRemoteAddr()
933 {
934   ReadersLock lock(mutex);
935   return remote_.addr;
936 }
937
938 Options& Options::setRemoteAddr(std::string r)
939 {
940   WritersLock lock(mutex);
941   remote_.addr = r;
942   return *this;
943 }
944
945 std::string Options::getRemotePort()
946 {
947   ReadersLock lock(mutex);
948   return remote_.port;
949 }
950
951 Options& Options::setRemotePort(std::string r)
952 {
953   WritersLock lock(mutex);
954   remote_.port = r;
955   return *this;
956 }
957
958 std::string Options::getLocalSyncAddr()
959 {
960   ReadersLock lock(mutex);
961   return local_sync_.addr;
962 }
963
964 Options& Options::setLocalSyncAddr(std::string l)
965 {
966   WritersLock lock(mutex);
967   local_sync_.addr = l;
968   return *this;
969 }
970
971 std::string Options::getLocalSyncPort()
972 {
973   ReadersLock lock(mutex);
974   return local_sync_.port;
975 }
976
977 Options& Options::setLocalSyncPort(std::string l)
978 {
979   WritersLock lock(mutex);
980   local_sync_.port = l;
981   return *this;
982 }
983
984 HostList Options::getRemoteSyncHosts()
985 {
986   ReadersLock lock(mutex);
987   return remote_sync_hosts_;
988 }
989
990
991
992 std::string Options::getDevName()
993 {
994   ReadersLock lock(mutex);
995   return dev_name_;
996 }
997
998 Options& Options::setDevName(std::string d)
999 {
1000   WritersLock lock(mutex);
1001   dev_name_ = d;
1002   return *this;
1003 }
1004
1005 std::string Options::getDevType()
1006 {
1007   ReadersLock lock(mutex);
1008   return dev_type_;
1009 }
1010
1011 Options& Options::setDevType(std::string d)
1012 {
1013   WritersLock lock(mutex);
1014   dev_type_ = d;
1015   return *this;
1016 }
1017
1018 OptionNetwork Options::getIfconfigParam()
1019 {
1020   ReadersLock lock(mutex);
1021   return ifconfig_param_;
1022 }
1023
1024 Options& Options::setIfconfigParam(OptionNetwork i)
1025 {
1026   WritersLock lock(mutex);
1027   ifconfig_param_ = i;
1028   return *this;
1029 }
1030
1031 std::string Options::getPostUpScript()
1032 {
1033   ReadersLock lock(mutex);
1034   return post_up_script_;
1035 }
1036
1037 Options& Options::setPostUpScript(std::string p)
1038 {
1039   WritersLock lock(mutex);
1040   post_up_script_ = p;
1041   return *this;
1042 }
1043
1044 NetworkList Options::getRoutes()
1045 {
1046   ReadersLock lock(mutex);
1047   return routes_;
1048 }
1049
1050
1051
1052 sender_id_t Options::getSenderId()
1053 {
1054   ReadersLock lock(mutex);
1055   return sender_id_;
1056 }
1057
1058 Options& Options::setSenderId(sender_id_t s)
1059 {
1060   WritersLock lock(mutex);
1061   sender_id_ = s;
1062   return *this;
1063 }
1064
1065 mux_t Options::getMux()
1066 {
1067   ReadersLock lock(mutex);
1068   return mux_;
1069 }
1070
1071 Options& Options::setMux(mux_t m)
1072 {
1073   WritersLock lock(mutex);
1074   mux_ = m;
1075   return *this;
1076 }
1077
1078 window_size_t Options::getSeqWindowSize()
1079 {
1080   ReadersLock lock(mutex);
1081   return seq_window_size_;
1082 }
1083
1084 Options& Options::setSeqWindowSize(window_size_t s)
1085 {
1086   WritersLock lock(mutex);
1087   seq_window_size_ = s;
1088   return *this;
1089 }
1090
1091
1092
1093 std::string Options::getCipher()
1094 {
1095   ReadersLock lock(mutex);
1096   return cipher_;
1097 }
1098
1099 Options& Options::setCipher(std::string c)
1100 {
1101   WritersLock lock(mutex);
1102   cipher_ = c;
1103   return *this;
1104 }
1105
1106 std::string Options::getAuthAlgo()
1107 {
1108   ReadersLock lock(mutex);
1109   return auth_algo_;
1110 }
1111
1112 Options& Options::setAuthAlgo(std::string a)
1113 {
1114   WritersLock lock(mutex);
1115   auth_algo_ = a;
1116   return *this;
1117 }
1118
1119 uint32_t Options::getAuthTagLength()
1120 {
1121   ReadersLock lock(mutex);
1122   return auth_tag_length_;
1123 }
1124
1125 Options& Options::setAuthTagLength(uint32_t a)
1126 {
1127   WritersLock lock(mutex);
1128   auth_tag_length_ = a;
1129   return *this;
1130 }
1131
1132
1133 std::string Options::getKdPrf()
1134 {
1135   ReadersLock lock(mutex);
1136   return kd_prf_;
1137 }
1138
1139 Options& Options::setKdPrf(std::string k)
1140 {
1141   WritersLock lock(mutex);
1142   kd_prf_ = k;
1143   return *this;
1144 }
1145
1146 role_t Options::getRole()
1147 {
1148   ReadersLock lock(mutex);
1149   return role_;
1150 }
1151
1152 Options& Options::setRole(role_t r)
1153 {
1154   WritersLock lock(mutex);
1155   role_ = r;
1156   return *this;
1157 }
1158
1159 std::string Options::getPassphrase()
1160 {
1161   ReadersLock lock(mutex);
1162   return passphrase_;
1163 }
1164
1165 Options& Options::setPassphrase(std::string p)
1166 {
1167   WritersLock lock(mutex);
1168   passphrase_ = p;
1169   return *this;
1170 }
1171
1172 Buffer Options::getKey()
1173 {
1174   ReadersLock lock(mutex);
1175   return key_;
1176 }
1177
1178 Options& Options::setKey(std::string k)
1179 {
1180   WritersLock lock(mutex);
1181   key_ = k;
1182   return *this;
1183 }
1184
1185 Buffer Options::getSalt()
1186 {
1187   ReadersLock lock(mutex);
1188   return salt_;
1189 }
1190
1191 Options& Options::setSalt(std::string s)
1192 {
1193   WritersLock lock(mutex);
1194   salt_ = s;
1195   return *this;
1196 }