Example #1
0
        public void SendAnswer_TooBig()
        {
            var done = new ManualResetEvent(false);
            var mdns = new MulticastService();

            mdns.NetworkInterfaceDiscovered += (s, e) => done.Set();
            mdns.Start();
            try
            {
                Assert.IsTrue(done.WaitOne(TimeSpan.FromSeconds(1)), "no nic");
                var answer = new Message();
                answer.Answers.Add(new ARecord {
                    Name = "foo.bar.org", Address = IPAddress.Loopback
                });
                answer.Answers.Add(new NULLRecord {
                    Name = "foo.bar.org", Data = new byte[9000]
                });
                ExceptionAssert.Throws <ArgumentOutOfRangeException>(() => {
                    mdns.SendAnswer(answer);
                });
            }
            finally
            {
                mdns.Stop();
            }
        }
Example #2
0
        public void Multiple_Services()
        {
            var     service  = Guid.NewGuid().ToString() + ".local";
            var     done     = new ManualResetEvent(false);
            Message response = null;

            var a = new MulticastService();

            a.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
                        });
                    }
                    a.SendAnswer(res);
                }
            };

            var b = new MulticastService();

            b.NetworkInterfaceDiscovered += (s, e) => b.SendQuery(service);
            b.AnswerReceived             += (s, e) =>
            {
                var msg = e.Message;
                if (msg.Answers.Any(ans => ans.Name == service))
                {
                    response = msg;
                    done.Set();
                }
            };
            try
            {
                a.Start();
                b.Start();
                Assert.IsTrue(done.WaitOne(TimeSpan.FromSeconds(1)), "answer timeout");
                Assert.IsNotNull(response);
                Assert.IsTrue(response.IsResponse);
                Assert.AreEqual(MessageStatus.NoError, response.Status);
                Assert.IsTrue(response.AA);
                Assert.AreNotEqual(0, response.Answers.Count);
            }
            finally
            {
                b.Stop();
                a.Stop();
            }
        }
Example #3
0
        public async Task ReceiveLegacyUnicastAnswer()
        {
            var service = Guid.NewGuid().ToString() + ".local";
            var ready   = new ManualResetEvent(false);

            var query = new Message();

            query.Questions.Add(new Question
            {
                Name = service,
                Type = DnsType.A
            });
            var packet = query.ToByteArray();
            var client = new UdpClient();

            using (var mdns = new MulticastService())
            {
                mdns.NetworkInterfaceDiscovered += (s, e) => ready.Set();
                mdns.QueryReceived += (s, e) =>
                {
                    var msg = e.Message;
                    if (msg.Questions.Any(q => q.Name == service))
                    {
                        var res = msg.CreateResponse();
                        res.Answers.Add(new ARecord
                        {
                            Name    = service,
                            Address = IPAddress.Parse("127.1.1.1")
                        });
                        mdns.SendAnswer(res, e);
                    }
                };
                mdns.Start();
                Assert.IsTrue(ready.WaitOne(TimeSpan.FromSeconds(1)), "ready timeout");
                await client.SendAsync(packet, packet.Length, "224.0.0.251", 5353);

                var r = await client.ReceiveAsync();

                var response = new Message();
                response.Read(r.Buffer, 0, r.Buffer.Length);
                Assert.IsTrue(response.IsResponse);
                Assert.AreEqual(MessageStatus.NoError, response.Status);
                Assert.IsTrue(response.AA);
                Assert.AreEqual(1, response.Questions.Count);
                var a = (ARecord)response.Answers[0];
                Assert.AreEqual(IPAddress.Parse("127.1.1.1"), a.Address);
                Assert.AreEqual(service, a.Name);
                Assert.AreEqual(TimeSpan.FromSeconds(10), a.TTL);
            }
        }
