summaryrefslogtreecommitdiff
path: root/src/win32
diff options
context:
space:
mode:
Diffstat (limited to 'src/win32')
-rw-r--r--src/win32/tunDevice.cpp43
-rw-r--r--src/win32/winService.cpp47
2 files changed, 28 insertions, 62 deletions
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