Пример #1
0
        private Packet BuildResponse(string name, UInt16 id, ServiceInfo info, IPAddress ip4Address, IPAddress ip6Address)
        {
            var ret = new Packet();

            ret.TransactionID         = id;
            ret.Flags.IsResponse      = true;
            ret.Flags.IsAuthoritative = true;
            var dnsName     = name;
            var domainName  = string.Join(".", info.Name, dnsName);
            var machineName = fullName;
            var ptr         = PTRAnswer.Build(dnsName, domainName, 4500, false, 1);
            var txt         = TXTAnswer.Build(domainName, 4500, info.Flags, info.Data, true, 1);
            var srv         = SRVAnswer.Build(domainName, 120, info.Priority, info.Weight, info.Port, machineName, true, 1);
            var a           = AAnswer.Build(machineName, ip4Address, (UInt16)120, true, 1);
            var aaaa        = AAAAAnswer.Build(machineName, ip6Address, (UInt16)120, true, 1);

            ret.Answers.Add(ptr);
            ret.Answers.Add(txt);
            ret.Answers.Add(srv);

            if (aaaa != null)
            {
                ret.Answers.Add(aaaa);
            }

            if (a != null)
            {
                ret.Answers.Add(a);
            }

            return(ret);
        }
        public void RespondWithServiceInfo()
        {
            var    core     = BuildService("10.99.99.99", "fe80::20c:29ff:fe0d:e789");
            Packet received = null;

            core.Setup(x => x.SendPacket(It.IsAny <Packet>())).Callback <Packet>(x => received = x);

            using (var service = new ServicePublisher(core.Object))
            {
                var publishInfo = new ServiceInfo();
                publishInfo.Name       = "Pubtest";
                publishInfo.IP4Address = IPAddress.Parse("10.0.0.10");
                publishInfo.IP6Address = IPAddress.Parse("fe80::20c:29ff:fe0d:e789");
                publishInfo.Port       = 9999;
                publishInfo.Priority   = 10;
                publishInfo.Weight     = 10;
                publishInfo.Flags.Add("TestFlag");
                publishInfo.Data["TestData"] = "TestValue";

                service.LocalName = "Scooby";
                service.AddService("_pubtest._tcp.local", publishInfo);
                service.Start();
                core.Raise(x => x.PacketReceived += null, BuildQueryPacket("_pubtest._tcp.local"), new IPEndPoint(IPAddress.Parse("10.0.0.10"), 5353));

                Assert.IsTrue(received.Flags.IsResponse);
                Assert.IsTrue(received.Flags.IsAuthoritative);
                var ptrAnswer = received.Answers.Where(x => x.Record.RecordType == PTRAnswer.RecordType).First().Data as PTRAnswer;
                Assert.AreEqual("Pubtest._pubtest._tcp.local", ptrAnswer.DomainName);

                var srv       = received.Answers.Where(x => x.Record.RecordType == SRVAnswer.RecordType).First();
                var srvAnswer = srv.Data as SRVAnswer;
                Assert.AreEqual(SRVAnswer.GetService(srv.Record), "Pubtest");
                Assert.AreEqual(SRVAnswer.GetProtocol(srv.Record), "_pubtest");
                Assert.AreEqual(SRVAnswer.GetName(srv.Record), "_tcp.local");

                //Assert.AreEqual("Pubtest._pubtest._tcp.local", srvAnswer.Name);
                Assert.AreEqual(9999, srvAnswer.Port);
                Assert.AreEqual(10, srvAnswer.Priority);
                Assert.AreEqual(10, srvAnswer.Weight);
                Assert.AreEqual("Scooby.local", srvAnswer.Target);

                var txtAnswer = received.Answers.Where(x => x.Record.RecordType == TXTAnswer.RecordType).First().Data as TXTAnswer;
                Assert.AreEqual("TestFlag", txtAnswer.Flags[0]);
                Assert.AreEqual("TestValue", txtAnswer.Data["TestData"]);

                var aAnswer = received.Answers.Where(x => x.Record.RecordType == AAnswer.RecordType).First().Data as AAnswer;
                Assert.AreEqual(IPAddress.Parse("10.99.99.99"), aAnswer.Address);

                var aaaaAnswer = received.Answers.Where(x => x.Record.RecordType == AAAAAnswer.RecordType).First().Data as AAAAAnswer;
                Assert.AreEqual(IPAddress.Parse("fe80::20c:29ff:fe0d:e789"), aaaaAnswer.Address);
            }
        }
