1sys_socket.h(0P)           POSIX Programmer's Manual          sys_socket.h(0P)
2
3
4

PROLOG

6       This  manual  page is part of the POSIX Programmer's Manual.  The Linux
7       implementation of this interface may differ (consult the  corresponding
8       Linux  manual page for details of Linux behavior), or the interface may
9       not be implemented on Linux.
10

NAME

12       sys/socket.h — main sockets header
13

SYNOPSIS

15       #include <sys/socket.h>
16

DESCRIPTION

18       The <sys/socket.h> header shall define the socklen_t type, which is  an
19       integer type of width of at least 32 bits; see APPLICATION USAGE.
20
21       The <sys/socket.h> header shall define the sa_family_t unsigned integer
22       type.
23
24       The <sys/socket.h> header shall define the  sockaddr  structure,  which
25       shall include at least the following members:
26
27
28           sa_family_t  sa_family  Address family.
29           char         sa_data[]  Socket address (variable-length data).
30
31       The sockaddr structure is used to define a socket address which is used
32       in the bind(), connect(), getpeername(), getsockname(), recvfrom(), and
33       sendto() functions.
34
35       The  <sys/socket.h> header shall define the sockaddr_storage structure,
36       which shall be:
37
38        *  Large enough to accommodate all supported protocol-specific address
39           structures
40
41        *  Aligned  at  an  appropriate boundary so that pointers to it can be
42           cast as pointers to protocol-specific address structures  and  used
43           to access the fields of those structures without alignment problems
44
45       The  sockaddr_storage  structure  shall  include at least the following
46       members:
47
48
49           sa_family_t   ss_family
50
51       When a pointer to a sockaddr_storage structure is cast as a pointer  to
52       a  sockaddr  structure,  the  ss_family  field  of the sockaddr_storage
53       structure shall map onto the sa_family field of the sockaddr structure.
54       When  a pointer to a sockaddr_storage structure is cast as a pointer to
55       a protocol-specific address structure, the ss_family  field  shall  map
56       onto  a  field  of  that structure that is of type sa_family_t and that
57       identifies the protocol's address family.
58
59       The <sys/socket.h> header shall  define  the  msghdr  structure,  which
60       shall include at least the following members:
61
62
63           void          *msg_name        Optional address.
64           socklen_t      msg_namelen     Size of address.
65           struct iovec  *msg_iov         Scatter/gather array.
66           int            msg_iovlen      Members in msg_iov.
67           void          *msg_control     Ancillary data; see below.
68           socklen_t      msg_controllen  Ancillary data buffer len.
69           int            msg_flags       Flags on received message.
70
71       The  msghdr  structure  is used to minimize the number of directly sup‐
72       plied parameters to the recvmsg() and sendmsg() functions. This  struc‐
73       ture  is used as a valueresult parameter in the recvmsg() function and
74       value only for the sendmsg() function.
75
76       The <sys/socket.h> header shall define the iovec structure as described
77       in <sys/uio.h>.
78
79       The  <sys/socket.h>  header  shall  define the cmsghdr structure, which
80       shall include at least the following members:
81
82
83           socklen_t  cmsg_len    Data byte count, including the cmsghdr.
84           int        cmsg_level  Originating protocol.
85           int        cmsg_type   Protocol-specific type.
86
87       The cmsghdr structure is used for  storage  of  ancillary  data  object
88       information.
89
90       Ancillary  data  consists  of a sequence of pairs, each consisting of a
91       cmsghdr structure followed by a data array. The data array contains the
92       ancillary  data message, and the cmsghdr structure contains descriptive
93       information that allows an application to correctly parse the data.
94
95       The values for cmsg_level shall be legal values for the level  argument
96       to  the  getsockopt() and setsockopt() functions. The system documenta‐
97       tion shall specify the cmsg_type definitions for the  supported  proto‐
98       cols.
99
100       Ancillary data is also possible at the socket level. The <sys/socket.h>
101       header shall define the following symbolic  constant  for  use  as  the
102       cmsg_type value when cmsg_level is SOL_SOCKET:
103
104       SCM_RIGHTS    Indicates  that the data array contains the access rights
105                     to be sent or received.
106
107       The <sys/socket.h> header shall define the  following  macros  to  gain
108       access  to the data arrays in the ancillary data associated with a mes‐
109       sage header:
110
111       CMSG_DATA(cmsg)
112             If the argument is a pointer to a cmsghdr structure,  this  macro
113             shall  return  an  unsigned  character  pointer to the data array
114             associated with the cmsghdr structure.
115
116       CMSG_NXTHDR(mhdr,cmsg)
117             If the first argument is a pointer to a msghdr structure and  the
118             second argument is a pointer to a cmsghdr structure in the ancil‐
119             lary data pointed to by the  msg_control  field  of  that  msghdr
120             structure,  this macro shall return a pointer to the next cmsghdr
121             structure, or a null pointer if this structure is the  last  cms‐
122             ghdr in the ancillary data.
123
124       CMSG_FIRSTHDR(mhdr)
125             If  the  argument  is a pointer to a msghdr structure, this macro
126             shall return a pointer to the  first  cmsghdr  structure  in  the
127             ancillary  data  associated with this msghdr structure, or a null
128             pointer if there is no ancillary data associated with the  msghdr
129             structure.
130
131       The  <sys/socket.h>  header  shall  define  the linger structure, which
132       shall include at least the following members:
133
134
135           int  l_onoff   Indicates whether linger option is enabled.
136           int  l_linger  Linger time, in seconds.
137
138       The <sys/socket.h> header shall define the following symbolic constants
139       with distinct values:
140
141       SOCK_DGRAM    Datagram socket.
142
143       SOCK_RAW      Raw Protocol Interface.
144
145       SOCK_SEQPACKET
146                     Sequenced-packet socket.
147
148       SOCK_STREAM   Byte-stream socket.
149
150       The  <sys/socket.h> header shall define the following symbolic constant
151       for use as the level argument of setsockopt() and getsockopt().
152
153       SOL_SOCKET    Options to be accessed  at  socket  level,  not  protocol
154                     level.
155
156       The <sys/socket.h> header shall define the following symbolic constants
157       with distinct values for use as the option_name  argument  in  getsock‐
158       opt()  or  setsockopt()  calls  (see  the  System  Interfaces volume of
159       POSIX.1‐2017, Section 2.10.16, Use of Options):
160
161       SO_ACCEPTCONN Socket is accepting connections.
162
163       SO_BROADCAST  Transmission of broadcast messages is supported.
164
165       SO_DEBUG      Debugging information is being recorded.
166
167       SO_DONTROUTE  Bypass normal routing.
168
169       SO_ERROR      Socket error status.
170
171       SO_KEEPALIVE  Connections are kept alive with periodic messages.
172
173       SO_LINGER     Socket lingers on close.
174
175       SO_OOBINLINE  Out-of-band data is transmitted in line.
176
177       SO_RCVBUF     Receive buffer size.
178
179       SO_RCVLOWAT   Receive ``low water mark''.
180
181       SO_RCVTIMEO   Receive timeout.
182
183       SO_REUSEADDR  Reuse of local addresses is supported.
184
185       SO_SNDBUF     Send buffer size.
186
187       SO_SNDLOWAT   Send ``low water mark''.
188
189       SO_SNDTIMEO   Send timeout.
190
191       SO_TYPE       Socket type.
192
193       The <sys/socket.h> header shall define the following symbolic  constant
194       for  use  as the maximum backlog queue length which may be specified by
195       the backlog field of the listen() function:
196
197       SOMAXCONN     The maximum backlog queue length.
198
199       The <sys/socket.h> header shall define the following symbolic constants
200       with  distinct  values  for  use  as the valid values for the msg_flags
201       field in the msghdr  structure,  or  the  flags  parameter  in  recv(),
202       recvfrom(), recvmsg(), send(), sendmsg(), or sendto() calls:
203
204       MSG_CTRUNC    Control data truncated.
205
206       MSG_DONTROUTE Send without using routing tables.
207
208       MSG_EOR       Terminates a record (if supported by the protocol).
209
210       MSG_OOB       Out-of-band data.
211
212       MSG_NOSIGNAL  No SIGPIPE generated when an attempt to send is made on a
213                     stream-oriented socket that is no longer connected.
214
215       MSG_PEEK      Leave received data in queue.
216
217       MSG_TRUNC     Normal data truncated.
218
219       MSG_WAITALL   Attempt to fill the read buffer.
220
221       The <sys/socket.h> header shall define the following symbolic constants
222       with distinct values:
223
224       AF_INET       Internet domain sockets for use with IPv4 addresses.
225
226       AF_INET6      Internet domain sockets for use with IPv6 addresses.
227
228       AF_UNIX       UNIX domain sockets.
229
230       AF_UNSPEC     Unspecified.
231
232       The value of AF_UNSPEC shall be 0.
233
234       The <sys/socket.h> header shall define the following symbolic constants
235       with distinct values:
236
237       SHUT_RD       Disables further receive operations.
238
239       SHUT_RDWR     Disables further send and receive operations.
240
241       SHUT_WR       Disables further send operations.
242
243       The <sys/socket.h> header shall define the size_t and ssize_t types  as
244       described in <sys/types.h>.
245
246       The following shall be declared as functions and may also be defined as
247       macros. Function prototypes shall be provided.
248
249
250           int     accept(int, struct sockaddr *restrict, socklen_t *restrict);
251           int     bind(int, const struct sockaddr *, socklen_t);
252           int     connect(int, const struct sockaddr *, socklen_t);
253           int     getpeername(int, struct sockaddr *restrict, socklen_t *restrict);
254           int     getsockname(int, struct sockaddr *restrict, socklen_t *restrict);
255           int     getsockopt(int, int, int, void *restrict, socklen_t *restrict);
256           int     listen(int, int);
257           ssize_t recv(int, void *, size_t, int);
258           ssize_t recvfrom(int, void *restrict, size_t, int,
259                   struct sockaddr *restrict, socklen_t *restrict);
260           ssize_t recvmsg(int, struct msghdr *, int);
261           ssize_t send(int, const void *, size_t, int);
262           ssize_t sendmsg(int, const struct msghdr *, int);
263           ssize_t sendto(int, const void *, size_t, int, const struct sockaddr *,
264                   socklen_t);
265           int     setsockopt(int, int, int, const void *, socklen_t);
266           int     shutdown(int, int);
267           int     sockatmark(int);
268           int     socket(int, int, int);
269           int     socketpair(int, int, int, int [2]);
270
271       Inclusion of <sys/socket.h> may also  make  visible  all  symbols  from
272       <sys/uio.h>.
273
274       The following sections are informative.
275

