net-mdns

repository·master·Indexed 42 days ago

https://github.com/richardschneider/net-mdns

A C# implementation of Multicast DNS (RFC 6762) and DNS Service Discovery (RFC 6763), published as the Makaretu.Dns.Multicast NuGet package. It enables developers to discover services on a local network or advertise their own services using the ServiceDiscovery and MulticastService classes. The library supports IPv4, IPv6, and targets .NET Standard 1.4, 2.0, and Framework 4.6.1.

Tokens
4.4K
Snippets
18
Records
21
Agent score
54%

What's inside net-mdns

  1. Use MulticastService for multicast DNS messaging

    master

    The MulticastService class implements RFC 6762 (Multicast DNS). It allows you to send and receive standard DNS messages via multicast, effectively broadcasting messages to all interested parties on link-local networks.

    To use it, instantiate MulticastService, subscribe to the appropriate received events, and call Start() to begin listening for network traffic.

    using Makaretu.Dns;
    
    var mdns = new MulticastService();
    mdns.Start();
  2. Configure ServiceProfile properties and subtypes

    master

    A ServiceProfile defines the identity and characteristics of the service being advertised.

    • Service Name: The first argument in the constructor (e.g., "me").
    • Service Type: The second argument, following the DNS-SD format (e.g., "_myservice._udp").
    • Port: The integer port the service is listening on.
    • IP Addresses: An array of IPAddress objects representing the host(s) where the service resides.
    • Subtypes: Use profile.Subtypes.Add("subtype_name") to allow discovery via specific subtypes. This generates PTR records for the subtype.
    • Properties: Use profile.AddProperty("key", "value") to add metadata. These are encoded into the TXT resource records.
    var profile = new ServiceProfile("me", "_myservice._udp", 1234, 
      new IPAddress[] { IPAddress.Loopback });
    profile.Subtypes.Add("apiv2");
    profile.AddProperty("someprop", "somevalue");
  3. How Subtypes work for selective service discovery

    master

    Subtypes allow service instances to declare specific features they implement, enabling clients to filter for services that support a particular capability. This follows the RFC 6763 standard for Selective Instance Enumeration.

    In net-mdns, you use subtypes to both advertise capabilities (via ServiceProfile.Subtypes) and query for specific capabilities (via ServiceDiscovery.QueryServiceInstances).

  4. Perform Service Discovery and Advertising

    master

    To implement service discovery (finding services) and advertising (making a service visible), you use a combination of ServiceDiscovery and ServiceProfile:

    1. Advertising a service: Create a ServiceProfile containing your service details and then use the Advertise method to make it available on the network.
    2. Discovering services: Use ServiceDiscovery to find existing services and service instances on the network.
    3. Responding to queries: Once a service is advertised, any incoming queries for that specific service or instance will be automatically answered using the information provided in the ServiceProfile.
  5. Use MulticastService for DNS queries and answers

    master

    The MulticastService class is the core component for interacting with the link-local network.

    Key capabilities:

    • Sending: Use SendQuery to send DNS queries and SendAnswer to send DNS answers.
    • Listening: It listens for incoming Message objects.
    • Events: It provides QueryReceived and AnswerReceived events to handle incoming network traffic.
  6. Install Makaretu.Dns.Multicast via NuGet or .NET CLI

    master

    You can install the Makaretu.Dns.Multicast package using either the NuGet Package Manager Console or the .NET CLI. This library targets Framework 4.6.1, .NET Standard 1.4, and .NET Standard 2.0, and supports both IPv4 and IPv6.

    # Using Package Manager Console
    Install-Package Makaretu.Dns.Multicast
    # Using .NET CLI
    dotnet add package Makaretu.Dns.Multicast
  7. Find service instances using QueryServiceInstances

    master

    To find all specific instances of a particular service type (e.g., _myservice), use the QueryServiceInstances(string serviceName) method. Each discovered instance triggers the ServiceInstanceDiscovered event, which provides a ServiceInstanceDiscoveredEventArgs object containing the ServiceInstanceName.

    using Makaretu.Dns;
    
    var sd = new ServiceDiscovery();
    sd.ServiceInstanceDiscovered += (s, e) =>
    {
        Console.WriteLine($"service instance '{e.ServiceInstanceName}'");
    };
    sd.QueryServiceInstances("_myservice");
  8. Find all available services using ServiceDiscovery

    master

    To discover all available service types on the link-local network, use the QueryAllServices() method on a ServiceDiscovery instance. The discovery process is asynchronous and notifies you of each found service via the ServiceDiscovered event, which provides the service name.

    using Makaretu.Dns;
    
    var sd = new ServiceDiscovery();
    sd.ServiceDiscovered += (s, serviceName) =>
    {
        Console.WriteLine($"service '{serviceName}'");
    };
    sd.QueryAllServices();
  9. Find service instances with specific subtypes

    master

    To find all instances of a service that implement a specific feature, use the QueryServiceInstances method on a ServiceDiscovery instance. You can listen for discoveries by subscribing to the ServiceInstanceDiscovered event, which provides the ServiceInstanceName of the discovered service.

    using Makaretu.Dns;
    
    var sd = new ServiceDiscovery();
    sd.ServiceInstanceDiscovered += (s, e) =>
    {
        Console.WriteLine($"service instance '{e.ServiceInstanceName}'");
    };
    
    // Query for instances of '_myservice' that support the 'apiv2' subtype
    sd.QueryServiceInstances("_myservice", "apiv2");
  10. Advertise a service using ServiceDiscovery.Advertise

    master

    To make a service discoverable on the network via Multicast DNS or DNS-SD, use ServiceDiscovery.Advertise. This method takes a ServiceProfile and generates the necessary DNS resource records (PTR, SRV, TXT, A, etc.) required to respond to queries for that service.

    Steps to advertise:

    1. Create a ServiceProfile specifying the service name, service type (e.g., _myservice._udp), port, and a list of IP addresses.
    2. (Optional) Add subtypes to the Subtypes collection.
    3. (Optional) Add properties using AddProperty to populate TXT records.
    4. Instantiate ServiceDiscovery and call Advertise(profile).
    var profile = new ServiceProfile("me", "_myservice._udp", 1234, 
      new IPAddress[] { IPAddress.Loopback });
    profile.Subtypes.Add("apiv2");
    profile.AddProperty("someprop", "somevalue");
    
    var sd = new ServiceDiscovery();
    sd.Advertise(profile);
  11. Install net-mdns via NuGet

    master

    The net-mdns library (published as Makaretu.Dns.Multicast) can be installed via the NuGet package manager. It provides a simple implementation of Multicast Domain Name Service based on RFC 6762, allowing your application to act as both a client (sending queries) and a server (responding to queries).

    # Install the package via NuGet CLI
    dotnet add package Makaretu.Dns.Multicast
  12. Advertise a service with subtypes

    master

    To make a service discoverable with specific features, create a ServiceProfile, add your feature strings to its Subtypes collection, and then pass the profile to ServiceDiscovery.Advertise. Once advertised, any queries for that service or specific subtypes will be answered using the information in the profile.

    using Makaretu.Dns;
    
    // Create a profile for service 'x' on '_myservice._udp' at port 1024
    var profile = new ServiceProfile("x", "_myservice._udp", 1024);
    
    // Add the feature/subtype
    profile.Subtypes.Add("apiv2");
    
    var sd = new ServiceDiscovery();
    // Advertise the profile so it can be discovered
    sd.Advertise(profile);