What is the difference between iov.iov_base and msg.msg_control ? I'm looking at some code examples (ipuitls open source ping) When sending data using sendmsg the packet is set in iov.iov_base When reading data using recvmsg the packet is read from msg->msg_control directly.
What is the relationship between struct iovec and struct msghdr ? Is there a difference when reading/sending data ?
Sorry for the silly question. I didn't find an answer so far and I'm confused. thanks !
Ancillary data or control messages (
.msg_controllenbytes at.msg_control) is data provided or verified by the kernel, whereas the normal payload (iniovecs) is just data received from the other endpoint, unverified and unchecked by the kernel (except for checksum, if the protocol has one).For IP sockets (see man 7 ip), there are several socket options that cause the kernel to provide ancillary data on received messages. For example:
IP_RECVORIGDSTADDRsocket option tells the kernel to provide aIP_ORIGDSTADDRtype ancillary message (with astruct sockaddr_inas data), identifying the original destination address of the datagram receivedIP_RECVOPTSsocket option tells the kernel to provide aIP_OPTIONStype ancillary message containing all IP option headers (up to 40 bytes for IPv4) for incoming datagramsPing and traceroute uses ICMP messages over IP; see man 7 icmp (and man 7 raw) for details.
Because most ICMP responses do not contain useful data filled in by the sender, the
iovecs don't usually contain anything interesting. Instead, the interesting data is in the IP message headers and options.For example, an ICMP Echo reply packets contain just 8 bytes (64 bits): 8-bit type (0), 8-bit code (0), 16-bit checksum, 16-bit id, and 16-bit sequence number. To get the IP headers with the interesting fields, you need the kernel to provide them as ancillary data control messages.
The background:
As described in the sendmsg() and related man pages, we have
with man 3 cmsg describing how to construct and access such ancillary data,
These ancillary data messages are always sufficiently aligned for the current architecture (so that the data items can be directly accessed), so to construct a proper ancillary message (SCM_CREDENTIALS to pass user, group, and process ID information over an Unix domain socket, or SCM_RIGHTS to pass file descriptors), these macros have to be used. The man 3 cmsg man page contains example code for these.
Suffice it to say, that to loop over each ancillary data part in a given message (
struct msghdr msg), you use something that boils down to