socket.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  1. //
  2. // Copyright 2026 Aarav Ravindra Kharade
  3. //
  4. // Licensed under the Apache License, Version 2.0 (the "License");
  5. // you may not use this file except in compliance with the License.
  6. // You may obtain a copy of the License at
  7. //
  8. // http://www.apache.org/licenses/LICENSE-2.0
  9. //
  10. // Unless required by applicable law or agreed to in writing, software
  11. // distributed under the License is distributed on an "AS IS" BASIS,
  12. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. // See the License for the specific language governing permissions and
  14. // limitations under the License.
  15. //
  16. /* Declarations of socket constants, types, and functions.
  17. Copyright (C) 1991-2026 Free Software Foundation, Inc.
  18. This file is part of the GNU C Library.
  19. The GNU C Library is free software; you can redistribute it and/or
  20. modify it under the terms of the GNU Lesser General Public
  21. License as published by the Free Software Foundation; either
  22. version 2.1 of the License, or (at your option) any later version.
  23. The GNU C Library is distributed in the hope that it will be useful,
  24. but WITHOUT ANY WARRANTY; without even the implied warranty of
  25. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  26. Lesser General Public License for more details.
  27. You should have received a copy of the GNU Lesser General Public
  28. License along with the GNU C Library; if not, see
  29. <https://www.gnu.org/licenses/>. */
  30. #ifndef _SYS_SOCKET_H
  31. #define _SYS_SOCKET_H 1
  32. #include <features.h>
  33. __BEGIN_DECLS
  34. #include <bits/types/struct_iovec.h>
  35. #define __need_size_t
  36. #include <stddef.h>
  37. /* This operating system-specific header file defines the SOCK_*, PF_*,
  38. AF_*, MSG_*, SOL_*, and SO_* constants, and the `struct sockaddr',
  39. `struct msghdr', and `struct linger' types. */
  40. #include <bits/socket.h>
  41. #ifdef __USE_MISC
  42. # include <bits/types/struct_osockaddr.h>
  43. #endif
  44. /* The following constants should be used for the second parameter of
  45. `shutdown'. */
  46. enum
  47. {
  48. SHUT_RD = 0, /* No more receptions. */
  49. #define SHUT_RD SHUT_RD
  50. SHUT_WR, /* No more transmissions. */
  51. #define SHUT_WR SHUT_WR
  52. SHUT_RDWR /* No more receptions or transmissions. */
  53. #define SHUT_RDWR SHUT_RDWR
  54. };
  55. /* This is the type we use for generic socket address arguments.
  56. With GCC 2.7 and later, the funky union causes redeclarations or
  57. uses with any of the listed types to be allowed without complaint.
  58. G++ 2.7 does not support transparent unions so there we want the
  59. old-style declaration, too. */
  60. #if defined __cplusplus || !__GNUC_PREREQ (2, 7) || !defined __USE_GNU
  61. # define __SOCKADDR_ARG struct sockaddr *__restrict
  62. # define __CONST_SOCKADDR_ARG const struct sockaddr *
  63. #else
  64. /* Add more `struct sockaddr_AF' types here as necessary.
  65. These are all the ones I found on NetBSD and Linux. */
  66. # define __SOCKADDR_ALLTYPES \
  67. __SOCKADDR_ONETYPE (sockaddr) \
  68. __SOCKADDR_ONETYPE (sockaddr_at) \
  69. __SOCKADDR_ONETYPE (sockaddr_ax25) \
  70. __SOCKADDR_ONETYPE (sockaddr_dl) \
  71. __SOCKADDR_ONETYPE (sockaddr_eon) \
  72. __SOCKADDR_ONETYPE (sockaddr_in) \
  73. __SOCKADDR_ONETYPE (sockaddr_in6) \
  74. __SOCKADDR_ONETYPE (sockaddr_inarp) \
  75. __SOCKADDR_ONETYPE (sockaddr_ipx) \
  76. __SOCKADDR_ONETYPE (sockaddr_iso) \
  77. __SOCKADDR_ONETYPE (sockaddr_ns) \
  78. __SOCKADDR_ONETYPE (sockaddr_un) \
  79. __SOCKADDR_ONETYPE (sockaddr_x25)
  80. # define __SOCKADDR_ONETYPE(type) struct type *__restrict __##type##__;
  81. typedef union { __SOCKADDR_ALLTYPES
  82. } __SOCKADDR_ARG __attribute__ ((__transparent_union__));
  83. # undef __SOCKADDR_ONETYPE
  84. # define __SOCKADDR_ONETYPE(type) const struct type *__restrict __##type##__;
  85. typedef union { __SOCKADDR_ALLTYPES
  86. } __CONST_SOCKADDR_ARG __attribute__ ((__transparent_union__));
  87. # undef __SOCKADDR_ONETYPE
  88. #endif
  89. #ifdef __USE_GNU
  90. /* For `recvmmsg' and `sendmmsg'. */
  91. struct mmsghdr
  92. {
  93. struct msghdr msg_hdr; /* Actual message header. */
  94. unsigned int msg_len; /* Number of received or sent bytes for the
  95. entry. */
  96. };
  97. #endif
  98. /* Create a new socket of type TYPE in domain DOMAIN, using
  99. protocol PROTOCOL. If PROTOCOL is zero, one is chosen automatically.
  100. Returns a file descriptor for the new socket, or -1 for errors. */
  101. extern int socket (int __domain, int __type, int __protocol) __THROW;
  102. /* Create two new sockets, of type TYPE in domain DOMAIN and using
  103. protocol PROTOCOL, which are connected to each other, and put file
  104. descriptors for them in FDS[0] and FDS[1]. If PROTOCOL is zero,
  105. one will be chosen automatically. Returns 0 on success, -1 for errors. */
  106. extern int socketpair (int __domain, int __type, int __protocol,
  107. int __fds[2]) __THROW;
  108. /* Give the socket FD the local address ADDR (which is LEN bytes long). */
  109. extern int bind (int __fd, __CONST_SOCKADDR_ARG __addr, socklen_t __len)
  110. __THROW;
  111. /* Put the local address of FD into *ADDR and its length in *LEN. */
  112. extern int getsockname (int __fd, __SOCKADDR_ARG __addr,
  113. socklen_t *__restrict __len) __THROW;
  114. /* Open a connection on socket FD to peer at ADDR (which LEN bytes long).
  115. For connectionless socket types, just set the default address to send to
  116. and the only address from which to accept transmissions.
  117. Return 0 on success, -1 for errors.
  118. This function is a cancellation point and therefore not marked with
  119. __THROW. */
  120. extern int connect (int __fd, __CONST_SOCKADDR_ARG __addr, socklen_t __len);
  121. /* Put the address of the peer connected to socket FD into *ADDR
  122. (which is *LEN bytes long), and its actual length into *LEN. */
  123. extern int getpeername (int __fd, __SOCKADDR_ARG __addr,
  124. socklen_t *__restrict __len) __THROW;
  125. /* Send N bytes of BUF to socket FD. Returns the number sent or -1.
  126. This function is a cancellation point and therefore not marked with
  127. __THROW. */
  128. extern ssize_t send (int __fd, const void *__buf, size_t __n, int __flags);
  129. /* Read N bytes into BUF from socket FD.
  130. Returns the number read or -1 for errors.
  131. This function is a cancellation point and therefore not marked with
  132. __THROW. */
  133. extern ssize_t recv (int __fd, void *__buf, size_t __n, int __flags);
  134. /* Send N bytes of BUF on socket FD to peer at address ADDR (which is
  135. ADDR_LEN bytes long). Returns the number sent, or -1 for errors.
  136. This function is a cancellation point and therefore not marked with
  137. __THROW. */
  138. extern ssize_t sendto (int __fd, const void *__buf, size_t __n,
  139. int __flags, __CONST_SOCKADDR_ARG __addr,
  140. socklen_t __addr_len);
  141. /* Read N bytes into BUF through socket FD.
  142. If ADDR is not NULL, fill in *ADDR_LEN bytes of it with the address of
  143. the sender, and store the actual size of the address in *ADDR_LEN.
  144. Returns the number of bytes read or -1 for errors.
  145. This function is a cancellation point and therefore not marked with
  146. __THROW. */
  147. extern ssize_t recvfrom (int __fd, void *__restrict __buf, size_t __n,
  148. int __flags, __SOCKADDR_ARG __addr,
  149. socklen_t *__restrict __addr_len);
  150. /* Send a message described MESSAGE on socket FD.
  151. Returns the number of bytes sent, or -1 for errors.
  152. This function is a cancellation point and therefore not marked with
  153. __THROW. */
  154. #ifndef __USE_TIME64_REDIRECTS
  155. extern ssize_t sendmsg (int __fd, const struct msghdr *__message,
  156. int __flags);
  157. #else
  158. # ifdef __REDIRECT
  159. extern ssize_t __REDIRECT (sendmsg, (int __fd, const struct msghdr *__message,
  160. int __flags),
  161. __sendmsg64);
  162. # else
  163. extern ssize_t __sendmsg64 (int __fd, const struct msghdr *__message,
  164. int __flags);
  165. # define sendmsg __sendmsg64
  166. # endif
  167. #endif
  168. #ifdef __USE_GNU
  169. /* Send a VLEN messages as described by VMESSAGES to socket FD.
  170. Returns the number of datagrams successfully written or -1 for errors.
  171. This function is a cancellation point and therefore not marked with
  172. __THROW. */
  173. # ifndef __USE_TIME64_REDIRECTS
  174. extern int sendmmsg (int __fd, struct mmsghdr *__vmessages,
  175. unsigned int __vlen, int __flags);
  176. # else
  177. # ifdef __REDIRECT
  178. extern int __REDIRECT (sendmmsg, (int __fd, struct mmsghdr *__vmessages,
  179. unsigned int __vlen, int __flags),
  180. __sendmmsg64);
  181. # else
  182. extern int __sendmmsg64 (int __fd, struct mmsghdr *__vmessages,
  183. unsigned int __vlen, int __flags);
  184. # define sendmmsg __sendmmsg64
  185. # endif
  186. # endif /* __USE_TIME64_REDIRECTS */
  187. #endif /* __USE_GNU */
  188. /* Receive a message as described by MESSAGE from socket FD.
  189. Returns the number of bytes read or -1 for errors.
  190. This function is a cancellation point and therefore not marked with
  191. __THROW. */
  192. #ifndef __USE_TIME64_REDIRECTS
  193. extern ssize_t recvmsg (int __fd, struct msghdr *__message, int __flags);
  194. #else
  195. # ifdef __REDIRECT
  196. extern ssize_t __REDIRECT (recvmsg,
  197. (int __fd, struct msghdr *__message, int __flags),
  198. __recvmsg64);
  199. # else
  200. extern ssize_t __recvmsg64 (int __fd, struct msghdr *__message, int __flags);
  201. # define recvmsg __recvmsg64
  202. # endif
  203. #endif
  204. #ifdef __USE_GNU
  205. /* Receive up to VLEN messages as described by VMESSAGES from socket FD.
  206. Returns the number of messages received or -1 for errors.
  207. This function is a cancellation point and therefore not marked with
  208. __THROW. */
  209. # ifndef __USE_TIME64_REDIRECTS
  210. extern int recvmmsg (int __fd, struct mmsghdr *__vmessages,
  211. unsigned int __vlen, int __flags,
  212. struct timespec *__tmo);
  213. # else
  214. # ifdef __REDIRECT
  215. extern int __REDIRECT (recvmmsg, (int __fd, struct mmsghdr *__vmessages,
  216. unsigned int __vlen, int __flags,
  217. struct timespec *__tmo),
  218. __recvmmsg64);
  219. # else
  220. # define recvmmsg __recvmmsg64
  221. # endif
  222. # endif
  223. #endif
  224. /* Put the current value for socket FD's option OPTNAME at protocol level LEVEL
  225. into OPTVAL (which is *OPTLEN bytes long), and set *OPTLEN to the value's
  226. actual length. Returns 0 on success, -1 for errors. */
  227. #ifndef __USE_TIME64_REDIRECTS
  228. extern int getsockopt (int __fd, int __level, int __optname,
  229. void *__restrict __optval,
  230. socklen_t *__restrict __optlen) __THROW;
  231. #else
  232. # ifdef __REDIRECT
  233. extern int __REDIRECT_NTH (getsockopt,
  234. (int __fd, int __level, int __optname,
  235. void *__restrict __optval,
  236. socklen_t *__restrict __optlen),
  237. __getsockopt64);
  238. # else
  239. extern int __getsockopt64 (int __fd, int __level, int __optname,
  240. void *__restrict __optval,
  241. socklen_t *__restrict __optlen) __THROW;
  242. # define getsockopt __getsockopt64
  243. # endif
  244. #endif
  245. /* Set socket FD's option OPTNAME at protocol level LEVEL
  246. to *OPTVAL (which is OPTLEN bytes long).
  247. Returns 0 on success, -1 for errors. */
  248. #ifndef __USE_TIME64_REDIRECTS
  249. extern int setsockopt (int __fd, int __level, int __optname,
  250. const void *__optval, socklen_t __optlen) __THROW;
  251. #else
  252. # ifdef __REDIRECT
  253. extern int __REDIRECT_NTH (setsockopt,
  254. (int __fd, int __level, int __optname,
  255. const void *__optval, socklen_t __optlen),
  256. __setsockopt64);
  257. # else
  258. extern int __setsockopt64 (int __fd, int __level, int __optname,
  259. const void *__optval, socklen_t __optlen) __THROW;
  260. # define setsockopt __setsockopt64
  261. # endif
  262. #endif
  263. /* Prepare to accept connections on socket FD.
  264. N connection requests will be queued before further requests are refused.
  265. Returns 0 on success, -1 for errors. */
  266. extern int listen (int __fd, int __n) __THROW;
  267. /* Await a connection on socket FD.
  268. When a connection arrives, open a new socket to communicate with it,
  269. set *ADDR (which is *ADDR_LEN bytes long) to the address of the connecting
  270. peer and *ADDR_LEN to the address's actual length, and return the
  271. new socket's descriptor, or -1 for errors.
  272. This function is a cancellation point and therefore not marked with
  273. __THROW. */
  274. extern int accept (int __fd, __SOCKADDR_ARG __addr,
  275. socklen_t *__restrict __addr_len);
  276. #ifdef __USE_GNU
  277. /* Similar to 'accept' but takes an additional parameter to specify flags.
  278. This function is a cancellation point and therefore not marked with
  279. __THROW. */
  280. extern int accept4 (int __fd, __SOCKADDR_ARG __addr,
  281. socklen_t *__restrict __addr_len, int __flags);
  282. #endif
  283. /* Shut down all or part of the connection open on socket FD.
  284. HOW determines what to shut down:
  285. SHUT_RD = No more receptions;
  286. SHUT_WR = No more transmissions;
  287. SHUT_RDWR = No more receptions or transmissions.
  288. Returns 0 on success, -1 for errors. */
  289. extern int shutdown (int __fd, int __how) __THROW;
  290. #ifdef __USE_XOPEN2K
  291. /* Determine whether socket is at a out-of-band mark. */
  292. extern int sockatmark (int __fd) __THROW;
  293. #endif
  294. #ifdef __USE_MISC
  295. /* FDTYPE is S_IFSOCK or another S_IF* macro defined in <sys/stat.h>;
  296. returns 1 if FD is open on an object of the indicated type, 0 if not,
  297. or -1 for errors (setting errno). */
  298. extern int isfdtype (int __fd, int __fdtype) __THROW;
  299. #endif
  300. /* Define some macros helping to catch buffer overflows. */
  301. #if __USE_FORTIFY_LEVEL > 0 && defined __fortify_function
  302. # include <bits/socket2.h>
  303. #endif
  304. __END_DECLS
  305. #endif /* sys/socket.h */