I packed a struct using this line
# type(8) code(8) checksum(16) process id(16) sequence(16)
packet = struct.pack("bbHHh", ICMP_ECHO_REQUEST, 0, packet_checksum, pid, sequence)
And now im using a checksum calculation function which unpacks the packet in a way that the 16 bits of where the checksum should be (packet_checksum) is removed. But i don't know how it does that, and what does the sum function actually do? add up the different elements?:
n = len(pkt)
two_bytes = struct.unpack("%sH" % (n/2), pkt)
chksum = sum(two_bytes)
the code works well i'm just wondering how it works.
n = len(pkt); ..."%sH" % (n/2)
because, well, H is 2 bytes, so the length in bytes is taken, then divided by 2, then multiplied by the length of H, which is 2 again...n = (n/2) * 2
So this part seems pointless. – zxq9