Skip to content
Snippets Groups Projects
Commit 9e732300 authored by jo2-holdsworth's avatar jo2-holdsworth
Browse files

task2 fail

parent e66586f8
No related branches found
No related tags found
No related merge requests found
...@@ -12,8 +12,44 @@ async def main(): ...@@ -12,8 +12,44 @@ async def main():
message = await websocket.recv() message = await websocket.recv()
udp = decodePacket(message) udp = decodePacket(message)
printUDPPacket(udp) printUDPPacket(udp)
compute_checksum(udp['Source Port'],udp['Dest Port'],udp['Server Sent'][8:(len(udp['Server Sent'])+8)],udp['Data Length']) print(f"calculated checksum: {int(compute_checksum(udp['Source Port'],udp['Dest Port'],udp['Server Sent'][8:(len(udp['Server Sent'])+8)],udp['Data Length']),2)}")
print(bin(udp['Checksum'])) #print(f"calculated checksum: {int(compute_checksum(2,1,b'AB',9),2)}")
print(f"looking for bin : {bin(udp['Checksum'])}")
async def mainSend():
uri = "ws://localhost:5612"
async with websockets.connect(uri) as websocket:
await recv_and_decode_packet(websocket)
while True:
await send_packet(websocket, 0, 542, b'1111')
await recv_and_decode_packet(websocket)
time.sleep(1)
async def recv_and_decode_packet(websocket):
message = await websocket.recv()
decodePacket(message)
async def send_packet(websocket, destPort, sourcePort, payload):
print(type(payload))
checkSum = compute_checksum(sourcePort, destPort, payload, len(payload))
print(checkSum)
destPort.to_bytes(2, 'little')
sourcePort.to_bytes(2, 'little')
length = 8 + len(payload)
length.to_bytes(2, 'little')
print(f"length : {length}")
sourcePort = sourcePort << 48
destPort = destPort << 32
length = length << 16
packet = sourcePort + destPort + length + checkSum
print(destPort + sourcePort)
input()
await websocket.send()
def decodePacket(response): def decodePacket(response):
decoded = {} decoded = {}
...@@ -24,7 +60,7 @@ def decodePacket(response): ...@@ -24,7 +60,7 @@ def decodePacket(response):
decoded['Dest Port'] = int.from_bytes(packet[2:4], 'little') decoded['Dest Port'] = int.from_bytes(packet[2:4], 'little')
decoded['Data Length'] = int.from_bytes(packet[4:6], 'little') decoded['Data Length'] = int.from_bytes(packet[4:6], 'little')
decoded['Checksum'] = int.from_bytes(packet[6:8], 'little') decoded['Checksum'] = int.from_bytes(packet[6:8], 'little')
decoded['Payload'] = packet[8:(len(packet)+8)].decode("utf-8") decoded['Payload'] = packet[8:(len(packet))].decode("utf-8")
return decoded return decoded
...@@ -34,170 +70,76 @@ def printUDPPacket(dict): ...@@ -34,170 +70,76 @@ def printUDPPacket(dict):
print(f"{key}: {value}") print(f"{key}: {value}")
def compute_checksum(sourcePort, destPort, payload, length): def compute_checksum(sourcePort, destPort, payload, length):
print(sourcePort) print(f"sourcePort: {sourcePort}")
print(destPort) print(f"destPort: {destPort}")
print(payload) print(f"payload: {payload}")
print(length) print(f"length: {length}")
sourcePort = sourcePort.to_bytes(1, 'little')
sourcePort = sourcePort.to_bytes(2, 'little') destPort = destPort.to_bytes(1, 'little')
destPort = destPort.to_bytes(2, 'little') length = length.to_bytes(1, 'little')
length = length.to_bytes(2, 'little') print(f"sourcePort: {sourcePort}")
print(f"destPort: {destPort}")
calc = sourcePort + destPort + length + bytes(2) + payload print(f"payload: {payload}")
print(f"length: {length}")
print(len(length))
calc = bytes(1) + sourcePort + bytes(1) + destPort + bytes(1) + length + bytes(2) + payload
print(calc)
print(f"starting calc: {calc}") print(f"starting calc: {calc}")
calcPayload = ""
print(type(calcPayload))
sum = 0 sum = 0
if len(payload) % 2: if len(payload) % 2:
calc += bytes(1) calc += bytes(1)
print(calc)
for i in range(0, len(calc), 2): for i in range(0, len(calc), 2):
tmp = (calc[i] << 8) + calc[i+1] tmp = (calc[i] << 8) + calc[i+1]
sum += tmp sum += tmp
print(f"calc[i]: {calc[i]}")
sum = (sum << 16) + (sum & 0xFF) print(format(calc[i], 'b'))
print(f"calc[i+1]: {calc[i+1]}")
return ~sum print(f"temp: {tmp}")
print(f"temp b: {format(tmp, 'b')}")
# payload = bytearray(payload) print(i)
print(type(payload)) print(f"sum: {format(sum, 'b')}")
# if (len(payload) % 2) != 0:
# print("0") print(format(sum, 'b'))
# payload.append(bytes()) # sum = (sum & 0xFFFF)
x = 0
i = 0 sum = format(sum, 'b')
print(len(payload)) sum = sum[-16+len(sum):]
j = (len(payload) // 2)*2 print(sum)
print(j) print(len(sum))
while i < j: for f in range(0,16 - len(sum)):
x = format(payload[i],"b") sum = "0" + sum
for n in range(0,8 - len(x)): #n note used print("hello")
x = "0" + x # sum = (sum << 16) + (sum & 0xFF)
y = format(payload[i+1],"b") print(sum)
for n in range(0,8 - len(y)):
y = "0" + y
z = x + y
z = "0b" + z
print(f"x: {x}")
print(f"y: {y}")
print(f"z: {z}")
calc = calc + int(z,2)
print(f"int of z: {int(z,2)}")
print(f"calc after calc: {calc}")
i += 2
if len(payload) % 2 != 0:
print(f"i: {i}")
x = format(payload[i],"b")
for f in range(0,8 - len(x)):
x = "0" + x
print(f"payload: {payload[i]}")
y = "00000000"
z = x + y
print(x)
print(z)
z = "0b" + z
calc = calc + int(z,2)
# while i <= len(payload):
# x = format(payload[i],"b")
# for n in range(0,8 - len(x)): #n note used
# x = "0" + x
# y = format(payload[i+1],"b")
# for n in range(0,8 - len(z)):
# z = "0" + z
# z = x + y
# z = "0b" + z
# calc = calc + int(z,2)
# print(calc)
# i += 2
# else:
# print("heres")
# j = (len(payload) // 2)*2
# print(j)
# while i < j:
# x = format(payload[i],"b")
# for n in range(0,8 - len(x)): #n note used
# print("hereres")
# x = "0" + x
# y = format(payload[i+1],"b")
# print(f"y: {y}")
# for n in range(0,8 - len(y)):
# print("okkk")
# y = "0" + y
# z = x + y
# print(f"length of z: {len(z)}")
# z = "0b" + z
# calc = calc + int(z,2)
# print(calc)
# i += 2
# print(f"i: {i}")
# x = format(payload[i],"b")
# for f in range(0,8 - len(x)):
# x = "0" + x
# print(f"payload: {payload[i]}")
# y = "00000000"
# z = y + x
# print(x)
# print(z)
# z = "0b" + z
# calc = calc + int(z,2)
# for byte in payload:
# x = + 1
# print(type(byte))
# z = format(byte,"b")
# calcPayload += z
# calcPayload = "0b" + calcPayload
# if x % 2 != 0:
# calcPayload +="00000000"
# print("here")
print(f"before calc: {calc}")
print(format(calc,"b"))
flipped = "" flipped = ""
calc = calc & 0xFFFF # print(str(sum))
print(type(calc)) for bit in sum:
calc = format(calc, "b")
print(f"after calc1: {calc}")
for f in range(0,16 - len(calc)):
calc = "0" + calc
print(f"after calc2: {calc}")
print(int(calc,2))
# print(format(calc,"b"))
# calcPayload = bin(calc)
for bit in calc:
if bit == "1": if bit == "1":
flipped += "0" flipped += "0"
elif bit == "0": elif bit == "0":
flipped += "1" flipped += "1"
# flipped += bit # flipped += bit
print(flipped) print(flipped)
print(int(flipped,2)) return flipped
#mod odd number then add 8 0's
async def recv_message(websocket): async def recv_message(websocket):
message = json.loads(await websocket.recv()) message = json.loads(await websocket.recv())
return message['payload'] return message['payload']
# 0000 0000 0111 0010
# 1111 1101 1111 0111
# 0000 0010 0000 1000
# 0111 0110 0110 0101
# 0111 0110 0110 0101
# 1101 1111 0000 101
# 1101 1111 0000 101
# 0110 1111 1000 0101
#1001 0000 0111 1010
# 0111 0010 0000 0000
#0011 1011 1100 1011
# 1100 0100 0011 0100
if __name__ == "__main__": if __name__ == "__main__":
print("Echo client") print("Echo client")
print(f"calculated checksum: {int(compute_checksum(2,1,b'AB',9),2)}")
# while 1: # while 1:
asyncio.run(main()) asyncio.run(main())
time.sleep(10) time.sleep(10)
# 100 0110 1111 1000 0101
# 1000110111110000101
# 0110 1111 1000 0101
# 1000110111110000101
# 110 1111 1000 0101
# 100 0110 1111 1000 0101
# 1001 0000 0111 1010
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment