Rename namespace to Mono.Net.Dns
[mono.git] / mcs / class / System / Mono.Net.Dns / DnsResponse.cs
1 //
2 // Mono.Net.Dns.DnsResponse
3 //
4 // Authors:
5 //      Gonzalo Paniagua Javier (gonzalo.mono@gmail.com)
6 //
7 // Copyright 2011 Gonzalo Paniagua Javier
8 //
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
12 //
13 // http://www.apache.org/licenses/LICENSE-2.0
14 //
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.
20 //
21 using System;
22 using System.Collections.Generic;
23 using System.Collections.ObjectModel;
24 using System.IO;
25 using System.Text;
26
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]);
31
32                 ReadOnlyCollection<DnsQuestion> question;
33                 ReadOnlyCollection<DnsResourceRecord> answer;
34                 ReadOnlyCollection<DnsResourceRecord> authority;
35                 ReadOnlyCollection<DnsResourceRecord> additional;
36                 int offset = DnsHeader.DnsHeaderLength;
37
38                 public DnsResponse (byte [] buffer, int length)
39                         : base (buffer, length)
40                 {
41                 }
42
43                 public void Reset ()
44                 {
45                         question = null;
46                         answer = null;
47                         authority = null;
48                         additional = null;
49                         for (int i = 0; i < packet.Length; i++)
50                                 packet [i] = 0;
51                 }
52
53                 ReadOnlyCollection<DnsResourceRecord> GetRRs (int count)
54                 {
55                         if (count <= 0)
56                                 return EmptyRR;
57
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 ();
62                 }
63
64                 ReadOnlyCollection<DnsQuestion> GetQuestions (int count)
65                 {
66                         if (count <= 0)
67                                 return EmptyQS;
68
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);
73                                 records.Add (record);
74                         }
75                         return records.AsReadOnly ();
76                 }
77
78                 public ReadOnlyCollection<DnsQuestion> GetQuestions ()
79                 {
80                         if (question == null)
81                                 question = GetQuestions (Header.QuestionCount);
82                         return question;
83                 }
84
85                 public ReadOnlyCollection<DnsResourceRecord> GetAnswers ()
86                 {
87                         if (answer == null) {
88                                 GetQuestions ();
89                                 answer = GetRRs (Header.AnswerCount);
90                         }
91                         return answer;
92                 }
93
94                 public ReadOnlyCollection<DnsResourceRecord> GetAuthority ()
95                 {
96                         if (authority == null) {
97                                 GetQuestions ();
98                                 GetAnswers ();
99                                 authority = GetRRs (Header.AuthorityCount);
100                         }
101                         return authority;
102                 }
103
104                 public ReadOnlyCollection<DnsResourceRecord> GetAdditional ()
105                 {
106                         if (additional == null) {
107                                 GetQuestions ();
108                                 GetAnswers ();
109                                 GetAuthority ();
110                                 additional = GetRRs (Header.AdditionalCount);
111                         }
112                         return additional;
113                 }
114
115                 public override string ToString ()
116                 {
117                         StringBuilder sb = new StringBuilder();
118                         sb.Append(Header);
119                         sb.Append("Question:\r\n");
120                         foreach(DnsQuestion q in GetQuestions()) {
121                                 sb.AppendFormat("\t{0}\r\n", q);
122                         }
123                         sb.Append("Answer(s):\r\n");
124                         foreach(DnsResourceRecord q in GetAnswers()) {
125                                 sb.AppendFormat("\t{0}\r\n", q);
126                         }
127                         sb.Append("Authority:\r\n");
128                         foreach(DnsResourceRecord q in GetAuthority()) {
129                                 sb.AppendFormat("\t{0}\r\n", q);
130                         }
131                         sb.Append("Additional:\r\n");
132                         foreach(DnsResourceRecord q in GetAdditional()) {
133                                 sb.AppendFormat("\t{0}\r\n", q);
134                         }
135                         return sb.ToString();
136                 }
137         }
138 }
139