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.
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:
Would have to be converted to the following, which handles both IPv4 and IPv6
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/