CMSG (3)
Access ancillary data.
SYNOPSIS
#include <sys/socket.h>
struct cmsghdr *CMSG_FIRSTHDR(struct msghdr * msgh );
struct cmsghdr *CMSG_NXTHDR(struct msghdr * msgh , struct cmsghdr * cmsg );
size_t CMSG_ALIGN(size_t length );
size_t CMSG_SPACE(size_t length );
size_t CMSG_LEN(size_t length );
void *CMSG_DATA(struct cmsghdr * cmsg );
8n 20n 32n
struct cmsghdr {
socklen_t cmsg_len; /* data byte count, including header */
int cmsg_level; /* originating protocol */
int cmsg_type; /* protocol-specific type */
/* followed by
unsigned char cmsg_data[]; */
};
DESCRIPTION
These macros are used to create and access control messages (also called
ancillary data) that are not a part of the socket payload.
This control information may
include the interface the packet was received on, various rarely used header
fields, an extended error description, a set of file descriptors or unix
credentials. For instance, control messages can be used to send
additional header fields such as IP options.
Ancillary data is sent by calling
sendmsg (2)
and received by calling
recvmsg (2).
See their manual pages for more information.
Ancillary data is a sequence of
struct cmsghdr
structures with appended data. This sequence should only be accessed
using the macros described in this manual page and never directly.
See the specific protocol man pages for the available control message types.
The maximum ancillary buffer size allowed per socket can be set using the
net.core.optmem_max
sysctl; see
socket (7).
CMSG_FIRSTHDR
returns a pointer to the first
cmsghdr
in the ancillary
data buffer associated with the passed
msghdr .
CMSG_NXTHDR
returns the next valid
cmsghdr
after the passed
cmsghdr.
It returns
NULL
when there isn't enough space left in the buffer.
CMSG_ALIGN ,
given a length, returns it including the required alignment. This is a
constant expression.
CMSG_SPACE
returns the number of bytes an ancillary element with payload of the passed data length
occupies. This is a constant expression.
CMSG_DATA
returns a pointer to the data portion of a
cmsghdr .
CMSG_LEN
returns the value to store in the
cmsg_len
member of the
cmsghdr
structure, taking into account any necessary
alignment. It takes the data length as an argument. This is a constant
expression.
To create ancillary data, first initialize the
msg_controllen
member of the
msghdr
with the length of the control message buffer. Use
CMSG_FIRSTHDR
on the
msghdr
to get the first control message and
CMSG_NEXTHDR
to get all subsequent ones.
In each control message, initialize
cmsg_len
(with
CMSG_LEN ),
the other
cmsghdr
header fields, and the data portion using
CMSG_DATA .
Finally, the
msg_controllen
field of the
msghdr
should be set to the sum of the
CMSG_SPACE
of the length of
all control messages in the buffer.
For more information on the
msghdr ,
see
recvmsg (2).
When the control message buffer is too short to store all messages, the
MSG_CTRUNC
flag is set in the
msg_flags
member of the
msghdr .
EXAMPLE
This code looks for the
IP_TTL
option in a received ancillary buffer:
8n 16n 32n
struct msghdr msgh;
struct cmsghdr *cmsg;
int *ttlptr;
int received_ttl;
/* Receive auxiliary data in msgh */
for (cmsg = CMSG_FIRSTHDR(&msgh);
cmsg != NULL;
cmsg = CMSG_NXTHDR(&msgh,cmsg) {
if (cmsg->cmsg_level == SOL_IP
&& cmsg->cmsg_type == IP_TTL) {
ttlptr = (int *) CMSG_DATA(cmsg);
received_ttl = *ttlptr;
break;
}
}
if (cmsg == NULL) {
/* Error: IP_TTL not enabled or small buffer
* or I/O error.
*/
}
The code below passes an array of file descriptors over a Unix socket using
SCM_RIGHTS :
8n 16n 32n
struct msghdr msg = {0};
struct cmsghdr *cmsg;
int myfds[NUM_FD]; /* Contains the file descriptors to pass. */
char buf[CMSG_SPACE(sizeof myfds)]; /* ancillary data buffer */
int *fdptr;
msg.msg_control = buf;
msg.msg_controllen = sizeof buf;
cmsg = CMSG_FIRSTHDR(&msg);
cmsg->cmsg_level = SOL_SOCKET;
cmsg->cmsg_type = SCM_RIGHTS;
cmsg->cmsg_len = CMSG_LEN(sizeof(int) * NUM_FD);
/* Initialize the payload: */
fdptr = (int *)CMSG_DATA(cmsg);
memcpy(fdptr, myfds, NUM_FD * sizeof(int));
/* Sum of the length of all control messages in the buffer: */
msg.msg_controllen = cmsg->cmsg_len;
NOTES
For portability, ancillary data should be accessed only using the macros
described here.
CMSG_ALIGN
is a Linux extension and should be not used in portable programs.
In Linux,
CMSG_LEN ,
CMSG_DATA ,
and
CMSG_ALIGN
are constant expressions (assuming their argument is constant) -
this could be used to declare the size of global
variables. This may be not portable, however.
CONFORMS TO
This ancillary data model conforms to the POSIX.1003.1g draft, 4.4BSD-Lite,
the IPv6 advanced API described in RFC2292 and the Single Unix specification v2.
CMSG_ALIGN
is a Linux extension.
SEE ALSO
sendmsg(2) recvmsg(2) RFC 2292 ' " ' " Copyright(c) 1989-1993 The Regents of the University of California ' " Copyright(c) 1994-1996 Sun Microsystems Inc ' " ' " See the file "license terms" for information on usage and redistribution ' " of this file and for a DISCLAIMER OF ALL WARRANTIES ' " ' " RCS: @(#) $Id: Concat (3) v 1 2 1998/09/14 18:39:46 stanton Exp $ ' " ' " The definitions below are for supplemental macros used in Tcl/Tk ' " manual entries ' " ' " AP type name in/out ?indent? ' " Start paragraph describing an argument to a library procedure ' " type is type of argument(int etc ) in/out is either "in" "out" ' " or "in/out" to describe whether procedure reads or modifies arg ' " and indent is equivalent to second arg of IP(shouldn't ever be ' " needed; use AS below instead) ' " ' " AS ?type? ?name? ' " Give maximum sizes of arguments for setting tab stops Type and ' " name are examples of largest possible arguments that will be passed ' " to AP later If args are omitted default tab stops are used ' " ' " BS ' " Start box enclosure From here until next BE everything will be ' " enclosed in one large box ' " ' " BE ' " End of box enclosure ' " ' " CS ' " Begin code excerpt ' " ' " CE ' " End code excerpt ' " ' " VS ?version? ?br? ' " Begin vertical sidebar for use in marking newly-changed parts ' " of man pages The first argument is ignored and used for recording ' " the version when the VS was added so that the sidebars can be ' " found and removed when they reach a certain age If another argument ' " is present then a line break is forced before starting the sidebar ' " ' " VE ' " End of vertical sidebar ' " ' " DS ' " Begin an indented unfilled display ' " ' " DE ' " End of indented unfilled display ' " ' " SO ' " Start of list of standard options for a Tk widget The ' " options follow on successive lines in four columns separated ' " by tabs ' " ' " SE ' " End of list of standard options for a Tk widget ' " ' " OP cmdName dbName dbClass ' " Start of description of a specific option cmdName gives the ' " option's name as specified in the class command dbName gives ' " the option's name in the option database and dbClass gives ' " the option's class in the option database ' " ' " UL arg1 arg2 ' " Print arg1 underlined then print arg2 normally ' " ' " RCS: @(#) $Id: man macros v 1 2 1998/09/14 18:39:54 stanton Exp $ ' " ' " # Set up traps and other miscellaneous stuff for Tcl/Tk man pages t wh -1 3i ^B ^l n( l b ' " # Start an argument description AP !" $4"" TP $4 { !" $2"" TP n()Cu 15
|