Exemple #1
0
        public void SetRecord(DnsSection section, int index, IDnsRecord record)
        {
            CheckQuestion(section, record);

            object records = SectionAt(section);

            if (records == null)
            {
                throw new IndexOutOfRangeException($"index: {index} (expected: none)");
            }

            if (records is IDnsRecord)
            {
                if (index == 0)
                {
                    SetSection(section, record);
                }
                else
                {
                    throw new IndexOutOfRangeException($"index: {index} (expected: 0)");
                }
            }

            List <IDnsRecord> recordList = (List <IDnsRecord>)records;

            recordList[index] = record;
        }
Exemple #2
0
        public void AddRecord(DnsSection section, IDnsRecord record)
        {
            CheckQuestion(section, record);

            object records = SectionAt(section);

            if (records == null)
            {
                SetSection(section, record);
                return;
            }

            List <IDnsRecord> recordList;

            if (records is IDnsRecord)
            {
                recordList = new List <IDnsRecord>(2);
                recordList.Add((IDnsRecord)records);
                recordList.Add(record);
                SetSection(section, recordList);
                return;
            }

            recordList = (List <IDnsRecord>)records;
            recordList.Add(record);
        }
Exemple #3
0
 private static void CheckQuestion(DnsSection section, IDnsRecord record)
 {
     if (section == SECTION_QUESTION &&
         record != null &&
         !(record is IDnsQuestion))
     {
         throw new ArgumentException($"record: {record} (expected: DnsQuestion)");
     }
 }
Exemple #4
0
        private void EncodeRecords(IDnsResponse response, DnsSection section, IByteBuffer buffer)
        {
            int count = response.Count(section);

            for (int i = 0; i < count; i++)
            {
                recordEncoder.EncodeRecord(response.GetRecord <IDnsRecord>(section, i), buffer);
            }
        }
Exemple #5
0
        private void EncodeRecords(IDnsQuery query, DnsSection section, IByteBuffer buffer)
        {
            int count = query.Count(section);

            for (int i = 0; i < count; i++)
            {
                recordEncoder.EncodeRecord(query.GetRecord <IDnsRecord>(section, i), buffer);
            }
        }
        private void DecodeRecords(IDnsQuery query, DnsSection section, IByteBuffer buffer, int count)
        {
            for (int i = count; i > 0; i--)
            {
                IDnsRecord r = recordDecoder.DecodeRecord(buffer);
                if (r == null)
                {
                    break;
                }

                query.AddRecord(section, r);
            }
        }
        private void DecodeRecords(IDnsResponse response, DnsSection section, IByteBuffer buffer, int count)
        {
            for (int i = count - 1; i > 0; i--)
            {
                IDnsRecord r = recordDecoder.DecodeRecord(buffer);
                if (r == null)
                {
                    break;
                }

                response.AddRecord(section, r);
            }
        }
Exemple #8
0
        public int Count(DnsSection section)
        {
            object records = SectionAt(section);

            if (records == null)
            {
                return(0);
            }

            if (records is IDnsRecord)
            {
                return(1);
            }

            List <IDnsRecord> recordList = (List <IDnsRecord>)records;

            return(recordList.Count);
        }
Exemple #9
0
        public void AddRecord(DnsSection section, int index, IDnsRecord record)
        {
            CheckQuestion(section, record);

            object records = SectionAt(section);

            if (records == null)
            {
                if (index != 0)
                {
                    throw new IndexOutOfRangeException($"index: {index} (expected: 0)");
                }

                SetSection(section, record);
                return;
            }

            List <IDnsRecord> recordList;

            if (records is IDnsRecord)
            {
                if (index == 0)
                {
                    recordList = new List <IDnsRecord>();
                    recordList.Add(record);
                    recordList.Add((IDnsRecord)records);
                }
                else if (index == 1)
                {
                    recordList = new List <IDnsRecord>();
                    recordList.Add((IDnsRecord)records);
                    recordList.Add(record);
                }
                else
                {
                    throw new IndexOutOfRangeException($"index: {index} (expected: 0 or 1)");
                }
                SetSection(section, recordList);
                return;
            }

            recordList        = (List <IDnsRecord>)records;
            recordList[index] = record;
        }
Exemple #10
0
        private object SectionAt(DnsSection section)
        {
            switch (section)
            {
            case DnsSection.QUESTION:
                return(questions);

            case DnsSection.ANSWER:
                return(answers);

            case DnsSection.AUTHORITY:
                return(authorities);

            case DnsSection.ADDITIONAL:
                return(additionals);

            default:
                return(null);
            }
        }
Exemple #11
0
        private void SetSection(DnsSection section, object value)
        {
            switch (section)
            {
            case DnsSection.QUESTION:
                questions = value;
                break;

            case DnsSection.ANSWER:
                answers = value;
                break;

            case DnsSection.AUTHORITY:
                authorities = value;
                break;

            case DnsSection.ADDITIONAL:
                additionals = value;
                break;
            }
        }
Exemple #12
0
        public void Clear(DnsSection section)
        {
            object recordOrList = SectionAt(section);

            SetSection(section, null);

            if (recordOrList is IReferenceCounted)
            {
                ((IReferenceCounted)recordOrList).Release();
            }
            else if (recordOrList is IList)
            {
                List <IDnsRecord> list = (List <IDnsRecord>)recordOrList;
                if (list.Count == 0)
                {
                    foreach (var r in list)
                    {
                        ReferenceCountUtil.Release(r);
                    }
                }
            }
        }
Exemple #13
0
        public TRecord GetRecord <TRecord>(DnsSection section) where TRecord : IDnsRecord
        {
            object records = SectionAt(section);

            if (records == null)
            {
                return(default(TRecord));
            }

            if (records is IDnsRecord)
            {
                return((TRecord)records);
            }

            List <IDnsRecord> recordList = (List <IDnsRecord>)records;

            if (recordList.Count == 0)
            {
                return(default(TRecord));
            }

            return((TRecord)recordList[0]);
        }
Exemple #14
0
        public TRecord GetRecord <TRecord>(DnsSection section, int index) where TRecord : IDnsRecord
        {
            object records = SectionAt(section);

            if (records == null)
            {
                throw new IndexOutOfRangeException($"index: {index} (expected: none)");
            }

            if (records is IDnsRecord)
            {
                if (index == 0)
                {
                    return((TRecord)records);
                }

                throw new IndexOutOfRangeException($"index: {index} (expected: 0)");
            }

            List <IDnsRecord> recordList = (List <IDnsRecord>)records;

            return((TRecord)recordList[index]);
        }
Exemple #15
0
        public void RemoveRecord(DnsSection section, int index)
        {
            object records = SectionAt(section);

            if (records == null)
            {
                throw new IndexOutOfRangeException($"index: {index} (expected: none)");
            }

            if (records is IDnsRecord)
            {
                if (index != 0)
                {
                    throw new IndexOutOfRangeException($"index: {index} (expected: 0)");
                }

                SetSection(section, null);
            }

            List <IDnsRecord> recordList = (List <IDnsRecord>)records;

            recordList.RemoveAt(index);
        }
Exemple #16
0
        private static StringBuilder AppendRecords(this StringBuilder builder, IDnsMessage message, DnsSection section)
        {
            int count = message.Count(section);

            for (int i = 0; i < count; i++)
            {
                builder.Append(Environment.NewLine)
                .Append('\t')
                .Append(message.GetRecord <IDnsRecord>(section, i));
            }

            return(builder);
        }
Exemple #17
0
 public void SetRecord(DnsSection section, IDnsRecord record)
 {
     Clear(section);
     SetSection(section, record);
 }