mirror of
https://github.com/reactos/reactos.git
synced 2025-07-31 19:21:38 +00:00
- Rewrite the service handler and parts of the main control thread. The rest will be rewritten bit by bit.
- tcpsvcs now cleanly starts serves and stops in Windows. svn path=/trunk/; revision=27552
This commit is contained in:
parent
2c13c59b51
commit
4216adf631
8 changed files with 343 additions and 376 deletions
|
@ -11,84 +11,6 @@
|
|||
|
||||
extern BOOL bShutDown;
|
||||
|
||||
DWORD WINAPI ChargenHandler(VOID* Sock_)
|
||||
{
|
||||
INT RetVal = 0;
|
||||
SOCKET Sock = (SOCKET)Sock_;
|
||||
|
||||
if (!GenerateChars(Sock))
|
||||
{
|
||||
LogEvent(_T("Chargen: Char generation failed\n"), 0, FALSE);
|
||||
RetVal = 1;
|
||||
}
|
||||
|
||||
LogEvent(_T("Chargen: Shutting connection down...\n"), 0, FALSE);
|
||||
if (ShutdownConnection(Sock, FALSE))
|
||||
LogEvent(_T("Chargen: Connection is down.\n"), 0, FALSE);
|
||||
else
|
||||
{
|
||||
LogEvent(_T("Chargen: Connection shutdown failed\n"), 0, FALSE);
|
||||
RetVal = 1;
|
||||
}
|
||||
|
||||
LogEvent(_T("Chargen: Terminating thread\n"), 0, FALSE);
|
||||
ExitThread(RetVal);
|
||||
|
||||
}
|
||||
|
||||
|
||||
BOOL GenerateChars(SOCKET Sock)
|
||||
{
|
||||
int i;
|
||||
int charIndex; /* internal loop */
|
||||
int loopIndex; /* line loop */
|
||||
char ring[END-START];
|
||||
char *endring;
|
||||
char Line[LINESIZ];
|
||||
|
||||
/* fill ring with printable characters */
|
||||
for (charIndex=0, i=START; i<=END; charIndex++, i++)
|
||||
ring[charIndex] = (char)i;
|
||||
/* save the address of the end character in the ring */
|
||||
endring = &ring[charIndex];
|
||||
|
||||
/* where we will start output from */
|
||||
loopIndex = 0;
|
||||
while (! bShutDown)
|
||||
{
|
||||
/* if the loop index is equal to the last char,
|
||||
* start the loop again from the beginning */
|
||||
if (loopIndex == END-START)
|
||||
loopIndex = 0;
|
||||
|
||||
/* start printing from char controled by loopIndex */
|
||||
charIndex = loopIndex;
|
||||
for (i=0; i < LINESIZ - 2; i++)
|
||||
{
|
||||
Line[i] = ring[charIndex];
|
||||
|
||||
if (ring[charIndex] == *endring)
|
||||
charIndex = 0;
|
||||
else
|
||||
charIndex++;
|
||||
}
|
||||
|
||||
Line[LINESIZ - 2] = '\r';
|
||||
Line[LINESIZ - 1] = '\n';
|
||||
|
||||
if (! SendLine(Sock, Line))
|
||||
break;
|
||||
|
||||
/* increment loop index to start printing from next char in ring */
|
||||
loopIndex++;
|
||||
}
|
||||
|
||||
if (bShutDown)
|
||||
return FALSE;
|
||||
else
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
BOOL SendLine(SOCKET Sock, char* Line)
|
||||
{
|
||||
INT RetVal;
|
||||
|
@ -123,3 +45,79 @@ BOOL SendLine(SOCKET Sock, char* Line)
|
|||
LogEvent(_T("Chargen: Connection closed by peer.\n"), 0, FALSE);
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
BOOL GenerateChars(SOCKET Sock)
|
||||
{
|
||||
int i;
|
||||
int charIndex; /* internal loop */
|
||||
int loopIndex; /* line loop */
|
||||
char ring[ASCII_END - ASCII_START];
|
||||
char *endring;
|
||||
char Line[LINESIZ];
|
||||
|
||||
/* fill ring with printable characters */
|
||||
for (charIndex=0, i=ASCII_START; i<=ASCII_END; charIndex++, i++)
|
||||
ring[charIndex] = (char)i;
|
||||
/* save the address of the end character in the ring */
|
||||
endring = &ring[charIndex];
|
||||
|
||||
/* where we will start output from */
|
||||
loopIndex = 0;
|
||||
while (! bShutDown)
|
||||
{
|
||||
/* if the loop index is equal to the last char,
|
||||
* start the loop again from the beginning */
|
||||
if (loopIndex == ASCII_END-ASCII_START)
|
||||
loopIndex = 0;
|
||||
|
||||
/* start printing from char controled by loopIndex */
|
||||
charIndex = loopIndex;
|
||||
for (i=0; i < LINESIZ - 2; i++)
|
||||
{
|
||||
Line[i] = ring[charIndex];
|
||||
|
||||
if (ring[charIndex] == *endring)
|
||||
charIndex = 0;
|
||||
else
|
||||
charIndex++;
|
||||
}
|
||||
|
||||
Line[LINESIZ - 2] = '\r';
|
||||
Line[LINESIZ - 1] = '\n';
|
||||
|
||||
if (! SendLine(Sock, Line))
|
||||
break;
|
||||
|
||||
/* increment loop index to start printing from next char in ring */
|
||||
loopIndex++;
|
||||
}
|
||||
|
||||
if (bShutDown)
|
||||
return FALSE;
|
||||
else
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
DWORD WINAPI ChargenHandler(VOID* Sock_)
|
||||
{
|
||||
INT RetVal = 0;
|
||||
SOCKET Sock = (SOCKET)Sock_;
|
||||
|
||||
if (!GenerateChars(Sock))
|
||||
{
|
||||
LogEvent(_T("Chargen: Char generation failed\n"), 0, FALSE);
|
||||
RetVal = 1;
|
||||
}
|
||||
|
||||
LogEvent(_T("Chargen: Shutting connection down...\n"), 0, FALSE);
|
||||
if (ShutdownConnection(Sock, FALSE))
|
||||
LogEvent(_T("Chargen: Connection is down.\n"), 0, FALSE);
|
||||
else
|
||||
{
|
||||
LogEvent(_T("Chargen: Connection shutdown failed\n"), 0, FALSE);
|
||||
RetVal = 1;
|
||||
}
|
||||
|
||||
LogEvent(_T("Chargen: Terminating thread\n"), 0, FALSE);
|
||||
ExitThread(RetVal);
|
||||
}
|
||||
|
|
|
@ -9,6 +9,19 @@
|
|||
|
||||
#include "tcpsvcs.h"
|
||||
|
||||
BOOL SendTime(SOCKET Sock, CHAR *time)
|
||||
{
|
||||
INT StringSize = (INT)strlen(time);
|
||||
INT RetVal = send(Sock, time, sizeof(CHAR) * StringSize, 0);
|
||||
|
||||
if (RetVal == SOCKET_ERROR)
|
||||
return FALSE;
|
||||
|
||||
LogEvent(_T("DayTime: Connection closed by peer.\n"), 0, FALSE);
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
|
||||
DWORD WINAPI DaytimeHandler(VOID* Sock_)
|
||||
{
|
||||
struct tm *newtime;
|
||||
|
@ -35,16 +48,3 @@ DWORD WINAPI DaytimeHandler(VOID* Sock_)
|
|||
LogEvent(_T("DayTime: Terminating thread\n"), 0, FALSE);
|
||||
ExitThread(RetVal);
|
||||
}
|
||||
|
||||
|
||||
BOOL SendTime(SOCKET Sock, CHAR *time)
|
||||
{
|
||||
INT StringSize = (INT)strlen(time);
|
||||
INT RetVal = send(Sock, time, sizeof(CHAR) * StringSize, 0);
|
||||
|
||||
if (RetVal == SOCKET_ERROR)
|
||||
return FALSE;
|
||||
|
||||
LogEvent(_T("DayTime: Connection closed by peer.\n"), 0, FALSE);
|
||||
return TRUE;
|
||||
}
|
||||
|
|
|
@ -11,32 +11,6 @@
|
|||
|
||||
extern BOOL bShutDown;
|
||||
|
||||
DWORD WINAPI DiscardHandler(VOID* Sock_)
|
||||
{
|
||||
DWORD RetVal = 0;
|
||||
SOCKET Sock = (SOCKET)Sock_;
|
||||
|
||||
if (!RecieveIncomingPackets(Sock))
|
||||
{
|
||||
LogEvent(_T("Discard: RecieveIncomingPackets failed\n"), 0, FALSE);
|
||||
RetVal = 1;
|
||||
}
|
||||
|
||||
LogEvent(_T("Discard: Shutting connection down...\n"), 0, FALSE);
|
||||
if (ShutdownConnection(Sock, TRUE))
|
||||
LogEvent(_T("Discard: Connection is down.\n"), 0, FALSE);
|
||||
else
|
||||
{
|
||||
LogEvent(_T("Discard: Connection shutdown failed\n"), 0, FALSE);
|
||||
RetVal = 1;
|
||||
}
|
||||
|
||||
LogEvent(_T("Discard: Terminating thread\n"), 0, FALSE);
|
||||
ExitThread(RetVal);
|
||||
}
|
||||
|
||||
|
||||
|
||||
BOOL RecieveIncomingPackets(SOCKET Sock)
|
||||
{
|
||||
char ReadBuffer[BUF];
|
||||
|
@ -64,3 +38,27 @@ BOOL RecieveIncomingPackets(SOCKET Sock)
|
|||
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
DWORD WINAPI DiscardHandler(VOID* Sock_)
|
||||
{
|
||||
DWORD RetVal = 0;
|
||||
SOCKET Sock = (SOCKET)Sock_;
|
||||
|
||||
if (!RecieveIncomingPackets(Sock))
|
||||
{
|
||||
LogEvent(_T("Discard: RecieveIncomingPackets failed\n"), 0, FALSE);
|
||||
RetVal = 1;
|
||||
}
|
||||
|
||||
LogEvent(_T("Discard: Shutting connection down...\n"), 0, FALSE);
|
||||
if (ShutdownConnection(Sock, TRUE))
|
||||
LogEvent(_T("Discard: Connection is down.\n"), 0, FALSE);
|
||||
else
|
||||
{
|
||||
LogEvent(_T("Discard: Connection shutdown failed\n"), 0, FALSE);
|
||||
RetVal = 1;
|
||||
}
|
||||
|
||||
LogEvent(_T("Discard: Terminating thread\n"), 0, FALSE);
|
||||
ExitThread(RetVal);
|
||||
}
|
||||
|
|
|
@ -11,32 +11,6 @@
|
|||
|
||||
extern BOOL bShutDown;
|
||||
|
||||
DWORD WINAPI EchoHandler(VOID* Sock_)
|
||||
{
|
||||
DWORD RetVal = 0;
|
||||
SOCKET Sock = (SOCKET)Sock_;
|
||||
|
||||
if (!EchoIncomingPackets(Sock)) {
|
||||
LogEvent(_T("Echo: EchoIncomingPackets failed\n"), 0, FALSE);
|
||||
RetVal = 1;
|
||||
}
|
||||
|
||||
LogEvent(_T("Echo: Shutting connection down...\n"), 0, FALSE);
|
||||
|
||||
if (ShutdownConnection(Sock, TRUE))
|
||||
LogEvent(_T("Echo: Connection is down\n"), 0, FALSE);
|
||||
else
|
||||
{
|
||||
LogEvent(_T("Echo: Connection shutdown failed\n"), 0, FALSE);
|
||||
RetVal = 1;
|
||||
}
|
||||
|
||||
LogEvent(_T("Echo: Terminating thread\n"), 0, FALSE);
|
||||
ExitThread(RetVal);
|
||||
}
|
||||
|
||||
|
||||
|
||||
BOOL EchoIncomingPackets(SOCKET Sock)
|
||||
{
|
||||
char ReadBuffer[BUF];
|
||||
|
@ -88,3 +62,27 @@ BOOL EchoIncomingPackets(SOCKET Sock)
|
|||
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
DWORD WINAPI EchoHandler(VOID* Sock_)
|
||||
{
|
||||
DWORD RetVal = 0;
|
||||
SOCKET Sock = (SOCKET)Sock_;
|
||||
|
||||
if (!EchoIncomingPackets(Sock)) {
|
||||
LogEvent(_T("Echo: EchoIncomingPackets failed\n"), 0, FALSE);
|
||||
RetVal = 1;
|
||||
}
|
||||
|
||||
LogEvent(_T("Echo: Shutting connection down...\n"), 0, FALSE);
|
||||
|
||||
if (ShutdownConnection(Sock, TRUE))
|
||||
LogEvent(_T("Echo: Connection is down\n"), 0, FALSE);
|
||||
else
|
||||
{
|
||||
LogEvent(_T("Echo: Connection shutdown failed\n"), 0, FALSE);
|
||||
RetVal = 1;
|
||||
}
|
||||
|
||||
LogEvent(_T("Echo: Terminating thread\n"), 0, FALSE);
|
||||
ExitThread(RetVal);
|
||||
}
|
||||
|
|
|
@ -13,6 +13,22 @@
|
|||
|
||||
LPCTSTR FilePath = _T("\\drivers\\etc\\quotes"); /* 19 chars */
|
||||
|
||||
BOOL SendQuote(SOCKET Sock, char* Quote)
|
||||
{
|
||||
INT StringSize;
|
||||
INT RetVal;
|
||||
|
||||
StringSize = (INT)strlen(Quote);
|
||||
RetVal = send(Sock, Quote, sizeof(char) * StringSize, 0);
|
||||
|
||||
if (RetVal == SOCKET_ERROR)
|
||||
return FALSE;
|
||||
|
||||
LogEvent(_T("QOTD: Connection closed by peer\n"), 0, FALSE);
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
|
||||
DWORD WINAPI QotdHandler(VOID* Sock_)
|
||||
{
|
||||
FILE *fp;
|
||||
|
@ -74,19 +90,3 @@ DWORD WINAPI QotdHandler(VOID* Sock_)
|
|||
ExitThread(0);
|
||||
|
||||
}
|
||||
|
||||
|
||||
BOOL SendQuote(SOCKET Sock, char* Quote)
|
||||
{
|
||||
INT StringSize;
|
||||
INT RetVal;
|
||||
|
||||
StringSize = (INT)strlen(Quote);
|
||||
RetVal = send(Sock, Quote, sizeof(char) * StringSize, 0);
|
||||
|
||||
if (RetVal == SOCKET_ERROR)
|
||||
return FALSE;
|
||||
|
||||
LogEvent(_T("QOTD: Connection closed by peer\n"), 0, FALSE);
|
||||
return TRUE;
|
||||
}
|
||||
|
|
|
@ -12,34 +12,8 @@
|
|||
extern BOOL bShutDown;
|
||||
extern BOOL bPause;
|
||||
|
||||
DWORD WINAPI StartServer(LPVOID lpParam)
|
||||
{
|
||||
SOCKET ListeningSocket;
|
||||
PSERVICES pServices;
|
||||
TCHAR buf[256];
|
||||
|
||||
pServices = (PSERVICES)lpParam;
|
||||
|
||||
//DebugBreak();
|
||||
ListeningSocket = SetUpListener(htons(pServices->Port));
|
||||
if (ListeningSocket == INVALID_SOCKET)
|
||||
{
|
||||
LogEvent(_T("Socket error when setting up listener\n"), 0, TRUE);
|
||||
return 3;
|
||||
}
|
||||
|
||||
_stprintf(buf, _T("%s is waiting for connections on port %d...\n"),
|
||||
pServices->Name, pServices->Port);
|
||||
LogEvent(buf, 0, FALSE);
|
||||
|
||||
if (! bShutDown)
|
||||
AcceptConnections(ListeningSocket, pServices->Service, pServices->Name);
|
||||
|
||||
ExitThread(0);
|
||||
}
|
||||
|
||||
|
||||
SOCKET SetUpListener(USHORT Port)
|
||||
static SOCKET
|
||||
SetUpListener(USHORT Port)
|
||||
{
|
||||
SOCKET Sock;
|
||||
SOCKADDR_IN Server;
|
||||
|
@ -59,6 +33,7 @@ SOCKET SetUpListener(USHORT Port)
|
|||
LogEvent(_T("bind() failed\n"), 0, TRUE);
|
||||
|
||||
}
|
||||
|
||||
return INVALID_SOCKET;
|
||||
}
|
||||
|
||||
|
@ -74,8 +49,10 @@ typedef struct _WORKER_THREAD {
|
|||
|
||||
*/
|
||||
|
||||
VOID AcceptConnections(SOCKET ListeningSocket,
|
||||
LPTHREAD_START_ROUTINE Service, TCHAR *Name)
|
||||
static VOID
|
||||
AcceptConnections(SOCKET ListeningSocket,
|
||||
LPTHREAD_START_ROUTINE Service,
|
||||
TCHAR *Name)
|
||||
{
|
||||
SOCKADDR_IN Client;
|
||||
SOCKET Sock;
|
||||
|
@ -87,8 +64,6 @@ VOID AcceptConnections(SOCKET ListeningSocket,
|
|||
TCHAR buf[256];
|
||||
INT TimeOut = 2000; // 2 seconds
|
||||
|
||||
//DebugBreak();
|
||||
|
||||
/* set timeout values */
|
||||
TimeVal.tv_sec = TimeOut / 1000;
|
||||
TimeVal.tv_usec = TimeOut % 1000;
|
||||
|
@ -144,7 +119,9 @@ VOID AcceptConnections(SOCKET ListeningSocket,
|
|||
}
|
||||
}
|
||||
|
||||
BOOL ShutdownConnection(SOCKET Sock, BOOL bRec)
|
||||
BOOL
|
||||
ShutdownConnection(SOCKET Sock,
|
||||
BOOL bRec)
|
||||
{
|
||||
TCHAR buf[256];
|
||||
|
||||
|
@ -181,3 +158,36 @@ BOOL ShutdownConnection(SOCKET Sock, BOOL bRec)
|
|||
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
|
||||
DWORD WINAPI
|
||||
StartServer(LPVOID lpParam)
|
||||
{
|
||||
SOCKET ListeningSocket;
|
||||
PSERVICES pServices;
|
||||
TCHAR buf[256];
|
||||
|
||||
pServices = (PSERVICES)lpParam;
|
||||
|
||||
ListeningSocket = SetUpListener(htons(pServices->Port));
|
||||
if (ListeningSocket == INVALID_SOCKET)
|
||||
{
|
||||
LogEvent(_T("Socket error when setting up listener"), 0, TRUE);
|
||||
return 3;
|
||||
}
|
||||
|
||||
_stprintf(buf,
|
||||
_T("%s is waiting for connections on port %d"),
|
||||
pServices->Name,
|
||||
pServices->Port);
|
||||
LogEvent(buf, 0, FALSE);
|
||||
|
||||
if (!bShutDown)
|
||||
AcceptConnections(ListeningSocket, pServices->Service, pServices->Name);
|
||||
|
||||
_stprintf(buf,
|
||||
_T("Exiting %s thread"),
|
||||
pServices->Name);
|
||||
LogEvent(buf, 0, FALSE);
|
||||
ExitThread(0);
|
||||
}
|
||||
|
|
|
@ -3,128 +3,148 @@
|
|||
* LICENSE: GPL - See COPYING in the top level directory
|
||||
* FILE: /base/services/tcpsvcs/tcpsvcs.c
|
||||
* PURPOSE: Provide CharGen, Daytime, Discard, Echo, and Qotd services
|
||||
* COPYRIGHT: Copyright 2005 - 2006 Ged Murphy <gedmurphy@gmail.com>
|
||||
* COPYRIGHT: Copyright 2005 - 2007 Ged Murphy <gedmurphy@reactos.org>
|
||||
*
|
||||
*/
|
||||
/*
|
||||
* TODO:
|
||||
* - fix bug when terminating chargen server
|
||||
* - log info in the event logger (when it's implemented)
|
||||
*/
|
||||
|
||||
|
||||
#include "tcpsvcs.h"
|
||||
|
||||
//#define NDEBUG
|
||||
//#include <debug.h>
|
||||
#define DEBUG
|
||||
|
||||
|
||||
/*
|
||||
* globals
|
||||
*/
|
||||
VOID WINAPI ServiceMain(DWORD argc, LPTSTR argv[]);
|
||||
volatile BOOL bShutDown = FALSE;
|
||||
volatile BOOL bPause = FALSE;
|
||||
|
||||
static SERVICE_STATUS hServStatus;
|
||||
static SERVICE_STATUS_HANDLE hSStat;
|
||||
|
||||
FILE *hLogFile;
|
||||
BOOL bShutDown = FALSE;
|
||||
BOOL bPause = FALSE;
|
||||
|
||||
LPCTSTR LogFileName = _T("\\tcpsvcs_log.log");
|
||||
LPCTSTR LogFileName = _T("C:\\tcpsvcs_log.log");
|
||||
LPTSTR ServiceName = _T("Simp Tcp");
|
||||
//LPTSTR DisplayName = _T("Simple TCP/IP Services");
|
||||
|
||||
static SERVICES
|
||||
Services[NUM_SERVICES] =
|
||||
{
|
||||
{ECHO_PORT, _T("Echo"), EchoHandler},
|
||||
{ECHO_PORT, _T("Echo"), EchoHandler},
|
||||
{DISCARD_PORT, _T("Discard"), DiscardHandler},
|
||||
{DAYTIME_PORT, _T("Daytime"), DaytimeHandler},
|
||||
{QOTD_PORT, _T("QOTD"), QotdHandler},
|
||||
{QOTD_PORT, _T("QOTD"), QotdHandler},
|
||||
{CHARGEN_PORT, _T("Chargen"), ChargenHandler}
|
||||
};
|
||||
|
||||
|
||||
int
|
||||
main(void)
|
||||
VOID
|
||||
LogEvent(LPCTSTR UserMessage,
|
||||
DWORD ExitCode,
|
||||
BOOL PrintErrorMsg)
|
||||
{
|
||||
SERVICE_TABLE_ENTRY ServiceTable[] =
|
||||
#ifdef DEBUG
|
||||
DWORD eMsgLen;
|
||||
DWORD ErrNum = GetLastError();
|
||||
LPTSTR lpvSysMsg;
|
||||
TCHAR MessageBuffer[512];
|
||||
FILE *hLogFile = NULL;
|
||||
|
||||
hLogFile = _tfopen(LogFileName, _T("a"));
|
||||
if (hLogFile == NULL) return;
|
||||
|
||||
if (PrintErrorMsg)
|
||||
{
|
||||
{ServiceName, ServiceMain},
|
||||
{NULL, NULL}
|
||||
};
|
||||
eMsgLen = FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM,
|
||||
NULL,
|
||||
ErrNum,
|
||||
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
|
||||
(LPTSTR)&lpvSysMsg,
|
||||
0,
|
||||
NULL);
|
||||
|
||||
//DPRINT("Starting tcpsvcs service. See \system32%s for logs\n", LogFileName);
|
||||
_stprintf(MessageBuffer,
|
||||
_T("\n%s %s ErrNum = %lu ExitCode = %lu"),
|
||||
UserMessage,
|
||||
lpvSysMsg,
|
||||
ErrNum,
|
||||
ExitCode);
|
||||
|
||||
if (! StartServiceCtrlDispatcher(ServiceTable))
|
||||
LogEvent(_T("failed to start the service control dispatcher\n"), -1, TRUE);
|
||||
HeapFree(GetProcessHeap(),
|
||||
0,
|
||||
lpvSysMsg);
|
||||
|
||||
//DPRINT("Shutdown tcpsvcs service\n");
|
||||
}
|
||||
else
|
||||
{
|
||||
_stprintf(MessageBuffer,
|
||||
_T("\n%s"),
|
||||
UserMessage);
|
||||
}
|
||||
|
||||
return 0;
|
||||
_fputts(MessageBuffer, hLogFile);
|
||||
|
||||
fclose(hLogFile);
|
||||
#endif
|
||||
if (ExitCode > 0)
|
||||
ExitProcess(ExitCode);
|
||||
else
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
VOID
|
||||
UpdateStatus(DWORD NewStatus,
|
||||
DWORD Check)
|
||||
{
|
||||
TCHAR szSet[50];
|
||||
|
||||
if (Check > 0)
|
||||
hServStatus.dwCheckPoint += Check;
|
||||
else
|
||||
hServStatus.dwCheckPoint = Check;
|
||||
|
||||
if (NewStatus > 0)
|
||||
hServStatus.dwCurrentState = NewStatus;
|
||||
|
||||
_sntprintf(szSet, 49, _T("setting service to 0x%lu, CheckPoint %lu"), NewStatus, hServStatus.dwCheckPoint);
|
||||
LogEvent(szSet, 0, FALSE);
|
||||
|
||||
if (!SetServiceStatus(hSStat, &hServStatus))
|
||||
LogEvent(_T("Cannot set service status"), 101, TRUE);
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
|
||||
VOID WINAPI
|
||||
ServiceMain(DWORD argc, LPTSTR argv[])
|
||||
{
|
||||
TCHAR LogFilePath[MAX_PATH + 17];
|
||||
LogEvent(_T("Starting service. First log entry."), 0, FALSE);
|
||||
LogEvent (_T("Entering ServiceMain."), 0, FALSE);
|
||||
|
||||
if(! GetSystemDirectory(LogFilePath, MAX_PATH))
|
||||
return;
|
||||
|
||||
_tcsncat(LogFilePath, LogFileName, 17);
|
||||
|
||||
hLogFile = _tfopen(LogFilePath, _T("a+"));
|
||||
if (hLogFile == NULL)
|
||||
{
|
||||
TCHAR buf[300];
|
||||
|
||||
_sntprintf(buf, 300, _T("Could not open log file: %s\n"), LogFilePath);
|
||||
MessageBox(NULL, buf, NULL, MB_OK);
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
LogEvent(_T("Entering ServiceMain\n"), 0, FALSE);
|
||||
|
||||
hServStatus.dwServiceType = SERVICE_WIN32_OWN_PROCESS;
|
||||
hServStatus.dwCurrentState = SERVICE_START_PENDING;
|
||||
hServStatus.dwControlsAccepted = SERVICE_ACCEPT_STOP |
|
||||
SERVICE_ACCEPT_SHUTDOWN | SERVICE_ACCEPT_PAUSE_CONTINUE;
|
||||
hServStatus.dwWin32ExitCode = ERROR_SERVICE_SPECIFIC_ERROR;
|
||||
hServStatus.dwServiceSpecificExitCode = NO_ERROR;
|
||||
hServStatus.dwCheckPoint = 0;
|
||||
hServStatus.dwWaitHint = 2*CS_TIMEOUT;
|
||||
hServStatus.dwServiceType = SERVICE_WIN32_OWN_PROCESS;
|
||||
hServStatus.dwCurrentState = SERVICE_STOPPED;
|
||||
hServStatus.dwControlsAccepted = SERVICE_ACCEPT_STOP | SERVICE_ACCEPT_SHUTDOWN | SERVICE_ACCEPT_PAUSE_CONTINUE;
|
||||
hServStatus.dwWin32ExitCode = ERROR_SERVICE_SPECIFIC_ERROR;
|
||||
hServStatus.dwServiceSpecificExitCode = 0;
|
||||
hServStatus.dwCheckPoint = 0;
|
||||
hServStatus.dwWaitHint = 2 * CS_TIMEOUT;
|
||||
|
||||
hSStat = RegisterServiceCtrlHandler(ServiceName, ServerCtrlHandler);
|
||||
if (hSStat == 0)
|
||||
LogEvent(_T("Failed to register service\n"), -1, TRUE);
|
||||
LogEvent(_T("Failed to register service\n"), 100, TRUE);
|
||||
|
||||
LogEvent(_T("Control handler registered successfully\n"), 0, FALSE);
|
||||
SetServiceStatus (hSStat, &hServStatus);
|
||||
LogEvent(_T("Service status set to SERVICE_START_PENDING\n"), 0, FALSE);
|
||||
LogEvent(_T("Control handler registered successfully"), 0, FALSE);
|
||||
UpdateStatus(SERVICE_START_PENDING, 1);
|
||||
LogEvent(_T("Service status set to SERVICE_START_PENDING"), 0, FALSE);
|
||||
|
||||
if (CreateServers() != 0)
|
||||
{
|
||||
hServStatus.dwCurrentState = SERVICE_STOPPED;
|
||||
hServStatus.dwServiceSpecificExitCode = 1;
|
||||
SetServiceStatus(hSStat, &hServStatus);
|
||||
UpdateStatus(SERVICE_STOPPED, 0);
|
||||
return;
|
||||
}
|
||||
|
||||
LogEvent(_T("Service threads shut down. Set SERVICE_STOPPED status\n"), 0, FALSE);
|
||||
/* We will only return here when the ServiceSpecific function
|
||||
completes, indicating system shutdown. */
|
||||
UpdateStatus (SERVICE_STOPPED, 0);
|
||||
LogEvent(_T("Service status set to SERVICE_STOPPED\n"), 0, FALSE);
|
||||
LogEvent(_T("Leaving ServiceMain\n"), 0, FALSE);
|
||||
|
||||
fclose(hLogFile);
|
||||
|
||||
return;
|
||||
LogEvent(_T("Service threads shut down. Set SERVICE_STOPPED status"), 0, FALSE);
|
||||
UpdateStatus(SERVICE_STOPPED, 0);
|
||||
LogEvent(_T("Service status set to SERVICE_STOPPED\n"), 0, FALSE);
|
||||
LogEvent(_T("Leaving ServiceMain\n"), 0, FALSE);
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
VOID WINAPI
|
||||
|
@ -132,55 +152,46 @@ ServerCtrlHandler(DWORD Control)
|
|||
{
|
||||
switch (Control)
|
||||
{
|
||||
case SERVICE_CONTROL_SHUTDOWN: /* fall through */
|
||||
case SERVICE_CONTROL_SHUTDOWN:
|
||||
case SERVICE_CONTROL_STOP:
|
||||
LogEvent(_T("stopping service\n"), 0, FALSE);
|
||||
LogEvent(_T("\nSetting the service to SERVICE_STOP_PENDING"), 0, FALSE);
|
||||
InterlockedExchange((LONG *)&bShutDown, TRUE);
|
||||
UpdateStatus(SERVICE_STOP_PENDING, -1);
|
||||
hServStatus.dwWin32ExitCode = 0;
|
||||
hServStatus.dwWaitHint = 0;
|
||||
UpdateStatus(SERVICE_STOP_PENDING, 1);
|
||||
break;
|
||||
|
||||
case SERVICE_CONTROL_PAUSE: /* not yet implemented */
|
||||
LogEvent(_T("pausing service\n"), 0, FALSE);
|
||||
LogEvent(_T("Setting the service to SERVICE_PAUSED"), 0, FALSE);
|
||||
InterlockedExchange((LONG *)&bPause, TRUE);
|
||||
UpdateStatus(SERVICE_PAUSED, 0);
|
||||
break;
|
||||
|
||||
case SERVICE_CONTROL_CONTINUE:
|
||||
LogEvent(_T("continuing service\n"), 0, FALSE);
|
||||
LogEvent(_T("Setting the service to SERVICE_RUNNING"), 0, FALSE);
|
||||
InterlockedExchange((LONG *)&bPause, FALSE);
|
||||
UpdateStatus(SERVICE_RUNNING, 0);
|
||||
break;
|
||||
|
||||
case SERVICE_CONTROL_INTERROGATE:
|
||||
break;
|
||||
|
||||
default:
|
||||
if (Control > 127 && Control < 256) /* user defined */
|
||||
break;
|
||||
}
|
||||
UpdateStatus(-1, -1); /* increment checkpoint */
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
void UpdateStatus (int NewStatus, int Check)
|
||||
/* Set a new service status and checkpoint (either specific value or increment) */
|
||||
{
|
||||
if (Check < 0 )
|
||||
hServStatus.dwCheckPoint++;
|
||||
else
|
||||
hServStatus.dwCheckPoint = Check;
|
||||
|
||||
if (NewStatus >= 0)
|
||||
hServStatus.dwCurrentState = NewStatus;
|
||||
|
||||
if (! SetServiceStatus (hSStat, &hServStatus))
|
||||
LogEvent(_T("Cannot set service status\n"), -1, TRUE);
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
INT
|
||||
CreateServers()
|
||||
{
|
||||
DWORD dwThreadId[NUM_SERVICES];
|
||||
HANDLE hThread[NUM_SERVICES];
|
||||
WSADATA wsaData;
|
||||
TCHAR buf[256];
|
||||
WSADATA wsaData;
|
||||
TCHAR buf[256];
|
||||
INT i;
|
||||
DWORD RetVal;
|
||||
|
||||
|
@ -191,86 +202,64 @@ CreateServers()
|
|||
return -1;
|
||||
}
|
||||
|
||||
UpdateStatus(-1, -1); /* increment checkpoint */
|
||||
UpdateStatus(0, 1); /* increment checkpoint */
|
||||
|
||||
LogEvent(_T("Creating server Threads\n"), 0, FALSE);
|
||||
LogEvent(_T("\nCreating server Threads"), 0, FALSE);
|
||||
|
||||
/* Create MAX_THREADS worker threads. */
|
||||
for( i=0; i<NUM_SERVICES; i++ )
|
||||
/* Create worker threads. */
|
||||
for(i = 0; i < NUM_SERVICES; i++)
|
||||
{
|
||||
_stprintf(buf, _T("Starting %s server....\n"), Services[i].Name);
|
||||
_stprintf(buf, _T("Starting %s server"), Services[i].Name);
|
||||
LogEvent(buf, 0, FALSE);
|
||||
|
||||
hThread[i] = CreateThread(
|
||||
NULL, // default security attributes
|
||||
0, // use default stack size
|
||||
StartServer, // thread function
|
||||
&Services[i], // argument to thread function
|
||||
0, // use default creation flags
|
||||
&dwThreadId[i]); // returns the thread identifier
|
||||
hThread[i] = CreateThread(NULL, // default security attributes
|
||||
0, // use default stack size
|
||||
StartServer, // thread function
|
||||
&Services[i], // argument to thread function
|
||||
0, // use default creation flags
|
||||
&dwThreadId[i]); // returns the thread identifier
|
||||
|
||||
/* Check the return value for success. */
|
||||
if (hThread[i] == NULL)
|
||||
{
|
||||
_stprintf(buf, _T("Failed to start %s server....\n"), Services[i].Name);
|
||||
_stprintf(buf, _T("\nFailed to start %s server\n"), Services[i].Name);
|
||||
/* don't exit process via LogEvent. We want to exit via the server
|
||||
* which failed to start, which could mean i=0 */
|
||||
LogEvent(buf, 0, TRUE);
|
||||
ExitProcess(i);
|
||||
}
|
||||
|
||||
UpdateStatus(0, 1); /* increment checkpoint */
|
||||
}
|
||||
|
||||
LogEvent(_T("setting service status to running\n"), 0, FALSE);
|
||||
LogEvent(_T("setting service status to running"), 0, FALSE);
|
||||
|
||||
UpdateStatus(SERVICE_RUNNING, 0);
|
||||
|
||||
/* Wait until all threads have terminated. */
|
||||
WaitForMultipleObjects(NUM_SERVICES, hThread, TRUE, INFINITE);
|
||||
|
||||
/* Close all thread handles upon completion. */
|
||||
for(i=0; i<NUM_SERVICES; i++)
|
||||
for(i = 0; i < NUM_SERVICES; i++)
|
||||
{
|
||||
CloseHandle(hThread[i]);
|
||||
}
|
||||
|
||||
LogEvent(_T("Detaching Winsock2...\n"), 0, FALSE);
|
||||
LogEvent(_T("Detaching Winsock2"), 0, FALSE);
|
||||
WSACleanup();
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
/* This is a temperary log system until our eventlog is in place */
|
||||
|
||||
VOID
|
||||
LogEvent (LPCTSTR UserMessage, INT ExitCode, BOOL PrintErrorMsg)
|
||||
int _tmain (int argc, LPTSTR argv [])
|
||||
{
|
||||
DWORD eMsgLen, ErrNum = GetLastError ();
|
||||
LPTSTR lpvSysMsg;
|
||||
TCHAR MessageBuffer[1024];
|
||||
|
||||
|
||||
|
||||
if (PrintErrorMsg)
|
||||
SERVICE_TABLE_ENTRY ServiceTable[] =
|
||||
{
|
||||
eMsgLen = FormatMessage (FORMAT_MESSAGE_ALLOCATE_BUFFER |
|
||||
FORMAT_MESSAGE_FROM_SYSTEM, NULL,
|
||||
ErrNum, MAKELANGID (LANG_NEUTRAL, SUBLANG_DEFAULT),
|
||||
(LPTSTR)&lpvSysMsg, 0, NULL);
|
||||
{ServiceName, ServiceMain},
|
||||
{NULL, NULL }
|
||||
};
|
||||
|
||||
_sntprintf(MessageBuffer, 1024, _T("%s %s ErrNum = %lu. ExitCode = %d."),
|
||||
UserMessage, lpvSysMsg, ErrNum, ExitCode);
|
||||
HeapFree(GetProcessHeap (), 0, lpvSysMsg);
|
||||
}
|
||||
else
|
||||
{
|
||||
_sntprintf(MessageBuffer, 1024, _T("%s"), UserMessage);
|
||||
}
|
||||
remove(LogFileName);
|
||||
|
||||
_fputts(MessageBuffer, hLogFile);
|
||||
if (!StartServiceCtrlDispatcher(ServiceTable))
|
||||
LogEvent(_T("failed to start the service control dispatcher\n"), 100, TRUE);
|
||||
|
||||
if (ExitCode != 0)
|
||||
ExitProcess(ExitCode);
|
||||
else
|
||||
return;
|
||||
return 0;
|
||||
}
|
||||
|
|
|
@ -1,12 +1,6 @@
|
|||
/*
|
||||
* PROJECT: ReactOS simple TCP/IP services
|
||||
* LICENSE: GPL - See COPYING in the top level directory
|
||||
* FILE: /base/services/tcpsvcs/tcpsvcs.h
|
||||
* PURPOSE: Provide CharGen, Daytime, Discard, Echo, and Qotd services
|
||||
* COPYRIGHT: Copyright 2005 - 2006 Ged Murphy <gedmurphy@gmail.com>
|
||||
*
|
||||
*/
|
||||
|
||||
#ifdef _MSC_VER
|
||||
#define _CRT_SECURE_NO_DEPRECATE 1
|
||||
#endif
|
||||
#include <stdio.h>
|
||||
#include <winsock2.h>
|
||||
#include <tchar.h>
|
||||
|
@ -28,8 +22,8 @@
|
|||
#define MAX_QUOTE_BUF 512
|
||||
|
||||
/* printable ASCII's characters for chargen */
|
||||
#define START 32
|
||||
#define END 126
|
||||
#define ASCII_START 32
|
||||
#define ASCII_END 126
|
||||
|
||||
/* number of chars to put on a line */
|
||||
#define LINESIZ 74 // 72 + /r and /n
|
||||
|
@ -42,38 +36,18 @@ typedef struct _Services {
|
|||
} SERVICES, *PSERVICES;
|
||||
|
||||
/* tcpsvcs functions */
|
||||
//static VOID WINAPI ServiceMain(DWORD argc, LPTSTR argv[]);
|
||||
VOID WINAPI ServerCtrlHandler(DWORD control);
|
||||
INT CreateServers(VOID);
|
||||
VOID LogEvent (LPCTSTR UserMessage, INT ExitCode, BOOL PrintErrorMsg);
|
||||
void UpdateStatus (int NewStatus, int Check);
|
||||
|
||||
VOID LogEvent(LPCTSTR UserMessage, DWORD ExitCode, BOOL PrintErrorMsg);
|
||||
void UpdateStatus(DWORD NewStatus, DWORD Check);
|
||||
|
||||
/* skelserver functions */
|
||||
DWORD WINAPI StartServer(LPVOID lpParam);
|
||||
SOCKET SetUpListener(USHORT Port);
|
||||
VOID AcceptConnections(SOCKET ListeningSocket,
|
||||
LPTHREAD_START_ROUTINE Service, TCHAR *Name);
|
||||
BOOL EchoIncomingPackets(SOCKET sd);
|
||||
BOOL ShutdownConnection(SOCKET Sock, BOOL bRec);
|
||||
|
||||
/* chargen functions */
|
||||
/* server thread handlers */
|
||||
DWORD WINAPI ChargenHandler(VOID* Sock_);
|
||||
BOOL GenerateChars(SOCKET Sock);
|
||||
BOOL SendLine(SOCKET Sock, char* Line);
|
||||
|
||||
/* daytime functions */
|
||||
DWORD WINAPI DaytimeHandler(VOID* Sock_);
|
||||
BOOL SendTime(SOCKET Sock, char *time);
|
||||
|
||||
/* echo functions */
|
||||
DWORD WINAPI EchoHandler(VOID* Sock_);
|
||||
BOOL EchoIncomingPackets(SOCKET Sock);
|
||||
|
||||
/* discard functions */
|
||||
DWORD WINAPI DiscardHandler(VOID* Sock_);
|
||||
BOOL RecieveIncomingPackets(SOCKET Sock);
|
||||
|
||||
/* qotd functions */
|
||||
DWORD WINAPI QotdHandler(VOID* Sock_);
|
||||
BOOL SendQuote(SOCKET Sock, char* Quote);
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue