Пример #1
0
        public static Response FromArray(byte[] message)
        {
            Header header = Header.FromArray(message);
            int    offset = header.Size;

            if (!header.Response || header.QuestionCount == 0)
            {
                throw new ArgumentException("Invalid response message");
            }

            if (header.Truncated)
            {
                return(new Response(header,
                                    Question.GetAllFromArray(message, offset, header.QuestionCount),
                                    new List <IResourceRecord>(),
                                    new List <IResourceRecord>(),
                                    new List <IResourceRecord>()));
            }

            return(new Response(header,
                                Question.GetAllFromArray(message, offset, header.QuestionCount, out offset),
                                ResourceRecordFactory.GetAllFromArray(message, offset, header.AnswerRecordCount, out offset),
                                ResourceRecordFactory.GetAllFromArray(message, offset, header.AuthorityRecordCount, out offset),
                                ResourceRecordFactory.GetAllFromArray(message, offset, header.AdditionalRecordCount, out offset)));
        }
Пример #2
0
        /// <summary>
        ///     Attempts to parse raw data into a structured packet
        /// </summary>
        /// <param name="buffer">Raw data to parse</param>
        /// <param name="packet">Parsed packet</param>
        /// <param name="count">The length of the packet in bytes</param>
        /// <param name="index">The index into the buffer at which the packet begins</param>
        /// <returns>True if parsing was successful, false if it is not.</returns>
        internal static bool TryParse(byte[] buffer, int index, int count, out DNSReply packet)
        {
            try
            {
                if (count < MinimumParseableBytes)
                {
                    packet = null;
                    return(false);
                }

                using (var ms = new MemoryStream(buffer, index, count, false))
                {
                    using (var br = new BinaryReader(ms))
                    {
                        var messageBytes = br.ReadBytes(count);
                        var header       = Header.FromArray(messageBytes);
                        int offset       = header.Size;

                        if (!header.Response || header.QuestionCount == 0)
                        {
                            packet = null;
                            return(false);
                        }


                        if (header.Truncated)
                        {
                            packet = new DNSReply(header,
                                                  Question.GetAllFromArray(messageBytes, offset, header.QuestionCount),
                                                  new List <IResourceRecord>(),
                                                  new List <IResourceRecord>(),
                                                  new List <IResourceRecord>());
                        }
                        else
                        {
                            packet = new DNSReply(header,
                                                  Question.GetAllFromArray(messageBytes, offset, header.QuestionCount, out offset),
                                                  ResourceRecordFactory.GetAllFromArray(messageBytes, offset, header.AnswerRecordCount, out offset),
                                                  ResourceRecordFactory.GetAllFromArray(messageBytes, offset, header.AuthorityRecordCount, out offset),
                                                  ResourceRecordFactory.GetAllFromArray(messageBytes, offset, header.AdditionalRecordCount, out offset));
                        }

                        return(true);
                    }
                }
            }
            catch (Exception)
            {
                packet = null;
                return(false);
            }
        }
Пример #3
0
        public static DnsRequestMessage FromArray(byte[] message)
        {
            Header header = Header.FromArray(message);
            int    offset = header.Size;

            if (header.Response || header.QuestionCount == 0 ||
                header.AnswerRecordCount + header.AuthorityRecordCount > 0 ||
                header.ResponseCode != ResponseCode.NoError)
            {
                throw new ArgumentException("Invalid request message");
            }

            return(new DnsRequestMessage(header,
                                         Question.GetAllFromArray(message, offset, header.QuestionCount, out offset),
                                         ResourceRecordFactory.GetAllFromArray(message, offset, header.AdditionalRecordCount, out offset)));
        }