[BACK]Return to CMSG_DATA.3 CVS log [TXT][DIR] Up to [cvs.NetBSD.org] / src / share / man / man3

File: [cvs.NetBSD.org] / src / share / man / man3 / CMSG_DATA.3 (download)

Revision 1.2, Fri Jun 20 14:35:10 2008 UTC (15 years, 10 months ago) by christos
Branch: MAIN
CVS Tags: yamt-pagecache-tag8, yamt-pagecache-base9, yamt-pagecache-base8, yamt-pagecache-base7, yamt-pagecache-base6, yamt-pagecache-base5, yamt-pagecache-base4, yamt-pagecache-base3, yamt-pagecache-base2, yamt-pagecache-base, yamt-pagecache, wrstuden-revivesa-base-3, wrstuden-revivesa-base-2, wrstuden-revivesa-base-1, wrstuden-revivesa-base, tls-maxphys-base, tls-maxphys, tls-earlyentropy-base, tls-earlyentropy, riastradh-xf86-video-intel-2-7-1-pre-2-21-15, riastradh-drm2-base3, riastradh-drm2-base2, riastradh-drm2-base1, riastradh-drm2-base, riastradh-drm2, netbsd-7-nhusb-base-20170116, netbsd-7-nhusb-base, netbsd-7-nhusb, netbsd-7-base, netbsd-7-2-RELEASE, netbsd-7-1-RELEASE, netbsd-7-1-RC2, netbsd-7-1-RC1, netbsd-7-1-2-RELEASE, netbsd-7-1-1-RELEASE, netbsd-7-1, netbsd-7-0-RELEASE, netbsd-7-0-RC3, netbsd-7-0-RC2, netbsd-7-0-RC1, netbsd-7-0-2-RELEASE, netbsd-7-0-1-RELEASE, netbsd-7-0, netbsd-7, netbsd-6-base, netbsd-6-1-RELEASE, netbsd-6-1-RC4, netbsd-6-1-RC3, netbsd-6-1-RC2, netbsd-6-1-RC1, netbsd-6-1-5-RELEASE, netbsd-6-1-4-RELEASE, netbsd-6-1-3-RELEASE, netbsd-6-1-2-RELEASE, netbsd-6-1-1-RELEASE, netbsd-6-1, netbsd-6-0-RELEASE, netbsd-6-0-RC2, netbsd-6-0-RC1, netbsd-6-0-6-RELEASE, netbsd-6-0-5-RELEASE, netbsd-6-0-4-RELEASE, netbsd-6-0-3-RELEASE, netbsd-6-0-2-RELEASE, netbsd-6-0-1-RELEASE, netbsd-6-0, netbsd-6, netbsd-5-base, netbsd-5-2-RELEASE, netbsd-5-2-RC1, netbsd-5-2-3-RELEASE, netbsd-5-2-2-RELEASE, netbsd-5-2-1-RELEASE, netbsd-5-2, netbsd-5-1-RELEASE, netbsd-5-1-RC4, netbsd-5-1-RC3, netbsd-5-1-RC2, netbsd-5-1-RC1, netbsd-5-1-5-RELEASE, netbsd-5-1-4-RELEASE, netbsd-5-1-3-RELEASE, netbsd-5-1-2-RELEASE, netbsd-5-1-1-RELEASE, netbsd-5-1, netbsd-5-0-RELEASE, netbsd-5-0-RC4, netbsd-5-0-RC3, netbsd-5-0-RC2, netbsd-5-0-RC1, netbsd-5-0-2-RELEASE, netbsd-5-0-1-RELEASE, netbsd-5-0, netbsd-5, mjf-devfs2-base, matt-premerge-20091211, matt-nb6-plus-nbase, matt-nb6-plus-base, matt-nb6-plus, matt-nb5-pq3-base, matt-nb5-pq3, matt-nb5-mips64-u2-k2-k4-k7-k8-k9, matt-nb5-mips64-u1-k1-k5, matt-nb5-mips64-premerge-20101231, matt-nb5-mips64-premerge-20091211, matt-nb5-mips64-k15, matt-nb5-mips64, matt-nb4-mips64-k7-u2a-k9b, matt-mips64-premerge-20101231, matt-mips64-base2, jym-xensuspend-nbase, jym-xensuspend-base, jym-xensuspend, cherry-xenmp-base, cherry-xenmp, bouyer-quota2-nbase, bouyer-quota2-base, bouyer-quota2, agc-symver-base, agc-symver
Branch point for: wrstuden-revivesa
Changes since 1.1: +24 -11 lines

and adjust it to our liking.

