d3304e4b75bd9843a9f32a7923a3fe4ea3867cd3
[reactos.git] / dll / win32 / ws2_32_new / src / wsautil.c
1 /*
2 * COPYRIGHT: See COPYING in the top level directory
3 * PROJECT: ReactOS WinSock 2 API
4 * FILE: wsautil.c
5 * PURPOSE: Winsock Utility Functions
6 * PROGRAMMER: Alex Ionescu (alex@relsoft.net)
7 */
8
9 /* INCLUDES ******************************************************************/
10 #include "ws2_32.h"
11
12 /* DATA **********************************************************************/
13
14 /* FUNCTIONS *****************************************************************/
15
16 HKEY
17 WSAAPI
18 WsOpenRegistryRoot(VOID)
19 {
20 HKEY WinsockRootKey;
21 INT ErrorCode;
22 ULONG CreateDisposition;
23
24 /* Open Registry Key */
25 ErrorCode = RegOpenKeyEx(HKEY_LOCAL_MACHINE,
26 WINSOCK_ROOT,
27 0,
28 MAXIMUM_ALLOWED,
29 &WinsockRootKey);
30
31 /* Check if it wasn't found */
32 if (ErrorCode == ERROR_FILE_NOT_FOUND)
33 {
34 /* Create it */
35 RegCreateKeyEx(HKEY_LOCAL_MACHINE,
36 WINSOCK_ROOT,
37 0,
38 NULL,
39 REG_OPTION_NON_VOLATILE,
40 KEY_ALL_ACCESS,
41 NULL,
42 &WinsockRootKey,
43 &CreateDisposition);
44 }
45 else if (ErrorCode == ERROR_SUCCESS)
46 {
47 /* It already exists */
48 CreateDisposition = REG_OPENED_EXISTING_KEY;
49 }
50
51 /* Check for failure */
52 if (ErrorCode != ERROR_SUCCESS) return NULL;
53
54 /* Check if we had to create a new key */
55 if (CreateDisposition == REG_CREATED_NEW_KEY)
56 {
57 /* Write the Winsock Version */
58 RegSetValueEx(WinsockRootKey,
59 "WinSock_Registry_Version",
60 0,
61 REG_SZ,
62 (BYTE*)"2.2",
63 4);
64 }
65 else
66 {
67 /* Read the Winsock Version */
68 }
69
70 /* Return the key */
71 return WinsockRootKey;
72 }
73
74 BOOL
75 WSAAPI
76 WsCheckCatalogState(IN HANDLE Event)
77 {
78 DWORD Return;
79
80 /* Wait for the object */
81 Return = WaitForSingleObject(Event, 0);
82
83 /* Check for the value */
84 if (Return == WAIT_OBJECT_0) return TRUE;
85
86 /* If it timedout or anything else, return false */
87 return FALSE;
88 }
89
90 INT
91 WSAAPI
92 WsApiProlog(OUT PWSPROCESS *Process,
93 OUT PWSTHREAD *Thread)
94 {
95 INT ErrorCode = WSANOTINITIALISED;
96
97 /* Try to get the current process */
98 if ((*Process = WsGetProcess()))
99 {
100 /* And the current thread */
101 ErrorCode = WsThreadGetCurrentThread(*Process, Thread);
102 }
103
104 /* Return init status */
105 return ErrorCode;
106 }
107
108 INT
109 WSAAPI
110 WsSlowProlog(VOID)
111 {
112 PWSPROCESS Process;
113 PWSTHREAD Thread;
114
115 /* Call the prolog */
116 return WsApiProlog(&Process, &Thread);
117 }
118
119 INT
120 WSAAPI
121 WsSlowPrologTid(OUT LPWSATHREADID *ThreadId)
122 {
123 PWSPROCESS Process;
124 PWSTHREAD Thread;
125 INT ErrorCode;
126
127 /* Call the prolog */
128 ErrorCode = WsApiProlog(&Process, &Thread);
129
130 /* Check for success */
131 if (ErrorCode == ERROR_SUCCESS)
132 {
133 /* Return the Thread ID */
134 *ThreadId = &Thread->WahThreadId;
135 }
136
137 /* Return status */
138 return ErrorCode;
139 }
140
141 INT
142 WSAAPI
143 WsSetupCatalogProtection(IN HKEY CatalogKey,
144 IN HANDLE CatalogEvent,
145 OUT LPDWORD UniqueId)
146 {
147 INT ErrorCode;
148 HKEY RegistryKey;
149 DWORD NewUniqueId;
150 CHAR KeyBuffer[32];
151 DWORD RegType = REG_DWORD;
152 DWORD RegSize = sizeof(DWORD);
153
154 /* Start loop */
155 do
156 {
157 #if 0
158 /* Ask for notifications */
159 ErrorCode = RegNotifyChangeKeyValue(CatalogKey,
160 FALSE,
161 REG_NOTIFY_CHANGE_NAME,
162 CatalogEvent,
163 TRUE);
164 if (ErrorCode != ERROR_SUCCESS)
165 {
166 /* Normalize error code */
167 ErrorCode = WSASYSCALLFAILURE;
168 break;
169 }
170 #endif
171
172 /* Read the current ID */
173 ErrorCode = RegQueryValueEx(CatalogKey,
174 "Serial_Access_Num",
175 0,
176 &RegType,
177 (LPBYTE)&NewUniqueId,
178 &RegSize);
179 if (ErrorCode != ERROR_SUCCESS)
180 {
181 /* Critical failure */
182 ErrorCode = WSASYSCALLFAILURE;
183 break;
184 }
185
186 /* Try to open it for writing */
187 sprintf(KeyBuffer, "%8.8lX", NewUniqueId);
188 ErrorCode = RegOpenKeyEx(CatalogKey,
189 KeyBuffer,
190 0,
191 MAXIMUM_ALLOWED,
192 &RegistryKey);
193
194 /* If the key doesn't exist or is being delete, that's ok for us */
195 if ((ErrorCode == ERROR_FILE_NOT_FOUND) ||
196 (ErrorCode == ERROR_KEY_DELETED))
197 {
198 /* Set success and return the new ID */
199 ErrorCode = ERROR_SUCCESS;
200 *UniqueId = NewUniqueId;
201 break;
202 }
203 else if (ErrorCode != ERROR_SUCCESS)
204 {
205 /* Any other failure is bad */
206 ErrorCode = WSASYSCALLFAILURE;
207 break;
208 }
209
210 /* If we could actually open the key, someone is using it :/ */
211 ErrorCode = RegCloseKey(RegistryKey);
212
213 /* In case we break out prematurely */
214 ErrorCode = WSANO_RECOVERY;
215
216 /* Keep looping until they let go of the registry writing */
217 } while (!WaitForSingleObject(CatalogEvent, 180 * 1000));
218
219 /* Return error code */
220 return ErrorCode;
221 }
222
223 INT
224 WSAAPI
225 MapUnicodeProtocolInfoToAnsi(IN LPWSAPROTOCOL_INFOW UnicodeInfo,
226 OUT LPWSAPROTOCOL_INFOA AnsiInfo)
227 {
228 INT ReturnValue;
229
230 /* Copy all the data that doesn't need converting */
231 RtlCopyMemory(AnsiInfo,
232 UnicodeInfo,
233 FIELD_OFFSET(WSAPROTOCOL_INFOA, szProtocol));
234
235 /* Now convert the protocol string */
236 ReturnValue = WideCharToMultiByte(CP_ACP,
237 0,
238 UnicodeInfo->szProtocol,
239 -1,
240 AnsiInfo->szProtocol,
241 sizeof(AnsiInfo->szProtocol),
242 NULL,
243 NULL);
244 if (!ReturnValue) return WSASYSCALLFAILURE;
245
246 /* Return success */
247 return ERROR_SUCCESS;
248 }
249
250 /*
251 * @implemented
252 */
253 VOID
254 WSAAPI
255 WEP(VOID)
256 {
257 return;
258 }