How to support IPv4 and IPv6 connections

I am currently working on a UDP socket application, and I need to build support so that IPV4 and IPV6 connections can send packets to the server.

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

Thanks in advance!

+48
c ++ sockets ipv4 ipv6 winsock
Oct 24 '09 at 15:15
source share
4 answers

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

The main difference between the systems is the availability of IPV6_V6ONLY a), b) turning on or off by default. It is disabled by default on Linux (i.e., allows two-stack sockets without setsockopt) and is enabled on most other systems.

In addition, the IPv6 stack in Windows XP does not support this option. In these cases, you need to create two separate server sockets and put them in one or more threads.

+74
Oct 24 '09 at 15:24
source share

The socket API is managed by the IETF RFC and must be the same on all platforms, including Windows WRT IPv6.

For IPv4 / IPv6 applications, this is ALL about getaddrinfo() and getnameinfo() . getaddrinfo - genius - examines DNS, port names, and client capabilities to solve the eternal question "can I use IPv4, IPv6, or both to reach a specific destination?" Or, if you are following the double-stack path and want it to return IPv4 IPv4 addresses, it will also do this.

It provides a direct sockaddr * structure that can be connected to bind() , recvfrom() , sendto() and the address family for socket() ... In many cases, this means that the messy sockaddr_in(6) structures are populated and have business with.

For UDP implementations, I would be careful about setting up sockets with two stacks or, more generally, binding to all interfaces ( INADDR_ANY ). The classic problem is that when addresses are not blocked (see bind() ) for certain interfaces, and the system has several interface requests, responses can go 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 implementations of UDP where this is not a problem, or TCP, dual stack sockets can save a lot of time when IPv * changes your system. Care must be taken not to rely entirely on the dual stack, where this is not entirely necessary, since there is no shortage of reasonable platforms (Old Linux, BSD, Windows 2003) deployed with IPv6 stacks that are not capable of dual socket sockets.

+6
Feb 11 '10 at 19:57
source share

I played with this under Windows, and itโ€™s actually a security issue, if you bind to the loopback address, then the IPv6 socket is correctly connected to [:: 1], but the mapped IPv4 socket is bound to INADDR_ANY, so your (supposedly) safe local application really accessible to the world.

+3
Aug 20 '10 at 15:59
source share

The RFCs do not actually indicate the existence of the IPV6_V6ONLY socket option, but if it is absent, the RFC is pretty clear that the implementation should be as if this parameter were FALSE.

If the option is present, I would say that it should have a default value of FALSE, but, for reasons that convey understanding, BSD and Windows versions default to TRUE. There is a strange assertion that this is a security problem because an unfamiliar IPv6 programmer might associate the thought that they only bind to IPv6-only IN6ADDR_ANY and accidentally accept an IPv4 connection that causes a security problem. I think this is far-fetched and absurd in addition to being unexpected for anyone who expects an RFC-compliant implementation.

In the case of Windows, non-compliance is usually not a surprise. In the case of BSD, this is unsuccessful at best.

+2
May 18 '10 at 1:41 p.m.
source share



All Articles