예제 #1
0
        private static Task ResolveNameServerAddressesAsync(DnsServer dnsServer, DnsResourceRecord nsRecord, List <NameServerAddress> outNameServers)
        {
            string nsDomain = (nsRecord.RDATA as DnsNSRecordData).NameServer;

            IReadOnlyList <DnsResourceRecord> glueRecords = nsRecord.GetGlueRecords();

            if (glueRecords.Count > 0)
            {
                foreach (DnsResourceRecord glueRecord in glueRecords)
                {
                    switch (glueRecord.Type)
                    {
                    case DnsResourceRecordType.A:
                        outNameServers.Add(new NameServerAddress(nsDomain, (glueRecord.RDATA as DnsARecordData).Address));
                        break;

                    case DnsResourceRecordType.AAAA:
                        if (dnsServer.PreferIPv6)
                        {
                            outNameServers.Add(new NameServerAddress(nsDomain, (glueRecord.RDATA as DnsAAAARecordData).Address));
                        }

                        break;
                    }
                }

                return(Task.CompletedTask);
            }
            else
            {
                return(ResolveNameServerAddressesAsync(dnsServer, nsDomain, 53, DnsTransportProtocol.Udp, outNameServers));
            }
        }
        private void ResolveAdditionalRecords(DnsResourceRecord refRecord, string domain, bool serveStale, bool dnssecOk, List <DnsResourceRecord> additionalRecords)
        {
            IReadOnlyList <DnsResourceRecord> glueRecords = refRecord.GetGlueRecords();

            if (glueRecords.Count > 0)
            {
                bool added = false;

                foreach (DnsResourceRecord glueRecord in glueRecords)
                {
                    if (!glueRecord.IsStale)
                    {
                        added = true;
                        additionalRecords.Add(glueRecord);

                        if (dnssecOk)
                        {
                            IReadOnlyList <DnsResourceRecord> rrsigRecords = glueRecord.GetRecordInfo().RRSIGRecords;
                            if (rrsigRecords is not null)
                            {
                                additionalRecords.AddRange(rrsigRecords);
                            }
                        }
                    }
                }

                if (added)
                {
                    return;
                }
            }

            if (_root.TryGet(domain, out CacheZone cacheZone))
            {
                {
                    IReadOnlyList <DnsResourceRecord> records = cacheZone.QueryRecords(DnsResourceRecordType.A, serveStale, true);
                    if ((records.Count > 0) && (records[0].Type == DnsResourceRecordType.A))
                    {
                        additionalRecords.AddRange(records);
                    }
                }

                {
                    IReadOnlyList <DnsResourceRecord> records = cacheZone.QueryRecords(DnsResourceRecordType.AAAA, serveStale, true);
                    if ((records.Count > 0) && (records[0].Type == DnsResourceRecordType.AAAA))
                    {
                        additionalRecords.AddRange(records);
                    }
                }
            }
        }
예제 #3
0
        private void ResolveAdditionalRecords(DnsResourceRecord refRecord, string domain, bool serveStale, List <DnsResourceRecord> additionalRecords)
        {
            IReadOnlyList <DnsResourceRecord> glueRecords = refRecord.GetGlueRecords();

            if (glueRecords.Count > 0)
            {
                bool added = false;

                foreach (DnsResourceRecord glueRecord in glueRecords)
                {
                    if (!glueRecord.IsStale)
                    {
                        added = true;
                        additionalRecords.Add(glueRecord);
                    }
                }

                if (added)
                {
                    return;
                }
            }

            CacheZone cacheZone = _root.FindZone(domain, out _, out _, out _);

            if (cacheZone != null)
            {
                {
                    IReadOnlyList <DnsResourceRecord> records = cacheZone.QueryRecords(DnsResourceRecordType.A, serveStale, true);
                    if ((records.Count > 0) && (records[0].RDATA is DnsARecord))
                    {
                        additionalRecords.AddRange(records);
                    }
                }

                {
                    IReadOnlyList <DnsResourceRecord> records = cacheZone.QueryRecords(DnsResourceRecordType.AAAA, serveStale, true);
                    if ((records.Count > 0) && (records[0].RDATA is DnsAAAARecord))
                    {
                        additionalRecords.AddRange(records);
                    }
                }
            }
        }
예제 #4
0
        private static async Task <IReadOnlyList <NameServerAddress> > GetNameServerAddressesAsync(DnsServer dnsServer, DnsResourceRecord record)
        {
            string nsDomain;

            switch (record.Type)
            {
            case DnsResourceRecordType.NS:
                nsDomain = (record.RDATA as DnsNSRecord).NameServer;
                break;

            case DnsResourceRecordType.SOA:
                nsDomain = (record.RDATA as DnsSOARecord).PrimaryNameServer;
                break;

            default:
                throw new InvalidOperationException();
            }

            List <NameServerAddress> nameServers = new List <NameServerAddress>(2);

            IReadOnlyList <DnsResourceRecord> glueRecords = record.GetGlueRecords();

            if (glueRecords.Count > 0)
            {
                foreach (DnsResourceRecord glueRecord in glueRecords)
                {
                    switch (glueRecord.Type)
                    {
                    case DnsResourceRecordType.A:
                        nameServers.Add(new NameServerAddress(nsDomain, (glueRecord.RDATA as DnsARecord).Address));
                        break;

                    case DnsResourceRecordType.AAAA:
                        if (dnsServer.PreferIPv6)
                        {
                            nameServers.Add(new NameServerAddress(nsDomain, (glueRecord.RDATA as DnsAAAARecord).Address));
                        }

                        break;
                    }
                }
            }
            else
            {
                //resolve addresses
                try
                {
                    DnsDatagram response = await dnsServer.DirectQueryAsync(new DnsQuestionRecord(nsDomain, DnsResourceRecordType.A, DnsClass.IN));

                    if ((response != null) && (response.Answer.Count > 0))
                    {
                        IReadOnlyList <IPAddress> addresses = DnsClient.ParseResponseA(response);
                        foreach (IPAddress address in addresses)
                        {
                            nameServers.Add(new NameServerAddress(nsDomain, address));
                        }
                    }
                }
                catch
                { }

                if (dnsServer.PreferIPv6)
                {
                    try
                    {
                        DnsDatagram response = await dnsServer.DirectQueryAsync(new DnsQuestionRecord(nsDomain, DnsResourceRecordType.AAAA, DnsClass.IN));

                        if ((response != null) && (response.Answer.Count > 0))
                        {
                            IReadOnlyList <IPAddress> addresses = DnsClient.ParseResponseAAAA(response);
                            foreach (IPAddress address in addresses)
                            {
                                nameServers.Add(new NameServerAddress(nsDomain, address));
                            }
                        }
                    }
                    catch
                    { }
                }
            }

            return(nameServers);
        }