summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorChristian Pointner <equinox@anytun.org>2009-02-22 07:20:56 +0000
committerChristian Pointner <equinox@anytun.org>2009-02-22 07:20:56 +0000
commitb7013f1be9a5f09ecae4b37f0986352abfcd0dc1 (patch)
treef890c7677d27953afaf05215a9c91a86b23897bd
parentfixed signalController on unix (diff)
replaced regular throws with AnytunError::thowErr
-rw-r--r--src/bsd/tunDevice.cpp14
-rw-r--r--src/daemon.hpp64
-rw-r--r--src/deviceConfig.hpp5
-rw-r--r--src/encryptedPacket.cpp7
-rw-r--r--src/logTargets.cpp28
-rw-r--r--src/networkAddress.cpp9
-rw-r--r--src/plainPacket.cpp3
-rw-r--r--src/routingTable.cpp12
-rw-r--r--src/routingTree.hpp4
-rw-r--r--src/signalController.cpp8
-rw-r--r--src/win32/tunDevice.cpp43
-rw-r--r--src/win32/winService.cpp47
12 files changed, 100 insertions, 144 deletions
diff --git a/src/bsd/tunDevice.cpp b/src/bsd/tunDevice.cpp
index 05b59f1..eee78ca 100644
--- a/src/bsd/tunDevice.cpp
+++ b/src/bsd/tunDevice.cpp
@@ -47,6 +47,8 @@
#include "tunDevice.h"
#include "threadUtils.hpp"
#include "log.h"
+#include "anytunError.hpp"
+
#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)
@@ -73,7 +75,7 @@ TunDevice::TunDevice(std::string dev_name, std::string dev_type, std::string ifc
}
#endif
else
- throw std::runtime_error("unable to recognize type of device (tun or tap)");
+ AnytunError::throwErr() << "unable to recognize type of device (tun or tap)";
u_int32_t dev_id=0;
if(dynamic) {
@@ -90,12 +92,10 @@ TunDevice::TunDevice(std::string dev_name, std::string dev_type, std::string ifc
fd_ = ::open(device_file.c_str(), O_RDWR);
if(fd_ < 0) {
- std::stringstream msg;
if(dynamic)
- msg << "can't open device file dynamically: no unused node left";
+ AnytunError::throwErr() << "can't open device file dynamically: no unused node left";
else
- msg << "can't open device file (" << device_file << "): " << LogErrno(errno);
- throw std::runtime_error(msg.str());
+ AnytunError::throwErr() << "can't open device file (" << device_file << "): " << LogErrno(errno);
}
if(dynamic) {
@@ -133,7 +133,7 @@ void TunDevice::init_post()
if (ioctl(fd_, TUNGIFINFO, &ti) < 0) {
::close(fd_);
- throw std::runtime_error("can't enable multicast for interface");
+ AnytunError::throwErr() << "can't enable multicast for interface";
}
ti.flags |= IFF_MULTICAST;
@@ -142,7 +142,7 @@ void TunDevice::init_post()
if (ioctl(fd_, TUNSIFINFO, &ti) < 0) {
::close(fd_);
- throw std::runtime_error("can't enable multicast for interface");
+ AnytunError::throwErr() << "can't enable multicast for interface";
}
}
diff --git a/src/daemon.hpp b/src/daemon.hpp
index cc9a2b2..992d354 100644
--- a/src/daemon.hpp
+++ b/src/daemon.hpp
@@ -33,8 +33,6 @@
#define _DAEMON_HPP
#ifndef NO_DAEMON
-#include <sstream>
-
#include <poll.h>
#include <fcntl.h>
#include <pwd.h>
@@ -44,6 +42,7 @@
#include <unistd.h>
#include "log.h"
+#include "anytunError.hpp"
#ifndef NO_PRIVDROP
class PrivInfo
@@ -59,7 +58,7 @@ public:
pw_ = getpwnam(username.c_str());
if(!pw_)
- throw std::runtime_error("unkown user " + username);
+ AnytunError::throwErr() << "unkown user " << username;
if(groupname != "")
gr_ = getgrnam(groupname.c_str());
@@ -67,7 +66,7 @@ public:
gr_ = getgrgid(pw_->pw_gid);
if(!gr_)
- throw std::runtime_error("unkown group " + groupname);
+ AnytunError::throwErr() << "unkown group " << groupname;
}
void drop()
@@ -75,25 +74,16 @@ public:
if(!pw_ || !gr_)
return;
- if(setgid(gr_->gr_gid)) {
- std::stringstream msg;
- msg << "setgid('" << gr_->gr_name << "') failed: " << LogErrno(errno);
- throw std::runtime_error(msg.str());
- }
+ if(setgid(gr_->gr_gid))
+ AnytunError::throwErr() << "setgid('" << gr_->gr_name << "') failed: " << LogErrno(errno);
gid_t gr_list[1];
gr_list[0] = gr_->gr_gid;
- if(setgroups (1, gr_list)) {
- std::stringstream msg;
- msg << "setgroups(['" << gr_->gr_name << "']) failed: " << LogErrno(errno);
- throw std::runtime_error(msg.str());
- }
+ if(setgroups (1, gr_list))
+ AnytunError::throwErr() << "setgroups(['" << gr_->gr_name << "']) failed: " << LogErrno(errno);
- if(setuid(pw_->pw_uid)) {
- std::stringstream msg;
- msg << "setuid('" << pw_->pw_name << "') failed: " << LogErrno(errno);
- throw std::runtime_error(msg.str());
- }
+ if(setuid(pw_->pw_uid))
+ AnytunError::throwErr() << "setuid('" << pw_->pw_name << "') failed: " << LogErrno(errno);
cLog.msg(Log::PRIO_NOTICE) << "dropped privileges to " << pw_->pw_name << ":" << gr_->gr_name;
}
@@ -107,14 +97,14 @@ private:
void do_chroot(std::string const& chrootdir)
{
if (getuid() != 0)
- throw std::runtime_error("this programm has to be run as root in order to run in a chroot");
+ AnytunError::throwErr() << "this programm has to be run as root in order to run in a chroot";
if(chroot(chrootdir.c_str()))
- throw std::runtime_error("can't chroot to " + chrootdir);
+ AnytunError::throwErr() << "can't chroot to " << chrootdir;
cLog.msg(Log::PRIO_NOTICE) << "we are in chroot jail (" << chrootdir << ") now" << std::endl;
if(chdir("/"))
- throw std::runtime_error("can't change to /");
+ AnytunError::throwErr() << "can't change to /";
}
void daemonize()
@@ -122,34 +112,24 @@ void daemonize()
pid_t pid;
pid = fork();
- if(pid < 0) {
- std::stringstream msg;
- msg << "daemonizing failed at fork(): " << LogErrno(errno) << ", exitting";
- throw std::runtime_error(msg.str());
- }
+ if(pid < 0)
+ AnytunError::throwErr() << "daemonizing failed at fork(): " << LogErrno(errno) << ", exitting";
+
if(pid) exit(0);
umask(0);
- if(setsid() < 0) {
- std::stringstream msg;
- msg << "daemonizing failed at setsid(): " << LogErrno(errno) << ", exitting";
- throw std::runtime_error(msg.str());
- }
+ if(setsid() < 0)
+ AnytunError::throwErr() << "daemonizing failed at setsid(): " << LogErrno(errno) << ", exitting";
pid = fork();
- if(pid < 0) {
- std::stringstream msg;
- msg << "daemonizing failed at fork(): " << LogErrno(errno) << ", exitting";
- throw std::runtime_error(msg.str());
- }
+ if(pid < 0)
+ AnytunError::throwErr() << "daemonizing failed at fork(): " << LogErrno(errno) << ", exitting";
+
if(pid) exit(0);
- if ((chdir("/")) < 0) {
- std::stringstream msg;
- msg << "daemonizing failed at chdir(): " << LogErrno(errno) << ", exitting";
- throw std::runtime_error(msg.str());
- }
+ if ((chdir("/")) < 0)
+ AnytunError::throwErr() << "daemonizing failed at chdir(): " << LogErrno(errno) << ", exitting";
// std::cout << "running in background now..." << std::endl;
diff --git a/src/deviceConfig.hpp b/src/deviceConfig.hpp
index 3c3fd41..515e38d 100644
--- a/src/deviceConfig.hpp
+++ b/src/deviceConfig.hpp
@@ -34,6 +34,7 @@
#include "networkAddress.h"
#include <boost/asio.hpp>
+#include "anytunError.hpp"
class TunDevice;
@@ -61,7 +62,7 @@ public:
}
#else
if(dev_type == "")
- throw std::runtime_error("Device type must be specified on Windows");
+ AnytunError::throwErr() << "Device type must be specified on Windows";
if(dev_type == "tun")
type_ = TYPE_TUN;
@@ -69,7 +70,7 @@ public:
type_ = TYPE_TAP;
if(type_ == TYPE_TUN && ifcfg_addr == "")
- throw std::runtime_error("Device type tun requires ifconfig parameter (--ifconfig)");
+ AnytunError::throwErr() << "Device type tun requires ifconfig parameter (--ifconfig)";
#endif
if(ifcfg_addr != "")
diff --git a/src/encryptedPacket.cpp b/src/encryptedPacket.cpp
index 9a1e063..34d2725 100644
--- a/src/encryptedPacket.cpp
+++ b/src/encryptedPacket.cpp
@@ -37,6 +37,7 @@
#include "endian.h"
#include "datatypes.h"
#include "log.h"
+#include "anytunError.hpp"
EncryptedPacket::EncryptedPacket(u_int32_t payload_length, bool allow_realloc)
: Buffer(payload_length + sizeof(struct HeaderStruct), allow_realloc)
@@ -137,14 +138,14 @@ void EncryptedPacket::reinit()
if(length_ < (sizeof(struct HeaderStruct))) {
header_ = NULL;
- throw std::runtime_error("encrypted packet can't be initialized, buffer is too small");
+ AnytunError::throwErr() << "encrypted packet can't be initialized, buffer is too small";
}
if(auth_tag_)
{
if(length_ < (sizeof(struct HeaderStruct) + AUTHTAG_SIZE)) {
auth_tag_ = NULL;
- throw std::runtime_error("auth-tag can't be enabled, buffer is too small");
+ AnytunError::throwErr() << "auth-tag can't be enabled, buffer is too small";
}
auth_tag_ = buf_ + length_ - AUTHTAG_SIZE;
}
@@ -179,7 +180,7 @@ void EncryptedPacket::withAuthTag(bool b)
if(b)
{
if(length_ < (sizeof(struct HeaderStruct) + AUTHTAG_SIZE))
- throw std::runtime_error("auth-tag can't be enabled, buffer is too small");
+ AnytunError::throwErr() << "auth-tag can't be enabled, buffer is too small";
auth_tag_ = buf_ + length_ - AUTHTAG_SIZE;
}
diff --git a/src/logTargets.cpp b/src/logTargets.cpp
index ac41c0b..d22734d 100644
--- a/src/logTargets.cpp
+++ b/src/logTargets.cpp
@@ -35,6 +35,7 @@
#include "logTargets.h"
#include "log.h"
+#include "anytunError.hpp"
#include "options.h"
@@ -108,50 +109,51 @@ LogTarget* LogTargetList::add(target_type_t type, int prio, std::string conf)
case TARGET_SYSLOG: {
#ifdef LOG_SYSLOG
if(!LogTargetSyslog::duplicateAllowed() && targets.count(TARGET_SYSLOG))
- throw std::runtime_error(targetTypeToString(TARGET_SYSLOG) + " logtarget is supported only once");
+ AnytunError::throwErr() << targetTypeToString(TARGET_SYSLOG) << " logtarget is supported only once";
return targets.insert(TargetsMap::value_type(TARGET_SYSLOG, new LogTargetSyslog(prio, conf)))->second;
#else
- throw std::runtime_error(targetTypeToString(TARGET_SYSLOG) + " logtarget is not supported");
+ AnytunError::throwErr() << targetTypeToString(TARGET_SYSLOG) << " logtarget is not supported";
#endif
}
case TARGET_FILE: {
#ifdef LOG_FILE
if(!LogTargetFile::duplicateAllowed() && targets.count(TARGET_FILE))
- throw std::runtime_error(targetTypeToString(TARGET_FILE) + " logtarget is supported only once");
+ AnytunError::throwErr() << targetTypeToString(TARGET_FILE) << " logtarget is supported only once";
return targets.insert(TargetsMap::value_type(TARGET_FILE, new LogTargetFile(prio, conf)))->second;
#else
- throw std::runtime_error(targetTypeToString(TARGET_FILE) + " logtarget is not supported");
+ AnytunError::throwErr() << targetTypeToString(TARGET_FILE) << " logtarget is not supported";
#endif
}
case TARGET_STDOUT:
case TARGET_STDERR: {
#ifdef LOG_STDOUT
if(!LogTargetStdout::duplicateAllowed() && targets.count(type))
- throw std::runtime_error(targetTypeToString(type) + " logtarget is supported only once");
-
+ AnytunError::throwErr() << targetTypeToString(type) << " logtarget is supported only once";
+
if(type == TARGET_STDERR)
return targets.insert(TargetsMap::value_type(type, new LogTargetStdout(prio, std::cerr)))->second;
else
return targets.insert(TargetsMap::value_type(type, new LogTargetStdout(prio, std::cout)))->second;
#else
- throw std::runtime_error(targetTypeToString(type) + " logtarget is not supported");
+ AnytunError::throwErr() << targetTypeToString(type) + " logtarget is not supported";
#endif
}
case TARGET_WINEVENTLOG: {
#ifdef LOG_WINEVENTLOG
if(!LogTargetWinEventlog::duplicateAllowed() && targets.count(TARGET_WINEVENTLOG))
- throw std::runtime_error(targetTypeToString(TARGET_WINEVENTLOG) + " logtarget is supported only once");
+ AnytunError::throwErr() << targetTypeToString(TARGET_WINEVENTLOG) << " logtarget is supported only once";
return targets.insert(TargetsMap::value_type(TARGET_WINEVENTLOG, new LogTargetWinEventlog(prio, conf)))->second;
#else
- throw std::runtime_error(targetTypeToString(TARGET_WINEVENTLOG) + " logtarget is not supported");
+ AnytunError::throwErr() << targetTypeToString(TARGET_WINEVENTLOG) << " logtarget is not supported";
#endif
}
default:
- throw std::runtime_error("unknown log target");
+ AnytunError::throwErr() << "unknown log target";
}
+ return NULL;
}
void LogTargetList::clear()
@@ -195,7 +197,8 @@ int LogTargetSyslog::facilityFromString(std::string fac)
if(fac == "local6") return FAC_LOCAL6;
if(fac == "local7") return FAC_LOCAL7;
- throw std::runtime_error("unknown syslog facility");
+ AnytunError::throwErr() << "unknown syslog facility";
+ return 0;
}
std::string LogTargetSyslog::facilityToString(int fac)
@@ -220,8 +223,9 @@ std::string LogTargetSyslog::facilityToString(int fac)
case FAC_LOCAL5: return "local5";
case FAC_LOCAL6: return "local6";
case FAC_LOCAL7: return "local7";
- default: throw std::runtime_error("unknown syslog facility");
+ default: AnytunError::throwErr() << "unknown syslog facility";
}
+ return "";
}
LogTargetSyslog::LogTargetSyslog(int prio, std::string conf) : LogTarget(prio)
diff --git a/src/networkAddress.cpp b/src/networkAddress.cpp
index 6863fab..99da84b 100644
--- a/src/networkAddress.cpp
+++ b/src/networkAddress.cpp
@@ -34,6 +34,7 @@
#include <exception>
#include "networkAddress.h"
+#include "anytunError.hpp"
NetworkAddress::NetworkAddress():ipv4_address_(),ipv6_address_()
{
@@ -126,7 +127,7 @@ network_address_type_t NetworkAddress::getNetworkAddressType() const
const boost::asio::ip::address_v4& NetworkAddress::getNetworkAddressV4() const
{
if(network_address_type_ != ipv4)
- throw std::runtime_error("wrong address type");
+ AnytunError::throwErr() << "wrong address type";
return ipv4_address_;
}
@@ -134,7 +135,7 @@ const boost::asio::ip::address_v4& NetworkAddress::getNetworkAddressV4() const
const boost::asio::ip::address_v6& NetworkAddress::getNetworkAddressV6() const
{
if(network_address_type_ != ipv6)
- throw std::runtime_error("wrong address type");
+ AnytunError::throwErr() << "wrong address type";
return ipv6_address_;
}
@@ -142,7 +143,7 @@ const boost::asio::ip::address_v6& NetworkAddress::getNetworkAddressV6() const
const u_int64_t NetworkAddress::getNetworkAdrressEther() const
{
if(network_address_type_ != ethernet)
- throw std::runtime_error("wrong address type");
+ AnytunError::throwErr() << "wrong address type";
return ethernet_address_;
}
@@ -186,7 +187,7 @@ ethernet_bytes_type NetworkAddress::to_bytes_ethernet() const
bool NetworkAddress::operator<(const NetworkAddress &right) const
{
if (network_address_type_!=right.network_address_type_)
- throw std::runtime_error("NetworkAddress::operator<() address types don't match");
+ AnytunError::throwErr() << "NetworkAddress::operator<() address types don't match";
if (network_address_type_==ipv4)
{
return (ipv4_address_ < right.ipv4_address_);
diff --git a/src/plainPacket.cpp b/src/plainPacket.cpp
index 6863c48..f3f9a8d 100644
--- a/src/plainPacket.cpp
+++ b/src/plainPacket.cpp
@@ -34,6 +34,7 @@
#include "datatypes.h"
#include "endian.h"
#include "plainPacket.h"
+#include "anytunError.hpp"
PlainPacket::PlainPacket(u_int32_t payload_length, bool allow_realloc) : Buffer(payload_length + sizeof(payload_type_t), allow_realloc)
{
@@ -103,7 +104,7 @@ void PlainPacket::reinit()
if(length_ < (sizeof(payload_type_t))) {
payload_type_ = NULL;
- throw std::runtime_error("plain packet can't be initialized, buffer is too small");
+ AnytunError::throwErr() << "plain packet can't be initialized, buffer is too small";
}
}
diff --git a/src/routingTable.cpp b/src/routingTable.cpp
index d1a21e9..f20983b 100644
--- a/src/routingTable.cpp
+++ b/src/routingTable.cpp
@@ -31,6 +31,7 @@
#include "networkPrefix.h"
#include "threadUtils.hpp"
#include "datatypes.h"
+#include "anytunError.hpp"
#include "routingTable.h"
#include "routingTree.hpp"
@@ -83,7 +84,7 @@ void RoutingTable::updateRouteTreeUnlocked(const NetworkPrefix & pref)
length=48;
RoutingTree::walk(bytes, node, length, mux);
} else {
- throw std::runtime_error("illegal protocoll type");
+ AnytunError::throwErr() << "illegal protocol type";
}
//root_[type].print(0);
}
@@ -109,7 +110,7 @@ void RoutingTable::addRoute(const NetworkPrefix & pref, u_int16_t mux)
} else if (type==ethernet) {
return; // TODO: add support for ethernet
} else {
- throw std::runtime_error("illegal protocoll type");
+ AnytunError::throwErr() << "illegal protocol type";
}
}
@@ -121,13 +122,13 @@ void RoutingTable::delRoute(const NetworkPrefix & pref )
routes_[pref.getNetworkAddressType()].erase(routes_[pref.getNetworkAddressType()].find(pref));
}
-u_int16_t RoutingTable::getRoute(const NetworkAddress & addr)
+u_int16_t RoutingTable::getRoute(const NetworkAddress & addr)
{
Lock lock(mutex_);
network_address_type_t type=addr.getNetworkAddressType();
if (routes_[type].empty())
- throw std::runtime_error("no route");
+ AnytunError::throwErr() << "no route";
if (type==ipv4)
{
@@ -142,8 +143,9 @@ u_int16_t RoutingTable::getRoute(const NetworkAddress & addr)
ethernet_bytes_type bytes(addr.to_bytes_ethernet());
return RoutingTree::find(bytes, root_[type]);
} else {
- throw std::runtime_error("illegal protocoll type");
+ AnytunError::throwErr() << "illegal protocol type";
}
+ return 0;
}
u_int16_t* RoutingTable::getOrNewRoutingTEUnlocked(const NetworkPrefix & addr)
diff --git a/src/routingTree.hpp b/src/routingTree.hpp
index c75aef5..645b84d 100644
--- a/src/routingTree.hpp
+++ b/src/routingTree.hpp
@@ -32,6 +32,8 @@
#ifndef __ROUTING_TREE_
#define __ROUTING_TREE_
+#include "anytunError.hpp"
+
class RoutingTree {
public:
@@ -91,7 +93,7 @@ public:
}
}
if(!valid)
- throw std::runtime_error("no route");
+ AnytunError::throwErr() << "no route";
return mux;
}
diff --git a/src/signalController.cpp b/src/signalController.cpp
index 4e5f8b6..266ef96 100644
--- a/src/signalController.cpp
+++ b/src/signalController.cpp
@@ -36,6 +36,7 @@
#include "signalController.h"
#include "log.h"
+#include "anytunError.hpp"
#include "threadUtils.hpp"
#ifndef _MSC_VER
@@ -202,11 +203,8 @@ void SignalController::init()
handler[SIGUSR1] = new SigUsr1Handler;
handler[SIGUSR2] = new SigUsr2Handler;
#else
- if(!SetConsoleCtrlHandler((PHANDLER_ROUTINE)SignalController::handle, true)) {
- std::stringstream msg;
- msg << "Error on SetConsoleCtrlhandler: " << LogErrno(GetLastError());
- throw std::runtime_error(msg.str());
- }
+ if(!SetConsoleCtrlHandler((PHANDLER_ROUTINE)SignalController::handle, true))
+ AnytunError::throwErr() << "Error on SetConsoleCtrlhandler: " << LogErrno(GetLastError());
handler[CTRL_C_EVENT] = new CtrlCHandler;
handler[CTRL_BREAK_EVENT] = new CtrlBreakHandler;
diff --git a/src/win32/tunDevice.cpp b/src/win32/tunDevice.cpp
index 9d95b30..9761997 100644
--- a/src/win32/tunDevice.cpp
+++ b/src/win32/tunDevice.cpp
@@ -38,6 +38,7 @@
#include "../tunDevice.h"
#include "../threadUtils.hpp"
#include "../log.h"
+#include "../anytunError.hpp"
#include "registryKey.h"
#include "common.h"
@@ -48,21 +49,18 @@
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)
{
if(conf_.type_ != TYPE_TUN && conf_.type_ != TYPE_TAP)
- throw std::runtime_error("unable to recognize type of device (tun or tap)");
+ AnytunError::throwErr() << "unable to recognize type of device (tun or tap)";
handle_ = INVALID_HANDLE_VALUE;
if(!getAdapter(dev_name))
- throw std::runtime_error("can't find any suitable device");
+ AnytunError::throwErr() << "can't find any suitable device";
if(handle_ == INVALID_HANDLE_VALUE) {
std::stringstream tapname;
tapname << USERMODEDEVICEDIR << actual_node_ << TAPSUFFIX;
handle_ = CreateFileA(tapname.str().c_str(), GENERIC_WRITE | GENERIC_READ, 0, 0, OPEN_EXISTING, FILE_ATTRIBUTE_SYSTEM | FILE_FLAG_OVERLAPPED, 0);
- if(handle_ == INVALID_HANDLE_VALUE) {
- std::stringstream msg;
- msg << "Unable to open device: " << actual_node_ << " (" << actual_name_ << "): " << LogErrno(GetLastError());
- throw std::runtime_error(msg.str());
- }
+ if(handle_ == INVALID_HANDLE_VALUE)
+ AnytunError::throwErr() << "Unable to open device: " << actual_node_ << " (" << actual_name_ << "): " << LogErrno(GetLastError());
}
DWORD err;
@@ -71,16 +69,12 @@ TunDevice::TunDevice(std::string dev_name, std::string dev_type, std::string ifc
err = performIoControl(TAP_IOCTL_GET_VERSION, info, sizeof(info), info, sizeof(info));
if(err != ERROR_SUCCESS) {
CloseHandle(handle_);
- std::stringstream msg;
- msg << "Unable to get device version: " << LogErrno(err);
- throw std::runtime_error(msg.str());
+ AnytunError::throwErr() << "Unable to get device version: " << LogErrno(err);
}
cLog.msg(Log::PRIO_NOTICE) << "Windows TAP Driver Version " << info[0] << "." << info[1] << " " << (info[2] ? "(DEBUG)" : "");
if(!(info[0] > MIN_TAP_VER_MAJOR || (info[0] == MIN_TAP_VER_MAJOR && info[1] >= MIN_TAP_VER_MINOR))) {
CloseHandle(handle_);
- std::stringstream msg;
- msg << "need a higher Version of TAP Driver (at least " << MIN_TAP_VER_MAJOR << "." << MIN_TAP_VER_MINOR << ")";
- throw std::runtime_error(msg.str());
+ AnytunError::throwErr() << "need a higher Version of TAP Driver (at least " << MIN_TAP_VER_MAJOR << "." << MIN_TAP_VER_MINOR << ")";
}
if(conf_.type_ == TYPE_TUN) {
@@ -91,9 +85,7 @@ TunDevice::TunDevice(std::string dev_name, std::string dev_type, std::string ifc
err = performIoControl(TAP_IOCTL_CONFIG_TUN, ep, sizeof(ep), ep, sizeof(ep));
if(err != ERROR_SUCCESS) {
CloseHandle(handle_);
- std::stringstream msg;
- msg << "Unable to set device tun mode: " << LogErrno(err);
- throw std::runtime_error(msg.str());
+ AnytunError::throwErr() << "Unable to set device tun mode: " << LogErrno(err);
}
}
@@ -104,9 +96,7 @@ TunDevice::TunDevice(std::string dev_name, std::string dev_type, std::string ifc
err = performIoControl(TAP_IOCTL_SET_MEDIA_STATUS, &status, sizeof(status), &status, sizeof(status));
if(err != ERROR_SUCCESS) {
CloseHandle(handle_);
- std::stringstream msg;
- msg << "Unable to set device media status: " << LogErrno(err);
- throw std::runtime_error(msg.str());
+ AnytunError::throwErr() << "Unable to set device media status: " << LogErrno(err);
}
roverlapped_.hEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
@@ -117,11 +107,8 @@ bool TunDevice::getAdapter(std::string const& dev_name)
{
RegistryKey akey;
DWORD err = akey.open(HKEY_LOCAL_MACHINE, ADAPTER_KEY, KEY_ENUMERATE_SUB_KEYS);
- if(err != ERROR_SUCCESS) {
- std::stringstream msg;
- msg << "Unable to open registry key (HKLM\\" << ADAPTER_KEY << "): " << LogErrno(err);
- throw std::runtime_error(msg.str());
- }
+ if(err != ERROR_SUCCESS)
+ AnytunError::throwErr() << "Unable to open registry key (HKLM\\" << ADAPTER_KEY << "): " << LogErrno(err);
bool found = false;
for(int i=0; ; ++i) {
@@ -271,18 +258,14 @@ void TunDevice::do_ifconfig()
DWORD err = performIoControl(TAP_IOCTL_CONFIG_DHCP_MASQ, ep, sizeof(ep), ep, sizeof(ep));
if(err != ERROR_SUCCESS) {
CloseHandle(handle_);
- std::stringstream msg;
- msg << "Unable to set device dhcp masq mode: " << LogErrno(err);
- throw std::runtime_error(msg.str());
+ AnytunError::throwErr() << "Unable to set device dhcp masq mode: " << LogErrno(err);
}
u_long mtu;
err = performIoControl(TAP_IOCTL_GET_MTU, &mtu, sizeof(mtu), &mtu, sizeof(mtu));
if(err != ERROR_SUCCESS) {
CloseHandle(handle_);
- std::stringstream msg;
- msg << "Unable to get device mtu: " << LogErrno(err);
- throw std::runtime_error(msg.str());
+ AnytunError::throwErr() << "Unable to get device mtu: " << LogErrno(err);
}
conf_.mtu_ = static_cast<u_int16_t>(mtu);
}
diff --git a/src/win32/winService.cpp b/src/win32/winService.cpp
index fb20f8f..58ad3ca 100644
--- a/src/win32/winService.cpp
+++ b/src/win32/winService.cpp
@@ -37,6 +37,7 @@
#include "winService.h"
#include "../log.h"
+#include "../anytunError.hpp"
#include "../threadUtils.hpp"
WinService* WinService::inst = NULL;
@@ -65,26 +66,18 @@ void WinService::install()
SC_HANDLE schService;
char szPath[MAX_PATH];
- if(!GetModuleFileNameA(NULL, szPath, MAX_PATH)) {
- std::stringstream msg;
- msg << "Error on GetModuleFileName: " << LogErrno(GetLastError());
- throw std::runtime_error(msg.str());
- }
+ if(!GetModuleFileNameA(NULL, szPath, MAX_PATH))
+ AnytunError::throwErr() << "Error on GetModuleFileName: " << LogErrno(GetLastError());
schSCManager = OpenSCManagerA(NULL, NULL, SC_MANAGER_ALL_ACCESS);
- if(NULL == schSCManager) {
- std::stringstream msg;
- msg << "Error on OpenSCManager: " << LogErrno(GetLastError());
- throw std::runtime_error(msg.str());
- }
+ if(NULL == schSCManager)
+ AnytunError::throwErr() << "Error on OpenSCManager: " << LogErrno(GetLastError());
schService = CreateServiceA(schSCManager, SVC_NAME, SVC_NAME, SERVICE_ALL_ACCESS, SERVICE_WIN32_OWN_PROCESS,
SERVICE_DEMAND_START, SERVICE_ERROR_NORMAL, szPath, NULL, NULL, NULL, NULL, NULL);
if(schService == NULL) {
CloseServiceHandle(schSCManager);
- std::stringstream msg;
- msg << "Error on CreateService: " << LogErrno(GetLastError());
- throw std::runtime_error(msg.str());
+ AnytunError::throwErr() << "Error on CreateService: " << LogErrno(GetLastError());
}
std::cout << "Service installed successfully" << std::endl;
@@ -99,26 +92,19 @@ void WinService::uninstall()
SC_HANDLE schService;
schSCManager = OpenSCManagerA(NULL, NULL, SC_MANAGER_ALL_ACCESS);
- if(NULL == schSCManager) {
- std::stringstream msg;
- msg << "Error on OpenSCManager: " << LogErrno(GetLastError());
- throw std::runtime_error(msg.str());
- }
+ if(NULL == schSCManager)
+ AnytunError::throwErr() << "Error on OpenSCManager: " << LogErrno(GetLastError());
schService = OpenServiceA(schSCManager, SVC_NAME, SERVICE_ALL_ACCESS);
if(schService == NULL) {
CloseServiceHandle(schSCManager);
- std::stringstream msg;
- msg << "Error on CreateService: " << LogErrno(GetLastError());
- throw std::runtime_error(msg.str());
+ AnytunError::throwErr() << "Error on CreateService: " << LogErrno(GetLastError());
}
if(!DeleteService(schService)) {
CloseServiceHandle(schService);
CloseServiceHandle(schSCManager);
- std::stringstream msg;
- msg << "Error on DeleteService: " << LogErrno(GetLastError());
- throw std::runtime_error(msg.str());
+ AnytunError::throwErr() << "Error on DeleteService: " << LogErrno(GetLastError());
}
std::cout << "Service uninstalled successfully" << std::endl;
@@ -134,17 +120,14 @@ void WinService::start()
{NULL, NULL}
};
- if(!StartServiceCtrlDispatcherA(DispatchTable)) {
- std::stringstream msg;
- msg << "Error on StartServiceCtrlDispatcher: " << LogErrno(GetLastError());
- throw std::runtime_error(msg.str());
- }
+ if(!StartServiceCtrlDispatcherA(DispatchTable))
+ AnytunError::throwErr() << "Error on StartServiceCtrlDispatcher: " << LogErrno(GetLastError());
}
void WinService::waitForStop()
{
if(!started_)
- throw std::runtime_error("Service not started correctly");
+ AnytunError::throwErr() << "Service not started correctly";
reportStatus(SERVICE_RUNNING, NO_ERROR);
WaitForSingleObject(stop_event_, INFINITE);
@@ -154,7 +137,7 @@ void WinService::waitForStop()
void WinService::stop()
{
if(!started_)
- throw std::runtime_error("Service not started correctly");
+ AnytunError::throwErr() << "Service not started correctly";
reportStatus(SERVICE_STOPPED, NO_ERROR);
}
@@ -225,4 +208,4 @@ void WinService::reportStatus(DWORD dwCurrentState, DWORD dwWin32ExitCode, DWORD
SetServiceStatus(status_handle_, &status_);
}
-#endif \ No newline at end of file
+#endif