Annotation of ntddk/src/network/sonic/packet.c, revision 1.1.1.1

1.1       root        1: /*++
                      2: 
                      3: Copyright (c) 1990-1992  Microsoft Corporation
                      4: 
                      5: Module Name:
                      6: 
                      7:     packet.c
                      8: 
                      9: Abstract:
                     10: 
                     11:     This module contains code to copy from ndis packets to ndis packets,
                     12:     and also to copy from ndis packets to a buffer.
                     13: 
                     14: Author:
                     15: 
                     16:     Anthony V. Ercolano (Tonye) 12-Sept-1990
                     17: 
                     18: Environment:
                     19: 
                     20:     Works in kernel mode, but is not important that it does.
                     21: 
                     22: Revision History:
                     23: 
                     24: 
                     25: --*/
                     26: 
                     27: #include <ndis.h>
                     28: #include <efilter.h>
                     29: 
                     30: #include <sonichrd.h>
                     31: #include <sonicsft.h>
                     32: 
                     33: 
                     34: extern
                     35: VOID
                     36: SonicCopyFromPacketToBuffer(
                     37:     IN PNDIS_PACKET Packet,
                     38:     IN UINT Offset,
                     39:     IN UINT BytesToCopy,
                     40:     OUT PCHAR Buffer,
                     41:     OUT PUINT BytesCopied
                     42:     )
                     43: 
                     44: /*++
                     45: 
                     46: Routine Description:
                     47: 
                     48:     Copy from an ndis packet into a buffer.
                     49: 
                     50: Arguments:
                     51: 
                     52:     Packet - The packet to copy from.
                     53: 
                     54:     Offset - The offset from which to start the copy.
                     55: 
                     56:     BytesToCopy - The number of bytes to copy from the packet.
                     57: 
                     58:     Buffer - The destination of the copy.
                     59: 
                     60:     BytesCopied - The number of bytes actually copied.  Can be less then
                     61:     BytesToCopy if the packet is shorter than BytesToCopy.
                     62: 
                     63: Return Value:
                     64: 
                     65:     None
                     66: 
                     67: --*/
                     68: 
                     69: {
                     70: 
                     71:     //
                     72:     // Holds the number of ndis buffers comprising the packet.
                     73:     //
                     74:     UINT NdisBufferCount;
                     75: 
                     76:     //
                     77:     // Points to the buffer from which we are extracting data.
                     78:     //
                     79:     PNDIS_BUFFER CurrentBuffer;
                     80: 
                     81:     //
                     82:     // Holds the virtual address of the current buffer.
                     83:     //
                     84:     PVOID VirtualAddress;
                     85: 
                     86:     //
                     87:     // Holds the length of the current buffer of the packet.
                     88:     //
                     89:     UINT CurrentLength;
                     90: 
                     91:     //
                     92:     // Keep a local variable of BytesCopied so we aren't referencing
                     93:     // through a pointer.
                     94:     //
                     95:     UINT LocalBytesCopied = 0;
                     96: 
                     97:     //
                     98:     // Take care of boundary condition of zero length copy.
                     99:     //
                    100: 
                    101:     *BytesCopied = 0;
                    102:     if (!BytesToCopy) return;
                    103: 
                    104:     //
                    105:     // Get the first buffer.
                    106:     //
                    107: 
                    108:     NdisQueryPacket(
                    109:         Packet,
                    110:         NULL,
                    111:         &NdisBufferCount,
                    112:         &CurrentBuffer,
                    113:         NULL
                    114:         );
                    115: 
                    116:     //
                    117:     // Could have a null packet.
                    118:     //
                    119: 
                    120:     if (!NdisBufferCount) return;
                    121: 
                    122:     NdisQueryBuffer(
                    123:         CurrentBuffer,
                    124:         &VirtualAddress,
                    125:         &CurrentLength
                    126:         );
                    127: 
                    128:     while (LocalBytesCopied < BytesToCopy) {
                    129: 
                    130:         if (!CurrentLength) {
                    131: 
                    132:             NdisGetNextBuffer(
                    133:                 CurrentBuffer,
                    134:                 &CurrentBuffer
                    135:                 );
                    136: 
                    137:             //
                    138:             // We've reached the end of the packet.  We return
                    139:             // with what we've done so far. (Which must be shorter
                    140:             // than requested.
                    141:             //
                    142: 
                    143:             if (!CurrentBuffer) break;
                    144: 
                    145:             NdisQueryBuffer(
                    146:                 CurrentBuffer,
                    147:                 &VirtualAddress,
                    148:                 &CurrentLength
                    149:                 );
                    150:             continue;
                    151: 
                    152:         }
                    153: 
                    154:         //
                    155:         // Try to get us up to the point to start the copy.
                    156:         //
                    157: 
                    158:         if (Offset) {
                    159: 
                    160:             if (Offset > CurrentLength) {
                    161: 
                    162:                 //
                    163:                 // What we want isn't in this buffer.
                    164:                 //
                    165: 
                    166:                 Offset -= CurrentLength;
                    167:                 CurrentLength = 0;
                    168:                 continue;
                    169: 
                    170:             } else {
                    171: 
                    172:                 VirtualAddress = (PCHAR)VirtualAddress + Offset;
                    173:                 CurrentLength -= Offset;
                    174:                 Offset = 0;
                    175: 
                    176:             }
                    177: 
                    178:         }
                    179: 
                    180:         //
                    181:         // Copy the data.
                    182:         //
                    183: 
                    184: 
                    185:         {
                    186: 
                    187:             //
                    188:             // Holds the amount of data to move.
                    189:             //
                    190:             UINT AmountToMove;
                    191: 
                    192:             AmountToMove =
                    193:                        ((CurrentLength <= (BytesToCopy - LocalBytesCopied))?
                    194:                         (CurrentLength):(BytesToCopy - LocalBytesCopied));
                    195: 
                    196:             SONIC_MOVE_MEMORY(
                    197:                 Buffer,
                    198:                 VirtualAddress,
                    199:                 AmountToMove
                    200:                 );
                    201: 
                    202:             Buffer = (PCHAR)Buffer + AmountToMove;
                    203:             VirtualAddress = (PCHAR)VirtualAddress + AmountToMove;
                    204: 
                    205:             LocalBytesCopied += AmountToMove;
                    206:             CurrentLength -= AmountToMove;
                    207: 
                    208:         }
                    209: 
                    210:     }
                    211: 
                    212:     *BytesCopied = LocalBytesCopied;
                    213: 
                    214: }
                    215: 
                    216: extern
                    217: VOID
                    218: SonicCopyFromBufferToPacket(
                    219:     IN PCHAR Buffer,
                    220:     IN UINT BytesToCopy,
                    221:     IN PNDIS_PACKET Packet,
                    222:     IN UINT Offset,
                    223:     OUT PUINT BytesCopied
                    224:     )
                    225: 
                    226: /*++
                    227: 
                    228: Routine Description:
                    229: 
                    230:     Copy from a buffer into an ndis packet.
                    231: 
                    232: Arguments:
                    233: 
                    234:     Buffer - The packet to copy from.
                    235: 
                    236:     Offset - The offset from which to start the copy.
                    237: 
                    238:     BytesToCopy - The number of bytes to copy from the buffer.
                    239: 
                    240:     Packet - The destination of the copy.
                    241: 
                    242:     BytesCopied - The number of bytes actually copied.  Will be less
                    243:                 than BytesToCopy if the packet is not large enough.
                    244: 
                    245: Return Value:
                    246: 
                    247:     None
                    248: 
                    249: --*/
                    250: 
                    251: {
                    252:     //
                    253:     // Holds the count of the number of ndis buffers comprising the
                    254:     // destination packet.
                    255:     //
                    256:     UINT DestinationBufferCount;
                    257: 
                    258:     //
                    259:     // Points to the buffer into which we are putting data.
                    260:     //
                    261:     PNDIS_BUFFER DestinationCurrentBuffer;
                    262: 
                    263:     //
                    264:     // Points to the location in Buffer from which we are extracting data.
                    265:     //
                    266:     PUCHAR SourceCurrentAddress;
                    267: 
                    268:     //
                    269:     // Holds the virtual address of the current destination buffer.
                    270:     //
                    271:     PVOID DestinationVirtualAddress;
                    272: 
                    273:     //
                    274:     // Holds the length of the current destination buffer.
                    275:     //
                    276:     UINT DestinationCurrentLength;
                    277: 
                    278:     //
                    279:     // Keep a local variable of BytesCopied so we aren't referencing
                    280:     // through a pointer.
                    281:     //
                    282:     UINT LocalBytesCopied = 0;
                    283: 
                    284: 
                    285:     //
                    286:     // Take care of boundary condition of zero length copy.
                    287:     //
                    288: 
                    289:     *BytesCopied = 0;
                    290:     if (!BytesToCopy) return;
                    291: 
                    292:     //
                    293:     // Get the first buffer of the destination.
                    294:     //
                    295: 
                    296:     NdisQueryPacket(
                    297:         Packet,
                    298:         NULL,
                    299:         &DestinationBufferCount,
                    300:         &DestinationCurrentBuffer,
                    301:         NULL
                    302:         );
                    303: 
                    304:     //
                    305:     // Could have a null packet.
                    306:     //
                    307: 
                    308:     if (!DestinationBufferCount) return;
                    309: 
                    310:     NdisQueryBuffer(
                    311:         DestinationCurrentBuffer,
                    312:         &DestinationVirtualAddress,
                    313:         &DestinationCurrentLength
                    314:         );
                    315: 
                    316:     //
                    317:     // Set up the source address.
                    318:     //
                    319: 
                    320:     SourceCurrentAddress = Buffer;
                    321: 
                    322: 
                    323:     while (LocalBytesCopied < BytesToCopy) {
                    324: 
                    325:         //
                    326:         // Check to see whether we've exhausted the current destination
                    327:         // buffer.  If so, move onto the next one.
                    328:         //
                    329: 
                    330:         if (!DestinationCurrentLength) {
                    331: 
                    332:             NdisGetNextBuffer(
                    333:                 DestinationCurrentBuffer,
                    334:                 &DestinationCurrentBuffer
                    335:                 );
                    336: 
                    337:             if (!DestinationCurrentBuffer) {
                    338: 
                    339:                 //
                    340:                 // We've reached the end of the packet.  We return
                    341:                 // with what we've done so far. (Which must be shorter
                    342:                 // than requested.)
                    343:                 //
                    344: 
                    345:                 break;
                    346: 
                    347:             }
                    348: 
                    349:             NdisQueryBuffer(
                    350:                 DestinationCurrentBuffer,
                    351:                 &DestinationVirtualAddress,
                    352:                 &DestinationCurrentLength
                    353:                 );
                    354: 
                    355:             continue;
                    356: 
                    357:         }
                    358: 
                    359:         //
                    360:         // Try to get us up to the point to start the copy.
                    361:         //
                    362: 
                    363:         if (Offset) {
                    364: 
                    365:             if (Offset > DestinationCurrentLength) {
                    366: 
                    367:                 //
                    368:                 // What we want isn't in this buffer.
                    369:                 //
                    370: 
                    371:                 Offset -= DestinationCurrentLength;
                    372:                 DestinationCurrentLength = 0;
                    373:                 continue;
                    374: 
                    375:             } else {
                    376: 
                    377:                 DestinationVirtualAddress = (PCHAR)DestinationVirtualAddress
                    378:                                             + Offset;
                    379:                 DestinationCurrentLength -= Offset;
                    380:                 Offset = 0;
                    381: 
                    382:             }
                    383: 
                    384:         }
                    385: 
                    386: 
                    387:         //
                    388:         // Copy the data.
                    389:         //
                    390: 
                    391:         {
                    392: 
                    393:             //
                    394:             // Holds the amount of data to move.
                    395:             //
                    396:             UINT AmountToMove;
                    397: 
                    398:             //
                    399:             // Holds the amount desired remaining.
                    400:             //
                    401:             UINT Remaining = BytesToCopy - LocalBytesCopied;
                    402: 
                    403: 
                    404:             AmountToMove = DestinationCurrentLength;
                    405: 
                    406:             AmountToMove = ((Remaining < AmountToMove)?
                    407:                             (Remaining):(AmountToMove));
                    408: 
                    409:             SONIC_MOVE_MEMORY(
                    410:                 DestinationVirtualAddress,
                    411:                 SourceCurrentAddress,
                    412:                 AmountToMove
                    413:                 );
                    414: 
                    415:             SourceCurrentAddress += AmountToMove;
                    416:             LocalBytesCopied += AmountToMove;
                    417:             DestinationCurrentLength -= AmountToMove;
                    418: 
                    419:         }
                    420: 
                    421:     }
                    422: 
                    423:     *BytesCopied = LocalBytesCopied;
                    424: 
                    425: 
                    426: }

unix.superglobalmegacorp.com

This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.