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 ******************************************************************/
15 /* DATA **********************************************************************/
17 PWS_SOCK_POST_ROUTINE WsSockPostRoutine
= NULL
;
18 CRITICAL_SECTION WsStartupLock
;
20 #define WsStartupLock() EnterCriticalSection(&WsStartupLock);
21 #define WsStartupUnlock() LeaveCriticalSection(&WsStartupLock);
23 /* FUNCTIONS *****************************************************************/
27 WsCreateStartupSynchronization(VOID
)
29 /* Initialize the startup lock */
30 InitializeCriticalSection(&WsStartupLock
);
35 WsDestroyStartupSynchronization(VOID
)
37 /* Destroy the startup lock */
38 DeleteCriticalSection(&WsStartupLock
);
46 WSApSetPostRoutine(PVOID Routine
)
48 /* Set the post routine */
49 DPRINT("WSApSetPostRoutine: %p\n", Routine
);
50 WsSockPostRoutine
= (PWS_SOCK_POST_ROUTINE
)Routine
;
65 DPRINT("WSACleanup\n");
67 /* Enter startup lock */
71 if ((ErrorCode
= WsApiProlog(&Process
, &Thread
)) == ERROR_SUCCESS
)
73 /* Decrement process reference count and check if it's zero */
74 if (!(RefCount
= InterlockedDecrement(&Process
->RefCount
)))
76 /* It's zero, destroy the process structure */
77 WsProcDelete(Process
);
79 else if (RefCount
== 1 && WsAsyncThreadInitialized
)
81 /* Kill async thread */
82 WsAsyncTerminateThread();
86 ErrorCode
= ERROR_SUCCESS
;
90 /* Weren't initialized */
91 SetLastError(ErrorCode
);
92 ErrorCode
= SOCKET_ERROR
;
95 /* Release startup lock */
107 WSAStartup(IN WORD wVersionRequested
,
108 OUT LPWSADATA lpWSAData
)
110 WORD VersionReturned
= 0;
111 DWORD ErrorCode
= ERROR_SUCCESS
;
112 PWSPROCESS CurrentProcess
;
113 DPRINT("WSAStartup: %wx\n", wVersionRequested
);
115 /* Make sure that we went through DLL Init */
116 if (!WsDllHandle
) return WSASYSNOTREADY
;
118 /* Check which version is being requested */
119 switch (LOBYTE(wVersionRequested
))
123 /* We don't support this unknown version */
124 ErrorCode
= WSAVERNOTSUPPORTED
;
128 /* We support only 1.0 and 1.1 */
129 if (HIBYTE(wVersionRequested
) == 0)
131 /* Caller wants 1.0, return it */
132 VersionReturned
= wVersionRequested
;
136 /* The only other version we support is 1.1 */
137 VersionReturned
= MAKEWORD(1, 1);
142 /* We support only 2.0, 2.1 and 2.2 */
143 if (HIBYTE(wVersionRequested
) <= 2)
145 /* Caller wants 2.0-2.2, return it */
146 VersionReturned
= MAKEWORD(2, HIBYTE(wVersionRequested
));
150 /* The highest version we support is 2.2 */
151 VersionReturned
= MAKEWORD(2, 2);
158 VersionReturned
= MAKEWORD(2, 2);;
162 /* Return the Version Requsted, unless error */
163 lpWSAData
->wVersion
= VersionReturned
;
165 /* We support Winsock 2.2 */
166 lpWSAData
->wHighVersion
= MAKEWORD(2,2);
167 lstrcpy(lpWSAData
->szDescription
, "WinSock 2.2");
168 lstrcpy(lpWSAData
->szSystemStatus
, "Running");
171 * On Winsock 1, the following values are returned.
172 * Taken straight from a Winsock Test app on Windows.
174 if (LOBYTE(wVersionRequested
) == 1)
176 lpWSAData
->iMaxSockets
= 32767;
177 lpWSAData
->iMaxUdpDg
= 65467;
181 lpWSAData
->iMaxSockets
= 0;
182 lpWSAData
->iMaxUdpDg
= 0;
185 /* Enter the startup syncronization lock */
188 /* Now setup all our objects */
191 /* Make sure we don't already have a process */
192 CurrentProcess
= WsGetProcess();
193 if (CurrentProcess
) break;
195 /* Setup the process object support */
196 ErrorCode
= WsProcStartup();
197 if (ErrorCode
!= ERROR_SUCCESS
) break;
199 /* Setup the process object support */
200 ErrorCode
= WsSockStartup();
201 if (ErrorCode
!= ERROR_SUCCESS
) break;
203 /* Setup the process object support */
204 ErrorCode
= WsThreadStartup();
205 if (ErrorCode
!= ERROR_SUCCESS
) break;
207 /* Try getting the process now */
208 CurrentProcess
= WsGetProcess();
211 /* Something is weird... */
212 ErrorCode
= WSASYSNOTREADY
;
217 /* Check if all worked */
218 if (ErrorCode
== ERROR_SUCCESS
)
220 /* Set the requested version */
221 WsProcSetVersion(CurrentProcess
, wVersionRequested
);
223 /* Increase the reference count */
224 InterlockedIncrement(&CurrentProcess
->RefCount
);
227 /* Leave the startup lock */
230 /* Return any Error */