heilong

Forum Replies Created

Viewing 3 posts - 1 through 3 (of 3 total)
  • Author
    Posts
  • in 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.

      in reply to: Modify packet [DELPHI] #5577
      heilong
      Participant

        Checksum = calculate (pointer, (header+data) div 2), as for ICMP as TCP

        Example:


        Packet = ETH_HDR+IP_HDR+TCP_HDR+Data;
        IP_HDR.Checksum := 00;
        IP_HDR.Checksum := htons(Checksum(Pword(IP_HDR), sizeof(IP_HDR) div 2));

        TCP.Checksum := 00;
        TCP_HDR.Checksum := htons(Checksum(Pword(TCP_HDR), sizeof((TCP_HDR+Length(Data)) div 2));

        For more: See ICMP example in previous reply

        in reply to: Modify packet [DELPHI] #5575
        heilong
        Participant

          Try it. It is universal function.

          p – pointer to begin of header.
          Size – size of header in words.
          Attention: Field Checksum must be = 0 before call function.

          function Checksum(p: PWORD; Size: word): WORD;
          var
          i: Integer;
          Sum: DWORD;
          begin
          Sum := 0;
          for i := 0 to Size – 1 do
          begin
          Sum := Sum + ntohs(p^);
          inc(p);
          end;
          while (Sum shr 16) > 0 do
          Sum := (Sum and $FFFF) + (Sum shr 16);
          Sum := not Sum;
          Result := Sum;
          end;

          Examples:
          1. Calc checksum for IPHeader. For uses without IPOptions.

          IPHeader.Checksum := 00;
          IPHeader.Checksum := htons(Checksum(Pword(IPHeader), sizeof(IP_HDR) div 2));

          2. Calc checksum for ICMPHeader.
          IcmpHeader.Checksum := 00;
          IcmpHeader.Checksum := htons(Checksum(Pword(IcmpHeader),
          (sizeof(ICMP_HDR)+Length(Data)) div 2));

          Don`t forget call htons for result of function Checksum.
          Sorry for my English.

        Viewing 3 posts - 1 through 3 (of 3 total)