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

Copyright (c) 1989-1993  Microsoft Corporation

Module Name:

    framesnd.c

Abstract:

    This module contains routines which build and send Sample transport
    frames for other modules.

Environment:

    Kernel mode

Revision History:

--*/

#include "st.h"



NTSTATUS
StSendConnect(
    IN PTP_CONNECTION Connection
    )

/*++

Routine Description:

    This routine sends a CONNECT frame of the appropriate type given the
    state of the specified connection.

Arguments:

    Connection - Pointer to a transport connection object.

Return Value:

    none.

--*/

{
    NTSTATUS Status;
    PDEVICE_CONTEXT DeviceContext;
    PUCHAR SourceRouting;
    UINT SourceRoutingLength;
    UINT HeaderLength;
    PSEND_PACKET_TAG SendTag;
    PTP_PACKET Packet;
    PST_HEADER StHeader;


    DeviceContext = Connection->Provider;

    //
    // Allocate a packet from the pool.
    //

    Status = StCreatePacket (DeviceContext, &Packet);
    if (!NT_SUCCESS (Status)) {                    // couldn't make frame.
        return STATUS_INSUFFICIENT_RESOURCES;
    }

    SendTag = (PSEND_PACKET_TAG)(Packet->NdisPacket->ProtocolReserved);
    SendTag->Type = TYPE_C_FRAME;
    SendTag->Packet = Packet;
    SendTag->Owner = (PVOID)Connection;

    //
    // Build the MAC header.
    //

    //
    // CONNECT frames go out as
    // single-route source routing.
    //

    MacReturnSingleRouteSR(
        &DeviceContext->MacInfo,
        &SourceRouting,
        &SourceRoutingLength);

    MacConstructHeader (
        &DeviceContext->MacInfo,
        Packet->Header,
        DeviceContext->MulticastAddress.Address,
        DeviceContext->LocalAddress.Address,
        sizeof(ST_HEADER),
        SourceRouting,
        SourceRoutingLength,
        &HeaderLength);


    //
    // Build the header: 'C', dest, source
    //

    StHeader = (PST_HEADER)(&Packet->Header[HeaderLength]);

    StHeader->Signature = ST_SIGNATURE;
    StHeader->Command = ST_CMD_CONNECT;
    StHeader->Flags = 0;

    RtlCopyMemory (StHeader->Destination, Connection->CalledAddress.NetbiosName, 16);
    RtlCopyMemory (StHeader->Source, Connection->AddressFile->Address->NetworkName->NetbiosName, 16);

    HeaderLength += sizeof(ST_HEADER);

    //
    // Modify the packet length and send the it.
    //

    StSetNdisPacketLength(Packet->NdisPacket, HeaderLength);

    StNdisSend (Packet);

    return STATUS_SUCCESS;
} /* StSendConnect */


NTSTATUS
StSendDisconnect(
    IN PTP_CONNECTION Connection
    )

/*++

Routine Description:

    This routine sends a DISCONNECT frame of the appropriate type given the
    state of the specified connection.

Arguments:

    Connection - Pointer to a transport connection object.

Return Value:

    none.

--*/

{
    NTSTATUS Status;
    PDEVICE_CONTEXT DeviceContext;
    PUCHAR SourceRouting;
    UINT SourceRoutingLength;
    UINT HeaderLength;
    PSEND_PACKET_TAG SendTag;
    PTP_PACKET Packet;
    PST_HEADER StHeader;


    DeviceContext = Connection->Provider;

    //
    // Allocate a packet from the pool.
    //

    Status = StCreatePacket (DeviceContext, &Packet);
    if (!NT_SUCCESS (Status)) {                    // couldn't make frame.
        return STATUS_INSUFFICIENT_RESOURCES;
    }

    SendTag = (PSEND_PACKET_TAG)(Packet->NdisPacket->ProtocolReserved);
    SendTag->Type = TYPE_D_FRAME;
    SendTag->Packet = Packet;
    SendTag->Owner = (PVOID)Connection;

    //
    // Build the MAC header.
    //

    //
    // CONNECT frames go out as
    // single-route source routing.
    //

    MacReturnSingleRouteSR(
        &DeviceContext->MacInfo,
        &SourceRouting,
        &SourceRoutingLength);

    MacConstructHeader (
        &DeviceContext->MacInfo,
        Packet->Header,
        DeviceContext->MulticastAddress.Address,
        DeviceContext->LocalAddress.Address,
        sizeof(ST_HEADER),
        SourceRouting,
        SourceRoutingLength,
        &HeaderLength);


    //
    // Build the header: 'D', dest, source
    //

    StHeader = (PST_HEADER)(&Packet->Header[HeaderLength]);

    StHeader->Signature = ST_SIGNATURE;
    StHeader->Command = ST_CMD_DISCONNECT;
    StHeader->Flags = 0;

    RtlCopyMemory (StHeader->Destination, Connection->CalledAddress.NetbiosName, 16);
    RtlCopyMemory (StHeader->Source, Connection->AddressFile->Address->NetworkName->NetbiosName, 16);

    HeaderLength += sizeof(ST_HEADER);

    //
    // Modify the packet length and send the it.
    //

    StSetNdisPacketLength(Packet->NdisPacket, HeaderLength);

    StNdisSend (Packet);

    return STATUS_SUCCESS;

} /* StSendDisconnect */


