1 .\" This man page is Copyright (C) 1999 Andi Kleen <ak@muc.de>.
2 .\" Permission is granted to distribute possibly modified copies
3 .\" of this page provided the header is included verbatim,
4 .\" and in case of nontrivial modification author and date
5 .\" of the modification is added to the header.
6 .\" $Id: cmsg.3,v 1.8 2000/12/20 18:10:31 ak Exp $
7 .TH CMSG 3 2008-11-20 "Linux" "Linux Programmer's Manual"
9 CMSG_ALIGN, CMSG_SPACE, CMSG_NXTHDR, CMSG_FIRSTHDR \- Access ancillary data
11 .B #include <sys/socket.h>
13 .BI "struct cmsghdr *CMSG_FIRSTHDR(struct msghdr *" msgh );
15 .BI "struct cmsghdr *CMSG_NXTHDR(struct msghdr *" msgh ", struct cmsghdr *" cmsg );
17 .BI "size_t CMSG_ALIGN(size_t " length );
19 .BI "size_t CMSG_SPACE(size_t " length );
21 .BI "size_t CMSG_LEN(size_t " length );
23 .BI "unsigned char *CMSG_DATA(struct cmsghdr *" cmsg );
27 socklen_t cmsg_len; /* data byte count, including header */
28 int cmsg_level; /* originating protocol */
29 int cmsg_type; /* protocol-specific type */
30 /* followed by unsigned char cmsg_data[]; */
34 These macros are used to create and access control messages (also called
35 ancillary data) that are not a part of the socket payload.
36 This control information may
37 include the interface the packet was received on, various rarely used header
38 fields, an extended error description, a set of file descriptors or UNIX
40 For instance, control messages can be used to send
41 additional header fields such as IP options.
42 Ancillary data is sent by calling
44 and received by calling
46 See their manual pages for more information.
48 Ancillary data is a sequence of
50 structures with appended data.
51 This sequence should only be accessed
52 using the macros described in this manual page and never directly.
53 See the specific protocol man pages for the available control message types.
54 The maximum ancillary buffer size allowed per socket can be set using
55 .IR /proc/sys/net/core/optmem_max ;
60 returns a pointer to the first
63 data buffer associated with the passed
67 returns the next valid
71 It returns NULL when there isn't enough space left in the buffer.
74 given a length, returns it including the required alignment.
79 returns the number of bytes an ancillary element with payload of the
80 passed data length occupies.
81 This is a constant expression.
84 returns a pointer to the data portion of a
88 returns the value to store in the
92 structure, taking into account any necessary
94 It takes the data length as an argument.
98 To create ancillary data, first initialize the
102 with the length of the control message buffer.
107 to get the first control message and
109 to get all subsequent ones.
110 In each control message, initialize
116 header fields, and the data portion using
122 should be set to the sum of the
125 all control messages in the buffer.
126 For more information on the
131 When the control message buffer is too short to store all messages, the
138 This ancillary data model conforms to the POSIX.1g draft, 4.4BSD-Lite,
139 the IPv6 advanced API described in RFC\ 2292 and the SUSv2.
141 is a Linux extension.
143 For portability, ancillary data should be accessed only using the macros
146 is a Linux extension and should be not used in portable programs.
153 are constant expressions (assuming their argument is constant);
154 this could be used to declare the size of global
156 This may be not portable, however.
158 This code looks for the
160 option in a received ancillary buffer:
165 struct cmsghdr *cmsg;
169 /* Receive auxiliary data in msgh */
170 for (cmsg = CMSG_FIRSTHDR(&msgh); cmsg != NULL;
171 cmsg = CMSG_NXTHDR(&msgh,cmsg)) {
172 if (cmsg\->cmsg_level == IPPROTO_IP
173 && cmsg\->cmsg_type == IP_TTL) {
174 ttlptr = (int *) CMSG_DATA(cmsg);
175 received_ttl = *ttlptr;
181 * Error: IP_TTL not enabled or small buffer
188 The code below passes an array of file descriptors over a
189 UNIX domain socket using
194 struct msghdr msg = {0};
195 struct cmsghdr *cmsg;
196 int myfds[NUM_FD]; /* Contains the file descriptors to pass. */
197 char buf[CMSG_SPACE(sizeof myfds)]; /* ancillary data buffer */
200 msg.msg_control = buf;
201 msg.msg_controllen = sizeof buf;
202 cmsg = CMSG_FIRSTHDR(&msg);
203 cmsg\->cmsg_level = SOL_SOCKET;
204 cmsg\->cmsg_type = SCM_RIGHTS;
205 cmsg\->cmsg_len = CMSG_LEN(sizeof(int) * NUM_FD);
206 /* Initialize the payload: */
207 fdptr = (int *) CMSG_DATA(cmsg);
208 memcpy(fdptr, myfds, NUM_FD * sizeof(int));
209 /* Sum of the length of all control messages in the buffer: */
210 msg.msg_controllen = cmsg\->cmsg_len;