add libnl-tiny as a small replacement for libnl with only genl support included
[openwrt.git] / package / libnl-tiny / src / 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-2008 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 #include <limits.h>
30
31 #include <arpa/inet.h>
32 #include <netdb.h>
33
34 #ifndef SOL_NETLINK
35 #define SOL_NETLINK 270
36 #endif
37
38 #include <linux/types.h>
39
40 /* local header copies */
41 #include <linux/if.h>
42 #include <linux/if_arp.h>
43 #include <linux/if_ether.h>
44 #include <linux/pkt_sched.h>
45 #include <linux/pkt_cls.h>
46 #include <linux/gen_stats.h>
47
48 #include <netlink/netlink.h>
49 #include <netlink/handlers.h>
50 #include <netlink/cache.h>
51 #include <netlink/object-api.h>
52 #include <netlink/cache-api.h>
53 #include <netlink-types.h>
54
55 struct trans_tbl {
56         int i;
57         const char *a;
58 };
59
60 #define __ADD(id, name) { .i = id, .a = #name },
61
62 struct trans_list {
63         int i;
64         char *a;
65         struct nl_list_head list;
66 };
67
68 #define NL_DEBUG        1
69
70 #define NL_DBG(LVL,FMT,ARG...) \
71         do {} while (0)
72
73 #define BUG()                            \
74         do {                                 \
75                 fprintf(stderr, "BUG: %s:%d\n",  \
76                         __FILE__, __LINE__);         \
77                 assert(0);      \
78         } while (0)
79
80 extern int __nl_read_num_str_file(const char *path,
81                                   int (*cb)(long, const char *));
82
83 extern int __trans_list_add(int, const char *, struct nl_list_head *);
84 extern void __trans_list_clear(struct nl_list_head *);
85
86 extern char *__type2str(int, char *, size_t, struct trans_tbl *, size_t);
87 extern int __str2type(const char *, struct trans_tbl *, size_t);
88
89 extern char *__list_type2str(int, char *, size_t, struct nl_list_head *);
90 extern int __list_str2type(const char *, struct nl_list_head *);
91
92 extern char *__flags2str(int, char *, size_t, struct trans_tbl *, size_t);
93 extern int __str2flags(const char *, struct trans_tbl *, size_t);
94
95 extern void dump_from_ops(struct nl_object *, struct nl_dump_params *);
96
97 static inline struct nl_cache *dp_cache(struct nl_object *obj)
98 {
99         if (obj->ce_cache == NULL)
100                 return nl_cache_mngt_require(obj->ce_ops->oo_name);
101
102         return obj->ce_cache;
103 }
104
105 static inline int nl_cb_call(struct nl_cb *cb, int type, struct nl_msg *msg)
106 {
107         return cb->cb_set[type](msg, cb->cb_args[type]);
108 }
109
110 #define ARRAY_SIZE(X) (sizeof(X) / sizeof((X)[0]))
111 #define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER)
112
113 #define __init __attribute__ ((constructor))
114 #define __exit __attribute__ ((destructor))
115 #undef __deprecated
116 #define __deprecated __attribute__ ((deprecated))
117
118 #define min(x,y) ({ \
119         typeof(x) _x = (x);     \
120         typeof(y) _y = (y);     \
121         (void) (&_x == &_y);            \
122         _x < _y ? _x : _y; })
123
124 #define max(x,y) ({ \
125         typeof(x) _x = (x);     \
126         typeof(y) _y = (y);     \
127         (void) (&_x == &_y);            \
128         _x > _y ? _x : _y; })
129
130 extern int nl_cache_parse(struct nl_cache_ops *, struct sockaddr_nl *,
131                           struct nlmsghdr *, struct nl_parser_param *);
132
133
134 static inline char *nl_cache_name(struct nl_cache *cache)
135 {
136         return cache->c_ops ? cache->c_ops->co_name : "unknown";
137 }
138
139 #define GENL_FAMILY(id, name) \
140         { \
141                 { id, NL_ACT_UNSPEC, name }, \
142                 END_OF_MSGTYPES_LIST, \
143         }
144
145 static inline int wait_for_ack(struct nl_sock *sk)
146 {
147         if (sk->s_flags & NL_NO_AUTO_ACK)
148                 return 0;
149         else
150                 return nl_wait_for_ack(sk);
151 }
152
153 #endif