Files
2024-02-19 00:21:47 -05:00

491 lines
18 KiB
HTML

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta name="generator" content="HTML Tidy, see www.w3.org">
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<link type="text/css" rel="stylesheet" href="style.css"><!-- Generated by The Open Group's rhtm tool v1.2.1 -->
<!-- Copyright (c) 2001 The Open Group, All Rights Reserved -->
<title>&lt;sys/socket.h&gt;</title>
</head>
<body bgcolor="white">
<script type="text/javascript" language="JavaScript" src="../../jscript/dotcode.js">
</script>
<basefont size="3"> <a name="&lt;sys/socket.h&gt;"></a> <a name="tag_13_61"></a><!-- &lt;sys/socket.h&gt; -->
<!--header start-->
<center><font size="2">The Open Group Base Specifications Issue 6<br>
IEEE Std 1003.1-2001<br>
Copyright &copy; 2001 The IEEE and The Open Group, All Rights reserved.</font></center>
<!--header end-->
<hr size="2" noshade>
<h4><a name="tag_13_61_01"></a>NAME</h4>
<blockquote>sys/socket.h - main sockets header</blockquote>
<h4><a name="tag_13_61_02"></a>SYNOPSIS</h4>
<blockquote class="synopsis">
<p><tt>#include &lt;sys/socket.h&gt;</tt></p>
</blockquote>
<h4><a name="tag_13_61_03"></a>DESCRIPTION</h4>
<blockquote>
<p>The <i>&lt;sys/socket.h&gt;</i> header shall define the type <b>socklen_t</b>, which is an integer type of width of at least 32
bits; see APPLICATION USAGE.</p>
<p>The <i>&lt;sys/socket.h&gt;</i> header shall define the unsigned integer type <b>sa_family_t</b>.</p>
<p>The <i>&lt;sys/socket.h&gt;</i> header shall define the <b>sockaddr</b> structure that includes at least the following
members:</p>
<pre>
<tt>sa_family_t sa_family </tt> Address family. <tt>
char sa_data[] </tt> Socket address (variable-length data). <tt>
</tt>
</pre>
<p>The <b>sockaddr</b> structure is used to define a socket address which is used in the <a href=
"../../functions/bind.html"><i>bind</i>()</a>, <a href="../../functions/connect.html"><i>connect</i>()</a>, <a href=
"../../functions/getpeername.html"><i>getpeername</i>()</a>, <a href="../../functions/getsockname.html"><i>getsockname</i>()</a>,
<a href="../../functions/recvfrom.html"><i>recvfrom</i>()</a>, and <a href="../../functions/sendto.html"><i>sendto</i>()</a>
functions.</p>
<p>The <i>&lt;sys/socket.h&gt;</i> header shall define the <b>sockaddr_storage</b> structure. This structure shall be:</p>
<ul>
<li>
<p>Large enough to accommodate all supported protocol-specific address structures</p>
</li>
<li>
<p>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</p>
</li>
</ul>
<p>The <b>sockaddr_storage</b> structure shall contain at least the following members:</p>
<pre>
<tt>sa_family_t ss_family
</tt>
</pre>
<p>When a <b>sockaddr_storage</b> structure is cast as a <b>sockaddr</b> structure, the <i>ss_family</i> field of the
<b>sockaddr_storage</b> structure shall map onto the <i>sa_family</i> field of the <b>sockaddr</b> structure. When a
<b>sockaddr_storage</b> structure is cast as a protocol-specific address structure, the <i>ss_family</i> field shall map onto a
field of that structure that is of type <b>sa_family_t</b> and that identifies the protocol's address family.</p>
<p>The <i>&lt;sys/socket.h&gt;</i> header shall define the <b>msghdr</b> structure that includes at least the following
members:</p>
<pre>
<tt>void *msg_name </tt> Optional address. <tt>
socklen_t msg_namelen </tt> Size of address. <tt>
struct iovec *msg_iov </tt> Scatter/gather array. <tt>
int msg_iovlen </tt> Members in <i>msg_iov</i>. <tt>
void *msg_control </tt> Ancillary data; see below. <tt>
socklen_t msg_controllen </tt> Ancillary data buffer <i>len</i>. <tt>
int msg_flags </tt> Flags on received message. <tt>
</tt>
</pre>
<p>The <b>msghdr</b> structure is used to minimize the number of directly supplied parameters to the <a href=
"../../functions/recvmsg.html"><i>recvmsg</i>()</a> and <a href="../../functions/sendmsg.html"><i>sendmsg</i>()</a> functions. This
structure is used as a <i>value</i>- <i>result</i> parameter in the <a href="../../functions/recvmsg.html"><i>recvmsg</i>()</a>
function and <i>value</i> only for the <a href="../../functions/sendmsg.html"><i>sendmsg</i>()</a> function.</p>
<p>The <b>iovec</b> structure shall be defined as described in <a href="../sys/uio.h.html#tag_13_68"><i>&lt;sys/uio.h&gt;</i></a>
.</p>
<p>The <i>&lt;sys/socket.h&gt;</i> header shall define the <b>cmsghdr</b> structure that includes at least the following
members:</p>
<pre>
<tt>socklen_t cmsg_len </tt> Data byte count, including the <b>cmsghdr</b>. <tt>
int cmsg_level </tt> Originating protocol. <tt>
int cmsg_type </tt> Protocol-specific type. <tt>
</tt>
</pre>
<p>The <b>cmsghdr</b> structure is used for storage of ancillary data object information.</p>
<p>Ancillary data consists of a sequence of pairs, each consisting of a <b>cmsghdr</b> structure followed by a data array. The data
array contains the ancillary data message, and the <b>cmsghdr</b> structure contains descriptive information that allows an
application to correctly parse the data.</p>
<p>The values for <i>cmsg_level</i> shall be legal values for the <i>level</i> argument to the <a href=
"../../functions/getsockopt.html"><i>getsockopt</i>()</a> and <a href="../../functions/setsockopt.html"><i>setsockopt</i>()</a>
functions. The system documentation shall specify the <i>cmsg_type</i> definitions for the supported protocols.</p>
<p>Ancillary data is also possible at the socket level. The <i>&lt;sys/socket.h&gt;</i> header defines the following macro for use
as the <i>cmsg_type</i> value when <i>cmsg_level</i> is SOL_SOCKET:</p>
<dl compact>
<dt>SCM_RIGHTS</dt>
<dd>Indicates that the data array contains the access rights to be sent or received.</dd>
</dl>
<p>The <i>&lt;sys/socket.h&gt;</i> header defines the following macros to gain access to the data arrays in the ancillary data
associated with a message header:</p>
<dl compact>
<dt>CMSG_DATA(<i>cmsg</i>)</dt>
<dd><br>
If the argument is a pointer to a <b>cmsghdr</b> structure, this macro shall return an unsigned character pointer to the data array
associated with the <b>cmsghdr</b> structure.</dd>
<dt>CMSG_NXTHDR(<i>mhdr,cmsg</i>)</dt>
<dd><br>
If the first argument is a pointer to a <b>msghdr</b> structure and the second argument is a pointer to a <b>cmsghdr</b> structure
in the ancillary data pointed to by the <i>msg_control</i> field of that <b>msghdr</b> structure, this macro shall return a pointer
to the next <b>cmsghdr</b> structure, or a null pointer if this structure is the last <b>cmsghdr</b> in the ancillary data.</dd>
<dt>CMSG_FIRSTHDR(<i>mhdr</i>)</dt>
<dd><br>
If the argument is a pointer to a <b>msghdr</b> structure, this macro shall return a pointer to the first <b>cmsghdr</b> structure
in the ancillary data associated with this <b>msghdr</b> structure, or a null pointer if there is no ancillary data associated with
the <b>msghdr</b> structure.</dd>
</dl>
<p>The <i>&lt;sys/socket.h&gt;</i> header shall define the <b>linger</b> structure that includes at least the following
members:</p>
<pre>
<tt>int l_onoff </tt> Indicates whether linger option is enabled. <tt>
int l_linger </tt> Linger time, in seconds. <tt>
</tt>
</pre>
<p>The <i>&lt;sys/socket.h&gt;</i> header shall define the following macros, with distinct integer values:</p>
<dl compact>
<dt>SOCK_DGRAM</dt>
<dd>Datagram socket.</dd>
<dt>SOCK_RAW</dt>
<dd><sup>[<a href="javascript:open_code('RS')">RS</a>]</sup> <img src="../../images/opt-start.gif" alt="[Option Start]" border="0">
Raw Protocol Interface. <img src="../../images/opt-end.gif" alt="[Option End]" border="0"></dd>
<dt>SOCK_SEQPACKET</dt>
<dd>Sequenced-packet socket.</dd>
<dt>SOCK_STREAM</dt>
<dd>Byte-stream socket.</dd>
</dl>
<p>The <i>&lt;sys/socket.h&gt;</i> header shall define the following macro for use as the <i>level</i> argument of <a href=
"../../functions/setsockopt.html"><i>setsockopt</i>()</a> and <a href=
"../../functions/getsockopt.html"><i>getsockopt</i>()</a>.</p>
<dl compact>
<dt>SOL_SOCKET</dt>
<dd>Options to be accessed at socket level, not protocol level.</dd>
</dl>
<p>The <i>&lt;sys/socket.h&gt;</i> header shall define the following macros, with distinct integer values, for use as the
<i>option_name</i> argument in <a href="../../functions/getsockopt.html"><i>getsockopt</i>()</a> or <a href=
"../../functions/setsockopt.html"><i>setsockopt</i>()</a> calls:</p>
<dl compact>
<dt>SO_ACCEPTCONN</dt>
<dd>Socket is accepting connections.</dd>
<dt>SO_BROADCAST</dt>
<dd>Transmission of broadcast messages is supported.</dd>
<dt>SO_DEBUG</dt>
<dd>Debugging information is being recorded.</dd>
<dt>SO_DONTROUTE</dt>
<dd>Bypass normal routing.</dd>
<dt>SO_ERROR</dt>
<dd>Socket error status.</dd>
<dt>SO_KEEPALIVE</dt>
<dd>Connections are kept alive with periodic messages.</dd>
<dt>SO_LINGER</dt>
<dd>Socket lingers on close.</dd>
<dt>SO_OOBINLINE</dt>
<dd>Out-of-band data is transmitted in line.</dd>
<dt>SO_RCVBUF</dt>
<dd>Receive buffer size.</dd>
<dt>SO_RCVLOWAT</dt>
<dd>Receive ``low water mark''.</dd>
<dt>SO_RCVTIMEO</dt>
<dd>Receive timeout.</dd>
<dt>SO_REUSEADDR</dt>
<dd>Reuse of local addresses is supported.</dd>
<dt>SO_SNDBUF</dt>
<dd>Send buffer size.</dd>
<dt>SO_SNDLOWAT</dt>
<dd>Send ``low water mark''.</dd>
<dt>SO_SNDTIMEO</dt>
<dd>Send timeout.</dd>
<dt>SO_TYPE</dt>
<dd>Socket type.</dd>
</dl>
<p>The <i>&lt;sys/socket.h&gt;</i> header shall define the following macro as the maximum <i>backlog</i> queue length which may be
specified by the <i>backlog</i> field of the <a href="../../functions/listen.html"><i>listen</i>()</a> function:</p>
<dl compact>
<dt>SOMAXCONN</dt>
<dd>The maximum <i>backlog</i> queue length.</dd>
</dl>
<p>The <i>&lt;sys/socket.h&gt;</i> header shall define the following macros, with distinct integer values, for use as the valid
values for the <i>msg_flags</i> field in the <b>msghdr</b> structure, or the <i>flags</i> parameter in <a href=
"../../functions/recvfrom.html"><i>recvfrom</i>()</a>, <a href="../../functions/recvmsg.html"><i>recvmsg</i>()</a>, <a href=
"../../functions/sendmsg.html"><i>sendmsg</i>()</a>, or <a href="../../functions/sendto.html"><i>sendto</i>()</a> calls:</p>
<dl compact>
<dt>MSG_CTRUNC</dt>
<dd>Control data truncated.</dd>
<dt>MSG_DONTROUTE</dt>
<dd>Send without using routing tables.</dd>
<dt>MSG_EOR</dt>
<dd>Terminates a record (if supported by the protocol).</dd>
<dt>MSG_OOB</dt>
<dd>Out-of-band data.</dd>
<dt>MSG_PEEK</dt>
<dd>Leave received data in queue.</dd>
<dt>MSG_TRUNC</dt>
<dd>Normal data truncated.</dd>
<dt>MSG_WAITALL</dt>
<dd>Attempt to fill the read buffer.</dd>
</dl>
<p>The <i>&lt;sys/socket.h&gt;</i> header shall define the following macros, with distinct integer values:</p>
<dl compact>
<dt>AF_INET</dt>
<dd>Internet domain sockets for use with IPv4 addresses.</dd>
<dt>AF_INET6</dt>
<dd><sup>[<a href="javascript:open_code('IP6')">IP6</a>]</sup> <img src="../../images/opt-start.gif" alt="[Option Start]" border=
"0"> Internet domain sockets for use with IPv6 addresses. <img src="../../images/opt-end.gif" alt="[Option End]" border="0"></dd>
<dt>AF_UNIX</dt>
<dd>UNIX domain sockets.</dd>
<dt>AF_UNSPEC</dt>
<dd>Unspecified.</dd>
</dl>
<p>The <i>&lt;sys/socket.h&gt;</i> header shall define the following macros, with distinct integer values:</p>
<dl compact>
<dt>SHUT_RD</dt>
<dd>Disables further receive operations.</dd>
<dt>SHUT_RDWR</dt>
<dd>Disables further send and receive operations.</dd>
<dt>SHUT_WR</dt>
<dd>Disables further send operations.</dd>
</dl>
<p>The following shall be declared as functions and may also be defined as macros. Function prototypes shall be provided.</p>
<pre>
<tt>int 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]);
</tt>
</pre>
<p>Inclusion of <i>&lt;sys/socket.h&gt;</i> may also make visible all symbols from <a href=
"../../basedefs/sys/uio.h.html"><i>&lt;sys/uio.h&gt;</i></a>.</p>
</blockquote>
<hr>
<div class="box"><em>The following sections are informative.</em></div>
<h4><a name="tag_13_61_04"></a>APPLICATION USAGE</h4>
<blockquote>
<p>To forestall portability problems, it is recommended that applications not use values larger than 2<sup><small>31</small></sup>
-1 for the <b>socklen_t</b> type.</p>
<p>The <b>sockaddr_storage</b> 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 <a href="../../functions/getpeername.html"><i>getpeername</i>()</a>, and determine the
address family by accessing the received content after the call.</p>
<p>The example below illustrates a data structure which aligns on a 64-bit boundary. An implementation-defined field
<i>_ss_align</i> following <i>_ss_pad1</i> is used to force a 64-bit alignment which covers proper alignment good enough for needs
of at least <b>sockaddr_in6</b> (IPv6) and <b>sockaddr_in</b> (IPv4) address data structures. The size of padding field
<i>_ss_pad1</i> depends on the chosen alignment boundary. The size of padding field <i>_ss_pad2</i> 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.</p>
<pre>
<tt>/*
* 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. */
<br>
/*
* 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. */
};
</tt>
</pre>
</blockquote>
<h4><a name="tag_13_61_05"></a>RATIONALE</h4>
<blockquote>
<p>None.</p>
</blockquote>
<h4><a name="tag_13_61_06"></a>FUTURE DIRECTIONS</h4>
<blockquote>
<p>None.</p>
</blockquote>
<h4><a name="tag_13_61_07"></a>SEE ALSO</h4>
<blockquote>
<p><a href="../sys/uio.h.html#tag_13_68"><i>&lt;sys/uio.h&gt;</i></a> , the System Interfaces volume of
IEEE&nbsp;Std&nbsp;1003.1-2001, <a href="../../functions/accept.html"><i>accept</i>()</a>, <a href=
"../../functions/bind.html"><i>bind</i>()</a>, <a href="../../functions/connect.html"><i>connect</i>()</a>, <a href=
"../../functions/getpeername.html"><i>getpeername</i>()</a>, <a href="../../functions/getsockname.html"><i>getsockname</i>()</a>,
<a href="../../functions/getsockopt.html"><i>getsockopt</i>()</a>, <a href="../../functions/listen.html"><i>listen</i>()</a>, <a
href="../../functions/recv.html"><i>recv</i>()</a>, <a href="../../functions/recvfrom.html"><i>recvfrom</i>()</a>, <a href=
"../../functions/recvmsg.html"><i>recvmsg</i>()</a>, <a href="../../functions/send.html"><i>send</i>()</a>, <a href=
"../../functions/sendmsg.html"><i>sendmsg</i>()</a>, <a href="../../functions/sendto.html"><i>sendto</i>()</a>, <a href=
"../../functions/setsockopt.html"><i>setsockopt</i>()</a>, <a href="../../functions/shutdown.html"><i>shutdown</i>()</a>, <a href=
"../../functions/socket.html"><i>socket</i>()</a>, <a href="../../functions/socketpair.html"><i>socketpair</i>()</a></p>
</blockquote>
<h4><a name="tag_13_61_08"></a>CHANGE HISTORY</h4>
<blockquote>
<p>First released in Issue 6. Derived from the XNS, Issue 5.2 specification.</p>
<p>The <b>restrict</b> keyword is added to the prototypes for <a href="../../functions/accept.html"><i>accept</i>()</a>, <a href=
"../../functions/getpeername.html"><i>getpeername</i>()</a>, <a href="../../functions/getsockname.html"><i>getsockname</i>()</a>,
<a href="../../functions/getsockopt.html"><i>getsockopt</i>()</a>, and <a href=
"../../functions/recvfrom.html"><i>recvfrom</i>()</a>.</p>
</blockquote>
<div class="box"><em>End of informative text.</em></div>
<hr>
<hr size="2" noshade>
<center><font size="2"><!--footer start-->
UNIX &reg; is a registered Trademark of The Open Group.<br>
POSIX &reg; is a registered Trademark of The IEEE.<br>
[ <a href="../../mindex.html">Main Index</a> | <a href="../../basedefs/contents.html">XBD</a> | <a href=
"../../utilities/contents.html">XCU</a> | <a href="../../functions/contents.html">XSH</a> | <a href=
"../../xrat/contents.html">XRAT</a> ]</font></center>
<!--footer end-->
<hr size="2" noshade>
</body>
</html>