Пример #1
0
        public static List <NameServerAddress> GetNameServersFromResponse(DnsDatagram response, bool preferIPv6)
        {
            IReadOnlyList <DnsResourceRecord> authorityRecords;

            if ((response.Question.Count > 0) && (response.Question[0].Type == DnsResourceRecordType.NS) && (response.Answer.Count > 0) && (response.Answer[0].Type == DnsResourceRecordType.NS))
            {
                authorityRecords = response.Answer;
            }
            else
            {
                authorityRecords = response.Authority;
            }

            List <NameServerAddress> nameServers = new List <NameServerAddress>(authorityRecords.Count);

            foreach (DnsResourceRecord authorityRecord in authorityRecords)
            {
                if (authorityRecord.Type == DnsResourceRecordType.NS)
                {
                    DnsNSRecord nsRecord = (DnsNSRecord)authorityRecord.RDATA;
                    IPEndPoint  endPoint = null;

                    //find ip address of authoritative name server from additional records
                    foreach (DnsResourceRecord rr in response.Additional)
                    {
                        if (nsRecord.NameServer.Equals(rr.Name, StringComparison.OrdinalIgnoreCase))
                        {
                            switch (rr.Type)
                            {
                            case DnsResourceRecordType.A:
                                endPoint = new IPEndPoint(((DnsARecord)rr.RDATA).Address, 53);
                                nameServers.Add(new NameServerAddress(nsRecord.NameServer, endPoint));
                                break;

                            case DnsResourceRecordType.AAAA:
                                endPoint = new IPEndPoint(((DnsAAAARecord)rr.RDATA).Address, 53);

                                if (preferIPv6)
                                {
                                    nameServers.Add(new NameServerAddress(nsRecord.NameServer, endPoint));
                                }

                                break;
                            }
                        }
                    }

                    if (endPoint == null)
                    {
                        nameServers.Add(new NameServerAddress(new DomainEndPoint(nsRecord.NameServer, 53)));
                    }
                }
            }

            return(nameServers);
        }
        public static NameServerAddress[] GetNameServersFromResponse(DnsDatagram response, bool preferIPv6)
        {
            List <NameServerAddress> nameServers = new List <NameServerAddress>(response.Authority.Length);

            foreach (DnsResourceRecord authorityRecord in response.Authority)
            {
                if (authorityRecord.Type == DnsResourceRecordType.NS)
                {
                    DnsNSRecord nsRecord = (DnsNSRecord)authorityRecord.RDATA;
                    IPEndPoint  endPoint = null;

                    //find ip address of authoritative name server from additional records
                    foreach (DnsResourceRecord rr in response.Additional)
                    {
                        if (nsRecord.NSDomainName.Equals(rr.Name, StringComparison.CurrentCultureIgnoreCase))
                        {
                            switch (rr.Type)
                            {
                            case DnsResourceRecordType.A:
                                endPoint = new IPEndPoint(((DnsARecord)rr.RDATA).Address, 53);
                                nameServers.Add(new NameServerAddress(nsRecord.NSDomainName, endPoint));
                                break;

                            case DnsResourceRecordType.AAAA:
                                endPoint = new IPEndPoint(((DnsAAAARecord)rr.RDATA).Address, 53);

                                if (preferIPv6)
                                {
                                    nameServers.Add(new NameServerAddress(nsRecord.NSDomainName, endPoint));
                                }

                                break;
                            }
                        }
                    }

                    if (endPoint == null)
                    {
                        nameServers.Add(new NameServerAddress(new DomainEndPoint(nsRecord.NSDomainName, 53)));
                    }
                }
            }

            NameServerAddress[] nsArray = nameServers.ToArray();

            DnsClient.ShuffleArray(nsArray);

            if (preferIPv6)
            {
                Array.Sort(nsArray);
            }

            return(nsArray);
        }
Пример #3
0
        public override bool Equals(object obj)
        {
            if (ReferenceEquals(null, obj))
            {
                return(false);
            }

            if (ReferenceEquals(this, obj))
            {
                return(true);
            }

            DnsNSRecord other = obj as DnsNSRecord;

            if (other == null)
            {
                return(false);
            }

            return(this._nsDomainName.Equals(other._nsDomainName, StringComparison.OrdinalIgnoreCase));
        }
Пример #4
0
        private IReadOnlyList <DnsResourceRecord> GetAdditionalRecords(IReadOnlyList <DnsResourceRecord> refRecords)
        {
            List <DnsResourceRecord> additionalRecords = new List <DnsResourceRecord>();

            foreach (DnsResourceRecord refRecord in refRecords)
            {
                switch (refRecord.Type)
                {
                case DnsResourceRecordType.NS:
                    DnsNSRecord nsRecord = refRecord.RDATA as DnsNSRecord;
                    if (nsRecord is not null)
                    {
                        ResolveAdditionalRecords(refRecord, nsRecord.NameServer, additionalRecords);
                    }

                    break;

                case DnsResourceRecordType.MX:
                    DnsMXRecord mxRecord = refRecord.RDATA as DnsMXRecord;
                    if (mxRecord is not null)
                    {
                        ResolveAdditionalRecords(refRecord, mxRecord.Exchange, additionalRecords);
                    }

                    break;

                case DnsResourceRecordType.SRV:
                    DnsSRVRecord srvRecord = refRecord.RDATA as DnsSRVRecord;
                    if (srvRecord is not null)
                    {
                        ResolveAdditionalRecords(refRecord, srvRecord.Target, additionalRecords);
                    }

                    break;
                }
            }

            return(additionalRecords);
        }