APPLICATION USAGE

277       To  forestall portability problems, it is recommended that applications
278       not use values larger than 231 -1 for the socklen_t type.
279
280       The sockaddr_storage structure solves the problem of declaring  storage
281       for  automatic  variables which is both large enough and aligned enough
282       for storing the socket address data structure of any family. For  exam‐
283       ple, code with a file descriptor and without the context of the address
284       family can pass a pointer to a variable of this type, where  a  pointer
285       to  a  socket  address  structure is expected in calls such as getpeer‐
286       name(), and determine the address family by accessing the received con‐
287       tent after the call.
288
289       The example below illustrates a data structure which aligns on a 64-bit
290       boundary. An implementation-defined field _ss_align following  _ss_pad1
291       is  used to force a 64-bit alignment which covers proper alignment good
292       enough for needs of at least sockaddr_in6 (IPv6) and sockaddr_in (IPv4)
293       address  data structures. The size of padding field _ss_pad1 depends on
294       the chosen alignment boundary.  The  size  of  padding  field  _ss_pad2
295       depends  on  the value of overall size chosen for the total size of the
296       structure. This size and alignment are represented in the above example
297       by  implementation-defined (not required) constants _SS_MAXSIZE (chosen
298       value  128)  and  _SS_ALIGNMENT  (with  chosen  value   8).   Constants
299       _SS_PAD1SIZE (derived value 6) and _SS_PAD2SIZE (derived value 112) are
300       also for illustration and not required. The implementation-defined def‐
301       initions  and structure field names above start with an <underscore> to
302       denote implementation private name space. Portable code is not expected
303       to access or reference those fields or constants.
304
305
306           /*
307            *  Desired design of maximum size and alignment.
308            */
309           #define _SS_MAXSIZE 128
310               /* Implementation-defined maximum size. */
311           #define _SS_ALIGNSIZE (sizeof(int64_t))
312               /* Implementation-defined desired alignment. */
313
314           /*
315            *  Definitions used for sockaddr_storage structure paddings design.
316            */
317           #define _SS_PAD1SIZE (_SS_ALIGNSIZE - sizeof(sa_family_t))
318           #define _SS_PAD2SIZE (_SS_MAXSIZE - (sizeof(sa_family_t)+ \
319                                 _SS_PAD1SIZE + _SS_ALIGNSIZE))
320           struct sockaddr_storage {
321               sa_family_t  ss_family;  /* Address family. */
322           /*
323            *  Following fields are implementation-defined.
324            */
325               char _ss_pad1[_SS_PAD1SIZE];
326                   /* 6-byte pad; this is to make implementation-defined
327                      pad up to alignment field that follows explicit in
328                      the data structure. */
329               int64_t _ss_align;  /* Field to force desired structure
330                                      storage alignment. */
331               char _ss_pad2[_SS_PAD2SIZE];
332                   /* 112-byte pad to achieve desired size,
333                      _SS_MAXSIZE value minus size of ss_family
334                      __ss_pad1, __ss_align fields is 112. */
335           };
336

