ReactOS  0.4.12-dev-14-gd0c8636
skelserver.c File Reference
#include "tcpsvcs.h"
Include dependency graph for skelserver.c:

Go to the source code of this file.

Macros

#define BUF   1024
 

Functions

static SOCKET SetUpListener (USHORT Port)
 
static VOID AcceptConnections (SOCKET listeningSocket, LPTHREAD_START_ROUTINE lpService, LPWSTR lpName)
 
BOOL ShutdownConnection (SOCKET sock, BOOL bRec)
 
DWORD WINAPI StartServer (LPVOID lpParam)
 

Macro Definition Documentation

◆ BUF

#define BUF   1024

Definition at line 12 of file skelserver.c.

Referenced by _cput(), js_fprintf(), and ShutdownConnection().

Function Documentation

◆ AcceptConnections()

static VOID AcceptConnections ( SOCKET  listeningSocket,
LPTHREAD_START_ROUTINE  lpService,
LPWSTR  lpName 
)
static

Definition at line 55 of file skelserver.c.

Referenced by StartServer().

58 {
60  SOCKET sock;
62  TIMEVAL timeVal;
63  FD_SET readFD;
64  WCHAR logBuf[256];
65  INT timeOut = 2000;
66 
67  timeVal.tv_sec = timeOut / 1000;
68  timeVal.tv_usec = timeOut % 1000;
69 
70  while (!bShutdown)
71  {
72  INT selRet = 0;
73 
74  FD_ZERO(&readFD);
75  FD_SET(listeningSocket, &readFD);
76 
77  selRet = select(0, &readFD, NULL, NULL, &timeVal);
78  if (selRet > 0)
79  {
80  if (!bShutdown || FD_ISSET(listeningSocket, &readFD))
81  {
82  INT addrSize = sizeof(SOCKADDR_IN);
83 
84  sock = accept(listeningSocket, (SOCKADDR*)&client, &addrSize);
85  if (sock != INVALID_SOCKET)
86  {
87  swprintf(logBuf,
88  L"Accepted connection to %s server from %S:%d",
89  lpName,
90  inet_ntoa(client.sin_addr),
91  ntohs(client.sin_port));
92  LogEvent(logBuf, 0, 0, LOG_FILE);
93 
94  swprintf(logBuf, L"Creating worker thread for %s", lpName);
95  LogEvent(logBuf, 0, 0, LOG_FILE);
96 
97  if (!bShutdown)
98  {
99  hThread = CreateThread(0, 0, lpService, (void*)sock, 0, NULL);
100  if (hThread != NULL)
101  {
102  CloseHandle(hThread);
103  }
104  else
105  {
106  swprintf(logBuf, L"Failed to start worker thread for the %s server",
107  lpName);
108  LogEvent(logBuf, 0, 0, LOG_FILE);
109  }
110  }
111  }
112  else
113  {
114  LogEvent(L"accept failed", WSAGetLastError(), 0, LOG_ERROR);
115  }
116  }
117  }
118  else if (selRet == SOCKET_ERROR)
119  {
120  LogEvent(L"select failed", WSAGetLastError(), 0, LOG_ERROR);
121  }
122  }
123 }
Definition: winsock.h:66
volatile BOOL bShutdown
Definition: tcpsvcs.c:16
#define SOCKET_ERROR
Definition: winsock.h:333
#define CloseHandle
Definition: compat.h:398
static SOCKET sock
Definition: syslog.c:45
u_short sin_port
Definition: winsock.h:511
__wchar_t WCHAR
Definition: xmlstorage.h:180
unsigned long tv_sec
Definition: linux.h:1738
_In_ LPCSTR lpName
Definition: winbase.h:2729
#define FD_ZERO(set)
Definition: winsock.h:96
#define FD_SET(fd, set)
Definition: winsock.h:89
int32_t INT
Definition: typedefs.h:56
#define ntohs(x)
Definition: module.h:208
#define FD_ISSET(fd, set)
Definition: winsock.h:100
INT WSAAPI select(IN INT s, IN OUT LPFD_SET readfds, IN OUT LPFD_SET writefds, IN OUT LPFD_SET exceptfds, IN CONST struct timeval *timeout)
Definition: select.c:41
HANDLE WINAPI DECLSPEC_HOTPATCH CreateThread(IN LPSECURITY_ATTRIBUTES lpThreadAttributes, IN DWORD dwStackSize, IN LPTHREAD_START_ROUTINE lpStartAddress, IN LPVOID lpParameter, IN DWORD dwCreationFlags, OUT LPDWORD lpThreadId)
Definition: thread.c:112
smooth NULL
Definition: ftsmooth.c:416
unsigned long tv_usec
Definition: linux.h:1739
#define LOG_ERROR
Definition: tcpsvcs.h:16
#define inet_ntoa(addr)
Definition: inet.h:100
INT WSAAPI WSAGetLastError(VOID)
Definition: dllmain.c:112
#define INVALID_SOCKET
Definition: winsock.h:332
#define swprintf(buf, format,...)
Definition: sprintf.c:56
static FILE * client
Definition: client.c:41
static const WCHAR L[]
Definition: oid.c:1087
SOCKET WSAAPI accept(IN SOCKET s, OUT LPSOCKADDR addr, OUT INT FAR *addrlen)
Definition: socklife.c:23
struct in_addr sin_addr
Definition: winsock.h:512
VOID LogEvent(LPCWSTR lpMsg, DWORD errNum, DWORD exitCode, UINT flags)
Definition: log.c:196
struct sockaddr_in SOCKADDR_IN
Definition: winsock.h:487
HANDLE hThread
Definition: wizard.c:27
UINT_PTR SOCKET
Definition: winsock.h:47

