New upstream version 0.3.6
[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
563 #if defined(__clang__)
564   std::cout << VERSION_STRING_1 << ", using CLANG " << __clang_version__ << std::endl;
565 #elif defined(__GNUC__)
566   std::cout << VERSION_STRING_1 << ", using GCC " << __GNUC__ << '.' << __GNUC_MINOR__
567             << '.' << __GNUC_PATCHLEVEL__ << std::endl;
568 #else
569   std::cout << VERSION_STRING_1 << std::endl;
570 #endif
571
572 }
573
574 void Options::printUsage()
575 {
576   std::cout << "USAGE:" << std::endl;
577
578 #if defined(ANYCTR_OPTIONS)
579   std::cout << "anytun-controld " << std::endl;
580 #elif defined(ANYCONF_OPTIONS)
581   std::cout << "anytun-config " << std::endl;
582 #else
583   std::cout << "anytun " << std::endl;
584 #endif
585
586   std::cout << "   [-h|--help]                         prints this..." << std::endl;
587   std::cout << "   [-v|--version]                      print version info and exit" << std::endl;
588
589 #if defined(ANYTUN_OPTIONS) || defined(ANYCTR_OPTIONS)
590
591 #if !defined(_MSC_VER) && !defined(MINGW)
592   std::cout << "   [-D|--nodaemonize]                  don't run in background" << std::endl;
593   std::cout << "   [-u|--username] <username>          change to this user" << std::endl;
594   std::cout << "   [-g|--groupname] <groupname>        change to this group" << std::endl;
595   std::cout << "   [-C|--chroot] <path>                chroot to this directory" << std::endl;
596   std::cout << "   [-P|--write-pid] <path>             write pid to this file" << std::endl;
597 #endif
598
599 #endif
600
601   std::cout << "   [-L|--log] <target>:<level>[,<param1>[,<param2>..]]" << std::endl;
602   std::cout << "                                       add a log target, can be invoked several times" << std::endl;
603   std::cout << "                                       i.e.: stdout:5" << std::endl;
604   std::cout << "   [-U|--debug]                        don't daemonize and log to stdout with maximum log level" << std::endl;
605
606 #if defined(ANYCTR_OPTIONS)
607
608   std::cout << "   [-f|--file] <path>                  path to input file" << std::endl;
609   std::cout << "   [-X|--control-host] < <hostname|ip>[:<port>] | :<port> >" << std::endl;
610   std::cout << "                                       local tcp port and or ip address to bind to" << std::endl;
611
612 #endif
613 #if defined(ANYTUN_OPTIONS)
614
615   std::cout << "   [-i|--interface] <hostname|ip>      local anycast ip address to bind to" << std::endl;
616   std::cout << "   [-p|--port] <port>                  local anycast(data) port to bind to" << std::endl;
617   std::cout << "   [-s|--sender-id ] <sender id>       the sender id to use" << std::endl;
618
619 #endif
620 #if defined(ANYTUN_OPTIONS) || defined(ANYCONF_OPTIONS)
621
622   std::cout << "   [-r|--remote-host] <hostname|ip>    remote host" << std::endl;
623   std::cout << "   [-o|--remote-port] <port>           remote port" << std::endl;
624   std::cout << "   [-4|--ipv4-only]                    always resolv IPv4 addresses" << std::endl;
625   std::cout << "   [-6|--ipv6-only]                    always resolv IPv6 addresses" << std::endl;
626
627 #endif
628 #if defined(ANYTUN_OPTIONS)
629
630   std::cout << "   [-I|--sync-interface] <ip-address>  local unicast(sync) ip address to bind to" << std::endl;
631   std::cout << "   [-S|--sync-port] <port>             local unicast(sync) port to bind to" << std::endl;
632   std::cout << "   [-M|--sync-hosts] <hostname|ip>[:<port>][,<hostname|ip>[:<port>][...]]"<< std::endl;
633   std::cout << "                                       remote hosts to sync with" << std::endl;
634   std::cout << "   [-X|--control-host] <hostname|ip>[:<port>]"<< std::endl;
635   std::cout << "                                       fetch the config from this host" << std::endl;
636
637   std::cout << "   [-d|--dev] <name>                   device name" << std::endl;
638   std::cout << "   [-t|--type] <tun|tap>               device type" << std::endl;
639   std::cout << "   [-n|--ifconfig] <local>/<prefix>    the local address for the tun/tap device and the used prefix length" << std::endl;
640   std::cout << "   [-x|--post-up-script] <script>      script gets called after interface is created" << std::endl;
641
642 #endif
643 #if defined(ANYTUN_OPTIONS) || defined(ANYCONF_OPTIONS)
644
645 #ifndef NO_ROUTING
646   std::cout << "   [-R|--route] <net>/<prefix length>  add a route to connection, can be invoked several times" << std::endl;
647 #endif
648
649   std::cout << "   [-m|--mux] <mux-id>                 the multiplex id to use" << std::endl;
650   std::cout << "   [-w|--window-size] <window size>    seqence number window size" << std::endl;
651
652 #ifndef NO_CRYPT
653   std::cout << "   [-k|--kd-prf] <kd-prf type>         key derivation pseudo random function" << std::endl;
654   std::cout << "   [-e|--role] <role>                  left (alice) or right (bob)" << std::endl;
655 #ifndef NO_PASSPHRASE
656   std::cout << "   [-E|--passphrase] <pass phrase>     a passprhase to generate master key and salt from" << std::endl;
657 #endif
658   std::cout << "   [-K|--key] <master key>             master key to use for encryption" << std::endl;
659   std::cout << "   [-A|--salt] <master salt>           master salt to use for encryption" << std::endl;
660 #endif
661
662 #endif
663 #if defined(ANYTUN_OPTIONS)
664
665 #ifndef NO_CRYPT
666   std::cout << "   [-c|--cipher] <cipher type>         payload encryption algorithm" << std::endl;
667   std::cout << "   [-a|--auth-algo] <algo type>        message authentication algorithm" << std::endl;
668   std::cout << "   [-b|--auth-tag-length]              length of the auth tag" << std::endl;
669 #endif
670
671 #endif
672 }
673
674 void Options::printOptions()
675 {
676   ReadersLock lock(mutex);
677
678   std::cout << "Options:" << std::endl;
679   std::cout << std::endl;
680   std::cout << "daemonize = " << daemonize_ << std::endl;
681   std::cout << "username = '" << username_ << "'" << std::endl;
682   std::cout << "groupname = '" << groupname_ << "'" << std::endl;
683   std::cout << "chroot_dir = '" << chroot_dir_ << "'" << std::endl;
684   std::cout << "pid_file = '" << pid_file_ << "'" << std::endl;
685   std::cout << std::endl;
686   std::cout << "log_targets:";
687   StringList::const_iterator lit = log_targets_.begin();
688   for(; lit != log_targets_.end(); ++lit) {
689     std::cout << " '" << *lit << "',";
690   }
691   std::cout << std::endl;
692   std::cout << "debug = " << debug_ << std::endl;
693   std::cout << std::endl;
694   std::cout << "file_name = '" << file_name_ << "'" << std::endl;
695   std::cout << "bind_to.addr = '" << bind_to_.addr << "'" << std::endl;
696   std::cout << "bind_to.port = '" << bind_to_.port << "'" << std::endl;
697   std::cout << std::endl;
698   std::cout << "resolv_addr_type = ";
699   switch(resolv_addr_type_) {
700   case ANY:
701     std::cout <<  "any" << std::endl;
702     break;
703   case IPV4_ONLY:
704     std::cout <<  "ipv4-only" << std::endl;
705     break;
706   case IPV6_ONLY:
707     std::cout <<  "ipv6-only" << std::endl;
708     break;
709   default:
710     std::cout <<  "?" << std::endl;
711     break;
712   }
713   std::cout << std::endl;
714   std::cout << "local.addr = '" << local_.addr << "'" << std::endl;
715   std::cout << "local.port = '" << local_.port << "'" << std::endl;
716   std::cout << "remote.addr = '" << remote_.addr << "'" << std::endl;
717   std::cout << "remote.port = '" << remote_.port << "'" << std::endl;
718   std::cout << "local_sync.addr = '" << local_sync_.addr << "'" << std::endl;
719   std::cout << "local_sync.port = '" << local_sync_.port << "'" << std::endl;
720   std::cout << "remote_sync_hosts:" << std::endl;
721   HostList::const_iterator hit = remote_sync_hosts_.begin();
722   for(; hit != remote_sync_hosts_.end(); ++hit) {
723     std::cout << "  '" << hit->addr << "','" << hit->port << "'" << std::endl;
724   }
725   std::cout << std::endl;
726   std::cout << "dev_name = '" << dev_name_ << "'" << std::endl;
727   std::cout << "dev_type = '" << dev_type_ << "'" << std::endl;
728   std::cout << "ifconfig_param_local = '" << ifconfig_param_.net_addr << "/" << ifconfig_param_.prefix_length << "'" << std::endl;
729   std::cout << "post_up_script = '" << post_up_script_ << "'" << std::endl;
730   std::cout << "routes:" << std::endl;
731   NetworkList::const_iterator rit;
732   for(rit = routes_.begin(); rit != routes_.end(); ++rit) {
733     std::cout << "  " << rit->net_addr << "/" << rit->prefix_length << std::endl;
734   }
735   std::cout << std::endl;
736   std::cout << "sender_id = '" << sender_id_ << "'" << std::endl;
737   std::cout << "mux_id = " << mux_ << std::endl;
738   std::cout << "seq_window_size = '" << seq_window_size_ << "'" << std::endl;
739   std::cout << std::endl;
740   std::cout << "cipher = '" << cipher_ << "'" << std::endl;
741   std::cout << "auth_algo = '" << auth_algo_ << "'" << std::endl;
742   std::cout << "auth_tag_length = " << auth_tag_length_ << std::endl;
743   std::cout << "kd_prf = '" << kd_prf_ << "'" << std::endl;
744   std::cout << "role = ";
745   switch(role_) {
746   case ROLE_LEFT:
747     std::cout << "left" << std::endl;
748     break;
749   case ROLE_RIGHT:
750     std::cout << "right" << std::endl;
751     break;
752   default:
753     std::cout << "??" << std::endl;
754     break;
755   }
756   std::cout << "passphrase = '" << passphrase_ << "'" << std::endl;
757   std::cout << "key = " << key_.getHexDumpOneLine() << std::endl;
758   std::cout << "salt = " << salt_.getHexDumpOneLine() << std::endl;
759 }
760
761
762
763 std::string Options::getProgname()
764 {
765   ReadersLock lock(mutex);
766   return progname_;
767 }
768
769 Options& Options::setProgname(std::string p)
770 {
771   WritersLock lock(mutex);
772   progname_ = p;
773   return *this;
774 }
775
776 bool Options::getDaemonize()
777 {
778   ReadersLock lock(mutex);
779   return daemonize_;
780 }
781
782 Options& Options::setDaemonize(bool d)
783 {
784   WritersLock lock(mutex);
785   daemonize_ = d;
786   return *this;
787 }
788
789 std::string Options::getUsername()
790 {
791   ReadersLock lock(mutex);
792   return username_;
793 }
794
795 Options& Options::setUsername(std::string u)
796 {
797   WritersLock lock(mutex);
798   username_ = u;
799   return *this;
800 }
801
802 std::string Options::getGroupname()
803 {
804   ReadersLock lock(mutex);
805   return groupname_;
806 }
807
808 Options& Options::setGroupname(std::string g)
809 {
810   WritersLock lock(mutex);
811   groupname_ = g;
812   return *this;
813 }
814
815 std::string Options::getChrootDir()
816 {
817   ReadersLock lock(mutex);
818   return chroot_dir_;
819 }
820
821 Options& Options::setChrootDir(std::string c)
822 {
823   WritersLock lock(mutex);
824   chroot_dir_ = c;
825   return *this;
826 }
827
828 std::string Options::getPidFile()
829 {
830   ReadersLock lock(mutex);
831   return pid_file_;
832 }
833
834 Options& Options::setPidFile(std::string p)
835 {
836   WritersLock lock(mutex);
837   pid_file_ = p;
838   return *this;
839 }
840
841
842 StringList Options::getLogTargets()
843 {
844   ReadersLock lock(mutex);
845   return log_targets_;
846 }
847
848 bool Options::getDebug()
849 {
850   ReadersLock lock(mutex);
851   return debug_;
852 }
853
854 Options& Options::setDebug(bool d)
855 {
856   WritersLock lock(mutex);
857   debug_ = d;
858   return *this;
859 }
860
861
862 std::string Options::getFileName()
863 {
864   ReadersLock lock(mutex);
865   return file_name_;
866 }
867
868 Options& Options::setFileName(std::string f)
869 {
870   WritersLock lock(mutex);
871   file_name_ = f;
872   return *this;
873 }
874
875 std::string Options::getBindToAddr()
876 {
877   ReadersLock lock(mutex);
878   return bind_to_.addr;
879 }
880
881 Options& Options::setBindToAddr(std::string b)
882 {
883   WritersLock lock(mutex);
884   bind_to_.addr = b;
885   return *this;
886 }
887
888 std::string Options::getBindToPort()
889 {
890   ReadersLock lock(mutex);
891   return bind_to_.port;
892 }
893
894 Options& Options::setBindToPort(std::string b)
895 {
896   WritersLock lock(mutex);
897   bind_to_.port = b;
898   return *this;
899 }
900
901
902 ResolvAddrType Options::getResolvAddrType()
903 {
904   ReadersLock lock(mutex);
905   return resolv_addr_type_;
906 }
907
908 Options& Options::setResolvAddrType(ResolvAddrType r)
909 {
910   WritersLock lock(mutex);
911   resolv_addr_type_ = r;
912   return *this;
913 }
914
915 std::string Options::getLocalAddr()
916 {
917   ReadersLock lock(mutex);
918   return local_.addr;
919 }
920
921 Options& Options::setLocalAddr(std::string l)
922 {
923   WritersLock lock(mutex);
924   local_.addr = l;
925   return *this;
926 }
927
928 std::string Options::getLocalPort()
929 {
930   ReadersLock lock(mutex);
931   return local_.port;
932 }
933
934 Options& Options::setLocalPort(std::string l)
935 {
936   WritersLock lock(mutex);
937   local_.port = l;
938   return *this;
939 }
940
941 std::string Options::getRemoteAddr()
942 {
943   ReadersLock lock(mutex);
944   return remote_.addr;
945 }
946
947 Options& Options::setRemoteAddr(std::string r)
948 {
949   WritersLock lock(mutex);
950   remote_.addr = r;
951   return *this;
952 }
953
954 std::string Options::getRemotePort()
955 {
956   ReadersLock lock(mutex);
957   return remote_.port;
958 }
959
960 Options& Options::setRemotePort(std::string r)
961 {
962   WritersLock lock(mutex);
963   remote_.port = r;
964   return *this;
965 }
966
967 std::string Options::getLocalSyncAddr()
968 {
969   ReadersLock lock(mutex);
970   return local_sync_.addr;
971 }
972
973 Options& Options::setLocalSyncAddr(std::string l)
974 {
975   WritersLock lock(mutex);
976   local_sync_.addr = l;
977   return *this;
978 }
979
980 std::string Options::getLocalSyncPort()
981 {
982   ReadersLock lock(mutex);
983   return local_sync_.port;
984 }
985
986 Options& Options::setLocalSyncPort(std::string l)
987 {
988   WritersLock lock(mutex);
989   local_sync_.port = l;
990   return *this;
991 }
992
993 HostList Options::getRemoteSyncHosts()
994 {
995   ReadersLock lock(mutex);
996   return remote_sync_hosts_;
997 }
998
999
1000
1001 std::string Options::getDevName()
1002 {
1003   ReadersLock lock(mutex);
1004   return dev_name_;
1005 }
1006
1007 Options& Options::setDevName(std::string d)
1008 {
1009   WritersLock lock(mutex);
1010   dev_name_ = d;
1011   return *this;
1012 }
1013
1014 std::string Options::getDevType()
1015 {
1016   ReadersLock lock(mutex);
1017   return dev_type_;
1018 }
1019
1020 Options& Options::setDevType(std::string d)
1021 {
1022   WritersLock lock(mutex);
1023   dev_type_ = d;
1024   return *this;
1025 }
1026
1027 OptionNetwork Options::getIfconfigParam()
1028 {
1029   ReadersLock lock(mutex);
1030   return ifconfig_param_;
1031 }
1032
1033 Options& Options::setIfconfigParam(OptionNetwork i)
1034 {
1035   WritersLock lock(mutex);
1036   ifconfig_param_ = i;
1037   return *this;
1038 }
1039
1040 std::string Options::getPostUpScript()
1041 {
1042   ReadersLock lock(mutex);
1043   return post_up_script_;
1044 }
1045
1046 Options& Options::setPostUpScript(std::string p)
1047 {
1048   WritersLock lock(mutex);
1049   post_up_script_ = p;
1050   return *this;
1051 }
1052
1053 NetworkList Options::getRoutes()
1054 {
1055   ReadersLock lock(mutex);
1056   return routes_;
1057 }
1058
1059
1060
1061 sender_id_t Options::getSenderId()
1062 {
1063   ReadersLock lock(mutex);
1064   return sender_id_;
1065 }
1066
1067 Options& Options::setSenderId(sender_id_t s)
1068 {
1069   WritersLock lock(mutex);
1070   sender_id_ = s;
1071   return *this;
1072 }
1073
1074 mux_t Options::getMux()
1075 {
1076   ReadersLock lock(mutex);
1077   return mux_;
1078 }
1079
1080 Options& Options::setMux(mux_t m)
1081 {
1082   WritersLock lock(mutex);
1083   mux_ = m;
1084   return *this;
1085 }
1086
1087 window_size_t Options::getSeqWindowSize()
1088 {
1089   ReadersLock lock(mutex);
1090   return seq_window_size_;
1091 }
1092
1093 Options& Options::setSeqWindowSize(window_size_t s)
1094 {
1095   WritersLock lock(mutex);
1096   seq_window_size_ = s;
1097   return *this;
1098 }
1099
1100
1101
1102 std::string Options::getCipher()
1103 {
1104   ReadersLock lock(mutex);
1105   return cipher_;
1106 }
1107
1108 Options& Options::setCipher(std::string c)
1109 {
1110   WritersLock lock(mutex);
1111   cipher_ = c;
1112   return *this;
1113 }
1114
1115 std::string Options::getAuthAlgo()
1116 {
1117   ReadersLock lock(mutex);
1118   return auth_algo_;
1119 }
1120
1121 Options& Options::setAuthAlgo(std::string a)
1122 {
1123   WritersLock lock(mutex);
1124   auth_algo_ = a;
1125   return *this;
1126 }
1127
1128 uint32_t Options::getAuthTagLength()
1129 {
1130   ReadersLock lock(mutex);
1131   return auth_tag_length_;
1132 }
1133
1134 Options& Options::setAuthTagLength(uint32_t a)
1135 {
1136   WritersLock lock(mutex);
1137   auth_tag_length_ = a;
1138   return *this;
1139 }
1140
1141
1142 std::string Options::getKdPrf()
1143 {
1144   ReadersLock lock(mutex);
1145   return kd_prf_;
1146 }
1147
1148 Options& Options::setKdPrf(std::string k)
1149 {
1150   WritersLock lock(mutex);
1151   kd_prf_ = k;
1152   return *this;
1153 }
1154
1155 role_t Options::getRole()
1156 {
1157   ReadersLock lock(mutex);
1158   return role_;
1159 }
1160
1161 Options& Options::setRole(role_t r)
1162 {
1163   WritersLock lock(mutex);
1164   role_ = r;
1165   return *this;
1166 }
1167
1168 std::string Options::getPassphrase()
1169 {
1170   ReadersLock lock(mutex);
1171   return passphrase_;
1172 }
1173
1174 Options& Options::setPassphrase(std::string p)
1175 {
1176   WritersLock lock(mutex);
1177   passphrase_ = p;
1178   return *this;
1179 }
1180
1181 Buffer Options::getKey()
1182 {
1183   ReadersLock lock(mutex);
1184   return key_;
1185 }
1186
1187 Options& Options::setKey(std::string k)
1188 {
1189   WritersLock lock(mutex);
1190   key_ = k;
1191   return *this;
1192 }
1193
1194 Buffer Options::getSalt()
1195 {
1196   ReadersLock lock(mutex);
1197   return salt_;
1198 }
1199
1200 Options& Options::setSalt(std::string s)
1201 {
1202   WritersLock lock(mutex);
1203   salt_ = s;
1204   return *this;
1205 }