2006-02-16 23:23:37 +00:00
|
|
|
/*
|
|
|
|
* PROJECT: ReactOS simple TCP/IP services
|
2006-02-19 16:33:14 +00:00
|
|
|
* LICENSE: GPL - See COPYING in the top level directory
|
2015-09-12 10:58:07 +00:00
|
|
|
* FILE: base/services/tcpsvcs/skelserver.c
|
2008-04-02 16:54:55 +00:00
|
|
|
* PURPOSE: Sets up a server and listens for connections
|
|
|
|
* COPYRIGHT: Copyright 2005 - 2008 Ged Murphy <gedmurphy@gmail.com>
|
2006-02-16 23:23:37 +00:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "tcpsvcs.h"
|
|
|
|
|
2008-04-02 16:54:55 +00:00
|
|
|
#define BUF 1024
|
2006-02-16 23:23:37 +00:00
|
|
|
|
2007-07-09 23:13:09 +00:00
|
|
|
static SOCKET
|
|
|
|
SetUpListener(USHORT Port)
|
2006-02-16 23:23:37 +00:00
|
|
|
{
|
2008-04-02 16:54:55 +00:00
|
|
|
SOCKET sock;
|
|
|
|
SOCKADDR_IN server;
|
|
|
|
BOOL bSetup = FALSE;
|
2006-02-16 23:23:37 +00:00
|
|
|
|
2008-04-02 16:54:55 +00:00
|
|
|
sock = socket(AF_INET, SOCK_STREAM, 0);
|
|
|
|
if (sock != INVALID_SOCKET)
|
2006-02-16 23:23:37 +00:00
|
|
|
{
|
2014-04-06 12:44:10 +00:00
|
|
|
ZeroMemory(&server, sizeof(SOCKADDR_IN));
|
2008-04-02 16:54:55 +00:00
|
|
|
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)
|
2006-02-16 23:23:37 +00:00
|
|
|
{
|
2008-04-02 16:54:55 +00:00
|
|
|
if (listen(sock, SOMAXCONN) != SOCKET_ERROR)
|
|
|
|
{
|
|
|
|
bSetup = TRUE;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2008-09-01 12:51:49 +00:00
|
|
|
LogEvent(L"listen() failed", WSAGetLastError(), 0, LOG_ERROR);
|
2008-04-02 16:54:55 +00:00
|
|
|
}
|
2006-02-16 23:23:37 +00:00
|
|
|
}
|
|
|
|
else
|
2008-04-02 16:54:55 +00:00
|
|
|
{
|
2008-09-01 12:51:49 +00:00
|
|
|
LogEvent(L"bind() failed", WSAGetLastError(), 0, LOG_ERROR);
|
2008-04-02 16:54:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2008-09-01 12:51:49 +00:00
|
|
|
LogEvent(L"socket() failed", WSAGetLastError(), 0, LOG_ERROR);
|
2006-02-16 23:23:37 +00:00
|
|
|
}
|
2007-07-09 23:13:09 +00:00
|
|
|
|
2008-04-02 16:54:55 +00:00
|
|
|
return bSetup ? sock : INVALID_SOCKET;
|
2006-02-16 23:23:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-07-09 23:13:09 +00:00
|
|
|
static VOID
|
2008-04-02 16:54:55 +00:00
|
|
|
AcceptConnections(SOCKET listeningSocket,
|
|
|
|
LPTHREAD_START_ROUTINE lpService,
|
2008-09-01 12:51:49 +00:00
|
|
|
LPWSTR lpName)
|
2006-02-16 23:23:37 +00:00
|
|
|
{
|
2008-04-02 16:54:55 +00:00
|
|
|
SOCKADDR_IN client;
|
|
|
|
SOCKET sock;
|
2006-02-16 23:23:37 +00:00
|
|
|
HANDLE hThread;
|
2008-04-02 16:54:55 +00:00
|
|
|
TIMEVAL timeVal;
|
|
|
|
FD_SET readFD;
|
2008-09-01 12:51:49 +00:00
|
|
|
WCHAR logBuf[256];
|
2008-04-02 16:54:55 +00:00
|
|
|
INT timeOut = 2000;
|
|
|
|
|
|
|
|
timeVal.tv_sec = timeOut / 1000;
|
|
|
|
timeVal.tv_usec = timeOut % 1000;
|
|
|
|
|
|
|
|
while (!bShutdown)
|
2006-02-16 23:23:37 +00:00
|
|
|
{
|
2008-04-02 16:54:55 +00:00
|
|
|
INT selRet = 0;
|
2006-02-16 23:23:37 +00:00
|
|
|
|
2008-04-02 16:54:55 +00:00
|
|
|
FD_ZERO(&readFD);
|
|
|
|
FD_SET(listeningSocket, &readFD);
|
2006-02-16 23:23:37 +00:00
|
|
|
|
2008-04-02 16:54:55 +00:00
|
|
|
selRet = select(0, &readFD, NULL, NULL, &timeVal);
|
|
|
|
if (selRet > 0)
|
2008-02-26 13:54:12 +00:00
|
|
|
{
|
2008-04-02 16:54:55 +00:00
|
|
|
if (!bShutdown || FD_ISSET(listeningSocket, &readFD))
|
2008-02-26 13:54:12 +00:00
|
|
|
{
|
2008-04-02 16:54:55 +00:00
|
|
|
INT addrSize = sizeof(SOCKADDR_IN);
|
|
|
|
|
|
|
|
sock = accept(listeningSocket, (SOCKADDR*)&client, &addrSize);
|
|
|
|
if (sock != INVALID_SOCKET)
|
2008-02-26 13:54:12 +00:00
|
|
|
{
|
2014-05-26 14:03:24 +00:00
|
|
|
swprintf(logBuf,
|
|
|
|
L"Accepted connection to %s server from %S:%d",
|
|
|
|
lpName,
|
|
|
|
inet_ntoa(client.sin_addr),
|
|
|
|
ntohs(client.sin_port));
|
2008-04-02 16:54:55 +00:00
|
|
|
LogEvent(logBuf, 0, 0, LOG_FILE);
|
2008-02-26 13:54:12 +00:00
|
|
|
|
2014-05-26 14:03:24 +00:00
|
|
|
swprintf(logBuf, L"Creating worker thread for %s", lpName);
|
2008-04-02 16:54:55 +00:00
|
|
|
LogEvent(logBuf, 0, 0, LOG_FILE);
|
2008-02-26 13:54:12 +00:00
|
|
|
|
2008-04-02 16:54:55 +00:00
|
|
|
if (!bShutdown)
|
2008-02-26 13:54:12 +00:00
|
|
|
{
|
2008-04-02 16:54:55 +00:00
|
|
|
hThread = CreateThread(0, 0, lpService, (void*)sock, 0, NULL);
|
|
|
|
if (hThread != NULL)
|
|
|
|
{
|
|
|
|
CloseHandle(hThread);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2014-05-26 14:03:24 +00:00
|
|
|
swprintf(logBuf, L"Failed to start worker thread for the %s server",
|
|
|
|
lpName);
|
2008-04-02 16:54:55 +00:00
|
|
|
LogEvent(logBuf, 0, 0, LOG_FILE);
|
|
|
|
}
|
2008-02-26 13:54:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2008-09-01 12:51:49 +00:00
|
|
|
LogEvent(L"accept failed", WSAGetLastError(), 0, LOG_ERROR);
|
2008-02-26 13:54:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2008-04-02 16:54:55 +00:00
|
|
|
else if (selRet == SOCKET_ERROR)
|
|
|
|
{
|
2008-09-01 12:51:49 +00:00
|
|
|
LogEvent(L"select failed", WSAGetLastError(), 0, LOG_ERROR);
|
2008-04-02 16:54:55 +00:00
|
|
|
}
|
2006-02-16 23:23:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-07-09 23:13:09 +00:00
|
|
|
BOOL
|
2008-04-02 16:54:55 +00:00
|
|
|
ShutdownConnection(SOCKET sock,
|
2007-07-09 23:13:09 +00:00
|
|
|
BOOL bRec)
|
2006-02-16 23:23:37 +00:00
|
|
|
{
|
2008-09-01 12:51:49 +00:00
|
|
|
WCHAR logBuf[256];
|
2006-02-16 23:23:37 +00:00
|
|
|
|
|
|
|
/* 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. */
|
2008-04-02 16:54:55 +00:00
|
|
|
if (shutdown(sock, SD_SEND) == SOCKET_ERROR)
|
2006-02-16 23:23:37 +00:00
|
|
|
{
|
2008-09-01 12:51:49 +00:00
|
|
|
LogEvent(L"Error in shutdown()", WSAGetLastError(), 0, LOG_ERROR);
|
2006-02-16 23:23:37 +00:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2008-04-02 16:54:55 +00:00
|
|
|
/* Receive any extra data still sitting on the socket
|
|
|
|
before we close it */
|
2006-02-16 23:23:37 +00:00
|
|
|
if (bRec)
|
|
|
|
{
|
2008-04-02 16:54:55 +00:00
|
|
|
CHAR readBuffer[BUF];
|
|
|
|
INT ret;
|
|
|
|
|
|
|
|
do
|
2006-02-16 23:23:37 +00:00
|
|
|
{
|
2008-04-02 16:54:55 +00:00
|
|
|
ret = recv(sock, readBuffer, BUF, 0);
|
|
|
|
if (ret >= 0)
|
|
|
|
{
|
2014-05-26 14:03:24 +00:00
|
|
|
swprintf(logBuf, L"FYI, received %d unexpected bytes during shutdown", ret);
|
2008-04-02 16:54:55 +00:00
|
|
|
LogEvent(logBuf, 0, 0, LOG_FILE);
|
|
|
|
}
|
|
|
|
} while (ret > 0);
|
2006-02-16 23:23:37 +00:00
|
|
|
}
|
|
|
|
|
2008-04-02 16:54:55 +00:00
|
|
|
closesocket(sock);
|
2006-02-16 23:23:37 +00:00
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
2007-07-09 23:13:09 +00:00
|
|
|
|
|
|
|
|
|
|
|
DWORD WINAPI
|
|
|
|
StartServer(LPVOID lpParam)
|
|
|
|
{
|
2008-04-02 16:54:55 +00:00
|
|
|
SOCKET listeningSocket;
|
2007-07-09 23:13:09 +00:00
|
|
|
PSERVICES pServices;
|
2008-04-02 16:54:55 +00:00
|
|
|
TCHAR logBuf[256];
|
2007-07-09 23:13:09 +00:00
|
|
|
|
|
|
|
pServices = (PSERVICES)lpParam;
|
|
|
|
|
2014-05-26 14:03:24 +00:00
|
|
|
swprintf(logBuf, L"Starting %s server", pServices->lpName);
|
2008-04-02 16:54:55 +00:00
|
|
|
LogEvent(logBuf, 0, 0, LOG_FILE);
|
2007-07-09 23:13:09 +00:00
|
|
|
|
2008-04-02 16:54:55 +00:00
|
|
|
if (!bShutdown)
|
|
|
|
{
|
|
|
|
listeningSocket = SetUpListener(htons(pServices->Port));
|
|
|
|
if (!bShutdown && listeningSocket != INVALID_SOCKET)
|
|
|
|
{
|
2014-05-26 14:03:24 +00:00
|
|
|
swprintf(logBuf,
|
|
|
|
L"%s is waiting for connections on port %d",
|
|
|
|
pServices->lpName,
|
|
|
|
pServices->Port);
|
2008-04-02 16:54:55 +00:00
|
|
|
LogEvent(logBuf, 0, 0, LOG_FILE);
|
2007-07-09 23:13:09 +00:00
|
|
|
|
2008-09-01 12:51:49 +00:00
|
|
|
AcceptConnections(listeningSocket, pServices->lpService, pServices->lpName);
|
2008-04-02 16:54:55 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2008-09-01 12:51:49 +00:00
|
|
|
LogEvent(L"Socket error when setting up listener", 0, 0, LOG_FILE);
|
2008-04-02 16:54:55 +00:00
|
|
|
}
|
|
|
|
}
|
2007-07-09 23:13:09 +00:00
|
|
|
|
2014-05-26 14:03:24 +00:00
|
|
|
swprintf(logBuf, L"Exiting %s thread", pServices->lpName);
|
2008-04-02 16:54:55 +00:00
|
|
|
LogEvent(logBuf, 0, 0, LOG_FILE);
|
2007-07-09 23:13:09 +00:00
|
|
|
ExitThread(0);
|
|
|
|
}
|