# spiceorg/usbredir architecture

> Architecture diagram of the GitHub repository spiceorg/usbredir, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-06-04). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/spiceorg/usbredir
- Repository: https://github.com/spiceorg/usbredir

## Overview

##### Project type and purpose
- usbredir is an open‐source toolkit implementing a USB redirection protocol
- Provides:
– usbredirparser: a standalone C library to parse/serialize the usbredir protocol
– usbredirhost: a C library that bridges libusb (real USB) with usbredir frames
– usbredirserver: a TCP‐based server using usbredirhost
– usbredirtestclient: a small client that uses usbredirparser to talk over TCP

##### Main components of the system
A. External USB device
B. libusb (native USB I/O library)
C. usbredirhost library
– Reads/writes USB via libusb
– Delegates framing to usbredirparser
– Exposes transport read/write callbacks
D. usbredirparser library
– Core encoder/decoder for usb‐redirection‐protocol.txt
– Shared by host library and test client
E. usbredirserver executable
– Implements a TCP listener
– Hooks its socket I/O into usbredirhost’s transport callbacks
F. usbredirtestclient executable
– Opens a TCP socket to usbredirserver
– Uses usbredirparser to build/parse frames

##### Relationships and data flow
USB Device → libusb API → usbredirhost
usbredirhost ↔ usbredirparser (encode/decode)
usbredirhost.transport callbacks ↔ TCP network
TCP network ↔ { usbredirserver, usbredirtestclient }

##### Architectural patterns & design principles
- Layered separation:
1. Hardware abstraction (libusb)
2. Host logic (usbredirhost)
3. Protocol logic (usbredirparser)
4. Applications (server & test client)
- Single responsibility: parser library only handles framing; host library only handles USB/transport glue
- Reusability: parser is a common core for all clients/servers

##### Additional details to capture
- Under parser, note “usbredirparser/*.c, *.h”
- Under server and client, list their source directories
- File usb-redirection-protocol.txt as the protocol definition

## Diagram (Mermaid)

```mermaid
flowchart TB
    %% External Hardware Layer
    USB(("External USB Device")):::external
    LIBUSB["libusb (USB I/O library)"]:::external

    %% Host Logic Layer
    HOST["usbredirhost\nusbredirhost/*.c, usbredirhost.h"]:::host

    %% Protocol Logic Layer
    PARSER["usbredirparser\nusbredirparser/*.c, *.h"]:::parser
    PROT["usb-redirection-protocol.txt"]:::parser

    %% Transport Layer
    NET(("TCP Network")):::network

    %% Application Layer
    SERVER["usbredirserver\nusbredirserver/*.c"]:::app
    CLIENT["usbredirtestclient\nusbredirtestclient/*.c"]:::app

    %% Data Flow
    USB -->|"USB traffic"| LIBUSB
    LIBUSB -->|"native USB I/O"| HOST
    HOST -->|"encode/decode"| PARSER
    PARSER -->|"framing"| HOST
    HOST -->|"transport callbacks"| NET
    NET -->|"TCP I/O"| SERVER
    NET -->|"TCP I/O"| CLIENT
    SERVER -->|"uses host callbacks"| HOST
    CLIENT -->|"uses parser"| PARSER

    %% Click Events
    click HOST "https://github.com/spiceorg/usbredir/tree/master/usbredirhost/"
    click PARSER "https://github.com/spiceorg/usbredir/tree/master/usbredirparser/"
    click SERVER "https://github.com/spiceorg/usbredir/tree/master/usbredirserver/"
    click CLIENT "https://github.com/spiceorg/usbredir/tree/master/usbredirtestclient/"
    click PROT "https://github.com/spiceorg/usbredir/blob/master/usb-redirection-protocol.txt"

    %% Style Definitions
    classDef external fill:#FFE4B5,stroke:#333,stroke-width:1px
    classDef host fill:#D0F0C0,stroke:#333,stroke-width:1px
    classDef parser fill:#98FB98,stroke:#333,stroke-width:1px
    classDef network fill:#D3D3D3,stroke:#333,stroke-width:1px
    classDef app fill:#ADD8E6,stroke:#333,stroke-width:1px
```
