您的代码似乎取自 Linux 的 uapi/linux/in.h 头文件。这些定义是在 Linux v3.12-rc1 的this commit 中引入的。正如提交消息所解释的:
The kernel promises not to break the UAPI ABI so I don't
see why we can't just have the two userspace headers
coordinate?
If you include the kernel headers first you get those,
and if you include the glibc headers first you get those,
and the following patch arranges a coordination and
synchronization between the two.
这里还有 glibc 中的sibling commit 供参考。
所以这些被添加到与用户空间 C 库(glibc)“同步”并避免冲突。然而,这现在提出了一个问题:为什么 glibc 一开始就有这些定义?好吧,只是为了在库的几个部分使用#ifdef 指令。比如在glibc的sysdeps/posix/getaddrinfo.c中可以看到:
static const struct gaih_typeproto gaih_inet_typeproto[] =
{
{ 0, 0, 0, false, "" },
{ SOCK_STREAM, IPPROTO_TCP, 0, true, "tcp" },
{ SOCK_DGRAM, IPPROTO_UDP, 0, true, "udp" },
#if defined SOCK_DCCP && defined IPPROTO_DCCP
{ SOCK_DCCP, IPPROTO_DCCP, 0, false, "dccp" },
#endif
#ifdef IPPROTO_UDPLITE
{ SOCK_DGRAM, IPPROTO_UDPLITE, 0, false, "udplite" },
#endif
#ifdef IPPROTO_SCTP
{ SOCK_STREAM, IPPROTO_SCTP, 0, false, "sctp" },
{ SOCK_SEQPACKET, IPPROTO_SCTP, 0, false, "sctp" },
#endif
{ SOCK_RAW, 0, GAI_PROTO_PROTOANY|GAI_PROTO_NOSERVICE, true, "raw" },
{ 0, 0, 0, false, "" }
};
由于这些也被导出,当包含netinet/in.h 时,用户也可以在他们的用户空间程序中以类似的方式依赖这些定义。
因此,总结一下:由于用户可以包含 glibc 的 netinet/in.h 或 Linux 的 linux/in.h,因此这些文件是同步的,Linux 适应 glibc,并添加了 glibc 标头提供的相同定义。