3 * Copyright (C) 2005 ReactOS Team
5 * LICENCE: GPL - See COPYING in the top level directory
6 * PROJECT: ReactOS simple TCP/IP services
7 * FILE: apps/utils/net/tcpsvcs/chargen.c
8 * PURPOSE: Provide CharGen, Daytime, Discard, Echo, and Qotd services
9 * PROGRAMMERS: Ged Murphy (gedmurphy@gmail.com)
20 extern BOOL bShutDown
;
22 DWORD WINAPI
ChargenHandler(VOID
* Sock_
)
25 SOCKET Sock
= (SOCKET
)Sock_
;
27 if (!GenerateChars(Sock
))
29 LogEvent(_T("Chargen: Char generation failed\n"), 0, FALSE
);
33 LogEvent(_T("Chargen: Shutting connection down...\n"), 0, FALSE
);
34 if (ShutdownConnection(Sock
, FALSE
))
35 LogEvent(_T("Chargen: Connection is down.\n"), 0, FALSE
);
38 LogEvent(_T("Chargen: Connection shutdown failed\n"), 0, FALSE
);
42 LogEvent(_T("Chargen: Terminating thread\n"), 0, FALSE
);
48 BOOL
GenerateChars(SOCKET Sock
)
51 int charIndex
; /* internal loop */
52 int loopIndex
; /* line loop */
57 /* fill ring with printable characters */
58 for (charIndex
=0, i
=START
; i
<=END
; charIndex
++, i
++)
59 ring
[charIndex
] = (char)i
;
60 /* save the address of the end character in the ring */
61 endring
= &ring
[charIndex
];
63 /* where we will start output from */
67 /* if the loop index is equal to the last char,
68 * start the loop again from the beginning */
69 if (loopIndex
== END
-START
)
72 /* start printing from char controled by loopIndex */
73 charIndex
= loopIndex
;
74 for (i
=0; i
< LINESIZ
- 2; i
++)
76 Line
[i
] = ring
[charIndex
];
78 if (ring
[charIndex
] == *endring
)
84 Line
[LINESIZ
- 2] = L
'\r';
85 Line
[LINESIZ
- 1] = L
'\n';
87 if (! SendLine(Sock
, Line
))
90 /* increment loop index to start printing from next char in ring */
100 BOOL
SendLine(SOCKET Sock
, TCHAR
* Line
)
106 LineSize
= sizeof(TCHAR
) * LINESIZ
;
109 RetVal
= send(Sock
, Line
, LineSize
, 0);
110 /*FIXME: need to establish if peer closes connection,
111 not just report a socket error */
114 if (RetVal
!= LineSize
)
116 LogEvent(_T("Chargen: Not sent enough bytes\n"), 0, FALSE
);
122 else if (RetVal
== SOCKET_ERROR
)
124 LogEvent(_T("Chargen: Socket error\n"), 0, FALSE
);
128 LogEvent(_T("Chargen: unknown error\n"), 0, FALSE
);
131 LogEvent(_T("Chargen: Connection closed by peer.\n"), 0, FALSE
);