C++ 如何同时支持 IPv4 和 IPv6 连接

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1618240/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 20:38:04  来源:igfitidea点击:

How to support both IPv4 and IPv6 connections

c++winsockipv6socketsipv4

提问by Charles

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.

我目前正在开发一个 UDP 套接字应用程序,我需要构建支持,以便 IPV4 和 IPV6 连接可以将数据包发送到服务器。

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.

我希望有人可以帮助我并指出正确的方向;我发现的大部分文档都不完整。如果您能指出 Winsock 和 BSD 套接字之间的任何差异,这也会很有帮助。

Thanks in advance!

提前致谢!

回答by Martin v. L?wis

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 offthe socket option IPV6_V6ONLY, bind it to the "any" address, and start receiving. IPv4 addresses will be presented as IPv6 addresses, in the IPv4-mappedformat.

最好的方法是创建一个也可以接受 IPv4 连接的 IPv6 服务器套接字。为此,请创建一个常规 IPv6 套接字,关闭套接字选项IPV6_V6ONLY,将其绑定到“任何”地址,然后开始接收。IPv4 地址将以IPv4 映射格式显示为 IPv6 地址。

The major difference across systems is whether IPV6_V6ONLYis 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.

系统之间的主要区别IPV6_V6ONLY是 a)是否可用,以及 b) 默认情况下是否打开或关闭。它在 Linux 上默认是关闭的(即允许双栈套接字而没有 setsockopt),而在大多数其他系统上是打开的。

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.

此外,Windows XP 上的 IPv6 堆栈不支持该选项。在这些情况下,您需要创建两个单独的服务器套接字,并将它们放入 select 或多个线程中。

回答by Einstein

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

套接字 API 由 IETF RFC 管理,并且在包括 windows WRT IPv6 在内的所有平台上都应该相同。

For IPv4/IPv6 applications it's ALLabout getaddrinfo()and getnameinfo(). getaddrinfois 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.

对于IPv4 / IPv6应用它的所有getaddrinfo()getnameinfo()getaddrinfo是个天才 - 查看客户端的 DNS、端口名称和功能来解决“我可以使用 IPv4、IPv6 或两者来到达特定目的地吗?”这个永恒的问题。或者,如果您要使用双栈路由并希望它返回 IPv4 映射的 IPv6 地址,它也会这样做。

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.

它提供了一个直接sockaddr *可以插入结构bind()recvfrom()sendto()和地址族socket()......在许多情况下,这意味着,没有凌乱的sockaddr_in(6)结构,填写及处理。

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.

对于 UDP 实现,我会小心设置双栈套接字,或者更一般地说,绑定到所有接口 ( INADDR_ANY)。经典的问题是,当地址没有锁定(参见bind())到特定接口并且系统有多个接口请求时,基于操作系统路由表的奇思妙想,对于具有多个地址的计算机,响应可能从不同的地址传输,混淆应用程序协议— 尤其是任何具有身份验证要求的系统。

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.

对于这不是问题的 UDP 实现或 TCP,双栈套接字可以在启用 IPv* 系统时节省大量时间。必须小心不要完全依赖双栈,因为在这不是绝对必要的情况下,不乏合理的平台(旧 Linux、BSD、Windows 2003)部署了不支持双栈套接字的 IPv6 栈。

回答by Dave

I've been playing with this under Windows and it actually does appear to be a security issue there, if you bind to the loopback address then the IPv6 socket is correctly bound to [::1] but the mapped IPv4 socket is bound to INADDR_ANY, so your (supposedly) safely local-only app is actually exposed to the world.

我一直在 Windows 下玩这个,它实际上似乎是一个安全问题,如果你绑定到环回地址,那么 IPv6 套接字正确绑定到 [::1] 但映射的 IPv4 套接字绑定到 INADDR_ANY ,所以你的(据说)安全的本地应用程序实际上是向世界公开的。

回答by Owen DeLong

The RFCs don't really specify the existence of the IPV6_V6ONLY socket option, but, if it is absent, the RFCs are pretty clear that the implementation should be as though that option is FALSE.

RFC 并没有真正指定 IPV6_V6ONLY 套接字选项的存在,但是,如果它不存在,则 RFC 很清楚实现应该就像该选项是 FALSE。

Where the option is present, I would argue that it should default FALSE, but, for reasons passing understanding, BSD and Windows implementations default to TRUE. There is a bizarre claim that this is a security concern because an unknowing IPv6 programmer could bind thinking they were binding only to IN6ADDR_ANY for only IPv6 and accidentally accept an IPv4 connection causing a security problem. I think this is both far-fetched and absurd in addition to a surprise to anyone expecting an RFC-compliant implementation.

如果存在该选项,我认为它应该默认为 FALSE,但是,出于理解的原因,BSD 和 Windows 实现默认为 TRUE。有一种奇怪的说法认为这是一个安全问题,因为一个不知情的 IPv6 程序员可能会认为他们只绑定到 IN6ADDR_ANY 仅用于 IPv6 并意外接受导致安全问题的 IPv4 连接。我认为这既牵强又荒谬,除了让任何期待 RFC 兼容实现的人感到惊讶之外。

In the case of Windows, non-compiance won't usually be a surprise. In the case of BSD, this is unfortunate at best.

在 Windows 的情况下,不合规通常不会让人感到意外。在 BSD 的情况下,这充其量是不幸的。