man-pages/man3/cmsg.3.html
2021-03-31 01:06:50 +01:00

347 lines
9.0 KiB
HTML

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<HTML><HEAD><TITLE>Man page of CMSG</TITLE>
</HEAD><BODY>
<H1>CMSG</H1>
Section: Linux Programmer's Manual (3)<BR>Updated: 2020-02-09<BR><A HREF="#index">Index</A>
<A HREF="/cgi-bin/man/man2html">Return to Main Contents</A><HR>
<A NAME="lbAB">&nbsp;</A>
<H2>NAME</H2>
CMSG_ALIGN, CMSG_SPACE, CMSG_NXTHDR, CMSG_FIRSTHDR - access ancillary data
<A NAME="lbAC">&nbsp;</A>
<H2>SYNOPSIS</H2>
<PRE>
<B>#include &lt;<A HREF="file:///usr/include/sys/socket.h">sys/socket.h</A>&gt;</B>
<B>struct cmsghdr *CMSG_FIRSTHDR(struct msghdr *</B><I>msgh</I><B>);</B>
<B>struct cmsghdr *CMSG_NXTHDR(struct msghdr *</B><I>msgh</I><B> ,</B>
<B> struct cmsghdr *</B>cmsg<B>);</B>
<B>size_t CMSG_ALIGN(size_t </B><I>length</I><B>);</B>
<B>size_t CMSG_SPACE(size_t </B><I>length</I><B>);</B>
<B>size_t CMSG_LEN(size_t </B><I>length</I><B>);</B>
<B>unsigned char *CMSG_DATA(struct cmsghdr *</B><I>cmsg</I><B>);</B>
</PRE>
<A NAME="lbAD">&nbsp;</A>
<H2>DESCRIPTION</H2>
These macros are used to create and access control messages (also called
ancillary data) that are not a part of the socket payload.
This control information may
include the interface the packet was received on, various rarely used header
fields, an extended error description, a set of file descriptors, or UNIX
credentials.
For instance, control messages can be used to send
additional header fields such as IP options.
Ancillary data is sent by calling
<B><A HREF="/cgi-bin/man/man2html?2+sendmsg">sendmsg</A></B>(2)
and received by calling
<B><A HREF="/cgi-bin/man/man2html?2+recvmsg">recvmsg</A></B>(2).
See their manual pages for more information.
<P>
Ancillary data is a sequence of
<I>cmsghdr</I>
structures with appended data.
See the specific protocol man pages for the available control message types.
The maximum ancillary buffer size allowed per socket can be set using
<I>/proc/sys/net/core/optmem_max</I>;
see
<B><A HREF="/cgi-bin/man/man2html?7+socket">socket</A></B>(7).
<P>
The
<I>cmsghdr</I>
structure is defined as follows:
<P>
struct cmsghdr {
<BR>&nbsp;&nbsp;&nbsp;&nbsp;size_t&nbsp;cmsg_len;&nbsp;&nbsp;&nbsp;&nbsp;/*&nbsp;Data&nbsp;byte&nbsp;count,&nbsp;including&nbsp;header
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(type&nbsp;is&nbsp;socklen_t&nbsp;in&nbsp;POSIX)&nbsp;*/
<BR>&nbsp;&nbsp;&nbsp;&nbsp;int&nbsp;&nbsp;&nbsp;&nbsp;cmsg_level;&nbsp;&nbsp;/*&nbsp;Originating&nbsp;protocol&nbsp;*/
<BR>&nbsp;&nbsp;&nbsp;&nbsp;int&nbsp;&nbsp;&nbsp;&nbsp;cmsg_type;&nbsp;&nbsp;&nbsp;/*&nbsp;Protocol-specific&nbsp;type&nbsp;*/
/* followed by
<BR>&nbsp;&nbsp;&nbsp;unsigned&nbsp;char&nbsp;cmsg_data[];&nbsp;*/
};
<P>
The sequence of
<I>cmsghdr</I>
structures should never be accessed directly.
Instead, use only the following macros:
<DL COMPACT>
<DT id="1">*<DD>
<B>CMSG_FIRSTHDR</B>()
returns a pointer to the first
<I>cmsghdr</I>
in the ancillary
data buffer associated with the passed
<I>msghdr</I>.
It returns NULL if there isn't enough space for a
<I>cmsghdr</I>
in the buffer.
<DT id="2">*<DD>
<B>CMSG_NXTHDR</B>()
returns the next valid
<I>cmsghdr</I>
after the passed
<I>cmsghdr</I>.
It returns NULL when there isn't enough space left in the buffer.
<DT id="3"><DD>
When initializing a buffer that will contain a series of
<I>cmsghdr</I>
structures (e.g., to be sent with
<B><A HREF="/cgi-bin/man/man2html?2+sendmsg">sendmsg</A></B>(2)),
that buffer should first be zero-initialized
to ensure the correct operation of
<B>CMSG_NXTHDR</B>().
<DT id="4">*<DD>
<B>CMSG_ALIGN</B>(),
given a length, returns it including the required alignment.
This is a
constant expression.
<DT id="5">*<DD>
<B>CMSG_SPACE</B>()
returns the number of bytes an ancillary element with payload of the
passed data length occupies.
This is a constant expression.
<DT id="6">*<DD>
<B>CMSG_DATA</B>()
returns a pointer to the data portion of a
<I>cmsghdr</I>.
The pointer returned cannot be assumed to be suitably aligned for
accessing arbitrary payload data types.
Applications should not cast it to a pointer type matching the payload,
but should instead use
<B><A HREF="/cgi-bin/man/man2html?3+memcpy">memcpy</A></B>(3)
to copy data to or from a suitably declared object.
<DT id="7">*<DD>
<B>CMSG_LEN</B>()
returns the value to store in the
<I>cmsg_len</I>
member of the
<I>cmsghdr</I>
structure, taking into account any necessary
alignment.
It takes the data length as an argument.
This is a constant
expression.
</DL>
<P>
To create ancillary data, first initialize the
<I>msg_controllen</I>
member of the
<I>msghdr</I>
with the length of the control message buffer.
Use
<B>CMSG_FIRSTHDR</B>()
on the
<I>msghdr</I>
to get the first control message and
<B>CMSG_NXTHDR</B>()
to get all subsequent ones.
In each control message, initialize
<I>cmsg_len</I>
(with
<B>CMSG_LEN</B>()),
the other
<I>cmsghdr</I>
header fields, and the data portion using
<B>CMSG_DATA</B>().
Finally, the
<I>msg_controllen</I>
field of the
<I>msghdr</I>
should be set to the sum of the
<B>CMSG_SPACE</B>()
of the length of
all control messages in the buffer.
For more information on the
<I>msghdr</I>,
see
<B><A HREF="/cgi-bin/man/man2html?2+recvmsg">recvmsg</A></B>(2).
<A NAME="lbAE">&nbsp;</A>
<H2>CONFORMING TO</H2>
This ancillary data model conforms to the POSIX.1g draft, 4.4BSD-Lite,
the IPv6 advanced API described in RFC&nbsp;2292 and SUSv2.
<B>CMSG_ALIGN</B>()
is a Linux extension.
<A NAME="lbAF">&nbsp;</A>
<H2>NOTES</H2>
For portability, ancillary data should be accessed using only the macros
described here.
<B>CMSG_ALIGN</B>()
is a Linux extension and should not be used in portable programs.
<P>
In Linux,
<B>CMSG_LEN</B>(),
<B>CMSG_DATA</B>(),
and
<B>CMSG_ALIGN</B>()
are constant expressions (assuming their argument is constant),
meaning that these values can be used to declare the size of global variables.
This may not be portable, however.
<A NAME="lbAG">&nbsp;</A>
<H2>EXAMPLE</H2>
This code looks for the
<B>IP_TTL</B>
option in a received ancillary buffer:
<P>
struct msghdr msgh;
struct cmsghdr *cmsg;
int received_ttl;
<P>
/* Receive auxiliary data in msgh */
<P>
for (cmsg = CMSG_FIRSTHDR(&amp;msgh); cmsg != NULL;
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;cmsg&nbsp;=&nbsp;CMSG_NXTHDR(&amp;msgh,&nbsp;cmsg))&nbsp;{
<BR>&nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;(cmsg-&gt;cmsg_level&nbsp;==&nbsp;IPPROTO_IP
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&amp;&amp;&nbsp;cmsg-&gt;cmsg_type&nbsp;==&nbsp;IP_TTL)&nbsp;{
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;memcpy(&amp;receive_ttl,&nbsp;CMSG_DATA(cmsg),&nbsp;sizeof(int));
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;break;
<BR>&nbsp;&nbsp;&nbsp;&nbsp;}
}
<P>
if (cmsg == NULL) {
<BR>&nbsp;&nbsp;&nbsp;&nbsp;/*&nbsp;Error:&nbsp;IP_TTL&nbsp;not&nbsp;enabled&nbsp;or&nbsp;small&nbsp;buffer&nbsp;or&nbsp;I/O&nbsp;error&nbsp;*/
}
<P>
The code below passes an array of file descriptors over a
UNIX domain socket using
<B>SCM_RIGHTS</B>:
<P>
struct msghdr msg = { 0 };
struct cmsghdr *cmsg;
int myfds[NUM_FD]; /* Contains the file descriptors to pass */
char iobuf[1];
struct iovec io = {
<BR>&nbsp;&nbsp;&nbsp;&nbsp;.iov_base&nbsp;=&nbsp;iobuf,
<BR>&nbsp;&nbsp;&nbsp;&nbsp;.iov_len&nbsp;=&nbsp;sizeof(iobuf)
};
union { /* Ancillary data buffer, wrapped in a union
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;in&nbsp;order&nbsp;to&nbsp;ensure&nbsp;it&nbsp;is&nbsp;suitably&nbsp;aligned&nbsp;*/
<BR>&nbsp;&nbsp;&nbsp;&nbsp;char&nbsp;buf[CMSG_SPACE(sizeof(myfds))];
<BR>&nbsp;&nbsp;&nbsp;&nbsp;struct&nbsp;cmsghdr&nbsp;align;
} u;
<P>
msg.msg_iov = &amp;io;
msg.msg_iovlen = 1;
msg.msg_control = u.buf;
msg.msg_controllen = sizeof(u.buf);
cmsg = CMSG_FIRSTHDR(&amp;msg);
cmsg-&gt;cmsg_level = SOL_SOCKET;
cmsg-&gt;cmsg_type = SCM_RIGHTS;
cmsg-&gt;cmsg_len = CMSG_LEN(sizeof(int) * NUM_FD);
memcpy(CMSG_DATA(cmsg), myfds, NUM_FD * sizeof(int));
<A NAME="lbAH">&nbsp;</A>
<H2>SEE ALSO</H2>
<B><A HREF="/cgi-bin/man/man2html?2+recvmsg">recvmsg</A></B>(2),
<B><A HREF="/cgi-bin/man/man2html?2+sendmsg">sendmsg</A></B>(2)
<P>
RFC&nbsp;2292
<A NAME="lbAI">&nbsp;</A>
<H2>COLOPHON</H2>
This page is part of release 5.05 of the Linux
<I>man-pages</I>
project.
A description of the project,
information about reporting bugs,
and the latest version of this page,
can be found at
<A HREF="https://www.kernel.org/doc/man-pages/.">https://www.kernel.org/doc/man-pages/.</A>
<P>
<HR>
<A NAME="index">&nbsp;</A><H2>Index</H2>
<DL>
<DT id="8"><A HREF="#lbAB">NAME</A><DD>
<DT id="9"><A HREF="#lbAC">SYNOPSIS</A><DD>
<DT id="10"><A HREF="#lbAD">DESCRIPTION</A><DD>
<DT id="11"><A HREF="#lbAE">CONFORMING TO</A><DD>
<DT id="12"><A HREF="#lbAF">NOTES</A><DD>
<DT id="13"><A HREF="#lbAG">EXAMPLE</A><DD>
<DT id="14"><A HREF="#lbAH">SEE ALSO</A><DD>
<DT id="15"><A HREF="#lbAI">COLOPHON</A><DD>
</DL>
<HR>
This document was created by
<A HREF="/cgi-bin/man/man2html">man2html</A>,
using the manual pages.<BR>
Time: 00:05:38 GMT, March 31, 2021
</BODY>
</HTML>