Пример #3
0
        public void DeserializeResponse()
        {
            var packetHex   = "000084000000000500000000085f70756274657374045f746370056c6f63616c00000c000100001194000a0750756274657374c00cc02b0010800100001194000100c02b0021800100000078000f00000000270f067562756e7475c01ac054001c8001000000780010fe80000000000000020c29fffe0de789c05400018001000000780004c0a81080";
            var packetBytes = ParseWiresharkString(packetHex);
            var reader      = new PacketReader();
            var packet      = reader.Read(packetBytes
                                          );

            Assert.IsTrue(packet.Flags.IsResponse);
            Assert.IsTrue(packet.Flags.IsAuthoritative);
            Assert.AreEqual(packet.TransactionID, 0);
            Assert.AreEqual(packet.Questions, 0);
            Assert.AreEqual(packet.AnswerRRs, 5);
            Assert.AreEqual(packet.AuthorityRRs, 0);
            Assert.AreEqual(packet.AdditionalRRs, 0);

            var ptrAns = packet.Answers.Single(x => x.Record.RecordType == 12);

            Assert.IsTrue(ptrAns.Record.Name.Equals("_pubtest._tcp.local"));
            Assert.AreEqual(ptrAns.Record.Class, 1);
            Assert.IsFalse(ptrAns.CacheFlush);
            Assert.AreEqual(ptrAns.TTL, (uint)4500);
            var ptrData = ptrAns.Data as PTRAnswer;

            Assert.IsTrue(ptrData.DomainName.Equals("Pubtest._pubtest._tcp.local"));

            var txtAns = packet.Answers.Single(x => x.Record.RecordType == 16);

            Assert.IsTrue(txtAns.Record.Name.Equals("Pubtest._pubtest._tcp.local"));
            Assert.AreEqual(txtAns.Record.Class, 1);
            Assert.IsTrue(txtAns.CacheFlush);
            Assert.AreEqual(txtAns.TTL, (uint)4500);
            var txtData = txtAns.Data as TXTAnswer;

            Assert.AreEqual(txtData.Data.Count, 0);
            Assert.AreEqual(txtData.Flags.Count, 0);

            var srv = packet.Answers.Single(x => x.Record.RecordType == 33);

            Assert.AreEqual(srv.Record.Class, 1);
            Assert.IsTrue(srv.CacheFlush);
            Assert.AreEqual(srv.TTL, (uint)120);

            var srvData = srv.Data as SRVAnswer;

            Assert.AreEqual(SRVAnswer.GetService(srv.Record), "Pubtest");
            Assert.AreEqual(SRVAnswer.GetProtocol(srv.Record), "_pubtest");
            Assert.AreEqual(SRVAnswer.GetName(srv.Record), "_tcp.local");
            Assert.AreEqual(srvData.Priority, 0);
            Assert.AreEqual(srvData.Weight, 0);
            Assert.AreEqual(srvData.Port, 9999);
            Assert.IsTrue(srvData.Target.Equals("ubuntu.local"));

            var aaaa = packet.Answers.Single(x => x.Record.RecordType == 28);

            Assert.AreEqual(aaaa.Record.Name, "ubuntu.local");
            Assert.AreEqual(aaaa.Record.Class, 1);
            Assert.IsTrue(aaaa.CacheFlush);
            Assert.AreEqual(aaaa.TTL, (uint)120);
            var aaaaData = aaaa.Data as AAAAAnswer;

            Assert.AreEqual(aaaaData.Address, IPAddress.Parse("fe80::20c:29ff:fe0d:e789"));

            var a = packet.Answers.Single(x => x.Record.RecordType == 1);

            Assert.AreEqual(a.Record.Name, "ubuntu.local");
            Assert.AreEqual(a.Record.Class, 1);
            Assert.IsTrue(a.CacheFlush);
            Assert.AreEqual(a.TTL, (uint)120);
            var aData = a.Data as AAnswer;

            Assert.AreEqual(aData.Address, IPAddress.Parse("192.168.16.128"));
        }