Reply To: Modify packet [DELPHI]

Home Forums Discussions Support Portal Modify packet [DELPHI] Reply To: Modify packet [DELPHI]

#5580

heilong
Participant

I not absolutely understand a question, therefore I shall answer how I have understood it.


...
var
ReadRequest: ETH_REQUEST;
Buffer: INTERMEDIATE_BUFFER;
pEthernetHeader: PETHERNET_HDR;
pIpHeader: PIP_HDR;
pTCPHeader: PTCP_HDR;
pData: Pbyte;
DataSize: Integer;
...

const
HSIZE = sizeof(ETHERNET_HDR) + sizeof(IP_HDR)+ sizeof(ICMP_HDR);

...

ReadRequest.EthPacket.Buffer := @Buffer;
ReadRequest.hAdapterHandle := hAdapter;

...

if ReadPacket (hFilt, @ReadRequest) <> 0 then
begin
...
pEthernetHeader := @ReadRequest.EthPacket.Buffer.m_IBuffer;
pIPHeader := Pointer(integer(pEthernetHeader)+sizeof(ETHERNET_HDR));
pTCPHeader := Pointer(integer(pIPHeader)+sizeof(IP_HDR));
DataSize := ReadRequest.EthPacket.Buffer.m_Length - HSIZE;

PData := Pointer(integer(pEthernetHeader)+HSIZE);
// or so
//PData := Pointer(integer(pTCPHeader)+sizeof(TCP_HDR));
...

// Change Port Destination to 4032

pTCPHeader.Destination := htons(4032);

// Tne IPHeader checksum it is not recalculated therefore as the size of a package has not changed

pTCPHeader.CheckSum := 00;
pTCPHeader.Checksum := htons(Checksum(Pword(pTCPHeader), (sizeof(TCP_HDR) + DataSize) div 2));

//If the size of a packet has changed, it is necessary to align a packet to word and recalc IP Header and accordingly TCP Header
...

SendPacketToAdapter(hFilt, @ReadRequest);
end;
...

In a code there can be mistakes, for what beforehand apologize.