0
0
mirror of https://github.com/cjdelisle/cjdns synced 2025-10-06 00:32:50 +02:00

Windows: Complete the transition away from setjmp based exceptions

This commit is contained in:
Caleb James DeLisle
2020-07-11 01:22:25 +02:00
parent d41d48e21b
commit 22ca4915f7
12 changed files with 82 additions and 129 deletions

View File

@@ -13,12 +13,12 @@
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#include "exception/WinFail.h"
#include "exception/WinEr.h"
#include <windows.h>
#include <ntstatus.h>
char* WinFail_strerror(long status)
char* WinEr_strerror(long status)
{
switch (status) {
case ERROR_SUCCESS:
@@ -3052,14 +3052,4 @@ char* WinFail_strerror(long status)
default:
return "__UNKNOWN__";
}
}
void WinFail_fail(struct Except* eh, const char* msg, LONG status)
{
Except_throw(eh, "%s [%s]", msg, WinFail_strerror(status));
}
void WinFail_failWithLastError(struct Except* eh, const char* msg)
{
Except_throw(eh, "%s [%s]", msg, WinFail_strerror(GetLastError()));
}
}

View File

@@ -12,15 +12,17 @@
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#ifndef WinFail_H
#define WinFail_H
#ifndef WinEr_H
#define WinEr_H
#include "exception/Er.h"
#include "exception/WinFail.h"
#include "util/Gcc.h"
Linker_require("exception/WinEr.c");
char* WinEr_strerror(long status);
#define WinEr_fail(alloc, msg, status) \
Er_raise(alloc, "%s [%s]", msg, WinFail_strerror(status));
Er_raise(alloc, "%s [%s]", msg, WinEr_strerror(status));
#define WinEr_check(alloc, expr) \
do { \

View File

@@ -1,48 +0,0 @@
/* vim: set expandtab ts=4 sw=4: */
/*
* You may redistribute this program and/or modify it under the terms of
* the GNU General Public License as published by the Free Software Foundation,
* either version 3 of the License, or (at your option) any later version.
*
* This program 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 this program. If not, see <https://www.gnu.org/licenses/>.
*/
#ifndef WinFail_H
#define WinFail_H
#include "exception/Except.h"
#include "util/Gcc.h"
#include "util/Linker.h"
Linker_require("exception/WinFail.c");
Gcc_NORETURN
void WinFail_fail(struct Except* eh, const char* msg, long status);
Gcc_NORETURN
void WinFail_failWithLastError(struct Except* eh, const char* msg);
char* WinFail_strerror(long status);
#define WinFail_check(eh, expr) \
do { \
long status = (expr); \
if (status != ERROR_SUCCESS) { \
WinFail_fail(eh, #expr, status); \
} \
} while (0)
// CHECKFILES_IGNORE expected ;
#define WinFail_assert(eh, expr) \
do { \
if (!(expr)) { \
WinFail_failWithLastError(eh, #expr); \
} \
} while (0)
// CHECKFILES_IGNORE expected ;
#endif

View File

@@ -27,7 +27,7 @@ Er_DEFUN(struct Iface* TUNInterface_new(const char* interfaceName,
struct Log* logger,
struct Allocator* alloc))
{
struct TAPInterface* tap = TAPInterface_new(interfaceName, eh, logger, base, alloc);
struct TAPInterface* tap = Er(TAPInterface_new(interfaceName, logger, base, alloc));
CString_safeStrncpy(assignedInterfaceName, tap->assignedName, TUNInterface_IFNAMSIZ);
if (isTapMode) { Er_ret(&tap->generic); }
struct TAPWrapper* tapWrapper = TAPWrapper_new(&tap->generic, logger, alloc);

View File

@@ -15,8 +15,8 @@
#include <stdbool.h>
#include "util/Bits.h"
#include "exception/Except.h"
#include "exception/WinFail.h"
#include "exception/Er.h"
#include "exception/WinEr.h"
#include "memory/Allocator.h"
#include "interface/tuntap/windows/TAPDevice.h"
#include "util/CString.h"
@@ -175,7 +175,7 @@ static int is_tap_win32_dev(const char *guid)
return FALSE;
}
static struct Taps* get_all_taps(struct Allocator* alloc, struct Except* eh)
static Er_DEFUN(struct Taps* get_all_taps(struct Allocator* alloc))
{
LONG status;
HKEY control_net_key;
@@ -183,7 +183,7 @@ static struct Taps* get_all_taps(struct Allocator* alloc, struct Except* eh)
struct Taps* taps = NULL;
struct Taps* tail = NULL;
WinFail_check(eh, (
WinEr_check(alloc, (
RegOpenKeyEx(HKEY_LOCAL_MACHINE, NETWORK_CONNECTIONS_KEY, 0, KEY_READ, &control_net_key)
));
@@ -205,7 +205,7 @@ static struct Taps* get_all_taps(struct Allocator* alloc, struct Except* eh)
if (status == ERROR_NO_MORE_ITEMS) {
break;
} else if (status != ERROR_SUCCESS) {
WinFail_fail(eh, "RegEnumKeyEx() failed", status);
WinEr_fail(alloc, "RegEnumKeyEx() failed", status);
}
if (len != CString_strlen(NETWORK_ADAPTER_GUID)) {
@@ -219,7 +219,7 @@ static struct Taps* get_all_taps(struct Allocator* alloc, struct Except* eh)
"%s\\%s\\Connection",
NETWORK_CONNECTIONS_KEY, enum_name);
WinFail_check(eh, (
WinEr_check(alloc, (
RegOpenKeyEx(HKEY_LOCAL_MACHINE, connection_string, 0, KEY_READ, &connKey)
));
@@ -235,11 +235,11 @@ static struct Taps* get_all_taps(struct Allocator* alloc, struct Except* eh)
// The interface has no name.
CString_safeStrncpy(name_data, "", sizeof (name_data));
} else if (status != ERROR_SUCCESS) {
WinFail_fail(eh, "RegQueryValueEx() for interface name failed", status);
WinEr_fail(alloc, "RegQueryValueEx() for interface name failed", status);
} else {
if (name_type != REG_SZ) {
// Someone named an interface with a non-string
WinFail_fail(eh, "RegQueryValueEx() name_type != REG_SZ", status);
WinEr_fail(alloc, "RegQueryValueEx() name_type != REG_SZ", status);
}
}
@@ -263,19 +263,18 @@ static struct Taps* get_all_taps(struct Allocator* alloc, struct Except* eh)
RegCloseKey (control_net_key);
return taps;
Er_ret(taps);
}
static int get_device_guid(char *name,
static Er_DEFUN(int get_device_guid(char *name,
int name_size,
char *actual_name,
int actual_name_size,
struct Allocator* alloc,
struct Except* eh)
struct Allocator* alloc))
{
char buff[BUFF_SZ] = {0};
HANDLE handle;
struct Taps* taps = get_all_taps(alloc, eh);
struct Taps* taps = Er(get_all_taps(alloc));
while (taps) {
if (actual_name && CString_strcmp(actual_name, "") != 0) {
if (CString_strcmp(taps->name, actual_name) != 0) {
@@ -299,17 +298,16 @@ static int get_device_guid(char *name,
snprintf(actual_name, actual_name_size, "%s", taps->name);
}
CloseHandle(handle);
return 0;
Er_ret(0);
}
taps = taps->next;
}
return -1;
Er_ret(-1);
}
struct TAPDevice* TAPDevice_find(const char* preferredName,
struct Except* eh,
struct Allocator* alloc)
Er_DEFUN(struct TAPDevice* TAPDevice_find(const char* preferredName,
struct Allocator* alloc))
{
char guid[BUFF_SZ] = {0};
char buff[BUFF_SZ] = {0};
@@ -317,8 +315,9 @@ struct TAPDevice* TAPDevice_find(const char* preferredName,
snprintf(buff, sizeof(buff), "%s", preferredName);
}
if (get_device_guid(guid, sizeof(guid), buff, sizeof(buff), alloc, eh)) {
return NULL;
int ret = Er(get_device_guid(guid, sizeof(guid), buff, sizeof(buff), alloc));
if (ret) {
Er_ret(NULL);
}
struct TAPDevice* out = Allocator_malloc(alloc, sizeof(struct TAPDevice));
@@ -329,5 +328,5 @@ struct TAPDevice* TAPDevice_find(const char* preferredName,
out->path = Allocator_malloc(alloc, CString_strlen(buff)+1);
Bits_memcpy(out->path, buff, CString_strlen(buff)+1);
return out;
Er_ret(out);
}

View File

@@ -15,7 +15,7 @@
#ifndef TAPDevice_H
#define TAPDevice_H
#include "exception/Except.h"
#include "exception/Er.h"
#include "memory/Allocator.h"
#include "util/Linker.h"
Linker_require("interface/tuntap/windows/TAPDevice.c");
@@ -29,8 +29,7 @@ struct TAPDevice
char* path;
};
struct TAPDevice* TAPDevice_find(const char* preferredName,
struct Except* eh,
struct Allocator* alloc);
Er_DEFUN(struct TAPDevice* TAPDevice_find(const char* preferredName,
struct Allocator* alloc));
#endif

View File

@@ -17,8 +17,8 @@
#include "util/events/libuv/UvWrapper.h"
#include "util/events/libuv/EventBase_pvt.h"
#include "exception/Except.h"
#include "exception/WinFail.h"
#include "exception/Er.h"
#include "exception/WinEr.h"
#include "memory/Allocator.h"
#include "interface/tuntap/windows/TAPInterface.h"
#include "interface/tuntap/windows/TAPDevice.h"
@@ -51,7 +51,10 @@ struct TAPInterface_Version_pvt {
unsigned long debug;
};
static void getVersion(HANDLE tap, struct TAPInterface_Version_pvt* version, struct Except* eh)
static Er_DEFUN(void getVersion(
HANDLE tap,
struct TAPInterface_Version_pvt* version,
struct Allocator* alloc))
{
ULONG version_len;
BOOL bret = DeviceIoControl(tap,
@@ -65,16 +68,17 @@ static void getVersion(HANDLE tap, struct TAPInterface_Version_pvt* version, str
if (!bret) {
DWORD err = GetLastError();
CloseHandle(tap);
WinFail_fail(eh, "DeviceIoControl(TAP_IOCTL_GET_VERSION)", err);
WinEr_fail(alloc, "DeviceIoControl(TAP_IOCTL_GET_VERSION)", err);
}
if (version_len != sizeof(struct TAPInterface_Version_pvt)) {
CloseHandle(tap);
Except_throw(eh, "DeviceIoControl(TAP_IOCTL_GET_VERSION) out size [%d] expected [%d]",
Er_raise(alloc, "DeviceIoControl(TAP_IOCTL_GET_VERSION) out size [%d] expected [%d]",
(int)version_len, (int)sizeof(struct TAPInterface_Version_pvt));
}
Er_ret();
}
static void setEnabled(HANDLE tap, int status, struct Except* eh)
static Er_DEFUN(void setEnabled(HANDLE tap, int status, struct Allocator* alloc))
{
unsigned long len = 0;
@@ -84,8 +88,9 @@ static void setEnabled(HANDLE tap, int status, struct Except* eh)
if (!bret) {
DWORD err = GetLastError();
CloseHandle(tap);
WinFail_fail(eh, "DeviceIoControl(TAP_IOCTL_SET_MEDIA_STATUS)", err);
WinEr_fail(alloc, "DeviceIoControl(TAP_IOCTL_SET_MEDIA_STATUS)", err);
}
Er_ret();
}
#define WRITE_MESSAGE_SLOTS 20
@@ -126,7 +131,7 @@ static void postRead(struct TAPInterface_pvt* tap)
switch (GetLastError()) {
case ERROR_IO_PENDING:
case ERROR_IO_INCOMPLETE: break;
default: Assert_failure("ReadFile(tap): %s\n", WinFail_strerror(GetLastError()));
default: Assert_failure("ReadFile(tap): %s\n", WinEr_strerror(GetLastError()));
}
} else {
// It doesn't matter if it returns immediately, it will also return async.
@@ -142,7 +147,7 @@ static void readCallbackB(struct TAPInterface_pvt* tap)
DWORD bytesRead;
OVERLAPPED* readol = (OVERLAPPED*) tap->readIocp.overlapped;
if (!GetOverlappedResult(tap->handle, readol, &bytesRead, FALSE)) {
Assert_failure("GetOverlappedResult(read, tap): %s\n", WinFail_strerror(GetLastError()));
Assert_failure("GetOverlappedResult(read, tap): %s\n", WinEr_strerror(GetLastError()));
}
msg->length = bytesRead;
Log_debug(tap->log, "Read [%d] bytes", msg->length);
@@ -171,7 +176,7 @@ static void postWrite(struct TAPInterface_pvt* tap)
switch (GetLastError()) {
case ERROR_IO_PENDING:
case ERROR_IO_INCOMPLETE: break;
default: Assert_failure("WriteFile(tap): %s\n", WinFail_strerror(GetLastError()));
default: Assert_failure("WriteFile(tap): %s\n", WinEr_strerror(GetLastError()));
}
} else {
// It doesn't matter if it returns immediately, it will also return async.
@@ -185,7 +190,7 @@ static void writeCallbackB(struct TAPInterface_pvt* tap)
DWORD bytesWritten;
OVERLAPPED* writeol = (OVERLAPPED*) tap->writeIocp.overlapped;
if (!GetOverlappedResult(tap->handle, writeol, &bytesWritten, FALSE)) {
Assert_failure("GetOverlappedResult(write, tap): %s\n", WinFail_strerror(GetLastError()));
Assert_failure("GetOverlappedResult(write, tap): %s\n", WinEr_strerror(GetLastError()));
}
Assert_true(tap->isPendingWrite);
@@ -237,17 +242,16 @@ static Iface_DEFUN sendMessage(struct Message* msg, struct Iface* iface)
return 0;
}
struct TAPInterface* TAPInterface_new(const char* preferredName,
struct Except* eh,
Er_DEFUN(struct TAPInterface* TAPInterface_new(const char* preferredName,
struct Log* logger,
struct EventBase* base,
struct Allocator* alloc)
struct Allocator* alloc))
{
Log_debug(logger, "Getting TAP-Windows device name");
struct TAPDevice* dev = TAPDevice_find(preferredName, eh, alloc);
struct TAPDevice* dev = Er(TAPDevice_find(preferredName, alloc));
NetDev_flushAddresses(dev->name, eh);
Er(NetDev_flushAddresses(dev->name, alloc));
Log_debug(logger, "Opening TAP-Windows device [%s] at location [%s]", dev->name, dev->path);
@@ -268,22 +272,22 @@ struct TAPInterface* TAPInterface_new(const char* preferredName,
0);
if (tap->handle == INVALID_HANDLE_VALUE) {
WinFail_fail(eh, "CreateFile(tapDevice)", GetLastError());
WinEr_fail(alloc, "CreateFile(tapDevice)", GetLastError());
}
struct EventBase_pvt* ebp = EventBase_privatize(tap->base);
int ret;
if ((ret = uv_iocp_start(ebp->loop, &tap->readIocp, tap->handle, readCallback))) {
Except_throw(eh, "uv_iocp_start(readIocp): %s", uv_strerror(ret));
Er_raise(alloc, "uv_iocp_start(readIocp): %s", uv_strerror(ret));
}
if ((ret = uv_iocp_start(ebp->loop, &tap->writeIocp, tap->handle, writeCallback))) {
Except_throw(eh, "uv_iocp_start(writeIocp): %s", uv_strerror(ret));
Er_raise(alloc, "uv_iocp_start(writeIocp): %s", uv_strerror(ret));
}
struct TAPInterface_Version_pvt ver = { .major = 0 };
getVersion(tap->handle, &ver, eh);
Er(getVersion(tap->handle, &ver, alloc));
setEnabled(tap->handle, 1, eh);
Er(setEnabled(tap->handle, 1, alloc));
Log_info(logger, "Opened TAP-Windows device [%s] version [%lu.%lu.%lu] at location [%s]",
dev->name, ver.major, ver.minor, ver.debug, dev->path);
@@ -291,5 +295,5 @@ struct TAPInterface* TAPInterface_new(const char* preferredName,
// begin listening.
postRead(tap);
return &tap->pub;
Er_ret(&tap->pub);
}

View File

@@ -16,7 +16,7 @@
#define TAPInterface_H
#include "interface/Iface.h"
#include "exception/Except.h"
#include "exception/Er.h"
#include "memory/Allocator.h"
#include "util/log/Log.h"
#include "util/events/EventBase.h"
@@ -29,10 +29,9 @@ struct TAPInterface
char* assignedName;
};
struct TAPInterface* TAPInterface_new(const char* preferredName,
struct Except* eh,
Er_DEFUN(struct TAPInterface* TAPInterface_new(const char* preferredName,
struct Log* logger,
struct EventBase* base,
struct Allocator* alloc);
struct Allocator* alloc));
#endif

View File

@@ -29,7 +29,7 @@ int main(int argc, char** argv)
int main(int argc, char** argv)
{
struct Allocator* alloc = MallocAllocator_new(1<<20);
struct TAPDevice* dev = TAPDevice_find(NULL, NULL, alloc);
struct TAPDevice* dev = Er_assert(TAPDevice_find(NULL, alloc));
Assert_true(dev && dev->name && dev->path);
printf("name [%s] path [%s]\n", dev->name, dev->path);
return 0;

View File

@@ -39,22 +39,27 @@ Er_DEFUN(void Security_setUser(int uid,
struct Log* logger,
struct Allocator* alloc))
{
Er_ret();
}
Er_DEFUN(void Security_nofiles(struct Allocator* errAlloc))
{
Er_ret();
}
Er_DEFUN(void Security_noforks(struct Allocator* errAlloc))
{
Er_ret();
}
Er_DEFUN(void Security_chroot(char* root, struct Allocator* errAlloc))
{
Er_ret();
}
Er_DEFUN(void Security_seccomp(struct Allocator* tempAlloc, struct Log* logger))
{
Er_ret();
}
struct Security_pvt
@@ -93,5 +98,5 @@ Er_DEFUN(struct Security_Permissions* Security_checkPermissions(struct Allocator
{
struct Security_Permissions* out =
Allocator_calloc(alloc, sizeof(struct Security_Permissions), 1);
return out;
Er_ret(out);
}

View File

@@ -24,7 +24,7 @@ Linker_require("util/platform/netdev/NetPlatform_" + builder.config.systemName +
#include <stdint.h>
void NetPlatform_flushAddresses(const char* deviceName, struct Except* eh);
Er_DEFUN(void NetPlatform_flushAddresses(const char* deviceName, struct Allocator*));
Er_DEFUN(void NetPlatform_addAddress(const char* interfaceName,
const uint8_t* address,

View File

@@ -79,7 +79,7 @@ static LONG flushAddresses(NET_LUID luid, MIB_UNICASTIPADDRESS_TABLE* table)
Er_DEFUN(void NetPlatform_flushAddresses(const char* deviceName, struct Allocator* alloc))
{
NET_LUID luid = getLuid(deviceName, alloc);
NET_LUID luid = Er(getLuid(deviceName, alloc));
MIB_UNICASTIPADDRESS_TABLE* table;
WinEr_check(alloc, GetUnicastIpAddressTable(AF_INET, &table));
@@ -100,16 +100,16 @@ Er_DEFUN(void NetPlatform_flushAddresses(const char* deviceName, struct Allocato
#include "util/Hex.h"
#include <stdio.h>
static void setupRoute(const char* deviceName,
static Er_DEFUN(void setupRoute(const char* deviceName,
const uint8_t* addrBytes,
int prefixLen,
int addrFam,
struct Allocator* alloc)
struct Allocator* alloc))
{
void WINAPI InitializeIpForwardEntry(PMIB_IPFORWARD_ROW2 Row);
MIB_IPFORWARD_ROW2 row = {
.InterfaceLuid = getLuid(deviceName, alloc),
.InterfaceLuid = Er(getLuid(deviceName, alloc)),
.ValidLifetime = WSA_INFINITE,
.PreferredLifetime = WSA_INFINITE,
.Metric = 0xffffffff,
@@ -176,7 +176,7 @@ Er_DEFUN(void NetPlatform_addAddress(const char* interfaceName,
.OnLinkPrefixLength = 0xFF
};
ipRow.InterfaceLuid = getLuid(interfaceName, eh);
ipRow.InterfaceLuid = Er(getLuid(interfaceName, tempAlloc));
ipRow.Address.si_family = addrFam;
if (addrFam == Sockaddr_AF_INET6) {
@@ -189,8 +189,11 @@ Er_DEFUN(void NetPlatform_addAddress(const char* interfaceName,
ipRow.OnLinkPrefixLength = prefixLen;
WinEr_check(CreateUnicastIpAddressEntry(&ipRow));
//setupRoute(interfaceName, address, prefixLen, addrFam, eh);
WinEr_check(tempAlloc, CreateUnicastIpAddressEntry(&ipRow));
if (0) {
// setupRoute was disabled in d7f5b302ac9215221428de81b9a496b40dcb3884
Er(setupRoute(interfaceName, address, prefixLen, addrFam, tempAlloc));
}
Er_ret();
}
@@ -226,14 +229,14 @@ Er_DEFUN(void NetPlatform_setMTU(const char* interfaceName,
Log_debug(logger, "Going to run command: %s", buffer);
// Make the netsh call, and die if it returns the wrong thing.
WinEr_check(eh, system(buffer));
WinEr_check(errAlloc, system(buffer));
// We should also configure the MTU for ipv4 (IpTunnel) case
const char* format1 = ("netsh interface ipv4 set subinterface "
"\"%s\" mtu=%d");
snprintf(buffer, totalSize, format1, interfaceName, mtu);
Log_debug(logger, "Going to run command: %s", buffer);
WinEr_check(eh, system(buffer));
WinEr_check(errAlloc, system(buffer));
Er_ret();
}