revert deleting this folder to keep it's history.
[reactos.git] / reactos / apps / utils / net / 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 <stdio.h>
16 #include <winsock2.h>
17 #include <tchar.h>
18 #include <time.h>
19 #include "tcpsvcs.h"
20
21 #define QBUFSIZ 160
22 #define NUMQUOTES 60
23
24 LPCTSTR FilePath = "C:\\ReactOS\\system32\\drivers\\etc\\quotes";
25
26 DWORD WINAPI QotdHandler(VOID* Sock_)
27 {
28 FILE *fp;
29 SOCKET Sock;
30 INT QuoteToPrint;
31 TCHAR Quote[NUMQUOTES][BUFSIZ]; // need to set this dynamically
32 INT i = 0;
33
34 Sock = (SOCKET)Sock_;
35
36 _tprintf(_T("Opening quotes file\n"));
37 if ((fp = _tfopen(FilePath, "r")) == NULL)
38 {
39 _tprintf(_T("Error opening file: %lu\n"), GetLastError());
40 _tprintf(_T("Terminating qotd thread\n"));
41 ExitThread(-1);
42 }
43
44 while (_fgetts(Quote[i], QBUFSIZ, fp) != NULL)
45 i++;
46
47 _tprintf(_T("Closing quotes file\n"));
48 fclose(fp);
49
50 /* randomise the quote */
51 srand((unsigned int) time(0));
52 QuoteToPrint = rand() % NUMQUOTES;
53
54 if (!SendQuote(Sock, Quote[QuoteToPrint]))
55 {
56 _tprintf(_T("Error sending data. Error: %x\n"), WSAGetLastError());
57 }
58
59 _tprintf(_T("Shutting connection down...\n"));
60 if (ShutdownConnection(Sock, FALSE))
61 _tprintf(_T("Connection is down.\n"));
62 else
63 {
64 _tprintf(_T("Connection shutdown failed\n"));
65 _tprintf(_T("Terminating qotd thread\n"));
66 ExitThread(-1);
67 }
68
69 _tprintf(_T("Terminating qotd thread\n"));
70 ExitThread(0);
71
72 //return Retval;
73 }
74
75
76 BOOL SendQuote(SOCKET Sock, TCHAR* Quote)
77 {
78 INT StringSize;
79 INT RetVal;
80
81 StringSize = strlen(Quote);
82 RetVal = send(Sock, Quote, sizeof(TCHAR) * StringSize, 0);
83
84 if (RetVal == SOCKET_ERROR)
85 return FALSE;
86
87 _tprintf(("Connection closed by peer.\n"));
88 return TRUE;
89 }