#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
size_t CMSG_SPACE(size_t
size_t CMSG_LEN(size_t
unsigned char *CMSG_DATA(struct cmsghdr *

struct cmsghdr {
socklen_t cmsg_len; /* data byte count, including header */
int cmsg_level; /* originating protocol */
int cmsg_type; /* protocol-specific type */
/* followed by unsigned char cmsg_data[]; */


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.


This ancillary data model conforms to the POSIX.1g draft, 4.4BSD-Lite, the IPv6 advanced API described in RFC 2292 and the SUSv2. CMSG_ALIGN() is a Linux extension.


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.


This code looks for the IP_TTL option in a received ancillary buffer:

struct msghdr msgh;
struct cmsghdr *cmsg;
int *ttlptr;
int received_ttl;

/* Receive auxiliary data in msgh */
for (cmsg = CMSG_FIRSTHDR(&msgh); cmsg != NULL;
cmsg = CMSG_NXTHDR(&msgh,cmsg)) {
if (cmsg−>cmsg_level == IPPROTO_IP
&& cmsg−>cmsg_type == IP_TTL) {
ttlptr = (int *) CMSG_DATA(cmsg);
received_ttl = *ttlptr;
if (cmsg == NULL) {
* Error: IP_TTL not enabled or small buffer
* or I/O error.

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 buf[CMSG_SPACE(sizeof myfds)]; /* ancillary data buffer */
int *fdptr;

msg.msg_control = buf;
msg.msg_controllen = sizeof buf;
cmsg = CMSG_FIRSTHDR(&msg);
cmsg−>cmsg_level = SOL_SOCKET;
cmsg−>cmsg_type = SCM_RIGHTS;
cmsg−>cmsg_len = CMSG_LEN(sizeof(int) * NUM_FD);
/* Initialize the payload: */
fdptr = (int *) CMSG_DATA(cmsg);
memcpy(fdptr, myfds, NUM_FD * sizeof(int));
/* Sum of the length of all control messages in the buffer: */
msg.msg_controllen = cmsg−>cmsg_len;


recvmsg(2), sendmsg(2)

RFC 2292


This page is part of release 3.69 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−pages/.

More Linux Commands

epoll(7) - I/O event notification facility - Linux man page
The epoll API performs a similar task to poll(2): monitoring multiple file descriptors to see if I/O is possible on any of them. The epoll API can be used eithe

XkbAddGeomDoodad(3) - Add one doodad to a section of a keybo
XkbAddGeomDoodad.3 - Xkb provides functions to add a single new element to the top-level keyboard geometry. In each case the num_ * fields of the corresponding

matherr(3) - SVID math library exception handling (ManPage)
The System V Interface Definition (SVID) specifies that various math functions should invoke a function called matherr() if a math exception is detected. This f

SDL_JoystickNumHats(3) - Get the number of joystick hats....
Return the number of hats available from a previously opened SDL_Joystick. RETURN VALUE Number of hats. SEE ALSO SDL_JoystickGetHat, SDL_JoystickOpen SDL_Joysti

XGetXCBConnection(3) - get the XCB connection for an Xlib Di
The XGetXCBConnection function returns the XCB connection associated with an Xlib Display. Clients can use this XCB connection with functions from the XCB libra

ldap_rename_s(3) - Renames the specified entry. (Man Page)
These routines are used to perform a LDAP rename operation. The function changes the leaf component of an entrys distinguished name and optionally moves the ent

mvwadd_wch(3ncurses) - add a complex character and rendition
The add_wch, wadd_wch, mvadd_wch, and mvwadd_wch functions put the complex character wch into the given window at its current position, which is then advanced.

Tk_CanvasTextInfo(3) - additional information for managing t
Textual canvas items are somewhat more complicated to manage than other items, due to things like the selection and the input focus. Tk_CanvasGetTextInfo may be

glLightModeliv(3gl) - set the lighting model parameters.....
glLightModel sets the lighting model parameter. pname names a parameter and params gives the new value. There are three lighting model parameters: GL_LIGHT_MODE

optionSaveState(3) saves the option state to memory.........
This routine will allocate enough memory to save the current option processing state. If this routine has been called before, that memory will be reused. You ma

nextto(n) invoke superclass method implementations..........
The next command is used to call implementations of a method by a class, superclass or mixin that are overridden by the current method. It can only be used from

is_linetouched(3ncurses) - curses refresh control routines
The touchwin and touchline routines throw away all optimization information about which parts of the window have been touched, by pretending that the entire win

We can't live, work or learn in freedom unless the software we use is free.