summaryrefslogtreecommitdiffstats
path: root/private/ntos/nbt/vxd/cinit.c
blob: c8e9c4efdc871f3bfd7a5e9ee33f9cb24768b527 (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
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
/**********************************************************************/
/**                       Microsoft Windows                          **/
/**                Copyright(c) Microsoft Corp., 1993                **/
/**********************************************************************/

/*

    Init.c

    Contains VxD initialization code


    FILE HISTORY:
        Johnl   24-Mar-1993     Created

*/

#include <nbtprocs.h>
#include <tdiinfo.h>
#include <ipinfo.h>
#include <dhcpinfo.h>
#include <nbtinfo.h>
#include <hosts.h>

int Init( void ) ;
int RegisterLana( int Lana ) ;

NTSTATUS
NbtReadRegistry(
    OUT tNBTCONFIG *pConfig
    ) ;

extern char DNSSectionName[];  // Section where we find DNS domain name

VOID GetDNSInfo( VOID );

ULONG GetDhcpOption( PUCHAR ValueName, ULONG DefaultValue );

VOID ParseDomainNames(
    PUCHAR      *ppDomainName,
    PUCHAR      *ppDNSDomains
    );

//*******************  Pageable Routine Declarations ****************
#ifdef ALLOC_PRAGMA
#pragma CTEMakePageable(INIT, Init)
#pragma CTEMakePageable(PAGE, NbtReadRegistry)
#pragma CTEMakePageable(PAGE, GetDNSInfo)
#pragma CTEMakePageable(PAGE, CreateDeviceObject)
#pragma CTEMakePageable(PAGE, GetDhcpOption)
#endif
//*******************  Pageable Routine Declarations ****************

//
//  Initialized in VNBT_Device_Init with the protocol(s) this driver sits
//  on.  Note that we currently only support one.  This should *not* be in
//  the initialization data segments.
//
TDIDispatchTable * TdiDispatch ;
UCHAR              LanaBase ;
BOOL               fInInit = TRUE ;

//
//  Used in conjunction with the CHECK_INT_TABLE macro
//
#ifdef DEBUG
    BYTE abVecTbl[256] ;
    DWORD DebugFlags = DBGFLAG_ALL | DBGFLAG_KDPRINTS ;
    char  DBOut[4096] ;
    int   iCurPos = 0 ;

void NbtDebugOut( char * str )
{
    if ( DebugFlags & (DBGFLAG_AUX_OUTPUT | DBGFLAG_ERROR) )
        CTEPrint( str ) ;

    iCurPos += strlen( str ) + 1 ;

    if ( iCurPos >= sizeof(DBOut) )
        iCurPos = 0;
}

#endif  // DEBUG

#pragma BEGIN_INIT

//
//  While reading initialization parameters, we may need to go to
//  the DHCP driver.  This communicates to the init routine which device
//  we are currently interested in.
//  0xfffffff means get the requested option for any IP address.
//
//  MUST BE IN NETWORK ORDER!!!
//
ULONG   CurrentIP = 0xffffffff ;

/*******************************************************************

    NAME:       Init

    SYNOPSIS:   Performs all driver initialization

    RETURNS:    TRUE if initialization successful, FALSE otherwise

    NOTES:

    HISTORY:
        Johnl   24-Mar-1993     Created

********************************************************************/

int Init( void )
{
    NTSTATUS            status ;
    int                 i ;
    ULONG               ulTmp ;
    int                 Retval;


    Retval = FALSE;

    if ( CTEInitialize() )
    {
        DbgPrint("Init: CTEInitialize succeeded\n\r") ;
    }
    else
        goto fail_init;

    INIT_NULL_PTR_CHECK() ;

#ifdef DEBUG
    InitializeListHead(&DbgMemList);
    DbgLeakCheck = 0;
#endif

#ifdef CHICAGO
    //
    // Tell TDI who to call if someone underneath unloads
    //
    CTESetUnloadNotifyProc( (CTENotifyRtn)VxdUnload );

    //
    // prepare to read a bunch of parms from the registry
    //
    VxdOpenNdis();
#endif

    CTERefillMem() ;
    CTEZeroMemory( pNbtGlobConfig, sizeof(*pNbtGlobConfig));

    status = NbtReadRegistry( pNbtGlobConfig ) ;
    if ( !NT_SUCCESS( status ) )
    {
        DbgPrint("Init: NbtReadRegistry failed\n\r") ;
        goto fail_init;
    }

    InitializeListHead(&pNbtGlobConfig->DelayedEvents);

    InitializeListHead(&pNbtGlobConfig->BlockingNcbs);

    status = InitNotOs() ;
    if ( !NT_SUCCESS( status ) )
    {
        DbgPrint("Init: InitNotOs failed\n\r") ;
        goto fail_init;
    }

    status = InitTimersNotOs() ;
    if ( !NT_SUCCESS( status ) )
    {
        DbgPrint("Init: InitTimersNotOs failed\n\r") ;
        StopInitTimers() ;
        goto fail_init;
    }

#ifdef CHICAGO

    //
    // if name server and/or dns server are defined in registry, read them now
    //
    SaveNameDnsServerAddrs();

    //
    //  Register an IP notification routine when new adapters are added or
    //  DHCP brings up an address
    //

    if ( !IPRegisterAddrChangeHandler( IPNotification, TRUE))
    {
        DbgPrint("Init: Failed to register with IP driver\r\n") ;
        StopInitTimers() ;
        goto fail_init;
    }
#else
    //
    //  Find all the active Lanas
    //

    if ( !GetActiveLanasFromIP() )
    {
        DbgPrint("Init: Failed to get addresses from IP driver\r\n") ;
        StopInitTimers() ;
        goto fail_init;
    }

#endif

    //
    // find out where hosts file is, what's the domain name etc.
    //
    GetDNSInfo();

    //
    //  Get the NCB timeout timer going
    //
    if ( !CheckForTimedoutNCBs( NULL, NULL) )
    {
        DbgPrint("Init: CheckForTimedoutNCBs failed\n\r") ;
        StopInitTimers() ;
        goto fail_init;
    }

    fInInit = FALSE ;
    CTERefillMem() ;
    Retval = TRUE ;


fail_init:

#ifdef CHICAGO
    VxdCloseNdis();
#endif

    return( Retval );
}

//----------------------------------------------------------------------------
NTSTATUS
NbtReadRegistry(
    OUT tNBTCONFIG *pConfig
    )
/*++

Routine Description:

    This routine is called to get information from the registry,
    starting at RegistryPath to get the parameters.

Arguments:

    pNbtConfig - ptr to global configuration strucuture for NBT

Return Value:

    NTSTATUS - STATUS_SUCCESS if everything OK, STATUS_INSUFFICIENT_RESOURCES
            otherwise.

--*/
{
    NTSTATUS    Status = STATUS_SUCCESS ;
    int         i;

    CTEPagedCode();

    //
    // Initialize the Configuration data structure
    //
    CTEZeroMemory(pConfig,sizeof(tNBTCONFIG));

    ReadParameters( pConfig, NULL );

    //
    //  Allocate necessary memory for lmhosts support if a lmhosts file
    //  was specified (was read from .ini file in ReadParameters)
    //
    if ( pConfig->pLmHosts )
    {
        if ( !VxdInitLmHostsSupport( pConfig->pLmHosts,
                                     260 /*strlen(pConfig->pLmHosts)+1*/ ))
        {
            return STATUS_INSUFFICIENT_RESOURCES ;
        }

        pConfig->EnableLmHosts = TRUE ;
    }
    else
    {
        pConfig->EnableLmHosts = FALSE ;
    }

    // keep the size around for allocating memory, so that when we run over
    // OSI, only this value should change (in theory at least)
    pConfig->SizeTransportAddress = sizeof(TDI_ADDRESS_IP);

    // fill in the node type value that is put into all name service Pdus
    // that go out identifying this node type
    switch (NodeType)
    {
        case BNODE:
            pConfig->PduNodeType = 0;
            break;
        case PNODE:
            pConfig->PduNodeType = 1 << 13;
            break;
        case MNODE:
            pConfig->PduNodeType = 1 << 14;
            break;
        case MSNODE:
            pConfig->PduNodeType = 3 << 13;
            break;

    }

    LanaBase = (UCHAR) CTEReadSingleIntParameter( NULL,
                                                  VXD_LANABASE_NAME,
                                                  VXD_DEF_LANABASE,
                                                  0 ) ;
    CTEZeroMemory( LanaTable, NBT_MAX_LANAS * sizeof( LANA_ENTRY )) ;

    return Status;
}

/*******************************************************************

    NAME:       GetDNSInfo

    SYNOPSIS:   Gets path to windows dir, then appends hosts to it

    RETURNS:    Nothing

    NOTES:      If something goes wrong, path is set to NULL

    HISTORY:
        Koti   13-July-1994     Created

********************************************************************/

VOID GetDNSInfo( VOID )
{

    PUCHAR    pszWinPath;
    PUCHAR    pszHosts="hosts";
    PUCHAR    pszHostsPath=NULL;
#ifdef CHICAGO
    PUCHAR    pszParmName="Domain";
    PUCHAR    pszParm2Name = "SearchList";
#else
    PUCHAR    pszParmName="DomainName";
    PUCHAR    pszParm2Name = "DNSDomains";
#endif
    PUCHAR    pszDomName;
    PUCHAR    pchTmp;
    int       len;


    CTEPagedCode();

    NbtConfig.pHosts = NULL;

    //
    // Remember, pszWinPath has '\' at the end: (i.e. Get_Config_Directory
    // returns pointer to "c:\windows\" )
    //
    pszWinPath = VxdWindowsPath();

    //
    // doc implies Get_Config_Directory can't fail!  But we are paranoid...
    //

    if (pszWinPath == NULL)
    {
        pszHostsPath = NULL;
        return;
    }

    len = strlen(pszWinPath) + strlen(pszHosts) + 1;

    //
    // allocate memory to hold "c:\windows\hosts" or whatever
    //
    pszHostsPath = CTEAllocInitMem( len );
    if (pszHostsPath == NULL)
        return;

    strcpy(pszHostsPath, pszWinPath);
    strcat(pszHostsPath, pszHosts);

    NbtConfig.pHosts = pszHostsPath;

    NbtConfig.pDomainName = NULL;
//
// chicago gets the string from the registry, snowball from system.ini
//
    NbtConfig.pDNSDomains = NULL;

#ifdef CHICAGO
    if ( !CTEReadIniString( NULL, pszParmName, &pchTmp ) )
    {
          NbtConfig.pDomainName = pchTmp;
    }

    if ( !CTEReadIniString( NULL, pszParm2Name, &pchTmp ) )
    {
       if (pchTmp[0] != '\0')
       {
          NbtConfig.pDNSDomains = pchTmp;
       }
       else
       {
          CTEMemFree(pchTmp);
       }
    }
#else
    pchTmp = GetProfileString( pszParmName, NULL, DNSSectionName );
    if ( pchTmp != NULL )
    {
       if ( pszDomName = CTEAllocInitMem( strlen( pchTmp ) + 1 ) )
       {
          strcpy( pszDomName, pchTmp ) ;

          NbtConfig.pDomainName = pszDomName;
       }
    }

    pchTmp = GetProfileString( pszParm2Name, NULL, DNSSectionName );
    if ( pchTmp != NULL && pchTmp[0] != '\0')
    {
       if ( pszDomName = CTEAllocInitMem( strlen( pchTmp ) + 1) )
       {
          strcpy( pszDomName, pchTmp ) ;

          NbtConfig.pDNSDomains = pszDomName;
       }
    }
#endif

    ParseDomainNames( &NbtConfig.pDomainName, &NbtConfig.pDNSDomains);

    return;
}

/*******************************************************************

    NAME:       ParseDomainNames

    SYNOPSIS:   Extracts heirarchical domain names from the primary DNS
                domain name, and prepends any found to the domains list.

    ENTRY:      ppDomainName - pointer to pointer to primary DNS domain name
                ppDNSDomains - pointer to pointer to other DNS domain names

    EXIT:       *ppDNSDomains updated with pointer to new DNS domains list
                if any other heirarchical levels found in *ppDomainName.

    RETURNS:    nothing

    NOTES:

    HISTORY:
        EarleH  10-Jan-1996 Created

********************************************************************/

VOID ParseDomainNames(
    PUCHAR      *ppDomainName,
    PUCHAR      *ppDNSDomains
    )
{
    PUCHAR pStr;
    UINT   iCount;
    PUCHAR pDomainName = *ppDomainName, pDNSDomains = *ppDNSDomains, pNewDNSDomains;

    if ( pDomainName != NULL )
    {
        for ( iCount = 0, pStr = pDomainName ; pStr[0] != '\0' ; pStr++ )
        {
            if ( pStr[0] == '.' )
            {
                iCount += strlen ( pStr );
            }
        }
        if ( pDNSDomains != NULL )
        {
            iCount += strlen ( pDNSDomains );
            if ( iCount )
            {
                iCount++;                       // for the separator
            }
        }
        if (iCount++)                           // ++ for the terminator
        {
            pNewDNSDomains = CTEAllocInitMem( iCount );
            if ( pNewDNSDomains != NULL )
            {
                pNewDNSDomains[0] = '\0';
                for ( pStr = pDomainName ; pStr[0] != '\0' ; pStr++ )
                {
                    if ( pStr[0] == '.' )
                    {
                        pStr++;
                        if ( pNewDNSDomains[0] != '\0' )
                        {
                            strcat ( pNewDNSDomains, "," );
                        }
                        strcat ( pNewDNSDomains, pStr );
                    }
                }
                if ( pDNSDomains != NULL )
                {
                    strcat ( pNewDNSDomains, "," );
                    strcat ( pNewDNSDomains, pDNSDomains );
                    CTEMemFree( pDNSDomains );
                }
                if ( pNewDNSDomains[0] != '\0' )
                {
                    *ppDNSDomains = pNewDNSDomains;
                }
                else
                {
                    *ppDNSDomains = NULL;
                }
            }
        }
    }
}

#pragma END_INIT

#ifndef CHICAGO
#pragma BEGIN_INIT
#endif
/*******************************************************************

    NAME:       CreateDeviceObject

    SYNOPSIS:   Initializes the device list of the global configuration
                structure

    ENTRY:      pConfig - Pointer to global config structure
                IpAddr  - IP Address for this adapter
                IpMask  - IP Mask for this adapter
                IpNameServer - IP Address of the name server for this adapter
                IpBackupServer - IP Address of the backup name server for
                                 this adapter
                IpDnsServer - IP Address of the dns server for this adapter
                IpDnsBackupServer - IP Address of the backup dns server
                MacAddr - hardware address of the adapter for this IP addr
                IpIndex - Index of the IP Address in the IP Driver's address
                          table (used for setting address by DHCP)

    EXIT:       The device list in pConfig will be fully initialized

    RETURNS:    STATUS_SUCCESS if successful, error otherwise

    NOTES:

    HISTORY:
        Johnl   14-Apr-1993     Created

********************************************************************/

NTSTATUS CreateDeviceObject(
    IN  tNBTCONFIG  *pConfig,
    IN  ULONG        IpAddr,
    IN  ULONG        IpMask,
    IN  ULONG        IpNameServer,
    IN  ULONG        IpBackupServer,
    IN  ULONG        IpDnsServer,
    IN  ULONG        IpDnsBackupServer,
    IN  UCHAR        MacAddr[],
    IN  UCHAR        IpIndex
    )
{
    NTSTATUS            status;
    tDEVICECONTEXT    * pDeviceContext, *pDevtmp;
    ULONG               ulTmp ;
    NCB               * pNCB ;
    DHCPNotify          dn ;
    PLIST_ENTRY         pEntry;
    ULONG               Adapter;
    ULONG               PreviousNodeType;

    CTEPagedCode();

    pDeviceContext = CTEAllocInitMem(sizeof( tDEVICECONTEXT )) ;
    if ( !pDeviceContext )
        return STATUS_INSUFFICIENT_RESOURCES ;

    //
    // zero out the data structure
    //
    CTEZeroMemory( pDeviceContext, sizeof(tDEVICECONTEXT) );

    // put a verifier value into the structure so that we can check that
    // we are operating on the right data when the OS passes a device context
    // to NBT
    pDeviceContext->Verify = NBT_VERIFY_DEVCONTEXT;

    //
    // we aren't up yet: don't want ncb's coming in before we are ready!
    //
    pDeviceContext->fDeviceUp = FALSE;

    // setup the spin lock);
    CTEInitLock(&pDeviceContext->SpinLock);
    pDeviceContext->LockNumber         = DEVICE_LOCK;
    pDeviceContext->lNameServerAddress = IpNameServer ;
    pDeviceContext->lBackupServer      = IpBackupServer ;
    pDeviceContext->lDnsServerAddress  = IpDnsServer ;
    pDeviceContext->lDnsBackupServer   = IpDnsBackupServer ;

    // copy the mac addresss
    CTEMemCopy(&pDeviceContext->MacAddress.Address[0], MacAddr, 6);

    //
    // if the node type is set to Bnode by default then switch to Hnode if
    // there are any WINS servers configured.
    //
    PreviousNodeType = NodeType;

    if ((NodeType & DEFAULT_NODE_TYPE) &&
        (IpNameServer || IpBackupServer))
    {
        NodeType = MSNODE;
        if (PreviousNodeType & PROXY)
            NodeType |= PROXY;
    }

    //
    // start the refresh timer (if we had already started it, this function
    // just returns success)
    //
    status = StartRefreshTimer();

    if ( !NT_SUCCESS( status ) )
    {
        CTEFreeMem( pDeviceContext ) ;
        return( status ) ;
    }


    // initialize the pDeviceContext data structure.  There is one of
    // these data structured tied to each "device" that NBT exports
    // to higher layers (i.e. one for each network adapter that it
    // binds to.
    // The initialization sets the forward link equal to the back link equal
    // to the list head
    InitializeListHead(&pDeviceContext->UpConnectionInUse);
    InitializeListHead(&pDeviceContext->LowerConnection);
    InitializeListHead(&pDeviceContext->LowerConnFreeHead);
    InitializeListHead(&pDeviceContext->RcvAnyFromAnyHead);
    InitializeListHead(&pDeviceContext->RcvDGAnyFromAnyHead);
    InitializeListHead(&pDeviceContext->PartialRcvHead) ;

    InitializeListHead(&pDeviceContext->DelayedEvents);

    //
    //  Pick an adapter number that hasn't been used yet
    //
    Adapter = 1;
    for ( pEntry  = pConfig->DeviceContexts.Flink;
          pEntry != &pConfig->DeviceContexts;
          pEntry  = pEntry->Flink )
    {
        pDevtmp = CONTAINING_RECORD( pEntry, tDEVICECONTEXT, Linkage );

        if ( !(pDevtmp->AdapterNumber & Adapter) )
            break;

        Adapter <<= 1;
    }

    pDeviceContext->AdapterNumber = Adapter ;
    pDeviceContext->IPIndex       = IpIndex ;
    NbtConfig.AdapterCount++ ;
    if ( NbtConfig.AdapterCount > 1 )
    {
        NbtConfig.MultiHomed = TRUE ;
    }

    //
    //  Allocate our name table and session table watching for both a
    //  minimum and a maximum size.
    //

    pDeviceContext->cMaxNames = (UCHAR) min( pConfig->lRegistryMaxNames, MAX_NCB_NUMS ) ;

    pDeviceContext->cMaxSessions = (UCHAR) min( pConfig->lRegistryMaxSessions, MAX_NCB_NUMS ) ;

    //
    //  Add one to the table size for the zeroth element (used for permanent
    //  name in the name table).  The user accessible table goes from 1 to n
    //

    if ( !(pDeviceContext->pNameTable = (tCLIENTELE**)
            CTEAllocInitMem((USHORT)((pDeviceContext->cMaxNames+1) * sizeof(tADDRESSELE*)))) ||
         !(pDeviceContext->pSessionTable = (tCONNECTELE**)
            CTEAllocInitMem((pDeviceContext->cMaxSessions+1) * sizeof(tCONNECTELE*))))
    {
        return STATUS_INSUFFICIENT_RESOURCES ;
    }

    CTEZeroMemory( &pDeviceContext->pNameTable[0],
                   (pDeviceContext->cMaxNames+1)*sizeof(tCLIENTELE*)) ;
    CTEZeroMemory( &pDeviceContext->pSessionTable[0],
                   (pDeviceContext->cMaxSessions+1)*sizeof(tCONNECTELE*) ) ;
    pDeviceContext->iNcbNum = 1 ;
    pDeviceContext->iLSNum  = 1 ;

    // add this new device context on to the List in the configuration
    // data structure
    InsertTailList(&pConfig->DeviceContexts,&pDeviceContext->Linkage);

    //
    // IpAddr can be 0 only in wfw case (when dhcp hasn't yet obtained one)
    // (in case of chicago, we will never come this far if ipaddr is 0)
    //
    if (!IpAddr)
    {
        pDeviceContext->IpAddress = 0;
        goto Skip_tdiaddr_init;
    }

    //
    //  open the required address objects with the underlying transport provider
    //
    status = NbtCreateAddressObjects(
                    IpAddr,
                    IpMask,
                    pDeviceContext);
    if (!NT_SUCCESS(status))
    {
        KdPrint(("Failed to create the Address Object, status=%lC\n",status));
        return(status);
    }

    // this call must converse with the transport underneath to create
    // connections and associate them with the session address object
    status = NbtInitConnQ(
                &pDeviceContext->LowerConnFreeHead,
                sizeof(tLOWERCONNECTION),
                NBT_NUM_INITIAL_CONNECTIONS,
                pDeviceContext);

    if (!NT_SUCCESS(status))
    {
        CDbgPrint( DBGFLAG_ERROR,
                   ("CreateDeviceObject: NbtInitConnQ Failed!")) ;

        return(status);
    }

    //
    //  Add the permanent name for this adapter
    //
    status = NbtAddPermanentName( pDeviceContext ) ;
    if ( !NT_SUCCESS( status ))
    {
        return status ;
    }

Skip_tdiaddr_init:

    //
    // ok, we are ready to function! (we are setting this to TRUE even if
    // there is no ipaddr yet (in case of wfw only) so that rdr,srv etc. can
    // add names without error
    //
    pDeviceContext->fDeviceUp = TRUE;

#ifndef CHICAGO
    //
    //  Set up a DHCP notification for this device in case the IP address
    //  changes
    //
    dn.dn_pfnNotifyRoutine = AddrChngNotification ;
    dn.dn_pContext         = pDeviceContext ;

    status = DhcpSetInfo( DHCP_SET_NOTIFY_HANDLER,
                          pDeviceContext->IPIndex,
                          &dn,
                          sizeof( dn )) ;
    if ( status )
    {
        ASSERT(0);
        CDbgPrint( DBGFLAG_ERROR,
                   ("CreateDeviceObject: Warning - Setting Dhcp notification handler failed")) ;
    }
#endif //!CHICAGO

    return(STATUS_SUCCESS);
}

/*******************************************************************

    NAME:       GetDhcpOption

    SYNOPSIS:   Checks to see if the passed .ini parameter is a potential
                DHCP option.  If it is, it calls DHCP to get the option.

                This routine is called when retrieving parameters from
                the .ini file if the parameter is not found.

    ENTRY:      ValueName - String of .ini parameter name
                DefaultValue - Value to return if not a DHCP option or
                    DHCP didn't have the option

    RETURNS:    DHCP option value or DefaultValue if an error occurred.
                If the requested parameter is a string option (such as
                scopeid), then a pointer to an allocated string is
                returned.

    NOTES:      Name Server address is handled in GetNameServerAddress

    HISTORY:
        Johnl   17-Dec-1993     Created

********************************************************************/

#define OPTION_NETBIOS_SCOPE_OPTION     47
#define OPTION_NETBIOS_NODE_TYPE        46
#define OPTION_BROADCAST_ADDRESS        28

struct
{
    PUCHAR pchParamName ;
    ULONG  DhcpOptionID ;
} OptionMapping[] =
    {   { WS_NODE_TYPE,         OPTION_NETBIOS_NODE_TYPE      },
        { NBT_SCOPEID,          OPTION_NETBIOS_SCOPE_OPTION   },
        { WS_ALLONES_BCAST,     OPTION_BROADCAST_ADDRESS      }
    } ;
#define NUM_OPTIONS         (sizeof(OptionMapping)/sizeof(OptionMapping[0]))


ULONG GetDhcpOption( PUCHAR ValueName, ULONG DefaultValue )
{
    int          i ;
    ULONG        Val ;
    TDI_STATUS   tdistatus ;
    ULONG        Size ;
    INT          OptionId ;
    PUCHAR       pStrVal ;

    CTEPagedCode();

    //
    //  Is this parameter a DHCP option?
    //
    for ( i = 0 ; i < NUM_OPTIONS ; i++ )
    {
        if ( !strcmp( OptionMapping[i].pchParamName, ValueName ))
            goto FoundOption ;
    }

    return DefaultValue ;

FoundOption:

    switch ( OptionId = OptionMapping[i].DhcpOptionID )
    {
    case OPTION_NETBIOS_SCOPE_OPTION:               // String options go here

        //
        //  Get the size of the string resource, then get the option
        //

        Size = MAX_SCOPE_LENGTH+1 ;
        pStrVal = CTEAllocInitMem( Size );
        if (pStrVal == NULL)
        {
            DbgPrint("GetDhcpOption: failed to allocate memory") ;
            return 0 ;
        }

        tdistatus = DhcpQueryOption( CurrentIP,
                                     OptionId,
                                     pStrVal,
                                     &Size ) ;

        if ( tdistatus == TDI_SUCCESS )
        {
            DbgPrint("GetDhcpOption: Successfully retrieved option ID ") ;
            DbgPrintNum( OptionId ) ; DbgPrint("\r\n") ;
            return (ULONG) pStrVal ;
        }
        else
        {
            DbgPrint("GetDhcpOption: returned error = 0x ") ;
            DbgPrintNum( tdistatus ) ; DbgPrint("\r\n") ;
            CTEMemFree( pStrVal ) ;
            return 0 ;
        }

    default:                        // ULONG options go here
        Size = sizeof( Val ) ;
        tdistatus = DhcpQueryOption( CurrentIP,
                                     OptionId,
                                     &Val,
                                     &Size ) ;
        break ;
    }

    switch ( tdistatus )
    {
    case TDI_SUCCESS:
    case TDI_BUFFER_OVERFLOW:       // May be more then one, only take the 1st
        DbgPrint("GetDhcpOption: Successfully retrieved option ID ") ;
        DbgPrintNum( OptionId ) ; DbgPrint("\r\n") ;
        return Val ;

    case TDI_INVALID_PARAMETER:     // Option not found
        DbgPrint("GetDhcpOption: Failed to retrieve option ID ") ;
        DbgPrintNum( OptionId ) ; DbgPrint("\r\n") ;
        return DefaultValue ;

    default:
        ASSERT( FALSE ) ;
        break ;
    }

    return DefaultValue ;
}

#ifndef CHICAGO
#pragma END_INIT
#endif