Merge remote-tracking branch 'github/master'
[mono.git] / mcs / class / System / Mono.Net.Dns / DnsResourceRecord.cs
1 //
2 // Mono.Net.Dns.DnsResourceRecord
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.IO;
24 using System.Text;
25
26 namespace Mono.Net.Dns {
27         class DnsResourceRecord {
28                 string name;
29                 DnsType type;
30                 DnsClass klass;
31                 int ttl;
32                 ushort rdlength;
33                 ArraySegment<byte> m_rdata;
34
35                 internal DnsResourceRecord() {
36                 }
37
38                 internal void CopyFrom(DnsResourceRecord rr) {
39                         name = rr.name;
40                         type = rr.type;
41                         klass = rr.klass;
42                         ttl = rr.ttl;
43                         rdlength = rr.rdlength;
44                         m_rdata = rr.m_rdata;
45                 }
46
47                 static internal DnsResourceRecord CreateFromBuffer(DnsPacket packet, int size, ref int offset) {
48                         string pname = packet.ReadName(ref offset);
49                         DnsType ptype = (DnsType)packet.ReadUInt16(ref offset);
50                         DnsClass pclass = (DnsClass)packet.ReadUInt16(ref offset);
51                         int pttl = packet.ReadInt32(ref offset);
52                         ushort prdlength = packet.ReadUInt16(ref offset);
53                         DnsResourceRecord rr = new DnsResourceRecord();
54                         rr.name = pname;
55                         rr.type = ptype;
56                         rr.klass = pclass;
57                         rr.ttl = pttl;
58                         rr.rdlength = prdlength;
59                         rr.m_rdata = new ArraySegment<byte>(packet.Packet, offset, prdlength);
60                         offset += prdlength;
61
62                         switch(pclass) {
63                         case DnsClass.IN:
64                                 switch(ptype) {
65                                 case DnsType.A:
66                                         rr = new DnsResourceRecordA(rr);
67                                         break;
68                                 case DnsType.AAAA:
69                                         rr = new DnsResourceRecordAAAA(rr);
70                                         break;
71                                 case DnsType.CNAME:
72                                         rr = new DnsResourceRecordCName(rr);
73                                         break;
74                                 case DnsType.PTR:
75                                         rr = new DnsResourceRecordPTR(rr);
76                                         break;
77                                 default:
78                                         break;
79                                         }
80                                 break;
81                         default:
82                                 break;
83                         }
84                         return rr;
85                 }
86
87                 public string Name {
88                         get { return name; }
89                 }
90
91                 public DnsType Type {
92                         get { return type; }
93                 }
94
95                 public DnsClass Class {
96                         get { return klass; }
97                 }
98
99                 public int Ttl {
100                         get { return ttl; }
101                 }
102
103                 public ArraySegment<byte> Data {
104                         get { return m_rdata; }
105                 }
106
107                 public override string ToString() {
108                         return String.Format("Name: {0}, Type: {1}, Class: {2}, Ttl: {3}, Data length: {4}", name, type, klass, ttl, Data.Count);
109                 }
110         }
111 }