Example #4
0
        public void ReceiveAnswer()
        {
            var     service  = Guid.NewGuid().ToString() + ".local";
            var     done     = new ManualResetEvent(false);
            Message response = null;

            var mdns = new MulticastService();

            mdns.NetworkInterfaceDiscovered += (s, e) => mdns.SendQuery(service);
            mdns.QueryReceived += (s, e) =>
            {
                var msg = e.Message;
                if (msg.Questions.Any(q => q.Name == service))
                {
                    var res = msg.CreateResponse();
                    res.Answers.Add(new ARecord
                    {
                        Name    = service,
                        Address = IPAddress.Parse("127.1.1.1")
                    });
                    mdns.SendAnswer(res);
                }
            };
            mdns.AnswerReceived += (s, e) =>
            {
                var msg = e.Message;
                if (msg.Answers.Any(a => a.Name == service))
                {
                    response = msg;
                    done.Set();
                }
            };
            try
            {
                mdns.Start();
                Assert.IsTrue(done.WaitOne(TimeSpan.FromSeconds(1)), "answer timeout");
                Assert.IsNotNull(response);
                Assert.IsTrue(response.IsResponse);
                Assert.AreEqual(MessageStatus.NoError, response.Status);
                Assert.IsTrue(response.AA);
                var a = (ARecord)response.Answers[0];
                Assert.AreEqual(IPAddress.Parse("127.1.1.1"), a.Address);
            }
            finally
            {
                mdns.Stop();
            }
        }
Example #5
0
        public async Task NoDuplicateResponse()
        {
            var service = Guid.NewGuid().ToString() + ".local";

            using (var mdns = new MulticastService())
            {
                var answerCount = 0;
                mdns.NetworkInterfaceDiscovered += (s, e) =>
                {
                    mdns.SendQuery(service);
                    Thread.Sleep(250);
                    mdns.SendQuery(service);
                };
                mdns.QueryReceived += (s, e) =>
                {
                    var msg = e.Message;
                    if (msg.Questions.Any(q => q.Name == service))
                    {
                        var res = msg.CreateResponse();
                        res.Answers.Add(new ARecord
                        {
                            Name    = service,
                            Address = IPAddress.Parse("127.1.1.1")
                        });
                        mdns.SendAnswer(res, checkDuplicate: false);
                    }
                };
                mdns.AnswerReceived += (s, e) =>
                {
                    var msg = e.Message;
                    if (msg.Answers.Any(answer => answer.Name == service))
                    {
                        ++answerCount;
                    }
                    ;
                };
                mdns.Start();
                await Task.Delay(2000);

                Assert.AreEqual(1, answerCount);

                mdns.SendQuery(service);
                await Task.Delay(2000);

                Assert.AreEqual(2, answerCount);
            }
        }
Example #6
0
        public void ReceiveErrorAnswer()
        {
            var service = Guid.NewGuid().ToString() + ".local";
            var done    = new ManualResetEvent(false);

            var mdns = new MulticastService();

            mdns.NetworkInterfaceDiscovered += (s, e) => mdns.SendQuery(service);
            mdns.QueryReceived += (s, e) =>
            {
                var msg = e.Message;
                if (msg.Questions.Any(q => q.Name == service))
                {
                    var res = msg.CreateResponse();
                    res.Status = MessageStatus.Refused;
                    res.Answers.Add(new ARecord
                    {
                        Name    = service,
                        Address = IPAddress.Parse("127.1.1.1")
                    });
                    mdns.SendAnswer(res);
                }
            };
            mdns.AnswerReceived += (s, e) =>
            {
                var msg = e.Message;
                if (msg.Answers.Any(a => a.Name == service))
                {
                    done.Set();
                }
            };
            try
            {
                mdns.Start();
                Assert.IsFalse(done.WaitOne(TimeSpan.FromSeconds(0.5)), "answer was not ignored");
            }
            finally
            {
                mdns.Stop();
            }
        }
Example #7
0
        public async Task Resolve()
        {
            var service = Guid.NewGuid().ToString() + ".local";
            var query   = new Message();

            query.Questions.Add(new Question {
                Name = service, Type = DnsType.ANY
            });
            var cancellation = new CancellationTokenSource(2000);

            using (var mdns = new MulticastService())
            {
                mdns.QueryReceived += (s, e) =>
                {
                    var msg = e.Message;
                    if (msg.Questions.Any(q => q.Name == service))
                    {
                        var res = msg.CreateResponse();
                        res.Answers.Add(new ARecord
                        {
                            Name    = service,
                            Address = IPAddress.Parse("127.1.1.1")
                        });
                        mdns.SendAnswer(res);
                    }
                };
                mdns.Start();
                var response = await mdns.ResolveAsync(query, cancellation.Token);

                Assert.IsNotNull(response, "no response");
                Assert.IsTrue(response.IsResponse);
                Assert.AreEqual(MessageStatus.NoError, response.Status);
                Assert.IsTrue(response.AA);
                var a = (ARecord)response.Answers[0];
                Assert.AreEqual(IPAddress.Parse("127.1.1.1"), a.Address);
            }
        }