TASK 1
UDP Client for IoT Server
Description
This project is a simple Python script that acts as a UDP client. It connects to a server, receives base64 encoded UDP packets, decodes them, and prints the extracted information.
The client can be useful for testing and debugging UDP servers, especially those that communicate using base64 encoded packets.
Features include:
UDP connection Base64 decoding UDP packet decoding Header and payload extraction Installation Requirements Python 3.x
Steps
Make sure you have Python installed on your system. You can download Python from here. Clone this repository or download the Python script udp_client.py. Usage Open a terminal and navigate to the directory containing the script. Run the script with the command python udp_client.py.
The script connects to a server running on localhost at port 5612, receives a base64 encoded UDP packet from the server, decodes the packet, and prints the decoded packet and extracted fields (Source Port, Destination Port, Length, Checksum, Payload).
How to Run
-
Make sure you have Python installed on your system. You can download Python from here.
-
Clone this repository or download the Python script
udp_client.py
. -
Open a terminal and navigate to the directory containing the script.
-
Run the script with the command
python udp_client.py
.
What the Script Does
Once started, the script will:
-
Connect to the server running on localhost at port 5612.
-
Receive a base64 encoded UDP packet from the server.
-
Decode the received packet and print the decoded packet.
-
Extract the header fields (Source Port, Destination Port, Length, and Checksum) from the packet and print these fields.
-
Extract and decode the payload as a UTF-8 string and print the payload.
The script continues to receive, decode, and print packets until it receives an empty packet from the server, at which point it closes the socket and ends.
TASK 2
UDP Checksum Computation
This module provides a function to compute the checksum for a UDP packet. The UDP checksum is used to verify the integrity of the packet by performing a one's complement calculation on the packet data.
Description
The compute_checksum
function takes the source port, destination port, and payload of a UDP packet as input and computes the corresponding message's checksum. The function follows the UDP checksum algorithm to calculate the checksum value. The computed checksum can be used to verify the integrity of the UDP packet during transmission.
Function
The module includes the following function:
compute_checksum(source_port: int, dest_port: int, payload: bytearray) -> int This function takes the source port, destination port, and payload of a UDP packet as input and computes the corresponding message's checksum. The parameters are defined as follows: source_port: An integer representing the source port of the UDP packet. dest_port: An integer representing the destination port of the UDP packet. payload: A bytearray object containing the payload data of the UDP packet. The function returns the computed checksum as an integer.
How to Run
To run the script and compute the UDP checksum, follow these steps:
-
Make sure you have Python installed on your system. This script requires Python 3.
-
Download or clone the repository to your local machine.
-
Import the
udp_checksum
module in your Python script or interactive shell:from udp_checksum import compute_checksum
-
Call the
compute_checksum
function with the appropriate parameters:source_port = 2 dest_port = 1 payload = bytearray(b'AB') checksum = compute_checksum(source_port, dest_port, payload)
-
The
checksum
variable will contain the computed checksum value. -
You can use the computed checksum to verify the integrity of UDP packets in your networking application or further processing.
Example
Here's a complete example demonstrating the usage of the compute_checksum
function:
from udp_checksum import compute_checksum
source_port = 2
dest_port = 1
payload = bytearray(b'AB')
checksum = compute_checksum(source_port, dest_port, payload)
print(f"Checksum: {checksum}")
Output:
Checksum: 65489
.