ReactOS  0.4.12-dev-18-gf469aca
tcpsvcs.h File Reference
#include <stdarg.h>
#include <windef.h>
#include <winbase.h>
#include <winsock2.h>
#include <tchar.h>
#include <strsafe.h>
Include dependency graph for tcpsvcs.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Classes

struct  _Services
 

Macros

#define WIN32_NO_STATUS
 
#define _INC_WINDOWS
 
#define LOG_FILE   1
 
#define LOG_EVENTLOG   2
 
#define LOG_ERROR   4
 
#define LOG_ALL   (LOG_FILE | LOG_EVENTLOG | LOG_ERROR)
 
#define ECHO_PORT   7
 
#define DISCARD_PORT   9
 
#define DAYTIME_PORT   13
 
#define QOTD_PORT   17
 
#define CHARGEN_PORT   19
 
#define NUM_SERVICES   5
 
#define CS_TIMEOUT   1000
 

Typedefs

typedef struct _Services SERVICES
 
typedef struct _ServicesPSERVICES
 

Functions

BOOL InitLogging (VOID)
 
VOID UninitLogging (VOID)
 
VOID LogEvent (LPCWSTR lpMsg, DWORD errNum, DWORD exitCode, UINT flags)
 
DWORD WINAPI StartServer (LPVOID lpParam)
 
BOOL ShutdownConnection (SOCKET Sock, BOOL bRec)
 
DWORD WINAPI ChargenHandler (VOID *sock_)
 
DWORD WINAPI DaytimeHandler (VOID *sock_)
 
DWORD WINAPI EchoHandler (VOID *sock_)
 
DWORD WINAPI DiscardHandler (VOID *sock_)
 
DWORD WINAPI QotdHandler (VOID *sock_)
 

Variables

volatile BOOL bShutdown
 
volatile BOOL bPause
 

Macro Definition Documentation

◆ _INC_WINDOWS

#define _INC_WINDOWS

Definition at line 9 of file tcpsvcs.h.

◆ CHARGEN_PORT

#define CHARGEN_PORT   19

Definition at line 24 of file tcpsvcs.h.

◆ CS_TIMEOUT

#define CS_TIMEOUT   1000

Definition at line 27 of file tcpsvcs.h.

Referenced by ServiceMain().

◆ DAYTIME_PORT

#define DAYTIME_PORT   13

Definition at line 22 of file tcpsvcs.h.

◆ DISCARD_PORT

#define DISCARD_PORT   9

Definition at line 21 of file tcpsvcs.h.

◆ ECHO_PORT

#define ECHO_PORT   7

Definition at line 20 of file tcpsvcs.h.

◆ LOG_ALL

#define LOG_ALL   (LOG_FILE | LOG_EVENTLOG | LOG_ERROR)

Definition at line 17 of file tcpsvcs.h.

Referenced by _tmain(), CreateServers(), CreateServiceThread(), ServiceMain(), and UpdateStatus().

◆ LOG_ERROR

◆ LOG_EVENTLOG

#define LOG_EVENTLOG   2

Definition at line 15 of file tcpsvcs.h.

Referenced by InitLogging(), LogEvent(), and LogToFile().

◆ LOG_FILE

#define LOG_FILE   1

Definition at line 14 of file tcpsvcs.h.

◆ NUM_SERVICES

#define NUM_SERVICES   5

Definition at line 26 of file tcpsvcs.h.

Referenced by CreateServers().

◆ QOTD_PORT

#define QOTD_PORT   17

Definition at line 23 of file tcpsvcs.h.

◆ WIN32_NO_STATUS

#define WIN32_NO_STATUS

Definition at line 6 of file tcpsvcs.h.

Typedef Documentation

◆ PSERVICES

◆ SERVICES

Function Documentation

◆ ChargenHandler()

DWORD WINAPI ChargenHandler ( VOID sock_)

Definition at line 96 of file chargen.c.

