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
11

NAME

13       sys/socket.h — main sockets header
14

SYNOPSIS

16       #include <sys/socket.h>
17

DESCRIPTION

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

APPLICATION USAGE

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

RATIONALE

330       None.
331

FUTURE DIRECTIONS

333       None.
334

SEE ALSO

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