ReactOS 0.4.15-dev-7931-gfd331f1
startup.c
Go to the documentation of this file.
1/*
2 * COPYRIGHT: See COPYING in the top level directory
3 * PROJECT: ReactOS WinSock 2 API
4 * FILE: dll/win32/ws2_32/src/startup.c
5 * PURPOSE: Startup/Cleanup Support
6 * PROGRAMMER: Alex Ionescu (alex@relsoft.net)
7 */
8
9/* INCLUDES ******************************************************************/
10
11#include <ws2_32.h>
12
13#define NDEBUG
14#include <debug.h>
15
16/* DATA **********************************************************************/
17
20
21#define WsStartupLock() EnterCriticalSection(&WsStartupLock)
22#define WsStartupUnlock() LeaveCriticalSection(&WsStartupLock)
23
24/* FUNCTIONS *****************************************************************/
25
26VOID
29{
30 /* Initialize the startup lock */
32}
33
34VOID
37{
38 /* Destroy the startup lock */
40}
41
42/*
43 * @implemented
44 */
45BOOL
48{
49 /* Set the post routine */
50 DPRINT("WSApSetPostRoutine: %p\n", Routine);
52 return ERROR_SUCCESS;
53}
54
55/*
56 * @implemented
57 */
58INT
61{
65 LONG RefCount;
66 DPRINT("WSACleanup\n");
67
68 /* Enter startup lock */
70
71 /* Enter prolog */
73 {
74 /* Decrement process reference count and check if it's zero */
75 if (!(RefCount = InterlockedDecrement(&Process->RefCount)))
76 {
77 /* It's zero, destroy the process structure */
79 }
80 else if (RefCount == 1 && WsAsyncThreadInitialized)
81 {
82 /* Kill async thread */
84 }
85
86 DPRINT("WSACleanup RefCount = %ld\n", RefCount);
87 /* Return success */
89
90 /* Clear last error */
92 }
93 else
94 {
95 DPRINT("WSACleanup uninitialized\n");
96 /* Weren't initialized */
99 }
100
101 /* Release startup lock */
103
104 /* Done */
105 return ErrorCode;
106}
107
108/*
109 * @implemented
110 */
111INT
112WINAPI
113WSAStartup(IN WORD wVersionRequested,
114 OUT LPWSADATA lpWSAData)
115{
116 WORD VersionReturned = 0;
118 PWSPROCESS CurrentProcess;
119 DPRINT("WSAStartup: %wx %d.%d\n", wVersionRequested, LOBYTE(wVersionRequested), HIBYTE(wVersionRequested));
120
121 /* Make sure that we went through DLL Init */
122 if (!WsDllHandle) return WSASYSNOTREADY;
123
124 /* Check which version is being requested */
125 switch (LOBYTE(wVersionRequested))
126 {
127 case 0:
128
129 /* We don't support this unknown version */
131 VersionReturned = MAKEWORD(2, 2);
132 break;
133
134 case 1:
135 /* We support only 1.0 and 1.1 */
136 if (HIBYTE(wVersionRequested) <= 1)
137 {
138 /* Caller wants 1.0, return it */
139 VersionReturned = MAKEWORD(1, HIBYTE(wVersionRequested));
140 }
141 else
142 {
143 /* The only other version we support is 1.1 */
144 VersionReturned = MAKEWORD(1, 1);
145 }
146 break;
147
148 case 2:
149 /* We support only 2.0, 2.1 and 2.2 */
150 if (HIBYTE(wVersionRequested) <= 2)
151 {
152 /* Caller wants 2.0-2.2, return it */
153 VersionReturned = MAKEWORD(2, HIBYTE(wVersionRequested));
154 }
155 else
156 {
157 /* The highest version we support is 2.2 */
158 VersionReturned = MAKEWORD(2, 2);
159 }
160 break;
161
162 default:
163
164 /* Return 2.2 */
165 VersionReturned = MAKEWORD(2, 2);
166 break;
167 }
168
169 if (lpWSAData == NULL)
170 {
173 }
174
175 /* Return the Version Requested, unless error */
176 lpWSAData->wVersion = VersionReturned;
177
178 /* We support Winsock 2.2 */
179 lpWSAData->wHighVersion = MAKEWORD(2,2);
180 lstrcpy(lpWSAData->szDescription, "WinSock 2.0");
181 lstrcpy(lpWSAData->szSystemStatus, "Running");
182
183 /*
184 * On Winsock 1, the following values are returned.
185 * Taken straight from a Winsock Test app on Windows.
186 */
187 if (LOBYTE(wVersionRequested) == 1)
188 {
189 lpWSAData->iMaxSockets = 32767;
190 lpWSAData->iMaxUdpDg = 65467;
191 }
192 else
193 {
194 lpWSAData->iMaxSockets = 0;
195 lpWSAData->iMaxUdpDg = 0;
196 }
197
198 /* Requested invalid version (0) */
200 {
202 return ErrorCode;
203 }
204
205 /* Enter the startup synchronization lock */
207
208 /* Now setup all our objects */
209 while (TRUE)
210 {
211 /* Make sure we don't already have a process */
212 CurrentProcess = WsGetProcess();
213 if (CurrentProcess) break;
214
215 /* Setup the process object support */
217 if (ErrorCode != ERROR_SUCCESS) break;
218
219 /* Setup the process object support */
221 if (ErrorCode != ERROR_SUCCESS) break;
222
223 /* Setup the process object support */
225 if (ErrorCode != ERROR_SUCCESS) break;
226
227 /* Try getting the process now */
228 CurrentProcess = WsGetProcess();
229 if (!CurrentProcess)
230 {
231 /* Something is weird... */
233 break;
234 }
235 }
236
237 /* Check if all worked */
239 {
240 /* Set the requested version */
241 WsProcSetVersion(CurrentProcess, wVersionRequested);
242
243 /* Increase the reference count */
244 InterlockedIncrement(&CurrentProcess->RefCount);
245 DPRINT("WSAStartup RefCount = %ld\n", CurrentProcess->RefCount);
246
247 /* Clear last error */
249 }
250 else
251 {
253 }
254
255 /* Leave the startup lock */
257
258 /* Return any Error */
259 return ErrorCode;
260}
#define InterlockedIncrement
Definition: armddk.h:53
#define InterlockedDecrement
Definition: armddk.h:52
#define ERROR_SUCCESS
Definition: deptool.c:10
#define NULL
Definition: types.h:112
#define TRUE
Definition: types.h:120
#define SetLastError(x)
Definition: compat.h:752
#define WsStartupLock()
Definition: startup.c:21
#define WsStartupUnlock()
Definition: startup.c:22
INT WINAPI WSAStartup(IN WORD wVersionRequested, OUT LPWSADATA lpWSAData)
Definition: startup.c:113
VOID WSAAPI WsDestroyStartupSynchronization(VOID)
Definition: startup.c:36
VOID WSAAPI WsCreateStartupSynchronization(VOID)
Definition: startup.c:28
BOOL WSAAPI WSApSetPostRoutine(PVOID Routine)
Definition: startup.c:47
PWS_SOCK_POST_ROUTINE WsSockPostRoutine
Definition: startup.c:18
unsigned int BOOL
Definition: ntddk_ex.h:94
unsigned long DWORD
Definition: ntddk_ex.h:95
unsigned short WORD
Definition: ntddk_ex.h:93
_In_opt_ PFILE_OBJECT _In_opt_ PETHREAD Thread
Definition: fltkernel.h:2653
_Must_inspect_result_ _In_ PLARGE_INTEGER _In_ PLARGE_INTEGER _In_ ULONG _In_ PFILE_OBJECT _In_ PVOID Process
Definition: fsrtlfuncs.h:223
#define LOBYTE(W)
Definition: jmemdos.c:487
#define HIBYTE(W)
Definition: jmemdos.c:486
_In_ NDIS_ERROR_CODE ErrorCode
Definition: ndis.h:4436
long LONG
Definition: pedump.c:60
#define DPRINT
Definition: sndvol32.h:71
LONG RefCount
Definition: ws2_32p.h:162
VOID WINAPI InitializeCriticalSection(OUT LPCRITICAL_SECTION lpCriticalSection)
Definition: synch.c:751
#define MAKEWORD(a, b)
Definition: typedefs.h:248
int32_t INT
Definition: typedefs.h:58
#define IN
Definition: typedefs.h:39
#define OUT
Definition: typedefs.h:40
#define lstrcpy
Definition: winbase.h:3874
void WINAPI DeleteCriticalSection(PCRITICAL_SECTION)
#define WINAPI
Definition: msvc.h:6
#define WSANOTINITIALISED
Definition: winerror.h:1987
#define WSASYSNOTREADY
Definition: winerror.h:1985
#define WSAVERNOTSUPPORTED
Definition: winerror.h:1986
#define WSAEFAULT
Definition: winerror.h:1945
#define WSAAPI
Definition: winsock2.h:605
int PASCAL FAR WSACleanup(void)
Definition: startup.c:60
#define SOCKET_ERROR
Definition: winsock.h:333
INT WSAAPI WsSockStartup(VOID)
Definition: dsocket.c:21
DWORD WSAAPI WsThreadStartup(VOID)
Definition: dthread.c:153
VOID WSAAPI WsAsyncTerminateThread(VOID)
Definition: async.c:899
FORCEINLINE PWSPROCESS WsGetProcess(VOID)
Definition: ws2_32p.h:885
VOID WSAAPI WsProcDelete(IN PWSPROCESS Process)
Definition: dprocess.c:247
BOOLEAN WsAsyncThreadInitialized
Definition: async.c:15
INT WSAAPI WsProcStartup(VOID)
Definition: dprocess.c:126
HINSTANCE WsDllHandle
Definition: dllmain.c:22
VOID WSAAPI WsProcSetVersion(IN PWSPROCESS Process, IN WORD VersionRequested)
Definition: dprocess.c:312
INT WSAAPI WsApiProlog(OUT PWSPROCESS *Process, OUT PWSTHREAD *Thread)
Definition: wsautil.c:91
BOOL(WINAPI * PWS_SOCK_POST_ROUTINE)(IN HWND hWnd, IN UINT wMsg, IN WPARAM wParam, IN LPARAM lParam)
Definition: ws2_32p.h:265