www.usr.com/support/gpl/USR9113_release1.0.tar.gz
[bcm963xx.git] / userapps / opensource / net-snmp / agent / helpers / scalar.c
1 #ifdef BRCM_SNMP_SUPPORT
2
3 #include <net-snmp/net-snmp-config.h>
4
5 #include <stdlib.h>
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 <net-snmp/agent/scalar.h>
16 #include <net-snmp/agent/instance.h>
17 #include <net-snmp/agent/serialize.h>
18 #include <net-snmp/agent/read_only.h>
19
20 #if HAVE_DMALLOC_H
21 #include <dmalloc.h>
22 #endif
23
24 /** @defgroup scalar scalar: process scalars easily.
25  *  @ingroup handler
26  *  @{
27  */
28 netsnmp_mib_handler *
29 netsnmp_get_scalar_handler(void)
30 {
31     return netsnmp_create_handler("scalar",
32                                   netsnmp_scalar_helper_handler);
33 }
34
35 int
36 netsnmp_register_scalar(netsnmp_handler_registration *reginfo)
37 {
38     /*
39      * Extend the registered OID with space for the instance subid
40      * (but don't extend the length just yet!)
41      */
42     reginfo->rootoid = realloc(reginfo->rootoid,
43                               (reginfo->rootoid_len+1) * sizeof(oid) );
44     reginfo->rootoid[ reginfo->rootoid_len ] = 0;
45
46     netsnmp_inject_handler(reginfo, netsnmp_get_instance_handler());
47     netsnmp_inject_handler(reginfo, netsnmp_get_scalar_handler());
48     return netsnmp_register_serialize(reginfo);
49 }
50
51 int
52 netsnmp_register_read_only_scalar(netsnmp_handler_registration *reginfo)
53 {
54     /*
55      * Extend the registered OID with space for the instance subid
56      * (but don't extend the length just yet!)
57      */
58     reginfo->rootoid = realloc(reginfo->rootoid,
59                               (reginfo->rootoid_len+1) * sizeof(oid) );
60     reginfo->rootoid[ reginfo->rootoid_len ] = 0;
61
62     netsnmp_inject_handler(reginfo, netsnmp_get_instance_handler());
63     netsnmp_inject_handler(reginfo, netsnmp_get_scalar_handler());
64     netsnmp_inject_handler(reginfo, netsnmp_get_read_only_handler());
65     return netsnmp_register_serialize(reginfo);
66 }
67
68
69
70 int
71 netsnmp_scalar_helper_handler(netsnmp_mib_handler *handler,
72                                 netsnmp_handler_registration *reginfo,
73                                 netsnmp_agent_request_info *reqinfo,
74                                 netsnmp_request_info *requests)
75 {
76
77     netsnmp_variable_list *var = requests->requestvb;
78
79     int             ret, cmp;
80     int             namelen;
81
82     DEBUGMSGTL(("helper:scalar", "Got request:\n"));
83     namelen = SNMP_MIN(requests->requestvb->name_length,
84                        reginfo->rootoid_len);
85     cmp = snmp_oid_compare(requests->requestvb->name, namelen,
86                            reginfo->rootoid, reginfo->rootoid_len);
87
88     DEBUGMSGTL(("helper:scalar", "  oid:", cmp));
89     DEBUGMSGOID(("helper:scalar", var->name, var->name_length));
90     DEBUGMSG(("helper:scalar", "\n"));
91
92     switch (reqinfo->mode) {
93     case MODE_GET:
94         if (cmp != 0) {
95             netsnmp_set_request_error(reqinfo, requests,
96                                       SNMP_NOSUCHOBJECT);
97             return SNMP_ERR_NOERROR;
98         } else {
99             reginfo->rootoid_len++;
100             ret = netsnmp_call_next_handler(handler, reginfo, reqinfo,
101                                              requests);
102             reginfo->rootoid_len--;
103             return ret;
104         }
105         break;
106
107     case MODE_SET_RESERVE1:
108     case MODE_SET_RESERVE2:
109     case MODE_SET_ACTION:
110     case MODE_SET_COMMIT:
111     case MODE_SET_UNDO:
112     case MODE_SET_FREE:
113         if (cmp != 0) {
114             netsnmp_set_request_error(reqinfo, requests,
115                                       SNMP_ERR_NOCREATION);
116             return SNMP_ERR_NOERROR;
117         } else {
118             reginfo->rootoid_len++;
119             ret = netsnmp_call_next_handler(handler, reginfo, reqinfo,
120                                              requests);
121             reginfo->rootoid_len--;
122             return ret;
123         }
124         break;
125
126     case MODE_GETNEXT:
127         reginfo->rootoid_len++;
128         ret = netsnmp_call_next_handler(handler, reginfo, reqinfo, requests);
129         reginfo->rootoid_len--;
130         return ret;
131     }
132     /*
133      * got here only if illegal mode found 
134      */
135     return SNMP_ERR_GENERR;
136 }
137
138 /*
139  * @} 
140  */
141 #endif /* BRCM_SNMP_SUPPORT */