I came across this peice of code here: to compute checksum.
As far as I understand in order to segregate the binary data structure into 16 bit words as required for TCP checksum:
I recon the value of w should be dirieved as w= ord(msg[i]) << 8 + ord(msg[i+1])
unless, the byte order has to be changed. I am not too sure as to why w would be assigned value as w = ord(msg[i]) + ord(msg[i+1]) << 8
. Is there anything specific I am missing here?
def checksum(msg):
s = 0
# loop taking 2 characters at a time
for i in range(0, len(msg), 2):
w = ord(msg[i]) + (ord(msg[i+1]) << 8 )
s = s + w
s = (s>>16) + (s & 0xffff);
s = s + (s >> 16);
#complement and mask to 4 byte short
s = ~s & 0xffff
return s
In this case I think "network order", "big endian" and "little endian" are being mixed with the TCP Checksum calculation.
The TCP Checksum calculation is defined in RFC 1071: https://www.rfc-editor.org/rfc/rfc1071
At the beginning of page 2:
Using the notation [a,b] for the 16-bit integer a*256+b, where a and b are bytes,
The bytes in the Pseudo Header and partially filled TCP Header are just "bytes" and no implication is made as to what they mean (they must already be in "network order")
The formula used by the author is just following RFC 1071