/// <summary> /// Initialises a new instance of the DnsQuestion class and specifies /// the class of query, the query type and the domain being queried. /// </summary> /// <param name="name">The domain to query.</param> /// <param name="type">The type of query.</param> /// <param name="cls">The class of the query.</param> /// <exception cref="System.ArgumentNullException"> /// Thrown when <paramref name="name"/> is <see langword="null"/>. /// </exception> public DnsQuestion(DnsName name, DnsQueryType type, DnsQueryClass cls) { Guard.NotNull(name, "name"); _class = cls; _type = type; _name = name; }
public void ctor_sets_correct_properties() { DnsQueryClass cls = DnsQueryClass.IN; DnsQueryType type = DnsQueryType.A; DnsName name = DnsName.Parse("test.com"); DnsQuestion question = new DnsQuestion(name, type, cls); Assert.AreEqual(cls, question.Class); Assert.AreEqual(type, question.Type); Assert.AreEqual(name, question.Name); }
/// <summary> /// Initialises a new instance of the DnsQuestion class and the reply /// reader from which the question will be decoded. /// </summary> /// <param name="reader">The reader.</param> /// <exception cref="System.ArgumentNullException"> /// Thrown when <paramref name="reader"/> is <see langword="null"/>. /// </exception> /// <exception cref="AK.Net.Dns.DnsFormatException"> /// Thrown when question data could not be read from the /// <paramref name="reader"/>. /// </exception> public DnsQuestion(IDnsReader reader) { Guard.NotNull(reader, "reader"); try { _name = reader.ReadName(); _type = reader.ReadQueryType(); _class = reader.ReadQueryClass(); } catch (DnsException exc) { throw Guard.InvalidDnsQuestionFormat(exc); } }
/// <summary> /// Returns a <see cref="System.String"/> representation of the specified /// <see cref="AK.Net.Dns.DnsQueryClass"/>. /// </summary> /// <param name="value">The class.</param> /// <returns>A <see cref="System.String"/> representation of the specified /// <see cref="AK.Net.Dns.DnsQueryClass"/>.</returns> public static string ToString(DnsQueryClass value) { switch (value) { case DnsQueryClass.IN: return("IN"); case DnsQueryClass.CH: return("CH"); case DnsQueryClass.HS: return("HS"); case DnsQueryClass.Any: return("ANY"); default: #if DEBUG throw Guard.ArgumentOutOfRange("value"); #else return(value.ToString().ToUpperInvariant()); #endif } }
private void DoSetCommand(string[] args) { switch (args[0].ToLowerInvariant()) { case "suffix": DnsName suffix = null; if (args.Length == 2 && !DnsName.TryParse(args[1], out suffix)) { WriteUsage("value: '{0}' is not a valid name", args[1]); return; } this.NameSuffix = suffix; WriteLine("suffix set: {0}", this.NameSuffix); break; case "qclass": DnsQueryClass qclass = DnsQueryClass.IN; if (args.Length == 2 && !TryParseEnum(args[1], out qclass)) { WriteUsage("value: '{0}' is not a valid qclass", args[1]); return; } this.DefaultQueryClass = qclass; WriteLine("qclass set: {0}", DnsUtility.ToString(this.DefaultQueryClass)); break; case "qtype": DnsQueryType qtype = DnsQueryType.A; if (args.Length == 2 && !TryParseEnum(args[1], out qtype)) { WriteUsage("value: '{0}' is not a valid qtype", args[1]); return; } this.DefaultQueryType = qtype; WriteLine("qtype set: {0}", DnsUtility.ToString(this.DefaultQueryType)); break; case "reverse": ParseBooleanOption(NDigOptions.ReverseAddrs, "reverse", args); break; case "sort": ParseBooleanOption(NDigOptions.SortRecords, "sort", args); break; case "header": ParseBooleanOption(NDigOptions.WriteHeader, "header", args); break; case "question": ParseBooleanOption(NDigOptions.WriteQuestion, "question", args); break; case "answer": ParseBooleanOption(NDigOptions.WriteAnswer, "answer", args); break; case "authority": ParseBooleanOption(NDigOptions.WriteAuthority, "authority", args); break; case "additional": ParseBooleanOption(NDigOptions.WriteAdditional, "additional", args); break; case "stats": ParseBooleanOption(NDigOptions.WriteStats, "stats", args); break; case "noempty": ParseBooleanOption(NDigOptions.SuppressEmptySections, "noempty", args); break; case "nologo": ParseBooleanOption(NDigOptions.SuppressLogo, "nologo", args); break; default: WriteUsage("opt: '{0}' is not a valid option", args[0]); return; } }
private void DoQueryCommand(string[] args) { DnsName qname; DnsQueryType ttype; DnsQueryType qtype = this.DefaultQueryType; DnsQueryClass tclass; DnsQueryClass qclass = this.DefaultQueryClass; string qnameArg = args[args.Length - 1]; IPAddress taddr; IPEndPoint qserver = null; for (int i = 0; i < args.Length - 1; ++i) { if (args[i][0] == '@') { int serverIndex; string arg = args[i].Substring(1); if (int.TryParse(arg, out serverIndex)) { if (serverIndex < 0 || serverIndex > this.Resolver.Servers.Count - 1) { WriteUsage("@server: {0} is not a valid forward server index (0-{1})", arg, this.Resolver.Servers.Count - 1); return; } qserver = this.Resolver.Servers[serverIndex]; } else if (IPAddress.TryParse(arg, out taddr) || TryGetHostAddress(arg, out taddr)) { qserver = new IPEndPoint(taddr, DnsTransport.DnsPort); } else { WriteUsage("@server: '{0}' is not a valid server/ip/index or it could not be resolved", arg); return; } continue; } if (TryParseEnum(args[i], out ttype)) { qtype = ttype; continue; } if (TryParseEnum(args[i], out tclass)) { qclass = tclass; continue; } WriteUsage("qtype,qclass: '{0}' is not a value qtype or qclass", args[i]); return; } if (qtype == DnsQueryType.Ptr) { if (!IPAddress.TryParse(qnameArg, out taddr)) { WriteUsage("qname: '{0}' is not a valid ip address", qnameArg); return; } qname = PtrRecord.MakeName(taddr); } else if (DnsName.TryParse(qnameArg, out qname)) { if (qname.Kind == DnsNameKind.Relative && this.NameSuffix != null) { qname = qname.Concat(this.NameSuffix); } } else { WriteUsage("qname: '{0}' is not a valid qname", qnameArg); return; } DoQueryCommand(new DnsQuestion(qname, qtype, qclass), qserver); }
/// <summary> /// Writes a <see cref="AK.Net.Dns.DnsQueryClass"/> to the underlying stream. /// </summary> /// <param name="value">The value to write.</param> /// <exception cref="System.ObjectDisposedException"> /// Thrown when the writer has been disposed of. /// </exception> public void WriteQueryClass(DnsQueryClass value) { WriteUInt16((ushort)value); }