Exemplo n.º 1
0
        /// <summary>
        /// Gets DNS host information for an IP address.
        /// </summary>
        public StatusCode GetHostByAddress(IPv4 address,
                                           out IPv4HostEntry hostEntry)
        {
            hostEntry = null;

            IPv4 dnsServer = DnsServer;

            if (dnsServer == IPv4.Zero)
            {
                return(StatusCode.NoNameServer);
            }

            // Formulate request a.b.c.d -> d.b.c.a
            IPv4   reversed = new IPv4(ByteOrder.Swap((uint)address));
            string name     = String.Format("{0}.IN-ADDR.ARPA", reversed);

            DebugPrint("Looking up {0}\n", name);

            Dns.Query  q = new Dns.Query(name, Dns.Type.PTR, Dns.Class.Internet);
            Dns.Format answer;

            StatusCode askResponse = Ask(dnsServer, q, out answer);

            if (askResponse != StatusCode.Success)
            {
                return(askResponse);
            }
            assert answer != null;

            if (answer.GetRCode() != Dns.RCode.NoError)
            {
                DebugPrint("Dns query failed: RCode = {0}\n", answer.GetRCode());
                return(StatusCode.RequestFailed);
            }

            hostEntry = new IPv4HostEntry(new string[] {}, new IPv4 [] { address });

            ArrayList aliases = new ArrayList();

            foreach (Dns.ResourceRecord !rr in answer.AnswerRRs)
            {
                DebugPrint("Answer: Type = {0} Class = {1} TTL = {2}\n",
                           rr.Type, rr.Class, rr.TtlSeconds);
                if ((rr.Type != Dns.Type.PTR && rr.Type != Dns.Type.CNAME) ||
                    rr.Class != Dns.Class.Internet)
                {
                    continue;
                }

                byte [] rdata = rr.RData;
                if (rdata == null)
                {
                    continue;
                }

                int offset = 0;
                while (offset != rdata.Length)
                {
                    string alias;
                    offset += Dns.LabelEncoding.GetString(rdata, offset,
                                                          out alias);
                    aliases.Add(alias);
                }
            }
            hostEntry.Aliases = (string [] !)aliases.ToArray(typeof(string));

            return(StatusCode.Success);
        }
Exemplo n.º 2
0
 public StatusCode Ask(IPv4 dnsServer,
                       Dns.Query query,
                       out Dns.Format answer)
 {
     return(Ask(dnsServer, new Dns.Query [] { query }, out answer));
 }
Exemplo n.º 3
0
        /// <summary>
        /// Gets the DNS information for the specified DNS host name.
        /// </summary>
        public StatusCode GetHostByName(string name,
                                        out IPv4HostEntry hostEntry)
        {
            hostEntry = null;

            IPv4 dnsServer = DnsServer;

            if (dnsServer == IPv4.Zero)
            {
                return(StatusCode.NoNameServer);
            }

            Dns.Query  q = new Dns.Query(name, Dns.Type.A, Dns.Class.Internet);
            Dns.Format answer;

            StatusCode askResponse = Ask(dnsServer, q, out answer);

            if (askResponse != StatusCode.Success)
            {
                return(askResponse);
            }
            assert answer != null;

            if (answer.GetRCode() != Dns.RCode.NoError)
            {
                DebugPrint("Dns query failed: RCode = {0}\n", answer.GetRCode());
                return(StatusCode.RequestFailed);
            }

            hostEntry = new IPv4HostEntry(new string [] { name }, new IPv4[] {});
            ArrayList addressList = new ArrayList();

            foreach (Dns.ResourceRecord !rr in answer.AnswerRRs)
            {
                if (rr.Type != Dns.Type.A || rr.Class != Dns.Class.Internet)
                {
                    continue;
                }

                byte [] rdata = rr.RData;
                if (rdata == null)
                {
                    continue;
                }

                if ((rdata.Length % IPv4.Length) != 0)
                {
                    continue;
                }

                int offset = 0;
                while (offset != rdata.Length)
                {
                    addressList.Add(IPv4.ParseBytes(rdata, offset));
                    offset += 4;
                }
            }

            IPv4[] al = hostEntry.AddressList = new IPv4 [addressList.Count];
            for (int i = 0; i < addressList.Count; i++)
            {
                al[i] = (IPv4)(!)addressList[i];
            }

            return(StatusCode.Success);
        }