// Diese Variante kommt ohne C/C++-Laufzeit aus. Dadurch kann das fertige // Win32-Plugin reproduzierbar mit dem mitgelieferten Zig-Build gebaut werden, // ohne auf dem Testrechner Visual Studio installieren zu müssen. #define DllSpecEuroScope __declspec(dllimport) #define ESINDEX void* #define NULL 0 typedef int BOOL; typedef unsigned long DWORD; typedef unsigned long DWORD_PTR; typedef unsigned long COLORREF; typedef unsigned short INTERNET_PORT; typedef void* HANDLE; typedef void* HMODULE; typedef void* HDC; typedef void* HINTERNET; struct POINT { long x; long y; }; struct RECT { long left; long top; long right; long bottom; }; struct SYSTEMTIME { unsigned short year; unsigned short month; unsigned short dayOfWeek; unsigned short day; unsigned short hour; unsigned short minute; unsigned short second; unsigned short milliseconds; }; struct CRITICAL_SECTION { void* debugInfo; long lockCount; long recursionCount; void* owningThread; void* lockSemaphore; unsigned long spinCount; }; #define WINAPI __stdcall #define APIENTRY WINAPI #define TRUE 1 #define FALSE 0 #define MAX_PATH 260 #define DLL_PROCESS_ATTACH 1 #define CP_UTF8 65001 #define GENERIC_WRITE 0x40000000UL #define FILE_SHARE_READ 0x00000001UL #define OPEN_ALWAYS 4UL #define FILE_ATTRIBUTE_NORMAL 0x00000080UL #define FILE_END 2UL #define WAIT_OBJECT_0 0UL #define WAIT_TIMEOUT 258UL #define INFINITE 0xffffffffUL #define WINHTTP_ACCESS_TYPE_DEFAULT_PROXY 0UL #define WINHTTP_FLAG_SECURE 0x00800000UL #define WINHTTP_QUERY_STATUS_CODE 19UL #define WINHTTP_QUERY_FLAG_NUMBER 0x20000000UL #define INTERNET_DEFAULT_HTTP_PORT 80 #define INTERNET_DEFAULT_HTTPS_PORT 443 extern "C" { __declspec(dllimport) DWORD WINAPI GetModuleFileNameA(HMODULE, char*, DWORD); __declspec(dllimport) BOOL WINAPI DisableThreadLibraryCalls(HMODULE); __declspec(dllimport) DWORD WINAPI GetPrivateProfileStringA( const char*, const char*, const char*, char*, DWORD, const char*); __declspec(dllimport) int WINAPI MultiByteToWideChar( unsigned int, DWORD, const char*, int, wchar_t*, int); __declspec(dllimport) void WINAPI GetSystemTime(SYSTEMTIME*); __declspec(dllimport) HANDLE WINAPI CreateFileA( const char*, DWORD, DWORD, void*, DWORD, DWORD, HANDLE); __declspec(dllimport) DWORD WINAPI SetFilePointer(HANDLE, long, long*, DWORD); __declspec(dllimport) BOOL WINAPI WriteFile( HANDLE, const void*, DWORD, DWORD*, void*); __declspec(dllimport) BOOL WINAPI CloseHandle(HANDLE); __declspec(dllimport) void WINAPI InitializeCriticalSection(CRITICAL_SECTION*); __declspec(dllimport) void WINAPI DeleteCriticalSection(CRITICAL_SECTION*); __declspec(dllimport) void WINAPI EnterCriticalSection(CRITICAL_SECTION*); __declspec(dllimport) void WINAPI LeaveCriticalSection(CRITICAL_SECTION*); __declspec(dllimport) HANDLE WINAPI CreateEventA(void*, BOOL, BOOL, const char*); __declspec(dllimport) BOOL WINAPI SetEvent(HANDLE); __declspec(dllimport) DWORD WINAPI WaitForSingleObject(HANDLE, DWORD); typedef DWORD(WINAPI* ThreadStart)(void*); __declspec(dllimport) HANDLE WINAPI CreateThread( void*, unsigned long, ThreadStart, void*, DWORD, DWORD*); __declspec(dllimport) void WINAPI Sleep(DWORD); __declspec(dllimport) HINTERNET WINAPI WinHttpOpen( const wchar_t*, DWORD, const wchar_t*, const wchar_t*, DWORD); __declspec(dllimport) BOOL WINAPI WinHttpSetTimeouts( HINTERNET, int, int, int, int); __declspec(dllimport) HINTERNET WINAPI WinHttpConnect( HINTERNET, const wchar_t*, INTERNET_PORT, DWORD); __declspec(dllimport) HINTERNET WINAPI WinHttpOpenRequest( HINTERNET, const wchar_t*, const wchar_t*, const wchar_t*, const wchar_t*, const wchar_t* const*, DWORD); __declspec(dllimport) BOOL WINAPI WinHttpSendRequest( HINTERNET, const wchar_t*, DWORD, void*, DWORD, DWORD, DWORD_PTR); __declspec(dllimport) BOOL WINAPI WinHttpReceiveResponse(HINTERNET, void*); __declspec(dllimport) BOOL WINAPI WinHttpQueryHeaders( HINTERNET, DWORD, const wchar_t*, void*, DWORD*, DWORD*); __declspec(dllimport) BOOL WINAPI WinHttpCloseHandle(HINTERNET); void* __cdecl memset(void* target, int value, unsigned int length) { unsigned char* bytes = static_cast(target); for (unsigned int index = 0; index < length; ++index) { bytes[index] = static_cast(value); } return target; } void* __cdecl memcpy(void* target, const void* source, unsigned int length) { unsigned char* destination = static_cast(target); const unsigned char* input = static_cast(source); for (unsigned int index = 0; index < length; ++index) { destination[index] = input[index]; } return target; } int __cdecl memcmp(const void* left, const void* right, unsigned int length) { const unsigned char* a = static_cast(left); const unsigned char* b = static_cast(right); for (unsigned int index = 0; index < length; ++index) { if (a[index] != b[index]) return a[index] < b[index] ? -1 : 1; } return 0; } unsigned int __cdecl strlen(const char* value) { unsigned int length = 0; if (value) { while (value[length]) ++length; } return length; } int __cdecl _purecall() { return 0; } } inline void* operator new(unsigned int, void* address) { return address; } inline void operator delete(void*, void*) {} void operator delete(void*, unsigned int) {} #include "EuroScopePlugIn.h" #pragma comment(lib, "kernel32.lib") #pragma comment(lib, "winhttp.lib") #pragma comment(lib, "EuroScopePlugInDll.lib") namespace { const HANDLE INVALID_HANDLE_VALUE_LOCAL = reinterpret_cast(-1); const unsigned int MAX_ENDPOINT = 2048; const unsigned int MAX_TOKEN = 256; const unsigned int MAX_AIRPORTS = 64; const unsigned int MAX_FIELD = 40; const unsigned int MAX_BODY = 2048; const unsigned int QUEUE_SIZE = 256; HMODULE g_module = NULL; unsigned int textLength(const char* value) { if (!value) return 0; unsigned int length = 0; while (value[length]) ++length; return length; } bool isSpace(char value) { return value == ' ' || value == '\t' || value == '\r' || value == '\n'; } char upperChar(char value) { if (value >= 'a' && value <= 'z') return static_cast(value - 32); return value; } template void copyText(char (&target)[Size], const char* source, bool upper = false) { unsigned int start = 0; const unsigned int sourceLength = textLength(source); while (start < sourceLength && isSpace(source[start])) ++start; unsigned int end = sourceLength; while (end > start && isSpace(source[end - 1])) --end; unsigned int written = 0; while (start < end && written + 1 < Size) { const char next = source[start++]; target[written++] = upper ? upperChar(next) : next; } target[written] = 0; } template void copySlice(char (&target)[Size], const char* source, unsigned int length, bool upper = false) { unsigned int start = 0; while (start < length && isSpace(source[start])) ++start; while (length > start && isSpace(source[length - 1])) --length; unsigned int written = 0; while (start < length && written + 1 < Size) { const char next = source[start++]; target[written++] = upper ? upperChar(next) : next; } target[written] = 0; } bool equals(const char* left, const char* right) { if (!left || !right) return false; unsigned int index = 0; while (left[index] && right[index]) { if (left[index] != right[index]) return false; ++index; } return left[index] == right[index]; } bool startsWith(const char* value, const char* prefix) { if (!value || !prefix) return false; unsigned int index = 0; while (prefix[index]) { if (value[index] != prefix[index]) return false; ++index; } return true; } template bool appendText(char (&target)[Size], unsigned int& used, const char* value) { if (!value) return true; for (unsigned int index = 0; value[index]; ++index) { if (used + 1 >= Size) return false; target[used++] = value[index]; } target[used] = 0; return true; } template bool appendChar(char (&target)[Size], unsigned int& used, char value) { if (used + 1 >= Size) return false; target[used++] = value; target[used] = 0; return true; } template void appendNumber(char (&target)[Size], unsigned int& used, unsigned int value, unsigned int width) { char digits[16]; unsigned int count = 0; do { digits[count++] = static_cast('0' + value % 10); value /= 10; } while (value && count < 16); while (count < width && count < 16) digits[count++] = '0'; while (count > 0) appendChar(target, used, digits[--count]); } void utcNow(char (&target)[32]) { SYSTEMTIME time; memset(&time, 0, sizeof(time)); GetSystemTime(&time); unsigned int used = 0; appendNumber(target, used, time.year, 4); appendChar(target, used, '-'); appendNumber(target, used, time.month, 2); appendChar(target, used, '-'); appendNumber(target, used, time.day, 2); appendChar(target, used, 'T'); appendNumber(target, used, time.hour, 2); appendChar(target, used, ':'); appendNumber(target, used, time.minute, 2); appendChar(target, used, ':'); appendNumber(target, used, time.second, 2); appendChar(target, used, '.'); appendNumber(target, used, time.milliseconds, 3); appendChar(target, used, 'Z'); } template bool moduleSibling(char (&target)[Size], const char* extension) { const DWORD length = GetModuleFileNameA(g_module, target, Size); if (length == 0 || length >= Size) return false; unsigned int dot = length; while (dot > 0 && target[dot - 1] != '.' && target[dot - 1] != '\\') --dot; if (dot > 0 && target[dot - 1] == '.') --dot; unsigned int used = dot; target[used] = 0; return appendText(target, used, extension); } template void readIni(const char* path, const char* key, char (&target)[Size]) { target[0] = 0; GetPrivateProfileStringA("Bridge", key, "", target, Size, path); char trimmed[Size]; copyText(trimmed, target); copyText(target, trimmed); } template bool toWide(const char* source, wchar_t (&target)[Size]) { target[0] = 0; if (!source || !source[0]) return false; const int written = MultiByteToWideChar(CP_UTF8, 0, source, -1, target, Size); return written > 0; } struct BridgeEvent { char action[20]; char callsign[MAX_FIELD]; char destination[8]; char stand[MAX_FIELD]; char observedAt[32]; char controller[MAX_FIELD]; }; struct JsonWriter { char value[MAX_BODY]; unsigned int used; bool valid; JsonWriter() : used(0), valid(true) { value[0] = 0; } void raw(const char* text) { if (valid) valid = appendText(value, used, text); } void string(const char* text) { raw("\""); for (unsigned int index = 0; valid && text && text[index]; ++index) { const unsigned char next = static_cast(text[index]); if (next == '"') raw("\\\""); else if (next == '\\') raw("\\\\"); else if (next == '\b') raw("\\b"); else if (next == '\f') raw("\\f"); else if (next == '\n') raw("\\n"); else if (next == '\r') raw("\\r"); else if (next == '\t') raw("\\t"); else if (next >= 0x20) valid = appendChar(value, used, text[index]); } raw("\""); } }; bool eventBody(const BridgeEvent& event, char (&body)[MAX_BODY]) { JsonWriter out; out.raw("{\"action\":"); out.string(event.action); out.raw(",\"callsign\":"); out.string(event.callsign); out.raw(",\"observedAt\":"); out.string(event.observedAt); out.raw(",\"controller\":"); if (event.controller[0]) out.string(event.controller); else out.raw("null"); if (event.destination[0]) { out.raw(",\"destination\":"); out.string(event.destination); } if (event.stand[0]) { out.raw(",\"stand\":"); out.string(event.stand); } out.raw("}"); copyText(body, out.value); return out.valid; } struct ParsedEndpoint { bool secure; INTERNET_PORT port; char host[256]; char path[1536]; }; bool parsePort(const char* value, INTERNET_PORT& port) { if (!value || !value[0]) return false; unsigned int parsed = 0; for (unsigned int index = 0; value[index]; ++index) { if (value[index] < '0' || value[index] > '9') return false; parsed = parsed * 10 + static_cast(value[index] - '0'); if (parsed > 65535) return false; } if (parsed == 0) return false; port = static_cast(parsed); return true; } bool parseEndpoint(const char* endpoint, ParsedEndpoint& parsed) { memset(&parsed, 0, sizeof(parsed)); const char* authority = NULL; if (startsWith(endpoint, "https://")) { parsed.secure = true; parsed.port = INTERNET_DEFAULT_HTTPS_PORT; authority = endpoint + 8; } else if (startsWith(endpoint, "http://localhost:") || startsWith(endpoint, "http://127.0.0.1:")) { parsed.secure = false; parsed.port = INTERNET_DEFAULT_HTTP_PORT; authority = endpoint + 7; } else { return false; } unsigned int authorityLength = 0; while (authority[authorityLength] && authority[authorityLength] != '/') { ++authorityLength; } if (authorityLength == 0 || authorityLength >= sizeof(parsed.host)) { return false; } copySlice(parsed.host, authority, authorityLength); const char* path = authority + authorityLength; copyText(parsed.path, path[0] ? path : "/"); unsigned int separator = 0; while (parsed.host[separator] && parsed.host[separator] != ':') ++separator; if (parsed.host[separator] == ':') { char portText[8]; copyText(portText, parsed.host + separator + 1); parsed.host[separator] = 0; if (!parsePort(portText, parsed.port)) return false; } return parsed.host[0] && parsed.path[0]; } class VatgerGrpBridge final : public EuroScopePlugIn::CPlugIn { public: VatgerGrpBridge() : CPlugIn(EuroScopePlugIn::COMPATIBILITY_CODE, "VATGER GRP Bridge", "0.1.0", "LH Virtual / VATGER Test", "MIT - test integration for GRPlugin stand events"), stop_(false), configured_(false), queueStart_(0), queueCount_(0), wakeEvent_(NULL), worker_(NULL) { InitializeCriticalSection(&queueLock_); InitializeCriticalSection(&logLock_); moduleSibling(iniPath_, ".ini"); moduleSibling(logPath_, ".log"); readIni(iniPath_, "Endpoint", endpoint_); readIni(iniPath_, "Token", token_); readIni(iniPath_, "Airports", airports_); if (!airports_[0]) copyText(airports_, "EDDF,EDDM", true); else { char normalized[MAX_AIRPORTS]; copyText(normalized, airports_, true); copyText(airports_, normalized); } configured_ = parseEndpoint(endpoint_, parsedEndpoint_) && textLength(token_) >= 32; if (configured_) { wakeEvent_ = CreateEventA(NULL, FALSE, FALSE, NULL); worker_ = wakeEvent_ ? CreateThread(NULL, 0, workerEntry, this, 0, NULL) : NULL; configured_ = worker_ != NULL; } log(configured_ ? "Bridge loaded; event worker started" : "Bridge disabled: Endpoint/Token missing or invalid"); } ~VatgerGrpBridge() override { stop_ = true; if (wakeEvent_) SetEvent(wakeEvent_); if (worker_) WaitForSingleObject(worker_, 30000); log("Bridge unloaded"); if (worker_) CloseHandle(worker_); if (wakeEvent_) CloseHandle(wakeEvent_); DeleteCriticalSection(&logLock_); DeleteCriticalSection(&queueLock_); } void OnFlightPlanControllerAssignedDataUpdate( EuroScopePlugIn::CFlightPlan flightPlan, int dataType) override { if (!configured_ || dataType != EuroScopePlugIn::CTR_DATA_TYPE_SCRATCH_PAD_STRING || !flightPlan.IsValid()) { return; } const char* scratch = flightPlan.GetControllerAssignedData().GetScratchPadString(); const char* callsign = flightPlan.GetCallsign(); if (!scratch || !callsign) return; BridgeEvent event; memset(&event, 0, sizeof(event)); copyText(event.callsign, callsign, true); utcNow(event.observedAt); const EuroScopePlugIn::CController controller = ControllerMyself(); if (controller.IsValid()) { copyText(event.controller, controller.GetCallsign(), true); } if (startsWith(scratch, "GRP/S/")) { copyText(event.destination, flightPlan.GetFlightPlanData().GetDestination(), true); copyText(event.stand, scratch + 6, true); copyText(event.action, event.stand[0] ? "assign" : "cancel"); } else if (startsWith(scratch, "GRP/M/")) { const char* rest = scratch + 6; while (*rest && isSpace(*rest)) ++rest; if (!*rest) { copyText(event.action, "manual_cancel"); } else { unsigned int separator = 0; while (rest[separator] && rest[separator] != '/') ++separator; if (!rest[separator]) return; copySlice(event.destination, rest, separator, true); copyText(event.stand, rest + separator + 1, true); copyText(event.action, "manual_occupy"); } } else { return; } if (!event.callsign[0] || (event.destination[0] && !airportAllowed(event.destination)) || ((equals(event.action, "assign") || equals(event.action, "manual_occupy")) && !event.stand[0]) || ((equals(event.action, "assign") || equals(event.action, "cancel")) && !event.destination[0])) { return; } enqueue(event); } // Clang muss diese im SDK inline definierte Methode bei einer dllimport- // Basisklasse explizit im abgeleiteten VTable sehen. void OnFunctionCall(int, const char*, POINT, RECT) override {} private: bool airportAllowed(const char* airport) const { unsigned int start = 0; const unsigned int length = textLength(airports_); while (start < length) { while (start < length && (airports_[start] == ',' || airports_[start] == ';' || isSpace(airports_[start]))) { ++start; } unsigned int end = start; while (end < length && airports_[end] != ',' && airports_[end] != ';' && !isSpace(airports_[end])) { ++end; } char item[8]; copySlice(item, airports_ + start, end - start, true); if (equals(item, airport)) return true; start = end + 1; } return false; } void enqueue(const BridgeEvent& event) { bool dropped = false; EnterCriticalSection(&queueLock_); if (queueCount_ == QUEUE_SIZE) { queueStart_ = (queueStart_ + 1) % QUEUE_SIZE; --queueCount_; dropped = true; } const unsigned int target = (queueStart_ + queueCount_) % QUEUE_SIZE; queue_[target] = event; ++queueCount_; LeaveCriticalSection(&queueLock_); if (dropped) log("Queue full; oldest event dropped"); logEvent("Captured", event, 0); SetEvent(wakeEvent_); } bool dequeue(BridgeEvent& event) { bool found = false; EnterCriticalSection(&queueLock_); if (queueCount_ > 0) { event = queue_[queueStart_]; queueStart_ = (queueStart_ + 1) % QUEUE_SIZE; --queueCount_; found = true; } LeaveCriticalSection(&queueLock_); return found; } static DWORD WINAPI workerEntry(void* context) { static_cast(context)->run(); return 0; } void run() { while (!stop_) { BridgeEvent event; if (!dequeue(event)) { WaitForSingleObject(wakeEvent_, 1000); continue; } bool sent = false; for (unsigned int attempt = 1; attempt <= 4 && !stop_; ++attempt) { DWORD status = 0; if (post(event, status)) { logEvent("Delivered", event, status); sent = true; break; } logEvent("Delivery failed", event, status); if (attempt < 4 && !stop_) Sleep(1000UL << (attempt - 1)); } if (!sent && !stop_) logEvent("Event abandoned", event, 0); } } bool post(const BridgeEvent& event, DWORD& status) const { char body[MAX_BODY]; if (!eventBody(event, body)) return false; wchar_t host[256]; wchar_t path[1536]; wchar_t token[MAX_TOKEN]; if (!toWide(parsedEndpoint_.host, host) || !toWide(parsedEndpoint_.path, path) || !toWide(token_, token)) { return false; } const HINTERNET session = WinHttpOpen( L"VATGER-GRP-Bridge/0.1", WINHTTP_ACCESS_TYPE_DEFAULT_PROXY, NULL, NULL, 0); if (!session) return false; WinHttpSetTimeouts(session, 5000, 5000, 5000, 10000); const HINTERNET connection = WinHttpConnect(session, host, parsedEndpoint_.port, 0); if (!connection) { WinHttpCloseHandle(session); return false; } const HINTERNET request = WinHttpOpenRequest( connection, L"POST", path, NULL, NULL, NULL, parsedEndpoint_.secure ? WINHTTP_FLAG_SECURE : 0); if (!request) { WinHttpCloseHandle(connection); WinHttpCloseHandle(session); return false; } wchar_t headers[512]; const wchar_t prefix[] = L"Content-Type: application/json\r\nAuthorization: Bearer "; unsigned int used = 0; for (unsigned int index = 0; prefix[index] && used + 1 < 512; ++index) { headers[used++] = prefix[index]; } for (unsigned int index = 0; token[index] && used + 3 < 512; ++index) { headers[used++] = token[index]; } headers[used++] = L'\r'; headers[used++] = L'\n'; headers[used] = 0; const DWORD bodyLength = textLength(body); const BOOL sent = WinHttpSendRequest( request, headers, used, body, bodyLength, bodyLength, 0); const BOOL received = sent && WinHttpReceiveResponse(request, NULL); DWORD statusSize = sizeof(status); const BOOL statusRead = received && WinHttpQueryHeaders( request, WINHTTP_QUERY_STATUS_CODE | WINHTTP_QUERY_FLAG_NUMBER, NULL, &status, &statusSize, NULL); const bool ok = statusRead && status >= 200 && status < 300; WinHttpCloseHandle(request); WinHttpCloseHandle(connection); WinHttpCloseHandle(session); return ok; } void logEvent(const char* prefix, const BridgeEvent& event, DWORD status) const { char message[512]; unsigned int used = 0; appendText(message, used, prefix); appendChar(message, used, ' '); appendText(message, used, event.action); appendChar(message, used, ' '); appendText(message, used, event.callsign); if (event.destination[0]) { appendChar(message, used, ' '); appendText(message, used, event.destination); } if (event.stand[0]) { appendChar(message, used, ' '); appendText(message, used, event.stand); } if (status) { appendText(message, used, " (HTTP "); appendNumber(message, used, status, 0); appendChar(message, used, ')'); } log(message); } void log(const char* message) const { if (!logPath_[0]) return; char line[768]; char timestamp[32]; utcNow(timestamp); unsigned int used = 0; appendText(line, used, timestamp); appendChar(line, used, ' '); appendText(line, used, message); appendText(line, used, "\r\n"); EnterCriticalSection(&logLock_); const HANDLE file = CreateFileA( logPath_, GENERIC_WRITE, FILE_SHARE_READ, NULL, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL); if (file != INVALID_HANDLE_VALUE_LOCAL) { SetFilePointer(file, 0, NULL, FILE_END); DWORD written = 0; WriteFile(file, line, used, &written, NULL); CloseHandle(file); } LeaveCriticalSection(&logLock_); } char iniPath_[MAX_PATH]; char logPath_[MAX_PATH]; char endpoint_[MAX_ENDPOINT]; char token_[MAX_TOKEN]; char airports_[MAX_AIRPORTS]; ParsedEndpoint parsedEndpoint_; volatile bool stop_; bool configured_; unsigned int queueStart_; unsigned int queueCount_; BridgeEvent queue_[QUEUE_SIZE]; HANDLE wakeEvent_; HANDLE worker_; CRITICAL_SECTION queueLock_; mutable CRITICAL_SECTION logLock_; }; VatgerGrpBridge* g_plugin = NULL; } // namespace extern "C" BOOL APIENTRY DllMain(HMODULE module, DWORD reason, void*) { if (reason == DLL_PROCESS_ATTACH) { g_module = module; DisableThreadLibraryCalls(module); } return TRUE; } void __declspec(dllexport) EuroScopePlugInInit( EuroScopePlugIn::CPlugIn** instance) { // EuroScope owns the Lebensdauer zwischen Init und Exit. __declspec(align(8)) static unsigned char storage[sizeof(VatgerGrpBridge)]; g_plugin = new (storage) VatgerGrpBridge(); *instance = g_plugin; } void __declspec(dllexport) EuroScopePlugInExit() { if (g_plugin) { g_plugin->~VatgerGrpBridge(); g_plugin = NULL; } }