1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
/*
 * COPYRIGHT:   See COPYING in the top level directory
 * PROJECT:     ReactOS WinSock 2 API
 * FILE:        dll/win32/ws2_32_new/src/wsautil.c
 * PURPOSE:     Winsock Utility Functions
 * PROGRAMMER:  Alex Ionescu (alex@relsoft.net)
 */

/* INCLUDES ******************************************************************/

#include <ws2_32.h>

/* FUNCTIONS *****************************************************************/

HKEY
WSAAPI
WsOpenRegistryRoot(VOID)
{
    HKEY WinsockRootKey;
    INT ErrorCode;
    ULONG CreateDisposition;

    /* Open Registry Key */
    ErrorCode = RegOpenKeyEx(HKEY_LOCAL_MACHINE,
                             WINSOCK_ROOT,
                             0,
                             MAXIMUM_ALLOWED,
                             &WinsockRootKey);

    /* Check if it wasn't found */
    if (ErrorCode == ERROR_FILE_NOT_FOUND)
    {
        /* Create it */
        ErrorCode = RegCreateKeyEx(HKEY_LOCAL_MACHINE,
                                   WINSOCK_ROOT,
                                   0,
                                   NULL,
                                   REG_OPTION_NON_VOLATILE,
                                   KEY_ALL_ACCESS,
                                   NULL,
                                   &WinsockRootKey,
                                   &CreateDisposition);
    }
    else if (ErrorCode == ERROR_SUCCESS)
    {
        /* It already exists */
        CreateDisposition = REG_OPENED_EXISTING_KEY;
    }

    /* Check for failure */
    if (ErrorCode != ERROR_SUCCESS) return NULL;

    /* Check if we had to create a new key */
    if (CreateDisposition == REG_CREATED_NEW_KEY)
    {
        /* Write the Winsock Version */
        RegSetValueEx(WinsockRootKey,
                      "WinSock_Registry_Version",
                      0,
                      REG_SZ,
                      (BYTE*)"2.2",
                      4);
    }
    else
    {
        /* Read the Winsock Version */
    }

    /* Return the key */
    return WinsockRootKey;
}

BOOL
WSAAPI
WsCheckCatalogState(IN HANDLE Event)
{
    DWORD Return;

    /* Wait for the object */
    Return = WaitForSingleObject(Event, 0);

    /* Check for the value */
    if (Return == WAIT_OBJECT_0) return TRUE;

    /* If it timedout or anything else, return false */
    return FALSE;
}

INT
WSAAPI
WsApiProlog(OUT PWSPROCESS *Process,
            OUT PWSTHREAD *Thread)
{
    INT ErrorCode = WSANOTINITIALISED;

    /* Try to get the current process */
    if ((*Process = WsGetProcess()))
    {
        /* And the current thread */
        ErrorCode = WsThreadGetCurrentThread(*Process, Thread);
    }

    /* Return init status */
    return ErrorCode;
}

INT
WSAAPI
WsSlowProlog(VOID)
{
    PWSPROCESS Process;
    PWSTHREAD Thread;

    /* Call the prolog */
    return WsApiProlog(&Process, &Thread);
}

INT
WSAAPI
WsSlowPrologTid(OUT LPWSATHREADID *ThreadId)
{
    PWSPROCESS Process;
    PWSTHREAD Thread;
    INT ErrorCode;

    /* Call the prolog */
    ErrorCode = WsApiProlog(&Process, &Thread);

    /* Check for success */
    if (ErrorCode == ERROR_SUCCESS)
    {
        /* Return the Thread ID */
        *ThreadId = &Thread->WahThreadId;
    }

    /* Return status */
    return ErrorCode;
}

INT
WSAAPI
WsSetupCatalogProtection(IN HKEY CatalogKey,
                         IN HANDLE CatalogEvent,
                         OUT LPDWORD UniqueId)
{
    INT ErrorCode;
    HKEY RegistryKey;
    DWORD NewUniqueId;
    CHAR KeyBuffer[32];
    DWORD RegType = REG_DWORD;
    DWORD RegSize = sizeof(DWORD);

    /* Start loop */
    do
    {
#if 0
        /* Ask for notifications */
        ErrorCode = RegNotifyChangeKeyValue(CatalogKey,
                                            FALSE,
                                            REG_NOTIFY_CHANGE_NAME,
                                            CatalogEvent,
                                            TRUE);
        if (ErrorCode != ERROR_SUCCESS)
        {
            /* Normalize error code */
            ErrorCode = WSASYSCALLFAILURE;
            break;
        }
#endif

        /* Read the current ID */
        ErrorCode = RegQueryValueEx(CatalogKey,
                                    "Serial_Access_Num",
                                    0,
                                    &RegType,
                                    (LPBYTE)&NewUniqueId,
                                    &RegSize);
        if (ErrorCode != ERROR_SUCCESS)
        {
            /* Critical failure */
            ErrorCode = WSASYSCALLFAILURE;
            break;
        }

        /* Try to open it for writing */
        sprintf(KeyBuffer, "%8.8lX", NewUniqueId);
        ErrorCode = RegOpenKeyEx(CatalogKey,
                                 KeyBuffer,
                                 0,
                                 MAXIMUM_ALLOWED,
                                 &RegistryKey);

        /* If the key doesn't exist or is being delete, that's ok for us */
        if ((ErrorCode == ERROR_FILE_NOT_FOUND) ||
            (ErrorCode == ERROR_KEY_DELETED))
        {
            /* Set success and return the new ID */
            ErrorCode = ERROR_SUCCESS;
            *UniqueId = NewUniqueId;
            break;
        }
        else if (ErrorCode != ERROR_SUCCESS)
        {
            /* Any other failure is bad */
            ErrorCode = WSASYSCALLFAILURE;
            break;
        }

        /* If we could actually open the key, someone is using it :/ */
        ErrorCode = RegCloseKey(RegistryKey);<--- ErrorCode is assigned

        /* In case we break out prematurely */
        ErrorCode = WSANO_RECOVERY;<--- ErrorCode is overwritten

        /* Keep looping until they let go of the registry writing */
    } while (!WaitForSingleObject(CatalogEvent, 180 * 1000));

    /* Return error code */
    return ErrorCode;
}

INT
WSAAPI
MapUnicodeProtocolInfoToAnsi(IN LPWSAPROTOCOL_INFOW UnicodeInfo,
                             OUT LPWSAPROTOCOL_INFOA AnsiInfo)
{
    INT ReturnValue;

    /* Copy all the data that doesn't need converting */
    RtlCopyMemory(AnsiInfo,
                  UnicodeInfo,
                  FIELD_OFFSET(WSAPROTOCOL_INFOA, szProtocol));

    /* Now convert the protocol string */
    ReturnValue = WideCharToMultiByte(CP_ACP,
                                      0,
                                      UnicodeInfo->szProtocol,
                                      -1,
                                      AnsiInfo->szProtocol,
                                      sizeof(AnsiInfo->szProtocol),
                                      NULL,
                                      NULL);
    if (!ReturnValue) return WSASYSCALLFAILURE;

    /* Return success */
    return ERROR_SUCCESS;
}

/*
 * @implemented
 */
VOID
WSAAPI
WEP(VOID)
{
    return;
}