public DnsQuery(string name, DnsQType qtype, DnsQClass qclass) { if (String.IsNullOrEmpty(name)) { throw new ArgumentNullException("name"); } int length = DnsUtil.GetEncodedLength(name); if (length == -1) { throw new ArgumentException("Invalid DNS name", "name"); } length += 12 + 2 + 2; // Header + qtype + qclass packet = new byte [length]; header = new DnsHeader(packet, 0); position = 12; WriteDnsName(name); WriteUInt16((ushort)qtype); WriteUInt16((ushort)qclass); Header.QuestionCount = 1; Header.IsQuery = true; Header.RecursionDesired = true; }
protected void WriteDnsName(string name) { if (!DnsUtil.IsValidDnsName(name)) { throw new ArgumentException("Invalid DNS name"); } if (!String.IsNullOrEmpty(name)) { int len = name.Length; int label_start = 0; int label_len = 0; for (int i = 0; i < len; i++) { char c = name [i]; if (c != '.') { label_len++; } else { if (i == 0) { break; // "." } WriteLabel(name, label_start, label_len); label_start += label_len + 1; // Skip the dot label_len = 0; } } if (label_len > 0) { WriteLabel(name, label_start, label_len); } } packet [position++] = 0; }
protected internal static string ReadName(byte [] buffer, ref int offset) { return(DnsUtil.ReadName(buffer, ref offset)); }
protected internal string ReadName(ref int offset) { return(DnsUtil.ReadName(packet, ref offset)); }