< Chap 7 TOC: IP Address & DNS | Main | C++, VB .NET & C# Name Resolution Program Examples >

 


 

Chapter 7 Part 1:

IP Addressing and DNS

 

 

What do we have in this chapter 7 Part 1?

  1. Overview

  2. IP Addressing

  3. IPv4 Protocol

  4. Unicast Addresses

  5. Wildcard Address

  6. Loopback Address

  7. Broadcast Address

  8. Multicast Addresses

  9. IPv6 Protocol (external)

  10. Unicast Addresses

  11. Wildcard Address

  12. Loopback Address

  13. Broadcast Address

  14. Multicast Addresses

  15. DNS

  16. Forward Name Lookup

 

 

 

Overview

 

Internet Protocol version 4 (IPv4) is commonly known as the network protocol used on the Internet. IPv4 is widely available on most computer operating systems and can be used on most local area networks (LANs), such as a small network in your office, and on wide area networks (WANs), such as the Internet. With the explosion of computers on the Internet, the limitations of IPv4 are becoming apparent, and as a result, the next generation of Internet Protocol was developed, IP version 6 (IPv6).

When a computer or a device is connected to an IP network, it must obtain at least one or more IP addresses either dynamically or statically for it to communicate over an IP network. Static IPs are configured manually by the administrator of the machine or device, and dynamic IPs are automatically assigned to a computer over a network using one of the following methods: Dynamic Host Configuration Protocol (DHCP) or Router Advertisement (RA). DHCP is commonly used on IPv4 networks, while RA is typically found on IPv6 networks. As you’ll see in this chapter, IP addresses are represented in numerical form, which makes it difficult for users to remember a computer or device identification on an IP network. To make addressing more flexible, IP networks typically have a Domain Name System (DNS) available, which allows you to identify machines or devices by a more meaningful, friendly name instead of the difficult-to-remember numerical identification.

In this chapter, we’ll describe how computers identify and address one another using the IPv4 and IPv6 protocols. Understanding IP addressing is essential for working with the Microsoft Windows .NET Framework networking classes, which allow you to communicate over IP networks. These classes will be discussed in the next two chapters. The .NET Framework networking classes also support addressing non-IP address-based communications using Internetwork Packet Exchange (IPX), AppleTalk, Infrared, and so on. However, the focus of this book is only the IP-based classes. Once IP addressing is explained, we’ll explain name resolution techniques using DNS through the .NET Framework that can simplify the complexities of IP addressing.

 

IP Addressing

 

On IP networks, computers and devices identify one another using numerical IP addresses. As mentioned earlier in this chapter, two versions of IP are in use today: IPv4 and IPv6. The next two sections describe IP address formats in more detail.

 

IPv4 Protocol

 

The IPv4 protocol was developed in the mid 1970s as a part of the Advanced Research Project Agency (ARPA) of the United States Department of Defense. Ipv4 has become the de facto standard protocol on the Internet. With IPv4, computers are assigned an address that’s represented as a 32-bit quantity, formally known as an IPv4 address. All current versions of the Windows operating system support the IPv4 protocol.

IPv4 addresses are typically represented in a dotted decimal format, for example, xxx.xxx.xxx.xxx, where each xxx section is an octet (8 bits) of the address and is converted to a decimal number that’s separated by a period (dot). An IPv4 address might look like 172.31.28.120. IPv4 addresses are divided into classes that describe the portion of the address assigned to the network and the portion assigned to actual end points (or computers). Table 7-1 lists the different classes.

 

Table 7-1: IPv4 Address Classes

 

Class

Network Portion

First Number

Number of End Points

A

8 bits

0 to 127

16,777,216

B

16 bits

128 to 191

65,536

C

24 bits

192 to 223

256

D

n/a

224 to 239

n/a

E

n/a

240 to 255

n/a

 

