2 // Mono.Net.Dns.DnsResponse
5 // Gonzalo Paniagua Javier (gonzalo.mono@gmail.com)
7 // Copyright 2011 Gonzalo Paniagua Javier
9 // Licensed under the Apache License, Version 2.0 (the "License");
10 // you may not use this file except in compliance with the License.
11 // You may obtain a copy of the License at
13 // http://www.apache.org/licenses/LICENSE-2.0
15 // Unless required by applicable law or agreed to in writing, software
16 // distributed under the License is distributed on an "AS IS" BASIS,
17 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 // See the License for the specific language governing permissions and
19 // limitations under the License.
22 using System.Collections.Generic;
23 using System.Collections.ObjectModel;
27 namespace Mono.Net.Dns {
28 class DnsResponse : DnsPacket {
29 static readonly ReadOnlyCollection<DnsResourceRecord> EmptyRR = new ReadOnlyCollection<DnsResourceRecord> (new DnsResourceRecord [0]);
30 static readonly ReadOnlyCollection<DnsQuestion> EmptyQS = new ReadOnlyCollection<DnsQuestion> (new DnsQuestion [0]);
32 ReadOnlyCollection<DnsQuestion> question;
33 ReadOnlyCollection<DnsResourceRecord> answer;
34 ReadOnlyCollection<DnsResourceRecord> authority;
35 ReadOnlyCollection<DnsResourceRecord> additional;
36 int offset = DnsHeader.DnsHeaderLength;
38 public DnsResponse (byte [] buffer, int length)
39 : base (buffer, length)
49 for (int i = 0; i < packet.Length; i++)
53 ReadOnlyCollection<DnsResourceRecord> GetRRs (int count)
58 List<DnsResourceRecord> records = new List<DnsResourceRecord>(count);
59 for (int i = 0; i < count; i++)
60 records.Add (DnsResourceRecord.CreateFromBuffer (this, position, ref offset));
61 return records.AsReadOnly ();
64 ReadOnlyCollection<DnsQuestion> GetQuestions (int count)
69 List<DnsQuestion> records = new List<DnsQuestion> (count);
70 for(int i = 0; i < count; i++) {
71 DnsQuestion record = new DnsQuestion ();
72 offset = record.Init (this, offset);
75 return records.AsReadOnly ();
78 public ReadOnlyCollection<DnsQuestion> GetQuestions ()
81 question = GetQuestions (Header.QuestionCount);
85 public ReadOnlyCollection<DnsResourceRecord> GetAnswers ()
89 answer = GetRRs (Header.AnswerCount);
94 public ReadOnlyCollection<DnsResourceRecord> GetAuthority ()
96 if (authority == null) {
99 authority = GetRRs (Header.AuthorityCount);
104 public ReadOnlyCollection<DnsResourceRecord> GetAdditional ()
106 if (additional == null) {
110 additional = GetRRs (Header.AdditionalCount);
115 public override string ToString ()
117 StringBuilder sb = new StringBuilder();
119 sb.Append("Question:\r\n");
120 foreach(DnsQuestion q in GetQuestions()) {
121 sb.AppendFormat("\t{0}\r\n", q);
123 sb.Append("Answer(s):\r\n");
124 foreach(DnsResourceRecord q in GetAnswers()) {
125 sb.AppendFormat("\t{0}\r\n", q);
127 sb.Append("Authority:\r\n");
128 foreach(DnsResourceRecord q in GetAuthority()) {
129 sb.AppendFormat("\t{0}\r\n", q);
131 sb.Append("Additional:\r\n");
132 foreach(DnsResourceRecord q in GetAdditional()) {
133 sb.AppendFormat("\t{0}\r\n", q);
135 return sb.ToString();