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
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
/*
 * COPYRIGHT:   See COPYING in the top level directory
 * PROJECT:     ReactOS WinSock 2 API
 * FILE:        dll/win32/ws2_32_new/src/getproto.c
 * PURPOSE:     GetProtoByY Functions.
 * PROGRAMMER:  Alex Ionescu (alex@relsoft.net)
 */

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

#include <ws2_32.h>

#define NDEBUG
#include <debug.h>

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

HANDLE
WSAAPI
GetProtoOpenNetworkDatabase(PCHAR Name)
{
    PCHAR ExpandedPath;
    PCHAR DatabasePath;
    INT ErrorCode;
    HKEY DatabaseKey;
    DWORD RegType;
    DWORD RegSize = 0;
    HANDLE ret;

    ExpandedPath = HeapAlloc(WsSockHeap, 0, MAX_PATH);
    if (!ExpandedPath)
        return INVALID_HANDLE_VALUE;

    /* Open the database path key */
    ErrorCode = RegOpenKeyEx(HKEY_LOCAL_MACHINE,
                             "System\\CurrentControlSet\\Services\\Tcpip\\Parameters",
                             0,
                             KEY_READ,
                             &DatabaseKey);
    if (ErrorCode == NO_ERROR)
    {
        /* Read the actual path */
        ErrorCode = RegQueryValueEx(DatabaseKey,<--- ErrorCode is assigned
                                    "DatabasePath",
                                    NULL,
                                    &RegType,
                                    NULL,
                                    &RegSize);

        DatabasePath = HeapAlloc(WsSockHeap, 0, RegSize);
        if (!DatabasePath)
        {
            HeapFree(WsSockHeap, 0, ExpandedPath);
            return INVALID_HANDLE_VALUE;
        }

        /* Read the actual path */
        ErrorCode = RegQueryValueEx(DatabaseKey,<--- ErrorCode is overwritten
                                    "DatabasePath",
                                    NULL,
                                    &RegType,
                                    (LPBYTE)DatabasePath,
                                    &RegSize);


        /* Close the key */
        RegCloseKey(DatabaseKey);

        /* Expand the name */
        ExpandEnvironmentStrings(DatabasePath, ExpandedPath, MAX_PATH);

        HeapFree(WsSockHeap, 0, DatabasePath);
    }
    else
    {
        /* Use default path */
        GetSystemDirectory(ExpandedPath, MAX_PATH);
        if (ExpandedPath[strlen(ExpandedPath) - 1] != '\\')
        {
            /* It isn't, so add it ourselves */
            strncat(ExpandedPath, "\\", MAX_PATH);
        }
        strncat(ExpandedPath, "DRIVERS\\ETC\\", MAX_PATH);
    }

    /* Make sure that the path is backslash-terminated */
    if (ExpandedPath[strlen(ExpandedPath) - 1] != '\\')
    {
        /* It isn't, so add it ourselves */
        strncat(ExpandedPath, "\\", MAX_PATH);
    }

    /* Add the database name */
    strncat(ExpandedPath, Name, MAX_PATH);

    /* Return a handle to the file */
    ret = CreateFile(ExpandedPath,
                      FILE_READ_ACCESS,
                      0,
                      NULL,
                      OPEN_EXISTING,
                      FILE_ATTRIBUTE_NORMAL,
                      NULL);

    HeapFree(WsSockHeap, 0, ExpandedPath);
    return ret;
}

PCHAR
WSAAPI
GetProtoPatternMatch(IN PCHAR Buffer,
                     IN PCHAR Lookup)
{
    CHAR ScanChar;

    /* Loop as long as we have data */
    while ((ScanChar = *Buffer))
    {
        /* Check for a match and return its pointer if found */
        if (strchr(Lookup, ScanChar)) return Buffer;

        /* Keep going */
        Buffer++;
    }

    /* Nothing found */
    return NULL;
}

