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
26 options. 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
66 expression.
67
68 * CMSG_DATA() returns a pointer to the data portion of a cmsghdr.
69
70 * CMSG_LEN() returns the value to store in the cmsg_len member of the
71 cmsghdr structure, taking into account any necessary alignment. It
72 takes the data length as an argument. This is a constant expres‐
73 sion.
74
75 To create ancillary data, first initialize the msg_controllen member of
76 the msghdr with the length of the control message buffer. Use
77 CMSG_FIRSTHDR() on the msghdr to get the first control message and
78 CMSG_NXTHDR() to get all subsequent ones. In each control message,
79 initialize cmsg_len (with CMSG_LEN()), the other cmsghdr header fields,
80 and the data portion using CMSG_DATA(). Finally, the msg_controllen
81 field of the msghdr should be set to the sum of the CMSG_SPACE() of the
82 length of all control messages in the buffer. For more information on
83 the msghdr, see recvmsg(2).
84
86 This ancillary data model conforms to the POSIX.1g draft, 4.4BSD-Lite,
87 the IPv6 advanced API described in RFC 2292 and SUSv2. CMSG_ALIGN() is
88 a Linux extension.
89
91 For portability, ancillary data should be accessed using only the
92 macros described here. CMSG_ALIGN() is a Linux extension and should
93 not be used in portable programs.
94
95 In Linux, CMSG_LEN(), CMSG_DATA(), and CMSG_ALIGN() are constant
96 expressions (assuming their argument is constant), meaning that these
97 values can be used to declare the size of global variables. This may
98 not be portable, however.
99
101 This code looks for the IP_TTL option in a received ancillary buffer:
102
103 struct msghdr msgh;
104 struct cmsghdr *cmsg;
105 int *ttlptr;
106 int received_ttl;
107
108 /* Receive auxiliary data in msgh */
109
110 for (cmsg = CMSG_FIRSTHDR(&msgh); cmsg != NULL;
111 cmsg = CMSG_NXTHDR(&msgh, cmsg)) {
112 if (cmsg->cmsg_level == IPPROTO_IP
113 && cmsg->cmsg_type == IP_TTL) {
114 ttlptr = (int *) CMSG_DATA(cmsg);
115 received_ttl = *ttlptr;
116 break;
117 }
118 }
119
120 if (cmsg == NULL) {
121 /* Error: IP_TTL not enabled or small buffer or I/O error */
122 }
123
124 The code below passes an array of file descriptors over a UNIX domain
125 socket using SCM_RIGHTS:
126
127 struct msghdr msg = { 0 };
128 struct cmsghdr *cmsg;
129 int myfds[NUM_FD]; /* Contains the file descriptors to pass */
130 char iobuf[1];
131 struct iovec io = {
132 .iov_base = iobuf,
133 .iov_len = sizeof(iobuf)
134 };
135 union { /* Ancillary data buffer, wrapped in a union
136 in order to ensure it is suitably aligned */
137 char buf[CMSG_SPACE(sizeof(myfds))];
138 struct cmsghdr align;
139 } u;
140
141 msg.msg_iov = &io;
142 msg.msg_iovlen = 1;
143 msg.msg_control = u.buf;
144 msg.msg_controllen = sizeof(u.buf);
145 cmsg = CMSG_FIRSTHDR(&msg);
146 cmsg->cmsg_level = SOL_SOCKET;
147 cmsg->cmsg_type = SCM_RIGHTS;
148 cmsg->cmsg_len = CMSG_LEN(sizeof(int) * NUM_FD);
149 memcpy(CMSG_DATA(cmsg), myfds, NUM_FD * sizeof(int));
150
152 recvmsg(2), sendmsg(2)
153
154 RFC 2292
155
157 This page is part of release 5.02 of the Linux man-pages project. A
158 description of the project, information about reporting bugs, and the
159 latest version of this page, can be found at
160 https://www.kernel.org/doc/man-pages/.
161
162
163
164Linux 2019-03-06 CMSG(3)