Pass descriptor and SENDMSG/RECVMSG functions through UNIX domain sockets
In the front we introduced UNIX domain socket programming, and more importantly UNIX domain sockets can pass file descriptors between processes on the same host.
Let's take a look at two functions:
#include <sys/types.h>
#include <sys/socket.h>
ssize_t sendmsg (int sockfd, const struct MSGHDR *msg, int flags);
ssize_t recvmsg (int sockfd, struct MSGHDR *msg, int flags);
They are similar to the SendTo and recvfrom functions, but can transmit more complex data structures, not only to transmit general data, but also to transmit additional data, i.e. file descriptors. Here's a look at the structural body MSGHDR:
struct MSGHDR {
void *msg_name; /* Optional Address * *
Socklen_t Msg_namelen; /* Size of address * *
struct Iovec *msg_iov; /* Scatter/gather Array * *
size_t Msg_iovlen; * * Elements in Msg_iov * *
void *msg_control; /* Ancillary data, below * *
size_t Msg_controllen; /* Ancillary Data buffer len * *
int msg_flags; * Flags on received message * *
};
As shown in the following illustration:
1, Msg_name: The peer's address pointer, do not care when set to null;
2, Msg_namelen: address length, do not care when set to 0;
3, Msg_iov: Is the structure body Iovec the pointer.
struct Iovec {
void *iov_base; /* Starting Address * *
size_t Iov_len; /* Number of bytes to transfer * *
};
Member Iov_base can be considered to be the size of the BUF when the normal data is transmitted Buf,iov_len.
4, Msg_iovlen: When there are n Iovec structure body, this value is n;