![]() |
Ip-Protokoll und (CRC-16) Berechnung... Und ich versteh nix.
Ich hab so ein kleines Problem mit der Checksumberechnung von einm IP-Protokoll Packet...
1. Ich kann es noch nicht per Hand berechnen, da ich da nicht wirklich durchsteig... 2. wenn ich sie berechnen lasse, bekomm ich was anderes raus als sollte... Erst mal was dazu: Internet Protocol
Code:
So die Headerchecksume wird aus dem Header berechnet...
0 1 2 3
0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |Version| IHL |Type of Service| Total Length | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | Identification |Flags| Fragment Offset | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | Time to Live | Protocol | Header Checksum | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | Source Address | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | Destination Address | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | Options | Padding | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ ![]() Zitat:
Delphi-Quellcode:
Der Header sollte dann folgendes sein:
Const
cProto: Array[ $00..$2D ] Of Byte = ( $45, $00, $00, $28, $A2, $EE, $40, $00, $80, $06, $0E, $55, $C0, $A8, $64, $39, $C0, $A8, $64, $02, $06, $4D, $01, $BD, $0A, $62, $36, $C5, $47, $E8, $96, $1E, $50, $10, $FC, $FD, $42, $12, $00, $00, $00, $00, $00, $00, $00, $00 );
Delphi-Quellcode:
Aber wenn ich die Checksumme mit tools wie z.B. dem HxD-Hexeditor :) berechnnen lasse, komme nicht auf die Checksumme von 0x0E55 ...
Const
cIPHeader: Array[ $00..$09 ] Of Byte = ( $45, $00, $00, $28, $A2, $EE, $40, $00, $80, $06 ); laut Ethereal ist die Checksumme aber io. Kann mir wer sagen ob die tools falsch rechnen, ich den Ip-Header falsch abgrenze, oder ka.. Vieleicht is ja auch wer so lieb mir genau zu erklären wie ich das per Hand ausrechnen kann *liebgug* Irgendwie wird auf Bit-Ebene durch 2 dividiert und der Rest ist die Checksumme... Und da hört es schon auf... Bye |
Re: Ip-Protokoll und (CRC-16) Berechnung... Und ich versteh
Hat keiner nen kleinen Tip???
Ich bin der Lösung nähmlich noch kein Stück näher gekommen :( Bye |
Re: Ip-Protokoll und (CRC-16) Berechnung... Und ich versteh
Zitat:
Also grob so:
Delphi-Quellcode:
IPHeader : Array[0..12-1] of Word;
Result := 0; for i:=0 to 12-1 do begin if i <> 5 then // das Feld Header Checksum auslassen Inc(Result, not IPHeader[i]); Result := not Result; // one's Complement end; |
Re: Ip-Protokoll und (CRC-16) Berechnung... Und ich versteh
:wiejetzt:
Zitat:
Also wird die Checksumme aus 24 Bytes berechnet sprich: "Version", "IHL", "Type of Service", "Total Length", "Identification", "Flags", "Fragment Offset", "Time to Live", "Protocol", "Source Address", "Destination Address" (=16Byte) + weiteren 8Bytes aus "Options", "Padding" und Datenteil... Hab ich das nun korrect verstanden? :drunken: Bye |
Re: Ip-Protokoll und (CRC-16) Berechnung... Und ich versteh
Zitat:
Grund: die Prüfsumme muss jedesmal wenn das Paket durch einen Router geht neu berechnet werden Würden die Nutzdaten mit in die Prüfsumme einfliesen, würde das den Transport verzögern. Die Prüfsumme wird über den gesamten Header gebildet; also von "Version" bis einschliesslich "Padding". Hier nochmals mein geänderter Sourcecode:
Delphi-Quellcode:
IPHeader : Array[0..12-1] of Word;
Result := 0; //The checksum field is the 16 bit one's complement of the one's //complement sum of all 16 bit words in the header. For purposes of //computing the checksum, the value of the checksum field is zero. IPHeader[5] := 0; // also Prüfsumme löschen // jetzt den ganzen IP-Header Wort für Wort summieren // one's complement = not for i:=0 to 12-1 do begin Inc(Result, not IPHeader[i]); end; Result := not Result; // one's Complement |
Re: Ip-Protokoll und (CRC-16) Berechnung... Und ich versteh
sry erstmal, das ich erst jetzt schreibe, war mir nicht anders möglich...
Zitat:
Zitat:
Zum nächsten Erzeugt deine Funktion nen Integerüberlauf :zwinker: Ok mit ausgeschalteten Überprüfungen bekomm ich zwar ein Ergebniss aber nicht das Richtige... Das ist ein komplettes IP-Packet:
Delphi-Quellcode:
Dieses nun zerlegt:
Const
cProto: Array[ $00..$2D ] Of Byte = ( $45, $00, $00, $28, $A2, $EE, $40, $00, $80, $06, $0E, $55, $C0, $A8, $64, $39, $C0, $A8, $64, $02, $06, $4D, $01, $BD, $0A, $62, $36, $C5, $47, $E8, $96, $1E, $50, $10, $FC, $FD, $42, $12, $00, $00, $00, $00, $00, $00, $00, $00 );
Code:
-> 0 Byte Options + 0 Byte Padding...
Version (4 Bit $4 ):= 4
IHL (4 Bit $5 ):= 5 --> Headerlänge 20 Byte Type Of Service (1 Byte $00 ):= 0 Total Length (2 Byte $0028 ):= 40 Identification (2 Byte $A2EE ):= 41710 Flags (3 Bit ):= 0 1 0 Fragment Offset (1 Byte + 5 Bit ):= 0 Time to Live (1 Byte $80 ):= 128 Protocol (1 Byte $06 ):= 6 Header Checksum (2 Byte $0E55 ):= 3669 Source Address (4 Byte $C0A86439 ) Destination Address (4 Byte $C0A86402 ) ----------------------------------------- = 20 Byte -> Datenteil ( 20 Byte ):
Delphi-Quellcode:
-> 4Bytes := $00 Padding...
Const
cData: Array[ $00..$13 ] Of Byte = ( $06, $4D, $01, $BD, $0A, $62, $36, $C5, $47, $E8, $96, $1E, $50, $10, $FC, $FD, $42, $12, $00, $00 ); Soweit so gut! Wenn du nun aus cProto den CRC $0E55 berechnest bist mein Held/Heldin :angel: Ich habst immernoch nicht geschafft... Ich bin scheinbar einfach zu plöd dafür :wall: Bye |
Re: Ip-Protokoll und (CRC-16) Berechnung... Und ich versteh
hier meine Routine:
Delphi-Quellcode:
die Checksumme wird von den ersten n Bytes des Packets berechnet, wobei n die Header-Länge gemäss IP-Header ist.
Function CalcIPChecksum(Const Packet : Pointer;Const Bytes : LongWord) : Word;
Var Checksum : LongWord; X : Integer; begin Checksum:=0; For X:=1 To (Bytes shr 1) Do Checksum:=Checksum + PWords(Packet)^[X - 1]; If ((Bytes and 1) > 0) Then Checksum:=Checksum + PBytes(Packet)^[Bytes - 1]; CheckSum:=(CheckSum shr 16) + (CheckSum and $FFFF); CheckSum:=CheckSum + (CheckSum shr 16); Result:=Word(not Checksum); end; Zu beachten ist: - vor der Berechnung wird das Feld der Checksumme im IP-Header auf $0000 gesetzt - die Checksumme liegt im Packet in Network-Byte-Order vor.... mittels WinSock.NToHS lässt sich ein Word (S-hort) von Network-Byte-Order zu Host-Byte-Order wandeln (WinSock.HToNS für Gegenrichtung) |
Re: Ip-Protokoll und (CRC-16) Berechnung... Und ich versteh
Zitat:
:wall: :wiejetzt: 1. Was ist PWords??? 2. Was ist PBytes??? 3. Kommt in Packet mein Byte Array? Wenn ja warum arbeitest du dann mit Pointern? 4. Kommt in Bytes die Länge des Byte Arrays? Wenn ja ein Word reicht aus, da ein Ip-Packet net größer als 1500 Bytes werden kann. Zitat:
Bye |
Re: Ip-Protokoll und (CRC-16) Berechnung... Und ich versteh
Zitat:
Delphi-Quellcode:
2.:
type
PWords = ^TWords; TWords = Array[0..high(word)] Of Word;
Delphi-Quellcode:
3. Packet soll ein Pointer auf das 1. Byte des IP-Header sein
type
PBytes = ^TBytes; TBytes = Array[0..high(word)] Of Byte; 4. Richtig; allerdings verwende ich einheitlich LongWord, wenn es um die Grösse von Pointer-Daten geht Zitat:
Delphi-Quellcode:
Const
cProto: Array[ $00..$2D ] Of Byte = ( $45, $00, $00, $28, $A2, $EE, $40, $00, $80, $06, $0E, $55, $C0, $A8, $64, $39, $C0, $A8, $64, $02, $06, $4D, $01, $BD, $0A, $62, $36, $C5, $47, $E8, $96, $1E, $50, $10, $FC, $FD, $42, $12, $00, $00, $00, $00, $00, $00, $00, $00 ); var headerlen : LongWord; buf : array[0..$f * 4 - 1] of byte; cs : word; begin headerlen:=(cProto[0] and $0f) shl 2; move(cProto,buf,headerlen); cs:=pwords(@buf)^[5]; pwords(@buf)^[5]:=0; // checksum feld 0-en vor Berechnung pwords(@buf)^[5]:=CalcIPChecksum(@buf,headerlen); // Checksum neu berechnen Assert(cs = pwords(@buf)^[5],'Checksum incorrect'); end; |
Re: Ip-Protokoll und (CRC-16) Berechnung... Und ich versteh
*tüttüt*
Zusammenfassung:
Delphi-Quellcode:
Dann kommt bei mir 21774 raus obwohl 3669 das richtige ergebniss wär ?!?!?
Function CRC2Test: Word;
Type PWords = ^TWords; TWords = Array[0..high(word)] Of Word; PBytes = ^TBytes; TBytes = Array[0..high(word)] Of Byte; Function CalcIPChecksum(Const Packet : Pointer;Const Bytes : LongWord) : Word; Var Checksum : LongWord; X : Integer; Begin Checksum:=0; For X:=1 To (Bytes shr 1) Do Checksum:=Checksum + PWords(Packet)^[X - 1]; If ((Bytes and 1) > 0) Then Checksum:=Checksum + PBytes(Packet)^[Bytes - 1]; CheckSum:=(CheckSum shr 16) + (CheckSum and $FFFF); CheckSum:=CheckSum + (CheckSum shr 16); Result:=Word(not Checksum); End; Const cProto: Array[ $00..$2D ] Of Byte = ( $45, $00, $00, $28, $A2, $EE, $40, $00, $80, $06, $0E, $55, $C0, $A8, $64, $39, $C0, $A8, $64, $02, $06, $4D, $01, $BD, $0A, $62, $36, $C5, $47, $E8, $96, $1E, $50, $10, $FC, $FD, $42, $12, $00, $00, $00, $00, $00, $00, $00, $00 ); Var headerlen : LongWord; buf : Array[0..$f * 4 - 1] Of byte; cs : word; Begin headerlen:=(cProto[0] and $0f) shl 2; move(cProto,buf,headerlen); cs:=pwords(@buf)^[5]; pwords(@buf)^[5]:=0; // checksum feld 0-en vor Berechnung pwords(@buf)^[5]:=CalcIPChecksum(@buf,headerlen); // Checksum neu berechnen Assert(cs = pwords(@buf)^[5],'Checksum incorrect'); Result := cs; End; :wall: Bin ich zu blöd? Im Grunde soll doch die Checksum das 1er Komplement der Summe alle Word vom Ip-Header sein, wobei aber die Checksumm auf 0 gesetzt wird korrekt? Vereinfacht:
Delphi-Quellcode:
Aber auch hier lieg ich mit 3 zuviel :love:
Function CRC1Test: Word;
Const cIPPHeader: Array[ $00..$09 ] Of Integer = ( $4500, $0028, $A2EE, $4000, $8006, $0000, $C0A8, $6439, $C0A8, $6402 ); Begin Result := Not ( Word( SumInt( cIPPHeader ) And $FFFF ) ); End; Bye |
Alle Zeitangaben in WEZ +1. Es ist jetzt 17:41 Uhr. |
Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO © 2011, Crawlability, Inc.
Delphi-PRAXiS (c) 2002 - 2023 by Daniel R. Wolf, 2024 by Thomas Breitkreuz