Add support for an alternative managed DNS resolver
[mono.git] / mcs / class / System / Mono.Dns / DnsPacket.cs
1 //
2 // Mono.Dns.DnsPacket
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.Text;
24 using System.IO;
25
26 namespace Mono.Dns {
27 #if !NET_2_0
28         public
29 #endif
30         abstract class DnsPacket {
31                 protected byte [] packet;
32                 protected int position;
33                 protected DnsHeader header;
34
35                 protected DnsPacket ()
36                 {
37                         // Caller has to initialize packet, position and header
38                 }
39
40                 protected DnsPacket (int length)
41                         : this (new byte [length], length)
42                 {
43                 }
44
45                 protected DnsPacket (byte [] buffer, int length)
46                 {
47                         if (buffer == null)
48                                 throw new ArgumentNullException("buffer");
49                         if (length <= 0)
50                                 throw new ArgumentOutOfRangeException("length", "Must be greater than zero.");
51
52                         packet = buffer;
53                         position = length;
54                         header = new DnsHeader(new ArraySegment<byte>(packet, 0, 12));
55                 }
56
57                 public byte [] Packet {
58                         get { return packet; }
59                 }
60
61                 public int Length {
62                         get { return position; }
63                 }
64
65                 public DnsHeader Header {
66                         get { return header; }
67                 }
68
69                 protected void WriteUInt16 (ushort v)
70                 {
71                         packet [position++] = (byte) ((v & 0x0ff00) >> 8);
72                         packet [position++] = (byte) (v & 0x0ff);
73                 }
74
75                 protected void WriteStringBytes (string str, int offset, int count)
76                 {
77                         for (int i = offset, c = 0; c < count; c++, i++)
78                                 packet [position++] = (byte) str [i]; // Don't care about encoding.
79                 }
80
81                 protected void WriteLabel (string str, int offset, int count)
82                 {
83                         packet [position++] = (byte) count;
84                         WriteStringBytes (str, offset, count);
85                 }
86
87                 protected void WriteDnsName (string name)
88                 {
89                         if (!DnsUtil.IsValidDnsName (name))
90                                 throw new ArgumentException ("Invalid DNS name");
91
92                         if (!String.IsNullOrEmpty (name)) {
93                                 int len = name.Length;
94                                 int label_start = 0;
95                                 int label_len = 0;
96                                 for (int i = 0; i < len; i++) {
97                                         char c = name [i];
98                                         if (c != '.') {
99                                                 label_len++;
100                                         } else {
101                                                 if (i == 0)
102                                                         break; // "."
103                                                 WriteLabel (name, label_start, label_len);
104                                                 label_start += label_len + 1; // Skip the dot
105                                                 label_len = 0;
106                                         }
107                                 }
108                                 if (label_len > 0)
109                                         WriteLabel (name, label_start, label_len);
110                         }
111
112                         packet [position++] = 0;
113                 }
114
115                 protected internal string ReadName (ref int offset)
116                 {
117                         return DnsUtil.ReadName (packet, ref offset);
118                 }
119
120                 protected internal static string ReadName (byte [] buffer, ref int offset)
121                 {
122                         return DnsUtil.ReadName (buffer, ref offset);
123                 }
124
125                 protected internal ushort ReadUInt16 (ref int offset)
126                 {
127                         return (ushort)((packet[offset++] << 8) + packet[offset++]);
128                 }
129
130                 protected internal int ReadInt32 (ref int offset)
131                 {
132                         return (packet [offset++] << 24) + (packet [offset++] << 16) + (packet [offset++] << 8) + packet [offset++];
133                 }
134         }
135 }
136