1CMSG(3)                    Linux Programmer's Manual                   CMSG(3)
2
3
4

NAME

6       CMSG_ALIGN,  CMSG_SPACE,  CMSG_NXTHDR, CMSG_FIRSTHDR - access ancillary
7       data
8

SYNOPSIS

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
19       struct cmsghdr {
20           socklen_t cmsg_len;    /* data byte count, including header */
21           int       cmsg_level;  /* originating protocol */
22           int       cmsg_type;   /* protocol-specific type */
23           /* followed by unsigned char cmsg_data[]; */
24       };
25

DESCRIPTION

27       These macros are used to  create  and  access  control  messages  (also
28       called ancillary data) that are not a part of the socket payload.  This
29       control information may include the interface the packet  was  received
30       on, various rarely used header fields, an extended error description, a
31       set of file descriptors or UNIX  credentials.   For  instance,  control
32       messages  can  be  used  to  send  additional  header fields such as IP
33       options.  Ancillary data is sent by calling sendmsg(2) and received  by
34       calling recvmsg(2).  See their manual pages for more information.
35
36       Ancillary data is a sequence of struct cmsghdr structures with appended
37       data.  This sequence should be accessed using only the macros described
38       in  this manual page and never directly.  See the specific protocol man
39       pages for the available control message types.  The  maximum  ancillary
40       buffer size allowed per socket can be set using /proc/sys/net/core/opt‐
41       mem_max; see socket(7).
42
43       CMSG_FIRSTHDR() returns a pointer to the first cmsghdr in the ancillary
44       data buffer associated with the passed msghdr.
45
46       CMSG_NXTHDR()  returns the next valid cmsghdr after the passed cmsghdr.
47       It returns NULL when there isn't enough space left in the buffer.
48
49       CMSG_ALIGN(), given a length, returns it including the required  align‐
50       ment.  This is a constant expression.
51
52       CMSG_SPACE() returns the number of bytes an ancillary element with pay‐
53       load of the passed data length occupies.  This is  a  constant  expres‐
54       sion.
55
56       CMSG_DATA() returns a pointer to the data portion of a cmsghdr.
57
58       CMSG_LEN()  returns  the  value  to store in the cmsg_len member of the
59       cmsghdr structure, taking into account  any  necessary  alignment.   It
60       takes the data length as an argument.  This is a constant expression.
61
62       To create ancillary data, first initialize the msg_controllen member of
63       the msghdr  with  the  length  of  the  control  message  buffer.   Use
64       CMSG_FIRSTHDR()  on  the  msghdr  to  get the first control message and
65       CMSG_NXTHDR() to get all subsequent ones.   In  each  control  message,
66       initialize cmsg_len (with CMSG_LEN()), the other cmsghdr header fields,
67       and the data portion using CMSG_DATA().   Finally,  the  msg_controllen
68       field of the msghdr should be set to the sum of the CMSG_SPACE() of the
69       length of all control messages in the buffer.  For more information  on
70       the msghdr, see recvmsg(2).
71
72       When the control message buffer is too short to store all messages, the
73       MSG_CTRUNC flag is set in the msg_flags member of the msghdr.
74

CONFORMING TO

76       This ancillary data model conforms to the POSIX.1g draft,  4.4BSD-Lite,
77       the   IPv6   advanced   API   described  in  RFC 2292  and  the  SUSv2.
78       CMSG_ALIGN() is a Linux extension.
79

NOTES

81       For portability, ancillary data  should  be  accessed  using  only  the
82       macros described here.  CMSG_ALIGN() is a Linux extension and should be
83       not used in portable programs.
84
85       In  Linux,  CMSG_LEN(),  CMSG_DATA(),  and  CMSG_ALIGN()  are  constant
86       expressions  (assuming  their argument is constant); this could be used
87       to declare the size of global variables.  This  may  be  not  portable,
88       however.
89

EXAMPLE

91       This code looks for the IP_TTL option in a received ancillary buffer:
92
93           struct msghdr msgh;
94           struct cmsghdr *cmsg;
95           int *ttlptr;
96           int received_ttl;
97
98           /* Receive auxiliary data in msgh */
99           for (cmsg = CMSG_FIRSTHDR(&msgh); cmsg != NULL;
100                   cmsg = CMSG_NXTHDR(&msgh,cmsg)) {
101               if (cmsg->cmsg_level == IPPROTO_IP
102                       && cmsg->cmsg_type == IP_TTL) {
103                   ttlptr = (int *) CMSG_DATA(cmsg);
104                   received_ttl = *ttlptr;
105                   break;
106               }
107           }
108           if (cmsg == NULL) {
109               /*
110                * Error: IP_TTL not enabled or small buffer
111                * or I/O error.
112                */
113           }
114
115       The  code  below passes an array of file descriptors over a UNIX domain
116       socket using SCM_RIGHTS:
117
118           struct msghdr msg = {0};
119           struct cmsghdr *cmsg;
120           int myfds[NUM_FD]; /* Contains the file descriptors to pass. */
121           char buf[CMSG_SPACE(sizeof myfds)];  /* ancillary data buffer */
122           int *fdptr;
123
124           msg.msg_control = buf;
125           msg.msg_controllen = sizeof buf;
126           cmsg = CMSG_FIRSTHDR(&msg);
127           cmsg->cmsg_level = SOL_SOCKET;
128           cmsg->cmsg_type = SCM_RIGHTS;
129           cmsg->cmsg_len = CMSG_LEN(sizeof(int) * NUM_FD);
130           /* Initialize the payload: */
131           fdptr = (int *) CMSG_DATA(cmsg);
132           memcpy(fdptr, myfds, NUM_FD * sizeof(int));
133           /* Sum of the length of all control messages in the buffer: */
134           msg.msg_controllen = cmsg->cmsg_len;
135

SEE ALSO

137       recvmsg(2), sendmsg(2)
138
139       RFC 2292
140

COLOPHON

142       This page is part of release 3.53 of the Linux  man-pages  project.   A
143       description  of  the project, and information about reporting bugs, can
144       be found at http://www.kernel.org/doc/man-pages/.
145
146
147
148Linux                             2008-11-20                           CMSG(3)
Impressum