Example #1
0
        /// <summary>
        /// Resolves and returns an <see cref="System.Net.IPHostEntry"/> for the
        /// specified host name or <see cref="System.Net.IPAddress"/>.
        /// </summary>
        /// <param name="hostOrAddress">A host name or IP address.</param>
        /// <returns>
        /// An <see cref="System.Net.IPHostEntry"/> containing information
        /// associated with the host.
        /// </returns>
        /// <exception cref="System.ArgumentNullException">
        /// Thrown when <paramref name="hostOrAddress"/> is <see langword="null"/>.
        /// </exception>
        /// <exception cref="System.ArgumentException">
        /// Thrown when <paramref name="hostOrAddress"/> is not a valid DNS name
        /// or IP address.
        /// </exception>
        /// <exception cref="AK.Net.Dns.DnsTransportException">
        /// Thrown when a transport error occurs.
        /// </exception>
        /// <exception cref="AK.Net.Dns.DnsResolutionException">
        /// Thrown when an error occurs during the resolution, such as the query
        /// not being answered.
        /// </exception>
        public virtual IPHostEntry GetHostEntry(string hostOrAddress)
        {
            Guard.NotEmpty(hostOrAddress, "hostOrAddress");

            DnsName   qname;
            IPAddress address;

            if (IPAddress.TryParse(hostOrAddress, out address))
            {
                return(GetHostEntry(address));
            }
            else if (DnsName.TryParse(hostOrAddress, out qname))
            {
                DnsReply reply;

                qname = AppendNameSuffix(qname);
                reply = Resolve(new DnsQuestion(qname, DnsQueryType.A, DnsQueryClass.IN));
                if (reply.Answers.Count > 0)
                {
                    return(new IPHostEntry()
                    {
                        HostName = (from cn in reply.Answers
                                    where F.IsCName(cn) && cn.Owner.Equals(qname)
                                    select F.ToCName(cn).Canonical).FirstOrDefault() ?? qname,
                        AddressList = (from a in reply.Answers
                                       where F.IsAOrAaaa(a)
                                       select F.ToIP(a)).ToArray(),
                        Aliases = DnsUtility.EMPTY_STRING_ARRAY
                    });
                }
                else
                {
                    return(new IPHostEntry()
                    {
                        HostName = qname,
                        AddressList = DnsUtility.EMPTY_IP_ARRAY,
                        Aliases = DnsUtility.EMPTY_STRING_ARRAY
                    });
                }
            }

            throw Guard.MustBeAnIPAddressOrDnsName("hostOrAddress");
        }
Example #2
0
        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;
            }
        }
Example #3
0
        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);
        }