Start to implement the beginneings of tcpsvcs.exe. Just ideas at the moment, subject...
authorGed Murphy <gedmurphy@reactos.org>
Tue, 27 Sep 2005 21:44:24 +0000 (21:44 +0000)
committerGed Murphy <gedmurphy@reactos.org>
Tue, 27 Sep 2005 21:44:24 +0000 (21:44 +0000)
Working code for basic echo and chargen included. Tested in Windows from both Windows and Linux clients
Run from cmd, not incorporated into services.exe at the moment
Builds ok, but not included into build system just yet.

svn path=/trunk/; revision=18129

13 files changed:
reactos/apps/utils/net/tcpsvcs/chargen/chargen.c [new file with mode: 0644]
reactos/apps/utils/net/tcpsvcs/chargen/chargen.h [new file with mode: 0644]
reactos/apps/utils/net/tcpsvcs/chargen/chargen.xml [new file with mode: 0644]
reactos/apps/utils/net/tcpsvcs/daytime/.gitignore [new file with mode: 0644]
reactos/apps/utils/net/tcpsvcs/discard/.gitignore [new file with mode: 0644]
reactos/apps/utils/net/tcpsvcs/echo/echo.c [new file with mode: 0644]
reactos/apps/utils/net/tcpsvcs/echo/echo.h [new file with mode: 0644]
reactos/apps/utils/net/tcpsvcs/qotd/.gitignore [new file with mode: 0644]
reactos/apps/utils/net/tcpsvcs/skelserver/skelserver.c [new file with mode: 0644]
reactos/apps/utils/net/tcpsvcs/skelserver/skelserver.h [new file with mode: 0644]
reactos/apps/utils/net/tcpsvcs/tcpsvcs.c [new file with mode: 0644]
reactos/apps/utils/net/tcpsvcs/tcpsvcs.h [new file with mode: 0644]
reactos/apps/utils/net/tcpsvcs/tcpsvcs.xml [new file with mode: 0644]

