summaryrefslogtreecommitdiffstats
path: root/private/ntos/nbt/vxd/nbtinfo.c
blob: ad267685f0542b8ac4f61348dc718ee2cafabe04 (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
/**********************************************************************/
/**			  Microsoft Windows/NT			     **/
/**                Copyright(c) Microsoft Corp., 1993                **/
/**********************************************************************/

/*
    Nbtinfo.c

    This file contains the NBT Info APIs



    FILE HISTORY:
        Johnl       13-Dec-1993     Created

*/


#include <nbtprocs.h>
#include <dhcpinfo.h>
#include <nbtinfo.h>

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

    NAME:       AddrChngNotification

    SYNOPSIS:   Notification handler called by Dhcp when an IpAddress
                lease has expired or changed.

    ENTRY:      Context      - Pointer to device context
                OldIpAddress - in network order
                NewIpAddress - in network order
                NewMask      - in network order

    NOTES:

    HISTORY:
        Johnl       21-Dec-1993     Created

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

VOID AddrChngNotification( PVOID Context,
                           ULONG OldIpAddress,
                           ULONG NewIpAddress,
                           ULONG NewMask )
{
    tDEVICECONTEXT  * pDeviceContext = (tDEVICECONTEXT*) Context ;
    TDI_STATUS        tdistatus ;
    NTSTATUS          status ;
    ULONG             IpBuff[4] ;
    UINT              Size ;
    ULONG             TmpNodeType;

    DbgPrint("DhcpNotification: Nbt being notified of IP Address change by DHCP\r\n") ;

    //
    //  NBT assumes the address goes to zero then comes up on the new
    //  address, so if the address is going to a new address (not to
    //  zero first) then fake it.
    //

    if ( NewIpAddress && pDeviceContext->IpAddress )
    {
        if ( status = NbtNewDhcpAddress( pDeviceContext, 0, 0 ) )
        {
            CDbgPrint( DBGFLAG_ERROR, ("DhcpNotification: NbtSetNewDhcpAddress failed")) ;
        }
    }

    if ( NewIpAddress == 0 )
    {
        if ( status = NbtNewDhcpAddress( pDeviceContext, 0, 0 ) )
        {
            CDbgPrint( DBGFLAG_ERROR, ("DhcpNotification: NbtSetNewDhcpAddress failed")) ;
        }
        pDeviceContext->IpAddress = 0 ;
        return ;
    }

    //
    //  Get all of the values that may change when the IP address changes.
    //  Currently this is only NBNS (scope & broadcast address are global
    //  NBT config parameters).
    //

    Size = sizeof( IpBuff ) ;
    tdistatus = DhcpQueryOption( NewIpAddress,
                                 44,            // NBNS
                                 IpBuff,
                                 &Size ) ;

    if ( tdistatus != TDI_SUCCESS &&
         tdistatus != TDI_BUFFER_OVERFLOW )
    {
        CDbgPrint( DBGFLAG_ERROR, ("DhcpNotification: Query on NBNS failed")) ;
    }
    else
    {
        if ( Size >= 4 )
            pDeviceContext->lNameServerAddress = ntohl(IpBuff[0]) ;

        if ( Size >= 8 )
            pDeviceContext->lBackupServer = ntohl(IpBuff[1]) ;
    }

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

    if ((NodeType & DEFAULT_NODE_TYPE) &&
        (pDeviceContext->lNameServerAddress || pDeviceContext->lBackupServer))
    {
        NodeType = MSNODE;
        if (TmpNodeType & PROXY)
            NodeType |= PROXY;
    }

    //
    //  Now set the new IP address
    //

    status = NbtNewDhcpAddress( pDeviceContext,
                                NewIpAddress,
                                NewMask ) ;

    if ( NT_SUCCESS(status) )
    {
        if (pDeviceContext->IpAddress)
        {
            //
            // Add the "permanent" name to the local name table.
            //
            status = NbtAddPermanentName(pDeviceContext);

            if (!(NodeType & BNODE))
            {
               // the Ip address just changed and Dhcp may be informing
               // us of a new Wins Server addresses, so refresh all the
               // names to the new wins server
               //
               ReRegisterLocalNames();
            }
            else
            {
                //
                // no need to refresh on a Bnode
                //
                LockedStopTimer(&NbtConfig.pRefreshTimer);
            }
        }
    }

    else
    {
        CDbgPrint( DBGFLAG_ERROR, ("DhcpNotification: NbtSetNewDhcpAddress failed")) ;
    }



}


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

    NAME:       CloseAddressesWithTransport

    SYNOPSIS:   Closes address objects on the passed in device

    ENTRY:      pDeviceContext - Device context to close

    NOTES:      Used after an IP address loses its DHCP lease by OS
                independent code.

    HISTORY:
        Johnl   13-Dec-1993     Created

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

NTSTATUS
CloseAddressesWithTransport(
    IN tDEVICECONTEXT   *pDeviceContext )
{
    TDI_REQUEST       Request ;
    NTSTATUS          status;


    if (pDeviceContext->pDgramFileObject)
    {
        Request.Handle.AddressHandle = pDeviceContext->pDgramFileObject ;
        if ( TdiVxdCloseAddress( &Request ))
            CDbgPrint( DBGFLAG_ERROR, ("NbtSetInfo: Warning - CloseAddress Failed\r\n")) ;
        pDeviceContext->pDgramFileObject = NULL;
    }

    if (pDeviceContext->pNameServerFileObject)
    {
        Request.Handle.AddressHandle = pDeviceContext->pNameServerFileObject ;
        if ( TdiVxdCloseAddress( &Request ))
            CDbgPrint( DBGFLAG_ERROR, ("NbtSetInfo: Warning - CloseAddress Failed\r\n")) ;
        pDeviceContext->pNameServerFileObject = NULL;
    }

    if (pDeviceContext->pSessionFileObject)
    {
        Request.Handle.AddressHandle = pDeviceContext->pSessionFileObject ;
        if ( TdiVxdCloseAddress( &Request ))
            CDbgPrint( DBGFLAG_ERROR, ("NbtSetInfo: Warning - CloseAddress Failed\r\n")) ;
        pDeviceContext->pSessionFileObject = NULL;
    }

    if (pDeviceContext->hBroadcastAddress)
    {
        Request.Handle.ConnectionContext = pDeviceContext->hBroadcastAddress ;
        status = NbtCloseAddress( &Request, NULL, pDeviceContext, NULL );
        if ( !NT_SUCCESS(status) )
        {
            CDbgPrint( DBGFLAG_ERROR, ("NbtSetInfo: Warning - Close Broadcast Address Failed\r\n")) ;
            ASSERT(0);
        }
    }

    return STATUS_SUCCESS ;
}