1 .\" $OpenBSD: CMSG_DATA.3,v 1.5 2008/03/24 16:11:07 deraadt Exp $
2 .\" Written by Jared Yanovich <jaredy@openbsd.org>
3 .\" Public domain, July 3, 2005
13 .Nd socket control message routines
17 .Fn CMSG_DATA "struct cmsghdr *"
19 .Fn CMSG_FIRSTHDR "struct msghdr *"
23 .Fn CMSG_NXTHDR "struct msghdr *" "struct cmsghdr *"
25 .Fn CMSG_SPACE "size_t"
27 The control message API is used to construct ancillary data objects for
28 use in control messages sent and received across sockets.
30 Control messages are passed around by the
37 structure, described in
39 is used to specify a chain of control messages.
41 These routines should be used instead of directly accessing the control
42 message header members and data buffers as they ensure that necessary
43 alignment constraints are met.
45 The following routines are provided:
48 This routine accesses the data portion of the control message header
50 It ensures proper alignment constraints on the beginning of ancillary
52 .It Fn CMSG_FIRSTHDR mhdr
53 This routine accesses the first control message attached to the
56 If no control messages are attached to the message, this routine
60 This routine determines the size in bytes of a control message,
61 which includes the control message header.
63 specifies the length of the data held by the control message.
64 This value is what is normally stored in the
66 of each control message.
67 This routine accounts for any alignment constraints on the beginning of
69 .It Fn CMSG_NXTHDR mhdr cmsg
70 This routine returns the location of the control message following
76 is the last control message in the chain, this routine returns
79 This routine determines the size in bytes needed to hold a control
80 message and its contents of length
82 which includes the control message header.
83 This value is what is normally stored in
84 .Fa msg_msgcontrollen .
85 This routine accounts for any alignment constraints on the beginning of
86 ancillary data as well as any needed to pad the next control message.
89 The following example constructs a control message containing a file
90 descriptor and passes it over a socket:
91 .Bd -literal -offset indent
96 unsigned char buf[CMSG_SPACE(sizeof(int))];
99 memset(&msg, 0, sizeof(msg));
100 msg.msg_control = &cmsgbuf.buf;
101 msg.msg_controllen = sizeof(cmsgbuf.buf);
103 cmsg = CMSG_FIRSTHDR(&msg);
104 cmsg->cmsg_len = CMSG_LEN(sizeof(int));
105 cmsg->cmsg_level = SOL_SOCKET;
106 cmsg->cmsg_type = SCM_RIGHTS;
107 *(int *)CMSG_DATA(cmsg) = fd;
109 if (sendmsg(s, &msg, 0) == -1)
113 And an example that receives and decomposes the control message:
114 .Bd -literal -offset indent
116 struct cmsghdr *cmsg;
119 unsigned char buf[CMSG_SPACE(sizeof(int))];
122 memset(&msg, 0, sizeof(msg));
123 msg.msg_control = &cmsgbuf.buf;
124 msg.msg_controllen = sizeof(cmsgbuf.buf);
126 if (recvmsg(s, &msg, 0) == -1)
128 if ((msg.msg_flags & MSG_TRUNC) || (msg.msg_flags & MSG_CTRUNC))
129 errx(1, "control message truncated");
130 for (cmsg = CMSG_FIRSTHDR(&msg); cmsg != NULL;
131 cmsg = CMSG_NXTHDR(&msg, cmsg)) {
132 if (cmsg->cmsg_len == CMSG_LEN(sizeof(int)) &&
133 cmsg->cmsg_level == SOL_SOCKET &&
134 cmsg->cmsg_type == SCM_RIGHTS) {
135 fd = *(int *)CMSG_DATA(cmsg);
136 /* Do something with the descriptor. */
145 The control message API first appeared in