Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to support both IPv4 and IPv6 connections

I'm currently working on a UDP socket application and I need to build in support so that IPV4 and IPV6 connections can send packets to a server.

I was hoping that someone could help me out and point me in the right direction; the majority of the documentation that I found was not complete. It'd also be helpful if you could point out any differences between Winsock and BSD sockets.

Thanks in advance!

like image 839
Charles Avatar asked Oct 24 '09 15:10

Charles


People also ask

Can a network support both IPv4 and IPv6?

IPv4 and IPv6 must coexist for some number of years, and their coexistence must be transparent to end users. If an IPv4-to-IPv6 transition is successful, end users should not even notice it. A dual-stack device is a device with network interfaces that can originate and understand both IPv4 and IPv6 packets.

How can I use IPv4 and IPv6 at the same time?

IPv4/IPv6 co-existence can take one of three forms.. One is dual stack, where your network hardware runs IPv4 and IPv6 simultaneously. Next is when you "tunnel" one protocol within another. Usually, this means taking IPv6 packets and encapsulating them in IPv4 packets.

Should I enable both IPv4 and IPv6?

Do you need both IPv4 and IPv6? When possible, it is better to keep both IPv4 and IPv6 addresses enabled. For example, using only IPv6 can cause some accessibility issues, as only about one third of the internet supports IPv6 addresses.

Which technique allows networks to run both IPv4 and IPv6 on the same network?

Dual Stack – As shown in Figure 1, dual stack allows IPv4 and IPv6 to coexist on the same network.


2 Answers

The best approach is to create an IPv6 server socket that can also accept IPv4 connections. To do so, create a regular IPv6 socket, turn off the socket option IPV6_V6ONLY, bind it to the "any" address, and start receiving. IPv4 addresses will be presented as IPv6 addresses, in the IPv4-mapped format.

The major difference across systems is whether IPV6_V6ONLY is a) available, and b) turned on or off by default. It is turned off by default on Linux (i.e. allowing dual-stack sockets without setsockopt), and is turned on on most other systems.

In addition, the IPv6 stack on Windows XP doesn't support that option. In these cases, you will need to create two separate server sockets, and place them into select or into multiple threads.

like image 99
Martin v. Löwis Avatar answered Sep 28 '22 21:09

Martin v. Löwis


The socket API is governed by IETF RFCs and should be the same on all platforms including windows WRT IPv6.

For IPv4/IPv6 applications it's ALL about getaddrinfo() and getnameinfo(). getaddrinfo is a genius - looks at DNS, port names and capabilities of the client to resolve the eternal question of “can I use IPv4, IPv6 or both to reach a particular destination?” Or if you're going the dual-stack route and want it to return IPv4-mapped IPv6 addresses, it will do that too.

It provides a direct sockaddr * structure that can be plugged into bind(), recvfrom(), sendto() and the address family for socket()… In many cases this means no messy sockaddr_in(6) structures to fill out and deal with.

For UDP implementations I would be careful about setting dual-stack sockets or, more generally, binding to all interfaces (INADDR_ANY). The classic issue is that, when addresses are not locked down (see bind()) to specific interfaces and the system has multiple interfaces requests, responses may transit from different addresses for computers with multiple addresses based on the whims of the OS routing table, confusing application protocols—especially any systems with authentication requirements.

For UDP implementations where this is not a problem, or TCP, dual stack sockets can save a lot of time when IPv*-enabling your system. One must be careful to not rely entirely on dual-stack where it`s not absolutely necessary as there are no shortage of reasonable platforms (Old Linux, BSD, Windows 2003) deployed with IPv6 stacks not capable of dual stack sockets.

like image 42
Einstein Avatar answered Sep 28 '22 19:09

Einstein