socket.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405
  1. /* System-specific socket constants and types. Linux version.
  2. Copyright (C) 1991-2026 Free Software Foundation, Inc.
  3. This file is part of the GNU C Library.
  4. The GNU C Library is free software; you can redistribute it and/or
  5. modify it under the terms of the GNU Lesser General Public
  6. License as published by the Free Software Foundation; either
  7. version 2.1 of the License, or (at your option) any later version.
  8. The GNU C Library is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. Lesser General Public License for more details.
  12. You should have received a copy of the GNU Lesser General Public
  13. License along with the GNU C Library; if not, see
  14. <https://www.gnu.org/licenses/>. */
  15. #ifndef __BITS_SOCKET_H
  16. #define __BITS_SOCKET_H
  17. #ifndef _SYS_SOCKET_H
  18. # error "Never include <bits/socket.h> directly; use <sys/socket.h> instead."
  19. #endif
  20. #define __need_size_t
  21. #include <stddef.h>
  22. #include <sys/types.h>
  23. /* Type for length arguments in socket calls. */
  24. #ifndef __socklen_t_defined
  25. typedef __socklen_t socklen_t;
  26. # define __socklen_t_defined
  27. #endif
  28. /* Get the architecture-dependent definition of enum __socket_type. */
  29. #include <bits/socket_type.h>
  30. /* Protocol families. */
  31. #define PF_UNSPEC 0 /* Unspecified. */
  32. #define PF_LOCAL 1 /* Local to host (pipes and file-domain). */
  33. #define PF_UNIX PF_LOCAL /* POSIX name for PF_LOCAL. */
  34. #define PF_FILE PF_LOCAL /* Another non-standard name for PF_LOCAL. */
  35. #define PF_INET 2 /* IP protocol family. */
  36. #define PF_AX25 3 /* Amateur Radio AX.25. */
  37. #define PF_IPX 4 /* Novell Internet Protocol. */
  38. #define PF_APPLETALK 5 /* Appletalk DDP. */
  39. #define PF_NETROM 6 /* Amateur radio NetROM. */
  40. #define PF_BRIDGE 7 /* Multiprotocol bridge. */
  41. #define PF_ATMPVC 8 /* ATM PVCs. */
  42. #define PF_X25 9 /* Reserved for X.25 project. */
  43. #define PF_INET6 10 /* IP version 6. */
  44. #define PF_ROSE 11 /* Amateur Radio X.25 PLP. */
  45. #define PF_DECnet 12 /* Reserved for DECnet project. */
  46. #define PF_NETBEUI 13 /* Reserved for 802.2LLC project. */
  47. #define PF_SECURITY 14 /* Security callback pseudo AF. */
  48. #define PF_KEY 15 /* PF_KEY key management API. */
  49. #define PF_NETLINK 16
  50. #define PF_ROUTE PF_NETLINK /* Alias to emulate 4.4BSD. */
  51. #define PF_PACKET 17 /* Packet family. */
  52. #define PF_ASH 18 /* Ash. */
  53. #define PF_ECONET 19 /* Acorn Econet. */
  54. #define PF_ATMSVC 20 /* ATM SVCs. */
  55. #define PF_RDS 21 /* RDS sockets. */
  56. #define PF_SNA 22 /* Linux SNA Project */
  57. #define PF_IRDA 23 /* IRDA sockets. */
  58. #define PF_PPPOX 24 /* PPPoX sockets. */
  59. #define PF_WANPIPE 25 /* Wanpipe API sockets. */
  60. #define PF_LLC 26 /* Linux LLC. */
  61. #define PF_IB 27 /* Native InfiniBand address. */
  62. #define PF_MPLS 28 /* MPLS. */
  63. #define PF_CAN 29 /* Controller Area Network. */
  64. #define PF_TIPC 30 /* TIPC sockets. */
  65. #define PF_BLUETOOTH 31 /* Bluetooth sockets. */
  66. #define PF_IUCV 32 /* IUCV sockets. */
  67. #define PF_RXRPC 33 /* RxRPC sockets. */
  68. #define PF_ISDN 34 /* mISDN sockets. */
  69. #define PF_PHONET 35 /* Phonet sockets. */
  70. #define PF_IEEE802154 36 /* IEEE 802.15.4 sockets. */
  71. #define PF_CAIF 37 /* CAIF sockets. */
  72. #define PF_ALG 38 /* Algorithm sockets. */
  73. #define PF_NFC 39 /* NFC sockets. */
  74. #define PF_VSOCK 40 /* vSockets. */
  75. #define PF_KCM 41 /* Kernel Connection Multiplexor. */
  76. #define PF_QIPCRTR 42 /* Qualcomm IPC Router. */
  77. #define PF_SMC 43 /* SMC sockets. */
  78. #define PF_XDP 44 /* XDP sockets. */
  79. #define PF_MCTP 45 /* Management component transport protocol. */
  80. #define PF_MAX 46 /* For now.. */
  81. /* Address families. */
  82. #define AF_UNSPEC PF_UNSPEC
  83. #define AF_LOCAL PF_LOCAL
  84. #define AF_UNIX PF_UNIX
  85. #define AF_FILE PF_FILE
  86. #define AF_INET PF_INET
  87. #define AF_AX25 PF_AX25
  88. #define AF_IPX PF_IPX
  89. #define AF_APPLETALK PF_APPLETALK
  90. #define AF_NETROM PF_NETROM
  91. #define AF_BRIDGE PF_BRIDGE
  92. #define AF_ATMPVC PF_ATMPVC
  93. #define AF_X25 PF_X25
  94. #define AF_INET6 PF_INET6
  95. #define AF_ROSE PF_ROSE
  96. #define AF_DECnet PF_DECnet
  97. #define AF_NETBEUI PF_NETBEUI
  98. #define AF_SECURITY PF_SECURITY
  99. #define AF_KEY PF_KEY
  100. #define AF_NETLINK PF_NETLINK
  101. #define AF_ROUTE PF_ROUTE
  102. #define AF_PACKET PF_PACKET
  103. #define AF_ASH PF_ASH
  104. #define AF_ECONET PF_ECONET
  105. #define AF_ATMSVC PF_ATMSVC
  106. #define AF_RDS PF_RDS
  107. #define AF_SNA PF_SNA
  108. #define AF_IRDA PF_IRDA
  109. #define AF_PPPOX PF_PPPOX
  110. #define AF_WANPIPE PF_WANPIPE
  111. #define AF_LLC PF_LLC
  112. #define AF_IB PF_IB
  113. #define AF_MPLS PF_MPLS
  114. #define AF_CAN PF_CAN
  115. #define AF_TIPC PF_TIPC
  116. #define AF_BLUETOOTH PF_BLUETOOTH
  117. #define AF_IUCV PF_IUCV
  118. #define AF_RXRPC PF_RXRPC
  119. #define AF_ISDN PF_ISDN
  120. #define AF_PHONET PF_PHONET
  121. #define AF_IEEE802154 PF_IEEE802154
  122. #define AF_CAIF PF_CAIF
  123. #define AF_ALG PF_ALG
  124. #define AF_NFC PF_NFC
  125. #define AF_VSOCK PF_VSOCK
  126. #define AF_KCM PF_KCM
  127. #define AF_QIPCRTR PF_QIPCRTR
  128. #define AF_SMC PF_SMC
  129. #define AF_XDP PF_XDP
  130. #define AF_MCTP PF_MCTP
  131. #define AF_MAX PF_MAX
  132. /* Socket level values. Others are defined in the appropriate headers.
  133. XXX These definitions also should go into the appropriate headers as
  134. far as they are available. */
  135. #define SOL_RAW 255
  136. #define SOL_DECNET 261
  137. #define SOL_X25 262
  138. #define SOL_PACKET 263
  139. #define SOL_ATM 264 /* ATM layer (cell level). */
  140. #define SOL_AAL 265 /* ATM Adaption Layer (packet level). */
  141. #define SOL_IRDA 266
  142. #define SOL_NETBEUI 267
  143. #define SOL_LLC 268
  144. #define SOL_DCCP 269
  145. #define SOL_NETLINK 270
  146. #define SOL_TIPC 271
  147. #define SOL_RXRPC 272
  148. #define SOL_PPPOL2TP 273
  149. #define SOL_BLUETOOTH 274
  150. #define SOL_PNPIPE 275
  151. #define SOL_RDS 276
  152. #define SOL_IUCV 277
  153. #define SOL_CAIF 278
  154. #define SOL_ALG 279
  155. #define SOL_NFC 280
  156. #define SOL_KCM 281
  157. #define SOL_TLS 282
  158. #define SOL_XDP 283
  159. #define SOL_MPTCP 284
  160. #define SOL_MCTP 285
  161. #define SOL_SMC 286
  162. #define SOL_VSOCK 287
  163. /* Maximum queue length specifiable by listen. */
  164. #define SOMAXCONN 4096
  165. /* Get the definition of the macro to define the common sockaddr members. */
  166. #include <bits/sockaddr.h>
  167. /* Structure describing a generic socket address. */
  168. struct __attribute_struct_may_alias__ sockaddr
  169. {
  170. __SOCKADDR_COMMON (sa_); /* Common data: address family and length. */
  171. char sa_data[14]; /* Address data. */
  172. };
  173. /* Structure large enough to hold any socket address (with the historical
  174. exception of AF_UNIX). */
  175. #define __ss_aligntype unsigned long int
  176. #define _SS_PADSIZE \
  177. (_SS_SIZE - __SOCKADDR_COMMON_SIZE - sizeof (__ss_aligntype))
  178. struct __attribute_struct_may_alias__ sockaddr_storage
  179. {
  180. __SOCKADDR_COMMON (ss_); /* Address family, etc. */
  181. char __ss_padding[_SS_PADSIZE];
  182. __ss_aligntype __ss_align; /* Force desired alignment. */
  183. };
  184. /* Bits in the FLAGS argument to `send', `recv', et al. */
  185. enum
  186. {
  187. MSG_OOB = 0x01, /* Process out-of-band data. */
  188. #define MSG_OOB MSG_OOB
  189. MSG_PEEK = 0x02, /* Peek at incoming messages. */
  190. #define MSG_PEEK MSG_PEEK
  191. MSG_DONTROUTE = 0x04, /* Don't use local routing. */
  192. #define MSG_DONTROUTE MSG_DONTROUTE
  193. #ifdef __USE_GNU
  194. /* DECnet uses a different name. */
  195. MSG_TRYHARD = MSG_DONTROUTE,
  196. # define MSG_TRYHARD MSG_DONTROUTE
  197. #endif
  198. MSG_CTRUNC = 0x08, /* Control data lost before delivery. */
  199. #define MSG_CTRUNC MSG_CTRUNC
  200. MSG_PROXY = 0x10, /* Supply or ask second address. */
  201. #define MSG_PROXY MSG_PROXY
  202. MSG_TRUNC = 0x20,
  203. #define MSG_TRUNC MSG_TRUNC
  204. MSG_DONTWAIT = 0x40, /* Nonblocking IO. */
  205. #define MSG_DONTWAIT MSG_DONTWAIT
  206. MSG_EOR = 0x80, /* End of record. */
  207. #define MSG_EOR MSG_EOR
  208. MSG_WAITALL = 0x100, /* Wait for a full request. */
  209. #define MSG_WAITALL MSG_WAITALL
  210. MSG_FIN = 0x200,
  211. #define MSG_FIN MSG_FIN
  212. MSG_SYN = 0x400,
  213. #define MSG_SYN MSG_SYN
  214. MSG_CONFIRM = 0x800, /* Confirm path validity. */
  215. #define MSG_CONFIRM MSG_CONFIRM
  216. MSG_RST = 0x1000,
  217. #define MSG_RST MSG_RST
  218. MSG_ERRQUEUE = 0x2000, /* Fetch message from error queue. */
  219. #define MSG_ERRQUEUE MSG_ERRQUEUE
  220. MSG_NOSIGNAL = 0x4000, /* Do not generate SIGPIPE. */
  221. #define MSG_NOSIGNAL MSG_NOSIGNAL
  222. MSG_MORE = 0x8000, /* Sender will send more. */
  223. #define MSG_MORE MSG_MORE
  224. MSG_WAITFORONE = 0x10000, /* Wait for at least one packet to return.*/
  225. #define MSG_WAITFORONE MSG_WAITFORONE
  226. MSG_BATCH = 0x40000, /* sendmmsg: more messages coming. */
  227. #define MSG_BATCH MSG_BATCH
  228. MSG_SOCK_DEVMEM = 0x2000000, /* Receive devmem skbs as cmsg. */
  229. #define MSG_SOCK_DEVMEM MSG_SOCK_DEVMEM
  230. MSG_ZEROCOPY = 0x4000000, /* Use user data in kernel path. */
  231. #define MSG_ZEROCOPY MSG_ZEROCOPY
  232. MSG_FASTOPEN = 0x20000000, /* Send data in TCP SYN. */
  233. #define MSG_FASTOPEN MSG_FASTOPEN
  234. MSG_CMSG_CLOEXEC = 0x40000000 /* Set close_on_exit for file
  235. descriptor received through
  236. SCM_RIGHTS. */
  237. #define MSG_CMSG_CLOEXEC MSG_CMSG_CLOEXEC
  238. };
  239. /* Structure describing messages sent by
  240. `sendmsg' and received by `recvmsg'. */
  241. struct msghdr
  242. {
  243. void *msg_name; /* Address to send to/receive from. */
  244. socklen_t msg_namelen; /* Length of address data. */
  245. struct iovec *msg_iov; /* Vector of data to send/receive into. */
  246. size_t msg_iovlen; /* Number of elements in the vector. */
  247. void *msg_control; /* Ancillary data (eg BSD filedesc passing). */
  248. size_t msg_controllen; /* Ancillary data buffer length.
  249. !! The type should be socklen_t but the
  250. definition of the kernel is incompatible
  251. with this. */
  252. int msg_flags; /* Flags on received message. */
  253. };
  254. /* Structure used for storage of ancillary data object information. */
  255. struct cmsghdr
  256. {
  257. size_t cmsg_len; /* Length of data in cmsg_data plus length
  258. of cmsghdr structure.
  259. !! The type should be socklen_t but the
  260. definition of the kernel is incompatible
  261. with this. */
  262. int cmsg_level; /* Originating protocol. */
  263. int cmsg_type; /* Protocol specific type. */
  264. #if __glibc_c99_flexarr_available
  265. __extension__ unsigned char __cmsg_data __flexarr; /* Ancillary data. */
  266. #endif
  267. };
  268. /* Ancillary data object manipulation macros. */
  269. #if __glibc_c99_flexarr_available
  270. # define CMSG_DATA(cmsg) ((cmsg)->__cmsg_data)
  271. #else
  272. # define CMSG_DATA(cmsg) ((unsigned char *) ((struct cmsghdr *) (cmsg) + 1))
  273. #endif
  274. #define CMSG_NXTHDR(mhdr, cmsg) __cmsg_nxthdr (mhdr, cmsg)
  275. #define CMSG_FIRSTHDR(mhdr) \
  276. ((size_t) (mhdr)->msg_controllen >= sizeof (struct cmsghdr) \
  277. ? (struct cmsghdr *) (mhdr)->msg_control : (struct cmsghdr *) 0)
  278. #define CMSG_ALIGN(len) (((len) + sizeof (size_t) - 1) \
  279. & (size_t) ~(sizeof (size_t) - 1))
  280. #define CMSG_SPACE(len) (CMSG_ALIGN (len) \
  281. + CMSG_ALIGN (sizeof (struct cmsghdr)))
  282. #define CMSG_LEN(len) (CMSG_ALIGN (sizeof (struct cmsghdr)) + (len))
  283. /* Given a length, return the additional padding necessary such that
  284. len + __CMSG_PADDING(len) == CMSG_ALIGN (len). */
  285. #define __CMSG_PADDING(len) ((sizeof (size_t) \
  286. - ((len) & (sizeof (size_t) - 1))) \
  287. & (sizeof (size_t) - 1))
  288. extern struct cmsghdr *__cmsg_nxthdr (struct msghdr *__mhdr,
  289. struct cmsghdr *__cmsg) __THROW;
  290. #ifdef __USE_EXTERN_INLINES
  291. # ifndef _EXTERN_INLINE
  292. # define _EXTERN_INLINE __extern_inline
  293. # endif
  294. _EXTERN_INLINE struct cmsghdr *
  295. __NTH (__cmsg_nxthdr (struct msghdr *__mhdr, struct cmsghdr *__cmsg))
  296. {
  297. /* We may safely assume that __cmsg lies between __mhdr->msg_control and
  298. __mhdr->msg_controllen because the user is required to obtain the first
  299. cmsg via CMSG_FIRSTHDR, set its length, then obtain subsequent cmsgs
  300. via CMSG_NXTHDR, setting lengths along the way. However, we don't yet
  301. trust the value of __cmsg->cmsg_len and therefore do not use it in any
  302. pointer arithmetic until we check its value. */
  303. unsigned char * __msg_control_ptr = (unsigned char *) __mhdr->msg_control;
  304. unsigned char * __cmsg_ptr = (unsigned char *) __cmsg;
  305. size_t __size_needed = sizeof (struct cmsghdr)
  306. + __CMSG_PADDING (__cmsg->cmsg_len);
  307. /* The current header is malformed, too small to be a full header. */
  308. if ((size_t) __cmsg->cmsg_len < sizeof (struct cmsghdr))
  309. return (struct cmsghdr *) 0;
  310. /* There isn't enough space between __cmsg and the end of the buffer to
  311. hold the current cmsg *and* the next one. */
  312. if (((size_t)
  313. (__msg_control_ptr + __mhdr->msg_controllen - __cmsg_ptr)
  314. < __size_needed)
  315. || ((size_t)
  316. (__msg_control_ptr + __mhdr->msg_controllen - __cmsg_ptr
  317. - __size_needed)
  318. < __cmsg->cmsg_len))
  319. return (struct cmsghdr *) 0;
  320. /* Now, we trust cmsg_len and can use it to find the next header. */
  321. __cmsg = (struct cmsghdr *) ((unsigned char *) __cmsg
  322. + CMSG_ALIGN (__cmsg->cmsg_len));
  323. return __cmsg;
  324. }
  325. #endif /* Use `extern inline'. */
  326. /* Socket level message types. This must match the definitions in
  327. <linux/socket.h>. */
  328. enum
  329. {
  330. SCM_RIGHTS = 0x01 /* Transfer file descriptors. */
  331. #define SCM_RIGHTS SCM_RIGHTS
  332. #ifdef __USE_GNU
  333. , SCM_CREDENTIALS = 0x02 /* Credentials passing. */
  334. # define SCM_CREDENTIALS SCM_CREDENTIALS
  335. , SCM_SECURITY = 0x03 /* Security label. */
  336. # define SCM_SECURITY SCM_SECURITY
  337. , SCM_PIDFD = 0x04 /* Pidfd. */
  338. # define SCM_PIDFD SCM_PIDFD
  339. #endif
  340. };
  341. #ifdef __USE_GNU
  342. /* User visible structure for SCM_CREDENTIALS message */
  343. struct ucred
  344. {
  345. pid_t pid; /* PID of sending process. */
  346. uid_t uid; /* UID of sending process. */
  347. gid_t gid; /* GID of sending process. */
  348. };
  349. #endif
  350. #ifdef __USE_MISC
  351. # include <bits/types/time_t.h>
  352. # include <asm/socket.h>
  353. #else
  354. # define SO_DEBUG 1
  355. # include <bits/socket-constants.h>
  356. #endif
  357. /* Structure used to manipulate the SO_LINGER option. */
  358. struct linger
  359. {
  360. int l_onoff; /* Nonzero to linger on close. */
  361. int l_linger; /* Time to linger. */
  362. };
  363. #endif /* bits/socket.h */