summaryrefslogtreecommitdiffstats
path: root/private/ntos/ndis/testprot/tpctl/globals.c
blob: 49f9c5d6e7177680d13654f105d212779777ab58 (plain) (blame)
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
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
// ******************************************************************
//
// Copyright (c) 1991 Microsoft Corporation
//
// Module Name:
// 
//     globals.c
// 
// Abstract:
// 
//     This module contains the routines for parsing global variables entered from
//     the command line or read from script files.
// 
// Author:
// 
//     Tim Wynsma (timothyw) 5-18-94    
// 
// Revision History:
// 
// ******************************************************************


#include <nt.h>
#include <ntrtl.h>
#include <nturtl.h>

#include <windows.h>


#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>

#include "tpctl.h"
#include "parse.h"

typedef struct _GLOBALS
{
    // String variables

    UCHAR   TestCard[64];
    UCHAR   TrustedCard[64];

    // Address6 variables

    UCHAR   TestCardAddress[6];
    UCHAR   TrustedCardAddress[6];
    UCHAR   MulticastAddress[6];
    UCHAR   MulticastAddress2[6];
    UCHAR   BroadcastAddress[6];
    UCHAR   RandomAddress[6];
    UCHAR   RemoteTestCardAddress[6];
    UCHAR   RemoteTrustedCardAddress[6];

    // Address4 variables

    UCHAR   FunctionalAddress[4];
    UCHAR   FunctionalAddress2[4];

    // Integer variables

    ULONG   MaxFrameSize;
    ULONG   MaxLookaheadSize;

} GLOBALS;

GLOBALS glob;

typedef struct _GLOBALVAR
{
    PUCHAR      varname;
    PARAMTYPES  vartype;
    PVOID       varaddress;
} GLOBALVAR, *PGLOBALVAR;


GLOBALVAR   globalvars[] = 
  { { "test_card",                  String,     glob.TestCard                   },
    { "trusted_card",               String,     glob.TrustedCard                },
    { "test_card_address",          Address6,   glob.TestCardAddress            },
    { "trusted_card_address",       Address6,   glob.TrustedCardAddress         },
    { "multicast_address",          Address6,   glob.MulticastAddress           },
    { "multicast_address2",         Address6,   glob.MulticastAddress2          },
    { "broadcast_address",          Address6,   glob.BroadcastAddress           },
    { "random_address",             Address6,   glob.RandomAddress              },
    { "rem_test_card_address",      Address6,   glob.RemoteTestCardAddress      },
    { "rem_trusted_card_address",   Address6,   glob.RemoteTrustedCardAddress   },
    { "functional_address",         Address4,   glob.FunctionalAddress          },
    { "functional_address2",        Address4,   glob.FunctionalAddress2         },
    { "max_frame_size",             Integer,    &glob.MaxFrameSize              },
    { "max_lookahead_size",         Integer,    &glob.MaxLookaheadSize          }
   };


DWORD
NumGlobalVars = sizeof(globalvars) / sizeof(globalvars[0]);



DWORD
ParseGlobalArgs(OUT PUCHAR  commandline,
                OUT LPSTR   tokenptr[],
                IN  DWORD   ArgC,
                IN  LPSTR   ArgV[]);



PVOID
TpctlParseGlobalVariable(
    IN BYTE         Buffer[],
    IN PARAMTYPES   reqtype
    )

// -------------
// 
// Routine Description:
// 
// Arguments:
// 
// Return Value:
// 
// ------------

{
    BYTE    TmpBuffer[100];
    LPSTR   EndOfVar = Buffer;        // Anything that isn't NULL.
    ULONG   count;


    //
    // make sure that there is actually something passed in..
    //

    if ( Buffer == NULL) 
    {
        return NULL;
    }

    //
    // copy the variable into a temp buffer.
    //

    strcpy( TmpBuffer,&Buffer[1] );

    //
    // Now null out the '$' symbol if it exists to allow the querying
    // of the global variable.
    //

    EndOfVar = strchr( TmpBuffer,'$' );

    if ( EndOfVar == NULL ) 
    {
        return NULL;
    }

    *EndOfVar = '\0';

    //
    //  Search for the named global variable
    //

    for (count=0; count < NumGlobalVars; count++)
    {
        if (!_stricmp(TmpBuffer, globalvars[count].varname))
        {
            //
            // Make sure the required type matched the type of the global
            //
            if (globalvars[count].vartype != reqtype)
            {
                return NULL;
            }
            return globalvars[count].varaddress;

        }
    }

    return NULL;
}

