1CMSG(3) Linux Programmer's Manual CMSG(3)
2
3
4
6 CMSG_ALIGN, CMSG_SPACE, CMSG_NXTHDR, CMSG_FIRSTHDR - access ancillary
7 data
8
10 #include <sys/socket.h>
11 struct cmsghdr *CMSG_FIRSTHDR(struct msghdr *msgh);
12 struct cmsghdr *CMSG_NXTHDR(struct msghdr *msgh,
13 struct cmsghdr *cmsg);
14 size_t CMSG_ALIGN(size_t length);
15 size_t CMSG_SPACE(size_t length);
16 size_t CMSG_LEN(size_t length);
17 unsigned char *CMSG_DATA(struct cmsghdr *cmsg);
18
20 These macros are used to create and access control messages (also
21 called ancillary data) that are not a part of the socket payload. This
22 control information may include the interface the packet was received
23 on, various rarely used header fields, an extended error description, a
24 set of file descriptors, or UNIX credentials. For instance, control
25 messages can be used to send additional header fields such as IP op‐
26 tions. Ancillary data is sent by calling sendmsg(2) and received by
27 calling recvmsg(2). See their manual pages for more information.
28
29 Ancillary data is a sequence of cmsghdr structures with appended data.
30 See the specific protocol man pages for the available control message
31 types. The maximum ancillary buffer size allowed per socket can be set
32 using /proc/sys/net/core/optmem_max; see socket(7).
33
34 The cmsghdr structure is defined as follows:
35
36 struct cmsghdr {
37 size_t cmsg_len; /* Data byte count, including header
38 (type is socklen_t in POSIX) */
39 int cmsg_level; /* Originating protocol */
40 int cmsg_type; /* Protocol-specific type */
41 /* followed by
42 unsigned char cmsg_data[]; */
43 };
44
45 The sequence of cmsghdr structures should never be accessed directly.
46 Instead, use only the following macros:
47
48 * CMSG_FIRSTHDR() returns a pointer to the first cmsghdr in the ancil‐
49 lary data buffer associated with the passed msghdr. It returns NULL
50 if there isn't enough space for a cmsghdr in the buffer.
51
52 * CMSG_NXTHDR() returns the next valid cmsghdr after the passed cms‐
53 ghdr. It returns NULL when there isn't enough space left in the
54 buffer.
55
56 When initializing a buffer that will contain a series of cmsghdr
57 structures (e.g., to be sent with sendmsg(2)), that buffer should
58 first be zero-initialized to ensure the correct operation of
59 CMSG_NXTHDR().
60
61 * CMSG_ALIGN(), given a length, returns it including the required
62 alignment. This is a constant expression.
63
64 * CMSG_SPACE() returns the number of bytes an ancillary element with
65 payload of the passed data length occupies. This is a constant ex‐
66 pression.
67
68 * CMSG_DATA() returns a pointer to the data portion of a cmsghdr. The
69 pointer returned cannot be assumed to be suitably aligned for ac‐
70 cessing arbitrary payload data types. Applications should not cast
71 it to a pointer type matching the payload, but should instead use
72 memcpy(3) to copy data to or from a suitably declared object.
73
74 * CMSG_LEN() returns the value to store in the cmsg_len member of the
75 cmsghdr structure, taking into account any necessary alignment. It
76 takes the data length as an argument. This is a constant expres‐
77 sion.
78
79 To create ancillary data, first initialize the msg_controllen member of
80 the msghdr with the length of the control message buffer. Use
81 CMSG_FIRSTHDR() on the msghdr to get the first control message and
82 CMSG_NXTHDR() to get all subsequent ones. In each control message,
83 initialize cmsg_len (with CMSG_LEN()), the other cmsghdr header fields,
84 and the data portion using CMSG_DATA(). Finally, the msg_controllen
85 field of the msghdr should be set to the sum of the CMSG_SPACE() of the
86 length of all control messages in the buffer. For more information on
87 the msghdr, see recvmsg(2).
88
90 This ancillary data model conforms to the POSIX.1g draft, 4.4BSD-Lite,
91 the IPv6 advanced API described in RFC 2292 and SUSv2.
92 CMSG_FIRSTHDR(), CMSG_NXTHDR(), and CMSG_DATA() are specified in
93 POSIX.1-2008. CMSG_SPACE() and CMSG_LEN() will be included in the next
94 POSIX release (Issue 8).
95
96 CMSG_ALIGN() is a Linux extension.
97
99 For portability, ancillary data should be accessed using only the
100 macros described here. CMSG_ALIGN() is a Linux extension and should
101 not be used in portable programs.
102
103 In Linux, CMSG_LEN(), CMSG_DATA(), and CMSG_ALIGN() are constant ex‐
104 pressions (assuming their argument is constant), meaning that these
105 values can be used to declare the size of global variables. This may
106 not be portable, however.
107
109 This code looks for the IP_TTL option in a received ancillary buffer:
110
111 struct msghdr msgh;
112 struct cmsghdr *cmsg;
113 int received_ttl;
114
115 /* Receive auxiliary data in msgh */
116
117 for (cmsg = CMSG_FIRSTHDR(&msgh); cmsg != NULL;
118 cmsg = CMSG_NXTHDR(&msgh, cmsg)) {
119 if (cmsg->cmsg_level == IPPROTO_IP
120 && cmsg->cmsg_type == IP_TTL) {
121 memcpy(&receive_ttl, CMSG_DATA(cmsg), sizeof(received_ttl));
122 break;
123 }
124 }
125
126 if (cmsg == NULL) {
127 /* Error: IP_TTL not enabled or small buffer or I/O error */
128 }
129
130 The code below passes an array of file descriptors over a UNIX domain
131 socket using SCM_RIGHTS:
132
133 struct msghdr msg = { 0 };
134 struct cmsghdr *cmsg;
135 int myfds[NUM_FD]; /* Contains the file descriptors to pass */
136 char iobuf[1];
137 struct iovec io = {
138 .iov_base = iobuf,
139 .iov_len = sizeof(iobuf)
140 };
141 union { /* Ancillary data buffer, wrapped in a union
142 in order to ensure it is suitably aligned */
143 char buf[CMSG_SPACE(sizeof(myfds))];
144 struct cmsghdr align;
145 } u;
146
147 msg.msg_iov = &io;
148 msg.msg_iovlen = 1;
149 msg.msg_control = u.buf;
150 msg.msg_controllen = sizeof(u.buf);
151 cmsg = CMSG_FIRSTHDR(&msg);
152 cmsg->cmsg_level = SOL_SOCKET;
153 cmsg->cmsg_type = SCM_RIGHTS;
154 cmsg->cmsg_len = CMSG_LEN(sizeof(myfds));
155 memcpy(CMSG_DATA(cmsg), myfds, sizeof(myfds));
156
158 recvmsg(2), sendmsg(2)
159
160 RFC 2292
161
163 This page is part of release 5.10 of the Linux man-pages project. A
164 description of the project, information about reporting bugs, and the
165 latest version of this page, can be found at
166 https://www.kernel.org/doc/man-pages/.
167
168
169
170Linux 2020-11-01 CMSG(3)