diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/attr.c | 150 | ||||
-rw-r--r-- | src/nlmsg.c | 110 | ||||
-rw-r--r-- | src/socket.c | 4 |
3 files changed, 160 insertions, 104 deletions
@@ -33,7 +33,7 @@ * mnl_attr_get_type - get type of netlink attribute * \param attr pointer to netlink attribute * - * This function returns the attribute type. + * \return the attribute type */ EXPORT_SYMBOL uint16_t mnl_attr_get_type(const struct nlattr *attr) { @@ -44,8 +44,11 @@ EXPORT_SYMBOL uint16_t mnl_attr_get_type(const struct nlattr *attr) * mnl_attr_get_len - get length of netlink attribute * \param attr pointer to netlink attribute * - * This function returns the attribute length that is the attribute header - * plus the attribute payload. + * \return the attribute length + * + * The attribute length is the length of the attribute header plus the + * attribute payload. + * */ EXPORT_SYMBOL uint16_t mnl_attr_get_len(const struct nlattr *attr) { @@ -56,7 +59,7 @@ EXPORT_SYMBOL uint16_t mnl_attr_get_len(const struct nlattr *attr) * mnl_attr_get_payload_len - get the attribute payload-value length * \param attr pointer to netlink attribute * - * This function returns the attribute payload-value length. + * \return the attribute payload-value length */ EXPORT_SYMBOL uint16_t mnl_attr_get_payload_len(const struct nlattr *attr) { @@ -67,7 +70,7 @@ EXPORT_SYMBOL uint16_t mnl_attr_get_payload_len(const struct nlattr *attr) * mnl_attr_get_payload - get pointer to the attribute payload * \param attr pointer to netlink attribute * - * This function return a pointer to the attribute payload. + * \return pointer to the attribute payload */ EXPORT_SYMBOL void *mnl_attr_get_payload(const struct nlattr *attr) { @@ -85,10 +88,12 @@ EXPORT_SYMBOL void *mnl_attr_get_payload(const struct nlattr *attr) * truncated. * * This function does not set errno in case of error since it is intended - * for iterations. Thus, it returns true on success and false on error. + * for iterations. * * The len parameter may be negative in the case of malformed messages during * attribute iteration, that is why we use a signed integer. + * + * \return true if there is room for the attribute, false otherwise */ EXPORT_SYMBOL bool mnl_attr_ok(const struct nlattr *attr, int len) { @@ -101,9 +106,11 @@ EXPORT_SYMBOL bool mnl_attr_ok(const struct nlattr *attr, int len) * mnl_attr_next - get the next attribute in the payload of a netlink message * \param attr pointer to the current attribute * - * This function returns a pointer to the next attribute after the one passed - * as parameter. You have to use mnl_attr_ok() to ensure that the next - * attribute is valid. + * \return a pointer to the next attribute after the one passed in + * + * You have to use mnl_attr_ok() on the returned attribute to ensure that the + * next attribute is valid. + * */ EXPORT_SYMBOL struct nlattr *mnl_attr_next(const struct nlattr *attr) { @@ -115,14 +122,18 @@ EXPORT_SYMBOL struct nlattr *mnl_attr_next(const struct nlattr *attr) * \param attr pointer to attribute to be checked * \param max maximum attribute type * - * This function allows to check if the attribute type is higher than the - * maximum supported type. If the attribute type is invalid, this function - * returns -1 and errno is explicitly set. On success, this function returns 1. + * This function allows one to check if the attribute type is higher than the + * maximum supported type. * * Strict attribute checking in user-space is not a good idea since you may * run an old application with a newer kernel that supports new attributes. * This leads to backward compatibility breakages in user-space. Better check * if you support an attribute, if not, skip it. + * + * On an error, errno is explicitly set. + * + * \return 1 if the attribute is valid, -1 otherwise + * */ EXPORT_SYMBOL int mnl_attr_type_valid(const struct nlattr *attr, uint16_t max) { @@ -201,8 +212,11 @@ static const size_t mnl_attr_data_type_len[MNL_TYPE_MAX] = { * \param type data type (see enum mnl_attr_data_type) * * The validation is based on the data type. Specifically, it checks that - * integers (u8, u16, u32 and u64) have enough room for them. This function - * returns -1 in case of error, and errno is explicitly set. + * integers (u8, u16, u32 and u64) have enough room for them. + * + * On an error, errno is explicitly set. + * + * \return 0 on success, -1 on error */ EXPORT_SYMBOL int mnl_attr_validate(const struct nlattr *attr, enum mnl_attr_data_type type) { @@ -222,9 +236,13 @@ EXPORT_SYMBOL int mnl_attr_validate(const struct nlattr *attr, enum mnl_attr_dat * \param type attribute type (see enum mnl_attr_data_type) * \param exp_len expected attribute data size * - * This function allows to perform a more accurate validation for attributes - * whose size is variable. If the size of the attribute is not what we expect, - * this functions returns -1 and errno is explicitly set. + * This function allows one to perform a more accurate validation for attributes + * whose size is variable. + * + * On an error, errno is explicitly set. + * + * \return 0 if the attribute is valid and fits within the expected length, -1 + * otherwise */ EXPORT_SYMBOL int mnl_attr_validate2(const struct nlattr *attr, enum mnl_attr_data_type type, @@ -244,13 +262,13 @@ EXPORT_SYMBOL int mnl_attr_validate2(const struct nlattr *attr, * \param cb callback function that is called for each attribute * \param data pointer to data that is passed to the callback function * - * This function allows to iterate over the sequence of attributes that compose - * the Netlink message. You can then put the attribute in an array as it + * This function allows you to iterate over the sequence of attributes that + * compose the Netlink message. You can then put the attribute in an array as it * usually happens at this stage or you can use any other data structure (such * as lists or trees). * - * This function propagates the return value of the callback, which can be - * MNL_CB_ERROR, MNL_CB_OK or MNL_CB_STOP. + * \return propagated value from callback, one of MNL_CB_ERROR, MNL_CB_STOP + * or MNL_CB_OK */ EXPORT_SYMBOL int mnl_attr_parse(const struct nlmsghdr *nlh, unsigned int offset, mnl_attr_cb_t cb, @@ -271,13 +289,13 @@ EXPORT_SYMBOL int mnl_attr_parse(const struct nlmsghdr *nlh, * \param cb callback function that is called for each attribute in the nest * \param data pointer to data passed to the callback function * - * This function allows to iterate over the sequence of attributes that compose - * the Netlink message. You can then put the attribute in an array as it + * This function allows you to iterate over the sequence of attributes that + * compose the Netlink message. You can then put the attribute in an array as it * usually happens at this stage or you can use any other data structure (such * as lists or trees). * - * This function propagates the return value of the callback, which can be - * MNL_CB_ERROR, MNL_CB_OK or MNL_CB_STOP. +* \return propagated value from callback, one of MNL_CB_ERROR, MNL_CB_STOP +* or MNL_CB_OK */ EXPORT_SYMBOL int mnl_attr_parse_nested(const struct nlattr *nested, mnl_attr_cb_t cb, void *data) @@ -307,8 +325,8 @@ EXPORT_SYMBOL int mnl_attr_parse_nested(const struct nlattr *nested, * located at some payload offset. You can then put the attributes in one array * as usual, or you can use any other data structure (such as lists or trees). * - * This function propagates the return value of the callback, which can be - * MNL_CB_ERROR, MNL_CB_OK or MNL_CB_STOP. + * \return propagated value from callback, one of MNL_CB_ERROR, MNL_CB_STOP + * or MNL_CB_OK */ EXPORT_SYMBOL int mnl_attr_parse_payload(const void *payload, size_t payload_len, @@ -324,10 +342,10 @@ EXPORT_SYMBOL int mnl_attr_parse_payload(const void *payload, } /** - * mnl_attr_get_u8 - returns 8-bit unsigned integer attribute payload + * mnl_attr_get_u8 - get 8-bit unsigned integer attribute payload * \param attr pointer to netlink attribute * - * This function returns the 8-bit value of the attribute payload. + * \return 8-bit value of the attribute payload */ EXPORT_SYMBOL uint8_t mnl_attr_get_u8(const struct nlattr *attr) { @@ -335,10 +353,10 @@ EXPORT_SYMBOL uint8_t mnl_attr_get_u8(const struct nlattr *attr) } /** - * mnl_attr_get_u16 - returns 16-bit unsigned integer attribute payload + * mnl_attr_get_u16 - get 16-bit unsigned integer attribute payload * \param attr pointer to netlink attribute * - * This function returns the 16-bit value of the attribute payload. + * \return 16-bit value of the attribute payload */ EXPORT_SYMBOL uint16_t mnl_attr_get_u16(const struct nlattr *attr) { @@ -346,10 +364,10 @@ EXPORT_SYMBOL uint16_t mnl_attr_get_u16(const struct nlattr *attr) } /** - * mnl_attr_get_u32 - returns 32-bit unsigned integer attribute payload + * mnl_attr_get_u32 - get 32-bit unsigned integer attribute payload * \param attr pointer to netlink attribute * - * This function returns the 32-bit value of the attribute payload. + * \return 32-bit value of the attribute payload */ EXPORT_SYMBOL uint32_t mnl_attr_get_u32(const struct nlattr *attr) { @@ -357,12 +375,12 @@ EXPORT_SYMBOL uint32_t mnl_attr_get_u32(const struct nlattr *attr) } /** - * mnl_attr_get_u64 - returns 64-bit unsigned integer attribute. + * mnl_attr_get_u64 - get 64-bit unsigned integer attribute * \param attr pointer to netlink attribute * - * This function returns the 64-bit value of the attribute payload. This - * function is align-safe, since accessing 64-bit Netlink attributes is a - * common source of alignment issues. + * This function reads the 64-bit nlattr payload in an alignment safe manner. + * + * \return 64-bit value of the attribute payload */ EXPORT_SYMBOL uint64_t mnl_attr_get_u64(const struct nlattr *attr) { @@ -372,10 +390,10 @@ EXPORT_SYMBOL uint64_t mnl_attr_get_u64(const struct nlattr *attr) } /** - * mnl_attr_get_str - returns pointer to string attribute. + * mnl_attr_get_str - get pointer to string attribute * \param attr pointer to netlink attribute * - * This function returns the payload of string attribute value. + * \return string pointer of the attribute payload */ EXPORT_SYMBOL const char *mnl_attr_get_str(const struct nlattr *attr) { @@ -508,8 +526,9 @@ EXPORT_SYMBOL void mnl_attr_put_strz(struct nlmsghdr *nlh, uint16_t type, * \param type netlink attribute type * * This function adds the attribute header that identifies the beginning of - * an attribute nest. This function always returns a valid pointer to the - * beginning of the nest. + * an attribute nest. + * + * \return valid pointer to the beginning of the nest */ EXPORT_SYMBOL struct nlattr *mnl_attr_nest_start(struct nlmsghdr *nlh, uint16_t type) @@ -534,8 +553,9 @@ EXPORT_SYMBOL struct nlattr *mnl_attr_nest_start(struct nlmsghdr *nlh, * This function first checks that the data can be added to the message * (fits into the buffer) and then updates the length field of the Netlink * message (nlmsg_len) by adding the size (header + payload) of the new - * attribute. The function returns true if the attribute could be added - * to the message, otherwise false is returned. + * attribute. + * + * \return true if the attribute could be added, false otherwise */ EXPORT_SYMBOL bool mnl_attr_put_check(struct nlmsghdr *nlh, size_t buflen, uint16_t type, size_t len, @@ -557,8 +577,9 @@ EXPORT_SYMBOL bool mnl_attr_put_check(struct nlmsghdr *nlh, size_t buflen, * This function first checks that the data can be added to the message * (fits into the buffer) and then updates the length field of the Netlink * message (nlmsg_len) by adding the size (header + payload) of the new - * attribute. The function returns true if the attribute could be added - * to the message, otherwise false is returned. + * attribute. + * + * \return true if the attribute could be added, false otherwise */ EXPORT_SYMBOL bool mnl_attr_put_u8_check(struct nlmsghdr *nlh, size_t buflen, uint16_t type, uint8_t data) @@ -576,10 +597,9 @@ EXPORT_SYMBOL bool mnl_attr_put_u8_check(struct nlmsghdr *nlh, size_t buflen, * This function first checks that the data can be added to the message * (fits into the buffer) and then updates the length field of the Netlink * message (nlmsg_len) by adding the size (header + payload) of the new - * attribute. The function returns true if the attribute could be added - * to the message, otherwise false is returned. - * This function updates the length field of the Netlink message (nlmsg_len) - * by adding the size (header + payload) of the new attribute. + * attribute. + * + * \return true if the attribute could be added, false otherwise */ EXPORT_SYMBOL bool mnl_attr_put_u16_check(struct nlmsghdr *nlh, size_t buflen, uint16_t type, uint16_t data) @@ -597,10 +617,9 @@ EXPORT_SYMBOL bool mnl_attr_put_u16_check(struct nlmsghdr *nlh, size_t buflen, * This function first checks that the data can be added to the message * (fits into the buffer) and then updates the length field of the Netlink * message (nlmsg_len) by adding the size (header + payload) of the new - * attribute. The function returns true if the attribute could be added - * to the message, otherwise false is returned. - * This function updates the length field of the Netlink message (nlmsg_len) - * by adding the size (header + payload) of the new attribute. + * attribute. + * + * \return true if the attribute could be added, false otherwise */ EXPORT_SYMBOL bool mnl_attr_put_u32_check(struct nlmsghdr *nlh, size_t buflen, uint16_t type, uint32_t data) @@ -618,10 +637,9 @@ EXPORT_SYMBOL bool mnl_attr_put_u32_check(struct nlmsghdr *nlh, size_t buflen, * This function first checks that the data can be added to the message * (fits into the buffer) and then updates the length field of the Netlink * message (nlmsg_len) by adding the size (header + payload) of the new - * attribute. The function returns true if the attribute could be added - * to the message, otherwise false is returned. - * This function updates the length field of the Netlink message (nlmsg_len) - * by adding the size (header + payload) of the new attribute. + * attribute. + * + * \return true if the attribute could be added, false otherwise */ EXPORT_SYMBOL bool mnl_attr_put_u64_check(struct nlmsghdr *nlh, size_t buflen, uint16_t type, uint64_t data) @@ -639,10 +657,9 @@ EXPORT_SYMBOL bool mnl_attr_put_u64_check(struct nlmsghdr *nlh, size_t buflen, * This function first checks that the data can be added to the message * (fits into the buffer) and then updates the length field of the Netlink * message (nlmsg_len) by adding the size (header + payload) of the new - * attribute. The function returns true if the attribute could be added - * to the message, otherwise false is returned. - * This function updates the length field of the Netlink message (nlmsg_len) - * by adding the size (header + payload) of the new attribute. + * attribute. + * + * \return true if the attribute could be added, false otherwise */ EXPORT_SYMBOL bool mnl_attr_put_str_check(struct nlmsghdr *nlh, size_t buflen, uint16_t type, const char *data) @@ -663,8 +680,9 @@ EXPORT_SYMBOL bool mnl_attr_put_str_check(struct nlmsghdr *nlh, size_t buflen, * This function first checks that the data can be added to the message * (fits into the buffer) and then updates the length field of the Netlink * message (nlmsg_len) by adding the size (header + payload) of the new - * attribute. The function returns true if the attribute could be added - * to the message, otherwise false is returned. + * attribute. + * + * \return true if the attribute could be added, false otherwise */ EXPORT_SYMBOL bool mnl_attr_put_strz_check(struct nlmsghdr *nlh, size_t buflen, uint16_t type, const char *data) @@ -679,8 +697,10 @@ EXPORT_SYMBOL bool mnl_attr_put_strz_check(struct nlmsghdr *nlh, size_t buflen, * \param type netlink attribute type * * This function adds the attribute header that identifies the beginning of - * an attribute nest. If the nested attribute cannot be added then NULL, - * otherwise valid pointer to the beginning of the nest is returned. + * an attribute nest. + * + * \return NULL if the attribute cannot be added, otherwise a pointer to the + * beginning of the nest */ EXPORT_SYMBOL struct nlattr *mnl_attr_nest_start_check(struct nlmsghdr *nlh, size_t buflen, diff --git a/src/nlmsg.c b/src/nlmsg.c index d398e63..30a7e63 100644 --- a/src/nlmsg.c +++ b/src/nlmsg.c @@ -12,6 +12,7 @@ #include <ctype.h> #include <errno.h> #include <string.h> +#include <unistd.h> #include <libmnl/libmnl.h> #include "internal.h" @@ -151,9 +152,14 @@ EXPORT_SYMBOL void *mnl_nlmsg_get_payload_offset(const struct nlmsghdr *nlh, */ EXPORT_SYMBOL bool mnl_nlmsg_ok(const struct nlmsghdr *nlh, int len) { - return len >= (int)sizeof(struct nlmsghdr) && + size_t ulen = len; + + if (len < 0) + return false; + + return ulen >= sizeof(struct nlmsghdr) && nlh->nlmsg_len >= sizeof(struct nlmsghdr) && - (int)nlh->nlmsg_len <= len; + nlh->nlmsg_len <= ulen; } /** @@ -244,11 +250,57 @@ static void mnl_nlmsg_fprintf_header(FILE *fd, const struct nlmsghdr *nlh) fprintf(fd, "----------------\t------------------\n"); } +static void mnl_fprintf_attr_color(FILE *fd, const struct nlattr *attr) +{ + fprintf(fd, "|%c[%d;%dm" + "%.5u" + "%c[%dm" + "|" + "%c[%d;%dm" + "%c%c" + "%c[%dm" + "|" + "%c[%d;%dm" + "%.5u" + "%c[%dm|\t", + 27, 1, 31, + attr->nla_len, + 27, 0, + 27, 1, 32, + attr->nla_type & NLA_F_NESTED ? 'N' : '-', + attr->nla_type & NLA_F_NET_BYTEORDER ? 'B' : '-', + 27, 0, + 27, 1, 34, + attr->nla_type & NLA_TYPE_MASK, + 27, 0); +} + +static void mnl_fprintf_attr_raw(FILE *fd, const struct nlattr *attr) +{ + fprintf(fd, "|" + "%.5u" + "|" + "%c%c" + "|" + "%.5u" + "|\t", + attr->nla_len, + attr->nla_type & NLA_F_NESTED ? 'N' : '-', + attr->nla_type & NLA_F_NET_BYTEORDER ? 'B' : '-', + attr->nla_type & NLA_TYPE_MASK); +} + static void mnl_nlmsg_fprintf_payload(FILE *fd, const struct nlmsghdr *nlh, size_t extra_header_size) { - int rem = 0; + int colorize = 0; unsigned int i; + int rem = 0; + int fdnum; + + fdnum = fileno(fd); + if (fdnum != -1) + colorize = isatty(fdnum); for (i=sizeof(struct nlmsghdr); i<nlh->nlmsg_len; i+=4) { char *b = (char *) nlh; @@ -269,28 +321,11 @@ static void mnl_nlmsg_fprintf_payload(FILE *fd, const struct nlmsghdr *nlh, fprintf(fd, "| extra header |\n"); /* this seems like an attribute header. */ } else if (rem == 0 && (attr->nla_type & NLA_TYPE_MASK) != 0) { - fprintf(fd, "|%c[%d;%dm" - "%.5u" - "%c[%dm" - "|" - "%c[%d;%dm" - "%c%c" - "%c[%dm" - "|" - "%c[%d;%dm" - "%.5u" - "%c[%dm|\t", - 27, 1, 31, - attr->nla_len, - 27, 0, - 27, 1, 32, - attr->nla_type & NLA_F_NESTED ? 'N' : '-', - attr->nla_type & - NLA_F_NET_BYTEORDER ? 'B' : '-', - 27, 0, - 27, 1, 34, - attr->nla_type & NLA_TYPE_MASK, - 27, 0); + if (colorize) { + mnl_fprintf_attr_color(fd, attr); + } else { + mnl_fprintf_attr_raw(fd, attr); + } fprintf(fd, "|len |flags| type|\n"); if (!(attr->nla_type & NLA_F_NESTED)) { @@ -381,15 +416,16 @@ EXPORT_SYMBOL void mnl_nlmsg_fprintf(FILE *fd, const void *data, size_t datalen, * datagram. These helpers do not perform strict memory boundary checkings. * * The following figure represents a Netlink message batch: - * - * |<-------------- MNL_SOCKET_BUFFER_SIZE ------------->| - * |<-------------------- batch ------------------>| | - * |-----------|-----------|-----------|-----------|-----------| - * |<- nlmsg ->|<- nlmsg ->|<- nlmsg ->|<- nlmsg ->|<- nlmsg ->| - * |-----------|-----------|-----------|-----------|-----------| - * ^ ^ - * | | - * message N message N+1 + *\verbatim + |<-------------- MNL_SOCKET_BUFFER_SIZE ------------->| + |<-------------------- batch ------------------>| | + |-----------|-----------|-----------|-----------|-----------| + |<- nlmsg ->|<- nlmsg ->|<- nlmsg ->|<- nlmsg ->|<- nlmsg ->| + |-----------|-----------|-----------|-----------|-----------| + ^ ^ + | | + message N message N+1 +\endverbatim * * To start the batch, you have to call mnl_nlmsg_batch_start() and you can * use mnl_nlmsg_batch_stop() to release it. @@ -487,9 +523,9 @@ EXPORT_SYMBOL bool mnl_nlmsg_batch_next(struct mnl_nlmsg_batch *b) * mnl_nlmsg_batch_reset - reset the batch * \param b pointer to batch * - * This function allows to reset a batch, so you can reuse it to create a - * new one. This function moves the last message which does not fit the - * batch to the head of the buffer, if any. + * This function allows you to reset a batch, so you can reuse it to create a + * new one. This function moves the last message which does not fit the batch to + * the head of the buffer, if any. */ EXPORT_SYMBOL void mnl_nlmsg_batch_reset(struct mnl_nlmsg_batch *b) { diff --git a/src/socket.c b/src/socket.c index d7c67a8..85b6bcc 100644 --- a/src/socket.c +++ b/src/socket.c @@ -59,7 +59,7 @@ * * \section scm Git Tree * The current development version of libmnl can be accessed at: - * http://git.netfilter.org/cgi-bin/gitweb.cgi?p=libmnl.git;a=summary + * https://git.netfilter.org/libmnl/ * * \section using Using libmnl * You can access several example files under examples/ in the libmnl source @@ -135,7 +135,7 @@ EXPORT_SYMBOL struct mnl_socket *mnl_socket_open(int bus) * \param bus the netlink socket bus ID (see NETLINK_* constants) * \param flags the netlink socket flags (see SOCK_* constants in socket(2)) * - * This is similar to mnl_socket_open(), but allows to set flags like + * This is similar to mnl_socket_open(), but allows one to set flags like * SOCK_CLOEXEC at socket creation time (useful for multi-threaded programs * performing exec calls). * |