2004-11-03 13:51:07 +00:00
|
|
|
.\" This man page is Copyright (C) 1999 Andi Kleen <ak@muc.de>.
|
|
|
|
.\" Permission is granted to distribute possibly modified copies
|
|
|
|
.\" of this page provided the header is included verbatim,
|
|
|
|
.\" and in case of nontrivial modification author and date
|
|
|
|
.\" of the modification is added to the header.
|
|
|
|
.\" $Id: cmsg.3,v 1.8 2000/12/20 18:10:31 ak Exp $
|
connect.2, listen.2, send.2, uname.2, cmsg.3, proc.5, arp.7, ddp.7, icmp.7, ip.7, raw.7, socket.7, tcp.7, udp.7: Global fix: eliminate mention of the obsolete sysctl(2) interface
Many pages still mention use of the obsolete sysctl(2) system
call, or used the term "sysctls"; rewrite these mentions to
instead be in terms of /proc interfaces.
Signed-off-by: Michael Kerrisk <mtk.manpages@gmail.com>
2008-11-20 20:37:11 +00:00
|
|
|
.TH CMSG 3 2008-11-20 "Linux" "Linux Programmer's Manual"
|
2004-11-03 13:51:07 +00:00
|
|
|
.SH NAME
|
|
|
|
CMSG_ALIGN, CMSG_SPACE, CMSG_NXTHDR, CMSG_FIRSTHDR \- Access ancillary data
|
|
|
|
.SH SYNOPSIS
|
|
|
|
.B #include <sys/socket.h>
|
2007-04-05 12:36:57 +00:00
|
|
|
.sp
|
2004-11-03 13:51:07 +00:00
|
|
|
.BI "struct cmsghdr *CMSG_FIRSTHDR(struct msghdr *" msgh );
|
|
|
|
.br
|
|
|
|
.BI "struct cmsghdr *CMSG_NXTHDR(struct msghdr *" msgh ", struct cmsghdr *" cmsg );
|
|
|
|
.br
|
|
|
|
.BI "size_t CMSG_ALIGN(size_t " length );
|
|
|
|
.br
|
|
|
|
.BI "size_t CMSG_SPACE(size_t " length );
|
|
|
|
.br
|
|
|
|
.BI "size_t CMSG_LEN(size_t " length );
|
|
|
|
.br
|
2007-04-12 22:42:49 +00:00
|
|
|
.BI "unsigned char *CMSG_DATA(struct cmsghdr *" cmsg );
|
2004-11-03 13:51:07 +00:00
|
|
|
.sp
|
|
|
|
.nf
|
|
|
|
struct cmsghdr {
|
2007-04-05 12:36:57 +00:00
|
|
|
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[]; */
|
2004-11-03 13:51:07 +00:00
|
|
|
};
|
|
|
|
.fi
|
|
|
|
.SH DESCRIPTION
|
|
|
|
These macros are used to create and access control messages (also called
|
|
|
|
ancillary data) that are not a part of the socket payload.
|
2007-04-12 22:42:49 +00:00
|
|
|
This control information may
|
|
|
|
include the interface the packet was received on, various rarely used header
|
intro.1, time.1, accept.2, bind.2, connect.2, execve.2, flock.2, getdents.2, getpriority.2, getuid.2, intro.2, ioctl.2, mincore.2, mknod.2, personality.2, ptrace.2, read.2, recv.2, select_tut.2, send.2, sendfile.2, shmctl.2, sigaction.2, signal.2, stat.2, times.2, truncate.2, umask.2, wait.2, MB_CUR_MAX.3, MB_LEN_MAX.3, argz_add.3, btowc.3, clearenv.3, clock.3, cmsg.3, end.3, endian.3, errno.3, exit.3, fgetwc.3, fgetws.3, fopen.3, fputwc.3, fputws.3, fseek.3, fwide.3, getfsent.3, getgrnam.3, gethostid.3, getipnodebyname.3, getmntent.3, getpwnam.3, getwchar.3, grantpt.3, iconv.3, iconv_close.3, iconv_open.3, insque.3, intro.3, iswalnum.3, iswalpha.3, iswblank.3, iswcntrl.3, iswctype.3, iswdigit.3, iswgraph.3, iswlower.3, iswprint.3, iswpunct.3, iswspace.3, iswupper.3, iswxdigit.3, malloc.3, mblen.3, mbrlen.3, mbrtowc.3, mbsinit.3, mbsnrtowcs.3, mbsrtowcs.3, mbstowcs.3, mbtowc.3, mkstemp.3, mktemp.3, nl_langinfo.3, openpty.3, posix_openpt.3, printf.3, ptsname.3, putwchar.3, qecvt.3, rcmd.3, readdir.3, rexec.3, rpc.3, setnetgrent.3, shm_open.3, sigpause.3, stdin.3, stpcpy.3, strftime.3, strptime.3, syslog.3, towctrans.3, towlower.3, towupper.3, ttyslot.3, ungetwc.3, unlocked_stdio.3, wcpcpy.3, wcpncpy.3, wcrtomb.3, wcscasecmp.3, wcscat.3, wcschr.3, wcscmp.3, wcscpy.3, wcscspn.3, wcsdup.3, wcslen.3, wcsncasecmp.3, wcsncat.3, wcsncmp.3, wcsncpy.3, wcsnlen.3, wcsnrtombs.3, wcspbrk.3, wcsrchr.3, wcsrtombs.3, wcsspn.3, wcsstr.3, wcstok.3, wcstombs.3, wcswidth.3, wctob.3, wctomb.3, wctrans.3, wctype.3, wcwidth.3, wmemchr.3, wmemcmp.3, wmemcpy.3, wmemmove.3, wmemset.3, wprintf.3, console_ioctl.4, pts.4, elf.5, filesystems.5, hosts.5, proc.5, ttytype.5, boot.7, capabilities.7, credentials.7, epoll.7, glob.7, koi8-r.7, path_resolution.7, pty.7, signal.7, suffixes.7, time.7, unicode.7, unix.7, uri.7, utf-8.7: global fix: s/Unix/UNIX/
The man pages were rather inconsistent in the use of "Unix"
versus "UNIX". Let's go with the trademark usage.
Signed-off-by: Michael Kerrisk <mtk.manpages@gmail.com>
2010-10-12 04:45:38 +00:00
|
|
|
fields, an extended error description, a set of file descriptors or UNIX
|
2007-04-12 22:42:49 +00:00
|
|
|
credentials.
|
|
|
|
For instance, control messages can be used to send
|
|
|
|
additional header fields such as IP options.
|
|
|
|
Ancillary data is sent by calling
|
2004-11-03 13:51:07 +00:00
|
|
|
.BR sendmsg (2)
|
|
|
|
and received by calling
|
|
|
|
.BR recvmsg (2).
|
2007-04-12 22:42:49 +00:00
|
|
|
See their manual pages for more information.
|
2004-11-03 13:51:07 +00:00
|
|
|
.PP
|
2007-04-12 22:42:49 +00:00
|
|
|
Ancillary data is a sequence of
|
|
|
|
.I 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.
|
connect.2, listen.2, send.2, uname.2, cmsg.3, proc.5, arp.7, ddp.7, icmp.7, ip.7, raw.7, socket.7, tcp.7, udp.7: Global fix: eliminate mention of the obsolete sysctl(2) interface
Many pages still mention use of the obsolete sysctl(2) system
call, or used the term "sysctls"; rewrite these mentions to
instead be in terms of /proc interfaces.
Signed-off-by: Michael Kerrisk <mtk.manpages@gmail.com>
2008-11-20 20:37:11 +00:00
|
|
|
The maximum ancillary buffer size allowed per socket can be set using
|
|
|
|
.IR /proc/sys/net/core/optmem_max ;
|
|
|
|
see
|
2007-04-12 22:42:49 +00:00
|
|
|
.BR socket (7).
|
2004-11-03 13:51:07 +00:00
|
|
|
.PP
|
2005-10-19 06:54:38 +00:00
|
|
|
.BR CMSG_FIRSTHDR ()
|
2007-04-12 22:42:49 +00:00
|
|
|
returns a pointer to the first
|
2007-06-23 07:56:56 +00:00
|
|
|
.I cmsghdr
|
2004-11-03 13:51:07 +00:00
|
|
|
in the ancillary
|
2007-04-12 22:42:49 +00:00
|
|
|
data buffer associated with the passed
|
2007-06-23 07:56:56 +00:00
|
|
|
.IR msghdr .
|
2004-11-03 13:51:07 +00:00
|
|
|
.PP
|
2005-10-19 06:54:38 +00:00
|
|
|
.BR CMSG_NXTHDR ()
|
2007-04-12 22:42:49 +00:00
|
|
|
returns the next valid
|
2007-06-23 07:56:56 +00:00
|
|
|
.I cmsghdr
|
2007-04-12 22:42:49 +00:00
|
|
|
after the passed
|
2007-06-23 07:56:56 +00:00
|
|
|
.IR cmsghdr .
|
2005-11-02 13:55:25 +00:00
|
|
|
It returns NULL when there isn't enough space left in the buffer.
|
2004-11-03 13:51:07 +00:00
|
|
|
.PP
|
2005-10-19 06:54:38 +00:00
|
|
|
.BR CMSG_ALIGN (),
|
2007-04-12 22:42:49 +00:00
|
|
|
given a length, returns it including the required alignment.
|
|
|
|
This is a
|
2004-11-03 13:51:07 +00:00
|
|
|
constant expression.
|
|
|
|
.PP
|
2005-10-19 06:54:38 +00:00
|
|
|
.BR CMSG_SPACE ()
|
2007-04-12 22:42:49 +00:00
|
|
|
returns the number of bytes an ancillary element with payload of the
|
|
|
|
passed data length occupies.
|
2006-12-27 03:54:41 +00:00
|
|
|
This is a constant expression.
|
2004-11-03 13:51:07 +00:00
|
|
|
.PP
|
2008-07-07 13:00:11 +00:00
|
|
|
.BR CMSG_DATA ()
|
2007-04-12 22:42:49 +00:00
|
|
|
returns a pointer to the data portion of a
|
2007-06-23 07:56:56 +00:00
|
|
|
.IR cmsghdr .
|
2004-11-03 13:51:07 +00:00
|
|
|
.PP
|
2008-07-07 13:00:11 +00:00
|
|
|
.BR CMSG_LEN ()
|
2007-04-12 22:42:49 +00:00
|
|
|
returns the value to store in the
|
2004-11-03 13:51:07 +00:00
|
|
|
.I cmsg_len
|
2007-04-12 22:42:49 +00:00
|
|
|
member of the
|
2007-06-23 07:56:56 +00:00
|
|
|
.I cmsghdr
|
2004-11-03 13:51:07 +00:00
|
|
|
structure, taking into account any necessary
|
2007-04-12 22:42:49 +00:00
|
|
|
alignment.
|
|
|
|
It takes the data length as an argument.
|
|
|
|
This is a constant
|
|
|
|
expression.
|
2004-11-03 13:51:07 +00:00
|
|
|
.PP
|
2007-04-12 22:42:49 +00:00
|
|
|
To create ancillary data, first initialize the
|
2004-11-03 13:51:07 +00:00
|
|
|
.I msg_controllen
|
2007-04-12 22:42:49 +00:00
|
|
|
member of the
|
2007-06-23 07:56:56 +00:00
|
|
|
.I msghdr
|
2007-04-12 22:42:49 +00:00
|
|
|
with the length of the control message buffer.
|
|
|
|
Use
|
2005-10-19 06:54:38 +00:00
|
|
|
.BR CMSG_FIRSTHDR ()
|
2007-04-12 22:42:49 +00:00
|
|
|
on the
|
2007-06-23 07:56:56 +00:00
|
|
|
.I msghdr
|
2004-11-03 13:51:07 +00:00
|
|
|
to get the first control message and
|
2011-09-08 14:52:13 +00:00
|
|
|
.BR CMSG_NXTHDR ()
|
2004-11-03 13:51:07 +00:00
|
|
|
to get all subsequent ones.
|
|
|
|
In each control message, initialize
|
|
|
|
.I cmsg_len
|
2007-04-12 22:42:49 +00:00
|
|
|
(with
|
2008-09-29 09:33:04 +00:00
|
|
|
.BR CMSG_LEN ()),
|
2007-04-12 22:42:49 +00:00
|
|
|
the other
|
2007-06-23 07:56:56 +00:00
|
|
|
.I cmsghdr
|
2007-04-12 22:42:49 +00:00
|
|
|
header fields, and the data portion using
|
2008-09-29 09:33:04 +00:00
|
|
|
.BR CMSG_DATA ().
|
2007-04-12 22:42:49 +00:00
|
|
|
Finally, the
|
|
|
|
.I msg_controllen
|
|
|
|
field of the
|
2007-06-23 07:56:56 +00:00
|
|
|
.I msghdr
|
2007-04-12 22:42:49 +00:00
|
|
|
should be set to the sum of the
|
2005-10-19 06:54:38 +00:00
|
|
|
.BR CMSG_SPACE ()
|
2007-04-12 22:42:49 +00:00
|
|
|
of the length of
|
|
|
|
all control messages in the buffer.
|
|
|
|
For more information on the
|
2007-06-23 07:56:56 +00:00
|
|
|
.IR msghdr ,
|
2004-11-03 13:51:07 +00:00
|
|
|
see
|
2007-04-12 22:42:49 +00:00
|
|
|
.BR recvmsg (2).
|
2004-11-03 13:51:07 +00:00
|
|
|
.PP
|
|
|
|
When the control message buffer is too short to store all messages, the
|
2007-04-12 22:42:49 +00:00
|
|
|
.B MSG_CTRUNC
|
|
|
|
flag is set in the
|
|
|
|
.I msg_flags
|
|
|
|
member of the
|
2007-06-23 07:56:56 +00:00
|
|
|
.IR msghdr .
|
2007-05-19 04:30:20 +00:00
|
|
|
.SH "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.
|
2008-07-07 13:00:11 +00:00
|
|
|
.BR CMSG_ALIGN ()
|
2007-05-19 04:30:20 +00:00
|
|
|
is a Linux extension.
|
|
|
|
.SH NOTES
|
|
|
|
For portability, ancillary data should be accessed only using the macros
|
|
|
|
described here.
|
|
|
|
.BR CMSG_ALIGN ()
|
|
|
|
is a Linux extension and should be not used in portable programs.
|
|
|
|
.PP
|
|
|
|
In Linux,
|
2008-07-07 13:00:11 +00:00
|
|
|
.BR CMSG_LEN (),
|
|
|
|
.BR CMSG_DATA (),
|
2007-05-19 04:30:20 +00:00
|
|
|
and
|
|
|
|
.BR 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.
|
2004-11-03 13:51:07 +00:00
|
|
|
.SH EXAMPLE
|
2007-04-12 22:42:49 +00:00
|
|
|
This code looks for the
|
|
|
|
.B IP_TTL
|
2004-11-03 13:51:07 +00:00
|
|
|
option in a received ancillary buffer:
|
|
|
|
.PP
|
2007-12-19 06:16:04 +00:00
|
|
|
.in +4n
|
2004-11-03 13:51:07 +00:00
|
|
|
.nf
|
2007-04-12 22:42:49 +00:00
|
|
|
struct msghdr msgh;
|
2004-11-03 13:51:07 +00:00
|
|
|
struct cmsghdr *cmsg;
|
|
|
|
int *ttlptr;
|
|
|
|
int received_ttl;
|
|
|
|
|
2007-04-12 22:42:49 +00:00
|
|
|
/* Receive auxiliary data in msgh */
|
2007-12-30 05:42:10 +00:00
|
|
|
for (cmsg = CMSG_FIRSTHDR(&msgh); cmsg != NULL;
|
|
|
|
cmsg = CMSG_NXTHDR(&msgh,cmsg)) {
|
|
|
|
if (cmsg\->cmsg_level == IPPROTO_IP
|
|
|
|
&& cmsg\->cmsg_type == IP_TTL) {
|
|
|
|
ttlptr = (int *) CMSG_DATA(cmsg);
|
|
|
|
received_ttl = *ttlptr;
|
|
|
|
break;
|
2007-04-05 14:17:54 +00:00
|
|
|
}
|
2007-12-30 05:42:10 +00:00
|
|
|
}
|
2004-11-03 13:51:07 +00:00
|
|
|
if (cmsg == NULL) {
|
2007-04-05 13:29:41 +00:00
|
|
|
/*
|
|
|
|
* Error: IP_TTL not enabled or small buffer
|
|
|
|
* or I/O error.
|
2007-04-12 22:42:49 +00:00
|
|
|
*/
|
|
|
|
}
|
2004-11-03 13:51:07 +00:00
|
|
|
.fi
|
2007-12-19 06:16:04 +00:00
|
|
|
.in
|
2004-11-03 13:51:07 +00:00
|
|
|
.PP
|
2010-10-12 04:48:33 +00:00
|
|
|
The code below passes an array of file descriptors over a
|
|
|
|
UNIX domain socket using
|
2004-11-03 13:51:07 +00:00
|
|
|
.BR SCM_RIGHTS :
|
|
|
|
.PP
|
2007-12-19 06:16:04 +00:00
|
|
|
.in +4n
|
2004-11-03 13:51:07 +00:00
|
|
|
.nf
|
|
|
|
struct msghdr msg = {0};
|
|
|
|
struct cmsghdr *cmsg;
|
2007-04-12 22:42:49 +00:00
|
|
|
int myfds[NUM_FD]; /* Contains the file descriptors to pass. */
|
2004-11-03 13:51:07 +00:00
|
|
|
char buf[CMSG_SPACE(sizeof myfds)]; /* ancillary data buffer */
|
|
|
|
int *fdptr;
|
|
|
|
|
2007-04-12 22:42:49 +00:00
|
|
|
msg.msg_control = buf;
|
2004-11-03 13:51:07 +00:00
|
|
|
msg.msg_controllen = sizeof buf;
|
2007-04-12 22:42:49 +00:00
|
|
|
cmsg = CMSG_FIRSTHDR(&msg);
|
2007-06-20 21:39:45 +00:00
|
|
|
cmsg\->cmsg_level = SOL_SOCKET;
|
|
|
|
cmsg\->cmsg_type = SCM_RIGHTS;
|
|
|
|
cmsg\->cmsg_len = CMSG_LEN(sizeof(int) * NUM_FD);
|
2007-04-12 22:42:49 +00:00
|
|
|
/* Initialize the payload: */
|
2007-05-21 08:47:38 +00:00
|
|
|
fdptr = (int *) CMSG_DATA(cmsg);
|
2007-04-12 22:42:49 +00:00
|
|
|
memcpy(fdptr, myfds, NUM_FD * sizeof(int));
|
|
|
|
/* Sum of the length of all control messages in the buffer: */
|
2007-06-20 21:39:45 +00:00
|
|
|
msg.msg_controllen = cmsg\->cmsg_len;
|
2004-11-03 13:51:07 +00:00
|
|
|
.fi
|
2007-12-19 06:16:04 +00:00
|
|
|
.in
|
2004-11-03 13:51:07 +00:00
|
|
|
.SH "SEE ALSO"
|
|
|
|
.BR recvmsg (2),
|
|
|
|
.BR sendmsg (2)
|
|
|
|
.PP
|
2005-07-20 07:50:45 +00:00
|
|
|
RFC\ 2292
|