2 .\" $OpenBSD: CMSG_DATA.3,v 1.5 2008/03/24 16:11:07 deraadt Exp $
3 .\" Written by Jared Yanovich <jaredy@openbsd.org>
4 .\" Public domain, July 3, 2005
14 .Nd socket control message routines
18 .Fn CMSG_DATA "struct cmsghdr *"
20 .Fn CMSG_FIRSTHDR "struct msghdr *"
24 .Fn CMSG_NXTHDR "struct msghdr *" "struct cmsghdr *"
26 .Fn CMSG_SPACE "size_t"
28 The control message API is used to construct ancillary data objects for
29 use in control messages sent and received across sockets.
31 Control messages are passed around by the
38 structure, described in
40 is used to specify a chain of control messages.
42 These routines should be used instead of directly accessing the control
43 message header members and data buffers as they ensure that necessary
44 alignment constraints are met.
46 The following routines are provided:
49 This routine accesses the data portion of the control message header
51 It ensures proper alignment constraints on the beginning of ancillary
53 .It Fn CMSG_FIRSTHDR mhdr
54 This routine accesses the first control message attached to the
57 If no control messages are attached to the message, this routine
61 This routine determines the size in bytes of a control message,
62 which includes the control message header.
64 specifies the length of the data held by the control message.
65 This value is what is normally stored in the
67 of each control message.
68 This routine accounts for any alignment constraints on the beginning of
70 This macro might not evaluate to a compile-time constant.
71 .It Fn CMSG_NXTHDR mhdr cmsg
72 This routine returns the location of the control message following
78 is the last control message in the chain, this routine returns
81 This routine determines the size in bytes needed to hold a control
82 message and its contents of length
84 which includes the control message header.
85 This value is what is normally stored in
86 .Fa msg_msgcontrollen .
87 This routine accounts for any alignment constraints on the beginning of
88 ancillary data as well as any needed to pad the next control message.
89 This macro might not evaluate to a compile-time constant.
92 The following example constructs a control message containing a file
93 descriptor and passes it over a socket:
94 .Bd -literal -offset indent
97 /* We use a union to make sure hdr is aligned */
100 unsigned char buf[CMSG_SPACE(sizeof(int))];
104 * We allocate in the heap instead of the stack to avoid C99
105 * variable stack allocation, which breaks gcc -fstack-protector.
107 if ((cmsgbuf = malloc(sizeof(*cmsgbuf))) == NULL)
109 (void)memset(&msg, 0, sizeof(msg));
110 msg.msg_control = cmsgbuf->buf;
111 msg.msg_controllen = sizeof(cmsgbuf->buf);
113 cmsg = CMSG_FIRSTHDR(&msg);
114 cmsg->cmsg_len = CMSG_LEN(sizeof(int));
115 cmsg->cmsg_level = SOL_SOCKET;
116 cmsg->cmsg_type = SCM_RIGHTS;
117 *(int *)CMSG_DATA(cmsg) = fd;
119 if (sendmsg(s, &msg, 0) == -1)
124 And an example that receives and decomposes the control message:
125 .Bd -literal -offset indent
127 struct cmsghdr *cmsg;
130 unsigned char buf[CMSG_SPACE(sizeof(int))];
133 if ((cmsgbuf = malloc(sizeof(*cmsgbuf))) == NULL)
135 (void)memset(&msg, 0, sizeof(msg));
136 msg.msg_control = cmsgbuf->buf;
137 msg.msg_controllen = sizeof(cmsgbuf->buf);
139 if (recvmsg(s, &msg, 0) == -1)
141 if ((msg.msg_flags & (MSG_TRUNC|MSG_CTRUNC))
142 errx(1, "control message truncated");
143 for (cmsg = CMSG_FIRSTHDR(&msg); cmsg != NULL;
144 cmsg = CMSG_NXTHDR(&msg, cmsg)) {
145 if (cmsg->cmsg_len == CMSG_LEN(sizeof(int)) &&
146 cmsg->cmsg_level == SOL_SOCKET &&
147 cmsg->cmsg_type == SCM_RIGHTS) {
148 fd = *(int *)CMSG_DATA(cmsg);
149 /* Do something with the descriptor. */
159 The control message API first appeared in