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