Your code seems to be taken from Linux's uapi/linux/in.h
header file. Those defines were introduced in this commit in Linux v3.12-rc1. As the commit message explains:
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.
Here's also the sibling commit in glibc for reference.
So these were added to "sync" with the userspace C library (glibc) and avoid conflicts. However, this now raises the question: why did glibc have those defines to begin with? Well, just to make use of the #ifdef
directive in several parts of the library. For example, you can see in glibc's 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, "" }
};
Since those were also exported, when including netinet/in.h
users could rely on those definitions in a similar way in their userspace programs as well.
So, to summarize: since users could either include glibc's netinet/in.h
or Linux's linux/in.h
, those files were synchronized, with Linux adapting to glibc, and adding the same defines provided by the glibc header.