0

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?

Miroslav
  • 444
  • 1
  • 7
  • 21

1 Answers1

1

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);
glagolig
  • 1,100
  • 1
  • 12
  • 28
  • Thank you for your response. But now I have 2 packets and each of them have 1 buffer? Is it possible to have only 1 packet and 2 buffers in it? There is variable in NDIS_BUFFER structure called Next so something like pNdisBuffer1->Next = pNdisBuffer2; pNdisBuffer2->Next = NULL; NdisChainBufferAtFront(pNdisPacket, pNdisBuffer1); Am I understand it right? Is it possible? – Miroslav May 02 '12 at 20:49
  • You are probably right, but MS does not recommend touching NDIS_BUFFER fields directly. See "response to comment" at the end of the code. – glagolig May 02 '12 at 21:21
  • Actually I have just solved my problem thanks to your post. I have created 2 new buffers with NdisCopyBuffer where I have set Offset and Length. But then I have problem with chaining. And problem was actually with ->Next variable. There is no need to set that, just set for both buffers Next to NULL and then chain both of them to packet and send it. I saw it in your post and it worked. Thank you :) – Miroslav May 02 '12 at 21:26