/build/buildd/libnl-1.0~pre6/include/netlink/attr.h

00001 /*
00002  * netlink/attr.h               Netlink Attributes
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_ATTR_H_
00013 #define NETLINK_ATTR_H_
00014 
00015 #include <netlink/netlink.h>
00016 #include <netlink/object.h>
00017 #include <netlink/addr.h>
00018 #include <netlink/data.h>
00019 
00020 struct nl_msg;
00021 
00022 /**
00023  * @name Validation Policy Types
00024  * @{
00025  */
00026 
00027  /**
00028   * @ingroup attr
00029   * Standard attribute types to specify validation policy
00030   */
00031 enum {
00032         NLA_UNSPEC,     /**< Unspecified type */
00033         NLA_U8,         /**< 8bit integer */
00034         NLA_U16,        /**< 16bit integer */
00035         NLA_U32,        /**< 32bit integer */
00036         NLA_U64,        /**< 64bit integer */
00037         NLA_STRING,     /**< character string */
00038         NLA_FLAG,       /**< flag */
00039         NLA_MSECS,      /**< micro seconds (64bit) */
00040         NLA_NESTED,     /**< nested attributes */
00041         __NLA_TYPE_MAX,
00042 };
00043 
00044 /**
00045  * @ingroup attr
00046  * Maximum netlink validation policy type
00047  */
00048 #define NLA_TYPE_MAX (__NLA_TYPE_MAX - 1)
00049 
00050 /** @} */
00051 
00052 /**
00053  * @ingroup attr
00054  * attribute validation policy
00055  *
00056  * Policies are defined as arrays of this struct, the array must
00057  * be accessible by attribute type up to the highest identifier
00058  * to be expected.
00059  *
00060  * Example:
00061  * @code
00062  * static struct nla_policy my_policy[ATTR_MAX+1] __read_mostly = {
00063  *      [ATTR_FOO] = { .type = NLA_U16 },
00064  *      [ATTR_BAR] = { .type = NLA_STRING },
00065  *      [ATTR_BAZ] = { .minlen = sizeof(struct mystruct) },
00066  * };
00067  * @endcode
00068  */
00069 struct nla_policy {
00070         /** Type of attribute or NLA_UNSPEC */
00071         uint16_t        type;
00072 
00073         /** Minimal length of payload required to be available */
00074         uint16_t        minlen;
00075 
00076         /** Maximal length of payload required to be available */
00077         uint16_t        maxlen;
00078 };
00079 
00080 /* size calculations */
00081 extern int              nla_attr_size(int payload);
00082 extern int              nla_total_size(int payload);
00083 extern int              nla_padlen(int payload);
00084 
00085 /* payload access */
00086 extern void *           nla_data(const struct nlattr *);
00087 extern int              nla_len(const struct nlattr *);
00088 
00089 /* attribute parsing */
00090 extern int              nla_ok(const struct nlattr *, int);
00091 extern struct nlattr *  nla_next(const struct nlattr *, int *);
00092 extern int              nla_parse(struct nlattr **, int, struct nlattr *,
00093                                   int, struct nla_policy *);
00094 extern int              nla_parse_nested(struct nlattr **, int, struct nlattr *,
00095                                          struct nla_policy *);
00096 extern int              nla_validate(struct nlattr *, int, int,
00097                                      struct nla_policy *);
00098 extern struct nlattr *  nla_find(struct nlattr *, int, int);
00099 
00100 /* utilities */
00101 extern int              nla_memcpy(void *, struct nlattr *, int);
00102 extern size_t           nla_strlcpy(char *, const struct nlattr *, size_t);
00103 extern int              nla_memcmp(const struct nlattr *, const void *, size_t);
00104 extern int              nla_strcmp(const struct nlattr *, const char *);
00105 
00106 /* attribute construction */
00107 extern struct nlattr *  nla_reserve(struct nl_msg *, int, int);
00108 extern int              nla_put(struct nl_msg *, int, int, const void *);
00109 extern int              nla_put_nested(struct nl_msg *, int, struct nl_msg *);
00110 extern int              nla_put_u8(struct nl_msg *, int, uint8_t);
00111 extern int              nla_put_u16(struct nl_msg *, int, uint16_t);
00112 extern int              nla_put_u32(struct nl_msg *, int, uint32_t);
00113 extern int              nla_put_u64(struct nl_msg *, int, uint64_t);
00114 extern int              nla_put_string(struct nl_msg *, int, const char *);
00115 extern int              nla_put_flag(struct nl_msg *, int);
00116 extern int              nla_put_msecs(struct nl_msg *, int, unsigned long);
00117 extern int              nla_put_data(struct nl_msg *, int, struct nl_data *);
00118 extern int              nla_put_addr(struct nl_msg *, int, struct nl_addr *);
00119 
00120 /* attribute nesting */
00121 extern struct nlattr *  nla_nest_start(struct nl_msg *, int);
00122 extern int              nla_nest_end(struct nl_msg *, struct nlattr *);
00123 
00124 /* attribute reading */
00125 extern uint8_t          nla_get_u8(struct nlattr *);
00126 extern uint16_t         nla_get_u16(struct nlattr *);
00127 extern uint32_t         nla_get_u32(struct nlattr *);
00128 extern uint64_t         nla_get_u64(struct nlattr *);
00129 extern int              nla_get_flag(struct nlattr *);
00130 extern unsigned long    nla_get_msecs(struct nlattr *);
00131 extern struct nl_data * nla_get_data(struct nlattr *);
00132 extern struct nl_addr * nla_get_addr(struct nlattr *, int);
00133 
00134 /**
00135  * @name Attribute Construction (Exception Based)
00136  *
00137  * All these functions jump to nla_put_failure in case of a failure
00138  * instead of returning an error code.
00139  * 
00140  * @{
00141  */
00142 
00143 /**
00144  * @ingroup attr
00145  * Add a netlink attribute to a netlink message
00146  * @arg n               netlink message
00147  * @arg attrtype        attribute type
00148  * @arg attrlen         length of attribute payload
00149  * @arg data            head of attribute payload
00150  */
00151 #define NLA_PUT(n, attrtype, attrlen, data) \
00152         do { \
00153                 if (nla_put(n, attrtype, attrlen, data) < 0) \
00154                         goto nla_put_failure; \
00155         } while(0)
00156 
00157 /**
00158  * @ingroup attr
00159  * Add a basic netlink attribute to a netlink message
00160  * @arg n               netlink message
00161  * @arg type            atomic type
00162  * @arg attrtype        attribute type
00163  * @arg value           head of attribute payload
00164  */
00165 #define NLA_PUT_TYPE(n, type, attrtype, value) \
00166         do { \
00167                 type __tmp = value; \
00168                 NLA_PUT(n, attrtype, sizeof(type), &__tmp); \
00169         } while(0)
00170 
00171 /**
00172  * Add a u8 netlink attribute to a netlink message
00173  * @arg n               netlink message
00174  * @arg attrtype        attribute type
00175  * @arg value           numeric value
00176  */
00177 #define NLA_PUT_U8(n, attrtype, value) \
00178         NLA_PUT_TYPE(n, uint8_t, attrtype, value)
00179 
00180 /**
00181  * Add a u16 netlink attribute to a netlink message
00182  * @arg n               netlink message
00183  * @arg attrtype        attribute type
00184  * @arg value           numeric value
00185  */
00186 #define NLA_PUT_U16(n, attrtype, value) \
00187         NLA_PUT_TYPE(n, uint16_t, attrtype, value)
00188 
00189 /**
00190  * Add a u32 netlink attribute to a netlink message
00191  * @arg n               netlink message
00192  * @arg attrtype        attribute type
00193  * @arg value           numeric value
00194  */
00195 #define NLA_PUT_U32(n, attrtype, value) \
00196         NLA_PUT_TYPE(n, uint32_t, attrtype, value)
00197 
00198 /**
00199  * Add a u64 netlink attribute to a netlink message
00200  * @arg n               netlink message
00201  * @arg attrtype        attribute type
00202  * @arg value           numeric value
00203  */
00204 #define NLA_PUT_U64(n, attrtype, value) \
00205         NLA_PUT_TYPE(n, uint64_t, attrtype, value)
00206 
00207 /**
00208  * Add a character string netlink attribute to a netlink message
00209  * @arg n               netlink message
00210  * @arg attrtype        attribute type
00211  * @arg value           character string
00212  */
00213 #define NLA_PUT_STRING(n, attrtype, value) \
00214         NLA_PUT(n, attrtype, strlen(value) + 1, value)
00215 
00216 /**
00217  * Add a flag netlink attribute to a netlink message
00218  * @arg n               netlink message
00219  * @arg attrtype        attribute type
00220  */
00221 #define NLA_PUT_FLAG(n, attrtype) \
00222         NLA_PUT(n, attrtype, 0, NULL)
00223 
00224 /**
00225  * Add a msecs netlink attribute to a netlink message
00226  * @arg n               netlink message
00227  * @arg attrtype        attribute type
00228  * @arg msecs           numeric value in micro seconds
00229  */
00230 #define NLA_PUT_MSECS(n, attrtype, msecs) \
00231         NLA_PUT_U64(n, attrtype, msecs)
00232 
00233 /**
00234  * Add a address attribute to a netlink message
00235  * @arg n               netlink message
00236  * @arg attrtype        attribute type
00237  * @arg addr            abstract address object
00238  */
00239 #define NLA_PUT_ADDR(n, attrtype, addr) \
00240         NLA_PUT(n, attrtype, nl_addr_get_len(addr), \
00241                 nl_addr_get_binary_addr(addr))
00242 
00243 /** @} */
00244 
00245 /**
00246  * @name Iterators
00247  * @{
00248  */
00249 
00250 /**
00251  * @ingroup attr
00252  * iterate over a stream of attributes
00253  * @arg pos     loop counter, set to current attribute
00254  * @arg head    head of attribute stream
00255  * @arg len     length of attribute stream
00256  * @arg rem     initialized to len, holds bytes currently remaining in stream
00257  */
00258 #define nla_for_each_attr(pos, head, len, rem) \
00259         for (pos = head, rem = len; \
00260              nla_ok(pos, rem); \
00261              pos = nla_next(pos, &(rem)))
00262 
00263 /** @} */
00264 
00265 #endif

Generated on Fri Apr 27 14:14:07 2007 for libnl by  doxygen 1.5.1