diff --git a/reactos/apps/utils/net/tcpsvcs/chargen/chargen.c b/reactos/apps/utils/net/tcpsvcs/chargen/chargen.c
new file mode 100644 (file)
index 0000000..90b27a8
--- /dev/null
@@ -0,0 +1,104 @@
+#include <stdio.h>\r
+#include <winsock2.h>\r
+#include <tchar.h>\r
+#include "chargen.h"\r
+#include "../skelserver/skelserver.h"\r
+\r
+DWORD WINAPI ChargenHandler(VOID* Sock_)\r
+{\r
+    DWORD Retval = 0;\r
+    SOCKET Sock = (SOCKET)Sock_;\r
+\r
+    if (!GenerateChars(Sock)) {\r
+        _tprintf(_T("Echo incoming packets failed\n"));\r
+        Retval = 3;\r
+    }\r
+\r
+    _tprintf(_T("Shutting connection down...\n"));\r
+    if (ShutdownConnection(Sock)) {\r
+        _tprintf(_T("Connection is down.\n"));\r
+    }\r
+    else\r
+    {\r
+        _tprintf(_T("Connection shutdown failed\n"));\r
+        Retval = 3;\r
+    }\r
+\r
+    return Retval;\r
+}\r
+\r
+\r
+BOOL GenerateChars(SOCKET Sock)\r
+{\r
+    int i,\r
+        charIndex, /* internal loop */\r
+        loopIndex; /* line loop */\r
+    char ring[END-START];\r
+    char *endring;\r
+\r
+    /* fill ring with printable characters */\r
+    for (charIndex=0, i=START; i<=END; charIndex++, i++)\r
+        ring[charIndex] = i;\r
+    /* establish the end character in the ring */\r
+    endring = &ring[charIndex];\r
+\r
+    /* where we will start output from */\r
+    loopIndex = 0;\r
+\r
+    while (1)\r
+    {\r
+        /* if the loop index is equal to number of chars previously\r
+         * printed, start the loop from the beginning */\r
+        if (loopIndex == END-START)\r
+            loopIndex = 0;\r
+\r
+        /* start printing from char controled by loopIndex */\r
+        charIndex = loopIndex;\r
+        for (i=0; i<LINESIZ; i++)\r
+        {\r
+            SendChar(Sock, ring[charIndex]);\r
+            /* if current char equal last char, reset */\r
+            if (ring[charIndex] == *endring)\r
+                charIndex = 0;\r
+            else\r
+                charIndex++;\r
+        }\r
+        SendChar(Sock, L'\r');\r
+        SendChar(Sock, L'\n');\r
+\r
+        /* increment loop index to start printing from next char in ring */\r
+        loopIndex++;\r
+    }\r
+\r
+    return 0;\r
+}\r
+\r
+BOOL SendChar(SOCKET Sock, TCHAR c)\r
+{\r
+    INT Temp;\r
+    INT SentBytes;\r
+\r
+    SentBytes = 0;\r
+    Temp = send(Sock, &c, sizeof(TCHAR), 0);\r
+    if (Temp > 0) {\r
+        SentBytes += Temp;\r
+    }\r
+    else if (Temp == SOCKET_ERROR) {\r
+        return FALSE;\r
+    }\r
+    else\r
+    {\r
+        /* Client closed connection before we could reply to\r
+           all the data it sent, so quit early. */\r
+        _tprintf(_T("Peer unexpectedly dropped connection!\n"));\r
+        return FALSE;\r
+    }\r
+\r
+    _tprintf(("Connection closed by peer.\n"));\r
+    return TRUE;\r
+}\r
+\r
+\r
+\r
+\r
+\r
diff --git a/reactos/apps/utils/net/tcpsvcs/chargen/chargen.h b/reactos/apps/utils/net/tcpsvcs/chargen/chargen.h
new file mode 100644 (file)
index 0000000..5155c55
--- /dev/null
@@ -0,0 +1,8 @@
+#define START 32\r
+#define END 126\r
+#define LINESIZ 72\r
+#define BUF 1024\r
+\r
+DWORD WINAPI ChargenHandler(VOID* Sock_);\r
+BOOL GenerateChars(SOCKET Sock);\r
+BOOL SendChar(SOCKET Sock, CHAR c);\r
diff --git a/reactos/apps/utils/net/tcpsvcs/chargen/chargen.xml b/reactos/apps/utils/net/tcpsvcs/chargen/chargen.xml
new file mode 100644 (file)
index 0000000..0adefdf
--- /dev/null
@@ -0,0 +1,9 @@
+<module name="chargen" type="win32cui" installbase="system32" installname="chargen.exe">\r
+    <include base="arp">.</include>\r
+    <define name="__USE_W32API" />\r
+    <library>kernel32</library>\r
+    <library>iphlpapi</library>\r
+    <library>ws2_32</library>\r
+    <file>chargen/chargen.c</file>\r
+</module>\r
+\r
diff --git a/reactos/apps/utils/net/tcpsvcs/daytime/.gitignore b/reactos/apps/utils/net/tcpsvcs/daytime/.gitignore
new file mode 100644 (file)
index 0000000..e69de29
diff --git a/reactos/apps/utils/net/tcpsvcs/discard/.gitignore b/reactos/apps/utils/net/tcpsvcs/discard/.gitignore
new file mode 100644 (file)
index 0000000..e69de29
diff --git a/reactos/apps/utils/net/tcpsvcs/echo/echo.c b/reactos/apps/utils/net/tcpsvcs/echo/echo.c
new file mode 100644 (file)
index 0000000..ef8f5b4
--- /dev/null
@@ -0,0 +1,72 @@
+#include <stdio.h>\r
+#include <winsock2.h>\r
+#include <tchar.h>\r
+#include "echo.h"\r
+#include "../skelserver/skelserver.h"\r
+\r
+// Handles the incoming data by reflecting it back to the sender.\r
+DWORD WINAPI EchoHandler(VOID* Sock_)\r
+{\r
+    DWORD Retval = 0;\r
+    SOCKET Sock = (SOCKET)Sock_;\r
+\r
+    if (!EchoIncomingPackets(Sock)) {\r
+        _tprintf(_T("Echo incoming packets failed\n"));\r
+        Retval = 3;\r
+    }\r
+\r
+    _tprintf(_T("Shutting connection down...\n"));\r
+    if (ShutdownConnection(Sock)) {\r
+        _tprintf(_T("Connection is down.\n"));\r
+    }\r
+    else\r
+    {\r
+        _tprintf(_T("Connection shutdown failed\n"));\r
+        Retval = 3;\r
+    }\r
+\r
+    return Retval;\r
+}\r
+\r
+\r
+\r
+BOOL EchoIncomingPackets(SOCKET Sock)\r
+{\r
+    TCHAR ReadBuffer[BUF];\r
+    INT Temp;\r
+    INT ReadBytes;\r
+    INT SentBytes;\r
+\r
+    do {\r
+        ReadBytes = recv(Sock, ReadBuffer, BUF, 0);\r
+        if (ReadBytes > 0) {\r
+            _tprintf(_T("Received %d bytes from client\n"), ReadBytes);\r
+\r
+            SentBytes = 0;\r
+            while (SentBytes < ReadBytes) {\r
+                Temp = send(Sock, ReadBuffer + SentBytes,\r
+                        ReadBytes - SentBytes, 0);\r
+                if (Temp > 0) {\r
+                    _tprintf(_T("Sent %d bytes back to client\n"), Temp);\r
+                    SentBytes += Temp;\r
+                }\r
+                else if (Temp == SOCKET_ERROR) {\r
+                    return FALSE;\r
+                }\r
+                else {\r
+                    /* Client closed connection before we could reply to\r
+                    // all the data it sent, so quit early. */\r
+                    _tprintf(_T("Peer unexpectedly dropped connection!\n"));\r
+                    return FALSE;\r
+                }\r
+            }\r
+        }\r
+        else if (ReadBytes == SOCKET_ERROR) {\r
+            return FALSE;\r
+        }\r
+    } while (ReadBytes != 0);\r
+\r
+    _tprintf(("Connection closed by peer.\n"));\r
+    return TRUE;\r
+}\r
+\r
diff --git a/reactos/apps/utils/net/tcpsvcs/echo/echo.h b/reactos/apps/utils/net/tcpsvcs/echo/echo.h
new file mode 100644 (file)
index 0000000..3f0d2a7
--- /dev/null
@@ -0,0 +1,4 @@
+#define BUF 1024\r
+\r
+DWORD WINAPI EchoHandler(VOID* Sock_);\r
+BOOL EchoIncomingPackets(SOCKET Sock);\r
diff --git a/reactos/apps/utils/net/tcpsvcs/qotd/.gitignore b/reactos/apps/utils/net/tcpsvcs/qotd/.gitignore
new file mode 100644 (file)
index 0000000..e69de29
diff --git a/reactos/apps/utils/net/tcpsvcs/skelserver/skelserver.c b/reactos/apps/utils/net/tcpsvcs/skelserver/skelserver.c
new file mode 100644 (file)
index 0000000..fa65f40
--- /dev/null
@@ -0,0 +1,127 @@
+#include <stdio.h>\r
+#include <winsock2.h>\r
+#include <tchar.h>\r
+#include "../tcpsvcs.h"\r
+#include "skelserver.h"\r
+\r
+\r
+DWORD WINAPI StartServer(LPVOID lpParam)\r
+{\r
+    const TCHAR* HostIP = "127.0.0.1";\r
+    DWORD RetVal;\r
+    WSADATA wsaData;\r
+    PMYDATA pData;\r
+\r
+    pData = (PMYDATA)lpParam;\r
+\r
+    if ((RetVal = WSAStartup(MAKEWORD(2, 2), &wsaData)) != 0)\r
+    {\r
+        _tprintf(_T("WSAStartup() failed : %lu\n"), RetVal);\r
+        return -1;\r
+    }\r
+\r
+    SOCKET ListeningSocket = SetUpListener(HostIP, htons(pData->Port));\r
+    if (ListeningSocket == INVALID_SOCKET)\r
+    {\r
+        _tprintf(_T("error setting up socket\n"));\r
+        return 3;\r
+    }\r
+\r
+    printf("Waiting for connections...\n");\r
+    while (1)\r
+    {\r
+        AcceptConnections(ListeningSocket, pData->Service);\r
+        printf("Acceptor restarting...\n");\r
+    }\r
+\r
+    WSACleanup();\r
+    return 0;\r
+}\r
+\r
+\r
+SOCKET SetUpListener(const char* ServAddr, int Port)\r
+{\r
+    SOCKET Sock;\r
+    SOCKADDR_IN Server;\r
+    DWORD InterfaceAddr = inet_addr(ServAddr);\r
+\r
+    if (InterfaceAddr != INADDR_NONE)\r
+    {\r
+        Sock = socket(AF_INET, SOCK_STREAM, 0);\r
+        if (Sock != INVALID_SOCKET)\r
+        {\r
+            Server.sin_family = AF_INET;\r
+            Server.sin_addr.s_addr = InterfaceAddr;\r
+            Server.sin_port = Port;\r
+            if (bind(Sock, (SOCKADDR*)&Server, sizeof(SOCKADDR_IN)) != SOCKET_ERROR)\r
+            {\r
+                listen(Sock, SOMAXCONN);\r
+                return Sock;\r
+            }\r
+            else\r
+                printf("bind() failed\n");\r
+\r
+        }\r
+    }\r
+    return INVALID_SOCKET;\r
+}\r
+\r
+\r
+\r
+\r
+VOID AcceptConnections(SOCKET ListeningSocket, LPTHREAD_START_ROUTINE Service)\r
+{\r
+    SOCKADDR_IN Client;\r
+    SOCKET Sock;\r
+    INT nAddrSize = sizeof(Client);\r
+    DWORD ThreadID;\r
+\r
+    while (1)\r
+    {\r
+        Sock = accept(ListeningSocket, (SOCKADDR*)&Client, &nAddrSize);\r
+        if (Sock != INVALID_SOCKET)\r
+        {\r
+            _tprintf(_T("Accepted connection from %s:%d\n"),\r
+                inet_ntoa(Client.sin_addr), ntohs(Client.sin_port));\r
+\r
+            CreateThread(0, 0, Service, (void*)Sock, 0, &ThreadID);\r
+        }\r
+        else\r
+        {\r
+            _tprintf(_T("accept() failed\n"));\r
+            return;\r
+        }\r
+    }\r
+}\r
+\r
+BOOL ShutdownConnection(SOCKET Sock)\r
+{\r
+    /* Disallow any further data sends.  This will tell the other side\r
+       that we want to go away now.  If we skip this step, we don't\r
+       shut the connection down nicely. */\r
+    if (shutdown(Sock, SD_SEND) == SOCKET_ERROR)\r
+        return FALSE;\r
+\r
+      /* Receive any extra data still sitting on the socket.  After all\r
+         data is received, this call will block until the remote host\r
+         acknowledges the TCP control packet sent by the shutdown above.\r
+         Then we'll get a 0 back from recv, signalling that the remote\r
+         host has closed its side of the connection. */\r
+    while (1)\r
+    {\r
+        char ReadBuffer[BUF];\r
+        int NewBytes = recv(Sock, ReadBuffer, BUF, 0);\r
+        if (NewBytes == SOCKET_ERROR)\r
+            return FALSE;\r
+        else if (NewBytes != 0)\r
+            _tprintf(_T("FYI, received %d unexpected bytes during shutdown\n"), NewBytes);\r
+        else\r
+            break;\r
+    }\r
+\r
+    /* Close the socket. */\r
+    if (closesocket(Sock) == SOCKET_ERROR)\r
+        return FALSE;\r
+\r
+    return TRUE;\r
+}\r
diff --git a/reactos/apps/utils/net/tcpsvcs/skelserver/skelserver.h b/reactos/apps/utils/net/tcpsvcs/skelserver/skelserver.h
new file mode 100644 (file)
index 0000000..21305dd
--- /dev/null
@@ -0,0 +1,7 @@
+#define BUF 1024\r
+\r
+DWORD WINAPI StartServer(LPVOID lpParam);\r
+SOCKET SetUpListener(const char* ServAddr, int Port);\r
+VOID AcceptConnections(SOCKET ListeningSocket, LPTHREAD_START_ROUTINE Service);\r
+BOOL EchoIncomingPackets(SOCKET sd);\r
+BOOL ShutdownConnection(SOCKET Sock);\r
diff --git a/reactos/apps/utils/net/tcpsvcs/tcpsvcs.c b/reactos/apps/utils/net/tcpsvcs/tcpsvcs.c
new file mode 100644 (file)
index 0000000..8628fb7
--- /dev/null
@@ -0,0 +1,57 @@
+#include <stdio.h>\r
+#include <winsock2.h>\r
+#include <tchar.h>\r
+#include "tcpsvcs.h"\r
+#include "skelserver/skelserver.h"\r
+#include "echo/echo.h"\r
+#include "chargen/chargen.h"\r
+\r
+int main(int argc, char *argv[])\r
+{\r
+    PMYDATA pData[MAX_THREADS];\r
+    DWORD dwThreadId[MAX_THREADS];\r
+    HANDLE hThread[MAX_THREADS];\r
+    INT i;\r
+\r
+    /* Create MAX_THREADS worker threads. */\r
+    for( i=0; i<MAX_THREADS; i++ )\r
+    {\r
+        /* Allocate memory for thread data. */\r
+        pData[i] = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(MYDATA));\r
+\r
+        if( pData == NULL )\r
+            ExitProcess(2);\r
+\r
+        /* Generate unique data for each thread. */\r
+        pData[0]->Port = ECHO_PORT;\r
+        pData[0]->Service = EchoHandler;\r
+        pData[1]->Port = CHARGEN_PORT;\r
+        pData[1]->Service = ChargenHandler;\r
+\r
+        hThread[i] = CreateThread(\r
+            NULL,              // default security attributes\r
+            0,                 // use default stack size\r
+            StartServer,       // thread function\r
+            pData[i],          // argument to thread function\r
+            0,                 // use default creation flags\r
+            &dwThreadId[i]);   // returns the thread identifier\r
+\r
+        /* Check the return value for success. */\r
+        if (hThread[i] == NULL)\r
+        {\r
+            ExitProcess(i);\r
+        }\r
+    }\r
+\r
+    /* Wait until all threads have terminated. */\r
+    WaitForMultipleObjects(MAX_THREADS, hThread, TRUE, INFINITE);\r
+\r
+    /* Close all thread handles upon completion. */\r
+    for(i=0; i<MAX_THREADS; i++)\r
+    {\r
+        CloseHandle(hThread[i]);\r
+    }\r
+\r
+    return 0;\r
+}\r
+\r
diff --git a/reactos/apps/utils/net/tcpsvcs/tcpsvcs.h b/reactos/apps/utils/net/tcpsvcs/tcpsvcs.h
new file mode 100644 (file)
index 0000000..97bfa60
--- /dev/null
@@ -0,0 +1,13 @@
+#define ECHO_PORT 7\r
+#define CHARGEN_PORT 19\r
+#define DAYTIME_PORT 13\r
+#define DISCARD_PORT 9\r
+#define QOTD_PORT 17\r
+\r
+#define MAX_THREADS 2\r
+#define BUF_SIZE 255\r
+\r
+typedef struct _MyData {\r
+    INT Port;\r
+    LPTHREAD_START_ROUTINE Service;\r
+} MYDATA, *PMYDATA;\r
diff --git a/reactos/apps/utils/net/tcpsvcs/tcpsvcs.xml b/reactos/apps/utils/net/tcpsvcs/tcpsvcs.xml
new file mode 100644 (file)
index 0000000..152f4e6
--- /dev/null
@@ -0,0 +1,13 @@
+<module name="tcpsvcs" type="win32cui" installbase="system32" installname="tcpsvcs.exe">\r
+    <include base="arp">.</include>\r
+    <define name="__USE_W32API" />\r
+    <library>kernel32</library>\r
+    <library>iphlpapi</library>\r
+    <library>ws2_32</library>\r
+    <library>shlwapi</library>\r
+    <file>tcpsvcs.c</file>\r
+    <file>skelserver/skelserver.c</file>\r
+    <file>echo/echo.c</file>\r
+    <file>chargen/chargen.c</file>\r
+</module>\r
+\r