97 {
98  INT retVal = 0;
99  SOCKET sock = (SOCKET)sock_;
100 
101  if (!GenerateChars(sock))
102  {
103  LogEvent(L"Chargen: Char generation failed", 0, 0, LOG_FILE);
104  retVal = 1;
105  }
106 
107  LogEvent(L"Chargen: Shutting connection down...", 0, 0, LOG_FILE);
108  if (ShutdownConnection(sock, FALSE))
109  {
110  LogEvent(L"Chargen: Connection is down", 0, 0, LOG_FILE);
111  }
112  else
113  {
114  LogEvent(L"Chargen: Connection shutdown failed", 0, 0, LOG_FILE);
115  retVal = 1;
116  }
117 
118  LogEvent(L"Chargen: Terminating thread", 0, 0, LOG_FILE);
119  ExitThread(retVal);
120 }
int32_t INT
Definition: typedefs.h:56
VOID WINAPI ExitThread(IN DWORD uExitCode)
Definition: thread.c:327
BOOL ShutdownConnection(SOCKET sock, BOOL bRec)
Definition: skelserver.c:126
static BOOL GenerateChars(SOCKET sock)
Definition: chargen.c:51
static const WCHAR L[]
Definition: oid.c:1087
VOID LogEvent(LPCWSTR lpMsg, DWORD errNum, DWORD exitCode, UINT flags)
Definition: log.c:196
Definition: tcpcore.h:1455
UINT_PTR SOCKET
Definition: winsock.h:47

◆ DaytimeHandler()

DWORD WINAPI DaytimeHandler ( VOID sock_)

Definition at line 29 of file daytime.c.

30 {
31  struct tm *localTime;
32  time_t aclock;
33  CHAR *pszTime;
34  DWORD retVal = 0;
35  SOCKET Sock = (SOCKET)Sock_;
36 
37  time(&aclock);
38  localTime = localtime(&aclock);
39  if (localTime)
40  {
41  pszTime = asctime(localTime);
42  if (!SendTime(Sock, pszTime))
43  retVal = 1;
44  }
45 
46  LogEvent(L"DayTime: Shutting connection down", 0, 0, LOG_FILE);
47  if (ShutdownConnection(Sock, FALSE))
48  LogEvent(L"DayTime: Connection is down", 0, 0, LOG_FILE);
49  else
50  {
51  LogEvent(L"DayTime: Connection shutdown failed", 0, 0, LOG_FILE);
52  retVal = 1;
53  }
54 
55  LogEvent(L"DayTime: Terminating thread", 0, 0, LOG_FILE);
56  ExitThread(retVal);
57 }
char CHAR
Definition: xmlstorage.h:175
__u16 time
Definition: mkdosfs.c:366
_Check_return_ _CRTIMP char *__cdecl asctime(_In_ const struct tm *_Tm)
VOID WINAPI ExitThread(IN DWORD uExitCode)
Definition: thread.c:327
unsigned long DWORD
Definition: ntddk_ex.h:95
BOOL ShutdownConnection(SOCKET sock, BOOL bRec)
Definition: skelserver.c:126
static const WCHAR L[]
Definition: oid.c:1087
Definition: time.h:76
_CRTIMP struct tm *__cdecl localtime(const time_t *_Time)
Definition: time.h:426
VOID LogEvent(LPCWSTR lpMsg, DWORD errNum, DWORD exitCode, UINT flags)
Definition: log.c:196
__kernel_time_t time_t
Definition: linux.h:252
static BOOL SendTime(SOCKET sock, CHAR *time)
Definition: daytime.c:15
UINT_PTR SOCKET
Definition: winsock.h:47

◆ DiscardHandler()

DWORD WINAPI DiscardHandler ( VOID sock_)

Definition at line 44 of file discard.c.

45 {
46  DWORD retVal = 0;
47  SOCKET sock = (SOCKET)sock_;
48 
49  if (!ReceiveIncomingPackets(sock))
50  {
51  LogEvent(L"Discard: ReceiveIncomingPackets failed", 0, 0, LOG_FILE);
52  retVal = 1;
53  }
54 
55  LogEvent(L"Discard: Shutting connection down", 0, 0, LOG_FILE);
56  if (ShutdownConnection(sock, TRUE))
57  {
58  LogEvent(L"Discard: Connection is down.", 0, 0, LOG_FILE);
59  }
60  else
61  {
62  LogEvent(L"Discard: Connection shutdown failed", 0, 0, LOG_FILE);
63  retVal = 1;
64  }
65 
66  LogEvent(L"Discard: Terminating thread", 0, 0, LOG_FILE);
67  ExitThread(retVal);
68 }
#define TRUE
Definition: types.h:120
VOID WINAPI ExitThread(IN DWORD uExitCode)
Definition: thread.c:327
unsigned long DWORD
Definition: ntddk_ex.h:95
BOOL ShutdownConnection(SOCKET sock, BOOL bRec)
Definition: skelserver.c:126
static const WCHAR L[]
Definition: oid.c:1087
VOID LogEvent(LPCWSTR lpMsg, DWORD errNum, DWORD exitCode, UINT flags)
Definition: log.c:196
Definition: tcpcore.h:1455
static BOOL ReceiveIncomingPackets(SOCKET sock)
Definition: discard.c:15
UINT_PTR SOCKET
Definition: winsock.h:47

