Files

276 lines
8.6 KiB
C++

#ifndef WIN32_LEAN_AND_MEAN
#define WIN32_LEAN_AND_MEAN
#endif
#include <windows.h>
#include <winnetwk.h>
#include <wnnc.h>
#include <stdio.h>
#include "../include/retro_ioctl.h"
static const char* kProviderName = "RetroNFS95";
static const char* kDeviceName = "\\\\.\\RETROVXD";
static const char* kConfigDir = "\\RETRO95";
static const char* kConfigFile = "\\RETRO95\\mappings.ini";
#ifndef WNNC_SPEC_VERSION
#define WNNC_SPEC_VERSION 0x00000001
#endif
#ifndef WNNC_SPEC_VERSION51
#define WNNC_SPEC_VERSION51 0x00050001
#endif
#ifndef WNNC_NET_TYPE
#define WNNC_NET_TYPE 0x00000002
#endif
#ifndef WNNC_CONNECTION
#define WNNC_CONNECTION 0x00000006
#endif
#ifndef WNNC_CON_ADDCONNECTION
#define WNNC_CON_ADDCONNECTION 0x00000001
#endif
#ifndef WNNC_CON_CANCELCONNECTION
#define WNNC_CON_CANCELCONNECTION 0x00000002
#endif
#ifndef WNNC_CON_GETCONNECTIONS
#define WNNC_CON_GETCONNECTIONS 0x00000004
#endif
#ifndef WNNC_CON_ADDCONNECTION3
#define WNNC_CON_ADDCONNECTION3 0x00000008
#endif
#ifndef WNNC_ENUMERATION
#define WNNC_ENUMERATION 0x0000000B
#endif
#ifndef WNNC_ENUM_LOCAL
#define WNNC_ENUM_LOCAL 0x00000002
#endif
static int prefix_i(const char* value, const char* prefix) {
if (!value || !prefix) return 0;
while (*prefix) {
char a = *value++;
char b = *prefix++;
if (a >= 'a' && a <= 'z') a = (char)(a - 'a' + 'A');
if (b >= 'a' && b <= 'z') b = (char)(b - 'a' + 'A');
if (a != b) return 0;
}
return 1;
}
static void get_windows_path(char* out, DWORD out_len, const char* suffix) {
UINT n = GetWindowsDirectoryA(out, out_len);
if (n == 0 || n >= out_len) {
lstrcpynA(out, "C:\\WINDOWS", out_len);
}
lstrcatA(out, suffix);
}
static BOOL ensure_config_dir() {
char path[MAX_PATH];
get_windows_path(path, sizeof(path), kConfigDir);
CreateDirectoryA(path, NULL);
return TRUE;
}
static DWORD save_mapping(const char* local, const char* remote, const char* user) {
ensure_config_dir();
char path[MAX_PATH];
get_windows_path(path, sizeof(path), kConfigFile);
char section[32];
wsprintfA(section, "Drive.%c", local && local[0] ? local[0] : '?');
WritePrivateProfileStringA(section, "Local", local ? local : "", path);
WritePrivateProfileStringA(section, "Remote", remote ? remote : "", path);
WritePrivateProfileStringA(section, "User", user ? user : "", path);
WritePrivateProfileStringA(section, "Provider", kProviderName, path);
return WN_SUCCESS;
}
static DWORD call_vxd_map(const char* local, const char* remote) {
HANDLE h = CreateFileA(kDeviceName, 0, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
if (h == INVALID_HANDLE_VALUE) {
return WN_NOT_SUPPORTED;
}
RETRO_MAP_REQUEST request;
RETRO_MAPPING_INFO result;
ZeroMemory(&request, sizeof(request));
ZeroMemory(&result, sizeof(result));
lstrcpynA(request.local, local ? local : "", sizeof(request.local));
lstrcpynA(request.remote, remote ? remote : "", sizeof(request.remote));
DWORD returned = 0;
BOOL ok = DeviceIoControl(h,
RETRO_IOCTL_MAP,
&request,
sizeof(request),
&result,
sizeof(result),
&returned,
NULL);
CloseHandle(h);
return ok ? WN_SUCCESS : WN_NOT_SUPPORTED;
}
static DWORD call_vxd_unmap(const char* local) {
HANDLE h = CreateFileA(kDeviceName, 0, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
if (h == INVALID_HANDLE_VALUE) {
return WN_NOT_SUPPORTED;
}
RETRO_QUERY_REQUEST request;
ZeroMemory(&request, sizeof(request));
lstrcpynA(request.local, local ? local : "", sizeof(request.local));
DWORD returned = 0;
BOOL ok = DeviceIoControl(h,
RETRO_IOCTL_UNMAP,
&request,
sizeof(request),
NULL,
0,
&returned,
NULL);
CloseHandle(h);
return ok ? WN_SUCCESS : WN_NOT_SUPPORTED;
}
extern "C" DWORD APIENTRY NPGetCaps(DWORD index) {
switch (index) {
case WNNC_SPEC_VERSION:
return WNNC_SPEC_VERSION51;
case WNNC_NET_TYPE:
return WNNC_NET_SUN_PC_NFS;
case WNNC_CONNECTION:
return WNNC_CON_ADDCONNECTION |
WNNC_CON_ADDCONNECTION3 |
WNNC_CON_CANCELCONNECTION |
WNNC_CON_GETCONNECTIONS;
case WNNC_ENUMERATION:
return WNNC_ENUM_LOCAL;
default:
return 0;
}
}
extern "C" DWORD APIENTRY NPAddConnection3(HWND hwndOwner,
LPNETRESOURCEA lpNetResource,
LPSTR lpPassword,
LPSTR lpUserName,
DWORD dwFlags) {
(void)hwndOwner;
(void)lpPassword;
(void)dwFlags;
if (!lpNetResource || !lpNetResource->lpRemoteName || !lpNetResource->lpLocalName) {
return WN_BAD_VALUE;
}
if (!prefix_i(lpNetResource->lpRemoteName, "\\\\RetroNFS\\")) {
return WN_BAD_NETNAME;
}
save_mapping(lpNetResource->lpLocalName, lpNetResource->lpRemoteName, lpUserName);
DWORD vx = call_vxd_map(lpNetResource->lpLocalName, lpNetResource->lpRemoteName);
if (vx == WN_SUCCESS) {
return WN_SUCCESS;
}
// Keep the mapping persistent even before the VxD is complete. This makes
// the UI/configuration layer usable while redirector work continues.
return WN_SUCCESS;
}
extern "C" DWORD APIENTRY NPAddConnection(LPNETRESOURCEA lpNetResource,
LPSTR lpPassword,
LPSTR lpUserName) {
return NPAddConnection3(NULL, lpNetResource, lpPassword, lpUserName, 0);
}
extern "C" DWORD APIENTRY NPCancelConnection(LPSTR lpName, BOOL fForce) {
(void)fForce;
if (!lpName || !lpName[0]) return WN_BAD_VALUE;
call_vxd_unmap(lpName);
char path[MAX_PATH];
get_windows_path(path, sizeof(path), kConfigFile);
char section[32];
wsprintfA(section, "Drive.%c", lpName[0]);
WritePrivateProfileStringA(section, NULL, NULL, path);
return WN_SUCCESS;
}
extern "C" DWORD APIENTRY NPGetConnection(LPSTR lpLocalName,
LPSTR lpRemoteName,
LPDWORD lpnBufferLen) {
if (!lpLocalName || !lpRemoteName || !lpnBufferLen) return WN_BAD_VALUE;
char path[MAX_PATH];
get_windows_path(path, sizeof(path), kConfigFile);
char section[32];
wsprintfA(section, "Drive.%c", lpLocalName[0]);
char remote[MAX_PATH];
GetPrivateProfileStringA(section, "Remote", "", remote, sizeof(remote), path);
if (!remote[0]) return WN_NOT_CONNECTED;
DWORD need = lstrlenA(remote) + 1;
if (*lpnBufferLen < need) {
*lpnBufferLen = need;
return WN_MORE_DATA;
}
lstrcpyA(lpRemoteName, remote);
*lpnBufferLen = need;
return WN_SUCCESS;
}
extern "C" DWORD APIENTRY NPGetResourceInformation(LPNETRESOURCEA lpNetResource,
LPVOID lpBuffer,
LPDWORD lpcbBuffer,
LPSTR* lplpSystem) {
(void)lpBuffer;
(void)lpcbBuffer;
(void)lplpSystem;
if (!lpNetResource || !lpNetResource->lpRemoteName) return WN_BAD_VALUE;
if (!prefix_i(lpNetResource->lpRemoteName, "\\\\RetroNFS\\")) {
return WN_BAD_NETNAME;
}
return WN_SUCCESS;
}
extern "C" DWORD APIENTRY NPOpenEnum(DWORD dwScope,
DWORD dwType,
DWORD dwUsage,
LPNETRESOURCEA lpNetResource,
LPHANDLE lphEnum) {
(void)dwScope; (void)dwType; (void)dwUsage; (void)lpNetResource;
if (!lphEnum) return WN_BAD_VALUE;
*lphEnum = (HANDLE)1;
return WN_SUCCESS;
}
extern "C" DWORD APIENTRY NPEnumResource(HANDLE hEnum,
LPDWORD lpcCount,
LPVOID lpBuffer,
LPDWORD lpBufferSize) {
(void)hEnum; (void)lpBuffer; (void)lpBufferSize;
if (lpcCount) *lpcCount = 0;
return WN_NO_MORE_ENTRIES;
}
extern "C" DWORD APIENTRY NPCloseEnum(HANDLE hEnum) {
(void)hEnum;
return WN_SUCCESS;
}
BOOL WINAPI DllMain(HINSTANCE hinst, DWORD reason, LPVOID reserved) {
(void)hinst; (void)reason; (void)reserved;
return TRUE;
}