carta
CMSG_LEN(3)

CMSG_LEN3

Socket control message routines for ancillary data access

Part of CMSG_DATA(3).

Usage

unsigned char *

struct cmsghdr *

size_t

struct cmsghdr *

size_t

Overview

The control message API is used to construct ancillary data objects for use in control messages sent and received across sockets.

Control messages are passed around by the recvmsg(2) and sendmsg(2) system calls. The cmsghdr structure, described in recvmsg(2), is used to specify a chain of control messages.

These routines should be used instead of directly accessing the control message header members and data buffers as they ensure that necessary alignment constraints are met.

The following routines are provided:

CMSG_DATA(cmsg)

This routine accesses the data portion of the control message header cmsg. It ensures proper alignment constraints on the beginning of ancillary data are met.

CMSG_FIRSTHDR(msghdr)

This routine accesses the first control message attached to the message msghdr. If no control messages are attached to the message, this routine returns NULL.

CMSG_LEN(len)

This routine determines the size in bytes of a control message, which includes the control message header. len specifies the length of the data held by the control message. This value is what is normally stored in the cmsg_len of each control message. This routine accounts for any alignment constraints on the beginning of ancillary data.

CMSG_NXTHDR(msghdr, cmsg)

This routine returns the location of the control message following cmsg in the message msghdr. If cmsg is the last control message in the chain, this routine returns NULL.

CMSG_SPACE(len)

This routine determines the size in bytes needed to hold a control message and its contents of length len, which includes the control message header. This value is what is normally stored in msg_msgcontrollen. This routine accounts for any alignment constraints on the beginning of ancillary data as well as any needed to pad the next control message.

Examples

The following example constructs a control message containing a file descriptor in the parent process and passes it over a pre-shared socket over the child process. Then the child process sends a "hello" string to the parent process using the received file descriptor.

#include <sys/socket.h>
#include <err.h>
#include <stdio.h>
#include <string.h>
#include <sysexits.h>
#include <unistd.h>
#define	HELLOLEN    sizeof("hello")
int
main()
{
	struct msghdr msg;
	union {
		struct cmsghdr hdr;
		unsigned char	 buf[CMSG_SPACE(sizeof(int))];
	} cmsgbuf;
	char buf[HELLOLEN];
	int hellofd[2];
	int presharedfd[2];
	struct cmsghdr *cmsg;
	if (socketpair(PF_LOCAL, SOCK_STREAM, 0, presharedfd) == -1)
		err(EX_OSERR, "failed to create a pre-shared socket pair");
	memset(&msg, 0, sizeof(msg));
	msg.msg_control = &cmsgbuf.buf;
	msg.msg_controllen = sizeof(cmsgbuf.buf);
	msg.msg_iov = NULL;
	msg.msg_iovlen = 0;
	switch (fork()) {
	case -1:
		err(EX_OSERR, "fork");
	case 0:
		close(presharedfd[0]);
		strlcpy(buf, "hello", HELLOLEN);
		if (recvmsg(presharedfd[1], &msg, 0) == -1)
			err(EX_IOERR, "failed to receive a message");
		if (msg.msg_flags & (MSG_CTRUNC | MSG_TRUNC))
			errx(EX_IOERR, "control message truncated");
		for (cmsg = CMSG_FIRSTHDR(&msg); cmsg != NULL;
		    cmsg = CMSG_NXTHDR(&msg, cmsg)) {
			if (cmsg->cmsg_len == CMSG_LEN(sizeof(int)) &&
			    cmsg->cmsg_level == SOL_SOCKET &&
			    cmsg->cmsg_type == SCM_RIGHTS) {
				hellofd[1] = *(int *)CMSG_DATA(cmsg);
				printf("child: sending '%s'\n", buf);
				if (write(hellofd[1], buf, HELLOLEN) == -1)
				    err(EX_IOERR, "failed to send 'hello'");
			}
		}
		break;
	default:
		close(presharedfd[1]);
		if (socketpair(PF_LOCAL, SOCK_STREAM, 0, hellofd) == -1)
			err(EX_OSERR, "failed to create a 'hello' socket pair");
		cmsg = CMSG_FIRSTHDR(&msg);
		cmsg->cmsg_len = CMSG_LEN(sizeof(int));
		cmsg->cmsg_level = SOL_SOCKET;
		cmsg->cmsg_type = SCM_RIGHTS;
		*(int *)CMSG_DATA(cmsg) = hellofd[1];
		if (sendmsg(presharedfd[0], &msg, 0) == -1)
			err(EX_IOERR, "sendmsg");
		close(hellofd[1]);
		if (read(hellofd[0], buf, HELLOLEN) == -1)
			err(EX_IOERR, "failed to receive 'hello'");
		printf("parent: received '%s'\n", buf);
		break;
	}
	return (0);
}

History

  • FreeBSD 16.0-CURRENT (2026-10-27) to FreeBSD 13.3-RELEASE: the text shown here
  • FreeBSD 13.2-RELEASE
  • FreeBSD 13.1-RELEASE to FreeBSD 13.0-RELEASE
  • FreeBSD 12.4-RELEASE
  • FreeBSD 12.3-RELEASE
  • FreeBSD 12.2-RELEASE
  • FreeBSD 12.1-RELEASE
  • FreeBSD 12.0-RELEASE

Standards

W. Stevens and M. Thomas, Advanced Sockets API for IPv6, RFC 2292, February 1998.

W. Stevens, M. Thomas, E. Nordmark, and T. Jinmei, Advanced Sockets Application Program Interface (API) for IPv6, RFC 3542, May 2003.

History

The control message API first appeared in 4.2BSD. This manual page was originally written by Jared Yanovich ⟨[email protected]⟩ for OpenBSD 3.8 and eventually brought to FreeBSD 12.0 by Mateusz Piotrowski ⟨[email protected]⟩.

From base 16.0-CURRENT-20261027-61c70dc16a79 in FreeBSD 16.0-CURRENT (2026-10-27). Parsed by carta from the page's mdoc source.