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  * Portions of this file are copyrighted by:
12  * Copyright (c) 2016 VMware, Inc. All rights reserved.
13  * Use is subject to license terms specified in the COPYING file
14  * distributed with the Net-SNMP package.
15  */
16 /*
17  * @file snmp_agent.h
18  *
19  * @addtogroup agent
20  * @addtogroup table
21  * External definitions for functions and variables in snmp_agent.c.
22  *
23  * @{
24  */
25 
26 #ifndef SNMP_AGENT_H
27 #define SNMP_AGENT_H
28 
29 #include <stdint.h>
30 
31 #ifdef __cplusplus
32 extern          "C" {
33 #endif
34 
35 #include <net-snmp/library/snmp_impl.h>
36 #include <net-snmp/library/tools.h>
37 #include <net-snmp/library/data_list.h>
38 
39 #define SNMP_MAX_PDU_SIZE 64000 /* local constraint on PDU size sent by agent
40                                  * (see also SNMP_MAX_MSG_SIZE in snmp_api.h) */
41 
42 #define SNMP_AGENT_FLAGS_NONE                   0x0
43 #define SNMP_AGENT_FLAGS_CANCEL_IN_PROGRESS     0x1
44 
45     struct timeval;
46 
47     /*
48      * If non-zero, causes the addresses of peers to be logged when receptions
49      * occur.
50      */
51 
52     extern int      log_addresses;
53 
54     /*
55      * How many ticks since we last aged the address cache entries.
56      */
57 
58     extern int      lastAddrAge;
59 
60     /** @typedef struct netsnmp_request_info_s netsnmp_request_info
61      * Typedefs the netsnmp_request_info_s struct into
62      * netsnmp_request_info*/
63     /** @struct netsnmp_request_info_s
64      * The netsnmp request info structure.
65      */
66     typedef struct netsnmp_request_info_s {
67 	/**
68 	 * variable bindings
69  	 */
70         netsnmp_variable_list *requestvb;
71 
72  	/**
73  	 * can be used to pass information on a per-request basis from a
74  	 * helper to the later handlers
75  	 */
76         netsnmp_data_list *parent_data;
77 
78        /*
79         * pointer to the agent_request_info for this request
80         */
81        struct netsnmp_agent_request_info_s *agent_req_info;
82 
83  	/** don't free, reference to (struct tree)->end */
84         oid            *range_end;
85         size_t          range_end_len;
86 
87        /*
88         * flags
89         */
90         int             delegated;
91         int             processed;
92         int             inclusive;
93 
94         int             status;
95  	/** index in original pdu */
96         int             index;
97 
98        /** get-bulk */
99         int             repeat;
100         int             orig_repeat;
101         netsnmp_variable_list *requestvb_start;
102 
103        /* internal use */
104         struct netsnmp_request_info_s *next;
105         struct netsnmp_request_info_s *prev;
106         struct netsnmp_subtree_s      *subtree;
107     } netsnmp_request_info;
108 
109     typedef struct netsnmp_set_info_s {
110         int             action;
111         void           *stateRef;
112 
113         /*
114          * don't use yet:
115          */
116         void          **oldData;
117         int             setCleanupFlags;
118 #define AUTO_FREE_STATEREF 0x01 /* calls free(stateRef) */
119 #define AUTO_FREE_OLDDATA  0x02 /* calls free(*oldData) */
120 #define AUTO_UNDO          0x03 /* ... */
121     } netsnmp_set_info;
122 
123     typedef struct netsnmp_tree_cache_s {
124         struct netsnmp_subtree_s *subtree;
125         netsnmp_request_info *requests_begin;
126         netsnmp_request_info *requests_end;
127     } netsnmp_tree_cache;
128 
129 #define MODE_GET              SNMP_MSG_GET
130 #define MODE_GETNEXT          SNMP_MSG_GETNEXT
131 #define MODE_GETBULK          SNMP_MSG_GETBULK
132 #define MODE_GET_STASH        SNMP_MSG_INTERNAL_GET_STASH
133 #ifndef NETSNMP_NO_WRITE_SUPPORT
134 #define MODE_IS_GET(x)        ((x >= 128) && (x != -1)  && (x != SNMP_MSG_SET))
135 #else /* NETSNMP_NO_WRITE_SUPPORT */
136 #define MODE_IS_GET(x)        ((x >= 128) && (x != -1))
137 #endif /* NETSNMP_NO_WRITE_SUPPORT */
138 
139     /* #define MODE_IS_GET(x)        ((x == SNMP_MSG_GET) || (x == SNMP_MSG_GETNEXT) || (x == SNMP_MSG_GETBULK) || (x == SNMP_MSG_INTERNAL_GET_STASH)) */
140 
141 #ifndef NETSNMP_NO_WRITE_SUPPORT
142 #define MODE_SET_BEGIN        SNMP_MSG_INTERNAL_SET_BEGIN
143 #define MODE_SET_RESERVE1     SNMP_MSG_INTERNAL_SET_RESERVE1
144 #define MODE_SET_RESERVE2     SNMP_MSG_INTERNAL_SET_RESERVE2
145 #define MODE_SET_ACTION       SNMP_MSG_INTERNAL_SET_ACTION
146 #define MODE_SET_COMMIT       SNMP_MSG_INTERNAL_SET_COMMIT
147 #define MODE_SET_FREE         SNMP_MSG_INTERNAL_SET_FREE
148 #define MODE_SET_UNDO         SNMP_MSG_INTERNAL_SET_UNDO
149 #define MODE_IS_SET(x)         ((x < 128) || (x == -1) || (x == SNMP_MSG_SET))
150     /* #define MODE_IS_SET(x)         (!MODE_IS_GET(x)) */
151 #endif /* NETSNMP_NO_WRITE_SUPPORT */
152 
153 #define MODE_BSTEP_PRE_REQUEST   SNMP_MSG_INTERNAL_PRE_REQUEST
154 #define MODE_BSTEP_POST_REQUEST  SNMP_MSG_INTERNAL_POST_REQUEST
155 
156 #define MODE_BSTEP_OBJECT_LOOKUP       SNMP_MSG_INTERNAL_OBJECT_LOOKUP
157 #ifndef NETSNMP_NO_WRITE_SUPPORT
158 #define MODE_BSTEP_CHECK_VALUE         SNMP_MSG_INTERNAL_CHECK_VALUE
159 #define MODE_BSTEP_ROW_CREATE          SNMP_MSG_INTERNAL_ROW_CREATE
160 #define MODE_BSTEP_UNDO_SETUP          SNMP_MSG_INTERNAL_UNDO_SETUP
161 #define MODE_BSTEP_SET_VALUE           SNMP_MSG_INTERNAL_SET_VALUE
162 #define MODE_BSTEP_CHECK_CONSISTENCY   SNMP_MSG_INTERNAL_CHECK_CONSISTENCY
163 #define MODE_BSTEP_UNDO_SET            SNMP_MSG_INTERNAL_UNDO_SET
164 #define MODE_BSTEP_COMMIT              SNMP_MSG_INTERNAL_COMMIT
165 #define MODE_BSTEP_UNDO_COMMIT         SNMP_MSG_INTERNAL_UNDO_COMMIT
166 #define MODE_BSTEP_IRREVERSIBLE_COMMIT SNMP_MSG_INTERNAL_IRREVERSIBLE_COMMIT
167 #define MODE_BSTEP_UNDO_CLEANUP        SNMP_MSG_INTERNAL_UNDO_CLEANUP
168 #endif /* NETSNMP_NO_WRITE_SUPPORT */
169 
170 /** @typedef struct netsnmp_agent_request_info_s netsnmp_agent_request_info
171  * Typedefs the netsnmp_agent_request_info_s struct into
172  * netsnmp_agent_request_info
173  */
174 
175 /** @struct netsnmp_agent_request_info_s
176  * The agent transaction request structure
177  */
178     typedef struct netsnmp_agent_request_info_s {
179         int             mode;
180  	/** pdu contains authinfo, eg */
181 /*        netsnmp_pdu    *pdu;    */
182         struct netsnmp_agent_session_s *asp;    /* may not be needed */
183         /*
184          * can be used to pass information on a per-pdu basis from a
185          * helper to the later handlers
186          */
187         netsnmp_data_list *agent_data;
188     } netsnmp_agent_request_info;
189 
190     typedef struct netsnmp_cachemap_s {
191         int             globalid;
192         int             cacheid;
193         struct netsnmp_cachemap_s *next;
194     } netsnmp_cachemap;
195 
196     typedef struct netsnmp_agent_session_s {
197         int             mode;
198         netsnmp_session *session;
199         netsnmp_pdu    *pdu;
200         netsnmp_pdu    *orig_pdu;
201         int             rw;
202         int             exact;
203         int             status;
204         int             index;
205         int             oldmode;
206 
207         struct netsnmp_agent_session_s *next;
208 
209         /*
210          * new API pointers
211          */
212         netsnmp_agent_request_info *reqinfo;
213         netsnmp_request_info *requests;
214         netsnmp_tree_cache *treecache;
215         netsnmp_variable_list **bulkcache;
216         int             treecache_len;  /* length of cache array */
217         int             treecache_num;  /* number of current cache entries */
218         netsnmp_cachemap *cache_store;
219         int             vbcount;
220         int             flags;
221     } netsnmp_agent_session;
222 
223     /*
224      * Address cache handling functions.
225      */
226 
227     void            netsnmp_addrcache_initialise(void);
228     void            netsnmp_addrcache_destroy(void);
229     void            netsnmp_addrcache_age(void);
230 
231 
232     /*
233      * config file parsing routines
234      */
235     int             handle_snmp_packet(int, netsnmp_session *, int,
236                                        netsnmp_pdu *, void *);
237     void            snmp_agent_parse_config(char *, char *);
238     netsnmp_agent_session *init_agent_snmp_session(netsnmp_session *,
239                                                    netsnmp_pdu *);
240     void            free_agent_snmp_session(netsnmp_agent_session *);
241     void
242         netsnmp_remove_and_free_agent_snmp_session(netsnmp_agent_session
243                                                    *asp);
244 #ifdef SNMP_NEED_REQUEST_LIST
245     void
246         netsnmp_free_agent_snmp_session_by_session(netsnmp_session * sess,
247                                                    void (*free_request)
248                                                    (netsnmp_request_list
249                                                     *));
250 #endif
251     int             getNextSessID(void);
252     void            dump_sess_list(void);
253     int             init_master_agent(void);
254     void            shutdown_master_agent(void);
255     int             agent_check_and_process(int block);
256     void            netsnmp_check_delegated_requests(void);
257     void            netsnmp_check_outstanding_agent_requests(void);
258 
259     int             netsnmp_request_set_error(netsnmp_request_info *request,
260                                               int error_value);
261     int             netsnmp_check_requests_error(netsnmp_request_info *reqs);
262     int             netsnmp_check_all_requests_error(netsnmp_agent_session *asp,
263                                                      int look_for_specific);
264     int
265         netsnmp_set_all_requests_error(netsnmp_agent_request_info *reqinfo,
266                                        netsnmp_request_info *requests,
267                                        int error_value);
268     int netsnmp_request_set_error_idx(netsnmp_request_info *requests,
269                                        int error_value, int idx);
270     int
271         netsnmp_request_set_error_all(netsnmp_request_info *requests,
272                                        int error_value);
273 
274     /** deprecated, use netsnmp_request_set_error instead */
275     int             netsnmp_set_request_error(netsnmp_agent_request_info
276                                               *reqinfo,
277                                               netsnmp_request_info
278                                               *request, int error_value);
279     /** deprecated, use netsnmp_request_set_error instead */
280     int             netsnmp_set_mode_request_error(int mode,
281                                                    netsnmp_request_info
282                                                    *request,
283                                                    int error_value);
284 
285     u_long          netsnmp_marker_uptime(marker_t pm);
286     u_long          netsnmp_timeval_uptime(struct timeval *tv);
287     const_marker_t  netsnmp_get_agent_starttime(void);
288     uint64_t        netsnmp_get_agent_runtime(void);
289     void            netsnmp_set_agent_starttime(marker_t s);
290     u_long          netsnmp_get_agent_uptime(void);
291     void            netsnmp_set_agent_uptime(u_long hsec);
292     int             netsnmp_check_transaction_id(int transaction_id);
293     int             netsnmp_agent_check_packet(netsnmp_session *,
294                                                struct netsnmp_transport_s
295                                                *, void *, int);
296     int             netsnmp_agent_check_parse(netsnmp_session *,
297                                               netsnmp_pdu *, int);
298     int             netsnmp_allocate_globalcacheid(void);
299 
300     int netsnmp_remove_delegated_requests_for_session(netsnmp_session *sess);
301 
302     /*
303      * Register and de-register agent NSAPs.
304      */
305 
306     struct netsnmp_transport_s;
307 
308     int             netsnmp_register_agent_nsap(struct netsnmp_transport_s
309                                                 *t);
310     void            netsnmp_deregister_agent_nsap(int handle);
311 
312     int             netsnmp_agent_listen_on(const char *port);
313 
314     void
315         netsnmp_agent_add_list_data(netsnmp_agent_request_info *agent,
316                                     netsnmp_data_list *node);
317 
318     int
319         netsnmp_agent_remove_list_data(netsnmp_agent_request_info *ari,
320                                        const char * name);
321 
322     void *
323             netsnmp_agent_get_list_data(netsnmp_agent_request_info
324                                         *agent, const char *name);
325 
326     void
327             netsnmp_free_agent_data_set(netsnmp_agent_request_info *agent);
328 
329     void
330            netsnmp_free_agent_data_sets(netsnmp_agent_request_info *agent);
331     void
332         netsnmp_free_agent_request_info(netsnmp_agent_request_info *ari);
333 
334 
335 #ifndef NETSNMP_NO_PDU_STATS
336     /*
337      * pdu stats
338      */
339     typedef struct netsnmp_pdu_stats_s {
340         u_long          processing_time; /* ms */
341         time_t          timestamp; /* date/time */
342         netsnmp_pdu    *pdu;
343     } netsnmp_pdu_stats;
344 
345     netsnmp_container * netsnmp_get_pdu_stats(void);
346 
347 #endif /* NETSNMP_NO_PDU_STATS */
348 
349 
350 #ifdef __cplusplus
351 }
352 #endif
353 #endif
354 /** @} */
355