none
GetHostEntry returns local computername instead of DNS name RRS feed

  • Question

  • Hi,

    • I have a computer with a computername of "COMPUTERa".
    • It has a DNS entry with a name of "DNSa".

    If I run DNS.GetHostEntry(ipAddress_to_that_computer) on that computer, it comes back with "COMPUTERa".

    If I run the same command (same IP address) on another computer, if comes back with "DNSa".

    It appears that the code realises the IP is for the local machine, and queries the computername instead of the DNS name.

    My HOSTS file is empty. DNS has never had "COMPUTERa" as a record, so DNS cache is not a factor.

    This might be expected behaviour if you query your own IP address. However I need to check the DNS record. How can this be achieved?

    Wednesday, September 26, 2018 3:03 PM

All replies

  • Hello, 

    In .NET Dns class local name of the computer just copied from structure 

        [StructLayout(LayoutKind.Sequential, CharSet=CharSet.Unicode)]
        internal struct hostent {
            publicIntPtr h_name;
            publicIntPtr h_aliases;
            public short h_addrtype;
            public short h_length;
            publicIntPtr h_addr_list;
        }



                hostent Host = (hostent)Marshal.PtrToStructure(nativePointer, typeof(hostent));
                IPHostEntry HostEntry = new IPHostEntry();
     
                if (Host.h_name != IntPtr.Zero) {
                    HostEntry.HostName = Marshal.PtrToStringAnsi(Host.h_name);
                    GlobalLog.Print("HostEntry.HostName: " + HostEntry.HostName);
                }

    Structure hostent is one of the system thinks.

    So, if you need to get a hostname from DNS - make your own request directly to DNS server.

    Forgot... You also can check an IpHostEntry.Aliases. Something can be there.


    Sincerely, Highly skilled coding monkey.


    Wednesday, September 26, 2018 3:27 PM
  • Nothing in aliases.

    So, you are suggesting this is the expected behaviour.

    Not sure how to make request directly just now. Not sure if I want to invest time.

    Wednesday, September 26, 2018 3:44 PM
  • >Not sure how to make request directly just now. Not sure if I want to invest time.

    The same here.

    If you decide to go 

    https://referencesource.microsoft.com/#System/net/System/Net/DNS.cs,68


    Sincerely, Highly skilled coding monkey.

    Wednesday, September 26, 2018 4:04 PM