Networking IPv6 User Guide for JDK/JRE 5.0
5 stars based on
Within the past few years IPv6 has gained much greater acceptance in the industry, especially in certain regions of the world, i. Extensibility, mobility, quality of service, larger address space, auto-configuration, security, multi- homing, anycast and multicast, and renumbering—these are some of the features of IPv6 that make it desirable.
With the release of J2SE 1. We will prove these statements with code examples below and provide additional details on IPv6 support. Using IPv6 in Java is easy; it is transparent and automatic. Unlike in many other languages, no porting is necessary.
In fact, there is no need to even recompile the source files. Consider an example from The Java Tutorial:. You can run the same bytecode for this example in IPv6 mode if both your local host machine and the destination machine taranis are IPv6-enabled.
In contrast, if you wanted the corresponding C program to run in IPv6 mode, you would first need to port it. Here's what would need to happen:. Note that for new applications, 1933 bind before connect and setting the client interface address you write address-family-agnostic data structures, there is no need for porting. Namely, depending on whether your application is written for a dual-stack platform, such as Solaris or Linux, or a single-stack platform, such as Windows, you would need to structure the code differently.
For server-side programming, Java shows a big advantage. You still write the same code as before:. Now, however, if you run the code on an IPv6-enabled machine, you immediately have an IPv6-enabled service.
Notice that on a dual-stack machine, since one socket, the IPv6 socket, will be able to access both IPv4 and IPv6 protocol stacks, you only need to create one socket. Thus this server can potentially support both IPv4 and IPv6 clients. Here you need to create two server sockets, one for IPv6 stack and one for IPv4 stack.
You also need to multiplex on the two sockets to listen to connections from either IPv4 or IPv6 clients. If IPv6 is supported, it will try to use the IPv6 stack. More specifically, on dual-stack systems it will create an IPv6 socket. On separate-stack systems things are much more complicated.
Java will create two sockets, one for IPv4 and one for IPv6 communication. For client-side TCP applications, once the socket is connected, the internet-protocol family type will be fixed, and the extra socket can be closed. For server-side TCP applications, since there is no way to tell from which IP family type the next client request will come, two sockets need to be 1933 bind before connect and setting the client interface address.
For UDP applications, both sockets will be needed for the lifetime of the communication. You don't need to know the following in order to use IPv6 in Java. But if you are curious and what to know what happens under various circumstances, the remainder of this document should provide answers. This is also called anylocal or wildcard address.
We always try to bind to IPv6 anylocal address on a dual-stack machine unless a related system property is set to use IPv4 Stack. The option discussed below is introduced in Draft-ietf-ipngwg-rfcbis It will be supported in the Java 2 platform when it becomes a standard.
However, there is 1933 bind before connect and setting the client interface address new socket option that changes the above behaviour. When this option is turned on, the socket can be used to send and receive IPv6 packets only. By default this option is turned off. Packets with the loopback address must never be sent on a link or forwarded by an IPv6 router.
There are two separate loopback addresses for IPv4 and IPv6 and they are treated as such. This is used for hosts and routers to dynamically tunnel IPv6 packets over IPv4 routing infrastructure. It is meaningful for OS kernel and routers. Java provides a utility method to test it. This is an IPv6 address that is used to represent an 1933 bind before connect and setting the client interface address address.
It allows the native program to use the same address data structure and also the same socket when communicating with both IPv4 and IPv6 nodes. The OS will do the underlying plumbing required to send or receive an IPv4 datagram and to hand it to an IPv6 destination socket, and it will synthesize an IPv4-mapped IPv6 address when needed. For Java, it is used for internal representation; it has no functional role. Java will never return an IPv4-mapped address. It understands IPv4-mapped address syntax, both in byte array and text representation.
However, it will be converted into an IPv4 address. On dual stack machines, system properties are provided for setting the preferred protocol stack—IPv4 or IPv6—as well as the preferred address family types—inet4 or inet6. This setting can be changed through the java.
By default, we would prefer IPv4 addresses over IPv6 addresses, i. There are two reasons for this choice:. This setting can be changed through the system property java. Thus compatibility with the large installed base of IPv4 nodes is crucial for the success of the transition from IPv4 to IPv6. Dual stack, defined in RFCis one of the main mechanisms for guaranteeing a smooth transition. The former is the most relevant piece to the JDK.
However, unless a socket checks for the peers address type, it won't know whether it is talking to an IPv4 or an IPv6 peer. All the internal plumbing and conversion of address types is done by the dual-protocol stack. IPv4-mapped address has significance only at the implementation of a dual-protocol stack. It is used to fake i. At the conceptual level it has no role; its role is limited at the Java API level. Parsing of an IPv4-mapped address 1933 bind before connect and setting the client interface address supported, but an IPv4-mapped address is never returned.
Top row and left column represent various node types attempting to communicate. An x indicates that these nodes can communicate with each other. If host2 wants to talk to host1, it will create a V6 socket.
It then looks up the IP address for host1. Since host1 only has a v4 protocol stack, it will only have IPv4 records in the name lookup service. So host2 will try to reach host1 using an IPv4-mapped address. An IPv4 packet will be sent by host2, and host1 will think it is communicating with a v4 client.
If host2 is the server, it will first create a v6-type socket by defaultthen it will wait for connections. Since host1 supports v4 only, it creates a v4-type socket. They resolves the name for host2. It only gets v4 address for host2, since it doesn't understand IPv6 address. So it connects to host2 using v4 address. A v4 packet will be sent on the wire. On host2, the dual stack will convert the v4 packet into a v6 packet with a v4-mapped address in it and hand it over to the v6 socket.
The server application will handle it as if it is a connection from a v6 node. This class represents an IP address. It provides address storage, name-address translation methods, address conversion methods, as well as address testing methods. Utility methods are added 1933 bind before connect and setting the client interface address check address types and scopes.
Two new subclasses of InetAddress are created: V4- and V6-specific state and behaviors are implemented in these two subclasses. Due to Java's object-oriented nature, an application normally only needs to deal with InetAddress class—through polymorphism it will get the correct behavior. Only when it needs to access protocol-family-specific behaviors, such as in calling an IPv6-only method, or when it cares to know the class types of the IP address, will it ever become aware of Inet4Address and Inet6Address.
You can tell the JDK to use this provider by setting up a few system properties. These system properties are documented in the Java system properties section. In the future, we plan to provide a generic service provider framework so that you can 1933 bind before connect and setting the client interface address your own name service providers. They are serialized as InetAddress objects, and deserialized from InetAddress to Inet4Address to keep backward compatibility.
IPv6 addresses are represented as Inet6Address and are serialized as such. Due to the object-oriented nature of Java, the address types and storage structures are not exposed at the socket API level, so no new APIs are needed. All supported Ipv6 socket options have a IPv4 counterparts.
The two existing methods are used to set or retrieve the network interface used by the current MulticastSocket to send multicast packets i. For IPv4, the interface was indicated by an IP address. Thus we can use the equivalent InetAddress in Java. They will continue to work with IPv6 multicast socket.
To better support the concept of a network interface, we introduced a new class, NetworkInterface. It encapsulate the data representing the state of the network interface, including name and IP addresses and some basic manipulation methods. Thus we have introduced two 1933 bind before connect and setting the client interface address methods for setting the outgoing interface for multicast socket: They take or return a NetworkInterface object.