Skip to content
Snippets Groups Projects
Commit af3f020d authored by akramab's avatar akramab
Browse files

add initial work for checksum

parent 75d51713
No related merge requests found
import struct
# constant
SEG_SIZE = 32782
DATA_SIZE = SEG_SIZE - 12
......@@ -62,7 +64,21 @@ class Segment():
def count_checksum(self):
# To-Do
# ...
return 0
self.bytes[10] = 0 # dangerous
self.bytes[11] = 0 # dangerous
data = self.to_bytes()
len_data = len(data) # cara akses bagian data saja, karena perlu di rstrip("\x00")
if (len_data % 2 != 0): # kalau panjangnya ganjil, kudu ditambahin 0 di akhir, pakai struct
len_data += 1
data += struct.pack('!B', 0)
for i in range (0, len_data, 2):
w = (data[i] << 8) + (data[i + 1])
checksum += w
checksum = (checksum >> 16) + (checksum & 0xFFFF)
return checksum
def test_checksum(self):
# return self.count_checksum == self.get_checksum
......@@ -79,5 +95,12 @@ if __name__=="__main__":
seg = Segment()
seg.setFIN()
seg.set_seq_no()
seg.set_ack_no()
checksum = seg.count_checksum()
seg.set_checksum(checksum)
seg = Segment(seqno, ackno, flag, data)
print(seg.bytes[:100])
print(seg.bytes[:100])
\ No newline at end of file
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment