summaryrefslogtreecommitdiffstats
path: root/private/ntos/nbt/vxd/tdiout.c
blob: 147bcb785bfb3be1a8fda12093b0aa57372eda3c (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
/*++

Copyright (c) 1989-1993  Microsoft Corporation

Module Name:

    Tdiout.c

Abstract:


    This file represents the TDI interface on the bottom edge of NBT.
    The procedures herein conform to the TDI I/F spec. and then convert
    the information to NT specific Irps etc.  This implementation can be
    changed out to run on another OS.

Author:

    Jim Stewart (Jimst)    10-2-92

Revision History:

--*/

#include <nbtprocs.h>   // procedure headings

// function prototypes for completion routines used in this file
VOID
SendComplete(
    PVOID      pContext,
    TDI_STATUS tdistatus,
    UINT       cbSentSize
    );
VOID
TcpConnectComplete(
    PVOID      pContext,
    TDI_STATUS tdistatus,
    PVOID      pv
    );

VOID DisconnectWaitComplete( PVOID      pContext,
                             TDI_STATUS status,
                             ULONG      Extra ) ;

void VxdDelayedCallHandler( struct CTEEvent *pEvent, void * pContext ) ;

//----------------------------------------------------------------------------
NTSTATUS
TdiSendDatagram(
    IN  PTDI_REQUEST                    pRequest,
    IN  PTDI_CONNECTION_INFORMATION     pSendDgramInfo,
    IN  ULONG                           SendLength,
    OUT PULONG                          pSentSize,
    IN  tBUFFER                       * pSendBuffer,
    IN  ULONG                           SendFlags
    )
/*++

Routine Description:

    This routine sends a datagram to the transport

Arguments:

    pSendBuffer     - this is really an Mdl in NT land.  It must be tacked on
                      the end of the Mdl created for the Nbt datagram header.

Return Value:

    The function value is the status of the operation.

--*/
{
    TDI_STATUS              tdistatus = TDI_SUCCESS ;
    PTDI_SEND_CONTEXT       psendCont = NULL ;

    if ( !GetSendContext( &psendCont ) )
    {
        tdistatus = STATUS_INSUFFICIENT_RESOURCES ;
        goto ErrorExit ;
    }

    //
    //  Save away the old completion routine and context and replace them
    //  with new ones.  The new completion routines will call the old ones
    //  if they are non-NULL and then free the structure.
    //
    psendCont->OldRequestNotifyObject = pRequest->RequestNotifyObject ;
    psendCont->OldContext             = pRequest->RequestContext ;
    psendCont->NewContext             = NULL ;
    pRequest->RequestContext          = psendCont ;

    //
    //  Set the send completion callback
    //
    pRequest->RequestNotifyObject = SendComplete;

    InitNDISBuff( &psendCont->ndisHdr,
                  pSendBuffer->pDgramHdr,
                  pSendBuffer->HdrLength,
                  &psendCont->ndisData1 ) ;

    InitNDISBuff( &psendCont->ndisData1,
                  pSendBuffer->pBuffer,
                  pSendBuffer->Length,
                  NULL ) ;

    tdistatus = TdiVxdSendDatagram( pRequest,
                                    pSendDgramInfo,
                                    SendLength,
                                    pSentSize,
                                    &psendCont->ndisHdr ) ;

    if ( !NT_SUCCESS( tdistatus ) )
        goto ErrorExit ;

    return tdistatus ;

ErrorExit:

    DbgPrint("TdiSendDatagram ErrorExit: tdistatus= ") ;
    DbgPrintNum( tdistatus ) ;
    DbgPrint("\n\r") ;

    //
    //  Call *our* completion routine which frees memory etc.
    //
    if ( psendCont && pRequest->RequestNotifyObject )
    {
        ((NBT_COMPLETION)pRequest->RequestNotifyObject)(
                    psendCont,
                    tdistatus,
                    0 ) ;

        return( STATUS_PENDING );
    }

    return tdistatus ;
}

//----------------------------------------------------------------------------
VOID
SendComplete(
    PVOID      pContext,
    TDI_STATUS tdistatus,
    UINT       cbSentSize
    )
/*++

Routine Description:

    This routine handles the completion of a datagram/session send to the
    transport. It must call the client completion routine and free the TDI_SEND_CONTEXT
    structure pointed at by pContext.

    Note that this routine may also be called if an error is returned from
    the send call.  This is done to localize cleanup.

Arguments:

    pContext  - Pointer to a TDI_SEND_CONTEXT
    tdistatus - Completion status of the TDI request
    cbSentSize- Bytes taken by TDI

--*/
{
    PTDI_SEND_CONTEXT psendCont = pContext ;
    if ( tdistatus != TDI_SUCCESS )
    {
        DbgPrint("SendComplete: TDI Error reported: 0x") ;
        DbgPrintNum( tdistatus ) ;
        DbgPrint("\r\n") ;
    }

    if ( psendCont )
    {
        if ( psendCont->OldRequestNotifyObject )
        {
            //
            //  This calls the name server datagram completion routine which
            //  in turn will call CTEIoComplete (VxdIoComplete) which will call
            //  the NCB post routine (and fill out the NCB)
            //
            psendCont->OldRequestNotifyObject( psendCont->OldContext,
                                               tdistatus,
                                               cbSentSize ) ;
        }

        FreeSendContext( psendCont ) ;
    }
}

//----------------------------------------------------------------------------
NTSTATUS
TdiConnect(
    IN  PTDI_REQUEST                    pRequest,
    IN  ULONG                           lTimeout,
    IN  PTDI_CONNECTION_INFORMATION     pSendInfo,
    OUT PVOID                           pIrp      //IN  PTDI_CONNECTION_INFORMATION     pReturnInfo
    )
/*++

Routine Description:

    This routine sends a connect request to the tranport provider, to setup
    a connection to the other side...

Arguments:


Return Value:

    The function value is the status of the operation.

--*/
{
    TDI_STATUS status ;
    DbgPrint("TdiConnect Entered\n\r") ;
    status = TdiVxdConnect( pRequest,
                            (PVOID)lTimeout,
                            pSendInfo,
                            NULL ) ; // pReturnInfo) ;

    if ( !NT_SUCCESS( status ) )
    {
        DbgPrint("TdiVxdConnect: Returned error " ) ;
        DbgPrintNum( status ) ;
        DbgPrint("\n\r") ;

        //
        // call the completion routine with this status
        //
        //
        (*((NBT_COMPLETION)pRequest->RequestNotifyObject))
                   ((PVOID)pRequest->RequestContext,
                    status,
                    0L);
        return STATUS_PENDING;
    }
    else
    {
        DbgPrint("TdiVxdConnect - Connection ID: 0x") ;
        DbgPrintNum( (ULONG) pRequest->Handle.ConnectionContext ) ; DbgPrint("\r\n") ;
    }

    return status ;
}


//----------------------------------------------------------------------------
NTSTATUS
TdiDisconnect(
    IN  PTDI_REQUEST                    pRequest,
    IN  PVOID                           lTimeout,
    IN  ULONG                           Flags,
    IN  PTDI_CONNECTION_INFORMATION     pSendInfo,
    IN  PCTE_IRP                        pClientIrp,
    IN  BOOLEAN                         Wait
    )
/*++

Routine Description:

    This routine sends a connect request to the tranport provider, to setup
    a connection to the other side...

Arguments:

    Wait is only used for NT (used in case when deleting address object
    with open connections, which Vxd doesn't allow due to Netbios spec).

Return Value:

    The function value is the status of the operation.

--*/
{
    TDI_STATUS status ;
    DbgPrint("TdiDisconnect Entered\n\r") ;
    DbgPrint("TdiDisconnect - Disconnecting Connection ID: 0x") ;
    DbgPrintNum( (ULONG) pRequest->Handle.ConnectionContext ) ; DbgPrint("\r\n") ;

    ASSERT( Flags <= 0xffff ) ;
    status = TdiVxdDisconnect( pRequest,
                               lTimeout,
                               (ushort) Flags,
                               pSendInfo,
                               NULL ) ;

    if ( !NT_SUCCESS( status ) )
    {
        DbgPrint("TdiVxdConnect: Returned error " ) ;
        DbgPrintNum( status ) ;
        DbgPrint("\n\r") ;
    }

    return status ;
}

//----------------------------------------------------------------------------
NTSTATUS
TdiSend(
    IN  PTDI_REQUEST                    pRequest,
    IN  USHORT                          sFlags,
    IN  ULONG                           SendLength,
    OUT PULONG                          pSentSize,
    IN  tBUFFER                         *pBuff,
    IN  ULONG                           SendFlags
    )
/*++

Routine Description:

    This routine sends a packet to the transport on a TCP connection

    If this is a chain send (SendFlags & CHAIN_SEND_FLAG) then pBuff will
    point to a tBUFFERCHAINSEND (which contains a tBUFFER as its first element).

Arguments:


Return Value:

    The function value is the status of the operation.

--*/
{
    TDI_STATUS              tdistatus = TDI_SUCCESS ;
    PTDI_SEND_CONTEXT       psendCont = NULL ;
    tBUFFERCHAINSEND *      pSendBuff = (tBUFFERCHAINSEND*) pBuff ;
    PNDIS_BUFFER            pndis2    = NULL ;
    PNDIS_BUFFER            pndis1    = NULL ;
    DbgPrint("TdiSend Entered - sending 0x") ; DbgPrintNum( SendLength ) ;
    DbgPrint(" bytes\r\n") ;

    if ( !GetSendContext( &psendCont ))
    {
        tdistatus = STATUS_INSUFFICIENT_RESOURCES ;
        if ( pRequest->RequestNotifyObject )
        {
            ((NBT_COMPLETION)pRequest->RequestNotifyObject)(
                        pRequest->RequestContext,
                        tdistatus,
                        0 ) ;
        }
        return tdistatus ;
    }

    //
    //  Save away the old completion routine and context and replace them
    //  with new ones.  The new completion routines will call the old ones
    //  if they are non-NULL and then free the structure.
    //
    psendCont->OldRequestNotifyObject = pRequest->RequestNotifyObject ;
    psendCont->OldContext             = pRequest->RequestContext ;
    psendCont->NewContext             = NULL ;
    pRequest->RequestContext          = psendCont ;

    //
    //  Set the send completion callback
    //
    pRequest->RequestNotifyObject = SendComplete ;

    //
    //  Build the ndis buffer chain (Header and data)
    //

    if ( (SendFlags & CHAIN_SEND_FLAG) && pSendBuff->Length2 )
    {
        InitNDISBuff( &psendCont->ndisData2,
                      pSendBuff->pBuffer2,
                      pSendBuff->Length2,
                      NULL ) ;
        pndis2 = &psendCont->ndisData2 ;
    }

    if ( pSendBuff->tBuff.Length && (SendLength > pSendBuff->tBuff.HdrLength) )
    {
        InitNDISBuff( &psendCont->ndisData1,
                      pSendBuff->tBuff.pBuffer,
                      pSendBuff->tBuff.Length,
                      pndis2 ) ;
        pndis1 = &psendCont->ndisData1 ;
    }

    InitNDISBuff( &psendCont->ndisHdr,
                  pSendBuff->tBuff.pDgramHdr,
                  pSendBuff->tBuff.HdrLength,
                  pndis1 ) ;

    tdistatus = TdiVxdSend( pRequest,
                            sFlags,
                            SendLength,
                            &psendCont->ndisHdr ) ;

    if ( !NT_SUCCESS( tdistatus ) )
        goto ErrorExit ;
    else
        *pSentSize = SendLength ;

    return tdistatus ;

ErrorExit:
    //
    //  Call *our* completion routine which frees memory etc.
    //
    if ( psendCont && pRequest->RequestNotifyObject )
    {
        ((NBT_COMPLETION)pRequest->RequestNotifyObject)(
                    psendCont,
                    tdistatus,
                    0 ) ;
    }

    DbgPrint("TdiSend: returning ") ;
    DbgPrintNum( tdistatus ) ;
    DbgPrint("\n\r") ;
    return tdistatus ;
}

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

    NAME:       VxdScheduleDelayedCall

    SYNOPSIS:   Schedules a callback at some later time

    ENTRY:      pClientContext - Context to pass callback
                CallBackRoutine - Routine to call

    RETURNS:    STATUS_PENDING if successfully scheduled

    NOTES:      This is aliased to CTEQueueForNonDispProcessing.

                The memory for the DCC is freed by the application

    HISTORY:
        Johnl   2-Sep-1993      Created

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

NTSTATUS VxdScheduleDelayedCall( tDGRAM_SEND_TRACKING * pTracker,
                                 PVOID                  pClientContext,
                                 PVOID                  ClientCompletion,
                                 PVOID                  CallBackRoutine,
                                 tDEVICECONTEXT        *pDeviceContext )
{
    CTELockHandle         OldIrq;
    PDELAYED_CALL_CONTEXT pDCC = CTEAllocMem( sizeof( DELAYED_CALL_CONTEXT )) ;

    if ( !pDCC )
        return STATUS_INSUFFICIENT_RESOURCES ;

    ASSERT( CallBackRoutine != NULL ) ;

    pDCC->dc_WIC.pTracker         = pTracker ;
    pDCC->dc_WIC.pClientContext   = pClientContext ;
    pDCC->dc_WIC.ClientCompletion = ClientCompletion ;
    pDCC->dc_Callback             = CallBackRoutine ;
    pDCC->pDeviceContext          = pDeviceContext;

    //
    // put this event on the deviceContext queue if we know the devicecontext
    // otherwise, on the nbtconfig queue.  This allows us to cancel the event
    // later if we wish to (e.g. adapter goes away in pnp, or lease expires)
    // if the adapter is marked as going down, don't schedule an event but
    // execute it synchronously.
    //
    if (pDeviceContext)
    {
        ASSERT( pDeviceContext->Verify == NBT_VERIFY_DEVCONTEXT );

        if (!pDeviceContext->fDeviceUp)
        {
            pDCC->dc_Callback( pDCC ) ;

            DbgPrint("VxdScheduleDelayedCall: device going down,executing now\r\n") ;
            return( STATUS_SUCCESS );
        }
        else
        {
            CTESpinLock(pDeviceContext,OldIrq);
            InsertTailList(&pDeviceContext->DelayedEvents,&pDCC->Linkage);
            CTESpinFree(pDeviceContext,OldIrq);
        }
    }
    else
    {
        CTESpinLock(&NbtConfig,OldIrq);
        InsertTailList(&NbtConfig.DelayedEvents,&pDCC->Linkage);
        CTESpinFree(&NbtConfig,OldIrq);
    }

    CTEInitEvent( &pDCC->dc_event, VxdDelayedCallHandler ) ;

    CTEScheduleEvent( &pDCC->dc_event, pDCC) ;

    return STATUS_PENDING ;
}


void VxdDelayedCallHandler( struct CTEEvent *pEvent, void * pContext )
{
    PDELAYED_CALL_CONTEXT pDCC = pContext ;
    CTELockHandle         OldIrq;
    tDEVICECONTEXT       *pDeviceContext;


    ASSERT( pDCC != NULL && pDCC->dc_Callback != NULL ) ;

    pDeviceContext = pDCC->pDeviceContext;

    if (pDeviceContext)
    {
        ASSERT( pDeviceContext->Verify == NBT_VERIFY_DEVCONTEXT );
        CTESpinLock(pDeviceContext,OldIrq);
        RemoveEntryList(&pDCC->Linkage);
        CTESpinFree(pDeviceContext,OldIrq);
    }
    else
    {
        CTESpinLock(&NbtConfig.JointLock,OldIrq);
        RemoveEntryList(&pDCC->Linkage);
        CTESpinFree(&NbtConfig.JointLock,OldIrq);
    }

    pDCC->dc_Callback( pDCC ) ;
}


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

    NAME:       CancelAllDelayedEvents

    SYNOPSIS:   Since the device (or the entire vxd!) is going away,
                cancel all the events that were queued to be scheduled
                later.  If a particular device context is going away (but
                not the entire system) then execute all those events
                synchronously.

    ENTRY:      pDeviceContext - the device context that's going away
                                 NULL if the vxd is getting unloaded

    RETURNS:    TRUE  if at least one event present and was cancelled
                FALSE if there were no events queued.

    HISTORY:
        Koti    Jan. 9, 95

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

BOOL
CancelAllDelayedEvents( tDEVICECONTEXT *pDeviceContext )
{

    LIST_ENTRY            *pHead;
    LIST_ENTRY            *pEntry;
    PDELAYED_CALL_CONTEXT  pDCC;
    CTELockHandle          OldIrq;
    BOOL                   fAtLeastOne=FALSE;


    CTESpinLock(&NbtConfig.JointLock,OldIrq);

    if (pDeviceContext)
        pHead = &pDeviceContext->DelayedEvents;
    else
        pHead = &NbtConfig.DelayedEvents;

    pEntry = pHead->Flink;

    while (pEntry != pHead)
    {
        pDCC = CONTAINING_RECORD(pEntry,DELAYED_CALL_CONTEXT,Linkage);
        pEntry = pEntry->Flink;

        RemoveEntryList(&pDCC->Linkage);

        CTESpinFree(&NbtConfig.JointLock,OldIrq);

        CTECancelEvent( &pDCC->dc_event );

        //
        // if only one device context is going away, execute the event now
        //
        if (pDeviceContext)
        {
            pDCC->dc_Callback( pDCC ) ;
        }
        CTESpinLock(&NbtConfig.JointLock,OldIrq);

        fAtLeastOne = TRUE;
    }

    CTESpinFree(&NbtConfig.JointLock,OldIrq);

    ASSERT( IsListEmpty( pHead ) );

    return( fAtLeastOne );
}