diff options
| author | Odd Stranne <odd@mullvad.net> | 2018-04-23 11:49:53 +0200 |
|---|---|---|
| committer | Odd Stranne <odd@mullvad.net> | 2018-06-18 08:45:11 +0200 |
| commit | 4d3ae6c583950aa8677adb37d28e8f12513dbdb1 (patch) | |
| tree | 097ae8a6ea09e00fd2934b856a48a4ff03646e93 /windns/src | |
| parent | 91ecd812773c198c04fe503a1e3933001d3df040 (diff) | |
| download | mullvadvpn-4d3ae6c583950aa8677adb37d28e8f12513dbdb1.tar.xz mullvadvpn-4d3ae6c583950aa8677adb37d28e8f12513dbdb1.zip | |
Add basic WINDNS interface
Diffstat (limited to 'windns/src')
| -rw-r--r-- | windns/src/windns/windns.cpp | 143 | ||||
| -rw-r--r-- | windns/src/windns/windns.h | 78 | ||||
| -rw-r--r-- | windns/src/windns/windnscontext.cpp | 28 | ||||
| -rw-r--r-- | windns/src/windns/windnscontext.h | 27 |
4 files changed, 273 insertions, 3 deletions
diff --git a/windns/src/windns/windns.cpp b/windns/src/windns/windns.cpp new file mode 100644 index 0000000000..47d5d53b1f --- /dev/null +++ b/windns/src/windns/windns.cpp @@ -0,0 +1,143 @@ +#include "stdafx.h" +#include "windns.h" +#include "windnscontext.h" +#include <vector> +#include <string> + +namespace +{ + +WinDnsErrorSink g_ErrorSink = nullptr; +void *g_ErrorContext = nullptr; + +WinDnsContext *g_Context = nullptr; + +std::vector<std::wstring> MakeStringArray(const wchar_t **strings, uint32_t numStrings) +{ + std::vector<std::wstring> v; + + while (numStrings--) + { + v.emplace_back(*strings++); + } + + return v; +} + +} // anonymous namespace + +WINDNS_LINKAGE +bool +WINDNS_API +WinDns_Initialize( + WinDnsErrorSink errorSink, + void *errorContext +) +{ + if (nullptr != g_Context) + { + return false; + } + + g_ErrorSink = errorSink; + g_ErrorContext = errorContext; + + try + { + g_Context = new WinDnsContext; + } + catch (std::exception &err) + { + if (nullptr != g_ErrorSink) + { + g_ErrorSink(err.what(), g_ErrorContext); + } + + return false; + } + catch (...) + { + return false; + } + + return true; +} + +WINDNS_LINKAGE +bool +WINDNS_API +WinDns_Deinitialize( +) +{ + if (nullptr == g_Context) + { + return true; + } + + delete g_Context; + g_Context = nullptr; + + return true; +} + +WINDNS_LINKAGE +bool +WINDNS_API +WinDns_Set( + const wchar_t **servers, + uint32_t numServers +) +{ + if (nullptr == g_Context) + { + return false; + } + + try + { + return g_Context->set(MakeStringArray(servers, numServers), g_ErrorSink, g_ErrorContext); + } + catch (std::exception &err) + { + if (nullptr != g_ErrorSink) + { + g_ErrorSink(err.what(), g_ErrorContext); + } + + return false; + } + catch (...) + { + return false; + } +} + +WINDNS_LINKAGE +bool +WINDNS_API +WinDns_Reset( +) +{ + if (nullptr == g_Context) + { + return false; + } + + try + { + return g_Context->reset(); + } + catch (std::exception &err) + { + if (nullptr != g_ErrorSink) + { + g_ErrorSink(err.what(), g_ErrorContext); + } + + return false; + } + catch (...) + { + return false; + } +} diff --git a/windns/src/windns/windns.h b/windns/src/windns/windns.h index 450685d8a8..000913ad53 100644 --- a/windns/src/windns/windns.h +++ b/windns/src/windns/windns.h @@ -1,8 +1,80 @@ #pragma once +#include <cstdint> -// simple model: +// +// WINDNS public API +// -bool StartEnforcingDns(const wchar_t *primaryServer); +#ifdef WINDNS_EXPORTS +#define WINDNS_LINKAGE __declspec(dllexport) +#else +#define WINDNS_LINKAGE __declspec(dllimport) +#endif -void StopEnforcingDns(); +#define WINDNS_API __stdcall +/////////////////////////////////////////////////////////////////////////////// +// Functions +/////////////////////////////////////////////////////////////////////////////// + +typedef void (WINDNS_API *WinDnsErrorSink)(const char *errorMessage, void *context); + +// +// WinDns_Initialize: +// +// Call this function once at startup, to acquire resources etc. +// +// The OPTIONAL error callback is remembered and used to report exceptions that +// occur as a direct or indirect result of calling into WINDNS. +// +// (Recall that the monitoring provided by WINDNS is threaded.) +// +extern "C" +WINDNS_LINKAGE +bool +WINDNS_API +WinDns_Initialize( + WinDnsErrorSink errorSink, + void *errorContext +); + +// +// WinDns_Deinitialize: +// +// Call this function once before unloading WINDNS or exiting the process. +// +extern "C" +WINDNS_LINKAGE +bool +WINDNS_API +WinDns_Deinitialize( +); + +// +// WinDns_Set: +// +// Configure which DNS servers should be used and start enforcing these settings. +// +extern "C" +WINDNS_LINKAGE +bool +WINDNS_API +WinDns_Set( + const wchar_t **servers, + uint32_t numServers +); + +// +// Windns_Reset: +// +// Revert server settings to what they were before calling WinDns_Set. +// +// (Also taking into account external changes to DNS settings that have occurred +// during the period of enforcing specific settings.) +// +extern "C" +WINDNS_LINKAGE +bool +WINDNS_API +WinDns_Reset( +); diff --git a/windns/src/windns/windnscontext.cpp b/windns/src/windns/windnscontext.cpp new file mode 100644 index 0000000000..404c16f5e1 --- /dev/null +++ b/windns/src/windns/windnscontext.cpp @@ -0,0 +1,28 @@ +#include "stdafx.h" +#include "windnscontext.h" +#include "wmi/connection.h" +#include <process.h> + +WinDnsContext::WinDnsContext() +{ + // Create WMI connection and keep it around? +} + +bool WinDnsContext::set(const std::vector<std::wstring> &servers, WinDnsErrorSink errorSink, void *errorContext) +{ + ThreadArguments args; + + args.errorSink = errorSink; + args.errorContext = errorContext; + +// _beginthreadex(nullptr, 0, ) + + + + return false; +} + +bool WinDnsContext::reset() +{ + return false; +} diff --git a/windns/src/windns/windnscontext.h b/windns/src/windns/windnscontext.h new file mode 100644 index 0000000000..57d00ff530 --- /dev/null +++ b/windns/src/windns/windnscontext.h @@ -0,0 +1,27 @@ +#pragma once + +#include "windns.h" +#include <vector> +#include <string> + +class WinDnsContext +{ +public: + + WinDnsContext(); + + bool set(const std::vector<std::wstring> &servers, WinDnsErrorSink errorSink, void *errorContext); + bool reset(); + +// static unsigned monitoringThread() + +private: + + struct ThreadArguments + { + WinDnsContext *instance; + WinDnsErrorSink errorSink; + void *errorContext; + }; + +}; |
