I need to add IPv6 support to an existing socket-based application which currently only supports IPv4. To embrace the incoming era of IPv6, someone ordered me to add an IPv6 interface for the app and let the outside world to choose either the IPv4 interface or the IPv6 interface when trying to communicate with the app.
My question is: is it true that for the socket handling API level in Linux, there is no difference between handling an IPv4 based socket and IPv6 based socket?
Further more, is it possible to let a socket listen on two IP addresses with same port? If that is true, then to implement the requirement is a trivial work, I guess.
The main difference between IPv4 and IPv6 is the address size of IP addresses. The IPv4 is a 32-bit address, whereas IPv6 is a 128-bit hexadecimal address. IPv6 provides a large address space, and it contains a simple header as compared to IPv4.
IP Addresses with a Dual-Stack SocketAny IPv4 addresses must be represented in the IPv4-mapped IPv6 address format which enables an IPv6 only application to communicate with an IPv4 node. The IPv4-mapped IPv6 address format allows the IPv4 address of an IPv4 node to be represented as an IPv6 address.
One of the differences between IPv4 and IPv6 is the appearance of the IP addresses. IPv4 uses four 1 byte decimal numbers, separated by a dot (i.e. 192.168. 1.1), while IPv6 uses hexadecimal numbers that are separated by colons (i.e. fe80::d4a8:6435:d2d8:d9f3b11).
Port restrictions for IPv6 are identical to those available in IPv4. TCP and UDP have separate port spaces, each identified by port numbers in the range 1-65535. For IPv6, ports work the same as IPv4.
It is not possible to listen on 2 different IP addresses with 1 TCP socket, however if you listen on all interfaces using the in6addr_any address, that will include all IPv4 addresses as well (though I believe e.g. linux has a kernel option to disable that mapping).
The (newer version of) socket API is quite transparent as to whether you're using IPv4 or IPv6, but quite great care must be taken over how an IPv4 application is typically coded.
e.g. this IPv4 code which accepts a connection and prints out the address of the remote host:
struct sockaddr_in client_addr;
socklen_t addr_len = sizeof(client_addr);
client_data->fd = accept(server_fd,(struct sockaddr*)&client_addr,&addr_len);
log_printf("New client from %s\n",inet_ntoa(client_addr.sin_addr.s_addr));
Would have to be converted to the following, which handles both IPv4 and IPv6
struct sockaddr_storage client_addr;
char numeric_addr[INET6_ADDRSTRLEN];
socklen_t addr_len = sizeof(client_addr);
client_data->fd = accept(server_fd,(struct sockaddr*)&client_addr,&addr_len);
if(client_addr.ss_family == AF_INET)
log_printf("New client from %s\n",inet_ntop(client_addr.ss_family,((struct sockaddr_in*)&client_addr)->sin_addr.s_addr ,numeric_addr,sizeof numeric_addr));
else if(client_addr.ss_family == AF_INET6)
log_printf("New client from %s\n",inet_ntop(client_addr.ss_family,((struct sockaddr_in6*)&client_addr)->sin6_addr ,numeric_addr,sizeof numeric_addr));
Though I believe you could do it even more elegantly and transparent with getaddrinfo()
Here's additional notes on IP layer independance: http://uw714doc.sco.com/en/SDK_netapi/sockC.PortIPv4appIPv6.html http://www.kame.net/newsletter/19980604/
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With