NTSTATUS
StSendAddressFrame(
    PTP_ADDRESS Address
    )

/*++

Routine Description:

    It is intended that this routine be used for sending datagrams and
    braodcast datagrams.

    The datagram to be sent is described in the NDIS packet contained
    in the Address. When the send completes, the send completion handler
    returns the NDIS buffer describing the datagram to the buffer pool and
    marks the address ndis packet as usable again. Thus, all datagram
    frames are sequenced through the address they are sent on.

Arguments:

    Address - pointer to the address from which to send this datagram.

Return Value:

    NTSTATUS - status of operation.

--*/

{
    PDEVICE_CONTEXT DeviceContext;


    //
    // Send the packet.
    //

    DeviceContext = Address->Provider;

    INCREMENT_COUNTER (DeviceContext, PacketsSent);

    StNdisSend (Address->Packet);

    return STATUS_PENDING;
} /* StSendAddressFrame */


VOID
StSendDatagramCompletion(
    IN PTP_ADDRESS Address,
    IN PNDIS_PACKET NdisPacket,
    IN NDIS_STATUS NdisStatus
    )

/*++

Routine Description:

    This routine is called as an I/O completion handler at the time a
    StSendUIMdlFrame send request is completed.  Because this handler is only
    associated with StSendUIMdlFrame, and because StSendUIMdlFrame is only
    used with datagrams and broadcast datagrams, we know that the I/O being
    completed is a datagram.  Here we complete the in-progress datagram, and
    start-up the next one if there is one.

Arguments:

    Address - Pointer to a transport address on which the datagram
        is queued.

    NdisPacket - pointer to the NDIS packet describing this request.

Return Value:

    none.

--*/

{
    PTP_REQUEST Request;
    PLIST_ENTRY p;
    KIRQL oldirql;
    PNDIS_BUFFER HeaderBuffer;

    UNREFERENCED_PARAMETER(NdisPacket);

    StReferenceAddress ("Complete datagram", Address);

    //
    // Dequeue the current request and return it to the client.  Release
    // our hold on the send datagram queue.
    //
    // *** There may be no current request, if the one that was queued
    //     was aborted or timed out.
    //

    ACQUIRE_SPIN_LOCK (&Address->SpinLock, &oldirql);
    p = RemoveHeadList (&Address->SendDatagramQueue);

    if (p != &Address->SendDatagramQueue) {

        RELEASE_SPIN_LOCK (&Address->SpinLock, oldirql);

        Request = CONTAINING_RECORD (p, TP_REQUEST, Linkage);

        //
        // Strip off and unmap the buffers describing data and header.
        //

        NdisUnchainBufferAtFront (Address->Packet->NdisPacket, &HeaderBuffer);

        // drop the rest of the packet

        NdisReinitializePacket (Address->Packet->NdisPacket);

        NDIS_BUFFER_LINKAGE(HeaderBuffer) = (PNDIS_BUFFER)NULL;
        NdisChainBufferAtFront (Address->Packet->NdisPacket, HeaderBuffer);

        //
        // Ignore NdisStatus; datagrams always "succeed".
        //

        StCompleteRequest (Request, STATUS_SUCCESS, Request->Buffer2Length);

        ACQUIRE_SPIN_LOCK (&Address->SpinLock, &oldirql);
        Address->Flags &= ~ADDRESS_FLAGS_SEND_IN_PROGRESS;
        RELEASE_SPIN_LOCK (&Address->SpinLock, oldirql);

        //
        // Send more datagrams on the Address if possible.
        //

        StSendDatagramsOnAddress (Address);       // do more datagrams.

    } else {

        Address->Flags &= ~ADDRESS_FLAGS_SEND_IN_PROGRESS;
        RELEASE_SPIN_LOCK (&Address->SpinLock, oldirql);

    }

    StDereferenceAddress ("Complete datagram", Address);

} /* StSendDatagramCompletion */