Calculate Tcp checksum in delphi

Home Forums Discussions Support Calculate Tcp checksum in delphi

Viewing 2 posts - 1 through 2 (of 2 total)
  • Author
    Posts
  • #4832
    PLY
    Participant

      PLEASE Can any one help me? i try to tranlate VC’s source code of calculte tcp checksum to delphi but it dosn’t work like i expect.

      procedure TTimerThread.CalcTCPchekSum(pBuffer: INTERMEDIATE_BUFFER);
      type
      Tbyte = array of byte;

      var
      pIPHeader: TIPHeaderPtr;
      pTCPHeader: TTCPHeaderPtr;
      w16,padd: word;
      i,sum: integer;
      dwTcpLen: DWORD;
      IPProtocol,HL: Byte;
      buff: Tbyte;
      sIP,dIP: in_addr;
      begin
      padd := 0; sum := 0;
      pIPHeader := TIPHeaderPtr(@Buffer.m_IBuffer[MHdrSize+1]);
      IPProtocol := pIPHeader.Protocol;
      if IPProtocol = IPPROTO_TCP then begin
      HL := (pIPHeader.VerLen and $0F);
      pTCPHeader := TTCPHeaderPtr(@Buffer.m_IBuffer[(MHdrSize+1)+(4*HL)]);
      end else exit;

      dwTcpLen := ntohs(pIPHeader.TotalLen) – (4*HL);
      if (dwTcpLen div 2)*2 <> dwTcpLen then begin
      padd := 1;
      Buffer.m_IBuffer[dwTcpLen + (4*HL) + MHdrSize+1] := 0;
      end;

      buff := Tbyte(@Buffer.m_IBuffer[(MHdrSize+1)+(4*HL)]);

      Memo1.Lines.Add(‘oldTCPchecksum = ‘+IntToStr(pTCPHeader.Checksum));

      for i:=0 to dwTcpLen+padd-1 do begin
      w16 := ((buff[i*2] shl 8)and $FF00) + (buff[i*2+1]and $FF);
      sum := sum + integer(w16);
      end;

      //add IP length
      sIP := in_addr(pIPHeader.SourceIp);
      dIP := in_addr(pIPHeader.DestIp);
      sum := sum + ntohs(sIP.S_un_w.s_w1) + ntohs(sIP.S_un_w.s_w2);
      sum := sum + ntohs(dIP.S_un_w.s_w1) + ntohs(dIP.S_un_w.s_w2);
      sum := sum + IPPROTO_TCP + word(dwTcpLen);

      while (sum shr 16)>0 do
      sum := (sum and $FFFF)+(sum shr 16);
      sum := not sum;

      Memo1.Lines.Add(‘TCPchecksum = ‘+IntToStr(ntohs(word(sum))));

      end;

      #5484
      PLY
      Participant

        don’t have any one use Delphi? T_T

      Viewing 2 posts - 1 through 2 (of 2 total)
      • You must be logged in to reply to this topic.