c - Windows 中 IP_RECVOPTS 和 IP_RECVRETOPTS 的等效项是什么

标签 c windows sockets winsock2

我正在做一些套接字编程,试图使跨平台兼容。对于 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。

所以,我的问题:

  1. Windows 套接字中是否有等效的常量来替换这两个常量(或者,也许我只需要包含一些其他 header )?
  2. 如果不能,是否可以在 Windows 套接字中接收 IP 选项?
  3. 如果是这样,我在 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_RETOPTSIP_RECVRETOPTS 的别名。 :-/

最佳答案

访问 IP(以及 TCP 和 UDP)选项的唯一可移植标准是通过 sendmsgrecvmsg 的辅助数据参数。

不幸的是,虽然访问是标准化的,但可用选项的具体细节仍然因操作系统而异。

在 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

WSARecvMsgWSASendMsg 的 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/

相关文章:

c++ - 结构体用malloc分配,为什么? C++代码分析

在 Win8/Ubuntu 上编译 Makefile 行为不同?

Java int 到 python int

c++ - C语言编程——寻找素数

c - 为什么 "extern char name[]"中缺少数组索引不会影响 strlen(name) 但会导致 sizeof(name) 出错?

windows - 在 Windows 中使用 OpenAI API key 时出现问题

linux - 是否可以在 linux 中重用 TCP 端口号

file - 编程中流的底层原理是什么?

c - 尝试使用 C 程序通过 SMTP 与电子邮件服务器通信

c++ - GetQueuedCompletionStatusEx() 不返回 per-OVERLAPPED 错误代码