PPROTOENT
WSAAPI
GetProtoGetNextEnt(IN HANDLE DbHandle,
                   IN PWSPROTO_BUFFER Buffer)
{
    DWORD Read;
    LONG n;
    PCHAR p, p1, Entry, *Aliases;
    PPROTOENT ReturnedProtoent;

    /* Find out where we currently are in the file */
    n = SetFilePointer(DbHandle, 0, 0, FILE_CURRENT);

    while (TRUE)
    {
        /* Read 512 bytes */
        if (!ReadFile(DbHandle,
                      Buffer->LineBuffer,
                      sizeof(Buffer->LineBuffer) - 1,
                      &Read,
                      NULL)) return NULL;

        /* Find out where the line ends */
        p1 = Buffer->LineBuffer;
        Buffer->LineBuffer[Read] = ANSI_NULL;
        p = strchr(Buffer->LineBuffer, '\n');

        /* Bail out if the file is parsed */
        if (!p) return NULL;

        /* Calculate our new position */
        n += (LONG)(p - p1) + 1;

        /* Make it so we read from there next time */
        SetFilePointer(DbHandle, n, 0, FILE_BEGIN);

        /* Null-terminate the buffer so it only contains this line */
        *p = ANSI_NULL;

        /* If this line is a comment, skip it */
        if (*p1 == '#') continue;

        /* Get the entry in this line and null-terminate it */
        Entry = GetProtoPatternMatch(p1, "#\n");
        if (!Entry) continue;
        *Entry = ANSI_NULL;

        /* Start with the name */
        Buffer->Protoent.p_name = p1;

        /* Get the first tab and null-terminate */
        Entry = GetProtoPatternMatch(p1, " \t");
        if (!Entry) continue;
        *Entry++ = ANSI_NULL;

        /* Handle remaining tabs or spaces */
        while (*Entry == ' ' || *Entry == '\t') Entry++;

        /* Now move our read pointer */
        p1 = GetProtoPatternMatch(Entry, " \t");
        if (p1) *p1++ = ANSI_NULL;

        /* This is where the address is */
        Buffer->Protoent.p_proto = (short)atoi(Entry);

        /* Setup the alias buffer */
        Buffer->Protoent.p_aliases = Buffer->Aliases;
        Aliases = Buffer->Protoent.p_aliases;

        /* Check if the pointer is stil valid */
        if (p1)
        {
            /* The first entry is here */
            Entry = p1;

            /* Loop while there are non-null entries */
            while (Entry && *Entry)
            {
                /* Handle tabs and spaces */
                while (*Entry == ' ' || *Entry == '\t') Entry++;

                /* Make sure we don't go over the buffer */
                if (Aliases < &Buffer->Protoent.p_aliases[MAXALIASES - 1])
                {
                    /* Write the alias */
                    *Aliases++ = Entry;
                }

                /* Get to the next entry */
                Entry = GetProtoPatternMatch(Entry, " \t");
                if (Entry) *Entry++ = ANSI_NULL;
            }
        }

        /* Terminate the list */
        *Aliases = NULL;

        /* Return to caller */
        ReturnedProtoent = &Buffer->Protoent;
        break;
    }

    /* Return whatever we got */
    return ReturnedProtoent;
}

/*
 * @implemented
 */
LPPROTOENT
WSAAPI
getprotobynumber(IN INT number)
{
    PWSPROCESS Process;
    PWSTHREAD Thread;
    INT ErrorCode;
    PPROTOENT Protoent;
    PVOID GetProtoBuffer;
    HANDLE DbHandle;
    DPRINT("getprotobynumber: %lx\n", number);

    /* Enter prolog */
    if ((ErrorCode = WsApiProlog(&Process, &Thread)) != ERROR_SUCCESS)
    {
        /* Leave now */
        SetLastError(ErrorCode);
        return NULL;
    }

    /* Get our buffer */
    GetProtoBuffer = WsThreadGetProtoBuffer(Thread);
    if (!GetProtoBuffer)
    {
        /* Fail */
        SetLastError(WSANO_DATA);
        return NULL;
    }

    /* Open the network database */
    DbHandle = GetProtoOpenNetworkDatabase("protocol");
    if (DbHandle == INVALID_HANDLE_VALUE)
    {
        /* Couldn't open the DB; fail */
        SetLastError(WSANO_DATA);
        return NULL;
    }

    /* Start the scan loop */
    while (TRUE)
    {
        /* Get a protoent entry */
        Protoent = GetProtoGetNextEnt(DbHandle, GetProtoBuffer);

        /* Break if we didn't get any new one */
        if (!Protoent) break;

        /* Break if we have a match */
        if (Protoent->p_proto == number) break;
    }

    /* Close the network database */
    CloseHandle(DbHandle);

    /* Set error if we don't have a protoent */
    if (!Protoent) SetLastError(WSANO_DATA);

    /* Return it */
    return Protoent;
}

/*
 * @implemented
 */
LPPROTOENT
WSAAPI
getprotobyname(IN CONST CHAR FAR *name)
{
    PWSPROCESS Process;
    PWSTHREAD Thread;
    INT ErrorCode;
    PPROTOENT Protoent;
    PVOID GetProtoBuffer;
    HANDLE DbHandle;
    DPRINT("getprotobyname: %s\n", name);

    /* Enter prolog */
    if ((ErrorCode = WsApiProlog(&Process, &Thread)) != ERROR_SUCCESS)
    {
        /* Leave now */
        SetLastError(ErrorCode);
        return NULL;
    }

    /* Get our buffer */
    GetProtoBuffer = WsThreadGetProtoBuffer(Thread);
    if (!GetProtoBuffer)
    {
        /* Fail */
        SetLastError(WSANO_DATA);
        return NULL;
    }

    /* Open the network database */
    DbHandle = GetProtoOpenNetworkDatabase("protocol");
    if (DbHandle == INVALID_HANDLE_VALUE)
    {
        /* Couldn't open the DB; fail */
        SetLastError(WSANO_DATA);
        return NULL;
    }

    /* Start the scan loop */
    while (TRUE)
    {
        /* Get a protoent entry */
        Protoent = GetProtoGetNextEnt(DbHandle, GetProtoBuffer);

        /* Break if we didn't get any new one */
        if (!Protoent) break;

        /* Break if we have a match */
        if (!_stricmp(Protoent->p_name, name)) break;
    }

    /* Close the network database */
    CloseHandle(DbHandle);

    /* Set error if we don't have a protoent */
    if (!Protoent) SetLastError(WSANO_DATA);

    /* Return it */
    return Protoent;
}