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