Tizen 2.1 base
[platform/upstream/libnl2.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-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/stat.h>
27 #include <sys/socket.h>
28 #include <inttypes.h>
29 #include <assert.h>
30 #include <limits.h>
31
32 #include <arpa/inet.h>
33 #include <netdb.h>
34
35 #ifndef SOL_NETLINK
36 #define SOL_NETLINK 270
37 #endif
38
39 #include <linux/types.h>
40
41 /* local header copies */
42 #include <linux/if.h>
43 #include <linux/if_arp.h>
44 #include <linux/if_ether.h>
45 #include <linux/pkt_sched.h>
46 #include <linux/pkt_cls.h>
47 #include <linux/gen_stats.h>
48 #include <linux/ip_mp_alg.h>
49
50 #include <netlink/netlink.h>
51 #include <netlink/handlers.h>
52 #include <netlink/cache.h>
53 #include <netlink/route/tc.h>
54 #include <netlink/object-api.h>
55 #include <netlink/cache-api.h>
56 #include <netlink-types.h>
57
58 struct trans_tbl {
59         int i;
60         const char *a;
61 };
62
63 #define __ADD(id, name) { .i = id, .a = #name },
64
65 struct trans_list {
66         int i;
67         char *a;
68         struct nl_list_head list;
69 };
70
71 #define NL_DEBUG        1
72
73 #define NL_DBG(LVL,FMT,ARG...) \
74         do {    \
75                 if (LVL <= nl_debug) \
76                         fprintf(stderr, "DBG<" #LVL ">: " FMT, ##ARG); \
77         } while (0)
78
79 #define BUG()                            \
80         do {                                 \
81                 fprintf(stderr, "BUG: %s:%d\n",  \
82                         __FILE__, __LINE__);         \
83                 assert(0);      \
84         } while (0)
85
86 extern int __nl_read_num_str_file(const char *path,
87                                   int (*cb)(long, const char *));
88
89 extern int __trans_list_add(int, const char *, struct nl_list_head *);
90 extern void __trans_list_clear(struct nl_list_head *);
91
92 extern char *__type2str(int, char *, size_t, struct trans_tbl *, size_t);
93 extern int __str2type(const char *, struct trans_tbl *, size_t);
94
95 extern char *__list_type2str(int, char *, size_t, struct nl_list_head *);
96 extern int __list_str2type(const char *, struct nl_list_head *);
97
98 extern char *__flags2str(int, char *, size_t, struct trans_tbl *, size_t);
99 extern int __str2flags(const char *, struct trans_tbl *, size_t);
100
101 extern void dump_from_ops(struct nl_object *, struct nl_dump_params *);
102
103 static inline struct nl_cache *dp_cache(struct nl_object *obj)
104 {
105         if (obj->ce_cache == NULL)
106                 return nl_cache_mngt_require(obj->ce_ops->oo_name);
107
108         return obj->ce_cache;
109 }
110
111 static inline int nl_cb_call(struct nl_cb *cb, int type, struct nl_msg *msg)
112 {
113         return cb->cb_set[type](msg, cb->cb_args[type]);
114 }
115
116 #define ARRAY_SIZE(X) (sizeof(X) / sizeof((X)[0]))
117 #define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER)
118
119 #define __init __attribute__ ((constructor))
120 #define __exit __attribute__ ((destructor))
121 #undef __deprecated
122 #define __deprecated __attribute__ ((deprecated))
123
124 #define min(x,y) ({ \
125         typeof(x) _x = (x);     \
126         typeof(y) _y = (y);     \
127         (void) (&_x == &_y);            \
128         _x < _y ? _x : _y; })
129
130 #define max(x,y) ({ \
131         typeof(x) _x = (x);     \
132         typeof(y) _y = (y);     \
133         (void) (&_x == &_y);            \
134         _x > _y ? _x : _y; })
135
136 #define min_t(type,x,y) \
137         ({ type __x = (x); type __y = (y); __x < __y ? __x: __y; })
138 #define max_t(type,x,y) \
139         ({ type __x = (x); type __y = (y); __x > __y ? __x: __y; })
140
141 extern int nl_cache_parse(struct nl_cache_ops *, struct sockaddr_nl *,
142                           struct nlmsghdr *, struct nl_parser_param *);
143
144
145 static inline void rtnl_copy_ratespec(struct rtnl_ratespec *dst,
146                                       struct tc_ratespec *src)
147 {
148         dst->rs_cell_log = src->cell_log;
149         dst->rs_feature = src->feature;
150         dst->rs_addend = src->addend;
151         dst->rs_mpu = src->mpu;
152         dst->rs_rate = src->rate;
153 }
154
155 static inline void rtnl_rcopy_ratespec(struct tc_ratespec *dst,
156                                        struct rtnl_ratespec *src)
157 {
158         dst->cell_log = src->rs_cell_log;
159         dst->feature = src->rs_feature;
160         dst->addend = src->rs_addend;
161         dst->mpu = src->rs_mpu;
162         dst->rate = src->rs_rate;
163 }
164
165 static inline char *nl_cache_name(struct nl_cache *cache)
166 {
167         return cache->c_ops ? cache->c_ops->co_name : "unknown";
168 }
169
170 #define GENL_FAMILY(id, name) \
171         { \
172                 { id, NL_ACT_UNSPEC, name }, \
173                 END_OF_MSGTYPES_LIST, \
174         }
175
176 static inline int wait_for_ack(struct nl_sock *sk)
177 {
178         if (sk->s_flags & NL_NO_AUTO_ACK)
179                 return 0;
180         else
181                 return nl_wait_for_ack(sk);
182 }
183
184 #endif