There are some macros that can help gaining various information from the cmsghdr structure. This is necessary to obtain data from certain socket options (getsockopt(), setsockopt() - get and set socket options) and detailed error reports (Error analysis).
This functionality requires BCAM V25 or newer.
The macros are defined as follows:
#include <sys/socket.h> struct cmsghdr *CMSG_FIRSTHDR(struct msghdr *msg); struct cmsghdr *CMSG_NXTHDR(struct msghdr *msg, struct cmsghdr *cmsg); int CMSG_ALIGN(int length); int CMSG_SPACE(int length); int CMSG_LEN(int length); unsigned char *CMSG_DATA(struct cmsghdr *cmsg);
cmsghdr structure
The cmsghdr structure is declared in <sys.socket.h> as follows:
struct cmsghdr { u_int cmsg_len; /* number of data bytes incl. header */ int cmsg_level; /* generating protocol */ int cmsg_type; /* protocol-specific type */ /* followed by u_char cmsg_data[] */ }; struct cmsghdr *cmsg;
The sequence of cmsghdr structures should not be accessed directly. Instead, it is recommended to use the following macros.
CMSG_FIRSTHDR(struct msghdr *msg)
returns a pointer to the first cmsghdr associated with the passed msghdr. It returns NULL if there is not enough space in the buffer.
CMSG_NXTHDR(struct msghdr *msg, struct cmsghdr *cmsg)
returns the next valid cmsghdr after the passed cmsghdr. It returns NULL if there is not enough space in the buffer. CMSG_NEXTHDR can be used instead of CMSG_FIRSTHDR, wenn 0 is passed instead of a valid cmsghdr structure.
CMSG_ALIGN(int length)
given a length, returns it including the required alignment.
CMSG_SPACE(int length)
returns the total length of the cmsghdr under the assumption that the data portion of the cmsghdr requires space equal to length. The return value is only affected by length. The macro also takes account for potential padding and buffer space between two cmsghdr.
CMSG_DATA(struct cmsghdr *cmsg)
returns a pointer to the data area. The pointer returned cannot be assumend to be suitably alligned for accessing arbitrary payload data types. Applications should use memcpy() to copy data to or from a suitably declared object.
CMSG_LEN(int length)
returns the value to store in cmsg->cmsg_len. The length matches the size of the complete cmsghdr under the assumption that the data portion of the cmsghdr requires space equal to length. The macro takes account for potential padding between two cmsghdr, but ignores potential buffer space.