This commit was generated by cvs2svn to compensate for changes in r786,
[libnl.git] / include / netlink-local.h
1 /*
2  * netlink-local.h              Local Netlink Interface
3  *
4  *      This library is free software; you can redistribute it and/or
5  *      modify it under the terms of the GNU Lesser General Public
6  *      License as published by the Free Software Foundation version 2.1
7  *      of the License.
8  *
9  * Copyright (c) 2003-2006 Thomas Graf <tgraf@suug.ch>
10  */
11
12 #ifndef NETLINK_LOCAL_H_
13 #define NETLINK_LOCAL_H_
14
15 #include <stdio.h>
16 #include <errno.h>
17 #include <stdlib.h>
18 #include <string.h>
19 #include <unistd.h>
20 #include <fcntl.h>
21 #include <math.h>
22 #include <time.h>
23 #include <stdarg.h>
24 #include <ctype.h>
25 #include <sys/types.h>
26 #include <sys/socket.h>
27 #include <inttypes.h>
28 #include <assert.h>
29
30 #include <arpa/inet.h>
31 #include <netdb.h>
32
33 #include <asm/types.h>
34
35 /* local header copies */
36 #include <linux/if.h>
37 #include <linux/if_arp.h>
38 #include <linux/if_ether.h>
39 #include <linux/pkt_sched.h>
40 #include <linux/pkt_cls.h>
41 #include <linux/gen_stats.h>
42
43 #include <netlink/netlink-compat.h>
44 #include <netlink/netlink-kernel.h>
45 #include <netlink/rtnetlink-kernel.h>
46
47 #include <netlink/types.h>
48 #include <netlink-types.h>
49
50 #include <netlink/handlers.h>
51 #include <netlink/cache.h>
52 #include <netlink/route/tc.h>
53
54 struct trans_tbl {
55         int i;
56         const char *a;
57 };
58
59 #define __ADD(id, name) { .i = id, .a = #name },
60
61 #define NL_DBG(LVL,FMT,ARG...) \
62         do {    \
63                 if (LVL <= nl_debug) \
64                         fprintf(stderr, "DBG<" #LVL ">: " FMT, ##ARG); \
65         } while (0)
66
67 #define BUG()                            \
68         do {                                 \
69                 fprintf(stderr, "BUG: %s:%d\n",  \
70                         __FILE__, __LINE__);         \
71                 assert(0);      \
72         } while (0)
73
74 #define RET_ERR(R, E)                    \
75     do {                                 \
76                 errno = E;                       \
77                 return -R;                       \
78         } while (0)
79
80 extern int __nl_error(int, const char *, unsigned int,
81         const char *, const char *, ...);
82
83 #ifdef NL_ERROR_ASSERT
84 #include <assert.h>
85 static inline int __assert_error(const char *file, int line, char *func,
86         const char *fmt, ...)
87 {
88         va_list args;
89         fprintf(stderr, "%s:%d:%s: ", file, line, func);
90         va_start(args, fmt);
91         vfprintf(stderr, fmt, args);
92         va_end(args);
93         fprintf(stderr, "\n");
94         assert(0);
95         return 0;
96 }
97 #define nl_error(E, FMT,ARG...) \
98         __assert_error(__FILE__, __LINE__, __FUNCTION__, FMT, ##ARG)
99
100 #else
101 #define nl_error(E, FMT,ARG...) \
102         __nl_error(E, __FILE__, __LINE__, __FUNCTION__, FMT, ##ARG);
103
104 #endif
105
106 #define nl_errno(E)     nl_error(E, NULL)
107
108
109 static inline char *__type2str(int type, char *buf, size_t len,
110                                struct trans_tbl *tbl, size_t tbl_len)
111 {
112         int i;
113         for (i = 0; i < tbl_len; i++) {
114                 if (tbl[i].i == type) {
115                         snprintf(buf, len, "%s", tbl[i].a);
116                         return buf;
117                 }
118         }
119
120         snprintf(buf, len, "0x%x", type);
121         return buf;
122 }
123
124 static inline char *__flags2str(int flags, char *buf, size_t len,
125                                 struct trans_tbl *tbl, size_t tbl_len)
126 {
127         int i;
128         int tmp = flags;
129
130         memset(buf, 0, len);
131         
132         for (i = 0; i < tbl_len; i++) {
133                 if (tbl[i].i & tmp) {
134                         tmp &= ~tbl[i].i;
135                         strncat(buf, tbl[i].a, len - strlen(buf) - 1);
136                         if ((tmp & flags))
137                                 strncat(buf, ",", len - strlen(buf) - 1);
138                 }
139         }
140
141         return buf;
142 }
143
144 static inline int __str2type(const char *buf, struct trans_tbl *tbl,
145                              size_t tbl_len)
146 {
147         unsigned long l;
148         char *end;
149         int i;
150
151         if (*buf == '\0')
152                 return -1;
153
154         for (i = 0; i < tbl_len; i++)
155                 if (!strcasecmp(tbl[i].a, buf))
156                         return tbl[i].i;
157
158         l = strtoul(buf, &end, 0);
159         if (l == ULONG_MAX || *end != '\0')
160                 return -1;
161
162         return (int) l;
163 }
164
165 static inline int __str2flags(const char *buf, struct trans_tbl *tbl,
166                               size_t tbl_len)
167 {
168         int i, flags = 0, len;
169         char *p = (char *) buf, *t;
170
171         for (;;) {
172                 if (*p == ' ')
173                         p++;
174         
175                 t = strchr(p, ',');
176                 len = t ? t - p : strlen(p);
177                 for (i = 0; i < tbl_len; i++)
178                         if (!strncasecmp(tbl[i].a, p, len))
179                                 flags |= tbl[i].i;
180
181                 if (!t)
182                         return flags;
183
184                 p = ++t;
185         }
186
187         return 0;
188 }
189
190
191 static inline void dp_new_line(struct nl_dump_params *params,
192                                int line_nr)
193 {
194         if (params->dp_prefix) {
195                 int i;
196                 for (i = 0; i < params->dp_prefix; i++) {
197                         if (params->dp_fd)
198                                 fprintf(params->dp_fd, " ");
199                         else if (params->dp_buf)
200                                 strncat(params->dp_buf, " ",
201                                         params->dp_buflen -
202                                         sizeof(params->dp_buf) - 1);
203                 }
204         }
205
206         if (params->dp_nl_cb)
207                 params->dp_nl_cb(params, line_nr);
208 }
209
210 static inline void __dp_dump(struct nl_dump_params *parms, const char *fmt,
211                              va_list args)
212 {
213         if (parms->dp_fd)
214                 vfprintf(parms->dp_fd, fmt, args);
215         else if (parms->dp_buf || parms->dp_cb) {
216                 char *buf = NULL;
217                 vasprintf(&buf, fmt, args);
218                 if (parms->dp_cb)
219                         parms->dp_cb(parms, buf);
220                 else
221                         strncat(parms->dp_buf, buf,
222                                 parms->dp_buflen - strlen(parms->dp_buf) - 1);
223                 free(buf);
224         }
225 }
226
227 static inline void dp_dump(struct nl_dump_params *parms, const char *fmt, ...)
228 {
229         va_list args;
230
231         va_start(args, fmt);
232         __dp_dump(parms, fmt, args);
233         va_end(args);
234 }
235
236 static inline void dp_dump_line(struct nl_dump_params *parms, int line,
237                                 const char *fmt, ...)
238 {
239         va_list args;
240
241         dp_new_line(parms, line);
242
243         va_start(args, fmt);
244         __dp_dump(parms, fmt, args);
245         va_end(args);
246 }
247
248 static inline void dump_from_ops(struct nl_object *obj,
249                                  struct nl_dump_params *params)
250 {
251         int type = params->dp_type;
252         char buf[64];
253
254         if (type < 0 || type > NL_DUMP_MAX)
255                 BUG();
256
257         if (params->dp_dump_msgtype) {
258                 dp_dump_line(params, 0, "%s ",
259                              nl_cache_mngt_type2name(obj->ce_ops,
260                                                      obj->ce_msgtype,
261                                                      buf, sizeof(buf)));
262                 params->dp_pre_dump = 1;
263         } else
264                 dp_new_line(params, 0);
265
266         if (obj->ce_ops->co_dump[type])
267                 obj->ce_ops->co_dump[type](obj, params);
268 }
269
270 static inline struct nl_cache *dp_cache(struct nl_object *obj)
271 {
272         if (obj->ce_cache == NULL)
273                 return nl_cache_mngt_require(obj->ce_ops->co_name);
274
275         return obj->ce_cache;
276 }
277
278 static inline int nl_cb_call(struct nl_cb *cb, int type, struct nl_msg *msg)
279 {
280         return cb->cb_set[type](msg, cb->cb_args[type]);
281 }
282
283 #define ARRAY_SIZE(X) (sizeof(X) / sizeof((X)[0]))
284 #define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER)
285
286 #define __init __attribute__ ((constructor))
287 #define __exit __attribute__ ((destructor))
288
289 #define P_ACCEPT 0
290 #define P_IGNORE 0
291
292 #define NL_COPY_TEMPLATE(TB, PREFIX, BASE, FLAGBASE, IDX, VAR) \
293 ({      if (TB[PREFIX ##IDX]) { \
294                 err = NL_COPY_DATA(BASE ##VAR, TB[PREFIX ##IDX]); \
295                 if (err < 0) \
296                         return err; \
297                 BASE ##mask |= FLAGBASE ##IDX; \
298         } })
299
300 #define min(x,y) ({ \
301         typeof(x) _x = (x);     \
302         typeof(y) _y = (y);     \
303         (void) (&_x == &_y);            \
304         _x < _y ? _x : _y; })
305
306 #define max(x,y) ({ \
307         typeof(x) _x = (x);     \
308         typeof(y) _y = (y);     \
309         (void) (&_x == &_y);            \
310         _x > _y ? _x : _y; })
311
312 #define min_t(type,x,y) \
313         ({ type __x = (x); type __y = (y); __x < __y ? __x: __y; })
314 #define max_t(type,x,y) \
315         ({ type __x = (x); type __y = (y); __x > __y ? __x: __y; })
316
317 extern int nl_cache_parse(struct nl_cache_ops *, struct sockaddr_nl *,
318                           struct nlmsghdr *, struct nl_parser_param *);
319
320
321 static inline void rtnl_copy_ratespec(struct rtnl_ratespec *dst,
322                                       struct tc_ratespec *src)
323 {
324         dst->rs_cell_log = src->cell_log;
325         dst->rs_feature = src->feature;
326         dst->rs_addend = src->addend;
327         dst->rs_mpu = src->mpu;
328         dst->rs_rate = src->rate;
329 }
330
331 static inline void rtnl_rcopy_ratespec(struct tc_ratespec *dst,
332                                        struct rtnl_ratespec *src)
333 {
334         dst->cell_log = src->rs_cell_log;
335         dst->feature = src->rs_feature;
336         dst->addend = src->rs_addend;
337         dst->mpu = src->rs_mpu;
338         dst->rate = src->rs_rate;
339 }
340
341 #endif