1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141
|
//
// Mono.Net.Dns.DnsUtil
//
// Authors:
// Gonzalo Paniagua Javier (gonzalo.mono@gmail.com)
//
// Copyright 2011 Gonzalo Paniagua Javier
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
using System;
using System.Collections.Generic;
using System.Text;
namespace Mono.Net.Dns {
static class DnsUtil {
// RFC 2181 - Section 11
public static bool IsValidDnsName (string name)
{
if (name == null)
return false;
int len = name.Length;
if (len > 255)
return false;
int part_length = 0;
for (int i = 0; i < len; i++) {
char c = name [i];
if (c == '.') {
if (i == 0 && len > 1)
return false; // Can't begin with a dot unless it's "."
if (i > 0 && part_length == 0)
return false; // No ".." allowed
part_length = 0;
continue;
}
part_length++;
if (part_length > 63)
return false;
}
return true;
}
public static int GetEncodedLength (string name)
{
if (!IsValidDnsName (name))
return -1;
if (name == String.Empty)
return 1;
int len = name.Length;
if (name [len - 1] == '.')
return len + 1; // (length + label + ... + \0)
return len + 2; // need 1 more for the second to last label length
}
public static int GetNameLength (byte [] buffer)
{
return GetNameLength (buffer, 0);
}
public static int GetNameLength (byte [] buffer, int offset)
{
if (buffer == null)
throw new ArgumentNullException ("buffer");
if (offset < 0 || offset >= buffer.Length)
throw new ArgumentOutOfRangeException ("offset");
int i = 0;
int len = 0;
while (len < 256) {
i = buffer [offset++];
if (i == 0)
return len > 0 ? --len : 0;
int ptr = i & 0x0C0;
if (ptr == 0x0C0) {
i = ((ptr & 0x3F) << 8) + buffer[offset++];
offset = i;
continue;
} else if (ptr >= 0x40) {
return -2; // Invalid ptr
}
len += i + 1;
offset += i;
}
return -1; // Invalid length
}
public static string ReadName (byte [] buffer, ref int offset)
{
if (buffer == null)
throw new ArgumentNullException ("buffer");
if (offset < 0 || offset >= buffer.Length)
throw new ArgumentOutOfRangeException ("offset");
StringBuilder sb = new StringBuilder (32);
int i = 0;
bool no_ptr = true;
int off = offset;
while (sb.Length < 256) {
i = buffer [off++];
if (no_ptr) offset++;
if (i == 0) {
if (sb.Length > 0)
sb.Length--;
return sb.ToString ();
}
int ptr = i & 0x0C0;
if (ptr == 0x0C0) {
i = ((ptr & 0x3F) << 8) + buffer [off];
if (no_ptr) offset++;
no_ptr = false;
off = i;
continue;
} else if (i >= 0x40) {
return null; // Invalid ptr
}
for (int k = 0; k < i; k++)
sb.Append ((char) buffer [off + k]);
sb.Append ('.');
off += i;
if (no_ptr) offset += i;
}
return null; // never reached
}
}
}
|