Imported Upstream version 0.3.2
[anytun.git] / src / bsd / tunDevice.cpp
index 408434e..4fdd5fd 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
@@ -29,6 +30,9 @@
  *  along with anytun.  If not, see <http://www.gnu.org/licenses/>.
  */
 
+#include <sstream>
+#include <boost/assign.hpp>
+
 #include <fcntl.h>
 #include <unistd.h>
 #include <errno.h>
 #include <netinet/in.h>
 #include <netinet/ip.h>
 
-#include <sstream>
-
 #include "tunDevice.h"
 #include "threadUtils.hpp"
 #include "log.h"
 #include "anytunError.h"
+#include "sysExec.h"
 
 #define DEVICE_FILE_MAX 255
 
-TunDevice::TunDevice(std::string dev_name, std::string dev_type, std::string ifcfg_addr, std::string ifcfg_prefix) : conf_(dev_name, dev_type, ifcfg_addr, ifcfg_prefix, 1400)
+TunDevice::TunDevice(std::string dev_name, std::string dev_type, std::string ifcfg_addr, u_int16_t ifcfg_prefix) : conf_(dev_name, dev_type, ifcfg_addr, ifcfg_prefix, 1400)
 {
   std::string device_file = "/dev/";
   bool dynamic = true;
@@ -154,22 +157,22 @@ void TunDevice::init_post()
   if(conf_.type_ == TYPE_TAP)
     with_pi_ = false;
 
-  if(dev->type_ == TYPE_TUN) {
+  if(conf_.type_ == TYPE_TUN) {
     int arg = 0;
-    if(ioctl(dev->fd_, TUNSLMODE, &arg) < 0) {
+    if(ioctl(fd_, TUNSLMODE, &arg) < 0) {
       ::close(fd_);
       AnytunError::throwErr() << "can't disable link-layer mode for interface: " << AnytunErrno(errno);
     }
 
     arg = 1;
-    if(ioctl(dev->fd_, TUNSIFHEAD, &arg) < 0) {
+    if(ioctl(fd_, TUNSIFHEAD, &arg) < 0) {
       ::close(fd_);
       AnytunError::throwErr() << "can't enable multi-af modefor interface: " << AnytunErrno(errno);
     }
 
     arg = IFF_BROADCAST;
     arg |= IFF_MULTICAST;
-    if(ioctl(dev->fd_, TUNSIFMODE, &arg) < 0) {
+    if(ioctl(fd_, TUNSIFMODE, &arg) < 0) {
       ::close(fd_);
       AnytunError::throwErr() << "can't enable multicast for interface: " << AnytunErrno(errno);
     }
@@ -197,7 +200,7 @@ int TunDevice::fix_return(int ret, size_t pi_length) const
   if(ret < 0)
     return ret;
 
-  return (static_cast<size_t>(ret) > type_length ? (ret - type_length) : 0);
+  return (static_cast<size_t>(ret) > pi_length ? (ret - pi_length) : 0);
 }
 
 int TunDevice::read(u_int8_t* buf, u_int32_t len)
@@ -250,34 +253,23 @@ int TunDevice::write(u_int8_t* buf, u_int32_t len)
 
 void TunDevice::do_ifconfig()
 {
-  std::ostringstream command;
-  command << "/sbin/ifconfig " << actual_name_ << " " << conf_.addr_.toString()
-          << " netmask " << conf_.netmask_.toString() << " mtu " << conf_.mtu_;
+  std::ostringstream mtu_ss;
+  mtu_ss << conf_.mtu_;
+  StringVector args = boost::assign::list_of(actual_name_)(conf_.addr_.toString())("netmask")(conf_.netmask_.toString())("mtu")(mtu_ss.str());
 
   if(conf_.type_ == TYPE_TUN)
-    command << " up";
+    args.push_back("up");
   else {
 #if defined(__GNUC__) && defined(__OpenBSD__)
-    command << " link0";
+    args.push_back("link0");
 #elif defined(__GNUC__) && defined(__FreeBSD__)
-    command << " up";
+    args.push_back("up");
 #elif defined(__GNUC__) && defined(__NetBSD__)
-    command << "";
+        // nothing to be done here
 #else
  #error This Device works just for OpenBSD, FreeBSD or NetBSD
 #endif
   }
 
-  int result = system(command.str().c_str());
-  if(result == -1)
-    cLog.msg(Log::PRIO_ERROR) << "Execution of ifconfig failed" << AnytunErrno(errno);
-  else {
-    if(WIFEXITED(result))
-      cLog.msg(Log::PRIO_NOTICE) << "ifconfig returned " << WEXITSTATUS(result);  
-    else if(WIFSIGNALED(result))
-      cLog.msg(Log::PRIO_NOTICE) << "ifconfig terminated after signal " << WTERMSIG(result);
-    else
-      cLog.msg(Log::PRIO_ERROR) << "Execution of ifconfig: unkown error";
-  }
-
+  anytun_exec("/sbin/ifconfig", args);
 }