我正在做一些套接字编程,试图使跨平台兼容。对于 Windows 系统,我包括以下 header :
#include <winsock2.h>
#include <ws2tcpip.h>
当我试图在 Windows 上编译我的应用程序时,我收到关于以下常量未定义的错误:
IP_RECVOPTS
IP_RECVRETOPTS
我认为这很奇怪,因为我认为这些是非常常见的套接字选项,但也许我错了。无论哪种方式,果然,Windows 套接字文档中的任何地方都没有列出它们。
Linux 发行版的 in.h
中的文档对这些常量进行了如下说明:
IP_RECVOPTS /* bool; Receive all IP options w/datagram. */
IP_RECVRETOPTS /* bool; Receive IP options for response. */
在该发行版的 in.h
中,第一个的定义值似乎是 6,而第二个的定义值似乎是 7。
所以,我的问题:
- Windows 套接字中是否有等效的常量来替换这两个常量(或者,也许我只需要包含一些其他 header )?
- 如果不能,是否可以在 Windows 套接字中接收 IP 选项?
- 如果是这样,我在 Windows 系统上将这些值分别硬编码为 6 和 7 是否安全,或者它们应该是其他值吗?
更新 1
我今天继续我的 Google 研究。我发现了这两个有趣的花絮。我不知道他们是否帮助我。第一个是 Windows Runtime (WinRT) Socket Address header (WinRTSockAddr.h
) from the MixedRealityToolkit
repository on Microsoft's official GitHub account .它包含以下内容:
#define IP_RECVOPTS 6
#define IP_RETOPTS 7
这与我在其他地方看到的 *nix 值一致(我经常看到 IP_RETOPTS
别名为 IP_RECVRETOPTS
)。但是还有 this alleged Windows Sockets helper header from the "Geek Research Lab"'s GitHub account .我不知道它是否有任何可信度,但它对这些常量有不同的值:
#define IP_RECVOPTS 5 /* bool; receive all IP opts w/dgram */
#define IP_RECVRETOPTS 6 /* bool; receive IP opts for response */
#define IP_RECVDSTADDR 7 /* bool; receive IP dst addr w/dgram */
#define IP_RETOPTS 8 /* ip_opts; set/get IP options */
这在所有方面都是矛盾的:值和 IP_RETOPTS
到 IP_RECVRETOPTS
的别名。 :-/
最佳答案
访问 IP(以及 TCP 和 UDP)选项的唯一可移植标准是通过 sendmsg
和 recvmsg
的辅助数据参数。
不幸的是,虽然访问是标准化的,但可用选项的具体细节仍然因操作系统而异。
在 Linux 上,请参阅 cmsg man page并注意:
CONFORMING TO
This ancillary data model conforms to the POSIX.1g draft, 4.4BSD- Lite, the IPv6 advanced API described in RFC 2292 and SUSv2.
CMSG_ALIGN()
is a Linux extension.
相比之下,the man page describing IP_RECVOPTS
没有“符合”部分。
类似的 Windows 文档页面是 "IPPROTO_IP Socket Options" on MSDN
在 WSARecvMsg
和 WSASendMsg
的 Windows 文档中,有以下注释:
based on the Posix.1g specification for the msghdr structure
_WSAMSG
的文档列出可从 Winsock 获得的辅助数据。
POSIX 文档是 here
不幸的是可移植性:
The system documentation shall specify the cmsg_type definitions for the supported protocols.
也就是说,实际可用的辅助数据是不可移植的,因为它不是由 POSIX 和/或单一 Unix 规范指定的。
关于c - Windows 中 IP_RECVOPTS 和 IP_RECVRETOPTS 的等效项是什么,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54403985/