【问题标题】:How to Connect to a DNS Server using ARSoft.Tools.Net.Core DNSClient, IPAddress and port number如何使用 ARSoft.Tools.Net.Core DNSClient、IPAddress 和端口号连接到 DNS 服务器
【发布时间】:2023-03-09 04:21:01
【问题描述】:

我有 2 个 DNS 服务器(domainserverwithport.com:5356 和 domainserverwithoutport.com)我正在连接到一个查找请求。一个有端口号,而另一个没有。

没有端口号的连接并从请求中获得答案。但第二个什么也不返回。 在使用 ARSoft.Tools.Net.Core 时,我看不到使用 IP 地址和端口号实例化新 DNSClient 的选项。请问有没有办法解决这个问题?

我为没有端口号的 DNS 服务器做了以下操作。

var ip = Dns.GetHostEntry("domainserverwithoutport.com");
            Console.WriteLine(ip.AddressList[0]);
            var newIp = ip.AddressList[0];

       IPAddress iPAddress = IPAddress.Parse(newIp.ToString());
  var result = EnumDnsQuery(iPAddress.ToString(), "8.7.4.3.9.5.3.1.0.9.4.3.2.e164.arpa.4.couretech.0ceed", RecordType.Naptr, 0, 5000);

还有 EnumDnsQuery 方法

public static EnumDnsResponse EnumDnsQuery(string serverIp, string domain,
            RecordType recordType, int maximumRetries = 0, int queryTimeout = QUERY_TIMEOUT_MILLISECONDS)
        {

            int retriesToUse = maximumRetries > 0 ? maximumRetries : 0;
            DnsMessage dnsMessage = null;
            TimeSpan duration = TimeSpan.Zero;



            IPAddress ipAddress = IPAddress.Parse(serverIp);

            DomainName domainName = DomainName.Parse(domain);

            var stopwatch = new Stopwatch();
            DnsClient client = new DnsClient(ipAddress, queryTimeout);

            int attempts = 0;

            stopwatch.Start();
            while (dnsMessage == null && attempts <= retriesToUse)
            {
                attempts++;
                dnsMessage = client.Resolve(domainName, recordType);
            }
            stopwatch.Stop();

            string rawMessage = string.Empty;

            if (dnsMessage != null && recordType == RecordType.Naptr)
            {
                var naptrRecords = dnsMessage.AnswerRecords.Where(x => x.GetType() == typeof(NaptrRecord))
                    .Select(x => (NaptrRecord)x)
                    .Where(x => x.Services.Equals("E2U+pstn:tel"))
                    .OrderByDescending(x => x.Order);

                var naptrRecord = naptrRecords.FirstOrDefault();
                if (naptrRecord != null)
                    rawMessage = naptrRecord.RegExp.Trim('!');
            }
            else if (dnsMessage != null && recordType == RecordType.Txt)
            {
                var txtRecords = dnsMessage.AnswerRecords.Where(x => x.GetType() == typeof(TxtRecord))
                   .Select(x => (TxtRecord)x);

                var txtRecord = txtRecords.FirstOrDefault();
                if (txtRecord == null)
                    rawMessage = txtRecord.TextData.Trim('!');
            }

            return new EnumDnsResponse(
                dnsMessage: dnsMessage,
                rawMessage: rawMessage,
                totalDuration: stopwatch.Elapsed,
                attempts: attempts);
        }

上面的代码返回一个 DnsMessage。 但是当我为另一个需要端口号的域服务器做同样的事情时,DnsMessage 变为空。请问有没有办法解决这个问题?

【问题讨论】:

    标签: c# .net-core dns


    【解决方案1】:

    阅读https://github.com/mccj/ARSoft.Tools.Net.Core/blob/master/ARSoft.Tools.Net.Core/Dns/DnsClientBase.cs的代码,你可以看到有一个内部构造函数接受一个端口号......

    在这里https://github.com/mccj/ARSoft.Tools.Net.Core/blob/master/ARSoft.Tools.Net.Core/Dns/DnsClient.cs 在第 81 行,我们可以看到一个可访问构造函数的示例,该构造函数访问内部构造函数,将 53 作为硬编码值传入(即,如果您是使用提供的 DnsClient)

    似乎没有办法在实例化 DnsClient 时或之后(例如通过属性)更改端口号,并且您不能自己继承 DnsClientBase 子类,因为您想要的相关构造函数在另一个内部组装和不可访问。如果您迫切需要这样做,您可能必须自己克隆 ARSoft.Tools.Net.Core 存储库并向 DnsClient 添加构造函数

    值得注意的是端口设置是客户端范围的;没有在端口 53 上拥有服务器 A 和在端口 5353 上拥有服务器 B 的概念,并且这两个服务器都为同一个客户端所知。该端口在实例化时是固定的,适用于该 DnsClientBase 的已知列表中的所有服务器

    您还可以通过写信给包作者并询问有关为什么如此安排代码的更多信息,因为这似乎故意使外部使用自定义端口变得困难,但在内部,所有必要的胆量都是在那里使它成为一个变量设置

    【讨论】:

    • 谢谢。我不得不拉出 ARSoft 项目,将其添加到我的项目中并更改了端口。
    猜你喜欢
    • 1970-01-01
    • 2021-10-03
    • 1970-01-01
    • 2012-10-26
    • 1970-01-01
    • 1970-01-01
    • 2016-12-13
    • 2015-01-16
    • 1970-01-01
    相关资源
    最近更新 更多