diff --git a/README.md b/README.md index f40ce2d7a188b27fe2cd575b38eadabc54cef2fc..efee67e198ea71d176ef955df4fc201cd4858001 100644 --- a/README.md +++ b/README.md @@ -21,10 +21,12 @@ - [License](#license) ## Description - -Morse code :: Worksheet 3 -This is a rep for only worksheet 3. Worksheet 2 part 2 can be found here [IOT - Worksheet 2 part 2 ](https://gitlab.uwe.ac.uk/jo2-holdsworth/iot-worksheet-2-part-2) - +Worksheet 3: +-This worksheet explored the UDP protocol. We will be decoding, encoding and comunicating with it in this rep +**-This is a rep for only worksheet 3.** +* [IOT - Worksheet 2 part 1 ](https://gitlab.uwe.ac.uk/jo2-holdsworth/iot-worksheet-2-part-1) can be found here +* [IOT - Worksheet 2 part 2 ](https://gitlab.uwe.ac.uk/jo2-holdsworth/iot-worksheet-2-part-2) can be found here +* [IOT - Worksheet 3](https://gitlab.uwe.ac.uk/jo2-holdsworth/iot-worksheet-3) **you are here** ### Task 1: UDP decoder. * *The CSCT cloud server has a port open on 5612 which transmits and receives UDP packets. We are to create a basic client to receive the hello message from the UDP server and decode it. diff --git a/checksum.py b/checksum.py index d8b7d72c45101e8b8e8e245a65baa8737723c4a8..60ca77e6839346f45659e142744661b0c8454616 100644 --- a/checksum.py +++ b/checksum.py @@ -1,3 +1,10 @@ +# ============== +# File: checksum.py +# By: Jack Holdsworth - 20023933 +# Use: a program to calculate a udp checksum given inputs +# Written on: Python 3.8 +# ============== + from udpdecoder import * def compute_checksum(sourcePort, destPort, payload): diff --git a/server.py b/server.py index c08bc38a8a83e9af42307d1eadf5c7bc8bcd508f..bf0e812eaf2cc7c7527057fef06b121cdbe8b435 100644 --- a/server.py +++ b/server.py @@ -1,3 +1,10 @@ +# ============== +# File: server.py +# By: Jack Holdsworth - 20023933 +# Use: a program to get packets from the udp server +# Written on: Python 3.8 +# ============== + import asyncio import websockets import json diff --git a/serverudptime.py b/serverudptime.py index efb5d061f363be6733b909e5c48ec847647b8729..b853dc66bcbb25e26b8983c55b1454e561280eff 100644 --- a/serverudptime.py +++ b/serverudptime.py @@ -1,3 +1,10 @@ +# ============== +# File: serverudptime.py +# By: Jack Holdsworth - 20023933 +# Use: Task 3, a program to get time from a server via udp +# Written on: Python 3.8 +# ============== + from checksum import * async def mainSendandReceive(): diff --git a/udp.py b/udp.py deleted file mode 100644 index 9ff1f17612a07274f1ab90325e95033c3bf7a22e..0000000000000000000000000000000000000000 --- a/udp.py +++ /dev/null @@ -1,122 +0,0 @@ -import asyncio -import websockets -import json -import time -import base64 - - -async def main(): - uri = "ws://localhost:5612" - async with websockets.connect(uri) as websocket: - # After joining server will send client unique id. - 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)]) - # 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 : {int.from_bytes(length, 'little')}") - - packet = sourcePort + destPort + length + checkSum + payload - - print(f"packet: {packet}") - packet = base64.b64encode(packet) - await websocket.send(packet) - - -def decodePacket(response): - decoded = {} - decoded['Base64'] = response - packet = base64.b64decode(response) - decoded['Server Sent'] = packet - decoded['Source Port'] = int.from_bytes(packet[:2], 'little') - 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") - return decoded - - - -def printUDPPacket(dict): - print("=== Decoded UDP Packet ===") - for key, value in dict.items(): - print(f"{key}: {value}") - print("=== Decoded UDP Packet ===") - - -def compute_checksum(sourcePort, destPort, payload): - print(sourcePort) - print(destPort) - print(payload) - - sourcePort = sourcePort.to_bytes(2, 'little') - destPort = destPort.to_bytes(2, 'little') - length = (len(payload)+8).to_bytes(2, 'little') - - calc = sourcePort + destPort + length + bytes(2) + payload - - print(f"starting calc: {calc}") - calcPayload = "" - print(type(calcPayload)) - print(destPort) - sum = 0 - if len(payload) % 2: - calc += bytes(1) - - for i in range(0, len(calc), 2): - tmp = (calc[i] << 8) + calc[i+1] - print(calc[i]) - print(format(calc[i],'b')) - print(format(calc[i+1],'b')) - print(format(tmp,'b')) - print(calc[i+1]) - sum += tmp - - print(tmp) - - sum = (sum >> 16) + (sum & 0xFFFF) - sum = ~sum & 0xFFFF - sum = int(format(sum,'b'),2) - return sum - - - -async def recv_message(websocket): - message = json.loads(await websocket.recv()) - return message['payload'] - -if __name__ == "__main__": - print("Echo client") - -# while 1: - asyncio.run(main()) - time.sleep(10) diff --git a/udpdecoder.py b/udpdecoder.py index 360f4c5b0a2a7063aba84980c0dcdf1546ad65d0..69f4c8f5c7767af952e6caa0f739863737d19532 100644 --- a/udpdecoder.py +++ b/udpdecoder.py @@ -1,3 +1,9 @@ +# ============== +# File: udpdecoder.py +# By: Jack Holdsworth - 20023933 +# Use: Task 1, a udp packet decoder uses server.py +# Written on: Python 3.8 +# ============== from server import * import base64 diff --git a/unititest.py b/unititest.py index fba46d166178233cd27563e6c5d1476c48226381..84bc0bde0685868e7ca24114318b4f7caea1cf5e 100644 --- a/unititest.py +++ b/unititest.py @@ -1,3 +1,9 @@ +# ============== +# File: unittest.py +# By: Jack Holdsworth - 20023933 +# Use: A simple program to unittest the tasks from worksheet 3 +# Written on: Python 3.8 +# ============== import unittest import datetime from serverudptime import *