VOID
TpctlInitGlobalVariables(VOID)
{
    ULONG   count;
    UCHAR   NameBuf[128];
    PUCHAR  varptr;
    LPBYTE  NextToken;


    //
    // first, initialize those globals that will always have a certain value...
    // (all others should already be zero)
    //

    glob.MulticastAddress[0] = 0x01;
    glob.MulticastAddress[1] = 0x02;
    glob.MulticastAddress[2] = 0x03;
    glob.MulticastAddress[3] = 0x04;
    glob.MulticastAddress[4] = 0x05;
    glob.MulticastAddress[5] = 0x00;

    glob.MulticastAddress2[0] = 0x01;
    glob.MulticastAddress2[1] = 0x02;
    glob.MulticastAddress2[2] = 0x03;
    glob.MulticastAddress2[3] = 0x04;
    glob.MulticastAddress2[4] = 0x05;
    glob.MulticastAddress2[5] = 0x01;

    glob.BroadcastAddress[0] = 0xFF;
    glob.BroadcastAddress[1] = 0xFF;
    glob.BroadcastAddress[2] = 0xFF;
    glob.BroadcastAddress[3] = 0xFF;
    glob.BroadcastAddress[4] = 0xFF;
    glob.BroadcastAddress[5] = 0xFF;

    glob.RandomAddress[0] = 0x00;
    glob.RandomAddress[1] = 0x02;
    glob.RandomAddress[2] = 0x04;
    glob.RandomAddress[3] = 0x06;
    glob.RandomAddress[4] = 0x08;
    glob.RandomAddress[5] = 0x0A;

    glob.FunctionalAddress[0] = 0xC0;
    glob.FunctionalAddress[1] = 0x02;
    glob.FunctionalAddress[2] = 0x03;
    glob.FunctionalAddress[3] = 0x04;

    glob.FunctionalAddress2[0] = 0x00;
    glob.FunctionalAddress2[1] = 0x00;
    glob.FunctionalAddress2[2] = 0x00;
    glob.FunctionalAddress2[3] = 0x00;


    //
    // now, loop thru all the global vars, checking for an associated environment variable
    // If the env variable is found, then set that global variable accordingly.
    //

    for (count=0; count < NumGlobalVars; count++)
    {
        strcpy(NameBuf, "tp_");
        strcat(NameBuf, globalvars[count].varname);
        varptr = getenv( _strupr( NameBuf ));
        if (varptr != NULL)
        {
            switch ( globalvars[count].vartype ) 
            {
                case Integer:
                    *(PDWORD)globalvars[count].varaddress = strtol( varptr,&NextToken,0 );
                    break;

                case String:
                    strcpy( (LPSTR)globalvars[count].varaddress,varptr );
                    break;

                case Address4:
                    TpctlParseAddress( varptr,
                                      (PDWORD)globalvars[count].varaddress,
                                       0,               
                                       FUNCTIONAL_ADDRESS_LENGTH );
                    break;

                case Address6:
                    TpctlParseAddress( varptr,
                                      (PDWORD)globalvars[count].varaddress,
                                       0,               
                                       ADDRESS_LENGTH ) ; 
                    break;
            }
        }
    }
}


DWORD
TpctlParseSet(
    IN DWORD ArgC,
    IN LPSTR ArgV[]
    )

{
    DWORD   count;
    UCHAR   commandline[120];
    LPSTR   tokenptr[20];
    DWORD   numstrings;

    printf("\nIn TpctlParseSet\n");

    if (ArgC < 2)
    {
        printf("Error in setglobalvar command: no arguments\n");
        return 0;
    }

    numstrings = ParseGlobalArgs(commandline,tokenptr,ArgC, ArgV);

    for (count=0; count < numstrings; count++)
    {
        printf("token %d equals \"%s\".\n",count, tokenptr[count]);
    }
    printf("\n");

    // now that they are all parsed into separate strings
    return 0;      
}


DWORD
ParseGlobalArgs(OUT PUCHAR  clptr,
                OUT LPSTR   tokenptr[],
                IN  DWORD   ArgC,
                IN  LPSTR   ArgV[])

{

    DWORD   count;
    DWORD   tokencnt = 0;
    LPSTR   srcptr;
    DWORD   state;
    DWORD   chtype;
    UCHAR   ch;

    // parse into legal strings.  For our purposes, the following are legal strings:
    // 1)  Global variable.  Must start and end with a '$'.  Legal characters are 'A-Z', 'a-z',
    //     '0-9', and '_'.  Lowercase are converted to uppercase
    // 2)  Environment variable.  Same as global, except must start and end with a '%'
    // 3)  Number.  Must contain only digits '0' thru '9'
    // 4)  Address.  Must start and end with a '&'.  Fields are in hex, separated by '-'.
    //               For example, &00-03-a3-f1-07-54&
    // 5)  Comparisons  Legal strings are "=", "<", ">", "<>", "<=", ">="
    // 6)  Operators.  Legal strings are '+', '-', '*', '/'


    for(count=1; count < ArgC; count++)
    {
        srcptr = ArgV[count];
        state  = 0; 

        while ( (ch = *srcptr++) != 0)
        {
            if ((ch >= '0') && (ch <= '9'))
            {
                chtype = 1;
            }
            else if ((ch == '%') || (ch == '$') || ((ch >= 'A') && (ch <= 'Z')))
            {
                chtype = 2;
            }
            else if ((ch >= 'a') && (ch <= 'z'))
            {
                chtype = 2;
                ch = toupper(ch);
            }
            else if ((ch == '(') || (ch == ')') || (ch == '+') || (ch = '-') ||
                     (ch == '*') || (ch == '/') || (ch == '='))
            {
                chtype = 3;
            }
            else
            {
                printf("Error in setglobalvar command--illegal char\n");
                return 0;
            }

            if (chtype != state)
            {
                if (state != 0)
                {
                    *clptr++ = 0;
                }
                tokenptr[tokencnt++] = clptr;
                if (chtype == 3)
                {
                    state = 4;
                }
                else
                {
                    state = chtype;
                }
            }
            *clptr++ = ch;
        }
        *clptr++ = 0;
    }
    
    return tokencnt;
}