Re: Re: Modify packet [DELPHI]

Home Forums Discussions Support Modify packet [DELPHI] Re: Re: Modify packet [DELPHI]

#5582
SergeySW
Participant

    function RecalculateTCPChecksum(aLen: Integer; aSrcAddr, aDstAddr: Cardinal; buff: PByteArray): Word;
    var
    w16,padd: word;
    i,sum: ulong;
    sIP,dIP: in_addr;
    begin
    padd := 0;
    sum := 0;
    if (aLen div 2) * 2 <> aLen then begin
    padd := 1;
    buff[aLen] := 0;
    end;
    i := 0;
    while i < aLen+padd do begin
    w16 := ((buff shl 8)and $FF00) + (buff[i+1]and $FF);
    sum := sum + dword(w16);
    i := i + 2;
    end;

    //add IP length
    sIP := in_addr(aSrcAddr);
    dIP := in_addr(aDstAddr);
    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(aLen);

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

    sum := not sum;
    sum := htons(sum);
    Result := sum;
    end;

    aaLen := ntohs(pIPHeader.TotalLen) - ((pIPHeader.VerLen and $F)*4);
    pTCPHeader.Checksum := 0;
    testSum := RecalculateTCPChecksum(aaLen, pIPHeader.SourceIp, pIPHeader.DestIp, byteArray(pTCPHeader));
    pTCPHeader.Checksum := testSum;