Skip to content

wiperi/urp_v3

Repository files navigation

URP v3 - UDP-based Reliable Protocol

A Python implementation of a reliable transport protocol built on top of UDP, featuring TCP-like reliability mechanisms including sliding window, timeout/fast retransmission, and error detection.

Overview

URP (UDP-based Reliable Protocol) provides reliable, in-order data delivery over the unreliable UDP protocol. This implementation includes:

  • Connection Management: 2-way handshake (SYN/ACK) for setup, 1-way close (FIN/ACK) for teardown
  • Reliable Transfer: Sliding window protocol with pipelining (configurable window size)
  • Error Recovery: Timeout-based retransmission and fast retransmit (3 duplicate ACKs)
  • Error Detection: 16-bit checksum (TCP/UDP-style one's complement)
  • Network Simulation: Packet Loss and Corruption (PLC) module for testing
  • Comprehensive Logging: Real-time event logging with detailed statistics

Architecture

Sender                                  Receiver
┌─────────────────┐                    ┌─────────────────┐
│  File Reader    │                    │  File Writer    │
├─────────────────┤                    ├─────────────────┤
│ Sliding Window  │                    │ Receive Buffer  │
│ Send Buffer     │                    │ Out-of-order    │
│ Timer Manager   │                    │ Segment Handler │
├─────────────────┤                    ├─────────────────┤
│ PLC Module      │                    │                 │
│ (Loss/Corrupt)  │                    │                 │
├─────────────────┤                    ├─────────────────┤
│  UDP Socket     │ ←──────────────→  │  UDP Socket     │
└─────────────────┘                    └─────────────────┘

Project Structure

urp_v3/
├── src/urp_v3/
│   ├── sender.py       # Sender implementation with sliding window
│   ├── receiver.py     # Receiver implementation with buffering
│   ├── segment.py      # URP segment format (6-byte header)
│   ├── seqnum.py       # 16-bit sequence number with wraparound
│   ├── plc.py          # Packet Loss/Corruption simulator
│   ├── logger.py       # Event logging and statistics
│   └── constant.py     # Constants and state definitions
├── tests/              # Unit and integration tests
├── INTRO.md            # Detailed design documentation (1400+ lines)
└── README.md           # This file

Quick Start

1. Setup Environment

# Create and activate virtual environment
python -m venv .venv

# Windows (bash/Git Bash)
source .venv/Scripts/activate

# Windows (CMD)
.venv\Scripts\activate.bat

# Windows (PowerShell)
.venv\Scripts\Activate.ps1

# Linux/macOS
source .venv/bin/activate

# Install dependencies
pip install -r requirements.txt

# Install package in development mode
pip install -e .

2. Run Tests

# Run all tests
pytest -v

# Run specific test
pytest tests/test_integration_subprocess.py -v

3. Basic Usage

Terminal 1 - Start Receiver:

python -m urp_v3.receiver 56007 59606 output.txt 4000
# Args: receiver_port sender_port output_file max_win

Terminal 2 - Start Sender:

python -m urp_v3.sender 59606 56007 input.txt 4000 100 0.1 0.05 0.1 0.05
# Args: sender_port receiver_port input_file max_win rto flp rlp fcp rcp

Parameters:

  • max_win: Maximum window size in bytes (e.g., 4000)
  • rto: Retransmission timeout in milliseconds (e.g., 100)
  • flp: Forward loss probability 0-1 (e.g., 0.1 = 10%)
  • rlp: Reverse loss probability 0-1
  • fcp: Forward corruption probability 0-1
  • rcp: Reverse corruption probability 0-1

Features in Detail

Segment Format

6-byte header structure:

 0                   1                   2                   3
 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|      Sequence Number (16)     |Reserved(13)|A|S|F|             |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|      Checksum (16)            |      Payload (0-1000 bytes)   |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  • Sequence Number: 16-bit with modular arithmetic
  • Flags: ACK, SYN, FIN (only one can be set at a time)
  • Checksum: 16-bit one's complement (TCP/UDP-style)
  • MSS: 1000 bytes maximum payload

State Machines

Sender States:

CLOSED → SYN_SENT → ESTABLISHED → CLOSING → FIN_WAIT → CLOSED

Receiver States:

CLOSED → LISTEN → ESTABLISHED → TIME_WAIT → CLOSED

Reliability Mechanisms

  1. Sliding Window: Sender maintains window of unACKed segments
  2. Cumulative ACKs: Receiver ACKs next expected byte number
  3. Timeout Retransmission: Single timer for oldest unACKed segment
  4. Fast Retransmit: Triggered by 3 duplicate ACKs
  5. Out-of-order Buffering: Receiver buffers future segments
  6. TIME_WAIT: 2-second wait state for delayed packets

Logging

Both sender and receiver generate detailed logs:

sender_log.txt example:

snd  ok      0.00 SYN      1234     0
rcv  ok    102.34 ACK      1235     0
snd  ok    103.45 DATA     1235  1000
rcv  ok    105.67 ACK      2235     0
...
Original data sent:               3500
Total data sent:                  5500
Timeout retransmissions:             5
Fast retransmissions:                0

receiver_log.txt example:

rcv  ok      0.00 SYN      1234     0
snd  ok      0.12 ACK      1235     0
rcv  ok    103.50 DATA     1235  1000
snd  ok    103.55 ACK      2235     0
...
Original data received:           3500
Total data received:              5500
Duplicate segments received:         2

Testing

The project includes comprehensive tests:

  • Unit tests: Checksum, sequence number arithmetic, segment encoding
  • Integration tests: Full sender-receiver communication with various parameters
  • Stress tests: High packet loss/corruption scenarios
# Run all tests
pytest -v

# Run with coverage
pytest --cov=urp_v3 --cov-report=html

Development

Key Classes

  • Sender: Main sender class with state machine and window management
  • Receiver: Main receiver class with buffering and ACK generation
  • Segment: Segment encoding/decoding and checksum calculation
  • SequenceNumber: 16-bit sequence number with wraparound support
  • PLC: Packet loss and corruption simulator
  • Logger: Event logging and statistics tracking

Design Patterns

  • State Machine: Both sender and receiver use explicit state machines
  • Observer: Logger observes and records all protocol events
  • Strategy: PLC module applies configurable loss/corruption strategies

Documentation

  • INTRO.md - Comprehensive design document with:
    • Protocol specification details
    • Architecture diagrams
    • Implementation strategy
    • Testing plan
    • Development phases

License

MIT License

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages