项目作者: richardschneider

项目描述 :
Simple multicast DNS
高级语言: C#
项目地址: git://github.com/richardschneider/net-mdns.git
创建时间: 2018-02-25T00:43:28Z
项目社区:https://github.com/richardschneider/net-mdns

开源协议:MIT License

下载


net-mdns

build status
travis build
CircleCI
Coverage Status
Version
docs

A simple Multicast Domain Name Service based on RFC 6762. Can be used
as both a client (sending queries) or a server (responding to queries).

A higher level DNS Service Discovery based on RFC 6763 that automatically responds to any query for the
service or service instance.

Features

  • Targets Framework 4.6.1, .NET Standard 1.4 and 2.0
  • Supports IPv6 and IPv4 platforms
  • CI on Circle (Debian GNU/Linux), Travis (Ubuntu Xenial and OSX) and AppVeyor (Windows Server 2016)
  • Detects new and/or removed network interfaces
  • Supports multicasting on multiple network interfaces
  • Supports reverse address mapping
  • Supports service subtypes (features)
  • Handles legacy unicast queries, see #61

Getting started

Published releases are available on NuGet. To install, run the following command in the Package Manager Console

  1. PM> Install-Package Makaretu.Dns.Multicast

or using .NET CLI run the following command in the project folder

  1. > dotnet add package Makaretu.Dns.Multicast

Usage Service Discovery

Advertising

Always broadcast the service (“foo”) running on local host with port 1024.

  1. using Makaretu.Dns;
  2. var service = new ServiceProfile("x", "_foo._tcp", 1024);
  3. var sd = new ServiceDiscovery();
  4. sd.Advertise(service);

See the example advertiser for a working program.

Discovery

Find all services running on the local link.

  1. using Makaretu.Dns;
  2. var sd = new ServiceDiscovery();
  3. sd.ServiceDiscovered += (s, serviceName) => { // Do something };

Find all service instances running on the local link.

  1. using Makaretu.Dns;
  2. var sd = new ServiceDiscovery();
  3. sd.ServiceInstanceDiscovered += (s, e) => { // Do something };

See the example browser for a working program.

Usage Multicast

Event Based Queries

Get all the Apple TVs. The query is sent when a network interface is discovered.
The AnsweredReceived callback contains any answer that is seen, not just the answer
to the specific query.

  1. using Makaretu.Dns;
  2. var mdns = new MulticastService();
  3. mdns.NetworkInterfaceDiscovered += (s, e) => mdns.SendQuery("appletv.local");
  4. mdns.AnswerReceived += (s, e) => { // do something with e.Message };
  5. mdns.Start();

Async Queries

Get the first answer to Apple TVs. Wait 2 seconds for an answer.

  1. using Makaretu.Dns;
  2. var service = "appletv.local";
  3. var query = new Message();
  4. query.Questions.Add(new Question { Name = service, Type = DnsType.ANY });
  5. var cancellation = new CancellationTokenSource(2000);
  6. using (var mdns = new MulticastService())
  7. {
  8. mdns.Start();
  9. var response = await mdns.ResolveAsync(query, cancellation.Token);
  10. // Do something
  11. }

Broadcasting

Respond to a query for the service. Note that ServiceDiscovery.Advertise is much easier.

  1. using Makaretu.Dns;
  2. var service = "...";
  3. var mdns = new MulticastService();
  4. mdns.QueryReceived += (s, e) =>
  5. {
  6. var msg = e.Message;
  7. if (msg.Questions.Any(q => q.Name == service))
  8. {
  9. var res = msg.CreateResponse();
  10. var addresses = MulticastService.GetIPAddresses()
  11. .Where(ip => ip.AddressFamily == AddressFamily.InterNetwork);
  12. foreach (var address in addresses)
  13. {
  14. res.Answers.Add(new ARecord
  15. {
  16. Name = service,
  17. Address = address
  18. });
  19. }
  20. mdns.SendAnswer(res);
  21. }
  22. };
  23. mdns.Start();
  • net-dns - DNS data model and Name Server with serializer for the wire and master file format
  • net-udns - client for unicast DNS, DNS over HTTPS (DOH) and DNS over TLS (DOT)

License

Copyright © 2018-2019 Richard Schneider (makaretu@gmail.com)

The package is licensed under the MIT license. Refer to the LICENSE file for more information.

Buy Me A Coffee