All IP addresses that identify individual computer interfaces (also known as unicast addresses) can be broken into two parts: the network portion and the host ID. The network portion is the first part of the address, which can be a variable number of bits, and it identifies the specific network a host resides on. The host portion of the address is comprised of the remaining bits in the address and uniquely identifies that computer’s interface on the given network. When specifying an IP address, the number of bits indicating the network portion can be appended to the dotted decimal address after a slash mark (/). For example, the address 172.31.28.120/16 indicates that the first 16 bits make up the network portion of the address. This address, followed by the slash, is known as the Classless Interdomain Routing (CIDR) notation, which is equivalent to the netmask. The address 172.31.28.120/16 has an equivalent subnet mask of 255.255.0.0.

The last two entries in Table 7-1 are special classes of IPv4 addresses. Class D addresses are reserved for IPv4 multicasting, and class E addresses are experimental. Also, the following blocks of addresses have been reserved for private use and can’t be used by a system on the Internet:

 

  1. 10.0.0.0 to 10.255.255.255 (10.0.0.0/8)
  2. 172.16.0.0 to 172.31.255.255 (172.16.0.0/12)
  3. 192.168.0.0 to 192.168.255.255 (192.168.0.0/16)

 

Finally, the loopback address (127.0.0.1) is a special address that refers to the local computer.

To list the IPv4 addresses assigned to a Windows computer, the ipconfig.exe utility on Windows NT, Windows 2000, Windows XP, and Windows Server 2003 will output a list of the IP addresses for all network interfaces on the local computer.

 

The ipconfig tool options run from the command prompt

 

The ipconfig tool with /all option run from the command prompt

 

For Windows 9x (including Windows Me), the utility for retrieving IPv4 address information is winipcfg.exe.

We’ve briefly discussed the breakdown of the IPv4 address space, and within these different address classes are three types of IPv4 addresses:

 

  1. Unicast
  2. Multicast, and
  3. Broadcast.

 

There are also two other special address types: loopback and wildcard. Table 7-2 lists the different types of IPv4 addresses. The next sections will briefly discuss these different types of IPv4 addresses.

 

Table 7-2: IPv4 Address Types

 

Type

Address or Range

Description

Broadcast

255.255.255.255

When data is sent to this address, all computers on the local network will receive it.

Loopback

127.0.0.1

This address represents only the local computer.

Multicast

224.0.0.1 to 239.255.255.255

These are multicast addresses used to send from one source to many recipients.

Unicast

0.0.0.1 to 223.255.255.255

These are unique addresses assigned to a computer’s interfaces.

Wildcard

0.0.0.0

Also known as the any address, this address represents all interfaces on the computer.

 

Although IPv4 addresses are not too large in form, they still can be quite cumbersome to remember when trying to identify computers. Therefore, IP networks can have a DNS service that provides a mapping between a user-friendly name and an IP address. Later in the chapter, we’ll show how to use the DNS service to resolve names to addresses.

 

Unicast Addresses

 

Unicast addresses are assigned to an individual computer’s interface, and only one interface can be assigned that address. If another computer on the network is configured with that address, data will not be delivered correctly. Typically, computers are configured either with a manually assigned IPv4 address or they obtain one from a configuration protocol such as DHCP. If, for some reason, a computer configured to retrieve a DHCP-assigned address failed, the network stack will assign an autoconfigure address in the range of 169.254.0.0/16. An autoconfigure address is valid only on the local network link; it is not routable or valid on the Internet.

The IPv4 unicast address space sets aside several regions that are designed for use on isolated networks, that is, networks not connected directly to the Internet (often intranets). The following addresses are reserved for intranet (private) uses:

 

  1. 10.0.0.0 to 10.255.255.255 (10.0.0.0/8)
  2. 172.16.0.0 to 172.31.255.255 (172.16.0.0/12)
  3. 192.168.0.0 to 192.168.255.255 (192.168.0.0/16)

 

Other than the fact that these addresses can never appear on the Internet, they follow the same rules as regular unicast addresses, such as only one instance of a given intranet address can be present on the local network. These addresses are routable on the private network.

These private addresses are also used by Network Address Translators (NATs), which are commonly found on routers and Digital Subscriber Line (DSL) modems. These addresses are useful when an Internet service provider (ISP) allows a customer only one global IPv4 address, but the customer has several computers on a home network to connect to the Internet. A NAT assigns each computer on the home network one of the private intranet addresses, and the NAT router is assigned the global IPv4 Internet address. The NAT translates communication to and from the home network computers. NATs are useful in this respect, but they can introduce problems, as we will discuss later.

 

