tags:

views:

936

answers:

1

I found some code from the msdn site (Code included below), which looks like it will return all dns aliases for a given server. I've implemented the code in a cosole app, which should allow me to enter the host name of a server and it should return all dns alias names. I enter the host name of a server in our domain known to have aliases (I can ping the host and the aliases and they all resolve to the same IP), but this code does not find the alias names. Obvously my understanding of dns aliases and/or the code is lacking... please educate me...

static void Main(string[] args)
{
    Console.Write("Host? (Enter for local): ");
    string strHost = Console.ReadLine();
    if (strHost.Trim().Length == 0)
    {
        strHost = System.Net.Dns.GetHostName();
    }

    try
    {
        //System.Net.IPAddress hostIPAddress = System.Net.IPAddress.Parse(strHost);
        System.Net.IPHostEntry hostInfo = System.Net.Dns.GetHostByName(strHost);//.GetHostByAddress(hostIPAddress);
        // Get the IP address list that resolves to the host names contained in 
        // the Alias property.
        System.Net.IPAddress[] address = hostInfo.AddressList;
        // Get the alias names of the addresses in the IP address list.
        String[] alias = hostInfo.Aliases;

        Console.WriteLine("Host name : " + hostInfo.HostName);
        Console.WriteLine("\nAliases :");
        for (int index = 0; index < alias.Length; index++)
        {
            Console.WriteLine(alias[index]);
        }
        Console.WriteLine("\nIP address list : ");
        for (int index = 0; index < address.Length; index++)
        {
            Console.WriteLine(address[index]);
        }
    }
    catch (System.Net.Sockets.SocketException e)
    {
        Console.WriteLine("SocketException caught!!!");
        Console.WriteLine("Source : " + e.Source);
        Console.WriteLine("Message : " + e.Message);
    }
    catch (FormatException e)
    {
        Console.WriteLine("FormatException caught!!!");
        Console.WriteLine("Source : " + e.Source);
        Console.WriteLine("Message : " + e.Message);
    }
    catch (ArgumentNullException e)
    {
        Console.WriteLine("ArgumentNullException caught!!!");
        Console.WriteLine("Source : " + e.Source);
        Console.WriteLine("Message : " + e.Message);
    }
    catch (Exception e)
    {
        Console.WriteLine("Exception caught!!!");
        Console.WriteLine("Source : " + e.Source);
        Console.WriteLine("Message : " + e.Message);
    }

    Console.WriteLine("Any key to continue...");
    Console.ReadKey();
}
+1  A: 

For a DNS name, the list of aliases will only be non-empty if the name you are querying has a CNAME record; then, the alias list will give you all CNAMEs that had to be resolved in order to get the ultimate name.

Consider the following problems:

  • it is not possible (i.e. the protocol does not support it) to find out all CNAMEs for a given name. This is infeasible, as it would require searching the entire global DNS.
  • aliasing may not only occur by CNAMEs, but can also happen by multiple host names having the same address (A) record. In this case, it's not that one is alias of another, but they just point to the same IP address. Again, the protocol doesn't support finding all A records for an IP address (although reverse lookup may find some).
Martin v. Löwis