Skip to content

Phase 17: Enhanced mDNS discovery with peer caching and statistics#53

Merged
infinityabundance merged 4 commits intomainfrom
copilot/implement-mdns-discovery
Feb 13, 2026
Merged

Phase 17: Enhanced mDNS discovery with peer caching and statistics#53
infinityabundance merged 4 commits intomainfrom
copilot/implement-mdns-discovery

Conversation

Copy link
Contributor

Copilot AI commented Feb 13, 2026

Summary

Extends basic mDNS/Avahi discovery with peer caching (64 entries, TTL-based expiry), richer service metadata (capability, bandwidth, max_peers), and statistics tracking. Minimal changes to existing code - enhances rather than rewrites.

Details

  • Bug fix
  • New feature
  • Performance improvement
  • Documentation / tooling

What changed?

Peer Cache System (include/rootstream.h, src/discovery.c):

  • peer_cache_entry_t structure with TTL, online/offline tracking, contact/failure counters
  • Cache operations: discovery_cache_add_peer(), discovery_cache_get_peer(), discovery_cache_get_online(), etc.
  • Auto-expiry marks peers offline at 50% TTL, removes at 100% TTL
  • Capacity: 64 peers, default 1-hour TTL

Enhanced mDNS Advertiser (src/discovery.c):

  • TXT records now include: capability (host/client), max_peers, bandwidth
  • Resolver extracts all metadata into cache
  • Statistics: total discoveries/losses, breakdown by method (mDNS/broadcast/manual)

Broadcast Protocol (src/discovery_broadcast.c):

  • Extended packet format with capability, max_peers, bandwidth fields
  • Integrated with cache system

Manual Discovery (src/discovery_manual.c):

  • Statistics tracking for manual peer additions

Testing (tests/unit/test_discovery_cache.c):

  • 9 unit tests: add, update, get, remove, expiry, filtering, statistics
  • Stub functions to avoid dependency bloat
  • All tests passing

Rationale

Zero-configuration networking requires peers to discover capabilities without manual configuration. Basic mDNS provides hostname/port; this adds:

  • Capability filtering: clients find hosts, hosts find other hosts for mesh topologies
  • Resource awareness: bandwidth/peer capacity for intelligent connection decisions
  • Resilience: TTL-based cache survives transient network issues
  • Observability: statistics for debugging discovery problems

Aligns with RootStream's simplicity goal through automatic configuration and minimal operator intervention.

Testing

  • Built successfully (make)
  • Unit tests pass (9/9)
  • CodeQL security scan (0 alerts)
  • Code review feedback addressed
  • Basic streaming tested
  • Tested on:
    • Distro: Ubuntu 24.04 (CI)
    • Kernel: 6.x
    • GPU & driver: N/A (discovery only)

Notes

  • Latency impact: Negligible. Cache lookups are O(n) linear scan (n ≤ 64), expiry runs periodically not per-packet
  • Memory: ~80KB (64 peers × ~1.2KB/entry)
  • Follow-up: UI integration (discovery_dialog.cpp) deferred to keep PR focused
  • Backwards compatible: Existing code unchanged, cache supplements active peer list
Original prompt

PHASE 17: mDNS Discovery - Automatic Peer Detection on LAN

🎯 Objective

Implement comprehensive multicast DNS (mDNS/Bonjour) discovery system that:

  1. Automatically advertises RootStream hosts on the local network (mDNS registration)
  2. Discovers other RootStream peers without manual configuration
  3. Maintains dynamic peer list with real-time updates
  4. Provides fallback discovery methods (UDP broadcast, manual entry)
  5. Caches peer information with TTL management
  6. Supports service browsing with filtering and sorting
  7. Integrates with system DNS for hybrid name resolution

This is critical for seamless LAN setup - users should discover peers immediately without entering codes or IP addresses.


📋 Architecture Overview

┌────────────────────────────────────────────────────────────┐
│            RootStream Host (Advertiser)                    │
│  ┌──────────────────────────────────────────────────────┐  │
│  │  Service Registration:                               │  │
│  │  - Name: "Gaming-PC._rootstream._udp.local"         │  │
│  │  - Type: _rootstream._udp                           │  │
│  │  - Port: 9876                                       │  │
│  │  - TXT Records:                                     │  │
│  │    * version=1.0                                    │  │
│  │    * code=<rootstream-code>                        │  │
│  │    * capability=host|client                        │  │
│  │    * max_peers=10                                  │  │
│  │    * bandwidth=100Mbps                             │  │
│  └──────────────────────────────────────────────────────┘  │
└────────────────┬─────────────────────────────────────────┘
                 │ mDNS Multicast (224.0.0.251:5353)
                 │ Every 120s (with conflict detection)
                 │
      ┌──────────┴──────────┐
      │                     │
      ▼                     ▼
