Search code examples
splitbufferdriverwdkndis

Split NDIS_BUFFER (WDK)


I am new to driver programming and I cannot find solution for one probably simple problem. I am editing the ndis_prot sample driver from wdk examples. Curently, packet comes to driver as Irp and is stored into pNdisBuffer. I need to split up that buffer into 2 parts - pNdisBuffer1 and pNdisBuffer2 - first fill with header, second with the data and chain both of them to pNdisPacket. There are few articles about that but I cannot find example for this. It should be possible like it is described here

http://blogs.msdn.com/b/ntdebugging/archive/2008/09/19/ndis-part-1.aspx

but I would like to see example in wdk (code).

PS: Please don't ask why do I need to do this nor try to change it into something different. It just has to be done that way. Can you help me please?


Solution

  • Pseudo-code (error handling & some declarations are ommited etc)

    // initial code
    PNDIS_PACKET sourcePack;
    ...
    PNDIS_PACKET packet1, packet2, current;
    NdisAllocatePacket(&status, &packet1, handle);
    NdisAllocatePacket(&status, &packet2, handle);
    current = packet1;
    PNDIS_BUFFER sourceBuf, newBuf;
    NdisGetFirstBufferFromPacket(sourcePack,&sourceBuf,&va,&len,&totalLen);
    while(sourceBuf != NULL)
    {
      NdisQueryBuffer(sourceBuf,&va,&len);
      if( .. (PUCHAR)va+x is a split point ..)
      {
        if(x != 0)
        {
          newBuf = NewBuf(va, x);
          NdisCahinBufferAtBack(current,newBuf);
        }
        current = packet2;
        newBuf = NewBuf(va+x, len-x);
      }
      else
      {
        newBuf = NewBuf(va,len);
      }
      NdisChainBufferAtBack(current,newBuf);
      NdisGetNextBuffer(sourceBuf,&sourceBuf);
    }
    ...
    PNDIS_BUFFER NewBuf(PVOID va, ULONG len)
    {
      PNDIS_BUFFER newBuffer;
      NdisAllocateBuffer(&Status, &newBuffer, handle, va, len);
      return newBuffer;
    }
    
    // response to comment
    newBuf = NewBuf(va, x);
    NdisCahinBufferAtBack(current,newBuf);
    newBuf = NewBuf(va+x, len-x);
    NdisCahinBufferAtBack(current,newBuf);