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 continous lines of chars to the client
6 * COPYRIGHT: Copyright 2005 - 2008 Ged Murphy <gedmurphy@reactos.org>
12 /* printable ASCII's characters for chargen */
13 #define ASCII_START 32
15 #define NUM_CHARS ASCII_END - ASCII_START
17 /* number of chars to put on a line */
18 #define LINESIZE 74 // 72 + CR + NL
21 SendLine(SOCKET sock
, LPSTR lpLine
)
25 /*FIXME: need to establish if peer closes connection, not just report a socket error */
26 INT retVal
= send(sock
, lpLine
, LINESIZE
, 0);
29 if (retVal
== LINESIZE
)
35 LogEvent(L
"Chargen: Not sent enough bytes", 0, 0, LOG_FILE
);
38 else if (retVal
== SOCKET_ERROR
)
40 LogEvent(L
"Chargen: Socket error\n", WSAGetLastError(), 0, LOG_ERROR
);
44 LogEvent(L
"Chargen: unknown error\n", WSAGetLastError(), 0, LOG_ERROR
);
51 GenerateChars(SOCKET sock
)
53 CHAR chars
[NUM_CHARS
];
59 /* fill the array with printable characters */
60 for (charIndex
= 0, i
= ASCII_START
; i
<= ASCII_END
; charIndex
++, i
++)
61 chars
[charIndex
] = (char)i
;
66 /* reset the loop when we hit the last char */
67 if (loopIndex
== NUM_CHARS
)
70 /* fill a line array to send */
71 charIndex
= loopIndex
;
72 for (i
=0; i
< LINESIZE
- 2; i
++)
74 line
[i
] = chars
[charIndex
];
76 /* if we hit the end char, reset it */
77 if (chars
[charIndex
] == chars
[NUM_CHARS
- 1])
82 line
[LINESIZE
- 2] = '\r';
83 line
[LINESIZE
- 1] = '\n';
85 if (!SendLine(sock
, line
))
88 /* start printing from next char in the array */
96 ChargenHandler(VOID
* sock_
)
99 SOCKET sock
= (SOCKET
)sock_
;
101 if (!GenerateChars(sock
))
103 LogEvent(L
"Chargen: Char generation failed", 0, 0, LOG_FILE
);
107 LogEvent(L
"Chargen: Shutting connection down...", 0, 0, LOG_FILE
);
108 if (ShutdownConnection(sock
, FALSE
))
110 LogEvent(L
"Chargen: Connection is down", 0, 0, LOG_FILE
);
114 LogEvent(L
"Chargen: Connection shutdown failed", 0, 0, LOG_FILE
);
118 LogEvent(L
"Chargen: Terminating thread", 0, 0, LOG_FILE
);