┌──────────────┐    ┌──────────────┐
│  Client 1    │    │  Client 2    │
│              │    │              │
│  Browser:    │    │  Browser:    │
│  "Discover   │    │  "Discover   │
│   RootStream │    │   RootStream │
│   services"  │    │   services"  │
└──────┬───────┘    └──────┬───────┘
       │                   │
       └───────┬───────────┘
               │
               ▼
    ┌──────────────────────┐
    │  Peer Cache (TTL)    │
    │  - Host info         │
    │  - Timestamp         │
    │  - Status (online)   │
    └──────────────────────┘
               │
               ▼
    ┌──────────────────────┐
    │  Fallback Discovery  │
    │  - UDP Broadcast     │
    │  - Manual Entry      │
    │  - System DNS        │
    └──────────────────────┘

🔨 Implementation Plan

1. mDNS Service Registration

File: src/discovery/mdns_advertiser.h/cpp

class MDNSAdvertiser {
private:
    AvahiClient *client;
    AvahiEntryGroup *group;
    AvahiSimplePoll *poll;
    
    struct {
        char service_name[256];      // "Gaming-PC"
        uint16_t port;               // 9876
        char rootstream_code[128];   // Base64 public key
        char version[16];            // "1.0"
        char capability[32];         // "host" or "client"
        uint32_t max_peers;
        char bandwidth[32];          // "100Mbps"
        bool is_online;
    } service_info;
    
public:
    int init(const char *service_name, uint16_t port);
    
    // Register service
    int register_service(const char *code, const char *capability);
    
    // Update TXT records (capability, bandwidth, etc)
    int update_txt_record(const char *key, const char *value);
    
    // Online/offline status
    int set_online(bool online);
    
    // Query service status
    bool is_registered();
    const char *get_service_name();
    
    void cleanup();
};

Avahi Integration:

  • Use libavahi-client (Ubuntu: libavahi-client-dev)
  • Simple Poll API for easy integration
  • Entry Group for service registration
  • Collision detection and auto-rename on conflicts

2. mDNS Service Browser

File: src/discovery/mdns_browser.h/cpp

class MDNSBrowser : public QObject {
    Q_OBJECT
    
private:
    AvahiClient *client;
    AvahiServiceBrowser *browser;
    AvahiSimplePoll *poll;
    
    struct DiscoveredService {
        char name[256];              // Service instance name
        char hostname[256];          // Fully qualified hostname
        uint16_t port;
        AvahiAddress address;        // IPv4 or IPv6
        AvahiStringList *txt_list;   // TXT records
        uint32_t ttl;
        bool is_host;                // Host vs client
        char rootstream_code[128];
        uint64_t last_seen_time_us;
    };
    
    QMap<QString, DiscoveredService> discovered_peers;
    QMutex peer_lock;
    
public:
    int init();
    
    // Start browsing for RootStream services
    int start_browsing();
    int stop_browsing();
    
    // Query discovered peers
    QList<PeerInfo> get_discovered_peers();
    PeerInfo g...

</details>



<!-- START COPILOT CODING AGENT SUFFIX -->

*This pull request was created from Copilot chat.*
>

<!-- START COPILOT CODING AGENT TIPS -->
---

✨ Let Copilot coding agent [set things up for you](https://github.com/infinityabundance/RootStream/issues/new?title=✨+Set+up+Copilot+instructions&body=Configure%20instructions%20for%20this%20repository%20as%20documented%20in%20%5BBest%20practices%20for%20Copilot%20coding%20agent%20in%20your%20repository%5D%28https://gh.io/copilot-coding-agent-tips%29%2E%0A%0A%3COnboard%20this%20repo%3E&assignees=copilot) — coding agent works faster and does higher quality work when set up for your repo.

Copilot AI and others added 3 commits February 13, 2026 07:04
…istics

Co-authored-by: infinityabundance <255699974+infinityabundance@users.noreply.github.com>
Co-authored-by: infinityabundance <255699974+infinityabundance@users.noreply.github.com>
…iming dependency in tests

Co-authored-by: infinityabundance <255699974+infinityabundance@users.noreply.github.com>
Copilot AI changed the title [WIP] Implement mDNS discovery for automatic peer detection Phase 17: Enhanced mDNS discovery with peer caching and statistics Feb 13, 2026
@infinityabundance infinityabundance marked this pull request as ready for review February 13, 2026 07:34
@chatgpt-codex-connector
Copy link

You have reached your Codex usage limits for code reviews. You can see your limits in the Codex usage dashboard.
To continue using code reviews, you can upgrade your account or add credits to your account and enable them for code reviews in your settings.

@infinityabundance infinityabundance merged commit 0a02092 into main Feb 13, 2026
4 of 9 checks passed
@infinityabundance infinityabundance deleted the copilot/implement-mdns-discovery branch February 19, 2026 20:58
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants