summaryrefslogtreecommitdiffstats
path: root/private/ntos/tdi/st/rcv.c
blob: b2f0e779de47fd94a973379c269f62b895930374 (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
/*++

Copyright (c) 1989-1993  Microsoft Corporation

Module Name:

    rcv.c

Abstract:

    This module contains code which performs the following TDI services:

        o   TdiReceive
        o   TdiReceiveDatagram

Environment:

    Kernel mode

Revision History:

--*/

#include "st.h"


NTSTATUS
StTdiReceive(
    IN PIRP Irp
    )

/*++

Routine Description:

    This routine performs the TdiReceive request for the transport provider.

Arguments:

    Irp - I/O Request Packet for this request.

Return Value:

    NTSTATUS - status of operation.

--*/

{
    NTSTATUS status;
    PTP_CONNECTION connection;
    KIRQL oldirql, cancelirql;
    PTP_REQUEST tpRequest;
    LARGE_INTEGER timeout = {0,0};
    PIO_STACK_LOCATION irpSp;
    PMDL ReceiveBuffer;
    ULONG ReceiveBufferLength;
    PTDI_REQUEST_KERNEL_RECEIVE parameters;

    //
    // verify that the operation is taking place on a connection. At the same
    // time we do this, we reference the connection. This ensures it does not
    // get removed out from under us. Note also that we do the connection
    // lookup within a try/except clause, thus protecting ourselves against
    // really bogus handles
    //

    irpSp = IoGetCurrentIrpStackLocation (Irp);
    connection  = irpSp->FileObject->FsContext;

    status = StVerifyConnectionObject (connection);

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

    //
    // Initialize bytes transferred here.
    //

    Irp->IoStatus.Information = 0;              // reset byte transfer count.


    parameters = (PTDI_REQUEST_KERNEL_RECEIVE)(&irpSp->Parameters);
    ReceiveBuffer = Irp->MdlAddress;
    ReceiveBufferLength =parameters->ReceiveLength;

    //
    // Queue up this receive to the connection object.
    //

    status = StCreateRequest (
                 Irp,                           // IRP for this request.
                 connection,                    // context.
                 REQUEST_FLAGS_CONNECTION,      // partial flags.
                 ReceiveBuffer,
                 ReceiveBufferLength,
                 timeout,
                 &tpRequest);

    //
    // We have a request, now queue it. If the connection has gone south on us
    // while we were getting things going, we will avoid actually queing the
    // request.
    //

    if (NT_SUCCESS (status)) {

        // This reference is removed by StDestroyRequest.

        StReferenceConnection("TdiReceive request", connection);
        tpRequest->Owner = ConnectionType;

        IoAcquireCancelSpinLock(&cancelirql);
        ACQUIRE_SPIN_LOCK (&connection->SpinLock,&oldirql);
        if ((connection->Flags & CONNECTION_FLAGS_STOPPING) != 0) {
            RELEASE_SPIN_LOCK (&connection->SpinLock,oldirql);
            IoReleaseCancelSpinLock(cancelirql);
            StCompleteRequest (
                tpRequest,
                connection->Status,
                0);
            status = STATUS_PENDING;
        } else {

            //
            // Insert onto the receive queue, and make the IRP
            // cancellable.
            //

            InsertTailList (&connection->ReceiveQueue,&tpRequest->Linkage);
            RELEASE_SPIN_LOCK (&connection->SpinLock,oldirql);

            //
            // If this IRP has been cancelled, then call the
            // cancel routine.
            //

            if (Irp->Cancel) {
                Irp->CancelIrql = cancelirql;
                StCancelReceive((PDEVICE_OBJECT)(connection->Provider), Irp);
                StDereferenceConnection ("IRP cancelled", connection);   // release lookup hold.
                return STATUS_PENDING;
            }

            Irp->CancelRoutine = StCancelReceive;
            IoReleaseCancelSpinLock(cancelirql);

            AwakenReceive (connection);             // awaken if sleeping.

            status = STATUS_PENDING;
        }
    }

    StDereferenceConnection("temp TdiReceive", connection);

    return status;
} /* TdiReceive */


NTSTATUS
StTdiReceiveDatagram(
    IN PIRP Irp
    )

/*++

Routine Description:

    This routine performs the TdiReceiveDatagram request for the transport
    provider. Receive datagrams just get queued up to an address, and are
    completed when a DATAGRAM or DATAGRAM_BROADCAST frame is received at
    the address.

Arguments:

    Irp - I/O Request Packet for this request.

Return Value:

    NTSTATUS - status of operation.

--*/

{
    NTSTATUS status;
    KIRQL oldirql;
    PTP_ADDRESS address;
    PTP_ADDRESS_FILE addressFile;
    PTP_REQUEST tpRequest;
    LARGE_INTEGER timeout = {0,0};
    PIO_STACK_LOCATION irpSp;
    PMDL ReceiveBuffer;
    ULONG ReceiveBufferLength;

    //
    // verify that the operation is taking place on an address. At the same
    // time we do this, we reference the address. This ensures it does not
    // get removed out from under us. Note also that we do the address
    // lookup within a try/except clause, thus protecting ourselves against
    // really bogus handles
    //

    irpSp = IoGetCurrentIrpStackLocation (Irp);
    addressFile = irpSp->FileObject->FsContext;

    status = StVerifyAddressObject (addressFile);

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

    address = addressFile->Address;

    ReceiveBuffer = Irp->MdlAddress;
    ReceiveBufferLength =
          ((PTDI_REQUEST_KERNEL_RECEIVEDG)(&irpSp->Parameters))->ReceiveLength;

    status = StCreateRequest (
                 Irp,                           // IRP for this request.
                 address,                       // context
                 REQUEST_FLAGS_ADDRESS,        // partial flags.
                 ReceiveBuffer,
                 ReceiveBufferLength,
                 timeout,
                 &tpRequest);

    if (NT_SUCCESS (status)) {
        StReferenceAddress ("Receive datagram", address);
        tpRequest->Owner = AddressType;
        ACQUIRE_SPIN_LOCK (&address->SpinLock,&oldirql);
        if ((address->Flags & ADDRESS_FLAGS_STOPPING) != 0) {
            RELEASE_SPIN_LOCK (&address->SpinLock,oldirql);
            StCompleteRequest (tpRequest, STATUS_NETWORK_NAME_DELETED, 0);
            status = STATUS_PENDING;
        } else {
            InsertTailList (&addressFile->ReceiveDatagramQueue,&tpRequest->Linkage);
            RELEASE_SPIN_LOCK (&address->SpinLock,oldirql);
        }

        status = STATUS_PENDING;
    }

    StDereferenceAddress ("Temp rcv datagram", address);

    return status;
} /* TdiReceiveDatagram */