and added files
[bcm963xx.git] / userapps / opensource / net-snmp / agent / mibgroup / testhandler.c
1 #include <net-snmp/net-snmp-config.h>
2
3 #ifdef HAVE_STDLIB_H
4 #include <stdlib.h>
5 #endif
6 #if HAVE_STRING_H
7 #include <string.h>
8 #else
9 #include <strings.h>
10 #endif
11
12 #include <net-snmp/net-snmp-includes.h>
13 #include <net-snmp/agent/net-snmp-agent-includes.h>
14
15 #include "testhandler.h"
16
17 #include <net-snmp/agent/table.h>
18 #include <net-snmp/agent/instance.h>
19 #include <net-snmp/agent/table_data.h>
20 #include <net-snmp/agent/table_dataset.h>
21
22 static oid      my_test_oid[4] = { 1, 2, 3, 4 };
23 static oid      my_table_oid[4] = { 1, 2, 3, 5 };
24 static oid      my_instance_oid[5] = { 1, 2, 3, 6, 1 };
25 static oid      my_data_table_oid[4] = { 1, 2, 3, 7 };
26 static oid      my_data_table_set_oid[4] = { 1, 2, 3, 8 };
27 static oid      my_data_ulong_instance[4] = { 1, 2, 3, 9 };
28
29 u_long          my_ulong = 0;
30
31 void
32 init_testhandler(void)
33 {
34     /*
35      * we're registering at .1.2.3.4 
36      */
37     netsnmp_handler_registration *my_test;
38     netsnmp_table_registration_info *table_info;
39     u_long          ind1;
40     netsnmp_table_data *table;
41     netsnmp_table_data_set *table_set;
42     netsnmp_table_row *row;
43
44     DEBUGMSGTL(("testhandler", "initializing\n"));
45
46     /*
47      * basic handler test
48      */
49     netsnmp_register_handler(netsnmp_create_handler_registration
50                              ("myTest", my_test_handler, my_test_oid, 4,
51                               HANDLER_CAN_RONLY));
52
53     /*
54      * instance handler test
55      */
56
57     netsnmp_register_instance(netsnmp_create_handler_registration
58                               ("myInstance", my_test_instance_handler,
59                                my_instance_oid, 5, HANDLER_CAN_RWRITE));
60
61     netsnmp_register_ulong_instance("myulong",
62                                     my_data_ulong_instance, 4,
63                                     &my_ulong, NULL);
64
65     /*
66      * table helper test
67      */
68
69     my_test = netsnmp_create_handler_registration("myTable",
70                                                   my_test_table_handler,
71                                                   my_table_oid, 4,
72                                                   HANDLER_CAN_RONLY);
73     if (!my_test)
74         return;
75
76     table_info = SNMP_MALLOC_TYPEDEF(netsnmp_table_registration_info);
77
78     netsnmp_table_helper_add_indexes(table_info, ASN_INTEGER, ASN_INTEGER,
79                                      0);
80     table_info->min_column = 3;
81     table_info->max_column = 3;
82     netsnmp_register_table(my_test, table_info);
83
84     /*
85      * data table helper test
86      */
87     /*
88      * we'll construct a simple table here with two indexes: an
89      * integer and a string (why not).  It'll contain only one
90      * column so the data pointer is merely the data in that
91      * column. 
92      */
93
94     table = netsnmp_create_table_data("data_table_test");
95
96     netsnmp_table_data_add_index(table, ASN_INTEGER);
97     netsnmp_table_data_add_index(table, ASN_OCTET_STR);
98
99     /*
100      * 1 partridge in a pear tree 
101      */
102     row = netsnmp_create_table_data_row();
103     ind1 = 1;
104     netsnmp_table_row_add_index(row, ASN_INTEGER, &ind1, sizeof(ind1));
105     netsnmp_table_row_add_index(row, ASN_OCTET_STR, "partridge",
106                                 strlen("partridge"));
107     row->data = (void *) "pear tree";
108     netsnmp_table_data_add_row(table, row);
109
110     /*
111      * 2 turtle doves 
112      */
113     row = netsnmp_create_table_data_row();
114     ind1 = 2;
115     netsnmp_table_row_add_index(row, ASN_INTEGER, &ind1, sizeof(ind1));
116     netsnmp_table_row_add_index(row, ASN_OCTET_STR, "turtle",
117                                 strlen("turtle"));
118     row->data = (void *) "doves";
119     netsnmp_table_data_add_row(table, row);
120
121     /*
122      * we're going to register it as a normal table too, so we get the
123      * automatically parsed column and index information 
124      */
125     table_info = SNMP_MALLOC_TYPEDEF(netsnmp_table_registration_info);
126
127     netsnmp_table_helper_add_indexes(table_info, ASN_INTEGER,
128                                      ASN_OCTET_STR, 0);
129     table_info->min_column = 3;
130     table_info->max_column = 3;
131
132     netsnmp_register_read_only_table_data
133         (netsnmp_create_handler_registration
134          ("12days", my_data_table_handler, my_data_table_oid, 4,
135           HANDLER_CAN_RONLY), table, table_info);
136
137 }
138
139 int
140 my_test_handler(netsnmp_mib_handler *handler,
141                 netsnmp_handler_registration *reginfo,
142                 netsnmp_agent_request_info *reqinfo,
143                 netsnmp_request_info *requests)
144 {
145
146     oid             myoid1[] = { 1, 2, 3, 4, 5, 6 };
147     static u_long   accesses = 0;
148
149     DEBUGMSGTL(("testhandler", "Got request:\n"));
150     /*
151      * loop through requests 
152      */
153     while (requests) {
154         netsnmp_variable_list *var = requests->requestvb;
155
156         DEBUGMSGTL(("testhandler", "  oid:"));
157         DEBUGMSGOID(("testhandler", var->name, var->name_length));
158         DEBUGMSG(("testhandler", "\n"));
159
160         switch (reqinfo->mode) {
161         case MODE_GET:
162             if (netsnmp_oid_equals(var->name, var->name_length, myoid1, 6)
163                 == 0) {
164                 snmp_set_var_typed_value(var, ASN_INTEGER,
165                                          (u_char *) & accesses,
166                                          sizeof(accesses));
167                 return SNMP_ERR_NOERROR;
168             }
169             break;
170
171         case MODE_GETNEXT:
172             if (snmp_oid_compare(var->name, var->name_length, myoid1, 6)
173                 < 0) {
174                 snmp_set_var_objid(var, myoid1, 6);
175                 snmp_set_var_typed_value(var, ASN_INTEGER,
176                                          (u_char *) & accesses,
177                                          sizeof(accesses));
178                 return SNMP_ERR_NOERROR;
179             }
180             break;
181
182         default:
183             netsnmp_set_request_error(reqinfo, requests, SNMP_ERR_GENERR);
184             break;
185         }
186
187         requests = requests->next;
188     }
189     return SNMP_ERR_NOERROR;
190 }
191
192 /*
193  * functionally this is a simply a multiplication table for 12x12
194  */
195
196 #define MAX_COLONE 12
197 #define MAX_COLTWO 12
198 #define RESULT_COLUMN 3
199 int
200 my_test_table_handler(netsnmp_mib_handler *handler,
201                       netsnmp_handler_registration *reginfo,
202                       netsnmp_agent_request_info *reqinfo,
203                       netsnmp_request_info *requests)
204 {
205
206     netsnmp_table_registration_info
207      
208         
209         
210         
211         
212         
213         
214         
215         
216         
217         
218         
219         
220         
221         *handler_reg_info =
222         (netsnmp_table_registration_info *) handler->prev->myvoid;
223     netsnmp_table_request_info *table_info;
224     u_long          result;
225     int             x, y;
226
227
228     while (requests) {
229         netsnmp_variable_list *var = requests->requestvb;
230
231         if (requests->processed != 0)
232             continue;
233
234         DEBUGMSGTL(("testhandler_table", "Got request:\n"));
235         DEBUGMSGTL(("testhandler_table", "  oid:"));
236         DEBUGMSGOID(("testhandler_table", var->name, var->name_length));
237         DEBUGMSG(("testhandler_table", "\n"));
238
239         table_info = netsnmp_extract_table_info(requests);
240         if (table_info == NULL) {
241             requests = requests->next;
242             continue;
243         }
244
245         switch (reqinfo->mode) {
246         case MODE_GETNEXT:
247             /*
248              * beyond our search range? 
249              */
250             if (table_info->colnum > RESULT_COLUMN)
251                 break;
252
253             /*
254              * below our minimum column? 
255              */
256             if (table_info->colnum < RESULT_COLUMN ||
257                 /*
258                  * or no index specified 
259                  */
260                 table_info->indexes->val.integer == 0) {
261                 table_info->colnum = RESULT_COLUMN;
262                 x = 0;
263                 y = 0;
264             } else {
265                 x = *(table_info->indexes->val.integer);
266                 y = *(table_info->indexes->next_variable->val.integer);
267             }
268
269             if (table_info->number_indexes ==
270                 handler_reg_info->number_indexes) {
271                 y++;            /* GETNEXT is basically just y+1 for this table */
272                 if (y > MAX_COLTWO) {   /* (with wrapping) */
273                     y = 0;
274                     x++;
275                 }
276             }
277             if (x <= MAX_COLONE) {
278                 result = x * y;
279
280                 *(table_info->indexes->val.integer) = x;
281                 *(table_info->indexes->next_variable->val.integer) = y;
282                 netsnmp_table_build_result(reginfo, requests,
283                                            table_info, ASN_INTEGER,
284                                            (u_char *) & result,
285                                            sizeof(result));
286             }
287
288             break;
289
290         case MODE_GET:
291             if (var->type == ASN_NULL) {        /* valid request if ASN_NULL */
292                 /*
293                  * is it the right column? 
294                  */
295                 if (table_info->colnum == RESULT_COLUMN &&
296                     /*
297                      * and within the max boundries? 
298                      */
299                     *(table_info->indexes->val.integer) <= MAX_COLONE &&
300                     *(table_info->indexes->next_variable->val.integer)
301                     <= MAX_COLTWO) {
302
303                     /*
304                      * then, the result is column1 * column2 
305                      */
306                     result = *(table_info->indexes->val.integer) *
307                         *(table_info->indexes->next_variable->val.integer);
308                     snmp_set_var_typed_value(var, ASN_INTEGER,
309                                              (u_char *) & result,
310                                              sizeof(result));
311                 }
312             }
313             break;
314
315         }
316
317         requests = requests->next;
318     }
319
320     return SNMP_ERR_NOERROR;
321 }
322
323 #define TESTHANDLER_SET_NAME "my_test"
324 int
325 my_test_instance_handler(netsnmp_mib_handler *handler,
326                          netsnmp_handler_registration *reginfo,
327                          netsnmp_agent_request_info *reqinfo,
328                          netsnmp_request_info *requests)
329 {
330
331     static u_long   accesses = 0;
332     u_long         *accesses_cache = NULL;
333
334     DEBUGMSGTL(("testhandler", "Got instance request:\n"));
335
336     switch (reqinfo->mode) {
337     case MODE_GET:
338         snmp_set_var_typed_value(requests->requestvb, ASN_UNSIGNED,
339                                  (u_char *) & accesses, sizeof(accesses));
340         break;
341
342     case MODE_SET_RESERVE1:
343         if (requests->requestvb->type != ASN_UNSIGNED)
344             netsnmp_set_request_error(reqinfo, requests,
345                                       SNMP_ERR_WRONGTYPE);
346         break;
347
348     case MODE_SET_RESERVE2:
349         /*
350          * store old info for undo later 
351          */
352         memdup((u_char **) & accesses_cache,
353                (u_char *) & accesses, sizeof(accesses));
354         if (accesses_cache == NULL) {
355             netsnmp_set_request_error(reqinfo, requests,
356                                       SNMP_ERR_RESOURCEUNAVAILABLE);
357             return SNMP_ERR_NOERROR;
358         }
359         netsnmp_request_add_list_data(requests,
360                                       netsnmp_create_data_list
361                                       (TESTHANDLER_SET_NAME,
362                                        accesses_cache, free));
363         break;
364
365     case MODE_SET_ACTION:
366         /*
367          * update current 
368          */
369         accesses = *(requests->requestvb->val.integer);
370         DEBUGMSGTL(("testhandler", "updated accesses -> %d\n", accesses));
371         break;
372
373     case MODE_SET_UNDO:
374         accesses =
375             *((u_long *) netsnmp_request_get_list_data(requests,
376                                                        TESTHANDLER_SET_NAME));
377         break;
378
379     case MODE_SET_COMMIT:
380     case MODE_SET_FREE:
381         /*
382          * nothing to do 
383          */
384         break;
385     }
386
387     return SNMP_ERR_NOERROR;
388 }
389
390 int
391 my_data_table_handler(netsnmp_mib_handler *handler,
392                       netsnmp_handler_registration *reginfo,
393                       netsnmp_agent_request_info *reqinfo,
394                       netsnmp_request_info *requests)
395 {
396
397     char           *column3;
398     netsnmp_table_request_info *table_info;
399     netsnmp_table_row *row;
400
401     while (requests) {
402         if (requests->processed)
403             continue;
404
405         /*
406          * extract our stored data and table info 
407          */
408         row = netsnmp_extract_table_row(requests);
409         table_info = netsnmp_extract_table_info(requests);
410         if (row)
411             column3 = (char *) row->data;
412         if (!row || !table_info || !column3)
413             continue;
414
415         /*
416          * there's only one column, we don't need to check if it's right 
417          */
418         netsnmp_table_data_build_result(reginfo, reqinfo, requests, row,
419                                         table_info->colnum,
420                                         ASN_OCTET_STR, column3,
421                                         strlen(column3));
422         requests = requests->next;
423     }
424     return SNMP_ERR_NOERROR;
425 }