You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
52 lines
1.4 KiB
52 lines
1.4 KiB
# Code has been taken from https://www.codeproject.com/Tips/460867/Python-Implementation-of-IP-Checksum
|
|
# Example is provided at https://www.thegeekstuff.com/2012/05/ip-header-checksum/
|
|
|
|
# For now, manually changing values in the header field to compute IP checksum.
|
|
|
|
def ip_checksum(ip_header, size):
|
|
|
|
cksum = 0
|
|
pointer = 0
|
|
|
|
#The main loop adds up each set of 2 bytes. They are first converted to strings and then concatenated
|
|
#together, converted to integers, and then added to the sum.
|
|
while size > 1:
|
|
cksum += int((str("%02x" % (ip_header[pointer],)) +
|
|
str("%02x" % (ip_header[pointer+1],))), 16)
|
|
size -= 2
|
|
pointer += 2
|
|
if size: #This accounts for a situation where the header is odd
|
|
cksum += ip_header[pointer]
|
|
|
|
cksum = (cksum >> 16) + (cksum & 0xffff)
|
|
cksum += (cksum >>16)
|
|
|
|
return (~cksum) & 0xFFFF
|
|
|
|
|
|
if __name__=="__main__":
|
|
|
|
header = {}
|
|
|
|
header[0] = 0x45
|
|
header[1] = 0x00
|
|
header[2] = 0x00
|
|
header[3] = 0x28
|
|
header[4] = 0x00
|
|
header[5] = 0x02
|
|
header[6] = 0x00
|
|
header[7] = 0x00
|
|
header[8] = 0x28
|
|
header[9] = 0x06
|
|
header[10] = 0xfd
|
|
header[11] = 0x5f
|
|
header[12] = 0xc0
|
|
header[13] = 0xa8
|
|
header[14] = 0x0a
|
|
header[15] = 0x14
|
|
header[16] = 0xc0
|
|
header[17] = 0xa8
|
|
header[18] = 0x0a
|
|
header[19] = 0x0a
|
|
|
|
print("Checksum is: %x" % (ip_checksum(header, len(header)),))
|