Initial import package libnl: Convenience library for kernel netlink sockets
[external/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 #ifndef SOL_NETLINK
34 #define SOL_NETLINK 270
35 #endif
36
37 #include <linux/types.h>
38
39 /* local header copies */
40 #include <linux/if.h>
41 #include <linux/if_arp.h>
42 #include <linux/if_ether.h>
43 #include <linux/pkt_sched.h>
44 #include <linux/pkt_cls.h>
45 #include <linux/gen_stats.h>
46 #include <linux/ip_mp_alg.h>
47
48 #include <netlink/netlink.h>
49 #include <netlink/handlers.h>
50 #include <netlink/cache.h>
51 #include <netlink/route/tc.h>
52 #include <netlink/object-api.h>
53 #include <netlink/cache-api.h>
54 #include <netlink-types.h>
55
56 struct trans_tbl {
57         int i;
58         const char *a;
59 };
60
61 #define __ADD(id, name) { .i = id, .a = #name },
62
63 struct trans_list {
64         int i;
65         char *a;
66         struct nl_list_head list;
67 };
68
69 #define NL_DEBUG        1
70
71 #define NL_DBG(LVL,FMT,ARG...) \
72         do {    \
73                 if (LVL <= nl_debug) \
74                         fprintf(stderr, "DBG<" #LVL ">: " FMT, ##ARG); \
75         } while (0)
76
77 #define BUG()                            \
78         do {                                 \
79                 fprintf(stderr, "BUG: %s:%d\n",  \
80                         __FILE__, __LINE__);         \
81                 assert(0);      \
82         } while (0)
83
84 #define RET_ERR(R, E)                    \
85     do {                                 \
86                 errno = E;                       \
87                 return -R;                       \
88         } while (0)
89
90 extern int __nl_error(int, const char *, unsigned int,
91         const char *, const char *, ...);
92
93 extern int __nl_read_num_str_file(const char *path,
94                                   int (*cb)(long, const char *));
95
96 #ifdef NL_ERROR_ASSERT
97 #include <assert.h>
98 static inline int __assert_error(const char *file, int line, char *func,
99         const char *fmt, ...)
100 {
101         va_list args;
102         fprintf(stderr, "%s:%d:%s: ", file, line, func);
103         va_start(args, fmt);
104         vfprintf(stderr, fmt, args);
105         va_end(args);
106         fprintf(stderr, "\n");
107         assert(0);
108         return 0;
109 }
110 #define nl_error(E, FMT,ARG...) \
111         __assert_error(__FILE__, __LINE__, __FUNCTION__, FMT, ##ARG)
112
113 #else
114 #define nl_error(E, FMT,ARG...) \
115         __nl_error(E, __FILE__, __LINE__, __FUNCTION__, FMT, ##ARG)
116
117 #endif
118
119 #define nl_errno(E)     nl_error(E, NULL)
120
121 /* backwards compat */
122 #define dp_new_line(params, line)       nl_new_line(params, line)
123 #define dp_dump(params, fmt, arg...)    nl_dump(params, fmt, ##arg)
124
125 static inline int __trans_list_add(int i, const char *a,
126                                    struct nl_list_head *head)
127 {
128         struct trans_list *tl;
129
130         tl = calloc(1, sizeof(*tl));
131         if (!tl)
132                 return nl_errno(ENOMEM);
133
134         tl->i = i;
135         tl->a = strdup(a);
136
137         nl_list_add_tail(&tl->list, head);
138
139         return 0;
140 }
141
142 static inline void __trans_list_clear(struct nl_list_head *head)
143 {
144         struct trans_list *tl, *next;
145
146         nl_list_for_each_entry_safe(tl, next, head, list) {
147                 free(tl->a);
148                 free(tl);
149         }
150 }
151
152 static inline char *__type2str(int type, char *buf, size_t len,
153                                struct trans_tbl *tbl, size_t tbl_len)
154 {
155         int i;
156         for (i = 0; i < tbl_len; i++) {
157                 if (tbl[i].i == type) {
158                         snprintf(buf, len, "%s", tbl[i].a);
159                         return buf;
160                 }
161         }
162
163         snprintf(buf, len, "0x%x", type);
164         return buf;
165 }
166
167 static inline char *__list_type2str(int type, char *buf, size_t len,
168                                     struct nl_list_head *head)
169 {
170         struct trans_list *tl;
171
172         nl_list_for_each_entry(tl, head, list) {
173                 if (tl->i == type) {
174                         snprintf(buf, len, "%s", tl->a);
175                         return buf;
176                 }
177         }
178
179         snprintf(buf, len, "0x%x", type);
180         return buf;
181 }
182
183 static inline char *__flags2str(int flags, char *buf, size_t len,
184                                 struct trans_tbl *tbl, size_t tbl_len)
185 {
186         int i;
187         int tmp = flags;
188
189         memset(buf, 0, len);
190         
191         for (i = 0; i < tbl_len; i++) {
192                 if (tbl[i].i & tmp) {
193                         tmp &= ~tbl[i].i;
194                         strncat(buf, tbl[i].a, len - strlen(buf) - 1);
195                         if ((tmp & flags))
196                                 strncat(buf, ",", len - strlen(buf) - 1);
197                 }
198         }
199
200         return buf;
201 }
202
203 static inline int __str2type(const char *buf, struct trans_tbl *tbl,
204                              size_t tbl_len)
205 {
206         unsigned long l;
207         char *end;
208         int i;
209
210         if (*buf == '\0')
211                 return -1;
212
213         for (i = 0; i < tbl_len; i++)
214                 if (!strcasecmp(tbl[i].a, buf))
215                         return tbl[i].i;
216
217         l = strtoul(buf, &end, 0);
218         if (l == ULONG_MAX || *end != '\0')
219                 return -1;
220
221         return (int) l;
222 }
223
224 static inline int __list_str2type(const char *buf, struct nl_list_head *head)
225 {
226         struct trans_list *tl;
227         unsigned long l;
228         char *end;
229
230         if (*buf == '\0')
231                 return -1;
232
233         nl_list_for_each_entry(tl, head, list) {
234                 if (!strcasecmp(tl->a, buf))
235                         return tl->i;
236         }
237
238         l = strtoul(buf, &end, 0);
239         if (l == ULONG_MAX || *end != '\0')
240                 return -1;
241
242         return (int) l;
243 }
244
245 static inline int __str2flags(const char *buf, struct trans_tbl *tbl,
246                               size_t tbl_len)
247 {
248         int i, flags = 0, len;
249         char *p = (char *) buf, *t;
250
251         for (;;) {
252                 if (*p == ' ')
253                         p++;
254         
255                 t = strchr(p, ',');
256                 len = t ? t - p : strlen(p);
257                 for (i = 0; i < tbl_len; i++)
258                         if (!strncasecmp(tbl[i].a, p, len))
259                                 flags |= tbl[i].i;
260
261                 if (!t)
262                         return flags;
263
264                 p = ++t;
265         }
266
267         return 0;
268 }
269
270 static inline void __dp_dump(struct nl_dump_params *parms, const char *fmt,
271                              va_list args)
272 {
273         if (parms->dp_fd)
274                 vfprintf(parms->dp_fd, fmt, args);
275         else if (parms->dp_buf || parms->dp_cb) {
276                 char *buf = NULL;
277                 vasprintf(&buf, fmt, args);
278                 if (parms->dp_cb)
279                         parms->dp_cb(parms, buf);
280                 else
281                         strncat(parms->dp_buf, buf,
282                                 parms->dp_buflen - strlen(parms->dp_buf) - 1);
283                 free(buf);
284         }
285 }
286
287 static inline void dp_dump_line(struct nl_dump_params *parms, int line,
288                                 const char *fmt, ...)
289 {
290         va_list args;
291
292         nl_new_line(parms, line);
293
294         va_start(args, fmt);
295         __dp_dump(parms, fmt, args);
296         va_end(args);
297 }
298
299 static inline void dump_from_ops(struct nl_object *obj,
300                                  struct nl_dump_params *params)
301 {
302         int type = params->dp_type;
303
304         if (type < 0 || type > NL_DUMP_MAX)
305                 BUG();
306
307         if (params->dp_dump_msgtype) {
308 #if 0
309                 /* XXX */
310                 char buf[64];
311
312                 dp_dump_line(params, 0, "%s ",
313                              nl_cache_mngt_type2name(obj->ce_ops,
314                                                      obj->ce_ops->co_protocol,
315                                                      obj->ce_msgtype,
316                                                      buf, sizeof(buf)));
317 #endif
318                 params->dp_pre_dump = 1;
319         } else
320                 dp_new_line(params, 0);
321
322         if (obj->ce_ops->oo_dump[type])
323                 obj->ce_ops->oo_dump[type](obj, params);
324 }
325
326 static inline struct nl_cache *dp_cache(struct nl_object *obj)
327 {
328         if (obj->ce_cache == NULL)
329                 return nl_cache_mngt_require(obj->ce_ops->oo_name);
330
331         return obj->ce_cache;
332 }
333
334 static inline int nl_cb_call(struct nl_cb *cb, int type, struct nl_msg *msg)
335 {
336         return cb->cb_set[type](msg, cb->cb_args[type]);
337 }
338
339 #define ARRAY_SIZE(X) (sizeof(X) / sizeof((X)[0]))
340 #define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER)
341
342 #define __init __attribute__ ((constructor))
343 #define __exit __attribute__ ((destructor))
344
345 #define P_ACCEPT 0
346 #define P_IGNORE 0
347
348 #define min(x,y) ({ \
349         typeof(x) _x = (x);     \
350         typeof(y) _y = (y);     \
351         (void) (&_x == &_y);            \
352         _x < _y ? _x : _y; })
353
354 #define max(x,y) ({ \
355         typeof(x) _x = (x);     \
356         typeof(y) _y = (y);     \
357         (void) (&_x == &_y);            \
358         _x > _y ? _x : _y; })
359
360 #define min_t(type,x,y) \
361         ({ type __x = (x); type __y = (y); __x < __y ? __x: __y; })
362 #define max_t(type,x,y) \
363         ({ type __x = (x); type __y = (y); __x > __y ? __x: __y; })
364
365 extern int nl_cache_parse(struct nl_cache_ops *, struct sockaddr_nl *,
366                           struct nlmsghdr *, struct nl_parser_param *);
367
368
369 static inline void rtnl_copy_ratespec(struct rtnl_ratespec *dst,
370                                       struct tc_ratespec *src)
371 {
372         dst->rs_cell_log = src->cell_log;
373         dst->rs_feature = src->feature;
374         dst->rs_addend = src->addend;
375         dst->rs_mpu = src->mpu;
376         dst->rs_rate = src->rate;
377 }
378
379 static inline void rtnl_rcopy_ratespec(struct tc_ratespec *dst,
380                                        struct rtnl_ratespec *src)
381 {
382         dst->cell_log = src->rs_cell_log;
383         dst->feature = src->rs_feature;
384         dst->addend = src->rs_addend;
385         dst->mpu = src->rs_mpu;
386         dst->rate = src->rs_rate;
387 }
388
389 static inline char *nl_cache_name(struct nl_cache *cache)
390 {
391         return cache->c_ops ? cache->c_ops->co_name : "unknown";
392 }
393
394 #define GENL_FAMILY(id, name) \
395         { \
396                 { id, NL_ACT_UNSPEC, name }, \
397                 END_OF_MSGTYPES_LIST, \
398         }
399
400 #endif