Monday, February 20, 2012

struct msghdr msg

This appears to be a formidable structure to establish when seeing it for the first time. But don't fear the penguins! Examine the following structure definition:

struct msghdr {
   void *msg_name;
   socklen_t msg_namelen;
   struct iovec *msg_iov;
   size_t msg_iovlen;
   void *msg_control;
   size_t msg_controllen;
   int msg_flags;
};

NOTE:
Prior to the Posix.1g standard, the msg_name and msg_control members were typically defined as C data type (char *). Additionally, members msg_namelen and msg_controllen were previously declared as int types.

The structure members can be divided into four groups. These are
  • Socket address members msg_name and msg_namelen. 
  • I/O vector references msg_iov and msg_iovlen.
  • Ancillary data buffer members msg_control and msg_controllen.
  • Received message flag bits msg_flags.
After you divide this structure into the preceding categories, the structure becomes less intimidating.

Members msg_name and msg_namelen
These members are required only when your socket is a datagram socket. The msg_name member points to the socket address that you are sending to or receiving from. The member msg_namelen indicates the length of this socket address. When calling recvmsg(2), msg_name will point to a receiving area for the address being received. When calling sendmsg(2), this will point to the destination address that the datagram is being addressed to.

Note that msg_name is defined as a (void *) data type. You will not have to cast your socket address to (struct sockaddr *).

Members msg_iov And msg_iovlen
These members specify where your I/O vector array is and how many entries it contains. The msg_iov member points to the struct iovec array. You'll remember that the I/O vector points to your buffers (review the source Listing 17.1 if you need to). The member msg_iovlen indicates how many elements are in your I/O vector array.

Members msg_control And msg_controllen
These members will point to your ancillary data buffer and indicate the buffer size (recall that ancillary data is also known as control data). The member msg_control points to the ancillary data buffer whereas msg_controllen indicates the size of that buffer.

Member msg_flags
This member is used for receiving special flag bits when recvmsg(2) is used (it is not used for sendmsg(2)). The flag bits that can be received in this location are listed in Table below.

Flag Bit
Description
MSG_EOR
This flag bit is set when the end of a record has been received. This is generally useful with SOCK_SEQPACKET socket types.
MSG_TRUNC
This flag bit indicates that the trailing end of the datagram was truncated because the receiving buffer was too small to accommodate
it.
MSG_CTRUNC
This bit indicates that some control (ancillary) data was truncated because the buffer was too small.
MSG_OOB
This bit indicates that expedited or out-of-band data was received.
MSG_ERRQUEUE
This flag bit indicates that no data was received, but an extended error was returned.

More information can be located in the man pages for recvmsg(2) and sendmsg(2) for those who are curious.

See Also:
 

No comments:

Post a Comment