Wildcard Address

 

The wildcard address is a special address used when an application wants to receive network traffic on any interface on the computer, which is typically the case for server applications. However, if an application wants to operate on a single interface, it uses the unicast IPv4 address assigned to the interface that it’s interested in.

 

Loopback Address

 

The loopback address is typically used for interprocess communication. If multiple sockets on the sample computer need to communicate with each other, the loopback address is used to establish a connection within the local machine.

 

Broadcast Address

 

The broadcast address is used as a destination address when sending data. When data is sent to the broadcast address, every computer on the local network must process that packet to determine if anyone is waiting for broadcast data. It’s considered bad practice to design applications that use broadcast data because every machine on the network must process that message when most machines probably do not care about that data.

 

Multicast Addresses

 

Multicast addresses are used by applications that need to send data from a single source to many recipients. Unlike broadcast data, multicasting provides a way for applications to register whether they are interested in data sent to a particular multicast address. Multicasting will be discussed in greater detail in the next chapter.

 

IPv6 Protocol

 

The IPv6 protocol is a relatively new protocol aimed to replace the IPv4 protocol; it addresses several key limitations of IPv4. First and foremost, the world is running out of IPv4 addresses, which is leading to a proliferation of NAT techniques where multiple computers can share (or hide behind) a single IP address. The problem with NATs is that they break the end-to-end connectivity experience. As mentioned earlier in this chapter, computers behind a NAT are assigned private addresses. Consider the case where you and your buddy want to start an instant messenger–type application. If both computers are behind a NAT, when you connect to your buddy, all you have is your buddy’s private IP address, which is not valid to address over the Internet. To circumvent this problem, a central server (that has a valid non-private IPv4 address) needs to be present to route the traffic accordingly. The good thing about IPv6 is that it was designed with a much larger 128-bit address space, which means that there are plenty of addresses for everyone.

Because IPv6 addresses are much bigger than IPv4 addresses, they are represented in hexadecimal form, such as fe80::240:96ff:fe36:d757, where each hex digit represents 4 bits of the address and each 16-bit chunk of the address is separated by a colon. You’ll notice our example address has a double colon, which represents zero compression. If there is a series of 16-bit chunks that are all zero, they can be replaced by the double colon. This compression can occur only once in the address.

The IPv6 address space is hierarchical, which means that high-order bits define the address types shown in Table 7-3. For example, a multicast address always starts with a series of consecutive one bits (or 0xFF when viewed in hex). A hierarchical address space simplifies the protocol routing on networks.

IPv6 addresses and host names can also be registered with DNS so that applications can resolve a host name and receive IPv6 addresses in return. Again, we’ll talk more about using DNS to resolve names to IP addresses later in the chapter.

Note that the IPv6 protocol is only available on Windows XP and later. Also, the IPv6 sockets are not accessible from the .NET Framework unless explicitly enabled. To enable the IPv6 protocol, edit the machine.config file under %SYSTEMROOT%\Microsoft.Net\Framework\version\config, where version is the .NET Framework version number in which IPv6 is to be enabled. Versions 1.1.4322 and later support IPv6 sockets. The configuration file is XML based. Search for the ipv6 entry, which looks like this:

 

<!-- <ipv6 enabled="false"/> -->

 

Set the property to true, and remove the XML comment tags, <!-- and -->, so that the line looks like this:

 

<ipv6 enabled="true"/>

 

The following Figures show the XML tag setting for .NET Framework 1.1.x.

 

The machine.config file for .NET Framework 1.1.x

 

The XML IPv6 tag setting for .NET Framework 1.1.x  

 

For .NET Framework 2.x and above, you need to add the XML tag manually. The next sections will give a short introduction of the different types of IPv6 addresses. The summary is shown in the following Table.

 

Table 7-3: IPv6 Address Types

 

Type

Prefix/ Address

Description

Broadcast

n/a

The IPv6 protocol does not support the notion of broadcast data.

Global