◆ EchoHandler()

DWORD WINAPI EchoHandler ( VOID sock_)

Definition at line 69 of file echo.c.

70 {
71  DWORD retVal = 0;
72  SOCKET sock = (SOCKET)sock_;
73 
74  if (!EchoIncomingPackets(sock))
75  {
76  LogEvent(L"Echo: EchoIncomingPackets failed", 0, 0, LOG_FILE);
77  retVal = 1;
78  }
79 
80  LogEvent(L"Echo: Shutting connection down", 0, 0, LOG_FILE);
81 
82  if (ShutdownConnection(sock, TRUE))
83  {
84  LogEvent(L"Echo: Connection is down", 0, 0, LOG_FILE);
85  }
86  else
87  {
88  LogEvent(L"Echo: Connection shutdown failed", 0, 0, LOG_FILE);
89  retVal = 1;
90  }
91 
92  LogEvent(L"Echo: Terminating thread", 0, 0, LOG_FILE);
93  ExitThread(retVal);
94 }
#define TRUE
Definition: types.h:120
VOID WINAPI ExitThread(IN DWORD uExitCode)
Definition: thread.c:327
static BOOL EchoIncomingPackets(SOCKET sock)
Definition: echo.c:15
unsigned long DWORD
Definition: ntddk_ex.h:95
BOOL ShutdownConnection(SOCKET sock, BOOL bRec)
Definition: skelserver.c:126
static const WCHAR L[]
Definition: oid.c:1087
VOID LogEvent(LPCWSTR lpMsg, DWORD errNum, DWORD exitCode, UINT flags)
Definition: log.c:196
Definition: tcpcore.h:1455
UINT_PTR SOCKET
Definition: winsock.h:47

◆ InitLogging()

BOOL InitLogging ( VOID  )

◆ LogEvent()

VOID LogEvent ( LPCWSTR  lpMsg,
DWORD  errNum,
DWORD  exitCode,
UINT  flags 
)

Definition at line 196 of file log.c.

Referenced by _tmain(), AcceptConnections(), ChargenHandler(), CreateServers(), CreateServiceThread(), DaytimeHandler(), DiscardHandler(), EchoHandler(), EchoIncomingPackets(), QotdHandler(), ReceiveIncomingPackets(), RetrieveQuote(), SendLine(), SendTime(), ServerCtrlHandler(), ServiceMain(), SetUpListener(), ShutdownConnection(), StartServer(), and UpdateStatus().

200 {
201 #ifdef DEBUG
202  if (flags & LOG_FILE || flags & LOG_ERROR)
203  LogToFile(lpMsg, errNum, exitCode, flags);
204 #endif
205  if (flags & LOG_EVENTLOG)
206  LogToEventLog(lpMsg, errNum, exitCode, flags);
207 }
#define LOG_EVENTLOG
Definition: tcpsvcs.h:15
static VOID LogToFile(LPCWSTR lpMsg, DWORD errNum, DWORD exitCode, UINT flags)
Definition: log.c:67
#define LOG_ERROR
Definition: tcpsvcs.h:16
static VOID LogToEventLog(LPCWSTR lpMsg, DWORD errNum, DWORD exitCode, UINT flags)
Definition: log.c:23
GLbitfield flags
Definition: glext.h:7161

◆ QotdHandler()

DWORD WINAPI QotdHandler ( VOID sock_)

Definition at line 135 of file qotd.c.

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);
147  if (ShutdownConnection(sock, FALSE))
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 }
static BOOL RetrieveQuote(SOCKET sock)
Definition: qotd.c:27
VOID WINAPI ExitThread(IN DWORD uExitCode)
Definition: thread.c:327
unsigned long DWORD
Definition: ntddk_ex.h:95
BOOL ShutdownConnection(SOCKET sock, BOOL bRec)
Definition: skelserver.c:126
static const WCHAR L[]
Definition: oid.c:1087
VOID LogEvent(LPCWSTR lpMsg, DWORD errNum, DWORD exitCode, UINT flags)
Definition: log.c:196
Definition: tcpcore.h:1455
UINT_PTR SOCKET
Definition: winsock.h:47

◆ ShutdownConnection()

BOOL ShutdownConnection ( SOCKET  Sock,
BOOL  bRec 
)

Definition at line 126 of file skelserver.c.

Referenced by ChargenHandler(), DaytimeHandler(), DiscardHandler(), EchoHandler(), and QotdHandler().

