msg_iovis an array of I / O buffers with a length msg_iovlen. Each member of this array contains a pointer to a data buffer and a buffer size. This is where the data to read / write life. This allows you to read / write to an array of buffers that are not necessarily located in adjacent memory areas.
msg_controlpoints to a size buffer msg_controllenthat contains additional information about the package. To read this field, you first need to declare struct cmsghdr *(call him cmhdr). You fill it in, calling CMSG_FIRSTHDR()for the first time, passing it the address of the structure msghdrand CMSG_NXTHDR()each subsequent time, passing it the address of the structure msghdrand the current value cmhdr.
msg_control , IP- ( ) TOS/DSCP IP ( ), , setsockopt, . IP_PKTINFO IP_TOS.
. cmsg (3) manpage.
IP- msg_control, msg_name, struct sockaddr msg_namelen.
, :
struct msghdr mhdr;
struct iovec iov[1];
struct cmsghdr *cmhdr;
char control[1000];
struct sockaddr_in sin;
char databuf[1500];
unsigned char tos;
mhdr.msg_name = &sin
mhdr.msg_namelen = sizeof(sin);
mhdr.msg_iov = iov;
mhdr.msg_iovlen = 1;
mhdr.msg_control = &control;
mhdr.msg_controllen = sizeof(control);
iov[0].iov_base = databuf;
iov[0].iov_len = sizeof(databuf);
memset(databuf, 0, sizeof(databuf));
if ((*len = recvmsg(sock, &mhdr, 0)) == -1) {
perror("error on recvmsg");
exit(1);
} else {
cmhdr = CMSG_FIRSTHDR(&mhdr);
while (cmhdr) {
if (cmhdr->cmsg_level == IPPROTO_IP && cmhdr->cmsg_type == IP_TOS) {
tos = ((unsigned char *)CMSG_DATA(cmhdr))[0];
}
cmhdr = CMSG_NXTHDR(&mhdr, cmhdr);
}
printf("data read: %s, tos byte = %02X\n", databuf, tos);
}