.NET2 DNS 拒绝超过 126 个字符的主机名,因为它“太长”。

发布于 2024-12-13 07:50:41 字数 176 浏览 0 评论 0原文

在开发一个程序时,我最近发现 .net 中的主机名(或至少在 ping 类中)不应超过 126 个字符。如果主机名较长,则 ping 类会引发异常。

然而 Wikipedia 规定最多允许 255 个字符。 看起来确实有主机名超过 126 个字符的机器,所以问题是:这个限制可以改变吗?谁是对的?如果不能改变如何解析名称?

While working on a program I recently found that hostnames in .net (or at least in the ping class) are not supposed to have more than 126 characters. The ping class throws an exception if a hostname is longer.

However Wikipedia states that up to 255 characters are allowed.
And it looks that indeed there are machines with a hostname longer than 126 chars out there, so the question is: can this limit be changed, who is right and how to resolve names if it cannot?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

失而复得 2024-12-20 07:50:42

两个信息都是正确的。

255 个字符的限制是指整个主机名(例如some.thing.example.com)。

反过来,每个标签(例如 examplecom)的长度限制为 63 个字符。因此,顶级域名理论上有 126 个非点字符的限制。

Both informations are right.

The 255 character limit refers to the entire hostname (e.g. some.thing.example.com).

In turn, each label (e.g. example or com) is limited to 63 characters. So top-level domains have a theoretical limit of 126 non-dot characters.

辞取 2024-12-20 07:50:42

显然就像乔尔和丹尼斯解释的那样。 .Net 无法解析长度超过 126 个字符的名称。

但是,如果有人遇到同样的问题,请查看 DNS Plus:
http://www.simpledns.com/dns-client-lib.aspx#download

Apparently it is like Joel and Dennis explained. .Net is not capable of resolving names longer than 126 chars.

However if somebody has the same problem, take a look here at DNS Plus:
http://www.simpledns.com/dns-client-lib.aspx#download

热鲨 2024-12-20 07:50:41

.NET Dns 类的主机名硬性上限为 126 个字符(针对 .NET4 检查)。

但是,您可以使用较低级别的 Win32 DnsQuery 方法使用 P/Invoke 将主机名转换为 IP 地址,然后将这些原始地址与 .NET 网络类一起使用。

这是使用此方法的示例 DnsAddr 类:

public static class DnsAddr
{
    [DllImport("dnsapi", EntryPoint = "DnsQuery_W", CharSet = CharSet.Unicode, SetLastError = true, ExactSpelling = true)]
    private static extern int DnsQuery([MarshalAs(UnmanagedType.VBByRefStr)]ref string pszName, QueryTypes wType, QueryOptions options, int         aipServers, ref IntPtr ppQueryResults, int pReserved);

    [DllImport("dnsapi", CharSet = CharSet.Auto, SetLastError = true)]
    private static extern void DnsRecordListFree(IntPtr pRecordList, int FreeType);

    public static IEnumerable<IPAddress> GetAddress(string domain)
    {
        IntPtr ptr1 = IntPtr.Zero;
        IntPtr ptr2 = IntPtr.Zero;
        List<IPAddress> list = new List<IPAddress>();
        DnsRecord record = new DnsRecord();
        int num1 = DnsAddr.DnsQuery(ref domain, QueryTypes.DNS_TYPE_A, QueryOptions.DNS_QUERY_NONE, 0, ref ptr1, 0);
        if (num1 != 0)
            throw new Win32Exception(num1);
        for (ptr2 = ptr1; !ptr2.Equals(IntPtr.Zero); ptr2 = record.pNext)
        {
            record = (DnsRecord)Marshal.PtrToStructure(ptr2, typeof(DnsRecord));
            list.Add(new IPAddress(record.ipAddress));
        }
        DnsAddr.DnsRecordListFree(ptr1, 0);
        return list;
    }

    private enum QueryOptions
    {     
        DNS_QUERY_NONE = 0,
    }

    private enum QueryTypes
    {
        DNS_TYPE_A = 1,
    }

    [StructLayout(LayoutKind.Sequential)]
    private struct DnsRecord
    {
        public IntPtr pNext;
        public string pName;
        public short wType;
        public short wDataLength;
        public int flags;
        public int dwTtl;
        public int dwReserved;
        public uint ipAddress;
    }
}

这是一个示例测试程序:

class Program
{
    static void Main(string[] args)
    {
        var addresses = DnsAddr.GetAddress("google.com");
        foreach (var address in addresses)
            Console.WriteLine(address.ToString());
    }
}

在我的机器上生成以下输出:

173.194.33.51
173.194.33.50
173.194.33.49
173.194.33.52
173.194.33.48

The .NET Dns class has a hard upper limit of 126 characters for hostnames (checked for .NET4).

However, you can use the lower-level Win32 DnsQuery method using P/Invoke to translate host names into IP addresses and then use those raw addresses with the .NET networking classes.

Here is a sample DnsAddr class using this approach:

public static class DnsAddr
{
    [DllImport("dnsapi", EntryPoint = "DnsQuery_W", CharSet = CharSet.Unicode, SetLastError = true, ExactSpelling = true)]
    private static extern int DnsQuery([MarshalAs(UnmanagedType.VBByRefStr)]ref string pszName, QueryTypes wType, QueryOptions options, int         aipServers, ref IntPtr ppQueryResults, int pReserved);

    [DllImport("dnsapi", CharSet = CharSet.Auto, SetLastError = true)]
    private static extern void DnsRecordListFree(IntPtr pRecordList, int FreeType);

    public static IEnumerable<IPAddress> GetAddress(string domain)
    {
        IntPtr ptr1 = IntPtr.Zero;
        IntPtr ptr2 = IntPtr.Zero;
        List<IPAddress> list = new List<IPAddress>();
        DnsRecord record = new DnsRecord();
        int num1 = DnsAddr.DnsQuery(ref domain, QueryTypes.DNS_TYPE_A, QueryOptions.DNS_QUERY_NONE, 0, ref ptr1, 0);
        if (num1 != 0)
            throw new Win32Exception(num1);
        for (ptr2 = ptr1; !ptr2.Equals(IntPtr.Zero); ptr2 = record.pNext)
        {
            record = (DnsRecord)Marshal.PtrToStructure(ptr2, typeof(DnsRecord));
            list.Add(new IPAddress(record.ipAddress));
        }
        DnsAddr.DnsRecordListFree(ptr1, 0);
        return list;
    }

    private enum QueryOptions
    {     
        DNS_QUERY_NONE = 0,
    }

    private enum QueryTypes
    {
        DNS_TYPE_A = 1,
    }

    [StructLayout(LayoutKind.Sequential)]
    private struct DnsRecord
    {
        public IntPtr pNext;
        public string pName;
        public short wType;
        public short wDataLength;
        public int flags;
        public int dwTtl;
        public int dwReserved;
        public uint ipAddress;
    }
}

Here is a sample test program:

class Program
{
    static void Main(string[] args)
    {
        var addresses = DnsAddr.GetAddress("google.com");
        foreach (var address in addresses)
            Console.WriteLine(address.ToString());
    }
}

which on my machine produces this output:

173.194.33.51
173.194.33.50
173.194.33.49
173.194.33.52
173.194.33.48
水晶透心 2024-12-20 07:50:41

调用 gethostbyname,然后将 IP 地址(即使对于 IPv6,也不会超过几十个字符)传递给 ping 类。

Call gethostbyname, then pass an IP address (which is never more than a couple dozen characters, even for IPv6) to the ping class.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文