Creating IP Agnostic Applications - Part 1
In Windows Vista and Windows Server "Longhorn," IPv6 is installed and enabled by default. When both IPv4 and IPv6 are enabled on these OSs, the TCP/IP stack prefers to use IPv6 over IPv4. For example, APIs such as ConnectByName will attempt to connect first via IPv6. See this post for a detailed discussion of address sorting.
Since the IPv6 stack has been enhanced, is installed by default and the preferred network layer, it is important when writing network code to ensure your application is IP agnostic. That is, your application should be able to function well when:
- IPv4 and IPv6 are installed
- IPv4 is the only network layer protocol installed
- IPv6 is the only network layer protocol installed
At first glance this requirement may seem like significant added complexity for an application developer, but there is good news :). We have been actively working on ways to make writing IP agnostic applications simple. The remainder of this post will talk about how to get started making your apps IPv6 aware. In a subsequent post, I hope to drill down into specific topics.
Check Out the Porting Guide
When writing such applications, I suggest reading the Winsock IPv6 Porting Guide. This document is applicable to Windows Vista and Windows Server Longhorn as well as many prior OSs such as Windows XP and Window Server 2003. While the document speaks directly to the use of native code Winsock APIs, the concepts presented are directly applicable to both native and managed code (ie. .Net Framework). Would a version of this document targeted at managed code development be useful? Are there areas of the porting guide you would like expanded? If so, please let us know.
How to Check if IPv4 or IPv6 is Installed
One of the first tasks you may need to do is determine if IPv4 and/or IPv6 are installed on the machine. I believe there a number of suggested methods out there, but the recommended method which will reliably tell you if a protocol is installed is to attempt to create a socket for the address family in question.
For Winsock developers, this is accomplished by calling the socket or WSASocket function with the address family parameter set to AF_INET to test IPv4 availability and AF_INET6 for IPv6.
For .Net Framework applications, use the Socket.SupportsIPv4 property to test IPv4 availability and Socket.OSSupportsIPv6 for IPv6.
Note: There is a Socket.SupportsIPv6 property, but this is NOT the one you want in this case. For compat reasons we could not change the meaning of this property which does not tell you whether the machine has IPv6 installed, but instead gives back the value of a legacy configuration switch.
Brad Williamson has posted already on a couple of the APIs available on Windows Vista & Windows Server "Longhorn" which simplify connection establishment for IP agnostic apps.