128 {
129  WCHAR logBuf[256];
130 
131  /* Disallow any further data sends. This will tell the other side
132  that we want to go away now. If we skip this step, we don't
133  shut the connection down nicely. */
135  {
136  LogEvent(L"Error in shutdown()", WSAGetLastError(), 0, LOG_ERROR);
137  return FALSE;
138  }
139 
140  /* Receive any extra data still sitting on the socket
141  before we close it */
142  if (bRec)
143  {
144  CHAR readBuffer[BUF];
145  INT ret;
146 
147  do
148  {
149  ret = recv(sock, readBuffer, BUF, 0);
150  if (ret >= 0)
151  {
152  swprintf(logBuf, L"FYI, received %d unexpected bytes during shutdown", ret);
153  LogEvent(logBuf, 0, 0, LOG_FILE);
154  }
155  } while (ret > 0);
156  }
157 
158  closesocket(sock);
159 
160  return TRUE;
161 }
#define SOCKET_ERROR
Definition: winsock.h:333
#define TRUE
Definition: types.h:120
__wchar_t WCHAR
Definition: xmlstorage.h:180
char CHAR
Definition: xmlstorage.h:175
#define BUF
Definition: skelserver.c:12
int32_t INT
Definition: typedefs.h:56
#define closesocket
Definition: main.c:39
Definition: jssnprintf.c:29
#define LOG_ERROR
Definition: tcpsvcs.h:16
INT WSAAPI WSAGetLastError(VOID)
Definition: dllmain.c:112
INT WSAAPI shutdown(IN SOCKET s, IN INT how)
Definition: sockctrl.c:506
#define swprintf(buf, format,...)
Definition: sprintf.c:56
int ret
static const WCHAR L[]
Definition: oid.c:1087
VOID LogEvent(LPCWSTR lpMsg, DWORD errNum, DWORD exitCode, UINT flags)
Definition: log.c:196
#define SD_SEND
Definition: winsock.h:55
Definition: tcpcore.h:1455
INT WSAAPI recv(IN SOCKET s, OUT CHAR FAR *buf, IN INT len, IN INT flags)
Definition: recv.c:23

◆ StartServer()

DWORD WINAPI StartServer ( LPVOID  lpParam)

Definition at line 165 of file skelserver.c.

Referenced by CreateServers().

166 {
167  SOCKET listeningSocket;
168  PSERVICES pServices;
169  TCHAR logBuf[256];
170 
171  pServices = (PSERVICES)lpParam;
172 
173  swprintf(logBuf, L"Starting %s server", pServices->lpName);
174  LogEvent(logBuf, 0, 0, LOG_FILE);
175 
176  if (!bShutdown)
177  {
178  listeningSocket = SetUpListener(htons(pServices->Port));
179  if (!bShutdown && listeningSocket != INVALID_SOCKET)
180  {
181  swprintf(logBuf,
182  L"%s is waiting for connections on port %d",
183  pServices->lpName,
184  pServices->Port);
185  LogEvent(logBuf, 0, 0, LOG_FILE);
186 
187  AcceptConnections(listeningSocket, pServices->lpService, pServices->lpName);
188  }
189  else
190  {
191  LogEvent(L"Socket error when setting up listener", 0, 0, LOG_FILE);
192  }
193  }
194 
195  swprintf(logBuf, L"Exiting %s thread", pServices->lpName);
196  LogEvent(logBuf, 0, 0, LOG_FILE);
197  ExitThread(0);
198 }
volatile BOOL bShutdown
Definition: tcpsvcs.c:16
struct _Services * PSERVICES
static VOID AcceptConnections(SOCKET listeningSocket, LPTHREAD_START_ROUTINE lpService, LPWSTR lpName)
Definition: skelserver.c:55
VOID WINAPI ExitThread(IN DWORD uExitCode)
Definition: thread.c:327
USHORT Port
Definition: tcpsvcs.h:33
LPWSTR lpName
Definition: tcpsvcs.h:34
char TCHAR
Definition: xmlstorage.h:189
#define INVALID_SOCKET
Definition: winsock.h:332
#define swprintf(buf, format,...)
Definition: sprintf.c:56
static const WCHAR L[]
Definition: oid.c:1087
LPTHREAD_START_ROUTINE lpService
Definition: tcpsvcs.h:35
VOID LogEvent(LPCWSTR lpMsg, DWORD errNum, DWORD exitCode, UINT flags)
Definition: log.c:196
UINT_PTR SOCKET
Definition: winsock.h:47
#define htons(x)
Definition: module.h:213
static SOCKET SetUpListener(USHORT Port)
Definition: skelserver.c:15

◆ UninitLogging()

VOID UninitLogging ( VOID  )

Variable Documentation

◆ bPause

volatile BOOL bPause

Definition at line 17 of file tcpsvcs.c.

◆ bShutdown