MCPcopy Create free account
hub / github.com/Vidimensional/Icmp-File-Transfer / calcule_checksum

Method calcule_checksum

IcmpPacket.py:82–109  ·  view source on GitHub ↗

I'm not too confident that this is right but testing seems to suggest that it gives the same answers as in_cksum in ping.c

(self)

Source from the content-addressed store, hash-verified

80
81
82 def calcule_checksum (self):
83 """
84 I'm not too confident that this is right but testing seems
85 to suggest that it gives the same answers as in_cksum in ping.c
86 """
87 sum = 0
88 countTo = (len(self.packet)/2)*2
89 count = 0
90 while count<countTo:
91 thisVal = ord(self.packet[count + 1])*256 + \
92 ord(self.packet[count])
93 sum = sum + thisVal
94 sum = sum & 0xffffffff # Necessary?
95 count = count + 2
96
97 if countTo<len(self.packet):
98 sum = sum + ord(self.packet[len(self.packet) - 1])
99 sum = sum & 0xffffffff # Necessary?
100
101 sum = (sum >> 16) + (sum & 0xffff)
102 sum = sum + (sum >> 16)
103 answer = ~sum
104 answer = answer & 0xffff
105
106 # Swap bytes. Bugger me if I know why.
107 answer = answer >> 8 | (answer << 8 & 0xff00)
108
109 self.checksum = answer
110

Callers 1

__init__Method · 0.95

Calls

no outgoing calls

Tested by

no test coverage detected