◆ SetUpListener()

static SOCKET SetUpListener ( USHORT  Port)
static

Definition at line 15 of file skelserver.c.

Referenced by StartServer().

16 {
17  SOCKET sock;
19  BOOL bSetup = FALSE;
20 
21  sock = socket(AF_INET, SOCK_STREAM, 0);
22  if (sock != INVALID_SOCKET)
23  {
24  ZeroMemory(&server, sizeof(SOCKADDR_IN));
25  server.sin_family = AF_INET;
26  server.sin_addr.s_addr = htonl(INADDR_ANY);
27  server.sin_port = Port;
28 
29  if (bind(sock, (SOCKADDR*)&server, sizeof(SOCKADDR_IN)) != SOCKET_ERROR)
30  {
31  if (listen(sock, SOMAXCONN) != SOCKET_ERROR)
32  {
33  bSetup = TRUE;
34  }
35  else
36  {
37  LogEvent(L"listen() failed", WSAGetLastError(), 0, LOG_ERROR);
38  }
39  }
40  else
41  {
42  LogEvent(L"bind() failed", WSAGetLastError(), 0, LOG_ERROR);
43  }
44  }
45  else
46  {
47  LogEvent(L"socket() failed", WSAGetLastError(), 0, LOG_ERROR);
48  }
49 
50  return bSetup ? sock : INVALID_SOCKET;
51 }
#define SOCKET_ERROR
Definition: winsock.h:333
CPPORT Port[4]
Definition: headless.c:34
#define TRUE
Definition: types.h:120
static rfbScreenInfoPtr server
Definition: vnc.c:74
static SOCKET sock
Definition: syslog.c:45
SOCKET WSAAPI socket(IN INT af, IN INT type, IN INT protocol)
Definition: socklife.c:143
u_short sin_port
Definition: winsock.h:511
#define htonl(x)
Definition: module.h:212
INT WSAAPI bind(IN SOCKET s, IN CONST struct sockaddr *name, IN INT namelen)
Definition: socklife.c:36
#define INADDR_ANY
Definition: StrAddr.c:4
#define ZeroMemory
Definition: winbase.h:1635
#define SOMAXCONN
Definition: winsock.h:399
#define LOG_ERROR
Definition: tcpsvcs.h:16
INT WSAAPI WSAGetLastError(VOID)
Definition: dllmain.c:112
unsigned int BOOL
Definition: ntddk_ex.h:94
#define INVALID_SOCKET
Definition: winsock.h:332
static const WCHAR L[]
Definition: oid.c:1087
struct in_addr sin_addr
Definition: winsock.h:512
VOID LogEvent(LPCWSTR lpMsg, DWORD errNum, DWORD exitCode, UINT flags)
Definition: log.c:196
INT WSAAPI listen(IN SOCKET s, IN INT backlog)
Definition: sockctrl.c:123
#define AF_INET
Definition: tcpip.h:117
UINT_PTR SOCKET
Definition: winsock.h:47
#define SOCK_STREAM
Definition: tcpip.h:118
short sin_family
Definition: winsock.h:510

◆ ShutdownConnection()

BOOL ShutdownConnection ( SOCKET  sock,
BOOL  bRec 
)

Definition at line 126 of file skelserver.c.

Referenced by ChargenHandler(), DaytimeHandler(), DiscardHandler(), EchoHandler(), and QotdHandler().

