示例#1
0
        public static DnsResponseMessage Combine(params DnsResponseMessage[] messages)
        {
            if (messages.Length <= 1)
            {
                return(messages.FirstOrDefault());
            }

            var first = messages.First();

            var header = new DnsResponseHeader(
                first.Header.Id,
                (ushort)first.Header.HeaderFlags,
                first.Header.QuestionCount,
                messages.Sum(p => p.Header.AnswerCount),
                messages.Sum(p => p.Header.AdditionalCount),
                first.Header.NameServerCount);

            var response = new DnsResponseMessage(header, messages.Sum(p => p.MessageSize));

            response.Questions.AddRange(first.Questions);
            response.Additionals.AddRange(messages.SelectMany(p => p.Additionals));
            response.Answers.AddRange(messages.SelectMany(p => p.Answers));
            response.Authorities.AddRange(messages.SelectMany(p => p.Authorities));

            return(response);
        }
示例#2
0
 public void AuditResponseHeader(DnsResponseHeader header)
 {
     _auditWriter.AppendLine(";; Got answer:");
     _auditWriter.AppendLine(header.ToString());
     if (header.RecursionDesired && !header.RecursionAvailable)
     {
         _auditWriter.AppendLine(";; WARNING: recursion requested but not available");
     }
     _auditWriter.AppendLine();
 }
示例#3
0
        public DnsResponseMessage(DnsResponseHeader header, int messageSize)
        {
            if (header == null)
            {
                throw new ArgumentNullException(nameof(header));
            }

            Header      = header;
            MessageSize = messageSize;
        }
示例#4
0
        public DnsResponseMessage GetResponseMessage(ArraySegment <byte> responseData)
        {
            var reader = new DnsDatagramReader(responseData);

            var id              = reader.ReadUInt16NetworkOrder();
            var flags           = reader.ReadUInt16NetworkOrder();
            var questionCount   = reader.ReadUInt16NetworkOrder();
            var answerCount     = reader.ReadUInt16NetworkOrder();
            var nameServerCount = reader.ReadUInt16NetworkOrder();
            var additionalCount = reader.ReadUInt16NetworkOrder();

            var header   = new DnsResponseHeader(id, flags, questionCount, answerCount, additionalCount, nameServerCount);
            var response = new DnsResponseMessage(header, responseData.Count);

            for (var questionIndex = 0; questionIndex < questionCount; questionIndex++)
            {
                var questionQueryString = reader.ReadQuestionQueryString();
                var queryTypeValue      = reader.ReadUInt16NetworkOrder();
                var queryClass          = reader.ReadUInt16NetworkOrder();
                if (!_queryTypes.TryGetValue(queryTypeValue, out var queryType))
                {
                    queryType = new PseudoResourceRecordType(
                        abbreviation: "Unknown",
                        value: queryTypeValue
                        );
                }
                var question = new DnsQuestion(questionQueryString, queryType, (QueryClass)queryClass);
                response.AddQuestion(question);
            }

            for (var answerIndex = 0; answerIndex < answerCount; answerIndex++)
            {
                var info   = ReadRecordInfo(reader);
                var record = _dnsRecordFactory.GetRecord(info, reader);
                response.AddAnswer(record);
            }

            for (var serverIndex = 0; serverIndex < nameServerCount; serverIndex++)
            {
                var info   = ReadRecordInfo(reader);
                var record = _dnsRecordFactory.GetRecord(info, reader);
                response.AddAuthority(record);
            }

            for (var additionalIndex = 0; additionalIndex < additionalCount; additionalIndex++)
            {
                var info   = ReadRecordInfo(reader);
                var record = _dnsRecordFactory.GetRecord(info, reader);
                response.AddAdditional(record);
            }

            return(response);
        }
示例#5
0
        public virtual DnsResponseMessage GetResponseMessage(ArraySegment <byte> responseData)
        {
            var reader  = new DnsDatagramReader(responseData);
            var factory = new DnsRecordFactory(reader);

            var id              = reader.ReadUInt16NetworkOrder();
            var flags           = reader.ReadUInt16NetworkOrder();
            var questionCount   = reader.ReadUInt16NetworkOrder();
            var answerCount     = reader.ReadUInt16NetworkOrder();
            var nameServerCount = reader.ReadUInt16NetworkOrder();
            var additionalCount = reader.ReadUInt16NetworkOrder();

            var header   = new DnsResponseHeader(id, flags, questionCount, answerCount, additionalCount, nameServerCount);
            var response = new DnsResponseMessage(header, responseData.Count);

            for (int questionIndex = 0; questionIndex < questionCount; questionIndex++)
            {
                var question = new DnsQuestion(reader.ReadQuestionQueryString(), (QueryType)reader.ReadUInt16NetworkOrder(), (QueryClass)reader.ReadUInt16NetworkOrder());
                response.AddQuestion(question);
            }

            for (int answerIndex = 0; answerIndex < answerCount; answerIndex++)
            {
                var info   = factory.ReadRecordInfo();
                var record = factory.GetRecord(info);
                response.AddAnswer(record);
            }

            for (int serverIndex = 0; serverIndex < nameServerCount; serverIndex++)
            {
                var info   = factory.ReadRecordInfo();
                var record = factory.GetRecord(info);
                response.AddAuthority(record);
            }

            for (int additionalIndex = 0; additionalIndex < additionalCount; additionalIndex++)
            {
                var info   = factory.ReadRecordInfo();
                var record = factory.GetRecord(info);
                response.AddAdditional(record);
            }

            return(response);
        }