Пример #1
0
        public DnsDatagram(dynamic jsonResponse)
        {
            _header = new DnsHeader(jsonResponse);

            {
                _question = new DnsQuestionRecord[_header.QDCOUNT];
                int i = 0;
                foreach (dynamic jsonQuestionRecord in jsonResponse.Question)
                {
                    _question[i++] = new DnsQuestionRecord(jsonQuestionRecord);
                }
            }

            if (jsonResponse.Answer == null)
            {
                _answer = new DnsResourceRecord[] { };
            }
            else
            {
                _answer = new DnsResourceRecord[_header.ANCOUNT];
                int i = 0;
                foreach (dynamic jsonAnswerRecord in jsonResponse.Answer)
                {
                    _answer[i++] = new DnsResourceRecord(jsonAnswerRecord);
                }
            }

            if (jsonResponse.Authority == null)
            {
                _authority = new DnsResourceRecord[] { };
            }
            else
            {
                _authority = new DnsResourceRecord[_header.NSCOUNT];
                int i = 0;
                foreach (dynamic jsonAuthorityRecord in jsonResponse.Authority)
                {
                    _authority[i++] = new DnsResourceRecord(jsonAuthorityRecord);
                }
            }

            if (jsonResponse.Additional == null)
            {
                _additional = new DnsResourceRecord[] { };
            }
            else
            {
                _additional = new DnsResourceRecord[_header.ARCOUNT];
                int i = 0;
                foreach (dynamic jsonAdditionalRecord in jsonResponse.Additional)
                {
                    _additional[i++] = new DnsResourceRecord(jsonAdditionalRecord);
                }
            }
        }
Пример #2
0
        public override bool Equals(object obj)
        {
            if (ReferenceEquals(null, obj))
            {
                return(false);
            }

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

            DnsQuestionRecord other = obj as DnsQuestionRecord;

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

            if (!_name.Equals(other._name, StringComparison.OrdinalIgnoreCase))
            {
                return(false);
            }

            if (_type != other._type)
            {
                return(false);
            }

            if (_class != other._class)
            {
                return(false);
            }

            return(true);
        }
Пример #3
0
        public override DnsDatagram Query(DnsDatagram request)
        {
            DnsQuestionRecord question = request.Question[0];

            DnsResourceRecord[] answerRecords = GetRecords(question.Name, question.Type);
            if (answerRecords != null)
            {
                if (answerRecords[0].RDATA is DnsEmptyRecord)
                {
                    DnsResourceRecord[] responseAuthority;
                    DnsResourceRecord   authority = (answerRecords[0].RDATA as DnsEmptyRecord).Authority;

                    if (authority == null)
                    {
                        responseAuthority = new DnsResourceRecord[] { }
                    }
                    ;
                    else
                    {
                        responseAuthority = new DnsResourceRecord[] { authority }
                    };

                    return(new DnsDatagram(new DnsHeader(request.Header.Identifier, true, DnsOpcode.StandardQuery, false, false, request.Header.RecursionDesired, true, false, false, DnsResponseCode.NoError, 1, 0, 1, 0), request.Question, new DnsResourceRecord[] { }, responseAuthority, new DnsResourceRecord[] { }));
                }

                if (answerRecords[0].RDATA is DnsNXRecord)
                {
                    DnsResourceRecord[] responseAuthority;
                    DnsResourceRecord   authority = (answerRecords[0].RDATA as DnsNXRecord).Authority;

                    if (authority == null)
                    {
                        responseAuthority = new DnsResourceRecord[] { }
                    }
                    ;
                    else
                    {
                        responseAuthority = new DnsResourceRecord[] { authority }
                    };

                    return(new DnsDatagram(new DnsHeader(request.Header.Identifier, true, DnsOpcode.StandardQuery, false, false, request.Header.RecursionDesired, true, false, false, DnsResponseCode.NameError, 1, 0, 1, 0), request.Question, new DnsResourceRecord[] { }, responseAuthority, new DnsResourceRecord[] { }));
                }

                if (answerRecords[0].RDATA is DnsANYRecord)
                {
                    DnsANYRecord anyRR = answerRecords[0].RDATA as DnsANYRecord;
                    return(new DnsDatagram(new DnsHeader(request.Header.Identifier, true, DnsOpcode.StandardQuery, false, false, request.Header.RecursionDesired, true, false, false, DnsResponseCode.NoError, 1, (ushort)anyRR.Records.Length, 0, 0), request.Question, anyRR.Records, new DnsResourceRecord[] { }, new DnsResourceRecord[] { }));
                }

                if (answerRecords[0].RDATA is DnsFailureRecord)
                {
                    return(new DnsDatagram(new DnsHeader(request.Header.Identifier, true, DnsOpcode.StandardQuery, false, false, request.Header.RecursionDesired, true, false, false, (answerRecords[0].RDATA as DnsFailureRecord).RCODE, 1, 0, 0, 0), request.Question, new DnsResourceRecord[] { }, new DnsResourceRecord[] { }, new DnsResourceRecord[] { }));
                }

                return(new DnsDatagram(new DnsHeader(request.Header.Identifier, true, DnsOpcode.StandardQuery, false, false, request.Header.RecursionDesired, true, false, false, DnsResponseCode.NoError, 1, (ushort)answerRecords.Length, 0, 0), request.Question, answerRecords, new DnsResourceRecord[] { }, new DnsResourceRecord[] { }));
            }

            DnsResourceRecord[] nameServers = GetClosestNameServers(question.Name);
            if (nameServers != null)
            {
                List <DnsResourceRecord> glueRecords = new List <DnsResourceRecord>();

                foreach (DnsResourceRecord nameServer in nameServers)
                {
                    string nsDomain = (nameServer.RDATA as DnsNSRecord).NSDomainName;

                    DnsResourceRecord[] glueAs = GetRecords(nsDomain, DnsResourceRecordType.A);
                    if ((glueAs != null) && (glueAs.Length > 0) && (glueAs[0].RDATA is DnsARecord))
                    {
                        glueRecords.AddRange(glueAs);
                    }

                    DnsResourceRecord[] glueAAAAs = GetRecords(nsDomain, DnsResourceRecordType.AAAA);
                    if ((glueAAAAs != null) && (glueAAAAs.Length > 0) && (glueAAAAs[0].RDATA is DnsAAAARecord))
                    {
                        glueRecords.AddRange(glueAAAAs);
                    }
                }

                DnsResourceRecord[] additional = glueRecords.ToArray();

                return(new DnsDatagram(new DnsHeader(request.Header.Identifier, true, DnsOpcode.StandardQuery, false, false, request.Header.RecursionDesired, true, false, false, DnsResponseCode.NoError, 1, 0, (ushort)nameServers.Length, (ushort)additional.Length), request.Question, new DnsResourceRecord[] { }, nameServers, additional));
            }

            return(new DnsDatagram(new DnsHeader(request.Header.Identifier, true, DnsOpcode.StandardQuery, false, false, request.Header.RecursionDesired, true, false, false, DnsResponseCode.Refused, 1, 0, 0, 0), request.Question, new DnsResourceRecord[] { }, new DnsResourceRecord[] { }, new DnsResourceRecord[] { }));
        }