Imported Upstream version 0.3.3
[anytun.git] / src / options.cpp
index b77ab41..5d70751 100644 (file)
  *  tunneling and relaying of packets of any protocol.
  *
  *
- *  Copyright (C) 2007-2008 Othmar Gsenger, Erwin Nindl, 
+ *  Copyright (C) 2007-2009 Othmar Gsenger, Erwin Nindl, 
  *                          Christian Pointner <satp@wirdorange.org>
  *
  *  This file is part of Anytun.
  *
  *  Anytun is free software: you can redistribute it and/or modify
- *  it under the terms of the GNU General Public License version 3 as
- *  published by the Free Software Foundation.
+ *  it under the terms of the GNU General Public License as published by
+ *  the Free Software Foundation, either version 3 of the License, or
+ *  any later version.
  *
  *  Anytun is distributed in the hope that it will be useful,
  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
@@ -36,6 +37,8 @@
 #include <sstream>
 
 #include "datatypes.h"
+#include "version.h"
+
 #include "options.h"
 #include "log.h"
 #include "authAlgoFactory.h"
@@ -171,6 +174,8 @@ Options::Options() : key_(u_int32_t(0)), salt_(u_int32_t(0))
   chroot_dir_ = "";
   pid_file_ = "";
 
+  debug_ = false;
+
   file_name_ = "";
   bind_to_.addr = "127.0.0.1";
   bind_to_.port = "2323";
@@ -368,12 +373,18 @@ bool Options::parse(int argc, char* argv[])
     std::string str(argv[i]);
     argc--;
 
-    if(str == "-h" || str == "--help")
+    if(str == "-h" || str == "--help") {
+      printUsage();
       return false;
+    }
+    else if(str == "-v" || str == "--version") {
+      printVersion();
+      return false;
+    }
 
 #if defined(ANYTUN_OPTIONS) || defined(ANYCTR_OPTIONS)
 
-  #ifndef NO_DAEMON
+  #ifndef _MSC_VER
     PARSE_INVERSE_BOOL_PARAM("-D","--nodaemonize", daemonize_, NOTHING)
     PARSE_SCALAR_PARAM("-u","--username", username_, NOTHING)
     PARSE_SCALAR_PARAM("-g","--groupname", groupname_, NOTHING)
@@ -384,6 +395,7 @@ bool Options::parse(int argc, char* argv[])
 #endif
 
     PARSE_STRING_LIST("-L","--log", log_targets_, NOTHING)
+    PARSE_BOOL_PARAM("-U","--debug", debug_, NOTHING)
 
 #if defined(ANYCTR_OPTIONS)
 
@@ -416,9 +428,7 @@ bool Options::parse(int argc, char* argv[])
     PARSE_SCALAR_PARAM("-d","--dev", dev_name_, NOTHING)
     PARSE_SCALAR_PARAM("-t","--type", dev_type_, NOTHING)
     PARSE_SCALAR_PARAM("-n","--ifconfig", ifconfig_param_, NOTHING)
-  #ifndef NO_EXEC
     PARSE_SCALAR_PARAM("-x","--post-up-script", post_up_script_, NOTHING)
-  #endif
 
 #endif
 #if defined(ANYTUN_OPTIONS) || defined(ANYCONF_OPTIONS)
@@ -469,6 +479,27 @@ bool Options::parse(int argc, char* argv[])
       throw syntax_error("unknown role name: " + role, -1); 
   }
 
+  if(debug_) {
+    log_targets_.push_back("stdout:5");
+    daemonize_ = false; 
+  }
+
+  if(log_targets_.empty()) {
+#ifndef _MSC_VER
+ #if !defined(ANYCONF_OPTIONS)
+    log_targets_.push_back(std::string("syslog:3,").append(progname_).append(",daemon"));
+ #else
+    log_targets_.push_back("stderr:2");
+ #endif
+#else
+ #ifdef WIN_SERVICE
+    log_targets_.push_back(std::string("eventlog:3,").append(progname_));
+ #else
+    log_targets_.push_back("stdout:3");
+ #endif
+#endif
+  }
+
   return true;
 }
 
@@ -495,6 +526,19 @@ void Options::parse_post()
     dev_type_ = "tun";
 }
 
+void Options::printVersion()
+{
+#if defined(ANYCTR_OPTIONS)
+  std::cout << "anytun-controld";
+#elif defined(ANYCONF_OPTIONS)
+  std::cout << "anytun-config";
+#else
+  std::cout << "anytun";
+#endif
+  std::cout << VERSION_STRING_0 << std::endl;
+  std::cout << VERSION_STRING_1 << std::endl;
+}
+
 void Options::printUsage()
 {
   std::cout << "USAGE:" << std::endl;
@@ -508,10 +552,11 @@ void Options::printUsage()
 #endif
 
   std::cout << "   [-h|--help]                         prints this..." << std::endl;
+  std::cout << "   [-v|--version]                      print version info and exit" << std::endl;
 
 #if defined(ANYTUN_OPTIONS) || defined(ANYCTR_OPTIONS)
 
- #ifndef NO_DAEMON
+ #ifndef _MSC_VER
   std::cout << "   [-D|--nodaemonize]                  don't run in background" << std::endl;
   std::cout << "   [-u|--username] <username>          change to this user" << std::endl;
   std::cout << "   [-g|--groupname] <groupname>        change to this group" << std::endl;
@@ -523,6 +568,8 @@ void Options::printUsage()
 
   std::cout << "   [-L|--log] <target>:<level>[,<param1>[,<param2>..]]" << std::endl;
   std::cout << "                                       add a log target, can be invoked several times" << std::endl;
+  std::cout << "                                       i.e.: stdout:5" << std::endl;
+  std::cout << "   [-U|--debug]                        don't daemonize and log to stdout with maximum log level" << std::endl;
 
 #if defined(ANYCTR_OPTIONS)
 
@@ -558,9 +605,7 @@ void Options::printUsage()
   std::cout << "   [-d|--dev] <name>                   device name" << std::endl;
   std::cout << "   [-t|--type] <tun|tap>               device type" << std::endl;
   std::cout << "   [-n|--ifconfig] <local>/<prefix>    the local address for the tun/tap device and the used prefix length" << std::endl;
- #ifndef NO_EXEC
   std::cout << "   [-x|--post-up-script] <script>      script gets called after interface is created" << std::endl;
- #endif
 
 #endif
 #if defined(ANYTUN_OPTIONS) || defined(ANYCONF_OPTIONS)
@@ -610,7 +655,9 @@ void Options::printOptions()
   StringList::const_iterator lit = log_targets_.begin();
   for(; lit != log_targets_.end(); ++lit)
     std::cout << " '" << *lit << "',";
-  std::cout << std::endl << std::endl;
+  std::cout << std::endl;
+  std::cout << "debug = " << debug_ << std::endl;
+  std::cout << std::endl;
   std::cout << "file_name = '" << file_name_ << "'" << std::endl;
   std::cout << "bind_to.addr = '" << bind_to_.addr << "'" << std::endl;
   std::cout << "bind_to.port = '" << bind_to_.port << "'" << std::endl;
@@ -743,13 +790,24 @@ Options& Options::setPidFile(std::string p)
 }
 
 
-
 StringList Options::getLogTargets()
 {
   ReadersLock lock(mutex);
   return log_targets_;
 }
 
+bool Options::getDebug()
+{
+  ReadersLock lock(mutex);
+  return debug_;
+}
+
+Options& Options::setDebug(bool d)
+{
+  WritersLock lock(mutex);
+  debug_ = d;
+  return *this;
+}
 
 
 std::string Options::getFileName()