1<sys/socket.h>(0P)         POSIX Programmer's Manual        <sys/socket.h>(0P)
2
3
4

NAME

6       sys/socket.h - main sockets header
7

SYNOPSIS

9       #include <sys/socket.h>
10

DESCRIPTION

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

APPLICATION USAGE

307       To forestall portability problems, it is recommended that  applications
308       not use values larger than 2**31 -1 for the socklen_t type.
309
310       The  sockaddr_storage structure solves the problem of declaring storage
311       for automatic variables which is both large enough and  aligned  enough
312       for  storing the socket address data structure of any family. For exam‐
313       ple, code with a file descriptor and without the context of the address
314       family  can  pass a pointer to a variable of this type, where a pointer
315       to a socket address structure is expected in  calls  such  as  getpeer‐
316       name(), and determine the address family by accessing the received con‐
317       tent after the call.
318
319       The example below illustrates a data structure which aligns on a 64-bit
320       boundary.  An implementation-defined field _ss_align following _ss_pad1
321       is used to force a 64-bit alignment which covers proper alignment  good
322       enough for needs of at least sockaddr_in6 (IPv6) and sockaddr_in (IPv4)
323       address data structures. The size of padding field _ss_pad1 depends  on
324       the  chosen  alignment  boundary.  The  size  of padding field _ss_pad2
325       depends on the value of overall size chosen for the total size  of  the
326       structure. This size and alignment are represented in the above example
327       by implementation-defined (not required) constants _SS_MAXSIZE  (chosen
328       value   128)   and  _SS_ALIGNMENT  (with  chosen  value  8).  Constants
329       _SS_PAD1SIZE (derived value 6) and _SS_PAD2SIZE (derived value 112) are
330       also for illustration and not required. The implementation-defined def‐
331       initions and structure field names above start with  an  underscore  to
332       denote implementation private name space. Portable code is not expected
333       to access or reference those fields or constants.
334
335
336              /*
337               *  Desired design of maximum size and alignment.
338               */
339              #define _SS_MAXSIZE 128
340                  /* Implementation-defined maximum size. */
341              #define _SS_ALIGNSIZE (sizeof(int64_t))
342                  /* Implementation-defined desired alignment. */
343
344
345              /*
346               *  Definitions used for sockaddr_storage structure paddings design.
347               */
348              #define _SS_PAD1SIZE (_SS_ALIGNSIZE - sizeof(sa_family_t))
349              #define _SS_PAD2SIZE (_SS_MAXSIZE - (sizeof(sa_family_t)+ \
350                                    _SS_PAD1SIZE + _SS_ALIGNSIZE))
351              struct sockaddr_storage {
352                  sa_family_t  ss_family;  /* Address family. */
353              /*
354               *  Following fields are implementation-defined.
355               */
356                  char _ss_pad1[_SS_PAD1SIZE];
357                      /* 6-byte pad; this is to make implementation-defined
358                         pad up to alignment field that follows explicit in
359                         the data structure. */
360                  int64_t _ss_align;  /* Field to force desired structure
361                                         storage alignment. */
362                  char _ss_pad2[_SS_PAD2SIZE];
363                      /* 112-byte pad to achieve desired size,
364                         _SS_MAXSIZE value minus size of ss_family
365                         __ss_pad1, __ss_align fields is 112. */
366              };
367

RATIONALE

369       None.
370

FUTURE DIRECTIONS

372       None.
373

SEE ALSO

375       <sys/uio.h>, the  System  Interfaces  volume  of  IEEE Std 1003.1-2001,
376       accept(),  bind(),  connect(),  getpeername(),  getsockname(), getsock‐
377       opt(), listen(),  recv(),  recvfrom(),  recvmsg(),  send(),  sendmsg(),
378       sendto(), setsockopt(), shutdown(), socket(), socketpair()
379
381       Portions  of  this text are reprinted and reproduced in electronic form
382       from IEEE Std 1003.1, 2003 Edition, Standard for Information Technology
383       --  Portable  Operating  System  Interface (POSIX), The Open Group Base
384       Specifications Issue 6, Copyright (C) 2001-2003  by  the  Institute  of
385       Electrical  and  Electronics  Engineers, Inc and The Open Group. In the
386       event of any discrepancy between this version and the original IEEE and
387       The  Open Group Standard, the original IEEE and The Open Group Standard
388       is the referee document. The original Standard can be  obtained  online
389       at http://www.opengroup.org/unix/online.html .
390
391
392
393IEEE/The Open Group                  2003                   <sys/socket.h>(0P)
Impressum