-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
get checksum fields get fields which the checksum depends on pass msg as bytes and dict (kwargs) to checksum function test checksum implementation with icmp Ref. #240
- Loading branch information
Showing
4 changed files
with
220 additions
and
36 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
def checksum_icmp(msg: MessageValue) -> int: | ||
def add_ones_complement(num1, num2) -> int: | ||
MOD = 1 << 16 | ||
result = num1 + num2 | ||
return result if result < MOD else (result + 1) % MOD | ||
|
||
msg.set("Checksum", 0) | ||
message_in_sixteen_bit_chunks = [ | ||
int.from_bytes(msg.bytestring[i : i + 2], "big") for i in range(0, len(msg.bytestring), 2) | ||
] | ||
intermediary_result = message_in_sixteen_bit_chunks[0] | ||
for i in range(1, len(message_in_sixteen_bit_chunks)): | ||
intermediary_result = add_ones_complement( | ||
intermediary_result, message_in_sixteen_bit_chunks[i] | ||
) | ||
|
||
return intermediary_result ^ 0xFFFF |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters