Ejemplo n.º 1
0
 public static List <IPAddress> GetHostAddresses(Resolver r, string domain, string NSServer)
 {
     try
     {
         Resolver currentResolver = r.Clone();
         currentResolver.DnsServer = NSServer;
         List <IPAddress> lst      = new List <IPAddress>();
         Response         response = currentResolver.Query(domain, QType.A, QClass.IN);
         foreach (RecordA rA in response.RecordsA)
         {
             lst.Add(rA.Address);
         }
         response = currentResolver.Query(domain, QType.AAAA, QClass.IN);
         foreach (RecordAAAA rAAAA in response.RecordsAAAA)
         {
             lst.Add(rAAAA.Address);
         }
         return(lst);
     }
     catch
     {
         return(new List <IPAddress>());
     }
 }
Ejemplo n.º 2
0
        /// <summary>
        /// Devuelve todos los DNS que funcionen para un determinado dominio
        /// </summary>
        /// <param name="r"></param>
        /// <param name="domain"></param>
        /// <param name="NSServer"></param>
        /// <param name="previosNSrecords"></param>
        /// <returns></returns>
        public static ICollection <string> GetNSServer(Resolver r, string domain, string NSServer, ICollection <string> previosNSrecords = null)
        {
            try
            {
                Resolver         currentResolver = r.Clone();
                HashSet <string> ips             = new HashSet <string>(StringComparer.OrdinalIgnoreCase);
                if (previosNSrecords == null)
                {
                    previosNSrecords = new HashSet <string>(StringComparer.OrdinalIgnoreCase);
                }
                previosNSrecords.Add(NSServer);

                currentResolver.DnsServer = NSServer;
                Response response = currentResolver.Query(domain, QType.NS);
                if (response.RecordsNS.Length > 0)
                {
                    HashSet <string> nameservers = new HashSet <string>();
                    //No es autoritativa, volver a preguntar
                    if (!response.header.AA)
                    {
                        nameservers.UnionWith(response.RecordsNS.Where(p => !previosNSrecords.Contains(p.NSDNAME)).Select(p => p.NSDNAME));
                        foreach (string rNS in nameservers)
                        {
                            ips.UnionWith(GetNSServer(currentResolver, domain, rNS, previosNSrecords));
                        }
                    }
                    else
                    {
                        foreach (RecordNS rNS in response.RecordsNS)
                        {
                            nameservers.Add(RemoveLastPoint(rNS.NSDNAME));
                        }
                    }

                    //Resuelve los dominios de los DNS
                    foreach (string ns in nameservers)
                    {
                        foreach (IPAddress ip in DNSUtil.GetHostAddresses(ns))
                        {
                            if (!ips.Contains(ip.ToString()))
                            {
                                if (TestDNS(ip.ToString()))
                                {
                                    ips.Add(ip.ToString());
                                }
                            }
                        }
                    }
                    return(ips);
                }
                //Hay servidores autoritativos para esta petición
                else if (response.Authorities.Count > 0)
                {
                    try
                    {
                        //Se devuelve el servidor DNS autoritativo
                        if (response.Authorities[0].RECORD is RecordSOA recordSOA)
                        {
                            string dns = RemoveLastPoint(recordSOA.MNAME);
                            if (TestDNS(dns))
                            {
                                return(new List <string>()
                                {
                                    dns
                                });
                            }
                        }
                        if (response.Authorities[0].RECORD is RecordNS recordNS)
                        {
                            string dns = RemoveLastPoint(recordNS.NSDNAME);
                            if (TestDNS(dns))
                            {
                                return(new List <string>()
                                {
                                    dns
                                });
                            }
                        }
                    }
                    catch { }
                }
            }
            catch { }
            return(new List <string>());
        }