国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 學(xué)院 > 開(kāi)發(fā)設(shè)計(jì) > 正文

cmsg(3) - Linux man page

2019-11-14 12:47:58
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友

cmsg(3) - linux man page

Name

CMSG_ALIGN, CMSG_SPACE, CMSG_NXTHDR, CMSG_FIRSTHDR - 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);unsigned char *CMSG_DATA(struct cmsghdr *cmsg);

struct cmsghdr {    socklen_t cmsg_len;    /*數(shù)據(jù)字節(jié)數(shù),包含頭部*/    int       cmsg_level;  /* originating PRotocol */    int       cmsg_type;   /* protocol-specific type */    /* followed by unsigned char cmsg_data[]; */};

Description

這些宏用于創(chuàng)建和訪(fǎng)問(wèn)控制消息(也被稱(chēng)為附加數(shù)據(jù))--不是socket playload部分。這些控制信息包含接受報(bào)文的網(wǎng)絡(luò)接口,各種很少用的Header字段,一個(gè)擴(kuò)展的錯(cuò)誤描敘,文件描敘的集合,或者Unix UNIXcredentials。舉個(gè)例子,控制消息可以用來(lái)發(fā)送附加Header 字段比如IP 選項(xiàng)。通過(guò)調(diào)用sendmsg或者recvmsg 發(fā)送或者接受輔助數(shù)據(jù)。詳細(xì)信息可以參考man。

Ancillary data is a sequence of struct cmsghdr structures with appended data. This sequence should only be accessed using the macros described inthis manual page and never directly. See the specific protocol man pages for the available control message types. The maximum ancillary buffer size allowed persocket can be set using /proc/sys/net/core/optmem_max; see socket(7).

CMSG_FIRSTHDR() returns a pointer to the first cmsghdr in the ancillary data buffer associated with the passedmsghdr.

CMSG_NXTHDR() returns the next valid cmsghdr after the passedcmsghdr. It returns NULL when there isn't enough space left in thebuffer.

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 thecmsghdr structure, taking into account any necessary alignment. Ittakes the data length as an argument. This is a constant expression.

To create ancillary data, first initialize the msg_controllen member of themsghdr with the length of the control message buffer. UseCMSG_FIRSTHDR() on themsghdr to get the first control message and CMSG_NXTHDR() to get all subsequent ones. In each control message,initializecmsg_len (with CMSG_LEN()), the other cmsghdr header fields, and the data portion usingCMSG_DATA(). Finally, themsg_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. Formore information on themsghdr, 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 themsghdr.

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.CMSG_ALIGN() is aLinux extension.

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 inportable programs.

In Linux, CMSG_LEN(), CMSG_DATA(), and CMSG_ALIGN() are constant expressions (assuming their argument is constant); this could be usedto declare the size of global variables. This may be not portable, however.

Example

This code looks for the IP_TTL option in a received ancillary buffer:

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 == IPPROTO_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 domain socket usingSCM_RIGHTS:
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;

See Also

recvmsg(2),sendmsg(2)

RFC 2292

Referenced By

ipv6(7),netlink(3),netlink(7),recv(2),rtnetlink(7),send(2),unix(7)
上一篇:list容器

下一篇:list容器

發(fā)表評(píng)論 共有條評(píng)論
用戶(hù)名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 买车| 静海县| 酒泉市| 德安县| 沅陵县| 阜新| 磴口县| 周宁县| 军事| 永昌县| 潼关县| 吐鲁番市| 普兰店市| 洮南市| 孝义市| 五常市| 城固县| 新河县| 绵阳市| 台安县| 永定县| 广水市| 阜平县| 龙泉市| 汕尾市| 获嘉县| 泽库县| 武冈市| 闸北区| 镶黄旗| 仁化县| 鹤壁市| 长春市| 英超| 资阳市| 泗阳县| 普洱| 双城市| 延吉市| 城口县| 陇南市|