RATIONALE

338       None.
339

FUTURE DIRECTIONS

341       None.
342

SEE ALSO

344       <sys_types.h>, <sys_uio.h>
345
346       The  System  Interfaces  volume of POSIX.1‐2017, accept(), bind(), con‐
347       nect(), getpeername(), getsockname(), getsockopt(),  listen(),  recv(),
348       recvfrom(), recvmsg(), send(), sendmsg(), sendto(), setsockopt(), shut‐
349       down(), sockatmark(), socket(), socketpair()
350
352       Portions of this text are reprinted and reproduced in  electronic  form
353       from  IEEE Std 1003.1-2017, Standard for Information Technology -- Por‐
354       table Operating System Interface (POSIX), The Open Group Base  Specifi‐
355       cations  Issue  7, 2018 Edition, Copyright (C) 2018 by the Institute of
356       Electrical and Electronics Engineers, Inc and The Open Group.   In  the
357       event of any discrepancy between this version and the original IEEE and
358       The Open Group Standard, the original IEEE and The Open Group  Standard
359       is  the  referee document. The original Standard can be obtained online
360       at http://www.opengroup.org/unix/online.html .
361
362       Any typographical or formatting errors that appear  in  this  page  are
363       most likely to have been introduced during the conversion of the source
364       files to man page format. To report such errors,  see  https://www.ker
365       nel.org/doc/man-pages/reporting_bugs.html .
366
367
368
369IEEE/The Open Group                  2017                     sys_socket.h(0P)
Impressum