move network tools
[reactos.git] / reactos / services / tcpsvcs / qotd.c
1 /*
2 * ReactOS Services
3 * Copyright (C) 2005 ReactOS Team
4 *
5 * LICENCE: GPL - See COPYING in the top level directory
6 * PROJECT: ReactOS simple TCP/IP services
7 * FILE: apps/utils/net/tcpsvcs/qotd.c
8 * PURPOSE: Provide CharGen, Daytime, Discard, Echo, and Qotd services
9 * PROGRAMMERS: Ged Murphy (gedmurphy@gmail.com)
10 * REVISIONS:
11 * GM 04/10/05 Created
12 *
13 */
14
15 #include "tcpsvcs.h"
16
17 #define QBUFSIZ 160
18
19 LPCTSTR FilePath = _T("\\drivers\\etc\\quotes");
20
21 DWORD WINAPI QotdHandler(VOID* Sock_)
22 {
23 FILE *fp;
24 SOCKET Sock;
25 TCHAR Sys[MAX_PATH];
26 TCHAR Quote[60][BUFSIZ]; // need to set this dynamically
27 INT QuoteToPrint;
28 INT NumQuotes;
29
30 Sock = (SOCKET)Sock_;
31
32 if(! GetSystemDirectory(Sys, MAX_PATH))
33 {
34 LogEvent(_T("QOTD: Getting system path failed.\n"), 0, TRUE);
35 ExitThread(1);
36 }
37
38 _tcscat(Sys, FilePath);
39
40 LogEvent(_T("QOTD: Opening quotes file\n"), 0, FALSE);
41 if ((fp = _tfopen(Sys, "r")) == NULL)
42 {
43 TCHAR buf[256];
44
45 _stprintf(buf, _T("QOTD: Error opening quote file : %s\n"), Sys);
46 LogEvent(buf, 0, TRUE);
47 LogEvent(_T("QOTD: Terminating thread\n"), 0, FALSE);
48 ExitThread(1);
49 }
50
51 /* read all quotes in the file into an array */
52 NumQuotes = 0;
53 while (_fgetts(Quote[NumQuotes], QBUFSIZ, fp) != NULL)
54 NumQuotes++;
55
56 LogEvent(_T("QOTD: Closing quotes file\n"), 0, FALSE);
57 fclose(fp);
58
59 /* randomise the quote */
60 srand((unsigned int) time(0));
61 QuoteToPrint = rand() % NumQuotes;
62
63 if (!SendQuote(Sock, Quote[QuoteToPrint]))
64 LogEvent(_T("QOTD: Error sending data\n"), 0, TRUE);
65
66
67 LogEvent(_T("QOTD: Shutting connection down...\n"), 0, FALSE);
68 if (ShutdownConnection(Sock, FALSE))
69 LogEvent(_T("QOTD: Connection is down\n"), 0, FALSE);
70 else
71 {
72 LogEvent(_T("QOTD: Connection shutdown failed\n"), 0, FALSE);
73 LogEvent(_T("QOTD: Terminating thread\n"), 0, FALSE);
74 ExitThread(1);
75 }
76
77 LogEvent(_T("QOTD: Terminating thread\n"), 0, FALSE);
78 ExitThread(0);
79
80 //return Retval;
81 }
82
83
84 BOOL SendQuote(SOCKET Sock, TCHAR* Quote)
85 {
86 INT StringSize;
87 INT RetVal;
88
89 StringSize = (INT)_tcsclen(Quote);
90 RetVal = send(Sock, Quote, sizeof(TCHAR) * StringSize, 0);
91
92 if (RetVal == SOCKET_ERROR)
93 return FALSE;
94
95 LogEvent(_T("QOTD: Connection closed by peer\n"), 0, FALSE);
96 return TRUE;
97 }