Skip to content

Commit

Permalink
Merge pull request #72 from richardschneider/traffic
Browse files Browse the repository at this point in the history
Multicast traffic logger
  • Loading branch information
richardschneider authored Jul 15, 2019
2 parents e53c5d9 + d479d16 commit a5b730b
Show file tree
Hide file tree
Showing 5 changed files with 97 additions and 6 deletions.
6 changes: 6 additions & 0 deletions Mdns.sln
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Spike", "Spike\Spike.csproj
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Browser", "Browser\Browser.csproj", "{A77BEF8C-440E-46F7-ACFC-5EF06EFCA4BA}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "traffic", "traffic\traffic.csproj", "{5E51D502-05E1-4A45-A8D6-6FB3E295D798}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand All @@ -48,6 +50,10 @@ Global
{A77BEF8C-440E-46F7-ACFC-5EF06EFCA4BA}.Debug|Any CPU.Build.0 = Debug|Any CPU
{A77BEF8C-440E-46F7-ACFC-5EF06EFCA4BA}.Release|Any CPU.ActiveCfg = Release|Any CPU
{A77BEF8C-440E-46F7-ACFC-5EF06EFCA4BA}.Release|Any CPU.Build.0 = Release|Any CPU
{5E51D502-05E1-4A45-A8D6-6FB3E295D798}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{5E51D502-05E1-4A45-A8D6-6FB3E295D798}.Debug|Any CPU.Build.0 = Debug|Any CPU
{5E51D502-05E1-4A45-A8D6-6FB3E295D798}.Release|Any CPU.ActiveCfg = Release|Any CPU
{5E51D502-05E1-4A45-A8D6-6FB3E295D798}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down
15 changes: 14 additions & 1 deletion src/MulticastService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,14 @@ static MulticastService()
/// </remarks>
public event EventHandler<MessageEventArgs> AnswerReceived;

/// <summary>
/// Raised when a DNS message is received that cannot be decoded.
/// </summary>
/// <value>
/// The DNS message as a byte array.
/// </value>
public event EventHandler<byte[]> MalformedMessage;

/// <summary>
/// Raised when one or more network interfaces are discovered.
/// </summary>
Expand Down Expand Up @@ -603,8 +611,12 @@ void Send(Message msg, bool checkDuplicate, IPEndPoint remoteEndPoint = null)
/// Multicast DNS messages received with an OPCODE or RCODE other than zero
/// are silently ignored.
/// </para>
/// <para>
/// If the message cannot be decoded, then the <see cref="MalformedMessage"/>
/// event is raised.
/// </para>
/// </remarks>
void OnDnsMessage(object sender, UdpReceiveResult result)
public void OnDnsMessage(object sender, UdpReceiveResult result)
{
// If recently received, then ignore.
if (!receivedMessages.TryAdd(result.Buffer))
Expand All @@ -620,6 +632,7 @@ void OnDnsMessage(object sender, UdpReceiveResult result)
catch (Exception e)
{
log.Warn("Received malformed message", e);
MalformedMessage?.Invoke(this, result.Buffer);
return; // eat the exception
}

Expand Down
27 changes: 22 additions & 5 deletions test/MulticastServiceTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -529,10 +529,10 @@ public async Task DuplicateResponse()
using (var mdns = new MulticastService())
{
var answerCount = 0;
mdns.NetworkInterfaceDiscovered += async (s, e) =>
mdns.NetworkInterfaceDiscovered += (s, e) =>
{
mdns.SendQuery(service);
await Task.Delay(250);
Thread.Sleep(250);
mdns.SendQuery(service);
};
mdns.QueryReceived += (s, e) =>
Expand Down Expand Up @@ -572,10 +572,10 @@ public async Task NoDuplicateResponse()
using (var mdns = new MulticastService())
{
var answerCount = 0;
mdns.NetworkInterfaceDiscovered += async (s, e) =>
mdns.NetworkInterfaceDiscovered += (s, e) =>
{
mdns.SendQuery(service);
await Task.Delay(2000);
Thread.Sleep(2000);
mdns.SendQuery(service);
};
mdns.QueryReceived += (s, e) =>
Expand All @@ -601,7 +601,7 @@ public async Task NoDuplicateResponse()
};
};
mdns.Start();
await Task.Delay(3000);
await Task.Delay(5000);
Assert.AreEqual(2, answerCount);
}
}
Expand All @@ -624,5 +624,22 @@ public void Multiple_Listeners()
Assert.IsTrue(ready2.WaitOne(TimeSpan.FromSeconds(1)), "ready2 timeout");
}
}

[TestMethod]
public void MalformedMessage()
{
byte[] malformedMessage = null;
using (var mdns = new MulticastService())
{
mdns.MalformedMessage += (s, e) => malformedMessage = e;

var msg = new byte[] { 0xff };
var endPoint = new IPEndPoint(IPAddress.Loopback, 5353);
var udp = new UdpReceiveResult(msg, endPoint);
mdns.OnDnsMessage(this, udp);

CollectionAssert.AreEqual(msg, malformedMessage);
}
}
}
}
43 changes: 43 additions & 0 deletions traffic/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
using Makaretu.Dns;
using System;

namespace traffic
{
class Program
{
static readonly object ttyLock = new object();

static void Main(string[] args)
{
var mdns = new MulticastService();
mdns.NetworkInterfaceDiscovered += (s, e)
=> mdns.SendQuery(ServiceDiscovery.ServiceName, type: DnsType.PTR);
mdns.AnswerReceived += OnGoodDnsMessage;
mdns.QueryReceived += OnGoodDnsMessage;
mdns.MalformedMessage += OnBadDnsMessage;
mdns.Start();
Console.ReadKey();
}

private static void OnBadDnsMessage(object sender, byte[] packet)
{
lock (ttyLock)
{
Console.WriteLine(">>> {0:O} <<<", DateTime.Now);
Console.WriteLine("Malformed message (base64)");
Console.WriteLine(Convert.ToBase64String(packet));
}

Environment.Exit(1);
}

private static void OnGoodDnsMessage(object sender, MessageEventArgs e)
{
lock (ttyLock)
{
Console.WriteLine("=== {0:O} ===", DateTime.Now);
Console.WriteLine(e.Message.ToString());
}
}
}
}
12 changes: 12 additions & 0 deletions traffic/traffic.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp2.2</TargetFramework>
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="..\src\Mdns.csproj" />
</ItemGroup>

</Project>

0 comments on commit a5b730b

Please sign in to comment.