📄 hcl.cpp
字号:
// hcl.cpp// hook for HCL inetd operation// copyright SafeTP Development Group, Inc., 2000 Terms of use are as specified in license.txt#include "hcl.h" // this module#include "winetd.h"#include "socket.h" // SOCKET, api, etc.#include "syserr.h" // xsyserror#include <ctype.h> // isdigit// what I eventually want to wrap in its own moduleSOCKET inetdStartupHookHCL(int &argc, char **&argv){ // first, test to see if we've been started under HCL inetd if (!( argc > 1 && isdigit(argv[1][0]) )) { // the first character is always a digit under HCL inetd, and // since it was not in this case, we are not running under HCL inetd return INVALID_SOCKET; } // try to load the DLL that communicates with the inetd server HINSTANCE hHCLDLLInst = LoadLibrary("HCLLIB.DLL"); if (hHCLDLLInst == NULL) { xsyserror("LoadLibrary", "couldn't load HCLLIB.DLL, required for HCL inetd operation."); } // get a pointer to the function we need typedef SOCKET (*LPDLLInitInetd)(LPSTR lpszCmdLine); LPDLLInitInetd DLLInitInetd; DLLInitInetd = (LPDLLInitInetd)GetProcAddress(hHCLDLLInst, "DLLInitInetd"); if (!DLLInitInetd) { xsyserror("GetProcAddress", "trying to get address of DLLInitInetd from HCLLIB.DLL"); } // get the acceptance socket SOCKET socket; socket = DLLInitInetd(argv[1]); // This must be the command line passed // to the daemon application or argv[1] // for console applications. // On return lpszCmdLine will have // The optional parameters as specified // in Inetd. // SM: Since we're passing argv[1], inetd will toast it (no // big deal), and we'll increment argv on return (and assume // the caller won't try to read argv[0] as its executable name). // SM: I just noticed that DLLInitInetd returns SOCKET_ERROR instead // of INVALID_SOCKET on error.. that's a mistake on the part of // the interface designer, because technically SOCKET_ERROR is // in the range of valid socket descriptors (whereas // INVALID_SOCKET is not). // SM: Turns out they're the same... oh well. if (socket == SOCKET_ERROR) { // this is not just a simple failure to run under inetd, because // we already detected that the first argument was numeric xsyserror("DLLInitInetd","DLLInitInetd claims not to recognize my first argument.. " "did you specify a numeric first argument by mistake?"); } // having successfully processed the first argument, adjust argc // and argv so it looks like it wasn't there at all (note that this // is slightly problematic, in that argv[0], which wasn't involved // at all, has now effectively been clobbered) // DOB: this problem is now fixed- we no longer clobber argv[0] argv[1] = argv[0]; argc--; argv++; // put socket in blocking mode { // we need a dummy window handle... HWND dummyWindow = CreateWindow( "STATIC", // address of registered class name "", // address of window name WS_OVERLAPPED, // window style 0, // horizontal position of window 0, // vertical position of window 0, // window width 0, // window height NULL, // handle of parent or owner window NULL, // handle of menu or child-window identifier (HINSTANCE)GetModuleHandle(NULL), // handle of application instance NULL // address of window-creation data ); if (!dummyWindow) { xsyserror("CreateWindow", "making a dummy window"); } // must "reset" WSAAsyncSelect first, it appears if (0 != WSAAsyncSelect(socket, dummyWindow, 0,0)) { xsyserror("WSAAsyncSelect"); } // do the damned mode change unsigned long blockmode = 0; if (0 != ioctlsocket(socket, FIONBIO, &blockmode)) { xsyserror("ioctlsocket"); } // get rid of the dummy window if (!DestroyWindow(dummyWindow)) { //xsyserror("DestroyWindow"); // continue anyway } } // ok, we're in business return socket;}//------------------------------------------------------------------------------------SOCKET inetdStartupHookWinetd(int &argc, char **&argv) { // first, test to see if we've been started under HCL inetd if (!( argc > 1 && !strncmp(argv[1],INETD_CMD_STR,strlen(INETD_CMD_STR)) )) { // missing the Winetd command string - we are not running under Winetd return INVALID_SOCKET; } // try to load the DLL that communicates with the inetd server HINSTANCE hWinetDLLInst = LoadLibrary("WINETLIB.DLL"); if (hWinetDLLInst == NULL) { xsyserror("LoadLibrary", "couldn't load WINETLIB.DLL, required for Winetd inetd operation."); } // get a pointer to the function we need typedef SOCKET (*LPDLLInitInetd)(LPSTR lpszCmdLine); LPDLLInitInetd DLLInitInetd; DLLInitInetd = (LPDLLInitInetd)GetProcAddress(hWinetDLLInst, "DLLInitInetd"); if (!DLLInitInetd) { xsyserror("GetProcAddress", "trying to get address of DLLInitInetd from WINETLIB.DLL"); } // get the acceptance socket SOCKET socket; socket = DLLInitInetd(argv[1]); if (socket == INVALID_SOCKET) xsyserror("DLLInitInetd", "Winetd initialization failed."); argv[1] = argv[0]; argc--; argv++; return socket; }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -