Introducing the struct cmsghdr Structure
Ancillary information can include zero, one, or more separate ancillary data objects. Each of these objects is preceded by a struct cmsghdr. This header is followed possibly by pad bytes and
then the object itself. Finally, the ancillary data object itself might be followed by still more pad bytes before the next cmsghdr follows. In this post, the only ancillary data objects that you'll be concerned about will be the file descriptor and a credentials structure.
Figure below illustrates how a buffer containing ancillary data is structured.
Note the following additional points about below Figure :
- The value of cmsg_len is equivalent to the length shown as the macro value for CMSG_LEN() in Figure below.
- The macro CMSG_SPACE() computes the total necessary space required for one ancillary data object.
- The value of msg_controllen is the sum of the CMSG_SPACE() lengths, and is computed for each ancillary data object.
Figure:
Ancillary data structures are composed of various substructures, data zones, and pad•bytes.
Ancillary data structures are composed of various substructures, data zones, and pad•bytes.
The control message header itself is defined as the following C structure:
struct cmsghdr {
socklen_t cmsg_len;
int cmsg_level;
int cmsg_type;
/* u_char cmsg_data[]; */
};
socklen_t cmsg_len;
int cmsg_level;
int cmsg_type;
/* u_char cmsg_data[]; */
};
Table a: The struct cmshdr Members
Member
|
Description
|
cmsg_len
|
This is the
byte count of the ancillary data, which includes the size of this
structural header. This value is computed by the CMSG_LEN() macro.
|
cmsg_level
|
This value
indicates the originating protocol level (for example, SOL_SOCKET).
|
cmsg_type
|
This value
indicates the control message type (for example, SCM_RIGHTS).
|
cmsg_data
|
This member
does not actually exist. It is shown in comments to illustrate
where additional ancillary data is located physically.
|
The
example programs used in this chapter will use only a cmsg_level value
of SOL_SOCKET. The control message types that are of interest to you in
this chapter are shown in Table b.
Table b: cmsg_type Types for cmsg_level=SOL_SOCKET
cmsg_level
|
Description
|
SCM_RIGHTS
|
The
ancillary data object is a file descriptor.
|
SCM_CREDENTIALS
|
The
ancillary data object is a structure containing credential information.
|
See Also:
No comments:
Post a Comment