rpcd: iwinfo plugin fixes
[openwrt.git] / package / libs / libnl-tiny / src / include / netlink / handlers.h
1 /*
2  * netlink/handlers.c   default netlink message handlers
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_HANDLERS_H_
13 #define NETLINK_HANDLERS_H_
14
15 #include <stdio.h>
16 #include <stdint.h>
17 #include <sys/socket.h>
18 #include <sys/types.h>
19 #include <netlink/netlink-compat.h>
20 #include <netlink/netlink-kernel.h>
21 #include <netlink/types.h>
22
23 #ifdef __cplusplus
24 extern "C" {
25 #endif
26
27 struct nl_sock;
28 struct nl_msg;
29 struct nl_cb;
30 /**
31  * @name Callback Typedefs
32  * @{
33  */
34
35 /**
36  * nl_recvmsgs() callback for message processing customization
37  * @ingroup cb
38  * @arg msg             netlink message being processed
39  * @arg arg             argument passwd on through caller
40  */
41 typedef int (*nl_recvmsg_msg_cb_t)(struct nl_msg *msg, void *arg);
42
43 /**
44  * nl_recvmsgs() callback for error message processing customization
45  * @ingroup cb
46  * @arg nla             netlink address of the peer
47  * @arg nlerr           netlink error message being processed
48  * @arg arg             argument passed on through caller
49  */
50 typedef int (*nl_recvmsg_err_cb_t)(struct sockaddr_nl *nla,
51                                    struct nlmsgerr *nlerr, void *arg);
52
53 /** @} */
54
55 /**
56  * Callback actions
57  * @ingroup cb
58  */
59 enum nl_cb_action {
60         /** Proceed with wathever would come next */
61         NL_OK,
62         /** Skip this message */
63         NL_SKIP,
64         /** Stop parsing altogether and discard remaining messages */
65         NL_STOP,
66 };
67
68 /**
69  * Callback kinds
70  * @ingroup cb
71  */
72 enum nl_cb_kind {
73         /** Default handlers (quiet) */
74         NL_CB_DEFAULT,
75         /** Verbose default handlers (error messages printed) */
76         NL_CB_VERBOSE,
77         /** Debug handlers for debugging */
78         NL_CB_DEBUG,
79         /** Customized handler specified by the user */
80         NL_CB_CUSTOM,
81         __NL_CB_KIND_MAX,
82 };
83
84 #define NL_CB_KIND_MAX (__NL_CB_KIND_MAX - 1)
85
86 /**
87  * Callback types
88  * @ingroup cb
89  */
90 enum nl_cb_type {
91         /** Message is valid */
92         NL_CB_VALID,
93         /** Last message in a series of multi part messages received */
94         NL_CB_FINISH,
95         /** Report received that data was lost */
96         NL_CB_OVERRUN,
97         /** Message wants to be skipped */
98         NL_CB_SKIPPED,
99         /** Message is an acknowledge */
100         NL_CB_ACK,
101         /** Called for every message received */
102         NL_CB_MSG_IN,
103         /** Called for every message sent out except for nl_sendto() */
104         NL_CB_MSG_OUT,
105         /** Message is malformed and invalid */
106         NL_CB_INVALID,
107         /** Called instead of internal sequence number checking */
108         NL_CB_SEQ_CHECK,
109         /** Sending of an acknowledge message has been requested */
110         NL_CB_SEND_ACK,
111         __NL_CB_TYPE_MAX,
112 };
113
114 #define NL_CB_TYPE_MAX (__NL_CB_TYPE_MAX - 1)
115
116 struct nl_cb
117 {
118         nl_recvmsg_msg_cb_t     cb_set[NL_CB_TYPE_MAX+1];
119         void *                  cb_args[NL_CB_TYPE_MAX+1];
120         
121         nl_recvmsg_err_cb_t     cb_err;
122         void *                  cb_err_arg;
123
124         /** May be used to replace nl_recvmsgs with your own implementation
125          * in all internal calls to nl_recvmsgs. */
126         int                     (*cb_recvmsgs_ow)(struct nl_sock *,
127                                                   struct nl_cb *);
128
129         /** Overwrite internal calls to nl_recv, must return the number of
130          * octets read and allocate a buffer for the received data. */
131         int                     (*cb_recv_ow)(struct nl_sock *,
132                                               struct sockaddr_nl *,
133                                               unsigned char **,
134                                               struct ucred **);
135
136         /** Overwrites internal calls to nl_send, must send the netlink
137          * message. */
138         int                     (*cb_send_ow)(struct nl_sock *,
139                                               struct nl_msg *);
140
141         int                     cb_refcnt;
142 };
143
144
145 extern struct nl_cb *   nl_cb_alloc(enum nl_cb_kind);
146 extern struct nl_cb *   nl_cb_clone(struct nl_cb *);
147 extern void             nl_cb_put(struct nl_cb *);
148
149 extern int  nl_cb_set(struct nl_cb *, enum nl_cb_type, enum nl_cb_kind,
150                       nl_recvmsg_msg_cb_t, void *);
151 extern int  nl_cb_err(struct nl_cb *, enum nl_cb_kind, nl_recvmsg_err_cb_t,
152                       void *);
153
154 static inline struct nl_cb *nl_cb_get(struct nl_cb *cb)
155 {
156         cb->cb_refcnt++;
157
158         return cb;
159 }
160
161 /**
162  * Set up a all callbacks
163  * @arg cb              callback set
164  * @arg kind            kind of callback
165  * @arg func            callback function
166  * @arg arg             argument to be passwd to callback function
167  *
168  * @return 0 on success or a negative error code
169  */
170 static inline int nl_cb_set_all(struct nl_cb *cb, enum nl_cb_kind kind,
171                   nl_recvmsg_msg_cb_t func, void *arg)
172 {
173         int i, err;
174
175         for (i = 0; i <= NL_CB_TYPE_MAX; i++) {
176                 err = nl_cb_set(cb,(enum nl_cb_type)i, kind, func, arg);
177                 if (err < 0)
178                         return err;
179         }
180
181         return 0;
182 }
183
184
185 /**
186  * @name Overwriting
187  * @{
188  */
189
190 /**
191  * Overwrite internal calls to nl_recvmsgs()
192  * @arg cb              callback set
193  * @arg func            replacement callback for nl_recvmsgs()
194  */
195 static inline void nl_cb_overwrite_recvmsgs(struct nl_cb *cb,
196                               int (*func)(struct nl_sock *, struct nl_cb *))
197 {
198         cb->cb_recvmsgs_ow = func;
199 }
200
201 /**
202  * Overwrite internal calls to nl_recv()
203  * @arg cb              callback set
204  * @arg func            replacement callback for nl_recv()
205  */
206 static inline void nl_cb_overwrite_recv(struct nl_cb *cb,
207                           int (*func)(struct nl_sock *, struct sockaddr_nl *,
208                                       unsigned char **, struct ucred **))
209 {
210         cb->cb_recv_ow = func;
211 }
212
213 /**
214  * Overwrite internal calls to nl_send()
215  * @arg cb              callback set
216  * @arg func            replacement callback for nl_send()
217  */
218 static inline void nl_cb_overwrite_send(struct nl_cb *cb,
219                           int (*func)(struct nl_sock *, struct nl_msg *))
220 {
221         cb->cb_send_ow = func;
222 }
223
224 /** @} */
225
226
227 #ifdef __cplusplus
228 }
229 #endif
230
231 #endif