cmsg (3) - Linux Manuals
cmsg: access ancillary data
NAME
CMSG_ALIGN, CMSG_SPACE, CMSG_NXTHDR, CMSG_FIRSTHDR - access ancillary data
SYNOPSIS
#include <sys/socket.h> struct cmsghdr *CMSG_FIRSTHDR(struct msghdr *msgh); struct cmsghdr *CMSG_NXTHDR(struct msghdr *msgh, struct cmsghdr *cmsg); size_t CMSG_ALIGN(size_t length); size_t CMSG_SPACE(size_t length); size_t CMSG_LEN(size_t length); unsigned char *CMSG_DATA(struct cmsghdr *cmsg);
DESCRIPTION
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 sendmsg(2) and received by calling recvmsg(2). See their manual pages for more information.Ancillary data is a sequence of cmsghdr 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 /proc/sys/net/core/optmem_max; see socket(7).
The cmsghdr structure is defined as follows:
struct cmsghdr {
The sequence of
cmsghdr
structures should never be accessed directly.
Instead, use only the following macros:
To create ancillary data, first initialize the
msg_controllen
member of the
msghdr
with the length of the control message buffer.
Use
CMSG_FIRSTHDR()
on the
msghdr
to get the first control message and
CMSG_NXTHDR()
to get all subsequent ones.
In each control message, initialize
cmsg_len
(with
CMSG_LEN()),
the other
cmsghdr
header fields, and the data portion using
CMSG_DATA().
Finally, the
msg_controllen
field of the
msghdr
should be set to the sum of the
CMSG_SPACE()
of the length of
all control messages in the buffer.
For more information on the
msghdr,
see
recvmsg(2).
CMSG_ALIGN()
is a Linux extension.
In Linux,
CMSG_LEN(),
CMSG_DATA(),
and
CMSG_ALIGN()
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.
struct msghdr msgh;
struct cmsghdr *cmsg;
int received_ttl;
/* Receive auxiliary data in msgh */
for (cmsg = CMSG_FIRSTHDR(&msgh); cmsg != NULL;
if (cmsg == NULL) {
The code below passes an array of file descriptors over a
UNIX domain socket using
SCM_RIGHTS:
struct msghdr msg = { 0 };
struct cmsghdr *cmsg;
int myfds[NUM_FD]; /* Contains the file descriptors to pass */
char iobuf[1];
struct iovec io = {
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(myfds));
memcpy(CMSG_DATA(cmsg), myfds, sizeof(myfds));
CONFORMING TO
This ancillary data model conforms to the POSIX.1g draft, 4.4BSD-Lite,
the IPv6 advanced API described in RFC 2292 and SUSv2.
CMSG_FIRSTHDR(),
CMSG_NXTHDR(),
and
CMSG_DATA()
are specified in POSIX.1-2008.
CMSG_SPACE()
and
CMSG_LEN()
will be included in the next POSIX release (Issue 8).
NOTES
For portability, ancillary data should be accessed using only the macros
described here.
CMSG_ALIGN()
is a Linux extension and should not be used in portable programs.
EXAMPLES
This code looks for the
IP_TTL
option in a received ancillary buffer:
COLOPHON
This page is part of release 5.10 of the Linux
man-pages
project.
A description of the project,
information about reporting bugs,
and the latest version of this page,
can be found at
https://www.kernel.org/doc/man-pages/.