ReactOS
0.4.15-dev-4934-gfd1e799
echo.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/echo.c
5
* PURPOSE: Returns whatever input the client sends
6
* COPYRIGHT: Copyright 2005 - 2008 Ged Murphy <gedmurphy@reactos.org>
7
*
8
*/
9
10
#include "
tcpsvcs.h
"
11
12
#define RECV_BUF 1024
13
14
static
BOOL
15
EchoIncomingPackets
(
SOCKET
sock
)
16
{
17
CHAR
readBuffer[
RECV_BUF
];
18
WCHAR
logBuf[256];
19
INT
totalSentBytes;
20
INT
readBytes;
21
INT
retVal;
22
23
do
24
{
25
readBytes =
recv
(
sock
, readBuffer,
RECV_BUF
, 0);
26
if
(readBytes > 0)
27
{
28
swprintf
(logBuf,
L
"Received %d bytes from client"
, readBytes);
29
LogEvent
(logBuf, 0, 0,
LOG_FILE
);
30
31
totalSentBytes = 0;
32
while
(!
bShutdown
&& totalSentBytes < readBytes)
33
{
34
retVal =
send
(
sock
, readBuffer + totalSentBytes, readBytes - totalSentBytes, 0);
35
if
(retVal > 0)
36
{
37
swprintf
(logBuf,
L
"Sent %d bytes back to client"
, retVal);
38
LogEvent
(logBuf, 0, 0,
LOG_FILE
);
39
totalSentBytes += retVal;
40
}
41
else
if
(retVal ==
SOCKET_ERROR
)
42
{
43
LogEvent
(
L
"Echo: socket error"
,
WSAGetLastError
(), 0,
LOG_ERROR
);
44
return
FALSE
;
45
}
46
else
47
{
48
/* Client closed connection before we could reply to
49
all the data it sent, so quit early. */
50
LogEvent
(
L
"Peer unexpectedly dropped connection!"
, 0, 0,
LOG_FILE
);
51
return
FALSE
;
52
}
53
}
54
}
55
else
if
(readBytes ==
SOCKET_ERROR
)
56
{
57
LogEvent
(
L
"Echo: socket error"
,
WSAGetLastError
(), 0,
LOG_ERROR
);
58
return
FALSE
;
59
}
60
}
while
((readBytes != 0) && (!
bShutdown
));
61
62
if
(!
bShutdown
)
63
LogEvent
(
L
"Echo: Connection closed by peer"
, 0, 0,
LOG_FILE
);
64
65
return
TRUE
;
66
}
67
68
DWORD
WINAPI
69
EchoHandler
(
VOID
* sock_)
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
}
bShutdown
volatile BOOL bShutdown
Definition:
tcpsvcs.c:16
SOCKET_ERROR
#define SOCKET_ERROR
Definition:
winsock.h:333
TRUE
#define TRUE
Definition:
types.h:120
CHAR
char CHAR
Definition:
xmlstorage.h:175
tcpsvcs.h
INT
int32_t INT
Definition:
typedefs.h:58
L
#define L(x)
Definition:
ntvdm.h:50
FALSE
#define FALSE
Definition:
types.h:117
BOOL
unsigned int BOOL
Definition:
ntddk_ex.h:94
RECV_BUF
#define RECV_BUF
Definition:
echo.c:12
LOG_ERROR
#define LOG_ERROR
Definition:
tcpsvcs.h:16
ExitThread
VOID WINAPI ExitThread(IN DWORD uExitCode)
Definition:
thread.c:365
EchoIncomingPackets
static BOOL EchoIncomingPackets(SOCKET sock)
Definition:
echo.c:15
WSAGetLastError
INT WSAAPI WSAGetLastError(VOID)
Definition:
dllmain.c:112
WCHAR
__wchar_t WCHAR
Definition:
xmlstorage.h:180
send
INT WSAAPI send(IN SOCKET s, IN CONST CHAR FAR *buf, IN INT len, IN INT flags)
Definition:
send.c:23
WINAPI
#define WINAPI
Definition:
msvc.h:6
DWORD
unsigned long DWORD
Definition:
ntddk_ex.h:95
ShutdownConnection
BOOL ShutdownConnection(SOCKET sock, BOOL bRec)
Definition:
skelserver.c:126
swprintf
int _cdecl swprintf(const WCHAR *,...)
EchoHandler
DWORD WINAPI EchoHandler(VOID *sock_)
Definition:
echo.c:69
LogEvent
VOID LogEvent(LPCWSTR lpMsg, DWORD errNum, DWORD exitCode, UINT flags)
Definition:
log.c:196
_LOG_FILE
Definition:
winstation.c:22
sock
Definition:
tcpcore.h:1455
SOCKET
UINT_PTR SOCKET
Definition:
winsock.h:47
void
Definition:
nsiface.idl:2306
recv
INT WSAAPI recv(IN SOCKET s, OUT CHAR FAR *buf, IN INT len, IN INT flags)
Definition:
recv.c:23
base
services
tcpsvcs
echo.c
Generated on Wed Aug 17 2022 06:05:26 for ReactOS by
1.8.15