.\"	$NetBSD: CMSG_DATA.3,v 1.2 2008/06/20 14:35:10 christos Exp $
.\"	$OpenBSD: CMSG_DATA.3,v 1.5 2008/03/24 16:11:07 deraadt Exp $
.\" Written by Jared Yanovich <jaredy@openbsd.org>
.\" Public domain, July 3, 2005
.Dd June 20, 2008
.Dt CMSG_DATA 3
.Os
.Sh NAME
.Nm CMSG_DATA ,
.Nm CMSG_FIRSTHDR ,
.Nm CMSG_LEN ,
.Nm CMSG_NXTHDR ,
.Nm CMSG_SPACE
.Nd socket control message routines
.Sh SYNOPSIS
.In sys/socket.h
.Ft void *
.Fn CMSG_DATA "struct cmsghdr *"
.Ft struct cmsghdr *
.Fn CMSG_FIRSTHDR "struct msghdr *"
.Ft size_t
.Fn CMSG_LEN "size_t"
.Ft struct cmsghdr *
.Fn CMSG_NXTHDR "struct msghdr *" "struct cmsghdr *"
.Ft size_t
.Fn CMSG_SPACE "size_t"
.Sh DESCRIPTION
The control message API is used to construct ancillary data objects for
use in control messages sent and received across sockets.
.Pp
Control messages are passed around by the
.Xr recvmsg 2
and
.Xr sendmsg 2
system calls.
The
.Vt cmsghdr
structure, described in
.Xr recvmsg 2 ,
is used to specify a chain of control messages.
.Pp
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.
.Pp
The following routines are provided:
.Bl -tag -width Ds
.It Fn CMSG_DATA cmsg
This routine accesses the data portion of the control message header
.Fa cmsg .
It ensures proper alignment constraints on the beginning of ancillary
data are met.
.It Fn CMSG_FIRSTHDR mhdr
This routine accesses the first control message attached to the
message
.Fa msg .
If no control messages are attached to the message, this routine
returns
.Dv NULL .
.It Fn CMSG_LEN len
This routine determines the size in bytes of a control message,
which includes the control message header.
.Fa len
specifies the length of the data held by the control message.
This value is what is normally stored in the
.Fa cmsg_len
of each control message.
This routine accounts for any alignment constraints on the beginning of
ancillary data.
This macro might not evaluate to a compile-time constant.
.It Fn CMSG_NXTHDR mhdr cmsg
This routine returns the location of the control message following
.Fa cmsg
in the message
.Fa mhdr .
If
.Fa cmsg
is the last control message in the chain, this routine returns
.Dv NULL .
.It Fn CMSG_SPACE len
This routine determines the size in bytes needed to hold a control
message and its contents of length
.Fa len ,
which includes the control message header.
This value is what is normally stored in
.Fa 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.
This macro might not evaluate to a compile-time constant.
.El
.Sh EXAMPLES
The following example constructs a control message containing a file
descriptor and passes it over a socket:
.Bd -literal -offset indent
struct msghdr	 msg;
struct cmsghdr	*cmsg;
/* We use a union to make sure hdr is aligned */
union {
	struct cmsghdr hdr;
	unsigned char	 buf[CMSG_SPACE(sizeof(int))];
} *cmsgbuf;

/*
 * We allocate in the heap instead of the stack to avoid C99
 * variable stack allocation, which breaks gcc -fstack-protector.
 */
if ((cmsgbuf = malloc(sizeof(*cmsgbuf))) == NULL)
	err(1, "malloc");
(void)memset(&msg, 0, sizeof(msg));
msg.msg_control = cmsgbuf->buf;
msg.msg_controllen = sizeof(cmsgbuf->buf);

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) = fd;

if (sendmsg(s, &msg, 0) == -1)
	err(1, "sendmsg");
free(cmsgbuf);
.Ed
.Pp
And an example that receives and decomposes the control message:
.Bd -literal -offset indent
struct msghdr	 msg;
struct cmsghdr	*cmsg;
union {
	struct cmsghdr hdr;
	unsigned char	 buf[CMSG_SPACE(sizeof(int))];
} *cmsgbuf;

if ((cmsgbuf = malloc(sizeof(*cmsgbuf))) == NULL)
	err(1, "malloc");
(void)memset(&msg, 0, sizeof(msg));
msg.msg_control = cmsgbuf->buf;
msg.msg_controllen = sizeof(cmsgbuf->buf);

if (recvmsg(s, &msg, 0) == -1)
	err(1, "recvmsg");
if ((msg.msg_flags & (MSG_TRUNC|MSG_CTRUNC))
	errx(1, "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) {
		fd = *(int *)CMSG_DATA(cmsg);
		/* Do something with the descriptor. */
	}
}
free(cmsgbuf);
.Ed
.Sh SEE ALSO
.Xr recvmsg 2 ,
.Xr sendmsg 2 ,
.Xr socket 2
.Sh HISTORY
The control message API first appeared in
.Bx 4.2 .