libnl  3.2.3
/build/buildd/libnl3-3.2.3/include/netlink/msg.h
00001 /*
00002  * netlink/msg.c                Netlink Messages Interface
00003  *
00004  *      This library is free software; you can redistribute it and/or
00005  *      modify it under the terms of the GNU Lesser General Public
00006  *      License as published by the Free Software Foundation version 2.1
00007  *      of the License.
00008  *
00009  * Copyright (c) 2003-2006 Thomas Graf <tgraf@suug.ch>
00010  */
00011 
00012 #ifndef NETLINK_MSG_H_
00013 #define NETLINK_MSG_H_
00014 
00015 #include <netlink/netlink.h>
00016 #include <netlink/object.h>
00017 #include <netlink/attr.h>
00018 
00019 #ifdef __cplusplus
00020 extern "C" {
00021 #endif
00022 
00023 #define NL_DONTPAD      0
00024 
00025 /**
00026  * @ingroup msg
00027  * @brief
00028  * Will cause the netlink port to be set to the port assigned to
00029  * the netlink icoket ust before sending the message off.
00030  *
00031  * @note Requires the use of nl_send_auto()!
00032  */
00033 #define NL_AUTO_PORT    0
00034 #define NL_AUTO_PID     NL_AUTO_PORT
00035 
00036 /**
00037  * @ingroup msg
00038  * @brief
00039  * May be used to refer to a sequence number which should be
00040  * automatically set just before sending the message off.
00041  *
00042  * @note Requires the use of nl_send_auto()!
00043  */
00044 #define NL_AUTO_SEQ     0
00045 
00046 struct nl_msg;
00047 struct nl_tree;
00048 struct ucred;
00049 
00050 extern int                      nlmsg_size(int);
00051 extern int                      nlmsg_total_size(int);
00052 extern int                      nlmsg_padlen(int);
00053 
00054 extern void *                   nlmsg_data(const struct nlmsghdr *);
00055 extern int                      nlmsg_datalen(const struct nlmsghdr *);
00056 extern void *                   nlmsg_tail(const struct nlmsghdr *);
00057 
00058 /* attribute access */
00059 extern struct nlattr *    nlmsg_attrdata(const struct nlmsghdr *, int);
00060 extern int                nlmsg_attrlen(const struct nlmsghdr *, int);
00061 
00062 /* message parsing */
00063 extern int                nlmsg_valid_hdr(const struct nlmsghdr *, int);
00064 extern int                nlmsg_ok(const struct nlmsghdr *, int);
00065 extern struct nlmsghdr *  nlmsg_next(struct nlmsghdr *, int *);
00066 extern int                nlmsg_parse(struct nlmsghdr *, int, struct nlattr **,
00067                                       int, struct nla_policy *);
00068 extern struct nlattr *    nlmsg_find_attr(struct nlmsghdr *, int, int);
00069 extern int                nlmsg_validate(struct nlmsghdr *, int, int,
00070                                          struct nla_policy *);
00071 
00072 extern struct nl_msg *    nlmsg_alloc(void);
00073 extern struct nl_msg *    nlmsg_alloc_size(size_t);
00074 extern struct nl_msg *    nlmsg_alloc_simple(int, int);
00075 extern void               nlmsg_set_default_size(size_t);
00076 extern struct nl_msg *    nlmsg_inherit(struct nlmsghdr *);
00077 extern struct nl_msg *    nlmsg_convert(struct nlmsghdr *);
00078 extern void *             nlmsg_reserve(struct nl_msg *, size_t, int);
00079 extern int                nlmsg_append(struct nl_msg *, void *, size_t, int);
00080 extern int                nlmsg_expand(struct nl_msg *, size_t);
00081 
00082 extern struct nlmsghdr *  nlmsg_put(struct nl_msg *, uint32_t, uint32_t,
00083                                     int, int, int);
00084 extern struct nlmsghdr *  nlmsg_hdr(struct nl_msg *);
00085 extern void               nlmsg_get(struct nl_msg *);
00086 extern void               nlmsg_free(struct nl_msg *);
00087 
00088 /* attribute modification */
00089 extern void               nlmsg_set_proto(struct nl_msg *, int);
00090 extern int                nlmsg_get_proto(struct nl_msg *);
00091 extern size_t             nlmsg_get_max_size(struct nl_msg *);
00092 extern void               nlmsg_set_src(struct nl_msg *, struct sockaddr_nl *);
00093 extern struct sockaddr_nl *nlmsg_get_src(struct nl_msg *);
00094 extern void               nlmsg_set_dst(struct nl_msg *, struct sockaddr_nl *);
00095 extern struct sockaddr_nl *nlmsg_get_dst(struct nl_msg *);
00096 extern void               nlmsg_set_creds(struct nl_msg *, struct ucred *);
00097 extern struct ucred *     nlmsg_get_creds(struct nl_msg *);
00098 
00099 extern char *             nl_nlmsgtype2str(int, char *, size_t);
00100 extern int                nl_str2nlmsgtype(const char *);
00101 
00102 extern char *             nl_nlmsg_flags2str(int, char *, size_t);
00103 
00104 extern int                nl_msg_parse(struct nl_msg *,
00105                                        void (*cb)(struct nl_object *, void *),
00106                                        void *);
00107 
00108 extern void             nl_msg_dump(struct nl_msg *, FILE *);
00109 
00110 /**
00111  * @name Iterators
00112  * @{
00113  */
00114 
00115 /**
00116  * @ingroup msg
00117  * Iterate over a stream of attributes in a message
00118  * @arg pos     loop counter, set to current attribute
00119  * @arg nlh     netlink message header
00120  * @arg hdrlen  length of family header
00121  * @arg rem     initialized to len, holds bytes currently remaining in stream
00122  */
00123 #define nlmsg_for_each_attr(pos, nlh, hdrlen, rem) \
00124         nla_for_each_attr(pos, nlmsg_attrdata(nlh, hdrlen), \
00125                           nlmsg_attrlen(nlh, hdrlen), rem)
00126 
00127 /**
00128  * Iterate over a stream of messages
00129  * @arg pos     loop counter, set to current message
00130  * @arg head    head of message stream
00131  * @arg len     length of message stream
00132  */
00133 #define nlmsg_for_each(pos, head, len) \
00134         for (int rem = len, pos = head; \
00135                 nlmsg_ok(pos, rem); \
00136                 pos = nlmsg_next(pos, &rem))
00137 
00138 #define nlmsg_for_each_msg(pos, head, len, rem) \
00139                 nlmsg_for_each(pos, head, len)
00140 
00141 /** @} */
00142 
00143 #ifdef __cplusplus
00144 }
00145 #endif
00146 
00147 #endif