001

Address that is globally reachable on the Internet.

Link-local

1111 1110 10

Local address that is not routable but can be used to communicate to other computers on the same network link.

Loopback

::1

Address representing the local computer.

Multicast

1111 1111

Multicast addresses used to send from one source to many recipients.

Site-local

1111 1110 11

Represents addresses that are reachable only in a LAN environment.

Wildcard

::

Also known as the any address; represents all interfaces on the computer.

 

Unicast Addresses

 

IPv6 addresses are assigned to interfaces just as the IPv4 protocol addresses are, with a few exceptions. First, a computer with IPv6 installed will have multiple IPv6 unicast addresses assigned to it. By default, all interfaces will be assigned a link-local address regardless of whether a global address is obtained. This address is analogous to the autoconfigure assigned address in IPv4. A link-local address is always prefixed with the hex sequence FE80. So, fe80::240:96ff:fe36:d757%4 is an example of a local link address.

One thing to note is the %4 at the end of the address. The percent sign indicates a scope ID, while the number 4 indicates that this link-local address is on interface number 4. Link-local addresses such as IPv4 autoconfigure addresses are not routable, meaning that they are valid only on that local network link. So, an application connecting to another computer using link-local addresses must specify the local link on which the remote link-local address can be reached. In Figure 7-1, there are two computers on the same link: computer A with the address fe80::1%1 and computer B with fe80::2%2. If computer A needs to connect to B, A substitutes its local interface from which B is reachable as the scope-id in the destination address, which would be fe80::2%1.

 

Connecting using link-local addresses

 

Figure 7-1: Connecting using link-local addresses

 

An interface will always have a link-local address, and it might also have a site-local address, a global address, or both. A site-local address is analogous to the private address ranges in IPv4 except that all IPv6 site-local addresses begin with the prefix FEC0. Site-local addresses are typically used for corporate intranets where traffic should not be routable to the global Internet. There are IPv6 global addresses that are fully routable and valid everywhere.

Site-local and global addresses can either be manually configured or assigned via router advertisements, that is, the network infrastructure can assign the network portion of the address and the local computer will create the host portion from the Media Access Control (MAC) address of the interface. The same CIDR notation is used with IPv6 addresses. For example, a router might advertise a site-local prefix of fec0::/48, meaning that the first 48 bits of the address are the network portion of the 128-bit IPv6 address.

 

Wildcard Address

 

The IPv6 wildcard address is a double include, which indicates all zeroes and has the same meaning as in IPv4. An application binds to the wildcard address to listen across all interfaces on the local system.

 

Loopback Address

 

The IPv6 loopback address is ::1, which indicates all zeroes except that last bit, which is one. The loopback address indicates the local computer.

 

Broadcast Address

 

The IPv6 protocol does not support the notion of a broadcast packet, that is, there’s no support for sending a packet that will be received by every computer on the local network. Applications that require this ability should use multicasting because only computers interested in receiving the data would join the same group.

 

Multicast Addresses

 

All multicast addresses in IPv6 begin with the prefix 1111 1111 (FF). IPv6 multicasting is very similar to IPv4 multicasting and will be covered in more detail in the next chapter.

 

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() (this method is obsolete in the newer .NET Framework version and replaced by GetHostEnty()) and the Resolve() methods to perform forward DNS name lookup.

 

This GetHostEnty() method replacing the GetHostByName() and other related DNS methods

 

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

{

    IPHostEntry IPHost = Dns.GetHostEntry("www.microsoft.com");

 

    // Print out the host name that was queried

    Console.WriteLine("The host name is: " + IPHost.HostName.ToString());

 

    // Print out any aliases that are found

    if (IPHost.Aliases.Length > 0)

    {

        Console.WriteLine("Aliases found are:");

        foreach(string Alias in IPHost.Aliases)

        {

            Console.WriteLine(Alias);

        }

    }

 

    Console.WriteLine("IP addresses found are:");

 

    int IPv4Count = 0;

    int IPv6Count = 0;

 

    // Print out all the IP addresses that are found

    foreach(IPAddress Address in IPHost.AddressList)

    {

        if (Address.AddressFamily == AddressFamily.InterNetwork)

        {

            IPv4Count++;

            Console.WriteLine("IPv4 Address #" + IPv4Count.ToString() + " is " + Address.ToString());

        }

        else if (Address.AddressFamily == AddressFamily.InterNetworkV6)

        {

            IPv6Count++;

            Console.WriteLine("IPv6 Address #" + IPv6Count.ToString() + " is " + Address.ToString());

        }

    }

}