128 {
129  WCHAR logBuf[256];
130 
131  /* Disallow any further data sends. This will tell the other side
132  that we want to go away now. If we skip this step, we don't
133  shut the connection down nicely. */
135  {
136  LogEvent(L"Error in shutdown()", WSAGetLastError(), 0, LOG_ERROR);
137  return FALSE;
138  }
139 
140  /* Receive any extra data still sitting on the socket
141  before we close it */
142  if (bRec)
143  {
144  CHAR readBuffer[BUF];
145  INT ret;
146 
147  do
148  {
149  ret = recv(sock, readBuffer, BUF, 0);
150  if (ret >= 0)
151  {
152  swprintf(logBuf, L"FYI, received %d unexpected bytes during shutdown", ret);
153  LogEvent(logBuf, 0, 0, LOG_FILE);
154  }
155  } while (ret > 0);
156  }
157 
158  closesocket(sock);
159 
160  return TRUE;
161 }
#define SOCKET_ERROR
Definition: winsock.h:333
#define TRUE
Definition: types.h:120
__wchar_t WCHAR
Definition: xmlstorage.h:180
char CHAR
Definition: xmlstorage.h:175
#define BUF
Definition: skelserver.c:12
int32_t INT
Definition: typedefs.h:56
#define closesocket
Definition: main.c:39
Definition: jssnprintf.c:29
#define LOG_ERROR
Definition: tcpsvcs.h:16
INT WSAAPI WSAGetLastError(VOID)
Definition: dllmain.c:112
INT WSAAPI shutdown(IN SOCKET s, IN INT how)
Definition: sockctrl.c:506
#define swprintf(buf, format,...)
Definition: sprintf.c:56
int ret
static const WCHAR L[]
Definition: oid.c:1087
VOID LogEvent(LPCWSTR lpMsg, DWORD errNum, DWORD exitCode, UINT flags)
Definition: log.c:196
#define SD_SEND
Definition: winsock.h:55
Definition: tcpcore.h:1455
INT WSAAPI recv(IN SOCKET s, OUT CHAR FAR *buf, IN INT len, IN INT flags)
Definition: recv.c:23

◆ StartServer()

DWORD WINAPI StartServer ( LPVOID  lpParam)

Definition at line 165 of file skelserver.c.

Referenced by CreateServers().

166 {
167  SOCKET listeningSocket;
168  PSERVICES pServices;
169  TCHAR logBuf[256];
170 
171  pServices = (PSERVICES)lpParam;
172 
173  swprintf(logBuf, L"Starting %s server", pServices->lpName);
174  LogEvent(logBuf, 0, 0, LOG_FILE);
175 
176  if (!bShutdown)
177  {
178  listeningSocket = SetUpListener(htons(pServices->Port));
179  if (!bShutdown && listeningSocket != INVALID_SOCKET)
180  {
181  swprintf(logBuf,
182  L"%s is waiting for connections on port %d",
183  pServices->lpName,
184  pServices->Port);
185  LogEvent(logBuf, 0, 0, LOG_FILE);
186 
187  AcceptConnections(listeningSocket, pServices->lpService, pServices->lpName);
188  }
189  else
190  {
191  LogEvent(L"Socket error when setting up listener", 0, 0, LOG_FILE);
192  }
193  }
194 
195  swprintf(logBuf, L"Exiting %s thread", pServices->lpName);
196  LogEvent(logBuf, 0, 0, LOG_FILE);
197  ExitThread(0);
198 }
volatile BOOL bShutdown
Definition: tcpsvcs.c:16
struct _Services * PSERVICES
static VOID AcceptConnections(SOCKET listeningSocket, LPTHREAD_START_ROUTINE lpService, LPWSTR lpName)
Definition: skelserver.c:55
VOID WINAPI ExitThread(IN DWORD uExitCode)
Definition: thread.c:327
USHORT Port
Definition: tcpsvcs.h:33
LPWSTR lpName
Definition: tcpsvcs.h:34
char TCHAR
Definition: xmlstorage.h:189
#define INVALID_SOCKET
Definition: winsock.h:332
#define swprintf(buf, format,...)
Definition: sprintf.c:56
static const WCHAR L[]
Definition: oid.c:1087
LPTHREAD_START_ROUTINE lpService
Definition: tcpsvcs.h:35
VOID LogEvent(LPCWSTR lpMsg, DWORD errNum, DWORD exitCode, UINT flags)
Definition: log.c:196
UINT_PTR SOCKET
Definition: winsock.h:47
#define htons(x)
Definition: module.h:213
static SOCKET SetUpListener(USHORT Port)
Definition: skelserver.c:15