From eff5b300b4a440b353c44b37ef1c928d96137e36 Mon Sep 17 00:00:00 2001 From: Richard Schneider Date: Sat, 8 Jun 2019 16:17:56 +1200 Subject: [PATCH] feat(MessageEventArgs): detect legacy unicast messages --- Spike/Program.cs | 3 ++- src/MessageEventArgs.cs | 8 ++++++++ src/MulticastClient.cs | 9 ++++++++- test/MulticastServiceTest.cs | 8 ++++++-- 4 files changed, 24 insertions(+), 4 deletions(-) diff --git a/Spike/Program.cs b/Spike/Program.cs index 219c1d4..05804c3 100644 --- a/Spike/Program.cs +++ b/Spike/Program.cs @@ -51,13 +51,14 @@ static void Main(string[] args) } }; +#if false var sd = new ServiceDiscovery(mdns); sd.Advertise(new ServiceProfile("x1", "_xservice._tcp", 5011)); sd.Advertise(new ServiceProfile("x2", "_xservice._tcp", 666)); var z1 = new ServiceProfile("z1", "_zservice._udp", 5012); z1.AddProperty("foo", "bar"); sd.Advertise(z1); - +#endif mdns.Start(); Console.ReadKey(); } diff --git a/src/MessageEventArgs.cs b/src/MessageEventArgs.cs index 9aaba5c..c9e5c54 100644 --- a/src/MessageEventArgs.cs +++ b/src/MessageEventArgs.cs @@ -24,6 +24,14 @@ public class MessageEventArgs : EventArgs /// The endpoint from the message was received. /// public IPEndPoint RemoteEndPoint { get; set; } + + /// + /// Determines if the sender is using legacy unicast DNS. + /// + /// + /// false if the sender is using port 5353. + /// + public bool IsLegacyUnicast => RemoteEndPoint.Port != MulticastClient.MulticastPort; } } diff --git a/src/MulticastClient.cs b/src/MulticastClient.cs index 5856232..4af58ac 100644 --- a/src/MulticastClient.cs +++ b/src/MulticastClient.cs @@ -19,7 +19,14 @@ class MulticastClient : IDisposable { static readonly ILog log = LogManager.GetLogger(typeof(MulticastClient)); - const int MulticastPort = 5353; + /// + /// The port number assigned to Multicast DNS. + /// + /// + /// Port number 5353. + /// + public static readonly int MulticastPort = 5353; + static readonly IPAddress MulticastAddressIp4 = IPAddress.Parse("224.0.0.251"); static readonly IPAddress MulticastAddressIp6 = IPAddress.Parse("FF02::FB"); static readonly IPEndPoint MdnsEndpointIp6 = new IPEndPoint(MulticastAddressIp6, MulticastPort); diff --git a/test/MulticastServiceTest.cs b/test/MulticastServiceTest.cs index 254c77e..4560f13 100644 --- a/test/MulticastServiceTest.cs +++ b/test/MulticastServiceTest.cs @@ -40,8 +40,12 @@ public void SendQuery() mdns.NetworkInterfaceDiscovered += (s, e) => ready.Set(); mdns.QueryReceived += (s, e) => { - msg = e.Message; - done.Set(); + if ("some-service.local" == e.Message.Questions.First().Name) + { + msg = e.Message; + Assert.IsFalse(e.IsLegacyUnicast); + done.Set(); + } }; try {