Manual Pages for Linux CentOS command on man CMSG_NXTHDR
MyWebUniversity

Manual Pages for Linux CentOS command on man CMSG_NXTHDR

CMSG(3) Linux Programmer's Manual CMSG(3)

NAME

CMSGALIGN, CMSGSPACE, CMSGNXTHDR, CMSGFIRSTHDR - access ancillary data SYNOPSIS

#include struct cmsghdr *CMSGFIRSTHDR(struct msghdr *msgh); struct cmsghdr *CMSGNXTHDR(struct msghdr *msgh, struct cmsghdr *cmsg); sizet CMSGALIGN(sizet length); sizet CMSGSPACE(sizet length); sizet CMSGLEN(sizet length); unsigned char *CMSGDATA(struct cmsghdr *cmsg); struct cmsghdr { sizet cmsglen; /* Data byte count, including header (type is socklent in POSIX) */ int cmsglevel; /* Originating protocol */

int cmsgtype; /* Protocol-specific type */ /* followed by unsigned char cmsgdata[]; */ }; 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 be accessed using only 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 /proc/sys/net/core/opt‐ memmax; see socket(7). CMSGFIRSTHDR() returns a pointer to the first cmsghdr in the ancillary data buffer associated with the passed msghdr. CMSGNXTHDR() returns the next valid cmsghdr after the passed cmsghdr. It returns NULL when there isn't enough space left in the buffer. CMSGALIGN(), given a length, returns it including the required align‐ ment. This is a constant expression. CMSGSPACE() returns the number of bytes an ancillary element with pay‐ load of the passed data length occupies. This is a constant expres‐ sion. CMSGDATA() returns a pointer to the data portion of a cmsghdr. CMSGLEN() returns the value to store in the cmsglen 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 msgcontrollen member of the msghdr with the length of the control message buffer. Use CMSGFIRSTHDR() on the msghdr to get the first control message and CMSGNXTHDR() to get all subsequent ones. In each control message, initialize cmsglen (with CMSGLEN()), the other cmsghdr header fields, and the data portion using CMSGDATA(). Finally, the msgcontrollen field of the msghdr should be set to the sum of the CMSGSPACE() 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 MSGCTRUNC flag is set in the msgflags member of the msghdr. CONFORMING TO

This ancillary data model conforms to the POSIX.1g draft, 4.4BSD-Lite, the IPv6 advanced API described in RFC 2292 and the SUSv2. CMSGALIGN() is a Linux extension. NOTES For portability, ancillary data should be accessed using only the macros described here. CMSGALIGN() is a Linux extension and should be not used in portable programs. In Linux, CMSGLEN(), CMSGDATA(), and CMSGALIGN() 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. EXAMPLE This code looks for the IPTTL option in a received ancillary buffer: struct msghdr msgh; struct cmsghdr *cmsg; int *ttlptr; int receivedttl; /* Receive auxiliary data in msgh */ for (cmsg = CMSGFIRSTHDR(&msgh); cmsg != NULL; cmsg = CMSGNXTHDR(&msgh,cmsg)) {

if (cmsg->cmsglevel == IPPROTOIP

&& cmsg->cmsgtype == IPTTL) { ttlptr = (int *) CMSGDATA(cmsg); receivedttl = *ttlptr; break; } } if (cmsg == NULL) { /* * Error: IPTTL not enabled or small buffer * or I/O error. */ } The code below passes an array of file descriptors over a UNIX domain socket using SCMRIGHTS: struct msghdr msg = {0}; struct cmsghdr *cmsg; int myfds[NUMFD]; /* Contains the file descriptors to pass. */ char buf[CMSGSPACE(sizeof myfds)]; /* ancillary data buffer */ int *fdptr; msg.msgcontrol = buf; msg.msgcontrollen = sizeof buf; cmsg = CMSGFIRSTHDR(&msg);

cmsg->cmsglevel = SOLSOCKET;

cmsg->cmsgtype = SCMRIGHTS;

cmsg->cmsglen = CMSGLEN(sizeof(int) * NUMFD); /* Initialize the payload: */ fdptr = (int *) CMSGDATA(cmsg); memcpy(fdptr, myfds, NUMFD * sizeof(int)); /* Sum of the length of all control messages in the buffer: */

msg.msgcontrollen = cmsg->cmsglen; SEE ALSO recvmsg(2), sendmsg(2) RFC 2292 COLOPHON

This page is part of release 3.53 of the Linux man-pages project. A description of the project, and information about reporting bugs, can

be found at http://www.kernel.org/doc/man-pages/.

Linux 2008-11-20 CMSG(3)




Contact us      |      About us      |      Term of use      |       Copyright © 2000-2019 MyWebUniversity.com ™