ReactOS 0.4.15-dev-7906-g1b85a5f
qotd.c
Go to the documentation of this file.
1/*
2 * PROJECT: ReactOS simple TCP/IP services
3 * LICENSE: GPL - See COPYING in the top level directory
4 * FILE: base/services/tcpsvcs/qotd.c
5 * PURPOSE: Sends a random quote to the client
6 * COPYRIGHT: Copyright 2005 - 2008 Ged Murphy <gedmurphy@reactos.org>
7 *
8 */
9
10#include "tcpsvcs.h"
11
12#include <stdlib.h>
13
14static WCHAR szFilePath[] = L"\\drivers\\etc\\quotes";
15
16static BOOL
17SendQuote(SOCKET sock, char* Quote)
18{
19 INT strSize = strlen(Quote);
20 if (send(sock, Quote, strSize, 0) == SOCKET_ERROR)
21 return FALSE;
22
23 return TRUE;
24}
25
26static BOOL
28{
30 WCHAR szFullPath[MAX_PATH + 20];
31 DWORD dwBytesRead;
32 LPSTR lpQuotes;
33 LPSTR lpStr;
34 INT quoteNum;
35 INT NumQuotes = 0;
36 INT i;
37
38 if(!GetSystemDirectoryW(szFullPath, MAX_PATH))
39 {
40 LogEvent(L"QOTD: Getting system path failed", GetLastError(), 0, LOG_FILE);
41 return FALSE;
42 }
43 wcscat(szFullPath, szFilePath);
44
45
46 LogEvent(L"QOTD: Opening quotes file", 0, 0, LOG_FILE);
47 hFile = CreateFileW(szFullPath,
49 0,
50 NULL,
53 NULL);
55 {
56 LogEvent(L"QOTD: Error opening quotes file", GetLastError(), 0, LOG_FILE);
57 }
58 else
59 {
61 lpQuotes = (LPSTR)HeapAlloc(GetProcessHeap(), 0, dwSize + 1);
62 if (!lpQuotes)
63 {
65 return FALSE;
66 }
67
69 lpQuotes,
70 dwSize,
71 &dwBytesRead,
72 NULL);
74
75 lpQuotes[dwSize] = 0;
76
77 if (dwBytesRead != dwSize)
78 {
79 HeapFree(GetProcessHeap(), 0, lpQuotes);
80 return FALSE;
81 }
82
83 lpStr = lpQuotes;
84 while (*lpStr)
85 {
86 if (*lpStr == '%')
87 NumQuotes++;
88 lpStr++;
89 }
90
91 /* pick a random quote */
92 srand((unsigned int) GetTickCount());
93 quoteNum = rand() % NumQuotes;
94
95 /* retrieve the full quote */
96 lpStr = lpQuotes;
97 for (i = 1; i <= quoteNum; i++)
98 {
99 /* move past preceding quote */
100 lpStr++;
101
102 if (i == quoteNum)
103 {
104 LPSTR lpStart = lpStr;
105
106 while (*lpStr != '%' && *lpStr != '\0')
107 lpStr++;
108
109 *lpStr = 0;
110
111 /* send the quote */
112 if (!SendQuote(sock, lpStart))
113 LogEvent(L"QOTD: Error sending data", 0, 0, LOG_FILE);
114 break;
115 }
116 else
117 {
118 while (*lpStr != '%' && *lpStr != '\0')
119 lpStr++;
120
121 /* move past % and RN */
122 lpStr += 2;
123 }
124 }
125
126 HeapFree(GetProcessHeap(), 0, lpQuotes);
127 return TRUE;
128 }
129
130 return FALSE;
131}
132
133
136{
137 SOCKET sock = (SOCKET)sock_;
138 DWORD retVal = 0;
139
140 if (!RetrieveQuote(sock))
141 {
142 LogEvent(L"QOTD: Error retrieving quote", 0, 0, LOG_FILE);
143 retVal = 1;
144 }
145
146 LogEvent(L"QOTD: Shutting connection down", 0, 0, LOG_FILE);
148 {
149 LogEvent(L"QOTD: Connection is down", 0, 0, LOG_FILE);
150 }
151 else
152 {
153 LogEvent(L"QOTD: Connection shutdown failed", 0, 0, LOG_FILE);
154 LogEvent(L"QOTD: Terminating thread", 0, 0, LOG_FILE);
155 retVal = 1;
156 }
157
158 LogEvent(L"QOTD: Terminating thread", 0, 0, LOG_FILE);
159 ExitThread(retVal);
160}
ACPI_SIZE strlen(const char *String)
Definition: utclib.c:269
VOID LogEvent(LPCWSTR lpMsg, DWORD errNum, DWORD exitCode, UINT flags)
Definition: log.c:196
#define NULL
Definition: types.h:112
#define TRUE
Definition: types.h:120
#define FALSE
Definition: types.h:117
#define CloseHandle
Definition: compat.h:739
#define GetProcessHeap()
Definition: compat.h:736
#define OPEN_EXISTING
Definition: compat.h:775
#define ReadFile(a, b, c, d, e)
Definition: compat.h:742
#define INVALID_HANDLE_VALUE
Definition: compat.h:731
#define HeapAlloc
Definition: compat.h:733
#define GENERIC_READ
Definition: compat.h:135
#define MAX_PATH
Definition: compat.h:34
#define HeapFree(x, y, z)
Definition: compat.h:735
#define CreateFileW
Definition: compat.h:741
#define FILE_ATTRIBUTE_NORMAL
Definition: compat.h:137
DWORD WINAPI GetFileSize(HANDLE hFile, LPDWORD lpFileSizeHigh)
Definition: fileinfo.c:331
UINT WINAPI GetSystemDirectoryW(OUT LPWSTR lpBuffer, IN UINT uSize)
Definition: path.c:2313
VOID WINAPI ExitThread(IN DWORD uExitCode)
Definition: thread.c:365
DWORD WINAPI GetTickCount(VOID)
Definition: time.c:455
INT WSAAPI send(IN SOCKET s, IN CONST CHAR FAR *buf, IN INT len, IN INT flags)
Definition: send.c:23
unsigned int BOOL
Definition: ntddk_ex.h:94
unsigned long DWORD
Definition: ntddk_ex.h:95
GLsizei GLenum const GLvoid GLsizei GLenum GLbyte GLbyte GLbyte GLdouble GLdouble GLdouble GLfloat GLfloat GLfloat GLint GLint GLint GLshort GLshort GLshort GLubyte GLubyte GLubyte GLuint GLuint GLuint GLushort GLushort GLushort GLbyte GLbyte GLbyte GLbyte GLdouble GLdouble GLdouble GLdouble GLfloat GLfloat GLfloat GLfloat GLint GLint GLint GLint GLshort GLshort GLshort GLshort GLubyte GLubyte GLubyte GLubyte GLuint GLuint GLuint GLuint GLushort GLushort GLushort GLushort GLboolean const GLdouble const GLfloat const GLint const GLshort const GLbyte const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLdouble const GLfloat const GLfloat const GLint const GLint const GLshort const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort GLenum GLenum GLenum GLfloat GLenum GLint GLenum GLenum GLenum GLfloat GLenum GLenum GLint GLenum GLfloat GLenum GLint GLint GLushort GLenum GLenum GLfloat GLenum GLenum GLint GLfloat const GLubyte GLenum GLenum GLenum const GLfloat GLenum GLenum const GLint GLenum GLint GLint GLsizei GLsizei GLint GLenum GLenum const GLvoid GLenum GLenum const GLfloat GLenum GLenum const GLint GLenum GLenum const GLdouble GLenum GLenum const GLfloat GLenum GLenum const GLint GLsizei GLuint GLfloat GLuint GLbitfield GLfloat GLint GLuint GLboolean GLenum GLfloat GLenum GLbitfield GLenum GLfloat GLfloat GLint GLint const GLfloat GLenum GLfloat GLfloat GLint GLint GLfloat GLfloat GLint GLint const GLfloat GLint GLfloat GLfloat GLint GLfloat GLfloat GLint GLfloat GLfloat const GLdouble const GLfloat const GLdouble const GLfloat GLint i
Definition: glfuncs.h:248
void __cdecl srand(_In_ unsigned int _Seed)
_Check_return_ int __cdecl rand(void)
Definition: rand.c:10
PSDBQUERYRESULT_VISTA PVOID DWORD * dwSize
Definition: env.c:56
_In_ HANDLE hFile
Definition: mswsock.h:90
#define L(x)
Definition: ntvdm.h:50
static BOOL RetrieveQuote(SOCKET sock)
Definition: qotd.c:27
static WCHAR szFilePath[]
Definition: qotd.c:14
DWORD WINAPI QotdHandler(VOID *sock_)
Definition: qotd.c:135
static BOOL SendQuote(SOCKET sock, char *Quote)
Definition: qotd.c:17
_CRTIMP wchar_t *__cdecl wcscat(_Inout_updates_z_(_String_length_(_Dest)+_String_length_(_Source)+1) wchar_t *_Dest, _In_z_ const wchar_t *_Source)
BOOL ShutdownConnection(SOCKET sock, BOOL bRec)
Definition: skelserver.c:126
Definition: tcpcore.h:1455
int32_t INT
Definition: typedefs.h:58
DWORD WINAPI GetLastError(void)
Definition: except.c:1042
#define WINAPI
Definition: msvc.h:6
UINT_PTR SOCKET
Definition: winsock.h:47
#define SOCKET_ERROR
Definition: winsock.h:333
char * LPSTR
Definition: xmlstorage.h:182
__wchar_t WCHAR
Definition: xmlstorage.h:180