1 /* Portions of this file are subject to the following copyright(s).  See
2  * the Net-SNMP's COPYING file for more details and other copyrights
3  * that may apply:
4  */
5 /*
6  * Portions of this file are copyrighted by:
7  * Copyright � 2003 Sun Microsystems, Inc. All rights reserved.
8  * Use is subject to license terms specified in the COPYING file
9  * distributed with the Net-SNMP package.
10  */
11 #ifndef AGENT_HANDLER_H
12 #define AGENT_HANDLER_H
13 
14 #ifdef __cplusplus
15 extern          "C" {
16 #endif
17 
18 /** @file agent_handler.h
19  *
20  *  @addtogroup handler
21  *
22  * @{
23  */
24 
25 struct netsnmp_handler_registration_s;
26 
27 /*
28  * per mib handler flags.
29  * NOTE: Lower bits are reserved for the agent handler's use.
30  *       The high 4 bits (31-28) are reserved for use by the handler.
31  */
32 #define MIB_HANDLER_AUTO_NEXT                   0x00000001
33 #define MIB_HANDLER_AUTO_NEXT_OVERRIDE_ONCE     0x00000002
34 #define MIB_HANDLER_INSTANCE                    0x00000004
35 
36 #define MIB_HANDLER_CUSTOM4                     0x10000000
37 #define MIB_HANDLER_CUSTOM3                     0x20000000
38 #define MIB_HANDLER_CUSTOM2                     0x40000000
39 #define MIB_HANDLER_CUSTOM1                     0x80000000
40 
41 
42 /** @typedef struct netsnmp_mib_handler_s netsnmp_mib_handler
43  * Typedefs the netsnmp_mib_handler_s struct into  netsnmp_mib_handler */
44 
45 /** @struct netsnmp_mib_handler_s
46  *  the mib handler structure to be registered
47  */
48 typedef struct netsnmp_mib_handler_s {
49         char           *handler_name;
50 	/** for handler's internal use */
51         void           *myvoid;
52         /** for agent_handler's internal use */
53         int             flags;
54 
55         /** if you add more members, you probably also want to update */
56         /** _clone_handler in agent_handler.c. */
57 
58         int             (*access_method) (struct netsnmp_mib_handler_s *,
59                                           struct
60                                           netsnmp_handler_registration_s *,
61                                           struct
62                                           netsnmp_agent_request_info_s *,
63                                           struct netsnmp_request_info_s *);
64         /** data clone hook for myvoid
65          *  deep copy the myvoid member - default is to copy the pointer
66          *  This method is only called if myvoid != NULL
67          *  myvoid is the current myvoid pointer.
68          *  returns NULL on failure
69          */
70         void *(*data_clone)(void *myvoid);
71         /** data free hook for myvoid
72          *  delete the myvoid member - default is to do nothing
73          *  This method is only called if myvoid != NULL
74          */
75         void (*data_free)(void *myvoid); /**< data free hook for myvoid */
76 
77         struct netsnmp_mib_handler_s *next;
78         struct netsnmp_mib_handler_s *prev;
79 } netsnmp_mib_handler;
80 
81 /*
82  * per registration flags
83  */
84 #define HANDLER_CAN_GETANDGETNEXT     0x01       /* must be able to do both */
85 #define HANDLER_CAN_SET               0x02           /* implies create, too */
86 #define HANDLER_CAN_GETBULK           0x04
87 #define HANDLER_CAN_NOT_CREATE        0x08         /* auto set if ! CAN_SET */
88 #define HANDLER_CAN_BABY_STEP         0x10
89 #define HANDLER_CAN_STASH             0x20
90 
91 
92 #define HANDLER_CAN_RONLY   (HANDLER_CAN_GETANDGETNEXT)
93 #define HANDLER_CAN_RWRITE  (HANDLER_CAN_GETANDGETNEXT | HANDLER_CAN_SET)
94 #define HANDLER_CAN_SET_ONLY (HANDLER_CAN_SET | HANDLER_CAN_NOT_CREATE)
95 #define HANDLER_CAN_DEFAULT (HANDLER_CAN_RONLY | HANDLER_CAN_NOT_CREATE)
96 
97 /** @typedef struct netsnmp_handler_registration_s netsnmp_handler_registration
98  * Typedefs the netsnmp_handler_registration_s struct into netsnmp_handler_registration  */
99 
100 /** @struct netsnmp_handler_registration_s
101  *  Root registration info.
102  *  The variables handlerName, contextName, and rootoid need to be allocated
103  *  on the heap, when the registration structure is unregistered using
104  *  unregister_mib_context() the code attempts to free them.
105  */
106 typedef struct netsnmp_handler_registration_s {
107 
108 	/** for mrTable listings, and other uses */
109         char           *handlerName;
110 	/** NULL = default context */
111         char           *contextName;
112 
113         /**
114          * where are we registered at?
115          */
116         oid            *rootoid;
117         size_t          rootoid_len;
118 
119         /**
120          * handler details
121          */
122         netsnmp_mib_handler *handler;
123         int             modes;
124 
125         /**
126          * more optional stuff
127          */
128         int             priority;
129         int             range_subid;
130         oid             range_ubound;
131         int             timeout;
132         int             global_cacheid;
133 
134         /**
135          * void ptr for registeree
136          */
137         void *          my_reg_void;
138 
139 } netsnmp_handler_registration;
140 
141 /*
142  * function handler definitions
143  */
144 
145 typedef int (Netsnmp_Node_Handler) (netsnmp_mib_handler *handler,
146     /** pointer to registration struct */
147     netsnmp_handler_registration *reginfo,
148     /** pointer to current transaction */
149     netsnmp_agent_request_info *reqinfo,
150     netsnmp_request_info *requests);
151 
152     typedef struct netsnmp_handler_args_s {
153         netsnmp_mib_handler *handler;
154         netsnmp_handler_registration *reginfo;
155         netsnmp_agent_request_info *reqinfo;
156         netsnmp_request_info *requests;
157     } netsnmp_handler_args;
158 
159     typedef struct netsnmp_delegated_cache_s {
160         int             transaction_id;
161         netsnmp_mib_handler *handler;
162         netsnmp_handler_registration *reginfo;
163         netsnmp_agent_request_info *reqinfo;
164         netsnmp_request_info *requests;
165         void           *localinfo;
166     } netsnmp_delegated_cache;
167 
168     /*
169      * handler API functions
170      */
171     void            netsnmp_init_handler_conf(void);
172     int             netsnmp_register_handler(netsnmp_handler_registration
173                                              *reginfo);
174     int             netsnmp_unregister_handler(netsnmp_handler_registration
175                                                *reginfo);
176     int
177         netsnmp_register_handler_nocallback(netsnmp_handler_registration
178                                             *reginfo);
179     int             netsnmp_inject_handler(netsnmp_handler_registration
180                                            *reginfo,
181                                            netsnmp_mib_handler *handler);
182     int
183         netsnmp_inject_handler_before(netsnmp_handler_registration *reginfo,
184                                       netsnmp_mib_handler *handler,
185                                       const char *before_what);
186     netsnmp_mib_handler
187         *netsnmp_find_handler_by_name(netsnmp_handler_registration
188                                       *reginfo, const char *name);
189     void
190         *netsnmp_find_handler_data_by_name(netsnmp_handler_registration
191                                            *reginfo, const char *name);
192     int             netsnmp_call_handlers(netsnmp_handler_registration
193                                           *reginfo,
194                                           netsnmp_agent_request_info
195                                           *reqinfo,
196                                           netsnmp_request_info *requests);
197     int             netsnmp_call_handler(netsnmp_mib_handler *next_handler,
198                                          netsnmp_handler_registration
199                                          *reginfo,
200                                          netsnmp_agent_request_info
201                                          *reqinfo,
202                                          netsnmp_request_info *requests);
203     int             netsnmp_call_next_handler(netsnmp_mib_handler *current,
204                                               netsnmp_handler_registration
205                                               *reginfo,
206                                               netsnmp_agent_request_info
207                                               *reqinfo,
208                                               netsnmp_request_info
209                                               *requests);
210     int             netsnmp_call_next_handler_one_request(netsnmp_mib_handler *current,
211                                                           netsnmp_handler_registration *reginfo,
212                                                           netsnmp_agent_request_info *reqinfo,
213                                                           netsnmp_request_info *requests);
214 
215     netsnmp_mib_handler *netsnmp_create_handler(const char *name,
216                                                 Netsnmp_Node_Handler *
217                                                 handler_access_method);
218     netsnmp_handler_registration *
219     netsnmp_handler_registration_create(const char *name,
220                                         netsnmp_mib_handler *handler,
221                                         const oid * reg_oid, size_t reg_oid_len,
222                                         int modes);
223     netsnmp_handler_registration *
224     netsnmp_create_handler_registration(const char *name, Netsnmp_Node_Handler*
225                                         handler_access_method,
226                                         const oid *reg_oid, size_t reg_oid_len,
227                                         int modes);
228 
229     netsnmp_delegated_cache
230         *netsnmp_create_delegated_cache(netsnmp_mib_handler *,
231                                         netsnmp_handler_registration *,
232                                         netsnmp_agent_request_info *,
233                                         netsnmp_request_info *, void *);
234     void netsnmp_free_delegated_cache(netsnmp_delegated_cache *dcache);
235     netsnmp_delegated_cache
236         *netsnmp_handler_check_cache(netsnmp_delegated_cache *dcache);
237     void            netsnmp_register_handler_by_name(const char *,
238                                                      netsnmp_mib_handler
239                                                      *);
240 
241     void            netsnmp_clear_handler_list(void);
242 
243     void
244         netsnmp_request_add_list_data(netsnmp_request_info *request,
245                                       netsnmp_data_list *node);
246     int netsnmp_request_remove_list_data(netsnmp_request_info *request,
247                                          const char *name);
248 
249     int
250         netsnmp_request_remove_list_data(netsnmp_request_info *request,
251                                          const char *name);
252 
253     void    *netsnmp_request_get_list_data(netsnmp_request_info
254                                                   *request,
255                                                   const char *name);
256 
257     void
258               netsnmp_free_request_data_set(netsnmp_request_info *request);
259 
260     void
261              netsnmp_free_request_data_sets(netsnmp_request_info *request);
262 
263     void            netsnmp_handler_free(netsnmp_mib_handler *);
264     netsnmp_mib_handler *netsnmp_handler_dup(netsnmp_mib_handler *);
265     netsnmp_handler_registration
266         *netsnmp_handler_registration_dup(netsnmp_handler_registration *);
267     void
268         netsnmp_handler_registration_free(netsnmp_handler_registration *);
269 
270 #define REQUEST_IS_DELEGATED     1
271 #define REQUEST_IS_NOT_DELEGATED 0
272     void
273         netsnmp_handler_mark_requests_as_delegated(netsnmp_request_info *,
274                                                    int);
275     void           *netsnmp_handler_get_parent_data(netsnmp_request_info *,
276                                                     const char *);
277 
278 #ifdef __cplusplus
279 }
280 #endif
281 
282 #endif                          /* AGENT_HANDLER_H */
283 /** @} */
284