ReactOS  0.4.13-dev-443-g10f00f8
chargen.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/chargen.c
5  * PURPOSE: Sends continuous lines of chars to the client
6  * COPYRIGHT: Copyright 2005 - 2008 Ged Murphy <gedmurphy@reactos.org>
7  *
8  */
9 
10 #include "tcpsvcs.h"
11 
12 /* printable ASCII's characters for chargen */
13 #define ASCII_START 32
14 #define ASCII_END 126
15 #define NUM_CHARS ASCII_END - ASCII_START + 1
16 
17 /* number of chars to put on a line */
18 #define LINESIZE 74 // 72 + CR + NL
19 
20 static BOOL
22 {
23  BOOL bRet = FALSE;
24 
25  /*FIXME: need to establish if peer closes connection, not just report a socket error */
26  INT retVal = send(sock, lpLine, LINESIZE, 0);
27  if (retVal > 0)
28  {
29  if (retVal == LINESIZE)
30  {
31  bRet = TRUE;
32  }
33  else
34  {
35  LogEvent(L"Chargen: Not sent enough bytes", 0, 0, LOG_FILE);
36  }
37  }
38  else if (retVal == SOCKET_ERROR)
39  {
40  LogEvent(L"Chargen: Socket error\n", WSAGetLastError(), 0, LOG_ERROR);
41  }
42  else
43  {
44  LogEvent(L"Chargen: unknown error\n", WSAGetLastError(), 0, LOG_ERROR);
45  }
46 
47  return bRet;
48 }
49 
50 static BOOL
52 {
53  CHAR chars[NUM_CHARS];
55  INT charIndex;
56  INT loopIndex;
57  INT i;
58 
59  /* fill the array with printable characters */
60  for (charIndex = 0, i = ASCII_START; i <= ASCII_END; charIndex++, i++)
61  chars[charIndex] = (CHAR)i;
62 
63  loopIndex = 0;
64  while (!bShutdown)
65  {
66  /* reset the loop when we hit the last char */
67  if (loopIndex == NUM_CHARS)
68  loopIndex = 0;
69 
70  /* fill a line array to send */
71  charIndex = loopIndex;
72  for (i=0; i < LINESIZE - 2; i++)
73  {
74  line[i] = chars[charIndex];
75 
76  /* if we hit the end char, reset it */
77  if (chars[charIndex] == chars[NUM_CHARS - 1])
78  charIndex = 0;
79  else
80  charIndex++;
81  }
82  line[LINESIZE - 2] = '\r';
83  line[LINESIZE - 1] = '\n';
84 
85  if (!SendLine(sock, line))
86  break;
87 
88  /* start printing from next char in the array */
89  loopIndex++;
90  }
91 
92  return TRUE;
93 }
94 
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);
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 }
volatile BOOL bShutdown
Definition: tcpsvcs.c:16
#define SOCKET_ERROR
Definition: winsock.h:333
#define TRUE
Definition: types.h:120
#define ASCII_START
Definition: chargen.c:13
char CHAR
Definition: xmlstorage.h:175
#define NUM_CHARS
Definition: chargen.c:15
char * LPSTR
Definition: xmlstorage.h:182
int32_t INT
Definition: typedefs.h:56
#define ASCII_END
Definition: chargen.c:14
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
static BOOL SendLine(SOCKET sock, LPSTR lpLine)
Definition: chargen.c:21
unsigned int BOOL
Definition: ntddk_ex.h:94
Definition: parser.c:48
#define LOG_ERROR
Definition: tcpsvcs.h:16
VOID WINAPI ExitThread(IN DWORD uExitCode)
Definition: thread.c:326
DWORD WINAPI ChargenHandler(VOID *sock_)
Definition: chargen.c:96
INT WSAAPI WSAGetLastError(VOID)
Definition: dllmain.c:112
INT WSAAPI send(IN SOCKET s, IN CONST CHAR FAR *buf, IN INT len, IN INT flags)
Definition: send.c:23
#define WINAPI
Definition: msvc.h:8
unsigned long DWORD
Definition: ntddk_ex.h:95
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:1250
VOID LogEvent(LPCWSTR lpMsg, DWORD errNum, DWORD exitCode, UINT flags)
Definition: log.c:196
#define LINESIZE
Definition: chargen.c:18
Definition: tcpcore.h:1455
UINT_PTR SOCKET
Definition: winsock.h:47