2 * COPYRIGHT: See COPYING in the top level directory
3 * PROJECT: ReactOS WinSock 2 API
5 * PURPOSE: Startup/Cleanup Support
6 * PROGRAMMER: Alex Ionescu (alex@relsoft.net)
9 /* INCLUDES ******************************************************************/
16 /* DATA **********************************************************************/
18 PWS_SOCK_POST_ROUTINE WsSockPostRoutine
= NULL
;
19 CRITICAL_SECTION WsStartupLock
;
21 #define WsStartupLock() EnterCriticalSection(&WsStartupLock);
22 #define WsStartupUnlock() LeaveCriticalSection(&WsStartupLock);
24 /* FUNCTIONS *****************************************************************/
28 WsCreateStartupSynchronization(VOID
)
30 /* Initialize the startup lock */
31 InitializeCriticalSection(&WsStartupLock
);
36 WsDestroyStartupSynchronization(VOID
)
38 /* Destroy the startup lock */
39 DeleteCriticalSection(&WsStartupLock
);
47 WSApSetPostRoutine(PVOID Routine
)
49 /* Set the post routine */
50 DPRINT("WSApSetPostRoutine: %p\n", Routine
);
51 WsSockPostRoutine
= (PWS_SOCK_POST_ROUTINE
)Routine
;
66 DPRINT("WSACleanup\n");
68 /* Enter startup lock */
72 if ((ErrorCode
= WsApiProlog(&Process
, &Thread
)) == ERROR_SUCCESS
)
74 /* Decrement process reference count and check if it's zero */
75 if (!(RefCount
= InterlockedDecrement(&Process
->RefCount
)))
77 /* It's zero, destroy the process structure */
78 WsProcDelete(Process
);
80 else if (RefCount
== 1 && WsAsyncThreadInitialized
)
82 /* Kill async thread */
83 WsAsyncTerminateThread();
87 ErrorCode
= ERROR_SUCCESS
;
91 /* Weren't initialized */
92 SetLastError(ErrorCode
);
93 ErrorCode
= SOCKET_ERROR
;
96 /* Release startup lock */
108 WSAStartup(IN WORD wVersionRequested
,
109 OUT LPWSADATA lpWSAData
)
111 WORD VersionReturned
= 0;
112 DWORD ErrorCode
= ERROR_SUCCESS
;
113 PWSPROCESS CurrentProcess
;
114 DPRINT("WSAStartup: %wx\n", wVersionRequested
);
116 /* Make sure that we went through DLL Init */
117 if (!WsDllHandle
) return WSASYSNOTREADY
;
119 /* Check which version is being requested */
120 switch (LOBYTE(wVersionRequested
))
124 /* We don't support this unknown version */
125 ErrorCode
= WSAVERNOTSUPPORTED
;
129 /* We support only 1.0 and 1.1 */
130 if (HIBYTE(wVersionRequested
) == 0)
132 /* Caller wants 1.0, return it */
133 VersionReturned
= wVersionRequested
;
137 /* The only other version we support is 1.1 */
138 VersionReturned
= MAKEWORD(1, 1);
143 /* We support only 2.0, 2.1 and 2.2 */
144 if (HIBYTE(wVersionRequested
) <= 2)
146 /* Caller wants 2.0-2.2, return it */
147 VersionReturned
= MAKEWORD(2, HIBYTE(wVersionRequested
));
151 /* The highest version we support is 2.2 */
152 VersionReturned
= MAKEWORD(2, 2);
159 VersionReturned
= MAKEWORD(2, 2);;
163 /* Return the Version Requested, unless error */
164 lpWSAData
->wVersion
= VersionReturned
;
166 /* We support Winsock 2.2 */
167 lpWSAData
->wHighVersion
= MAKEWORD(2,2);
168 lstrcpy(lpWSAData
->szDescription
, "WinSock 2.2");
169 lstrcpy(lpWSAData
->szSystemStatus
, "Running");
172 * On Winsock 1, the following values are returned.
173 * Taken straight from a Winsock Test app on Windows.
175 if (LOBYTE(wVersionRequested
) == 1)
177 lpWSAData
->iMaxSockets
= 32767;
178 lpWSAData
->iMaxUdpDg
= 65467;
182 lpWSAData
->iMaxSockets
= 0;
183 lpWSAData
->iMaxUdpDg
= 0;
186 /* Enter the startup syncronization lock */
189 /* Now setup all our objects */
192 /* Make sure we don't already have a process */
193 CurrentProcess
= WsGetProcess();
194 if (CurrentProcess
) break;
196 /* Setup the process object support */
197 ErrorCode
= WsProcStartup();
198 if (ErrorCode
!= ERROR_SUCCESS
) break;
200 /* Setup the process object support */
201 ErrorCode
= WsSockStartup();
202 if (ErrorCode
!= ERROR_SUCCESS
) break;
204 /* Setup the process object support */
205 ErrorCode
= WsThreadStartup();
206 if (ErrorCode
!= ERROR_SUCCESS
) break;
208 /* Try getting the process now */
209 CurrentProcess
= WsGetProcess();
212 /* Something is weird... */
213 ErrorCode
= WSASYSNOTREADY
;
218 /* Check if all worked */
219 if (ErrorCode
== ERROR_SUCCESS
)
221 /* Set the requested version */
222 WsProcSetVersion(CurrentProcess
, wVersionRequested
);
224 /* Increase the reference count */
225 InterlockedIncrement(&CurrentProcess
->RefCount
);
228 /* Leave the startup lock */
231 /* Return any Error */