From c02eb3f16c0193799dc98c930b3cb8b16b01e45e Mon Sep 17 00:00:00 2001 From: Bram Matthys Date: Mon, 3 Jan 2022 13:18:51 +0100 Subject: [PATCH] Windows code cleanup: WSAStartup() -> init_winsock() --- include/h.h | 3 +++ src/ircd.c | 7 ++----- src/socket.c | 13 +++++++++++++ src/unrealircdctl.c | 5 +++-- src/windows/gui.c | 8 +------- src/windows/service.c | 9 +-------- 6 files changed, 23 insertions(+), 22 deletions(-) diff --git a/include/h.h b/include/h.h index ac1aca4a7..e026215cd 100644 --- a/include/h.h +++ b/include/h.h @@ -948,6 +948,9 @@ extern int unreal_bind(int fd, const char *ip, int port, SocketType socket_type) extern int unreal_connect(int fd, const char *ip, int port, int ipv6); extern int is_valid_ip(const char *str); extern int ipv6_capable(void); +#ifdef _WIN32 +extern void init_winsock(void); +#endif extern MODVAR Client *remote_rehash_client; extern MODVAR int debugfd; extern void convert_to_absolute_path(char **path, const char *reldir); diff --git a/src/ircd.c b/src/ircd.c index 1beb7de00..6622ba027 100644 --- a/src/ircd.c +++ b/src/ircd.c @@ -500,10 +500,7 @@ int main(int argc, char *argv[]) int InitUnrealIRCd(int argc, char *argv[]) #endif { -#ifdef _WIN32 - WORD wVersionRequested = MAKEWORD(1, 1); - WSADATA wsaData; -#else +#ifndef _WIN32 uid_t uid, euid; gid_t gid, egid; #endif @@ -573,7 +570,7 @@ int InitUnrealIRCd(int argc, char *argv[]) #ifndef _WIN32 (void)umask(077); /* better safe than sorry --SRB */ #else - WSAStartup(wVersionRequested, &wsaData); + init_winsock(); #endif setup_signals(); diff --git a/src/socket.c b/src/socket.c index 4eea04598..9ba0c4b19 100644 --- a/src/socket.c +++ b/src/socket.c @@ -1365,3 +1365,16 @@ int unreal_bind(int fd, const char *ip, int port, SocketType socket_type) return ret; } } + +#ifdef _WIN32 +void init_winsock(void) +{ + WSADATA WSAData; + if (WSAStartup(MAKEWORD(1, 1), &WSAData) != 0) + { + MessageBox(NULL, "Unable to initialize WinSock", "UnrealIRCD Initalization Error", MB_OK); + fprintf(stderr, "Unable to initialize WinSock\n"); + exit(1); + } +} +#endif diff --git a/src/unrealircdctl.c b/src/unrealircdctl.c index 9785834fd..7a1109d2b 100644 --- a/src/unrealircdctl.c +++ b/src/unrealircdctl.c @@ -73,9 +73,10 @@ void unrealircdctl_status(void) int main(int argc, char *argv[]) { - WSADATA wsaData; - WSAStartup(MAKEWORD(1,1), &wsaData); dbuf_init(); +#ifdef _WIN32 + init_winsock(); +#endif if (argc == 1) unrealircdctl_usage(argv[0]); diff --git a/src/windows/gui.c b/src/windows/gui.c index 3c199a14e..4af593297 100644 --- a/src/windows/gui.c +++ b/src/windows/gui.c @@ -188,7 +188,6 @@ int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLi MSG msg; unsigned char *s; HWND hWnd; - WSADATA WSAData; HICON hIcon; SC_HANDLE hService, hSCManager; SERVICE_TABLE_ENTRY DispatchTable[] = @@ -250,12 +249,7 @@ int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLi if (!LoadLibrary("riched20.dll")) LoadLibrary("riched32.dll"); InitDebug(); - - if (WSAStartup(MAKEWORD(1, 1), &WSAData) != 0) - { - MessageBox(NULL, "Unable to initialize WinSock", "UnrealIRCD Initalization Error", MB_OK); - return FALSE; - } + init_winsock(); hInst = hInstance; MainDlgBackground = CreateSolidBrush(RGB(75, 134, 238)); /* Background of main dialog */ diff --git a/src/windows/service.c b/src/windows/service.c index 7db873d4f..4286d910c 100644 --- a/src/windows/service.c +++ b/src/windows/service.c @@ -90,7 +90,6 @@ VOID WINAPI IRCDCtrlHandler(DWORD opcode) */ VOID WINAPI ServiceMain(DWORD dwArgc, LPTSTR *lpszArgv) { - WSADATA WSAData; DWORD error = 0; char path[MAX_PATH], *folder; @@ -121,13 +120,7 @@ VOID WINAPI ServiceMain(DWORD dwArgc, LPTSTR *lpszArgv) GetOSName(OSName); InitDebug(); - - /* Initialize Winsocks */ - if ((error = WSAStartup(MAKEWORD(1, 1), &WSAData)) != 0) - { - SetServiceStop(error); - return; - } + init_winsock(); /* Initialize the IRCd */ if ((error = InitUnrealIRCd(dwArgc, lpszArgv)) != 1)