ReactOS  0.4.14-dev-608-gd495a4f
wsautil.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_new/src/wsautil.c
5  * PURPOSE: Winsock Utility Functions
6  * PROGRAMMER: Alex Ionescu (alex@relsoft.net)
7  */
8 
9 /* INCLUDES ******************************************************************/
10 
11 #include <ws2_32.h>
12 
13 /* FUNCTIONS *****************************************************************/
14 
15 HKEY
16 WSAAPI
18 {
19  HKEY WinsockRootKey;
20  INT ErrorCode;
22 
23  /* Open Registry Key */
26  0,
28  &WinsockRootKey);
29 
30  /* Check if it wasn't found */
32  {
33  /* Create it */
36  0,
37  NULL,
40  NULL,
41  &WinsockRootKey,
43  }
44  else if (ErrorCode == ERROR_SUCCESS)
45  {
46  /* It already exists */
48  }
49 
50  /* Check for failure */
51  if (ErrorCode != ERROR_SUCCESS) return NULL;
52 
53  /* Check if we had to create a new key */
55  {
56  /* Write the Winsock Version */
57  RegSetValueEx(WinsockRootKey,
58  "WinSock_Registry_Version",
59  0,
60  REG_SZ,
61  (BYTE*)"2.2",
62  4);
63  }
64  else
65  {
66  /* Read the Winsock Version */
67  }
68 
69  /* Return the key */
70  return WinsockRootKey;
71 }
72 
73 BOOL
74 WSAAPI
76 {
77  DWORD Return;
78 
79  /* Wait for the object */
80  Return = WaitForSingleObject(Event, 0);
81 
82  /* Check for the value */
83  if (Return == WAIT_OBJECT_0) return TRUE;
84 
85  /* If it timedout or anything else, return false */
86  return FALSE;
87 }
88 
89 INT
90 WSAAPI
93 {
95 
96  /* Try to get the current process */
97  if ((*Process = WsGetProcess()))
98  {
99  /* And the current thread */
101  }
102 
103  /* Return init status */
104  return ErrorCode;
105 }
106 
107 INT
108 WSAAPI
110 {
113 
114  /* Call the prolog */
115  return WsApiProlog(&Process, &Thread);
116 }
117 
118 INT
119 WSAAPI
121 {
124  INT ErrorCode;
125 
126  /* Call the prolog */
128 
129  /* Check for success */
130  if (ErrorCode == ERROR_SUCCESS)
131  {
132  /* Return the Thread ID */
133  *ThreadId = &Thread->WahThreadId;
134  }
135 
136  /* Return status */
137  return ErrorCode;
138 }
139 
140 INT
141 WSAAPI
143  IN HANDLE CatalogEvent,
144  OUT LPDWORD UniqueId)
145 {
146  INT ErrorCode;
147  HKEY RegistryKey;
148  DWORD NewUniqueId;
149  CHAR KeyBuffer[32];
150  DWORD RegType = REG_DWORD;
151  DWORD RegSize = sizeof(DWORD);
152 
153  /* Start loop */
154  do
155  {
156 #if 0
157  /* Ask for notifications */
158  ErrorCode = RegNotifyChangeKeyValue(CatalogKey,
159  FALSE,
161  CatalogEvent,
162  TRUE);
163  if (ErrorCode != ERROR_SUCCESS)
164  {
165  /* Normalize error code */
167  break;
168  }
169 #endif
170 
171  /* Read the current ID */
172  ErrorCode = RegQueryValueEx(CatalogKey,
173  "Serial_Access_Num",
174  0,
175  &RegType,
176  (LPBYTE)&NewUniqueId,
177  &RegSize);
178  if (ErrorCode != ERROR_SUCCESS)
179  {
180  /* Critical failure */
182  break;
183  }
184 
185  /* Try to open it for writing */
186  sprintf(KeyBuffer, "%8.8lX", NewUniqueId);
187  ErrorCode = RegOpenKeyEx(CatalogKey,
188  KeyBuffer,
189  0,
191  &RegistryKey);
192 
193  /* If the key doesn't exist or is being delete, that's ok for us */
194  if ((ErrorCode == ERROR_FILE_NOT_FOUND) ||
196  {
197  /* Set success and return the new ID */
199  *UniqueId = NewUniqueId;
200  break;
201  }
202  else if (ErrorCode != ERROR_SUCCESS)
203  {
204  /* Any other failure is bad */
206  break;
207  }
208 
209  /* If we could actually open the key, someone is using it :/ */
210  ErrorCode = RegCloseKey(RegistryKey);
211 
212  /* In case we break out prematurely */
214 
215  /* Keep looping until they let go of the registry writing */
216  } while (!WaitForSingleObject(CatalogEvent, 180 * 1000));
217 
218  /* Return error code */
219  return ErrorCode;
220 }
221 
222 INT
223 WSAAPI
225  OUT LPWSAPROTOCOL_INFOA AnsiInfo)
226 {
228 
229  /* Copy all the data that doesn't need converting */
230  RtlCopyMemory(AnsiInfo,
231  UnicodeInfo,
232  FIELD_OFFSET(WSAPROTOCOL_INFOA, szProtocol));
233 
234  /* Now convert the protocol string */
236  0,
237  UnicodeInfo->szProtocol,
238  -1,
239  AnsiInfo->szProtocol,
240  sizeof(AnsiInfo->szProtocol),
241  NULL,
242  NULL);
243  if (!ReturnValue) return WSASYSCALLFAILURE;
244 
245  /* Return success */
246  return ERROR_SUCCESS;
247 }
248 
249 /*
250  * @implemented
251  */
252 VOID
253 WSAAPI
255 {
256  return;
257 }
#define RegQueryValueEx
Definition: winreg.h:524
#define MAXIMUM_ALLOWED
Definition: nt_native.h:83
INT WSAAPI WsSlowProlog(VOID)
Definition: wsautil.c:109
#define IN
Definition: typedefs.h:38
#define TRUE
Definition: types.h:120
NTSYSAPI VOID NTAPI RtlCopyMemory(VOID UNALIGNED *Destination, CONST VOID UNALIGNED *Source, ULONG Length)
_In_ NDIS_ERROR_CODE ErrorCode
Definition: ndis.h:4436
BOOL WSAAPI WsCheckCatalogState(IN HANDLE Event)
Definition: wsautil.c:75
#define ERROR_SUCCESS
Definition: deptool.c:10
#define WideCharToMultiByte
Definition: compat.h:101
#define CP_ACP
Definition: compat.h:99
char CHAR
Definition: xmlstorage.h:175
#define REG_NOTIFY_CHANGE_NAME
Definition: winreg.h:38
INT WSAAPI MapUnicodeProtocolInfoToAnsi(IN LPWSAPROTOCOL_INFOW UnicodeInfo, OUT LPWSAPROTOCOL_INFOA AnsiInfo)
Definition: wsautil.c:224
#define REG_OPENED_EXISTING_KEY
Definition: nt_native.h:1085
VOID WSAAPI WEP(VOID)
Definition: wsautil.c:254
INT WSAAPI WsSlowPrologTid(OUT LPWSATHREADID *ThreadId)
Definition: wsautil.c:120
#define DWORD
Definition: nt_native.h:44
int32_t INT
Definition: typedefs.h:56
#define sprintf(buf, format,...)
Definition: sprintf.c:55
#define ERROR_KEY_DELETED
Definition: winerror.h:597
DWORD WINAPI WaitForSingleObject(IN HANDLE hHandle, IN DWORD dwMilliseconds)
Definition: synch.c:82
LONG WINAPI RegCloseKey(HKEY hKey)
Definition: reg.c:423
unsigned char * LPBYTE
Definition: typedefs.h:52
UINT32 void void ** ReturnValue
Definition: acevents.h:214
unsigned int BOOL
Definition: ntddk_ex.h:94
#define WINSOCK_ROOT
Definition: ws2_32p.h:10
smooth NULL
Definition: ftsmooth.c:416
FORCEINLINE PWSPROCESS WsGetProcess(VOID)
Definition: ws2_32p.h:885
#define REG_CREATED_NEW_KEY
Definition: nt_native.h:1084
#define ERROR_FILE_NOT_FOUND
Definition: disk.h:79
#define REG_OPTION_NON_VOLATILE
Definition: nt_native.h:1057
#define WSANOTINITIALISED
Definition: winerror.h:1987
#define WSANO_RECOVERY
Definition: winerror.h:2002
#define WAIT_OBJECT_0
Definition: winbase.h:387
INT WSAAPI WsSetupCatalogProtection(IN HKEY CatalogKey, IN HANDLE CatalogEvent, OUT LPDWORD UniqueId)
Definition: wsautil.c:142
unsigned long DWORD
Definition: ntddk_ex.h:95
_In_opt_ PFILE_OBJECT _In_opt_ PETHREAD Thread
Definition: fltkernel.h:2653
unsigned char BYTE
Definition: mem.h:68
#define WSASYSCALLFAILURE
Definition: winerror.h:1994
#define RegOpenKeyEx
Definition: winreg.h:520
LONG WINAPI RegNotifyChangeKeyValue(HKEY hKey, BOOL bWatchSubtree, DWORD dwNotifyFilter, HANDLE hEvent, BOOL fAsynchronous)
Definition: reg.c:3175
DWORD WSAAPI WsThreadGetCurrentThread(IN PWSPROCESS Process, IN PWSTHREAD *Thread)
Definition: dthread.c:279
#define KEY_ALL_ACCESS
Definition: nt_native.h:1041
#define WSAAPI
Definition: winsock2.h:584
#define FIELD_OFFSET(t, f)
Definition: typedefs.h:254
INT WSAAPI WsApiProlog(OUT PWSPROCESS *Process, OUT PWSTHREAD *Thread)
Definition: wsautil.c:91
HKEY WSAAPI WsOpenRegistryRoot(VOID)
Definition: wsautil.c:17
_Must_inspect_result_ _In_ PLARGE_INTEGER _In_ PLARGE_INTEGER _In_ ULONG _In_ PFILE_OBJECT _In_ PVOID Process
Definition: fsrtlfuncs.h:219
#define OUT
Definition: typedefs.h:39
uint32_t * LPDWORD
Definition: typedefs.h:57
unsigned int ULONG
Definition: retypes.h:1
#define REG_DWORD
Definition: sdbapi.c:596
_In_ PIO_STACK_LOCATION _Inout_ PFILE_OBJECT _Inout_ PVCB _Outptr_result_maybenull_ PDCB _In_ PDCB _In_ PDIRENT _In_ ULONG _In_ ULONG _In_ PUNICODE_STRING _In_ PACCESS_MASK _In_ USHORT _In_ ULONG CreateDisposition
Definition: create.c:4157
#define RegSetValueEx
Definition: winreg.h:533
#define RegCreateKeyEx
Definition: winreg.h:501
#define HKEY_LOCAL_MACHINE
Definition: winreg.h:12
#define REG_SZ
Definition: layer.c:22