Skip to content

Commit

Permalink
feat: support reverse address mapping
Browse files Browse the repository at this point in the history
  • Loading branch information
richardschneider committed Jun 9, 2019
1 parent d47666b commit 05722b8
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 1 deletion.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ service or service instance.
- 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
- Handles legacy unicast queries, see #61

## Getting started
Expand Down
10 changes: 9 additions & 1 deletion src/ServiceDiscovery.cs
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,10 @@ public void QueryUnicastServiceInstances(string service)
/// <remarks>
/// Any queries for the service or service instance will be answered with
/// information from the profile.
/// <para>
/// Besides adding the profile's resource records to the <see cref="Catalog"/> PTR records are
/// created to support DNS-SD and reverse address mapping (DNS address lookup).
/// </para>
/// </remarks>
public void Advertise(ServiceProfile service)
{
Expand All @@ -209,6 +213,8 @@ public void Advertise(ServiceProfile service)
{
catalog.Add(r, authoritative: true);
}

catalog.IncludeReverseLookupRecords();
}

/// <summary>
Expand Down Expand Up @@ -255,7 +261,9 @@ void OnAnswer(object sender, MessageEventArgs e)
}

// Any DNS-SD answers?
var sd = msg.Answers.OfType<PTRRecord>();
var sd = msg.Answers
.OfType<PTRRecord>()
.Where(ptr => ptr.Name.EndsWith(".local"));
foreach (var ptr in sd)
{
if (ptr.Name == ServiceName)
Expand Down
44 changes: 44 additions & 0 deletions test/ServiceDiscoveryTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -321,5 +321,49 @@ public void Unadvertise()
mdns.Stop();
}
}
[TestMethod]
public void ReverseAddressMapping()
{
var service = new ServiceProfile("x9", "_sdtest-1._udp", 1024, new[] { IPAddress.Loopback, IPAddress.IPv6Loopback });
var arpaAddress = IPAddress.Loopback.GetArpaName();
var done = new ManualResetEvent(false);

var mdns = new MulticastService();
Message response = null;
mdns.NetworkInterfaceDiscovered += (s, e) =>
mdns.SendQuery(arpaAddress, DnsClass.IN, DnsType.PTR);
mdns.AnswerReceived += (s, e) =>
{
var msg = e.Message;
if (msg.Answers.OfType<PTRRecord>().Any(p => p.Name == arpaAddress))
{
response = msg;
done.Set();
}
};
try
{
using (var sd = new ServiceDiscovery(mdns))
{
sd.Advertise(service);
mdns.Start();
Assert.IsTrue(done.WaitOne(TimeSpan.FromSeconds(1)), "query timeout");
var answers = response.Answers
.OfType<PTRRecord>()
.Where(ptr => service.HostName == ptr.DomainName);
foreach (var answer in answers)
{
Assert.AreEqual(arpaAddress, answer.Name);
Assert.IsTrue(answer.TTL > TimeSpan.Zero);
Assert.AreEqual(DnsClass.IN, answer.Class);
}
}
}
finally
{
mdns.Stop();
}
}

}
}

0 comments on commit 05722b8

Please sign in to comment.