【问题标题】:C# : Resolve IP into host name when a host has more than 1 ipC#:当主机拥有超过 1 个 ip 时,将 IP 解析为主机名
【发布时间】:2016-08-08 05:07:10
【问题描述】:

这是我关于 StackOverflow 的第一个问题。所以,如果有任何错误,请忽略它们。如果您在理解我的问题时遇到问题,请告诉我,我会尽力解释我的问题:)。

目前我正在尝试使用 pcap.net in c# 创建一个数据包嗅探器。我在将 IP 地址解析为主机名时遇到了问题。

这是我的问题: 当一个网站的 ip 地址超过 1 个时,例如当我在命令提示符下键入命令时

nslookup yahoo.com

我得到以下输出

DNS request timed out.
    timeout was 2 seconds.
*** Can't find server name for address 192.168.0.1: Timed out
Server: google-public-dns-a.google.com
Address: 8.8.8.8

Non-authorative answer:
Name: yahoo.com
Addresses: 206.190.36.45, 98.139.183.24, 98.138.253.109

所以,很明显我有不止 1 个 yahoo.com 的 IP 地址。

现在,我正在使用以下代码,它适用于具有 1 个 ip 地址的主机(例如,当我将 ip 源/目标设置为 8.8.8.8 时,它会给我结果google-public-dns-a.google.com)

private void PacketHandler(Packet packet)
{
        IpV4Datagram ip = packet.Ethernet.IpV4; 

        string sourceHost = ip.Source.ToString();
        string destinationHost = ip.Destination.ToString();

        #region Code to get the host name of the source ip and the destination ip

        string tempSourceIP = sourceHost;
        string tempDestinationIP = destinationHost;
        IPHostEntry source_ipHostEntry, destination_ipHostEntry;
        try
        {
            source_ipHostEntry = Dns.GetHostEntry(sourceHost);
            destination_ipHostEntry = Dns.GetHostEntry(destinationHost);
            sourceHost = tempSourceIP + "   ::   (" + source_ipHostEntry.HostName + ")";
            destinationHost = tempDestinationIP + "   ::   (" + destination_ipHostEntry.HostName + ")";

        }
        catch (Exception)
        {
            sourceHost = tempSourceIP;
            destinationHost = tempDestinationIP;
        }
        #endregion
}

我将结果 sourceHost 和 destinationHost 添加到 GUI(准确地说是数据网格)以显示结果。

我的问题是 - 有没有办法找出源 IP 或目标 IP 是否属于未知主机的 IP 列表(例如,如果我的源 IP 或目标 IP 是 206.190。 36.45,我怎么知道这个IP是属于yahoo.com的)?

更新:我在网上冲浪以寻找解决问题的方法,我找到了这个链接How to retrieve a list of websites from an IP address?。这部分解决了我的问题,因为即使按照此方法,仍有一些 IP 地址无法解析为其主机名。而且由于我正在制作数据包嗅探器,因此这种方法可能不是最好的方法。因为我想在 GUI 中加载值时实时向用户显示 IP 地址的主机名(我的意思是我的数据网格)。我很高兴知道 - 如果链接中提供的解决方案是解决我的问题的唯一方法。

【问题讨论】:

    标签: c# networking dns pcap.net


    【解决方案1】:

    你可以试试 rDNS。

    https://en.wikipedia.org/wiki/Reverse_DNS_lookup

    但正如那里提到的:

    并非所有 IP 地址都有反向条目。

    【讨论】:

    • 您的链接很有帮助。谢谢。 “并非所有 IP 地址都有反向条目” - 实际上,这就是我的代码有限制的原因。 nslookup 也是如此。但我有一个问题 - domaintools 之类的网站如何解析 ips?如果他们使用某种文件,我在哪里可以获得该文件?其实这就是我开始思考并提出这个问题的原因。
    • 我试图支持你的答案,但由于我的声誉,它不起作用。请不要介意。
    • 我猜这个网站保留了某种反向表格。请注意,它们也有限制:“反向 IP 查找工具的限制:结果包括所有 .com、.net、.org、.biz、.us 和 .info 域以及我们知道的 IP 上列出的任何其他 gTLD 或 ccTLD 域关于。添加到 IP 地址的域每天都会更新。从任何特定 IP 地址中删除的域最多可能会在两周内得到反映。"
    • 谢谢。这对我很有帮助。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-03-12
    • 1970-01-01
    • 2013-11-29
    • 1970-01-01
    • 2021-10-08
    • 2011-05-07
    • 2019-09-29
    相关资源
    最近更新 更多