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
struct cmsghdr
structures with appended data.
This sequence should be accessed
using only the macros described in this manual page and never directly.
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).

CMSG_FIRSTHDR()
returns a pointer to the first
cmsghdr
in the ancillary
data buffer associated with the passed
msghdr.

CMSG_NXTHDR()
returns the next valid
cmsghdr
after the passed
cmsghdr.
It returns NULL when there isn't enough space left in the buffer.

CMSG_ALIGN(),
given a length, returns it including the required alignment.
This is a
constant expression.

CMSG_SPACE()
returns the number of bytes an ancillary element with payload of the
passed data length occupies.
This is a constant expression.

CMSG_DATA()
returns a pointer to the data portion of a
cmsghdr.

CMSG_LEN()
returns the value to store in the
cmsg_len
member of the
cmsghdr
structure, taking into account any necessary
alignment.
It takes the data length as an argument.
This is a constant
expression.

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).

When the control message buffer is too short to store all messages, the
MSG_CTRUNC
flag is set in the
msg_flags
member of the
msghdr.

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_ALIGN()
is a Linux extension.

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.

In Linux,
CMSG_LEN(),
CMSG_DATA(),
and
CMSG_ALIGN()
are constant expressions (assuming their argument is constant);
this could be used to declare the size of global
variables.
This may not be portable, however.

See Also

Colophon

This page is part of release 3.80 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
http://www.kernel.org/doc/man-pages/.

License & Copyright

This man page is Copyright (C) 1999 Andi Kleen .
%%%LICENSE_START(VERBATIM_ONE_PARA)
Permission is granted to distribute possibly modified copies
of this page provided the header is included verbatim,
and in case of nontrivial modification author and date
of the modification is added to the header.
%%%LICENSE_END
$Id: cmsg.3,v 1.8 2000/12/20 18:10:31 ak Exp $