DNS


Now that you have a basic understanding of IPv4 and IPv6 addressing, you probably can see that IP addresses are computer friendly but not very user friendly because of their numeric form, which is hard to remember. Imagine trying to browse the Web and having to remember long numerical strings of your favorite Web sites. Most people would much rather go to a Web site by memorizing a friendly name such as www.microsoft.com than navigate using a numerically formed IP address. IP networking features DNS, which is designed to associate one or more IP addresses with names and is normally referred to as DNS name resolution .

The .NET Framework features a Dns class in the System.Net namespace that s designed to perform DNS name resolution. The Dns class can perform both forward and reverse name lookup to a DNS server. Forward name lookup is resolving a name to one or more IP addresses, and reverse name lookup is resolving an IP address to a name. Forward name lookup is the most common name resolution technique used in network applications. One of the biggest benefits of using forward name lookup is that you can practically ignore the details of IP addressing in your network application.

Forward Name Lookup

The Dns class provides the GetHostByName and the Resolve methods to perform forward DNS name lookup. Both methods accept a string parameter representing a name to look up, and both return an IPHostEntry object that will return a list of one or more IPv4 or IPv6 addresses if the name was able to be resolved. Also, the IPHostEntry object will return any alias names that can be associated with the originating name. If a name can t be resolved, both methods will throw a SocketException . Both methods will also accept an IP address as a string instead of a name and return the address to the IPHostEntry object. The behavior of these methods might seem strange , but it s actually convenient because you can develop an application that can address computers by name and by IP address. When your application supplies an IP address, these methods do not actually query DNS; instead, they simply recognize the IP address canonical form and return the address to the IPHostEntry object.

An IPHostEntry object is designed to contain three pieces of information: HostName , Aliases , and AddressList . The HostName property receives the host name that you re trying to resolve. If you re trying to resolve an IP address, the HostName property will receive your IP address. The Aliases property will receive an array of alias names if any are returned from a DNS forward name lookup. The DNS service can potentially return alias names for the host you re querying if it s configured to do so. The final property is AddressList , which will contain an array of one or more IP addresses if your DNS query succeeds. The following code fragment demonstrates how to use GetHostByName to resolve the name www.microsoft.com to an IP address:

C#

 try { IPHostEntryIPHost= Dns.GetHostByName("www.microsoft.com"); //Printoutthehostnamethatwasqueried Console.WriteLine("Thehostnameis: " + IPHost.HostName.ToString()); //Printoutanyaliasesthatarefound if(IPHost.Aliases.Length>0) { Console.WriteLine("Aliasesfoundare:"); foreach(stringAliasinIPHost.Aliases) { Console.WriteLine(Alias); } } Console.WriteLine("IPaddressesfoundare:"); intIPv4Count=0; intIPv6Count=0; //PrintoutalltheIPaddressesthatarefound foreach(IPAddressAddressinIPHost.AddressList) { if(Address.AddressFamily== AddressFamily.InterNetwork) { IPv4Count++; Console.WriteLine("IPv4Address#" + IPv4Count.ToString()+ " is " + Address.ToString()); } elseif(Address.AddressFamily== AddressFamily.InterNetworkV6) { IPv6Count++; Console.WriteLine("IPv6Address#" + IPv6Count.ToString()+ " is " + Address.ToString()); } } } catch(Exceptione) { Console.WriteLine("GetHostByNamefailedwitherror: " +e.Message); } 

Visual Basic .NET

 Try DimIPHostAsIPHostEntry=_ Dns.GetHostByName("www.microsoft.com") Printoutthehostnamethatwasqueried Console.WriteLine("Theprimaryhostnameis: " +_ IPHost.HostName.ToString()) Printoutanyaliasesthatarefound If(IPHost.Aliases.Length>0)Then Console.WriteLine("Aliasesfoundare:") DimCurAliasAsString ForEachCurAliasInIPHost.Aliases Console.WriteLine(CurAlias) Next EndIf Console.WriteLine("IPaddressesfoundare:") DimIPv4CountAsInteger=0 DimIPv6CountAsInteger=0 PrintoutalltheIPaddressesthatarefound DimAddressAsIPAddress ForEachAddressInIPHost.AddressList If(Address.AddressFamily=_ AddressFamily.InterNetwork)Then IPv4Count+=1 Console.WriteLine("IPv4Address#" +_ IPv4Count.ToString()+ " is " +_ Address.ToString()) ElseIf(Address.AddressFamily=_ AddressFamily.InterNetworkV6)Then IPv6Count+=1 Console.WriteLine("IPv6Address#" +_ IPv6Count.ToString()+ " is " +_ Address.ToString()) EndIf Next CatcheAsException Console.WriteLine(_  "GetHostByNamefailedwitherror: " +_ e.Message) EndTry 

As you can see, GetHostByName can potentially return more than one IP address from a DNS query. This behavior is important to understand because your network application should attempt to use the entire array of available addresses one at a time to set up communication over an IP network. In the preceding code fragment, we simply printed out all the IPv4 and IPv6 addresses that are available from a name. When you develop a network client application, your client should walk the AddressList using one IPAddress at a time to attempt communication over IP. You should try each address returned, regardless of whether it s IPv4 or IPv6. If the communication does not work, you should go to the next item in the list and try again until you run out of addresses. If you do successfully establish communication before exhausting the list, you should stop processing the list and go about performing network communication. The next chapter introduces client network programming using sockets, which requires you to supply an IP address to set up communication. Handling DNS replies properly in a client application will make your client more robust.

DNS queries can potentially take a lot of time to perform in your application. So far, we ve shown how to perform forward name lookup using the synchronous GetHostByName call. When you call GetHostByName , it can block on a name query, which can result in making your application unresponsive . To keep your application from blocking this way, the .NET Framework provides an asynchronous version of forward name lookup that follows the asynchronous pattern described in Chapter 3. In this type of lookup, you use BeginGetHostByName and EndGetHostByName . The first thing you need to do when performing a forward name lookup DNS query asynchronously is to define a delegate method that handles the asynchronous completion of an asynchronous DNS query, as shown here:

C#

 voidProcessDnsResults(IAsyncResultar) { try { IPHostEntryIPHost=Dns.EndGetHostByName(ar); //ProcessIPinformationherefromthe //completedDNSquery. } catch(Exceptione) { Console.WriteLine(GetHostByNamefailedwitherror:   +e.Message); } finally { Console.WriteLine(FinishedqueryingDNS.); } } 

Visual Basic .NET

 SharedSubProcessDnsResults(ByValarAsIAsyncResult) Try DimIPHostAsIPHostEntry=Dns.EndGetHostByName(ar) ProcessIPinformationherefromthe completedDNSquery. CatcheAsException Console.WriteLine(_  "GetHostByNamefailedwitherror: " _ +e.Message) Finally Console.WriteLine("FinishedqueryingDNS.") EndTry EndSub 

Once a delegate method is defined, you can use it in the asynchronous call to BeginGetHostByName , as follows:

C#

 AsyncCallbackAsyncDnsCallback= newAsyncCallback(ProcessDnsResults); try { Dns.BeginGetHostByName(www.microsoft.com", AsyncDnsCallback,null); } catch(Exceptione) { Console.WriteLine(BeginGetHostByNamefailedwitherror:   +e.Message); } 

Visual Basic .NET

 DimAsyncDnsCallbackAsAsyncCallback=_ NewAsyncCallback(AddressOfProcessDnsResults) Try Dns.BeginGetHostByName(www.microsoft.com",_ AsyncDnsCallback,Nothing) CatcheAsException Console.WriteLine(_  BeginGetHostByNamefailedwitherror: _ +e.Message) EndTry 
Note  

In the downloadable samples, a sample named ResolveName demonstrates how to synchronously perform a forward name lookup using the Dns class. There s also an asynchronous version of this application called AsyncResolveName.

Reverse Name Lookup

So far, we ve seen how to resolve a name to an IP address, but what if you have an IP address and want to find what name is associated with the address? DNS provides reverse name lookup to perform this type of lookup. The .NET Framework Dns class performs DNS reverse name lookup using GetHostByAddress(IPAddress) . This function returns any names associated with the IP address in an IPHostEntry object if an IP is associated with a name. If a name can t be found, a SocketException will be thrown.

Note  

The IPv6 protocol in Windows XP and Windows Server 2003 does not register its reverse lookup information with DNS, which means that a reverse query on an IPv6 address will fail.

In DNS, more than one name can be associated with an IP address. As a result, the IPHostEntry object returned by GetHostByAddress can return multiple names. The HostNam e field of IPHostEntry will contain the primary host name associated with the IP address. The Aliases field will contain any additional names that might also be associated with an IP. The following code fragment demonstrates how to perform a reverse name lookup:

C#

 try { IPHostEntryIPHost= Dns.GetHostByAddress("10.1.2.3"); Console.WriteLine("ThePrimaryHostnameis: " + IPHost.HostName.ToString()); //PrintoutanyAliasesthatarefound if(IPHost.Aliases.Length>0) { Console.WriteLine("Additionalnamesare:"); foreach(stringAliasinIPHost.Aliases) { Console.WriteLine(Alias); } } } catch(Exceptione) { Console.WriteLine("GetHostByAddressfailedwitherror: " +e.Message); } 

Visual Basic .NET

 Try DimIPHostAsIPHostEntry=_ Dns.GetHostByAddress(10.1.2.3) Console.WriteLine(_  ThePrimaryHostnameis:  +_ IPHost.HostName.ToString()) PrintoutanyAliasesthatarefound If(IPHost.Aliases.Length>0)Then Console.WriteLine(_  Additionalnamesfoundare:) DimCurAliasAsString ForEachCurAliasInIPHost.Aliases Console.WriteLine(CurAlias) Next EndIf CatcheAsException Console.WriteLine(_  GetHostByAddressfailedwitherror:  +_ e.Message) EndTry 

Performing DNS reverse name lookup using GetHostByAddress can take some time, so it might cause your application to block on the call. Blocking on this call can make your application appear unresponsive, as described with forward name lookup. Therefore, two asynchronous counterpart methods, BeginHostByAddress and EndHostByAddress , are available for reverse name lookup that use the .NET Framework asynchronous pattern. In the downloadable samples is a sample named ResolveIP that demonstrates how to synchronously perform reverse name lookup using the Dns class.




Network Programming for the Microsoft. NET Framework
Network Programming for the MicrosoftВ® .NET Framework (Pro-Developer)
ISBN: 073561959X
EAN: 2147483647
Year: 2003
Pages: 121

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net