/// <summary> /// Creates a new instance of the <see cref="ServiceProfile"/> class /// with the specified details. /// </summary> /// <param name="instanceName"> /// A unique identifier for the specific service instance. /// </param> /// <param name="serviceName"> /// The <see cref="ServiceName">name</see> of the service. /// </param> /// <param name="port"> /// The TCP/UDP port of the service. /// </param> /// <param name="addresses"> /// The IP addresses of the specific service instance. If <b>null</b> then /// <see cref="MulticastService.GetIPAddresses"/> is used. /// </param> /// <remarks> /// The SRV, TXT and A/AAAA resoruce records are added to the <see cref="Resources"/>. /// </remarks> public ServiceProfile(DomainName instanceName, DomainName serviceName, ushort port, IEnumerable <IPAddress> addresses = null) { InstanceName = instanceName; ServiceName = serviceName; var fqn = FullyQualifiedName; var simpleServiceName = new DomainName(ServiceName.ToString() .Replace("._tcp", "") .Replace("._udp", "") .Trim('_') .Replace("_", "-")); HostName = DomainName.Join(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.GetIPAddresses()) { Resources.Add(AddressRecord.Create(HostName, address)); } }
/// <summary> /// Asks instances of the specified service with the subtype to send details. /// </summary> /// <param name="service"> /// The service name to query. Typically of the form "_<i>service</i>._tcp". /// </param> /// <param name="subtype"> /// The feature that is needed. /// </param> /// <remarks> /// When an answer is received the <see cref="ServiceInstanceDiscovered"/> event is raised. /// </remarks> /// <seealso cref="ServiceProfile.ServiceName"/> public void QueryServiceInstances(DomainName service, string subtype) { var name = DomainName.Join( new DomainName(subtype), SubName, service, LocalDomain); Mdns.SendQuery(name, type: DnsType.PTR); }
DomainName MakeAbsoluteDomainName(string name) { // If an absolute name. if (name.EndsWith(".")) { return(new DomainName(name.Substring(0, name.Length - 1))); } // Then its a relative name. return(DomainName.Join(new DomainName(name), Origin)); }
public void Joining() { var a = new DomainName(@"foo\.bar"); var b = new DomainName("x.y.z"); var c = DomainName.Join(a, b); Assert.AreEqual(4, c.Labels.Count); Assert.AreEqual("foo.bar", c.Labels[0]); Assert.AreEqual("x", c.Labels[1]); Assert.AreEqual("y", c.Labels[2]); Assert.AreEqual("z", c.Labels[3]); }
/// <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(); }
/// <summary> /// Asks instances of the specified service to send details. /// accepts unicast and/or broadcast answers. /// </summary> /// <param name="service"> /// The service name to query. Typically of the form "_<i>service</i>._tcp". /// </param> /// <remarks> /// When an answer is received the <see cref="ServiceInstanceDiscovered"/> event is raised. /// </remarks> /// <seealso cref="ServiceProfile.ServiceName"/> public void QueryUnicastServiceInstances(DomainName service) { Mdns.SendUnicastQuery(DomainName.Join(service, LocalDomain), type: DnsType.PTR); }