catch (Exception e)

{

    Console.WriteLine("GetHostEntry() failed with error: " + e.Message);

}

 

Visual Basic .NET

 

Try

Dim IPHost As IPHostEntry = Dns.GetHostEntry("www.microsoft.com")

 

' Print out the host name that was queried

    Console.WriteLine("The primary host name is: " + IPHost.HostName.ToString())

 

' Print out any aliases that are found

    If (IPHost.Aliases.Length > 0) Then

        Console.WriteLine("Aliases found are:")

Dim CurAlias As String

        For Each CurAlias In IPHost.Aliases

            Console.WriteLine(CurAlias)

        Next

    End If

 

    Console.WriteLine("IP addresses found are:")

 

Dim IPv4Count As Integer = 0

Dim IPv6Count As Integer = 0

 

' Print out all the IP addresses that are found

Dim Address As IPAddress

 

    For Each Address In IPHost.AddressList

        If (Address.AddressFamily = AddressFamily.InterNetwork) Then

            IPv4Count += 1

            Console.WriteLine("IPv4 Address #" + IPv4Count.ToString() + " is " + Address.ToString())

 

        ElseIf (Address.AddressFamily = AddressFamily.InterNetworkV6) Then

            IPv6Count += 1

            Console.WriteLine("IPv6 Address #" + IPv6Count.ToString() + " is " + Address.ToString())

        End If

 

    Next

 

Catch e As Exception

    Console.WriteLine("GetHostEntry() failed with error: " + e.Message)

End Try

 

As you can see, GetHostByName() (GetHostEntry()) 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() (this method is obsolete in newer .NET Framework, use BeginGetHostEntry() instead) and EndGetHostByName() (this method is obsolete in newer .NET Framework, use EndGetHostEntry() instead). 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#

 

void ProcessDnsResults(IAsyncResult ar)

{

    try

    {

         IPHostEntry IPHost = Dns.EndGetHostEntry(ar);

         // Process IP information here from the completed DNS query.

    }

    catch (Exception e)

    {

        Console.WriteLine("GetHostEntry() failed with error: "+ e.Message);

    }

    finally

    {

        Console.WriteLine("Finished querying DNS.");

    }

}

 

Visual Basic .NET

 

Shared Sub ProcessDnsResults(ByVal ar As IAsyncResult)

    Try

        Dim IPHost As IPHostEntry = Dns.EndGetHostEntry(ar)

 

        ' Process IP information here from the completed DNS query.

 

    Catch e As Exception

        Console.WriteLine("GetHostEntry() failed with error: " + e.Message)

    Finally

        Console.WriteLine("Finished querying DNS.")

    End Try

End Sub

 

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

 

C#

 

AsyncCallback AsyncDnsCallback = new AsyncCallback(ProcessDnsResults);

 

try

{

    Dns.BeginGetHostByName("www.microsoft.com", AsyncDnsCallback, null);

}

catch (Exception e)

{

   Console.WriteLine("BeginGetHostEntry() failed with error: " + e.Message);

}

 

Visual Basic .NET

 

Dim AsyncDnsCallback As AsyncCallback = New AsyncCallback(AddressOf ProcessDnsResults)

 

Try

    Dns.BeginGetHostByName("www.microsoft.com", AsyncDnsCallback, Nothing)

 

Catch e As Exception           

    Console.WriteLine("BeginGetHostEntry() failed with error: " + e.Message)

End Try

 

The following program samples demonstrate how to synchronously perform a forward name lookup using the Dns class. The example that follows is an asynchronous version.

 

 

 


 

< Chap 7 TOC: IP Address & DNS | Main | C++, VB .NET & C# Name Resolution Program Examples >