Skip to content

Commit

Permalink
docs: respond to a query
Browse files Browse the repository at this point in the history
  • Loading branch information
richardschneider committed Jun 29, 2018
1 parent e2faa60 commit 2d118f1
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 6 deletions.
33 changes: 33 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ Published releases are available on [NuGet](https://www.nuget.org/packages/Makar

## Usage

### Discovery

Get all the Apple TVs. The query is sent when a network interface is discovered.

```csharp
Expand All @@ -35,6 +37,37 @@ mdns.AnswerReceived += (s, e) => { // do something with e.Message };
mdns.Start();
```

### Broadcasting

Respond to a query for the service.

```csharp
using Makaretu.Dns;

var service = "...";
var mdns = new MulticastService();
mdns.QueryReceived += (s, e) =>
{
var msg = e.Message;
if (msg.Questions.Any(q => q.Name == service))
{
var res = msg.CreateResponse();
var addresses = MulticastService.GetIPAddresses()
.Where(ip => ip.AddressFamily == AddressFamily.InterNetwork);
foreach (var address in addresses)
{
res.Answers.Add(new ARecord
{
Name = service,
Address = address
});
}
mdns.SendAnswer(res);
}
};
mdns.Start();
```

# License
Copyright © 2018 Richard Schneider (makaretu@gmail.com)

Expand Down
17 changes: 11 additions & 6 deletions test/MulticastServiceTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using System.Linq;
using System.Net;
using System.Net.NetworkInformation;
using System.Net.Sockets;
using System.Threading;
using System.Threading.Tasks;

Expand Down Expand Up @@ -256,11 +257,16 @@ public void Multiple_Services()
if (msg.Questions.Any(q => q.Name == service))
{
var res = msg.CreateResponse();
res.Answers.Add(new ARecord
var addresses = MulticastService.GetIPAddresses()
.Where(ip => ip.AddressFamily == AddressFamily.InterNetwork);
foreach (var address in addresses)
{
Name = service,
Address = IPAddress.Parse("127.1.1.1")
});
res.Answers.Add(new ARecord
{
Name = service,
Address = address
});
}
a.SendAnswer(res);
}
};
Expand All @@ -285,8 +291,7 @@ public void Multiple_Services()
Assert.IsTrue(response.IsResponse);
Assert.AreEqual(MessageStatus.NoError, response.Status);
Assert.IsTrue(response.AA);
var answer = (ARecord)response.Answers[0];
Assert.AreEqual(IPAddress.Parse("127.1.1.1"), answer.Address);
Assert.AreNotEqual(0, response.Answers.Count);
}
finally
{
Expand Down

0 comments on commit 2d118f1

Please sign in to comment.