diff --git a/udp.py b/udp.py index 2d6e1071e383c84cf91b309c98f9315644a96269..da6b01d0a46b7da1e0001448da135ecc932ddc1e 100644 --- a/udp.py +++ b/udp.py @@ -12,8 +12,44 @@ 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']) - print(bin(udp['Checksum'])) + 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(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): decoded = {} @@ -24,7 +60,7 @@ def decodePacket(response): decoded['Dest Port'] = int.from_bytes(packet[2:4], 'little') decoded['Data Length'] = int.from_bytes(packet[4:6], '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 @@ -34,170 +70,76 @@ def printUDPPacket(dict): print(f"{key}: {value}") def compute_checksum(sourcePort, destPort, payload, length): - 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') - - calc = sourcePort + destPort + length + bytes(2) + payload - + print(f"sourcePort: {sourcePort}") + print(f"destPort: {destPort}") + print(f"payload: {payload}") + print(f"length: {length}") + sourcePort = sourcePort.to_bytes(1, 'little') + destPort = destPort.to_bytes(1, 'little') + length = length.to_bytes(1, 'little') + print(f"sourcePort: {sourcePort}") + print(f"destPort: {destPort}") + 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}") - calcPayload = "" - print(type(calcPayload)) sum = 0 if len(payload) % 2: calc += bytes(1) + print(calc) for i in range(0, len(calc), 2): tmp = (calc[i] << 8) + calc[i+1] sum += tmp - - sum = (sum << 16) + (sum & 0xFF) - - 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")) + print(f"calc[i]: {calc[i]}") + print(format(calc[i], 'b')) + print(f"calc[i+1]: {calc[i+1]}") + print(f"temp: {tmp}") + print(f"temp b: {format(tmp, 'b')}") + print(i) + print(f"sum: {format(sum, 'b')}") + + print(format(sum, 'b')) + # sum = (sum & 0xFFFF) + + sum = format(sum, 'b') + sum = sum[-16+len(sum):] + print(sum) + print(len(sum)) + for f in range(0,16 - len(sum)): + sum = "0" + sum + print("hello") + # sum = (sum << 16) + (sum & 0xFF) + print(sum) 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: + # print(str(sum)) + for bit in sum: 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 - - + return flipped 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") - + print(f"calculated checksum: {int(compute_checksum(2,1,b'AB',9),2)}") # while 1: asyncio.run(main()) 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