diff --git a/udp.py b/udp.py index 2d6e1071e383c84cf91b309c98f9315644a96269..dc4274e253946e020ac9086b74dfc00d2cb76335 100644 --- a/udp.py +++ b/udp.py @@ -12,8 +12,45 @@ async def main(): message = await websocket.recv() udp = decodePacket(message) printUDPPacket(udp) - compute_checksum(udp['Source Port'],udp['Dest Port'],udp['Server Sent'][8:(len(udp['Server Sent'])+8)],udp['Data Length']) + compute_checksum(udp['Source Port'],udp['Dest Port'],udp['Server Sent'][8:(len(udp['Server Sent'])+8)]) + #print(f"calculated checksum: {int(compute_checksum(2,1,b'AB'),2)}") + print(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() + print("here") + print(decodePacket(message)) + + + + +async def send_packet(websocket, sourcePort, destPort, payload): + print(type(payload)) + checkSum = compute_checksum(sourcePort, destPort, payload) + print(checkSum) + destPort = destPort.to_bytes(2, 'little') + sourcePort = sourcePort.to_bytes(2, 'little') + length = 8 + len(payload) + length = length.to_bytes(2, 'little') + checkSum = checkSum.to_bytes(2, 'little') + print(f"length : {length}") + + packet = sourcePort + destPort + length + checkSum + payload + + print(destPort + sourcePort) + packet = base64.b64encode(packet) + await websocket.send(packet) + def decodePacket(response): decoded = {} @@ -32,16 +69,16 @@ def decodePacket(response): def printUDPPacket(dict): for key, value in dict.items(): print(f"{key}: {value}") + -def compute_checksum(sourcePort, destPort, payload, length): +def compute_checksum(sourcePort, destPort, payload): print(sourcePort) print(destPort) print(payload) - print(length) sourcePort = sourcePort.to_bytes(2, 'little') destPort = destPort.to_bytes(2, 'little') - length = length.to_bytes(2, 'little') + length = (len(payload)+8).to_bytes(2, 'little') calc = sourcePort + destPort + length + bytes(2) + payload @@ -57,147 +94,20 @@ def compute_checksum(sourcePort, destPort, payload, length): tmp = (calc[i] << 8) + calc[i+1] sum += tmp - sum = (sum << 16) + (sum & 0xFF) - - return ~sum + sum = (sum >> 16) + (sum & 0xFFFF) + sum = ~sum & 0xFFFF + sum = int(format(sum,'b'),2) + return sum - # payload = bytearray(payload) - print(type(payload)) - # if (len(payload) % 2) != 0: - # print("0") - # payload.append(bytes()) - x = 0 - i = 0 - print(len(payload)) - 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 - x = "0" + x - y = format(payload[i+1],"b") - 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 = "" - calc = calc & 0xFFFF - print(type(calc)) - 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": - flipped += "0" - elif bit == "0": - flipped += "1" - # flipped += bit - print(flipped) - print(int(flipped,2)) - - - #mod odd number then add 8 0's async def recv_message(websocket): message = json.loads(await websocket.recv()) 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__": print("Echo client") # while 1: - asyncio.run(main()) + asyncio.run(mainSend()) time.sleep(10)