347 lines
9.0 KiB
HTML
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"> </A>
|
|
<H2>NAME</H2>
|
|
|
|
CMSG_ALIGN, CMSG_SPACE, CMSG_NXTHDR, CMSG_FIRSTHDR - access ancillary data
|
|
<A NAME="lbAC"> </A>
|
|
<H2>SYNOPSIS</H2>
|
|
|
|
<PRE>
|
|
<B>#include <<A HREF="file:///usr/include/sys/socket.h">sys/socket.h</A>></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"> </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> size_t cmsg_len; /* Data byte count, including header
|
|
<BR> (type is socklen_t in POSIX) */
|
|
<BR> int cmsg_level; /* Originating protocol */
|
|
<BR> int cmsg_type; /* Protocol-specific type */
|
|
/* followed by
|
|
<BR> unsigned char cmsg_data[]; */
|
|
};
|
|
|
|
|
|
<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"> </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 2292 and SUSv2.
|
|
<B>CMSG_ALIGN</B>()
|
|
|
|
is a Linux extension.
|
|
<A NAME="lbAF"> </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"> </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(&msgh); cmsg != NULL;
|
|
<BR> cmsg = CMSG_NXTHDR(&msgh, cmsg)) {
|
|
<BR> if (cmsg->cmsg_level == IPPROTO_IP
|
|
<BR> && cmsg->cmsg_type == IP_TTL) {
|
|
<BR> memcpy(&receive_ttl, CMSG_DATA(cmsg), sizeof(int));
|
|
<BR> break;
|
|
<BR> }
|
|
}
|
|
<P>
|
|
if (cmsg == NULL) {
|
|
<BR> /* Error: IP_TTL not enabled or small buffer or I/O error */
|
|
}
|
|
|
|
|
|
<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> .iov_base = iobuf,
|
|
<BR> .iov_len = sizeof(iobuf)
|
|
};
|
|
union { /* Ancillary data buffer, wrapped in a union
|
|
<BR> in order to ensure it is suitably aligned */
|
|
<BR> char buf[CMSG_SPACE(sizeof(myfds))];
|
|
<BR> struct cmsghdr align;
|
|
} u;
|
|
<P>
|
|
msg.msg_iov = &io;
|
|
msg.msg_iovlen = 1;
|
|
msg.msg_control = u.buf;
|
|
msg.msg_controllen = sizeof(u.buf);
|
|
cmsg = CMSG_FIRSTHDR(&msg);
|
|
cmsg->cmsg_level = SOL_SOCKET;
|
|
cmsg->cmsg_type = SCM_RIGHTS;
|
|
cmsg->cmsg_len = CMSG_LEN(sizeof(int) * NUM_FD);
|
|
memcpy(CMSG_DATA(cmsg), myfds, NUM_FD * sizeof(int));
|
|
|
|
|
|
<A NAME="lbAH"> </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 2292
|
|
<A NAME="lbAI"> </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"> </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>
|