Beispiel #1
0
        /**
         * Get the service types from a single domain.
         * @param domainName the domain to browse.
         * @return a list of service types.
         */
        private List <ServiceType> getServiceTypes(DomainName domainName)
        {
            List <ServiceType> results = new List <ServiceType>();

            DnsMessage dnsMessage = DNSClientUtil.GetDefaultClient().Resolve(SERVICES_DNSSD_UDP + domainName, RecordType.Ptr);

            if ((dnsMessage == null) || dnsMessage.AnswerRecords.Count == 0 || ((dnsMessage.ReturnCode != ReturnCode.NoError) && (dnsMessage.ReturnCode != ReturnCode.NxDomain)))
            {
                throw new Exception("DNS request failed");
            }
            else
            {
                foreach (DnsRecordBase record in dnsMessage.AnswerRecords)
                {
                    PtrRecord  ptr  = record as PtrRecord;
                    DomainName name = ptr.PointerDomainName;

                    String type      = name.Labels[0];
                    String transport = name.Labels[1];
                    results.Add(new ServiceType(type, transport));
                }
            }

            return(results);
        }
Beispiel #2
0
        /**
         * Get all service names of a specific type in a single domain.
         * @param type the service type as a string, including transport and subtype (if any).
         * @param domainName the domain to browse.
         * @param results a collection to put found service names into.
         */
        private void getServiceInstances(String type, DomainName domainName, ICollection <ServiceName> results)
        {
            DomainName typeDN         = DomainName.Parse(type);
            DomainName typeDomainName = typeDN + domainName;

            DnsMessage dnsMessage = DNSClientUtil.GetDefaultClient().Resolve(typeDomainName, RecordType.Ptr);

            if ((dnsMessage == null) || dnsMessage.AnswerRecords.Count == 0 || ((dnsMessage.ReturnCode != ReturnCode.NoError) && (dnsMessage.ReturnCode != ReturnCode.NxDomain)))
            {
                throw new Exception("DNS request failed");
            }


            else
            {
                foreach (DnsRecordBase record in dnsMessage.AnswerRecords)
                {
                    PtrRecord  ptr  = record as PtrRecord;
                    DomainName name = ptr.PointerDomainName;
                    try
                    {
                        results.Add(ServiceName.fromDnsName(name));
                    }
                    catch (ArgumentException e)
                    {
                        Log.Error("Invalid service instance " + name + ": " + e.Message, e);
                    }
                }
            }
        }
Beispiel #3
0
        /**
         * Create a DNS {@link Resolver} to handle updates to the given domain.
         * @param domain the domain for which updates will be generated.
         * @return a Resolver configured with the DNS server that handles zone for that domain.
         * @throws UnknownHostException if the DNS server name for the domain failed to resolve.
         */
        private DnsClient findUpdateResolver(DomainName domain)
        {
            DnsClient dnsClient = null;

            DnsMessage dnsMessage = DNSClientUtil.GetDefaultClient().Resolve(DNSUPDATE_UDP + domain, RecordType.Srv);



            if ((dnsMessage == null) || dnsMessage.AnswerRecords.Count == 0 || ((dnsMessage.ReturnCode != ReturnCode.NoError) && (dnsMessage.ReturnCode != ReturnCode.NxDomain)))
            {
                throw new Exception("DNS request failed");
            }
            else

            {
                foreach (DnsRecordBase record in dnsMessage.AnswerRecords)
                {
                    SrvRecord   srv     = record as SrvRecord;
                    IPAddress[] address = Dns.GetHostAddresses(srv.Target.ToString());
                    dnsClient = new DnsClient(address, 10000);

                    Log.Info(String.Format("Using DNS server {0} to perform updates.", address));

                    break;
                }
            }

            if (dnsClient == null)
            {
                dnsClient = DNSClientUtil.GetDefaultClient();
            }
            return(dnsClient);
        }
Beispiel #4
0
        /**
         * Get all domains pointed to by the given resource record name,
         * looking in a single computer domain.
         * @param rrName the DNS resource record name.
         * @return a collection of domain names.
         */
        private List <String> getDomains(DomainName rrName, DomainName domainName)
        {
            List <String> results = new List <String>();

            DnsMessage dnsMessage = DNSClientUtil.GetDefaultClient().Resolve(rrName + domainName, RecordType.Ptr);


            if ((dnsMessage == null) || dnsMessage.AnswerRecords.Count == 0 || ((dnsMessage.ReturnCode != ReturnCode.NoError) && (dnsMessage.ReturnCode != ReturnCode.NxDomain)))
            {
                throw new Exception("DNS request failed");
            }
            else

            {
                foreach (DnsRecordBase record in dnsMessage.AnswerRecords)
                {
                    PtrRecord  ptr  = record as PtrRecord;
                    DomainName name = ptr.PointerDomainName;
                    results.Add(name.ToString());
                }
            }

            return(results);
        }
Beispiel #5
0
        public ServiceData getServiceData(ServiceName service)
        {
            DomainName serviceName = service.toDnsName();

            DnsMessage dnsMessage = DNSClientUtil.GetDefaultClient().Resolve(serviceName, RecordType.Srv);

            if ((dnsMessage == null) || dnsMessage.AnswerRecords.Count == 0 || ((dnsMessage.ReturnCode != ReturnCode.NoError) && (dnsMessage.ReturnCode != ReturnCode.NxDomain)))
            {
                return(null);
            }

            ServiceData data = new ServiceData();

            data.setName(service);
            foreach (DnsRecordBase record in dnsMessage.AnswerRecords)
            {
                // TODO Handle priority and weight correctly in case of multiple SRV record.
                SrvRecord srv = record as SrvRecord;
                data.setHost(srv.Target.ToString());
                data.setPort(srv.Port);
                break;
            }

            dnsMessage = DNSClientUtil.GetDefaultClient().Resolve(serviceName, RecordType.Txt);


            if ((dnsMessage == null) || dnsMessage.AnswerRecords.Count == 0 || ((dnsMessage.ReturnCode != ReturnCode.NoError) && (dnsMessage.ReturnCode != ReturnCode.NxDomain)))
            {
                return(data);
            }
            foreach (DnsRecordBase record in dnsMessage.AnswerRecords)
            {
                // TODO Handle multiple TXT records as different variants of same service
                TxtRecord txt = record as TxtRecord;
                foreach (String str in txt.TextParts)
                {
                    // Safe cast
                    int    i = str.IndexOf('=');
                    String key;
                    String value;
                    if (i == 0 || str.Equals(""))
                    {
                        continue;                           // Invalid empty key, should be ignored
                    }
                    else if (i > 0)
                    {
                        key   = str.Substring(0, i).ToLower();
                        value = str.Substring(i + 1);
                    }
                    else
                    {
                        key   = str;
                        value = null;
                    }

                    if (!data.getProperties().ContainsKey(key))                         // Ignore all but the first
                    {
                        data.getProperties()[key] = value;
                    }
                }
                break;
            }
            return(data);
        }