2006-02-16 23:23:37 +00:00
|
|
|
/*
|
|
|
|
* PROJECT: ReactOS simple TCP/IP services
|
2006-02-19 16:33:14 +00:00
|
|
|
* LICENSE: GPL - See COPYING in the top level directory
|
|
|
|
* FILE: /base/services/tcpsvcs/qotd.c
|
|
|
|
* PURPOSE: Provide CharGen, Daytime, Discard, Echo, and Qotd services
|
|
|
|
* COPYRIGHT: Copyright 2005 - 2006 Ged Murphy <gedmurphy@gmail.com>
|
2006-02-16 23:23:37 +00:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "tcpsvcs.h"
|
|
|
|
|
2006-02-19 16:33:14 +00:00
|
|
|
#define QBUFSIZ 60
|
2006-02-16 23:23:37 +00:00
|
|
|
|
2006-02-19 16:33:14 +00:00
|
|
|
LPCTSTR FilePath = _T("\\drivers\\etc\\quotes"); /* 19 chars */
|
2006-02-16 23:23:37 +00:00
|
|
|
|
2007-07-09 23:13:09 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-02-16 23:23:37 +00:00
|
|
|
DWORD WINAPI QotdHandler(VOID* Sock_)
|
|
|
|
{
|
|
|
|
FILE *fp;
|
|
|
|
SOCKET Sock;
|
2006-02-19 16:33:14 +00:00
|
|
|
TCHAR Sys[MAX_PATH + 20];
|
|
|
|
char Quote[QBUFSIZ][BUFSIZ]; // need to set this dynamically
|
2006-02-16 23:23:37 +00:00
|
|
|
INT QuoteToPrint;
|
|
|
|
INT NumQuotes;
|
|
|
|
|
|
|
|
Sock = (SOCKET)Sock_;
|
|
|
|
|
|
|
|
if(! GetSystemDirectory(Sys, MAX_PATH))
|
|
|
|
{
|
|
|
|
LogEvent(_T("QOTD: Getting system path failed.\n"), 0, TRUE);
|
|
|
|
ExitThread(1);
|
|
|
|
}
|
2006-02-19 16:33:14 +00:00
|
|
|
|
|
|
|
_tcsncat(Sys, FilePath, _tcslen(FilePath));
|
2006-02-16 23:23:37 +00:00
|
|
|
|
|
|
|
LogEvent(_T("QOTD: Opening quotes file\n"), 0, FALSE);
|
2006-02-19 16:33:14 +00:00
|
|
|
if ((fp = _tfopen(Sys, _T("r"))) == NULL)
|
2006-02-16 23:23:37 +00:00
|
|
|
{
|
2006-02-19 16:33:14 +00:00
|
|
|
TCHAR buf[320];
|
2006-02-16 23:23:37 +00:00
|
|
|
|
2006-02-19 16:33:14 +00:00
|
|
|
_sntprintf(buf, 320, _T("QOTD: Error opening quote file : %s\n"), Sys);
|
2006-02-16 23:23:37 +00:00
|
|
|
LogEvent(buf, 0, TRUE);
|
|
|
|
LogEvent(_T("QOTD: Terminating thread\n"), 0, FALSE);
|
|
|
|
ExitThread(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* read all quotes in the file into an array */
|
|
|
|
NumQuotes = 0;
|
2006-02-19 16:33:14 +00:00
|
|
|
while ((fgets(Quote[NumQuotes], QBUFSIZ, fp) != NULL) &&
|
|
|
|
(NumQuotes != QBUFSIZ))
|
2006-02-16 23:23:37 +00:00
|
|
|
NumQuotes++;
|
|
|
|
|
|
|
|
LogEvent(_T("QOTD: Closing quotes file\n"), 0, FALSE);
|
|
|
|
fclose(fp);
|
|
|
|
|
|
|
|
/* randomise the quote */
|
|
|
|
srand((unsigned int) time(0));
|
|
|
|
QuoteToPrint = rand() % NumQuotes;
|
|
|
|
|
|
|
|
if (!SendQuote(Sock, Quote[QuoteToPrint]))
|
|
|
|
LogEvent(_T("QOTD: Error sending data\n"), 0, TRUE);
|
|
|
|
|
|
|
|
|
|
|
|
LogEvent(_T("QOTD: Shutting connection down...\n"), 0, FALSE);
|
|
|
|
if (ShutdownConnection(Sock, FALSE))
|
|
|
|
LogEvent(_T("QOTD: Connection is down\n"), 0, FALSE);
|
|
|
|
else
|
|
|
|
{
|
|
|
|
LogEvent(_T("QOTD: Connection shutdown failed\n"), 0, FALSE);
|
|
|
|
LogEvent(_T("QOTD: Terminating thread\n"), 0, FALSE);
|
|
|
|
ExitThread(1);
|
|
|
|
}
|
2006-02-19 16:33:14 +00:00
|
|
|
|
2006-02-16 23:23:37 +00:00
|
|
|
LogEvent(_T("QOTD: Terminating thread\n"), 0, FALSE);
|
|
|
|
ExitThread(0);
|
|
|
|
|
|
|
|
}
|