added files
[bcm963xx.git] / userapps / opensource / net-snmp / agent / helpers / scalar1.c
1 #include <net-snmp/net-snmp-config.h>
2
3 #include <stdlib.h>
4 #if HAVE_STRING_H
5 #include <string.h>
6 #else
7 #include <strings.h>
8 #endif
9
10 #include <net-snmp/net-snmp-includes.h>
11 #include <net-snmp/agent/net-snmp-agent-includes.h>
12
13 #include <net-snmp/agent/scalar.h>
14 #include <net-snmp/agent/serialize.h>
15 #include <net-snmp/agent/read_only.h>
16
17 #if HAVE_DMALLOC_H
18 #include <dmalloc.h>
19 #endif
20
21 /** @defgroup scalar scalar: process scalars easily.
22  *  @ingroup handler
23  *  @{
24  */
25 netsnmp_mib_handler *
26 netsnmp_get_scalar_handler(void)
27 {
28     return netsnmp_create_handler("scalar",
29                                   netsnmp_scalar_helper_handler);
30 }
31
32 int
33 netsnmp_register_scalar(netsnmp_handler_registration *reginfo)
34 {
35     netsnmp_inject_handler(reginfo, netsnmp_get_scalar_handler());
36     return netsnmp_register_serialize(reginfo);
37 }
38
39 int
40 netsnmp_register_read_only_scalar(netsnmp_handler_registration *reginfo)
41 {
42     netsnmp_inject_handler(reginfo, netsnmp_get_scalar_handler());
43     netsnmp_inject_handler(reginfo, netsnmp_get_read_only_handler());
44     return netsnmp_register_serialize(reginfo);
45 }
46
47
48 int
49 netsnmp_scalar_helper_handler(netsnmp_mib_handler *handler,
50                                 netsnmp_handler_registration *reginfo,
51                                 netsnmp_agent_request_info *reqinfo,
52                                 netsnmp_request_info *requests)
53 {
54
55     netsnmp_variable_list *var = requests->requestvb;
56
57     int             ret, cmp;
58     int             namelen;
59     oid             subid;
60     int             nosuch_err;
61
62     oid             build_space[MAX_OID_LEN];
63
64     DEBUGMSGTL(("helper:scalar", "Got request:\n"));
65     namelen = SNMP_MIN(requests->requestvb->name_length,
66                        reginfo->rootoid_len);
67     cmp = snmp_oid_compare(requests->requestvb->name, namelen,
68                            reginfo->rootoid, reginfo->rootoid_len);
69
70     namelen = requests->requestvb->name_length;
71     subid   = requests->requestvb->name[namelen-1];
72
73     DEBUGMSGTL(("helper:scalar", "  oid:", cmp));
74     DEBUGMSGOID(("helper:scalar", var->name, var->name_length));
75     DEBUGMSG(("helper:scalar", "\n"));
76
77     nosuch_err = SNMP_ERR_NOCREATION;
78     switch (reqinfo->mode) {
79     case MODE_GET:
80         nosuch_err = SNMP_NOSUCHINSTANCE;
81         /* Fall-through */
82
83     case MODE_SET_RESERVE1:
84     case MODE_SET_RESERVE2:
85     case MODE_SET_ACTION:
86     case MODE_SET_COMMIT:
87     case MODE_SET_UNDO:
88     case MODE_SET_FREE:
89         if (cmp != 0) {
90             netsnmp_set_request_error(reqinfo, requests,
91                                       SNMP_NOSUCHOBJECT);
92             return SNMP_ERR_NOERROR;
93         } else {
94             if (( namelen != reginfo->rootoid_len+1) ||
95                 ( subid   != 0 )) {
96                 netsnmp_set_request_error(reqinfo, requests, nosuch_err);
97                 return SNMP_ERR_NOERROR;
98             } else {
99                 return netsnmp_call_next_handler(handler, reginfo, reqinfo,
100                                              requests);
101             }
102         }
103         break;
104
105     case MODE_GETNEXT:
106         if (cmp < 0 ||
107            (cmp == 0 && namelen <= reginfo->rootoid_len) ||
108            (cmp == 0 && namelen == reginfo->rootoid_len+1 &&
109             subid == 0 && requests->inclusive)) {
110             reqinfo->mode = MODE_GET;
111             memcpy(build_space, reginfo->rootoid,
112                    reginfo->rootoid_len * sizeof(oid));
113             build_space[reginfo->rootoid_len] = 0;  /* add the instance subid */
114
115             snmp_set_var_objid(requests->requestvb, build_space,
116                                reginfo->rootoid_len+1);
117             ret =
118                 netsnmp_call_next_handler(handler, reginfo, reqinfo,
119                                           requests);
120             reqinfo->mode = MODE_GETNEXT;
121             return ret;
122         } else {
123             return SNMP_ERR_NOERROR;
124         }
125         break;
126     }
127     /*
128      * got here only if illegal mode found 
129      */
130     return SNMP_ERR_GENERR;
131 }
132
133 /*
134  * @} 
135  */