From 708722cfd961b517089b7901b629a0d588b8ef79 Mon Sep 17 00:00:00 2001 From: Ged Murphy Date: Wed, 28 Sep 2005 22:33:55 +0000 Subject: [PATCH] - Added daytime and quote of the day services - Use universal header and remove needless seperate dirs - Many bugfixes - Added to build, not yet included in install as it's still in the early stages Tested in WinXP using both Windows and Linux clients. Not yet tried in ROS, svn path=/trunk/; revision=18142 --- reactos/apps/utils/net/directory.xml | 23 ++++---- .../utils/net/tcpsvcs/{chargen => }/chargen.c | 51 +++++++++-------- .../apps/utils/net/tcpsvcs/chargen/chargen.h | 8 --- .../utils/net/tcpsvcs/chargen/chargen.xml | 9 --- reactos/apps/utils/net/tcpsvcs/daytime.c | 46 +++++++++++++++ .../apps/utils/net/tcpsvcs/daytime/.gitignore | 0 .../apps/utils/net/tcpsvcs/discard/.gitignore | 0 .../apps/utils/net/tcpsvcs/{echo => }/echo.c | 8 +-- reactos/apps/utils/net/tcpsvcs/echo/echo.h | 4 -- reactos/apps/utils/net/tcpsvcs/qotd.c | 56 +++++++++++++++++++ .../apps/utils/net/tcpsvcs/qotd/.gitignore | 0 .../net/tcpsvcs/{skelserver => }/skelserver.c | 52 +++++++++-------- .../utils/net/tcpsvcs/skelserver/skelserver.h | 7 --- reactos/apps/utils/net/tcpsvcs/tcpsvcs.c | 47 ++++++++++------ reactos/apps/utils/net/tcpsvcs/tcpsvcs.h | 46 ++++++++++++++- reactos/apps/utils/net/tcpsvcs/tcpsvcs.rc | 7 +++ reactos/apps/utils/net/tcpsvcs/tcpsvcs.xml | 28 +++++----- 17 files changed, 267 insertions(+), 125 deletions(-) rename reactos/apps/utils/net/tcpsvcs/{chargen => }/chargen.c (64%) delete mode 100644 reactos/apps/utils/net/tcpsvcs/chargen/chargen.h delete mode 100644 reactos/apps/utils/net/tcpsvcs/chargen/chargen.xml create mode 100644 reactos/apps/utils/net/tcpsvcs/daytime.c delete mode 100644 reactos/apps/utils/net/tcpsvcs/daytime/.gitignore delete mode 100644 reactos/apps/utils/net/tcpsvcs/discard/.gitignore rename reactos/apps/utils/net/tcpsvcs/{echo => }/echo.c (93%) delete mode 100644 reactos/apps/utils/net/tcpsvcs/echo/echo.h create mode 100644 reactos/apps/utils/net/tcpsvcs/qotd.c delete mode 100644 reactos/apps/utils/net/tcpsvcs/qotd/.gitignore rename reactos/apps/utils/net/tcpsvcs/{skelserver => }/skelserver.c (72%) delete mode 100644 reactos/apps/utils/net/tcpsvcs/skelserver/skelserver.h create mode 100644 reactos/apps/utils/net/tcpsvcs/tcpsvcs.rc diff --git a/reactos/apps/utils/net/directory.xml b/reactos/apps/utils/net/directory.xml index eb26921bf6a..df5702ef75a 100644 --- a/reactos/apps/utils/net/directory.xml +++ b/reactos/apps/utils/net/directory.xml @@ -1,30 +1,33 @@ - + - + - + - + - + - + - + + + + - + - + - + diff --git a/reactos/apps/utils/net/tcpsvcs/chargen/chargen.c b/reactos/apps/utils/net/tcpsvcs/chargen.c similarity index 64% rename from reactos/apps/utils/net/tcpsvcs/chargen/chargen.c rename to reactos/apps/utils/net/tcpsvcs/chargen.c index 76b9714b7d1..47ba87c6a25 100644 --- a/reactos/apps/utils/net/tcpsvcs/chargen/chargen.c +++ b/reactos/apps/utils/net/tcpsvcs/chargen.c @@ -1,8 +1,7 @@ #include #include #include -#include "chargen.h" -#include "../skelserver/skelserver.h" +#include "tcpsvcs.h" DWORD WINAPI ChargenHandler(VOID* Sock_) { @@ -10,12 +9,12 @@ DWORD WINAPI ChargenHandler(VOID* Sock_) SOCKET Sock = (SOCKET)Sock_; if (!GenerateChars(Sock)) { - _tprintf(_T("Echo incoming packets failed\n")); + _tprintf(_T("Char generation failed\n")); Retval = 3; } _tprintf(_T("Shutting connection down...\n")); - if (ShutdownConnection(Sock)) { + if (ShutdownConnection(Sock, FALSE)) { _tprintf(_T("Connection is down.\n")); } else @@ -23,6 +22,8 @@ DWORD WINAPI ChargenHandler(VOID* Sock_) _tprintf(_T("Connection shutdown failed\n")); Retval = 3; } + _tprintf(_T("Terminating thread\n")); + ExitThread(0); return Retval; } @@ -35,6 +36,7 @@ BOOL GenerateChars(SOCKET Sock) loopIndex; /* line loop */ char ring[END-START]; char *endring; + BOOL bLoop = TRUE; /* fill ring with printable characters */ for (charIndex=0, i=START; i<=END; charIndex++, i++) @@ -45,7 +47,7 @@ BOOL GenerateChars(SOCKET Sock) /* where we will start output from */ loopIndex = 0; - while (1) + while (bLoop) { /* if the loop index is equal to number of chars previously * printed, start the loop from the beginning */ @@ -56,21 +58,29 @@ BOOL GenerateChars(SOCKET Sock) charIndex = loopIndex; for (i=0; i 0) { + /*FIXME: need to establish if peer closes connection, + not just report a socket error */ + if (RetVal > 0) + { SentBytes += RetVal; + return TRUE; } - else if (RetVal == SOCKET_ERROR) { + else if (RetVal == SOCKET_ERROR) + { + _tprintf(("Socket error\n")); return FALSE; } else - { - /* Client closed connection before we could reply to - all the data it sent, so quit early. */ - _tprintf(_T("Peer unexpectedly dropped connection!\n")); - return FALSE; - } + _tprintf(("unknown error\n")); + //WSAGetLastError() _tprintf(("Connection closed by peer.\n")); return TRUE; } - - - - - diff --git a/reactos/apps/utils/net/tcpsvcs/chargen/chargen.h b/reactos/apps/utils/net/tcpsvcs/chargen/chargen.h deleted file mode 100644 index 2c318346b2d..00000000000 --- a/reactos/apps/utils/net/tcpsvcs/chargen/chargen.h +++ /dev/null @@ -1,8 +0,0 @@ -#define START 32 -#define END 126 -#define LINESIZ 72 -#define BUF 1024 - -DWORD WINAPI ChargenHandler(VOID* Sock_); -BOOL GenerateChars(SOCKET Sock); -BOOL SendChar(SOCKET Sock, CHAR c); diff --git a/reactos/apps/utils/net/tcpsvcs/chargen/chargen.xml b/reactos/apps/utils/net/tcpsvcs/chargen/chargen.xml deleted file mode 100644 index 0adefdf7866..00000000000 --- a/reactos/apps/utils/net/tcpsvcs/chargen/chargen.xml +++ /dev/null @@ -1,9 +0,0 @@ - - . - - kernel32 - iphlpapi - ws2_32 - chargen/chargen.c - - diff --git a/reactos/apps/utils/net/tcpsvcs/daytime.c b/reactos/apps/utils/net/tcpsvcs/daytime.c new file mode 100644 index 00000000000..271afdfc46b --- /dev/null +++ b/reactos/apps/utils/net/tcpsvcs/daytime.c @@ -0,0 +1,46 @@ +#include +#include +#include +#include +#include "tcpsvcs.h" + +DWORD WINAPI DaytimeHandler(VOID* Sock_) +{ + struct tm *newtime; + time_t aclock; + TCHAR *pszTime; + DWORD Retval = 0; + SOCKET Sock = (SOCKET)Sock_; + + time(&aclock); + newtime = localtime(&aclock); + pszTime = _tasctime(newtime); + + SendTime(Sock, pszTime); + + _tprintf(_T("Shutting connection down...\n")); + if (ShutdownConnection(Sock, FALSE)) + _tprintf(_T("Connection is down.\n")); + else + { + _tprintf(_T("Connection shutdown failed\n")); + Retval = 3; + } + _tprintf(_T("Terminating thread\n")); + ExitThread(0); + + return Retval; +} + + +BOOL SendTime(SOCKET Sock, TCHAR *time) +{ + INT StringSize = strlen(time); + INT RetVal = send(Sock, time, sizeof(TCHAR) * StringSize, 0); + + if (RetVal == SOCKET_ERROR) + return FALSE; + + _tprintf(("Connection closed by peer.\n")); + return TRUE; +} diff --git a/reactos/apps/utils/net/tcpsvcs/daytime/.gitignore b/reactos/apps/utils/net/tcpsvcs/daytime/.gitignore deleted file mode 100644 index e69de29bb2d..00000000000 diff --git a/reactos/apps/utils/net/tcpsvcs/discard/.gitignore b/reactos/apps/utils/net/tcpsvcs/discard/.gitignore deleted file mode 100644 index e69de29bb2d..00000000000 diff --git a/reactos/apps/utils/net/tcpsvcs/echo/echo.c b/reactos/apps/utils/net/tcpsvcs/echo.c similarity index 93% rename from reactos/apps/utils/net/tcpsvcs/echo/echo.c rename to reactos/apps/utils/net/tcpsvcs/echo.c index 98929f84137..3fc51fd358b 100644 --- a/reactos/apps/utils/net/tcpsvcs/echo/echo.c +++ b/reactos/apps/utils/net/tcpsvcs/echo.c @@ -1,8 +1,7 @@ #include #include #include -#include "echo.h" -#include "../skelserver/skelserver.h" +#include "tcpsvcs.h" DWORD WINAPI EchoHandler(VOID* Sock_) { @@ -15,7 +14,7 @@ DWORD WINAPI EchoHandler(VOID* Sock_) } _tprintf(_T("Shutting connection down...\n")); - if (ShutdownConnection(Sock)) { + if (ShutdownConnection(Sock, TRUE)) { _tprintf(_T("Connection is down.\n")); } else @@ -23,6 +22,8 @@ DWORD WINAPI EchoHandler(VOID* Sock_) _tprintf(_T("Connection shutdown failed\n")); Retval = 3; } + _tprintf(_T("Terminating thread\n")); + ExitThread(0); return Retval; } @@ -68,4 +69,3 @@ BOOL EchoIncomingPackets(SOCKET Sock) _tprintf(("Connection closed by peer.\n")); return TRUE; } - diff --git a/reactos/apps/utils/net/tcpsvcs/echo/echo.h b/reactos/apps/utils/net/tcpsvcs/echo/echo.h deleted file mode 100644 index 5c09b722114..00000000000 --- a/reactos/apps/utils/net/tcpsvcs/echo/echo.h +++ /dev/null @@ -1,4 +0,0 @@ -#define BUF 1024 - -DWORD WINAPI EchoHandler(VOID* Sock_); -BOOL EchoIncomingPackets(SOCKET Sock); diff --git a/reactos/apps/utils/net/tcpsvcs/qotd.c b/reactos/apps/utils/net/tcpsvcs/qotd.c new file mode 100644 index 00000000000..8eb222312f1 --- /dev/null +++ b/reactos/apps/utils/net/tcpsvcs/qotd.c @@ -0,0 +1,56 @@ +#include +#include +#include +#include +#include "tcpsvcs.h" + +//these need putting in an RC file. +TCHAR Quotes[][MAX_QUOTE_BUF] = { + _T("\"I have a penchant for mischief, property damage, stalking and cheesecake, of course\" - kjk hyperion"), + _T("\"Wow! I fixed a setmenu bug.\" - jimtabor"), + _T("\"if the code is broken though, your free to call it ur own\" - Alex Ionescu"), + _T("\"i don't know about any bug; none exist; ReactOS is prefect\" - filip2307"), + _T("\"if you were kernel code, cutler would rewrite you.\" - Alex Ionescu"), + _T("\"Looks like Hartmut is cleaning out his WC. working copy, that is\" - WaxDragon") +}; + +DWORD WINAPI QotdHandler(VOID* Sock_) +{ + DWORD Retval = 0; + SOCKET Sock; + INT NumOfQuotes; + + Sock = (SOCKET)Sock_; + NumOfQuotes = sizeof(Quotes) / MAX_QUOTE_BUF; + + SendQuote(Sock, Quotes[1]); + + _tprintf(_T("Shutting connection down...\n")); + if (ShutdownConnection(Sock, FALSE)) + _tprintf(_T("Connection is down.\n")); + else + { + _tprintf(_T("Connection shutdown failed\n")); + Retval = 3; + } + _tprintf(_T("Terminating thread\n")); + ExitThread(0); + + return Retval; +} + + +BOOL SendQuote(SOCKET Sock, TCHAR* Quote) +{ + INT StringSize; + INT RetVal; + + StringSize = strlen(Quote); + RetVal = send(Sock, Quote, sizeof(TCHAR) * StringSize, 0); + + if (RetVal == SOCKET_ERROR) + return FALSE; + + _tprintf(("Connection closed by peer.\n")); + return TRUE; +} diff --git a/reactos/apps/utils/net/tcpsvcs/qotd/.gitignore b/reactos/apps/utils/net/tcpsvcs/qotd/.gitignore deleted file mode 100644 index e69de29bb2d..00000000000 diff --git a/reactos/apps/utils/net/tcpsvcs/skelserver/skelserver.c b/reactos/apps/utils/net/tcpsvcs/skelserver.c similarity index 72% rename from reactos/apps/utils/net/tcpsvcs/skelserver/skelserver.c rename to reactos/apps/utils/net/tcpsvcs/skelserver.c index a70e044a5f0..cfce714e42d 100644 --- a/reactos/apps/utils/net/tcpsvcs/skelserver/skelserver.c +++ b/reactos/apps/utils/net/tcpsvcs/skelserver.c @@ -1,8 +1,7 @@ #include #include #include -#include "../tcpsvcs.h" -#include "skelserver.h" +#include "tcpsvcs.h" DWORD WINAPI StartServer(LPVOID lpParam) @@ -10,9 +9,9 @@ DWORD WINAPI StartServer(LPVOID lpParam) const TCHAR* HostIP = "127.0.0.1"; DWORD RetVal; WSADATA wsaData; - PMYDATA pData; + PSERVICES pServices; - pData = (PMYDATA)lpParam; + pServices = (PSERVICES)lpParam; if ((RetVal = WSAStartup(MAKEWORD(2, 2), &wsaData)) != 0) { @@ -20,20 +19,22 @@ DWORD WINAPI StartServer(LPVOID lpParam) return -1; } - SOCKET ListeningSocket = SetUpListener(HostIP, htons(pData->Port)); + SOCKET ListeningSocket = SetUpListener(HostIP, htons(pServices->Port)); if (ListeningSocket == INVALID_SOCKET) { _tprintf(_T("error setting up socket\n")); return 3; } - printf("Waiting for connections...\n"); + _tprintf(_T("Waiting for connections...\n")); while (1) { - AcceptConnections(ListeningSocket, pData->Service); + AcceptConnections(ListeningSocket, pServices->Service); printf("Acceptor restarting...\n"); } + /* won't see this yet as we kill the service */ + _tprintf(_T("Detaching Winsock2...\n")); WSACleanup(); return 0; } @@ -43,25 +44,21 @@ SOCKET SetUpListener(const char* ServAddr, int Port) { SOCKET Sock; SOCKADDR_IN Server; - DWORD InterfaceAddr = inet_addr(ServAddr); - if (InterfaceAddr != INADDR_NONE) + Sock = socket(AF_INET, SOCK_STREAM, 0); + if (Sock != INVALID_SOCKET) { - Sock = socket(AF_INET, SOCK_STREAM, 0); - if (Sock != INVALID_SOCKET) + Server.sin_family = AF_INET; + Server.sin_addr.s_addr = htonl(INADDR_ANY); + Server.sin_port = Port; + if (bind(Sock, (SOCKADDR*)&Server, sizeof(SOCKADDR_IN)) != SOCKET_ERROR) { - Server.sin_family = AF_INET; - Server.sin_addr.s_addr = InterfaceAddr; - Server.sin_port = Port; - if (bind(Sock, (SOCKADDR*)&Server, sizeof(SOCKADDR_IN)) != SOCKET_ERROR) - { - listen(Sock, SOMAXCONN); - return Sock; - } - else - printf("bind() failed\n"); - + listen(Sock, SOMAXCONN); + return Sock; } + else + printf("bind() failed\n"); + } return INVALID_SOCKET; } @@ -83,7 +80,7 @@ VOID AcceptConnections(SOCKET ListeningSocket, LPTHREAD_START_ROUTINE Service) { _tprintf(_T("Accepted connection from %s:%d\n"), inet_ntoa(Client.sin_addr), ntohs(Client.sin_port)); - + _tprintf(_T("About to create thread\n")); CreateThread(0, 0, Service, (void*)Sock, 0, &ThreadID); } else @@ -94,20 +91,23 @@ VOID AcceptConnections(SOCKET ListeningSocket, LPTHREAD_START_ROUTINE Service) } } -BOOL ShutdownConnection(SOCKET Sock) +BOOL ShutdownConnection(SOCKET Sock, BOOL bRec) { /* Disallow any further data sends. This will tell the other side that we want to go away now. If we skip this step, we don't shut the connection down nicely. */ if (shutdown(Sock, SD_SEND) == SOCKET_ERROR) + { + _tprintf(_T("Error in shutdown")); return FALSE; + } /* Receive any extra data still sitting on the socket. After all data is received, this call will block until the remote host acknowledges the TCP control packet sent by the shutdown above. Then we'll get a 0 back from recv, signalling that the remote host has closed its side of the connection. */ - while (1) + if (bRec) { char ReadBuffer[BUF]; int NewBytes = recv(Sock, ReadBuffer, BUF, 0); @@ -115,8 +115,6 @@ BOOL ShutdownConnection(SOCKET Sock) return FALSE; else if (NewBytes != 0) _tprintf(_T("FYI, received %d unexpected bytes during shutdown\n"), NewBytes); - else - break; } /* Close the socket. */ diff --git a/reactos/apps/utils/net/tcpsvcs/skelserver/skelserver.h b/reactos/apps/utils/net/tcpsvcs/skelserver/skelserver.h deleted file mode 100644 index d9326da6811..00000000000 --- a/reactos/apps/utils/net/tcpsvcs/skelserver/skelserver.h +++ /dev/null @@ -1,7 +0,0 @@ -#define BUF 1024 - -DWORD WINAPI StartServer(LPVOID lpParam); -SOCKET SetUpListener(const char* ServAddr, int Port); -VOID AcceptConnections(SOCKET ListeningSocket, LPTHREAD_START_ROUTINE Service); -BOOL EchoIncomingPackets(SOCKET sd); -BOOL ShutdownConnection(SOCKET Sock); diff --git a/reactos/apps/utils/net/tcpsvcs/tcpsvcs.c b/reactos/apps/utils/net/tcpsvcs/tcpsvcs.c index 435a987027b..9dbb9e028d0 100644 --- a/reactos/apps/utils/net/tcpsvcs/tcpsvcs.c +++ b/reactos/apps/utils/net/tcpsvcs/tcpsvcs.c @@ -2,37 +2,49 @@ #include #include #include "tcpsvcs.h" -#include "skelserver/skelserver.h" -#include "echo/echo.h" -#include "chargen/chargen.h" + +LPTHREAD_START_ROUTINE +ServiceHandler[NUM_SERVICES] = { + EchoHandler, + ChargenHandler, + DaytimeHandler, + NULL, + QotdHandler +}; + +INT ServicePort[NUM_SERVICES] = { + ECHO_PORT, + CHARGEN_PORT, + DAYTIME_PORT, + DISCARD_PORT, + QOTD_PORT +}; int main(int argc, char *argv[]) { - PMYDATA pData[MAX_THREADS]; - DWORD dwThreadId[MAX_THREADS]; - HANDLE hThread[MAX_THREADS]; + PSERVICES pServices[NUM_SERVICES]; + DWORD dwThreadId[NUM_SERVICES]; + HANDLE hThread[NUM_SERVICES]; INT i; /* Create MAX_THREADS worker threads. */ - for( i=0; iPort = ECHO_PORT; - pData[0]->Service = EchoHandler; - pData[1]->Port = CHARGEN_PORT; - pData[1]->Service = ChargenHandler; + pServices[i]->Service = ServiceHandler[i]; + pServices[i]->Port = ServicePort[i]; hThread[i] = CreateThread( NULL, // default security attributes 0, // use default stack size StartServer, // thread function - pData[i], // argument to thread function + pServices[i], // argument to thread function 0, // use default creation flags &dwThreadId[i]); // returns the thread identifier @@ -44,14 +56,13 @@ int main(int argc, char *argv[]) } /* Wait until all threads have terminated. */ - WaitForMultipleObjects(MAX_THREADS, hThread, TRUE, INFINITE); + WaitForMultipleObjects(NUM_SERVICES, hThread, TRUE, INFINITE); /* Close all thread handles upon completion. */ - for(i=0; i diff --git a/reactos/apps/utils/net/tcpsvcs/tcpsvcs.xml b/reactos/apps/utils/net/tcpsvcs/tcpsvcs.xml index 152f4e6a2a9..336838a9e47 100644 --- a/reactos/apps/utils/net/tcpsvcs/tcpsvcs.xml +++ b/reactos/apps/utils/net/tcpsvcs/tcpsvcs.xml @@ -1,13 +1,15 @@ - - . - - kernel32 - iphlpapi - ws2_32 - shlwapi - tcpsvcs.c - skelserver/skelserver.c - echo/echo.c - chargen/chargen.c - - + + . + + kernel32 + iphlpapi + ws2_32 + shlwapi + tcpsvcs.c + skelserver.c + echo.c + chargen.c + daytime.c + qotd.c + +