Skip to content
Snippets Groups Projects
Select Git revision
  • main
1 result

aira

Name Last commit Last update
README.md
task1.py
task2.py

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

  1. Make sure you have Python installed on your system. You can download Python from here.

  2. Clone this repository or download the Python script udp_client.py.

  3. Open a terminal and navigate to the directory containing the script.

  4. Run the script with the command python udp_client.py.

What the Script Does

Once started, the script will:

  1. Connect to the server running on localhost at port 5612.

  2. Receive a base64 encoded UDP packet from the server.

  3. Decode the received packet and print the decoded packet.

  4. Extract the header fields (Source Port, Destination Port, Length, and Checksum) from the packet and print these fields.

  5. 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:

  1. Make sure you have Python installed on your system. This script requires Python 3.

  2. Download or clone the repository to your local machine.

  3. Import the udp_checksum module in your Python script or interactive shell:

    from udp_checksum import compute_checksum
  4. 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)
  5. The checksum variable will contain the computed checksum value.

  6. 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

.