Exemplo n.º 1
0
        public void Roundtrip_Master()
        {
            var a = new PTRRecord
            {
                Name       = "emanon.org",
                DomainName = "somewhere.else.org"
            };
            var b = (PTRRecord) new ResourceRecord().Read(a.ToString());

            Assert.AreEqual(a.Name, b.Name);
            Assert.AreEqual(a.Class, b.Class);
            Assert.AreEqual(a.Type, b.Type);
            Assert.AreEqual(a.TTL, b.TTL);
            Assert.AreEqual(a.DomainName, b.DomainName);
        }
Exemplo n.º 2
0
        public void Equality()
        {
            var a = new PTRRecord
            {
                Name       = "emanon.org",
                DomainName = "somewhere.else.org"
            };
            var b = new PTRRecord
            {
                Name       = "emanon.org",
                DomainName = "somewhere.org"
            };

            Assert.IsTrue(a.Equals(a));
            Assert.IsFalse(a.Equals(b));
            Assert.IsFalse(a.Equals(null));
        }
Exemplo n.º 3
0
        /// <summary>
        /// Sends a goodbye message for the provided
        /// profile and removes its pointer from the name sever.
        /// </summary>
        /// <param name="profile">The profile to send a goodbye message for.</param>
        public void Unadvertise(ServiceProfile profile)
        {
            var message = new Message {
                QR = true
            };
            var ptrRecord = new PTRRecord {
                Name = profile.QualifiedServiceName, DomainName = profile.FullyQualifiedName
            };

            ptrRecord.TTL = TimeSpan.Zero;

            message.Answers.Add(ptrRecord);
            profile.Resources.ForEach((resource) =>
            {
                resource.TTL = TimeSpan.Zero;
                message.AdditionalRecords.Add(resource);
            });

            Mdns.SendAnswer(message);

            NameServer.Catalog.TryRemove(profile.QualifiedServiceName, out Node _);
        }
Exemplo n.º 4
0
        /// <summary>
        ///    Sends an unsolicited MDNS response describing the
        ///    service profile.
        /// </summary>
        /// <param name="profile">
        ///   The profile to describe.
        /// </param>
        /// <remarks>
        ///   Sends a MDNS response <see cref="Message"/> containing the pointer
        ///   and resource records of the <paramref name="profile"/>.
        ///   <para>
        ///   To provide increased robustness against packet loss,
        ///   two unsolicited responses are sent one second apart.
        ///   </para>
        /// </remarks>
        public void Announce(ServiceProfile profile)
        {
            var message = new Message {
                QR = true
            };

            // Add the shared records.
            var ptrRecord = new PTRRecord {
                Name = profile.QualifiedServiceName, DomainName = profile.FullyQualifiedName
            };

            message.Answers.Add(ptrRecord);

            // Add the resource records.
            profile.Resources.ForEach((resource) =>
            {
                message.Answers.Add(resource);
            });

            Mdns.SendAnswer(message, checkDuplicate: false);
            Task.Delay(1000).Wait();
            Mdns.SendAnswer(message, checkDuplicate: false);
        }
Exemplo n.º 5
0
        /// <summary>
        ///   Advertise a service profile.
        /// </summary>
        /// <param name="service">
        ///   The service profile.
        /// </param>
        /// <remarks>
        ///   Any queries for the service or service instance will be answered with
        ///   information from the profile.
        ///   <para>
        ///   Besides adding the profile's resource records to the <see cref="Catalog"/> PTR records are
        ///   created to support DNS-SD and reverse address mapping (DNS address lookup).
        ///   </para>
        /// </remarks>
        public void Advertise(ServiceProfile service)
        {
            profiles.Add(service);

            var catalog = NameServer.Catalog;

            catalog.Add(
                new PTRRecord {
                Name = ServiceName, DomainName = service.QualifiedServiceName
            },
                authoritative: true);
            catalog.Add(
                new PTRRecord {
                Name = service.QualifiedServiceName, DomainName = service.FullyQualifiedName
            },
                authoritative: true);

            foreach (var subtype in service.Subtypes)
            {
                var ptr = new PTRRecord
                {
                    Name = DomainName.Join(
                        new DomainName(subtype),
                        SubName,
                        service.QualifiedServiceName),
                    DomainName = service.FullyQualifiedName
                };
                catalog.Add(ptr, authoritative: true);
            }

            foreach (var r in service.Resources)
            {
                catalog.Add(r, authoritative: true);
            }

            catalog.IncludeReverseLookupRecords();
        }
Exemplo n.º 6
0
        public ServiceProfile(string instanceName, string serviceName, string domainName, ushort port, short ttl, IEnumerable <IPAddress> addresses = null)
        {
            InstanceName = instanceName;
            ServiceName  = serviceName;
            Domain       = domainName;
            var fqn = FullyQualifiedName;

            var simpleServiceName = ServiceName
                                    .Replace("._tcp", "")
                                    .Replace("._udp", "")
                                    .TrimStart('_');

            HostName = $"{InstanceName}.{simpleServiceName}.{Domain}";
            Resources.Add(new SRVRecord
            {
                Name   = fqn,
                Port   = port,
                Target = HostName
            });
            Resources.Add(new TXTRecord
            {
                Name    = fqn,
                Strings = { "txtvers=1" }
            });

            foreach (var address in addresses ?? MulticastService.GetLinkLocalAddresses())
            {
                Resources.Add(AddressRecord.Create(HostName, address));
            }

            servicePtrRecord = new PTRRecord {
                Name = ServiceName, DomainName = QualifiedServiceName, TTL = TimeSpan.FromSeconds(ttl)
            };
            instancePtrRecord = new PTRRecord {
                Name = QualifiedServiceName, DomainName = FullyQualifiedName, TTL = TimeSpan.FromSeconds(ttl)
            };
        }