From bbfbcb79cd3e713d70de5eb521c5e8cc31302090 Mon Sep 17 00:00:00 2001 From: Daniel Collins Date: Sun, 28 Aug 2011 21:27:06 +0000 Subject: [PATCH] Lots of code cleanup. --- Makefile | 10 ++--- src/common.c | 106 +++++++++++++++++++++++++++++++++++++++++++++++ src/common.h | 37 +++++++++++++++++ src/directplay.c | 75 +++++++++++++-------------------- src/ipxwrapper.c | 65 ++++------------------------- src/ipxwrapper.h | 2 - src/stubdll.c | 24 ++++------- src/winsock.c | 1 + 8 files changed, 195 insertions(+), 125 deletions(-) create mode 100644 src/common.c create mode 100644 src/common.h diff --git a/Makefile b/Makefile index 1b728ca..71ab25e 100644 --- a/Makefile +++ b/Makefile @@ -17,7 +17,7 @@ CFLAGS := -Wall -I./include/ CXXFLAGS := -Wall -I./include/ -IPXWRAPPER_DEPS := src/ipxwrapper.o src/winsock.o src/ipxwrapper_stubs.o src/log.o src/ipxwrapper.def +IPXWRAPPER_DEPS := src/ipxwrapper.o src/winsock.o src/ipxwrapper_stubs.o src/log.o src/common.o src/ipxwrapper.def BIN_FILES := changes.txt license.txt readme.txt ipxwrapper.dll mswsock.dll wsock32.dll ipxconfig.exe SRC_FILES := changes.txt license.txt Makefile mkstubs.pl readme.txt src/config.h src/ipxconfig.cpp \ @@ -51,8 +51,8 @@ ipxwrapper.dll: $(IPXWRAPPER_DEPS) ipxconfig.exe: src/ipxconfig.cpp $(CXX) $(CXXFLAGS) -static-libgcc -static-libstdc++ -Wl,-s -D_WIN32_IE=0x0400 -mwindows -o ipxconfig.exe src/ipxconfig.cpp -liphlpapi -dpwsockx.dll: src/directplay.o src/log.o src/dpwsockx_stubs.o ipxwrapper.dll - $(CC) $(CFLAGS) -Wl,--enable-stdcall-fixup,-s -shared -o dpwsockx.dll src/directplay.o src/log.o src/dpwsockx_stubs.o src/dpwsockx.def -L. -lipxwrapper -lwsock32 +dpwsockx.dll: src/directplay.o src/log.o src/dpwsockx_stubs.o src/common.o ipxwrapper.dll + $(CC) $(CFLAGS) -Wl,--enable-stdcall-fixup,-s -shared -o dpwsockx.dll src/directplay.o src/log.o src/common.o src/dpwsockx_stubs.o src/dpwsockx.def -L. -lipxwrapper -lwsock32 src/ipxwrapper_stubs.s: src/ipxwrapper_stubs.txt perl mkstubs.pl src/ipxwrapper_stubs.txt src/ipxwrapper_stubs.s @@ -66,11 +66,11 @@ src/mswsock_stubs.s: src/mswsock_stubs.txt src/dpwsockx_stubs.s: src/dpwsockx_stubs.txt perl mkstubs.pl src/dpwsockx_stubs.txt src/dpwsockx_stubs.s dpwsockx.dll -%.dll: src/stubdll.o src/%_stubs.o src/log.o src/%.def +%.dll: src/stubdll.o src/%_stubs.o src/log.o src/common.o src/%.def $(CC) $(CFLAGS) -Wl,--enable-stdcall-fixup,-s -shared -o $@ $^ src/%_stubs.o: src/%_stubs.s nasm -f win32 -o $@ $< -src/%.o: src/%.c src/ipxwrapper.h src/config.h +src/%.o: src/%.c src/ipxwrapper.h src/config.h src/common.h $(CC) $(CFLAGS) -c -o $@ $< diff --git a/src/common.c b/src/common.c new file mode 100644 index 0000000..c211f30 --- /dev/null +++ b/src/common.c @@ -0,0 +1,106 @@ +/* IPXWrapper - Common functions + * Copyright (C) 2011 Daniel Collins + * + * This program is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 2 as published by + * the Free Software Foundation. + * + * 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, write to the Free Software Foundation, Inc., 51 + * Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. +*/ + +#include + +#include "common.h" + +HKEY regkey = NULL; + +/* Convert a windows error number to an error message */ +const char *w32_error(DWORD errnum) { + static char buf[1024] = {'\0'}; + + FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM, NULL, errnum, 0, buf, 1023, NULL); + buf[strcspn(buf, "\r\n")] = '\0'; + return buf; +} + +BOOL reg_open(REGSAM access) { + int err = RegOpenKeyEx(HKEY_CURRENT_USER, "Software\\IPXWrapper", 0, access, ®key); + + if(err != ERROR_SUCCESS) { + log_printf("Could not open registry: %s", w32_error(err)); + regkey = NULL; + + return FALSE; + } + + return TRUE; +} + +void reg_close(void) { + if(regkey) { + RegCloseKey(regkey); + regkey = NULL; + } +} + +char reg_get_char(const char *val_name, char default_val) { + if(!regkey) { + return default_val; + } + + char buf; + DWORD size = 1; + + int err = RegQueryValueEx(regkey, val_name, NULL, NULL, (BYTE*)&buf, &size); + + if(err != ERROR_SUCCESS) { + log_printf("Error reading registry value: %s", w32_error(err)); + return default_val; + } + + return size == 1 ? buf : default_val; +} + +DWORD reg_get_bin(const char *val_name, void *buf, DWORD size) { + if(!regkey) { + return 0; + } + + int err = RegQueryValueEx(regkey, val_name, NULL, NULL, (BYTE*)buf, &size); + + if(err != ERROR_SUCCESS) { + log_printf("Error reading registry value: %s", w32_error(err)); + return 0; + } + + return size; +} + +/* Load a system DLL */ +HMODULE load_sysdll(const char *name) { + char path[1024]; + + GetSystemDirectory(path, sizeof(path)); + + if(strlen(path) + strlen(name) + 2 > sizeof(path)) { + log_printf("Path buffer too small, cannot load %s", name); + return NULL; + } + + strcat(path, "\\"); + strcat(path, name); + + HMODULE dll = LoadLibrary(path); + if(!dll) { + log_printf("Error loading %s: %s", path, w32_error(GetLastError())); + } + + return dll; +} diff --git a/src/common.h b/src/common.h new file mode 100644 index 0000000..7d404c8 --- /dev/null +++ b/src/common.h @@ -0,0 +1,37 @@ +/* IPXWrapper - Common header + * Copyright (C) 2011 Daniel Collins + * + * This program is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 2 as published by + * the Free Software Foundation. + * + * 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, write to the Free Software Foundation, Inc., 51 + * Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. +*/ + +#ifndef IPXWRAPPER_COMMON_H +#define IPXWRAPPER_COMMON_H + +#include + +extern HKEY regkey; + +void log_printf(const char *fmt, ...); + +const char *w32_error(DWORD errnum); + +BOOL reg_open(REGSAM access); +void reg_close(void); + +char reg_get_char(const char *val_name, char default_val); +DWORD reg_get_bin(const char *val_name, void *buf, DWORD size); + +HMODULE load_sysdll(const char *name); + +#endif /* !IPXWRAPPER_COMMON_H */ diff --git a/src/directplay.c b/src/directplay.c index fb5b839..4572b47 100644 --- a/src/directplay.c +++ b/src/directplay.c @@ -23,6 +23,7 @@ #include #include "ipxwrapper.h" +#include "common.h" struct sp_data { SOCKET sock; @@ -49,6 +50,12 @@ const GUID IPX_GUID = { #define DISCOVERY_SOCKET 42367 #define API_HEADER_SIZE sizeof(struct sockaddr_ipx) +#define CALL(n) if(log_calls) { log_printf("DirectPlay: %s", n); } + +extern char const *dllname; +unsigned char log_calls = 0; +static HMODULE sysdll = NULL; + /* Lock the object mutex and return the data pointer */ static struct sp_data *get_sp_data(IDirectPlaySP *sp) { struct sp_data_cont *cont; @@ -131,7 +138,7 @@ static BOOL init_worker(IDirectPlaySP *sp) { } static HRESULT WINAPI IPX_EnumSessions(LPDPSP_ENUMSESSIONSDATA data) { - //log_printf("IPX_EnumSessions called"); + CALL("SP_EnumSessions"); if(!init_worker(data->lpISP)) { return DPERR_GENERIC; @@ -158,7 +165,7 @@ static HRESULT WINAPI IPX_EnumSessions(LPDPSP_ENUMSESSIONSDATA data) { } static HRESULT WINAPI IPX_Send(LPDPSP_SENDDATA data) { - //log_printf("IPX_Send called"); + CALL("SP_Send"); struct sockaddr_ipx *addr = NULL; @@ -169,8 +176,8 @@ static HRESULT WINAPI IPX_Send(LPDPSP_SENDDATA data) { HRESULT r = IDirectPlaySP_GetSPPlayerData(data->lpISP, data->idPlayerTo, (void**)&addr, &size, DPGET_LOCAL); if(r != DP_OK) { + log_printf("GetSPPlayerData: %d", (int)r); addr = NULL; - //log_printf("GetSPPlayerData: %d", r); } }else if(sp_data->ns_addr.sa_family) { addr = &(sp_data->ns_addr); @@ -195,7 +202,7 @@ static HRESULT WINAPI IPX_Send(LPDPSP_SENDDATA data) { } static HRESULT WINAPI IPX_SendEx(LPDPSP_SENDEXDATA data) { - //log_printf("IPX_SendEx called"); + CALL("SP_SendEx"); DPSP_SENDDATA s_data; @@ -228,7 +235,7 @@ static HRESULT WINAPI IPX_SendEx(LPDPSP_SENDEXDATA data) { } static HRESULT WINAPI IPX_Reply(LPDPSP_REPLYDATA data) { - //log_printf("IPX_Reply called (idNameServer = %u)", data->idNameServer); + CALL("SP_Reply"); /* TODO: Only update ns_addr if idNameServer has changed? */ @@ -237,9 +244,10 @@ static HRESULT WINAPI IPX_Reply(LPDPSP_REPLYDATA data) { DWORD size; HRESULT r = IDirectPlaySP_GetSPPlayerData(data->lpISP, data->idNameServer, (void**)&addr_p, &size, DPGET_LOCAL); - //log_printf("GetSPPlayerData: %d", r); - - if(r == DP_OK && addr_p) { + if(r != DP_OK) { + log_printf("GetSPPlayerData: %d", (int)r); + addr_p = NULL; + }else if(addr_p) { memcpy(&(sp_data->ns_addr), addr_p, sizeof(struct sockaddr_ipx)); } @@ -257,6 +265,8 @@ static HRESULT WINAPI IPX_Reply(LPDPSP_REPLYDATA data) { } static HRESULT WINAPI IPX_CreatePlayer(LPDPSP_CREATEPLAYERDATA data) { + CALL("SP_CreatePlayer"); + if(data->lpSPMessageHeader) { HRESULT r = IDirectPlaySP_SetSPPlayerData(data->lpISP, data->idPlayer, data->lpSPMessageHeader, sizeof(struct sockaddr_ipx), DPSET_LOCAL); if(r != DP_OK) { @@ -268,12 +278,8 @@ static HRESULT WINAPI IPX_CreatePlayer(LPDPSP_CREATEPLAYERDATA data) { return DP_OK; } -static HRESULT WINAPI IPX_DeletePlayer(LPDPSP_DELETEPLAYERDATA data) { - return DP_OK; -} - static HRESULT WINAPI IPX_GetCaps(LPDPSP_GETCAPSDATA data) { - //log_printf("IPX_GetCaps called"); + CALL("SP_GetCaps"); if(data->lpCaps->dwSize < sizeof(DPCAPS)) { /* It's either this or DPERR_INVALIDOBJECT according to DirectX 7.0 */ @@ -298,7 +304,7 @@ static HRESULT WINAPI IPX_GetCaps(LPDPSP_GETCAPSDATA data) { } static HRESULT WINAPI IPX_Open(LPDPSP_OPENDATA data) { - //log_printf("IPX_Open called"); + CALL("SP_Open"); if(!init_worker(data->lpISP)) { return DPERR_GENERIC; @@ -325,6 +331,8 @@ static HRESULT WINAPI IPX_Open(LPDPSP_OPENDATA data) { } static HRESULT WINAPI IPX_CloseEx(LPDPSP_CLOSEDATA data) { + CALL("SP_CloseEx"); + struct sp_data *sp_data = get_sp_data(data->lpISP); /* Disable the special bind if in use */ @@ -335,6 +343,8 @@ static HRESULT WINAPI IPX_CloseEx(LPDPSP_CLOSEDATA data) { } static HRESULT WINAPI IPX_ShutdownEx(LPDPSP_SHUTDOWNDATA data) { + CALL("SP_ShutdownEx"); + struct sp_data *sp_data = get_sp_data(data->lpISP); if(sp_data->worker_thread && GetCurrentThreadId() != sp_data->worker_tid) { @@ -442,7 +452,6 @@ HRESULT WINAPI SPInit(LPSPINITDATA data) { data->lpCB->SendEx = &IPX_SendEx; data->lpCB->Reply = &IPX_Reply; data->lpCB->CreatePlayer = &IPX_CreatePlayer; - data->lpCB->DeletePlayer = &IPX_DeletePlayer; data->lpCB->GetCaps = &IPX_GetCaps; data->lpCB->Open = &IPX_Open; data->lpCB->CloseEx = &IPX_CloseEx; @@ -454,34 +463,15 @@ HRESULT WINAPI SPInit(LPSPINITDATA data) { return DP_OK; } -/* Convert a windows error number to an error message */ -char const *w32_error(DWORD errnum) { - static char buf[1024] = {'\0'}; - - FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM, NULL, errnum, 0, buf, 1023, NULL); - buf[strcspn(buf, "\r\n")] = '\0'; - return buf; -} - -extern char const *dllname; -unsigned char log_calls = 0; -static HMODULE sysdll = NULL; - BOOL WINAPI DllMain(HINSTANCE me, DWORD why, LPVOID res) { if(why == DLL_PROCESS_ATTACH) { log_open(); - HKEY key; + reg_open(KEY_QUERY_VALUE); - if(RegOpenKeyEx(HKEY_CURRENT_USER, "Software\\IPXWrapper", 0, KEY_QUERY_VALUE, &key) == ERROR_SUCCESS) { - DWORD size = 1; - - if(RegQueryValueEx(key, "log_calls", NULL, NULL, (BYTE*)&log_calls, &size) != ERROR_SUCCESS || size != 1) { - log_calls = 0; - } - - RegCloseKey(key); - } + log_calls = reg_get_char("log_calls", 0); + + reg_close(); }else if(why == DLL_PROCESS_DETACH) { if(sysdll) { FreeLibrary(sysdll); @@ -496,14 +486,7 @@ BOOL WINAPI DllMain(HINSTANCE me, DWORD why, LPVOID res) { void __stdcall *find_sym(char const *symbol) { if(!sysdll) { - char sysdir[1024], path[1024]; - - GetSystemDirectory(sysdir, 1024); - snprintf(path, 1024, "%s\\%s", sysdir, dllname); - - if(!(sysdll = LoadLibrary(path))) { - abort(); - } + sysdll = load_sysdll(dllname); } void *ptr = GetProcAddress(sysdll, symbol); diff --git a/src/ipxwrapper.c b/src/ipxwrapper.c index c252f3d..dfff327 100644 --- a/src/ipxwrapper.c +++ b/src/ipxwrapper.c @@ -27,6 +27,7 @@ #include #include "ipxwrapper.h" +#include "common.h" #define DLL_UNLOAD(dll) \ if(dll) {\ @@ -48,9 +49,7 @@ HMODULE wsock32_dll = NULL; static HANDLE mutex = NULL; static HANDLE router_thread = NULL; static DWORD router_tid = 0; -static HKEY regkey = NULL; -static HMODULE load_sysdll(char const *name); static int init_router(void); static DWORD WINAPI router_main(LPVOID argp); static void add_host(const unsigned char *net, const unsigned char *node, uint32_t ipaddr); @@ -68,22 +67,9 @@ BOOL WINAPI DllMain(HINSTANCE me, DWORD why, LPVOID res) { return FALSE; } - int reg_err = RegOpenKeyEx( - HKEY_CURRENT_USER, - "Software\\IPXWrapper", - 0, - KEY_QUERY_VALUE, - ®key - ); + reg_open(KEY_QUERY_VALUE); - if(reg_err != ERROR_SUCCESS) { - regkey = NULL; - log_printf("Could not open registry: %s", w32_error(reg_err)); - } - - DWORD gsize = sizeof(global_conf); - - if(!regkey || RegQueryValueEx(regkey, "global", NULL, NULL, (BYTE*)&global_conf, &gsize) != ERROR_SUCCESS || gsize != sizeof(global_conf)) { + if(reg_get_bin("global", &global_conf, sizeof(global_conf)) != sizeof(global_conf)) { global_conf.udp_port = DEFAULT_PORT; global_conf.w95_bug = 1; global_conf.bcast_all = 0; @@ -131,10 +117,7 @@ BOOL WINAPI DllMain(HINSTANCE me, DWORD why, LPVOID res) { WSACleanup(); - if(regkey) { - RegCloseKey(regkey); - regkey = NULL; - } + reg_close(); DLL_UNLOAD(winsock2_dll); DLL_UNLOAD(mswsock_dll); @@ -226,22 +209,6 @@ IP_ADAPTER_INFO *get_nics(void) { return buf; } -/* Load a system DLL */ -static HMODULE load_sysdll(char const *name) { - char sysdir[1024], path[1024]; - HMODULE ret = NULL; - - GetSystemDirectory(sysdir, 1024); - snprintf(path, 1024, "%s\\%s", sysdir, name); - - ret = LoadLibrary(path); - if(!ret) { - log_printf("Error loading %s: %s", path, w32_error(GetLastError())); - } - - return ret; -} - /* Initialize and start the router thread */ static int init_router(void) { net_fd = r_socket(AF_INET, SOCK_DGRAM, 0); @@ -382,15 +349,6 @@ static DWORD WINAPI router_main(LPVOID notused) { return 0; } -/* Convert a windows error number to an error message */ -char const *w32_error(DWORD errnum) { - static char buf[1024] = {'\0'}; - - FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM, NULL, errnum, 0, buf, 1023, NULL); - buf[strcspn(buf, "\r\n")] = '\0'; - return buf; -} - /* Add a host to the hosts list or update an existing one */ static void add_host(const unsigned char *net, const unsigned char *node, uint32_t ipaddr) { ipx_host *hptr = hosts; @@ -465,16 +423,11 @@ static BOOL load_nics(void) { struct reg_value rv; int got_rv = 0; - if(regkey) { - char vname[18]; - NODE_TO_STRING(vname, ifptr->Address); - - DWORD rv_size = sizeof(rv); - - int reg_err = RegQueryValueEx(regkey, vname, NULL, NULL, (BYTE*)&rv, &rv_size); - if(reg_err == ERROR_SUCCESS && rv_size == sizeof(rv)) { - got_rv = 1; - } + char vname[18]; + NODE_TO_STRING(vname, ifptr->Address); + + if(reg_get_bin(vname, &rv, sizeof(rv)) == sizeof(rv)) { + got_rv = 1; } if(got_rv && !rv.enabled) { diff --git a/src/ipxwrapper.h b/src/ipxwrapper.h index 247a200..cc5fc68 100644 --- a/src/ipxwrapper.h +++ b/src/ipxwrapper.h @@ -151,12 +151,10 @@ ipx_socket *get_socket(SOCKET fd); void lock_mutex(void); void unlock_mutex(void); IP_ADAPTER_INFO *get_nics(void); -char const *w32_error(DWORD errnum); ipx_host *find_host(const unsigned char *net, const unsigned char *node); void log_open(); void log_close(); -void log_printf(const char *fmt, ...); int ipx_ex_bind(SOCKET fd, const struct sockaddr_ipx *ipxaddr); diff --git a/src/stubdll.c b/src/stubdll.c index bbe59e1..5f9bb8b 100644 --- a/src/stubdll.c +++ b/src/stubdll.c @@ -19,6 +19,8 @@ #include #include +#include "common.h" + static HMODULE ipxdll = NULL; static HMODULE sysdll = NULL; extern char const *dllname; @@ -30,17 +32,13 @@ void log_close(); void log_printf(const char *fmt, ...); static void load_dlls() { - char sysdir[1024], path[1024]; - - GetSystemDirectory(sysdir, 1024); - snprintf(path, 1024, "%s\\%s", sysdir, dllname); - ipxdll = LoadLibrary("ipxwrapper.dll"); if(!ipxdll) { + log_printf("Error loading ipxwrapper.dll: %s", w32_error(GetLastError())); abort(); } - sysdll = LoadLibrary(path); + sysdll = load_sysdll(dllname); if(!sysdll) { abort(); } @@ -50,17 +48,11 @@ BOOL WINAPI DllMain(HINSTANCE me, DWORD why, LPVOID res) { if(why == DLL_PROCESS_ATTACH) { log_open(); - HKEY key; + reg_open(KEY_QUERY_VALUE); - if(RegOpenKeyEx(HKEY_CURRENT_USER, "Software\\IPXWrapper", 0, KEY_QUERY_VALUE, &key) == ERROR_SUCCESS) { - DWORD size = 1; - - if(RegQueryValueEx(key, "log_calls", NULL, NULL, (BYTE*)&log_calls, &size) != ERROR_SUCCESS || size != 1) { - log_calls = 0; - } - - RegCloseKey(key); - } + log_calls = reg_get_char("log_calls", 0); + + reg_close(); }else if(why == DLL_PROCESS_DETACH) { if(sysdll) { FreeLibrary(sysdll); diff --git a/src/winsock.c b/src/winsock.c index 0cb1a88..cdf0667 100644 --- a/src/winsock.c +++ b/src/winsock.c @@ -24,6 +24,7 @@ #include "winstuff.h" #include "ipxwrapper.h" +#include "common.h" INT APIENTRY EnumProtocolsA(LPINT protocols, LPVOID buf, LPDWORD bsptr) { int bufsize = *bsptr, rval, i, want_ipx = 0;