Click here to Skip to main content
15,883,933 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi all
I am trying to modify the data in MDL. I would like to change the default Header to my own header. My custom header size is lesser than default header. I don't want to disturb the packet data in Netbuffer. So here my requirement is to shrink NetBufferDataLength. How can I achieve this?
here I used following code: :
C++
for(NetBuffer=NET_BUFFER_LIST_FIRST_NB (NetBufferLists);
             NetBuffer != NULL;
             NetBuffer = NET_BUFFER_NEXT_NB (NetBuffer))
 {
  // here test buffer is Modified data buffer.
 NdisChangeNetBuffer(NetBuffer,testBuffer, Buffersize);
 }

/* I am Trying observe the modified data here in following code:*/
/***** Converted Data ***************/
for(NetBuffer=NET_BUFFER_LIST_FIRST_NB (NetBufferLists);
             NetBuffer != NULL;
             NetBuffer = NET_BUFFER_NEXT_NB (NetBuffer))
 {
dataLength  = NET_BUFFER_DATA_LENGTH (NetBuffer);
DbgPrint("Converted dataLength= %d", dataLength);      // here expecting a modified datalength but not happening
tempData=(PUCHAR)NdisAllocateMemoryWithTagPriority(											 FilterDriverHandle,				(UINT)dataLength,											 Tag,								NormalPoolPriority
												);
NdisQueryNetBuffer(NetBuffer, tempData, &BytesCopied);
DbgPrint("Bytes copied in converted data= %d", BytesCopied);
DbgPrint("Converted Data");
for(count=0;count<BytesCopied;count++)
{ DbgPrint("%d---> 0x%x \t",count, tempData[count]);
}	
 NdisFreeMemory((PVOID)tempData, dataLength, 0);
  tempData = NULL;
 }

/**************************************************************/



NDIS_STATUS
NdisChangeNetBuffer(
	IN  PNET_BUFFER         NetBufferToSend,
	IN  PUCHAR		Modified_data,
	IN  ULONG               BufferLength
	)
  {

   // Here Modified data is a buffer having OwnHeader + Packetdata
   // BufferLength is the whole dataLength of OwnHeader+Packetdata
    NDIS_STATUS     ndisStatus = NDIS_STATUS_SUCCESS;
    PNET_BUFFER     CurrentNetBuffer;
    ULONG           MdlOffset;
    PMDL            Mdl, MdlNext;
    ULONG           dataLength;
	ULONG			exit = 0;
	PMDL            NewMdl = NULL;
	ULONG           ArrayOffset=0;
	SIZE_T MdlByteCount, Remain, CopySize;
   
    do
    {
		CurrentNetBuffer = NetBufferToSend;
        Mdl    = NET_BUFFER_FIRST_MDL (CurrentNetBuffer);
        MdlOffset      = NET_BUFFER_DATA_OFFSET (CurrentNetBuffer);
		
		 //
    // Skip the required offset bytes in the MDL chain.
    //

		while ((Mdl != NULL) && (MdlOffset >= (MdlByteCount = MmGetMdlByteCount(Mdl))))
		{
			MdlOffset -= MdlByteCount;
			Mdl = Mdl->Next;
		}
		for (Remain = BufferLength; Mdl && (Remain > 0); Mdl = Mdl->Next)
		{
            
			PUCHAR SysVa = NULL ;
            MdlNext = Mdl->Next;
			//
			// Skip zero length MDLs.
			//

			MdlByteCount = MmGetMdlByteCount (Mdl);

			if (MdlByteCount == 0)
			{
				continue;
			}

			//
			// Compute the amount to transfer this time.
			//
			ASSERT (MdlOffset < MdlByteCount);

			MdlByteCount -= MdlOffset;
			CopySize = min (Remain, MdlByteCount);
			//
			// Perform the transfer, either directly or using our reserved PTE.
			//
			if(MdlNext!=NULL)
			{
				SysVa = MmGetSystemAddressForMdlSafe (Mdl, NormalPagePriority);
			}
			
			if(MdlNext == NULL)
			{
				NewMdl = NdisAllocateMdl(FilterDriverHandle, (PVOID)(Modified_data+ArrayOffset), CopySize);
			}
			
			else
			{
				RtlCopyMemory ( SysVa + MdlOffset, Modified_data + ArrayOffset, CopySize);
			}
			MdlOffset = 0;

			ArrayOffset += CopySize;
   
			Remain -= CopySize;
			if(MdlNext == NULL)
			{
			   Mdl = NewMdl;
			}
		}
    } while(exit);
	return ndisStatus;

 }


Here I am expecting a new dataLength as lesser value than actual value in converted data. But that is not happing. It is still pointing to original MDL? Why? Where i did mistake?
Thanks
Posted
Updated 17-Aug-15 23:09pm
v3
Comments
Richard MacCutchan 18-Aug-15 4:50am    
Where did you change the value? Try removing the code that is not relevant to the question so it is easier to read.
Member 11230702 18-Aug-15 5:11am    
In a function NdisChangeNetBuffer(), i allocated a new MDL and trying to replace/ append to existing Mdl.
Richard MacCutchan 18-Aug-15 5:54am    
Sorry, but I still cannot see where you are changing the size. Please add some proper comments to your code to indicate exactly where you make the changes.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900