From febb518a0e2401299970ad031842db3f90ab112d Mon Sep 17 00:00:00 2001 From: Christian Pointner Date: Wed, 21 Jan 2009 13:22:52 +0000 Subject: added minimalisstic registry wrapper --- src/anytun.suo | Bin 57344 -> 59392 bytes src/anytun.vcproj | 8 +++ src/win32/registryKey.cpp | 121 ++++++++++++++++++++++++++++++++++++++++++++++ src/win32/registryKey.h | 57 ++++++++++++++++++++++ src/win32/tunDevice.cpp | 60 +++++++++-------------- 5 files changed, 209 insertions(+), 37 deletions(-) create mode 100644 src/win32/registryKey.cpp create mode 100644 src/win32/registryKey.h diff --git a/src/anytun.suo b/src/anytun.suo index 8396fda..be5c60a 100644 Binary files a/src/anytun.suo and b/src/anytun.suo differ diff --git a/src/anytun.vcproj b/src/anytun.vcproj index 1f4d47d..b576bd5 100644 --- a/src/anytun.vcproj +++ b/src/anytun.vcproj @@ -258,6 +258,10 @@ RelativePath=".\plainPacket.h" > + + @@ -402,6 +406,10 @@ RelativePath=".\plainPacket.cpp" > + + diff --git a/src/win32/registryKey.cpp b/src/win32/registryKey.cpp new file mode 100644 index 0000000..f0ab73d --- /dev/null +++ b/src/win32/registryKey.cpp @@ -0,0 +1,121 @@ +/* + * anytun + * + * The secure anycast tunneling protocol (satp) defines a protocol used + * for communication between any combination of unicast and anycast + * tunnel endpoints. It has less protocol overhead than IPSec in Tunnel + * mode and allows tunneling of every ETHER TYPE protocol (e.g. + * ethernet, ip, arp ...). satp directly includes cryptography and + * message authentication based on the methodes used by SRTP. It is + * intended to deliver a generic, scaleable and secure solution for + * tunneling and relaying of packets of any protocol. + * + * + * Copyright (C) 2007-2008 Othmar Gsenger, Erwin Nindl, + * Christian Pointner + * + * 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. + * + * Anytun is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with anytun. If not, see . + */ + +#include +#include +#include + +#include "registryKey.h" + +#include "../log.h" + +RegistryKey::RegistryKey() : opened_(false) +{ +} + +RegistryKey::RegistryKey(HKEY hkey, std::string subKey, REGSAM samDesired) : opened_(false) +{ + open(hkey, subKey, samDesired); +} + +RegistryKey::~RegistryKey() +{ + close(); +} + +bool RegistryKey::isOpen() const +{ + return opened_; +} + +std::string RegistryKey::getName() const +{ + return name_; +} + +DWORD RegistryKey::open(HKEY hkey, std::string subKey, REGSAM samDesired) +{ + if(opened_) + RegCloseKey(key_); + + opened_ = false; + name_ = ""; + LONG err = RegOpenKeyExA(hkey, subKey.c_str(), 0, samDesired, &key_); + if(err != ERROR_SUCCESS) + return err; + + name_ = subKey; + opened_ = true; + return ERROR_SUCCESS; +} + +void RegistryKey::close() +{ + if(opened_) + RegCloseKey(key_); + opened_ = false; +} + +std::string RegistryKey::operator[](std::string const& name) const +{ + if(!opened_) + throw LogErrno(ERROR_INVALID_HANDLE); + + char value[STRING_VALUE_LENGTH]; + DWORD len = sizeof(value); + LONG err = RegQueryValueExA(key_, name.c_str(), NULL, NULL, (LPBYTE)value, &len); + if(err != ERROR_SUCCESS) + throw LogErrno(err); + + if(value[len-1] != 0) { + if(len < sizeof(value)) + value[len++] = 0; + else + throw LogErrno(ERROR_INSUFFICIENT_BUFFER); + } + return std::string(value); +} + +DWORD RegistryKey::getSubKey(DWORD index, RegistryKey& subKey, REGSAM sam) const +{ + char subkeyname[NAME_LENGTH]; + DWORD len = sizeof(subkeyname); + DWORD err = RegEnumKeyEx(key_, index, subkeyname, &len, NULL, NULL, NULL, NULL); + if(err != ERROR_SUCCESS) + return err; + + return subKey.open(key_, subkeyname, sam); +} + +DWORD RegistryKey::getSubKey(std::string name, RegistryKey& subKey, REGSAM sam) const +{ + return subKey.open(key_, name.c_str(), sam); +} diff --git a/src/win32/registryKey.h b/src/win32/registryKey.h new file mode 100644 index 0000000..ce79d47 --- /dev/null +++ b/src/win32/registryKey.h @@ -0,0 +1,57 @@ +/* + * anytun + * + * The secure anycast tunneling protocol (satp) defines a protocol used + * for communication between any combination of unicast and anycast + * tunnel endpoints. It has less protocol overhead than IPSec in Tunnel + * mode and allows tunneling of every ETHER TYPE protocol (e.g. + * ethernet, ip, arp ...). satp directly includes cryptography and + * message authentication based on the methodes used by SRTP. It is + * intended to deliver a generic, scaleable and secure solution for + * tunneling and relaying of packets of any protocol. + * + * + * Copyright (C) 2007-2008 Othmar Gsenger, Erwin Nindl, + * Christian Pointner + * + * 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. + * + * Anytun is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with anytun. If not, see . + */ + +#include +#include + +class RegistryKey +{ +public: + #define NAME_LENGTH 256 + #define STRING_VALUE_LENGTH 256 + + RegistryKey(); + RegistryKey(HKEY hkey, std::string subKey, REGSAM samDesired); + ~RegistryKey(); + + bool isOpen() const; + std::string getName() const; + DWORD open(HKEY hkey, std::string subKey, REGSAM samDesired); + void close(); + DWORD getSubKey(DWORD index, RegistryKey& subKey, REGSAM sam) const; + DWORD getSubKey(std::string name, RegistryKey& subKey, REGSAM sam) const; + std::string operator[](std::string const& name) const; + +private: + HKEY key_; + bool opened_; + std::string name_; +}; \ No newline at end of file diff --git a/src/win32/tunDevice.cpp b/src/win32/tunDevice.cpp index e531f2e..811d193 100644 --- a/src/win32/tunDevice.cpp +++ b/src/win32/tunDevice.cpp @@ -39,11 +39,9 @@ #include "../threadUtils.hpp" #include "../log.h" +#include "registryKey.h" #include "common.h" -#define REG_KEY_LENGTH 256 -#define REG_NAME_LENGTH 256 - TunDevice::TunDevice(std::string dev_name, std::string dev_type, std::string ifcfg_lp, std::string ifcfg_rnmp) : conf_(dev_name, dev_type, ifcfg_lp, ifcfg_rnmp, 1400) { if(conf_.type_ != TYPE_TUN && conf_.type_ != TYPE_TAP) @@ -74,7 +72,6 @@ TunDevice::TunDevice(std::string dev_name, std::string dev_type, std::string ifc CloseHandle(handle_); std::stringstream msg; msg << "Unable to set device point-to-point mode: " << LogErrno(err); - throw std::runtime_error(msg.str()); } } @@ -96,8 +93,8 @@ TunDevice::TunDevice(std::string dev_name, std::string dev_type, std::string ifc bool TunDevice::getAdapter(std::string const& dev_name) { - HKEY key, key2; - LONG err = RegOpenKeyEx(HKEY_LOCAL_MACHINE, NETWORK_CONNECTIONS_KEY, 0, KEY_ENUMERATE_SUB_KEYS, &key); + RegistryKey key; + DWORD err = key.open(HKEY_LOCAL_MACHINE, NETWORK_CONNECTIONS_KEY, KEY_ENUMERATE_SUB_KEYS); if(err != ERROR_SUCCESS) { std::stringstream msg; msg << "Unable to open registry key: " << LogErrno(err); @@ -105,47 +102,33 @@ bool TunDevice::getAdapter(std::string const& dev_name) } bool found = false; - DWORD len; - char adapterid[REG_KEY_LENGTH]; - char adaptername[REG_NAME_LENGTH]; for(int i=0; ; ++i) { - len = sizeof(adapterid); - err = RegEnumKeyEx(key, i, adapterid, &len, NULL, NULL, NULL, NULL); + RegistryKey key2; + DWORD err = key.getSubKey(i, key2, KEY_QUERY_VALUE); if(err == ERROR_NO_MORE_ITEMS) break; if(err != ERROR_SUCCESS) { - RegCloseKey(key); std::stringstream msg; msg << "Unable to read registry: " << LogErrno(err); throw std::runtime_error(msg.str()); } + actual_node_ = key2.getName(); + RegistryKey key3; + key2.getSubKey("Connection", key3, KEY_QUERY_VALUE); - std::stringstream regpath; - regpath << NETWORK_CONNECTIONS_KEY << "\\" << adapterid << "\\Connection"; - err = RegOpenKeyEx(HKEY_LOCAL_MACHINE, regpath.str().c_str(), 0, KEY_QUERY_VALUE, &key2); - if(err != ERROR_SUCCESS) - continue; + try { + actual_name_ = key3["Name"]; + } catch(LogErrno& e) { continue; } - len = sizeof(adaptername); - err = RegQueryValueEx(key2, "Name", NULL, NULL, (LPBYTE)adaptername, &len); - RegCloseKey(key2); - if(err != ERROR_SUCCESS) - continue; - if(adaptername[len-1] != 0) { - if(len < sizeof(adaptername)) - adaptername[len++] = 0; - else - continue; - } if(dev_name != "") { - if(!dev_name.compare(0, len-1, adaptername)) { + if(dev_name == actual_name_) { found = true; break; } } else { std::stringstream tapname; - tapname << USERMODEDEVICEDIR << adapterid << TAPSUFFIX; + tapname << USERMODEDEVICEDIR << actual_node_ << TAPSUFFIX; handle_ = CreateFile(tapname.str().c_str(), GENERIC_WRITE | GENERIC_READ, 0, 0, OPEN_EXISTING, FILE_ATTRIBUTE_SYSTEM | FILE_FLAG_OVERLAPPED, 0); if(handle_ == INVALID_HANDLE_VALUE) continue; @@ -153,10 +136,10 @@ bool TunDevice::getAdapter(std::string const& dev_name) break; } } - RegCloseKey(key); - - actual_node_ = adapterid; - actual_name_ = adaptername; + if(!found) { + actual_node_ = ""; + actual_name_ = ""; + } return found; } @@ -184,9 +167,12 @@ DWORD TunDevice::performIoControl(DWORD controlCode, LPVOID inBuffer, DWORD inBu TunDevice::~TunDevice() { - CloseHandle(handle_); - CloseHandle(roverlapped_.hEvent); - CloseHandle(woverlapped_.hEvent); + if(handle_ != INVALID_HANDLE_VALUE) + CloseHandle(handle_); + if(roverlapped_.hEvent != INVALID_HANDLE_VALUE) + CloseHandle(roverlapped_.hEvent); + if(woverlapped_.hEvent != INVALID_HANDLE_VALUE) + CloseHandle(woverlapped_.hEvent); } int TunDevice::fix_return(int ret, size_t pi_length) -- cgit v1.2.3