Sync with trunk head (part 1 of 2)
[reactos.git] / base / services / tcpsvcs / daytime.c
1 /*
2 * PROJECT: ReactOS simple TCP/IP services
3 * LICENSE: GPL - See COPYING in the top level directory
4 * FILE: /base/services/tcpsvcs/daytime.c
5 * PURPOSE: Sends the current date and time to the client
6 * COPYRIGHT: Copyright 2005 - 2008 Ged Murphy <gedmurphy@reactos.org>
7 *
8 */
9
10 #include "tcpsvcs.h"
11
12 static BOOL
13 SendTime(SOCKET sock, CHAR *time)
14 {
15 DWORD stringSize = strlen(time) + 1;
16 if (send(sock, time, stringSize, 0) == SOCKET_ERROR)
17 {
18 LogEvent(L"DayTime: Error sending data", WSAGetLastError(), 0, LOG_ERROR);
19 return FALSE;
20 }
21
22 return TRUE;
23 }
24
25
26 DWORD WINAPI
27 DaytimeHandler(VOID* Sock_)
28 {
29 struct tm *localTime;
30 time_t aclock;
31 CHAR *pszTime;
32 DWORD retVal = 0;
33 SOCKET Sock = (SOCKET)Sock_;
34
35 time(&aclock);
36 localTime = localtime(&aclock);
37 if (localTime)
38 {
39 pszTime = asctime(localTime);
40 if (!SendTime(Sock, pszTime))
41 retVal = 1;
42 }
43
44 LogEvent(L"DayTime: Shutting connection down", 0, 0, LOG_FILE);
45 if (ShutdownConnection(Sock, FALSE))
46 LogEvent(L"DayTime: Connection is down", 0, 0, LOG_FILE);
47 else
48 {
49 LogEvent(L"DayTime: Connection shutdown failed", 0, 0, LOG_FILE);
50 retVal = 1;
51 }
52
53 LogEvent(L"DayTime: Terminating thread", 0, 0, LOG_FILE);
54 ExitThread(retVal);
55 }