1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446
|
.\" Copyright (c) 2001-2003 The Open Group, All Rights Reserved
.TH "<sys/socket.h>" P 2003 POSIX
.\" <sys/socket.h>
.SH NAME
sys/socket.h \- main sockets header
.SH SYNOPSIS
.LP
\fB#include <sys/socket.h>\fP
.SH DESCRIPTION
.LP
The \fI<sys/socket.h>\fP header shall define the type \fBsocklen_t\fP,
which is an integer type of width of at least 32
bits; see APPLICATION USAGE.
.LP
The \fI<sys/socket.h>\fP header shall define the unsigned integer
type \fBsa_family_t\fP.
.LP
The \fI<sys/socket.h>\fP header shall define the \fBsockaddr\fP structure
that includes at least the following
members:
.sp
.RS
.nf
\fBsa_family_t sa_family \fP Address family. \fB
char sa_data[] \fP Socket address (variable-length data). \fB
\fP
.fi
.RE
.LP
The \fBsockaddr\fP structure is used to define a socket address which
is used in the \fIbind\fP(), \fIconnect\fP(), \fIgetpeername\fP(),
\fIgetsockname\fP(),
\fIrecvfrom\fP(), and \fIsendto\fP()
functions.
.LP
The \fI<sys/socket.h>\fP header shall define the \fBsockaddr_storage\fP
structure. This structure shall be:
.IP " *" 3
Large enough to accommodate all supported protocol-specific address
structures
.LP
.IP " *" 3
Aligned at an appropriate boundary so that pointers to it can be cast
as pointers to protocol-specific address structures and
used to access the fields of those structures without alignment problems
.LP
.LP
The \fBsockaddr_storage\fP structure shall contain at least the following
members:
.sp
.RS
.nf
\fBsa_family_t ss_family
\fP
.fi
.RE
.LP
When a \fBsockaddr_storage\fP structure is cast as a \fBsockaddr\fP
structure, the \fIss_family\fP field of the
\fBsockaddr_storage\fP structure shall map onto the \fIsa_family\fP
field of the \fBsockaddr\fP structure. When a
\fBsockaddr_storage\fP structure is cast as a protocol-specific address
structure, the \fIss_family\fP field shall map onto a
field of that structure that is of type \fBsa_family_t\fP and that
identifies the protocol's address family.
.LP
The \fI<sys/socket.h>\fP header shall define the \fBmsghdr\fP structure
that includes at least the following
members:
.sp
.RS
.nf
\fBvoid *msg_name \fP Optional address. \fB
socklen_t msg_namelen \fP Size of address. \fB
struct iovec *msg_iov \fP Scatter/gather array. \fB
int msg_iovlen \fP Members in \fImsg_iov\fP. \fB
void *msg_control \fP Ancillary data; see below. \fB
socklen_t msg_controllen \fP Ancillary data buffer \fIlen\fP. \fB
int msg_flags \fP Flags on received message. \fB
\fP
.fi
.RE
.LP
The \fBmsghdr\fP structure is used to minimize the number of directly
supplied parameters to the \fIrecvmsg\fP() and \fIsendmsg\fP() functions.
This
structure is used as a \fIvalue\fP- \fIresult\fP parameter in the
\fIrecvmsg\fP()
function and \fIvalue\fP only for the \fIsendmsg\fP() function.
.LP
The \fBiovec\fP structure shall be defined as described in \fI<sys/uio.h>\fP
\&.
.LP
The \fI<sys/socket.h>\fP header shall define the \fBcmsghdr\fP structure
that includes at least the following
members:
.sp
.RS
.nf
\fBsocklen_t cmsg_len \fP Data byte count, including the \fBcmsghdr\fP. \fB
int cmsg_level \fP Originating protocol. \fB
int cmsg_type \fP Protocol-specific type. \fB
\fP
.fi
.RE
.LP
The \fBcmsghdr\fP structure is used for storage of ancillary data
object information.
.LP
Ancillary data consists of a sequence of pairs, each consisting of
a \fBcmsghdr\fP structure followed by a data array. The data
array contains the ancillary data message, and the \fBcmsghdr\fP structure
contains descriptive information that allows an
application to correctly parse the data.
.LP
The values for \fIcmsg_level\fP shall be legal values for the \fIlevel\fP
argument to the \fIgetsockopt\fP() and \fIsetsockopt\fP()
functions. The system documentation shall specify the \fIcmsg_type\fP
definitions for the supported protocols.
.LP
Ancillary data is also possible at the socket level. The \fI<sys/socket.h>\fP
header defines the following macro for use
as the \fIcmsg_type\fP value when \fIcmsg_level\fP is SOL_SOCKET:
.TP 7
SCM_RIGHTS
Indicates that the data array contains the access rights to be sent
or received.
.sp
.LP
The \fI<sys/socket.h>\fP header defines the following macros to gain
access to the data arrays in the ancillary data
associated with a message header:
.TP 7
CMSG_DATA(\fIcmsg\fP)
.sp
If the argument is a pointer to a \fBcmsghdr\fP structure, this macro
shall return an unsigned character pointer to the data array
associated with the \fBcmsghdr\fP structure.
.TP 7
CMSG_NXTHDR(\fImhdr,cmsg\fP)
.sp
If the first argument is a pointer to a \fBmsghdr\fP structure and
the second argument is a pointer to a \fBcmsghdr\fP structure
in the ancillary data pointed to by the \fImsg_control\fP field of
that \fBmsghdr\fP structure, this macro shall return a pointer
to the next \fBcmsghdr\fP structure, or a null pointer if this structure
is the last \fBcmsghdr\fP in the ancillary data.
.TP 7
CMSG_FIRSTHDR(\fImhdr\fP)
.sp
If the argument is a pointer to a \fBmsghdr\fP structure, this macro
shall return a pointer to the first \fBcmsghdr\fP structure
in the ancillary data associated with this \fBmsghdr\fP structure,
or a null pointer if there is no ancillary data associated with
the \fBmsghdr\fP structure.
.sp
.LP
The \fI<sys/socket.h>\fP header shall define the \fBlinger\fP structure
that includes at least the following
members:
.sp
.RS
.nf
\fBint l_onoff \fP Indicates whether linger option is enabled. \fB
int l_linger \fP Linger time, in seconds. \fB
\fP
.fi
.RE
.LP
The \fI<sys/socket.h>\fP header shall define the following macros,
with distinct integer values:
.TP 7
SOCK_DGRAM
Datagram socket.
.TP 7
SOCK_RAW
Raw Protocol Interface.
.TP 7
SOCK_SEQPACKET
Sequenced-packet socket.
.TP 7
SOCK_STREAM
Byte-stream socket.
.sp
.LP
The \fI<sys/socket.h>\fP header shall define the following macro for
use as the \fIlevel\fP argument of \fIsetsockopt\fP() and \fIgetsockopt\fP().
.TP 7
SOL_SOCKET
Options to be accessed at socket level, not protocol level.
.sp
.LP
The \fI<sys/socket.h>\fP header shall define the following macros,
with distinct integer values, for use as the
\fIoption_name\fP argument in \fIgetsockopt\fP() or \fIsetsockopt\fP()
calls:
.TP 7
SO_ACCEPTCONN
Socket is accepting connections.
.TP 7
SO_BROADCAST
Transmission of broadcast messages is supported.
.TP 7
SO_DEBUG
Debugging information is being recorded.
.TP 7
SO_DONTROUTE
Bypass normal routing.
.TP 7
SO_ERROR
Socket error status.
.TP 7
SO_KEEPALIVE
Connections are kept alive with periodic messages.
.TP 7
SO_LINGER
Socket lingers on close.
.TP 7
SO_OOBINLINE
Out-of-band data is transmitted in line.
.TP 7
SO_RCVBUF
Receive buffer size.
.TP 7
SO_RCVLOWAT
Receive ``low water mark''.
.TP 7
SO_RCVTIMEO
Receive timeout.
.TP 7
SO_REUSEADDR
Reuse of local addresses is supported.
.TP 7
SO_SNDBUF
Send buffer size.
.TP 7
SO_SNDLOWAT
Send ``low water mark''.
.TP 7
SO_SNDTIMEO
Send timeout.
.TP 7
SO_TYPE
Socket type.
.sp
.LP
The \fI<sys/socket.h>\fP header shall define the following macro as
the maximum \fIbacklog\fP queue length which may be
specified by the \fIbacklog\fP field of the \fIlisten\fP() function:
.TP 7
SOMAXCONN
The maximum \fIbacklog\fP queue length.
.sp
.LP
The \fI<sys/socket.h>\fP header shall define the following macros,
with distinct integer values, for use as the valid
values for the \fImsg_flags\fP field in the \fBmsghdr\fP structure,
or the \fIflags\fP parameter in \fIrecvfrom\fP(), \fIrecvmsg\fP(),
\fIsendmsg\fP(), or \fIsendto\fP() calls:
.TP 7
MSG_CTRUNC
Control data truncated.
.TP 7
MSG_DONTROUTE
Send without using routing tables.
.TP 7
MSG_EOR
Terminates a record (if supported by the protocol).
.TP 7
MSG_OOB
Out-of-band data.
.TP 7
MSG_PEEK
Leave received data in queue.
.TP 7
MSG_TRUNC
Normal data truncated.
.TP 7
MSG_WAITALL
Attempt to fill the read buffer.
.sp
.LP
The \fI<sys/socket.h>\fP header shall define the following macros,
with distinct integer values:
.TP 7
AF_INET
Internet domain sockets for use with IPv4 addresses.
.TP 7
AF_INET6
Internet domain sockets for use with IPv6 addresses.
.TP 7
AF_UNIX
UNIX domain sockets.
.TP 7
AF_UNSPEC
Unspecified.
.sp
.LP
The \fI<sys/socket.h>\fP header shall define the following macros,
with distinct integer values:
.TP 7
SHUT_RD
Disables further receive operations.
.TP 7
SHUT_RDWR
Disables further send and receive operations.
.TP 7
SHUT_WR
Disables further send operations.
.sp
.LP
The following shall be declared as functions and may also be defined
as macros. Function prototypes shall be provided.
.sp
.RS
.nf
\fBint accept(int, struct sockaddr *restrict, socklen_t *restrict);
int bind(int, const struct sockaddr *, socklen_t);
int connect(int, const struct sockaddr *, socklen_t);
int getpeername(int, struct sockaddr *restrict, socklen_t *restrict);
int getsockname(int, struct sockaddr *restrict, socklen_t *restrict);
int getsockopt(int, int, int, void *restrict, socklen_t *restrict);
int listen(int, int);
ssize_t recv(int, void *, size_t, int);
ssize_t recvfrom(int, void *restrict, size_t, int,
struct sockaddr *restrict, socklen_t *restrict);
ssize_t recvmsg(int, struct msghdr *, int);
ssize_t send(int, const void *, size_t, int);
ssize_t sendmsg(int, const struct msghdr *, int);
ssize_t sendto(int, const void *, size_t, int, const struct sockaddr *,
socklen_t);
int setsockopt(int, int, int, const void *, socklen_t);
int shutdown(int, int);
int socket(int, int, int);
int sockatmark(int);
int socketpair(int, int, int, int[2]);
\fP
.fi
.RE
.LP
Inclusion of \fI<sys/socket.h>\fP may also make visible all symbols
from \fI<sys/uio.h>\fP.
.LP
\fIThe following sections are informative.\fP
.SH APPLICATION USAGE
.LP
To forestall portability problems, it is recommended that applications
not use values larger than 2**31
-1 for the \fBsocklen_t\fP type.
.LP
The \fBsockaddr_storage\fP structure solves the problem of declaring
storage for automatic variables which is both large enough
and aligned enough for storing the socket address data structure of
any family. For example, code with a file descriptor and
without the context of the address family can pass a pointer to a
variable of this type, where a pointer to a socket address
structure is expected in calls such as \fIgetpeername\fP(), and determine
the
address family by accessing the received content after the call.
.LP
The example below illustrates a data structure which aligns on a 64-bit
boundary. An implementation-defined field
\fI_ss_align\fP following \fI_ss_pad1\fP is used to force a 64-bit
alignment which covers proper alignment good enough for needs
of at least \fBsockaddr_in6\fP (IPv6) and \fBsockaddr_in\fP (IPv4)
address data structures. The size of padding field
\fI_ss_pad1\fP depends on the chosen alignment boundary. The size
of padding field \fI_ss_pad2\fP depends on the value of overall
size chosen for the total size of the structure. This size and alignment
are represented in the above example by
implementation-defined (not required) constants _SS_MAXSIZE (chosen
value 128) and _SS_ALIGNMENT (with chosen value 8). Constants
_SS_PAD1SIZE (derived value 6) and _SS_PAD2SIZE (derived value 112)
are also for illustration and not required. The
implementation-defined definitions and structure field names above
start with an underscore to denote implementation private name
space. Portable code is not expected to access or reference those
fields or constants.
.sp
.RS
.nf
\fB/*
* Desired design of maximum size and alignment.
*/
#define _SS_MAXSIZE 128
/* Implementation-defined maximum size. */
#define _SS_ALIGNSIZE (sizeof(int64_t))
/* Implementation-defined desired alignment. */
.sp
/*
* Definitions used for sockaddr_storage structure paddings design.
*/
#define _SS_PAD1SIZE (_SS_ALIGNSIZE - sizeof(sa_family_t))
#define _SS_PAD2SIZE (_SS_MAXSIZE - (sizeof(sa_family_t)+ \\
_SS_PAD1SIZE + _SS_ALIGNSIZE))
struct sockaddr_storage {
sa_family_t ss_family; /* Address family. */
/*
* Following fields are implementation-defined.
*/
char _ss_pad1[_SS_PAD1SIZE];
/* 6-byte pad; this is to make implementation-defined
pad up to alignment field that follows explicit in
the data structure. */
int64_t _ss_align; /* Field to force desired structure
storage alignment. */
char _ss_pad2[_SS_PAD2SIZE];
/* 112-byte pad to achieve desired size,
_SS_MAXSIZE value minus size of ss_family
__ss_pad1, __ss_align fields is 112. */
};
\fP
.fi
.RE
.SH RATIONALE
.LP
None.
.SH FUTURE DIRECTIONS
.LP
None.
.SH SEE ALSO
.LP
\fI<sys/uio.h>\fP , the System Interfaces volume of
IEEE\ Std\ 1003.1-2001, \fIaccept\fP(), \fIbind\fP(), \fIconnect\fP(),
\fIgetpeername\fP(), \fIgetsockname\fP(),
\fIgetsockopt\fP(), \fIlisten\fP(), \fIrecv\fP(), \fIrecvfrom\fP(),
\fIrecvmsg\fP(), \fIsend\fP(), \fIsendmsg\fP(), \fIsendto\fP(), \fIsetsockopt\fP(),
\fIshutdown\fP(), \fIsocket\fP(), \fIsocketpair\fP()
.SH COPYRIGHT
Portions of this text are reprinted and reproduced in electronic form
from IEEE Std 1003.1, 2003 Edition, Standard for Information Technology
-- Portable Operating System Interface (POSIX), The Open Group Base
Specifications Issue 6, Copyright (C) 2001-2003 by the Institute of
Electrical and Electronics Engineers, Inc and The Open Group. In the
event of any discrepancy between this version and the original IEEE and
The Open Group Standard, the original IEEE and The Open Group Standard
is the referee document. The original Standard can be obtained online at
http://www.opengroup.org/unix/online.html .
|