Debian Codegraph @

agent/mibgroup/disman/expr/expValueTable.c

raw
1 /*
2 * DisMan Expression MIB:
3 * Implementation of the expValueTable MIB interface
4 * See 'expValue.c' for active evaluation of expressions.
5 *
6 * (Based roughly on mib2c.raw-table.conf output)
7 */
8
9 #include <net-snmp/net-snmp-config.h>
10 #include <net-snmp/net-snmp-includes.h>
11 #include <net-snmp/agent/net-snmp-agent-includes.h>
12 #include "expValue.h"
13 #include "expValueTable.h"
14
15 static Netsnmp_Node_Handler expValueTable_handler;
16 static netsnmp_handler_registration *value_table_reg;
17 static netsnmp_table_registration_info *value_table_info;
18
19 /** Initializes the expValueTable module */
20 void
21 init_expValueTable(void)
22 {
23 static oid expValueTable_oid[] = { 1, 3, 6, 1, 2, 1, 90, 1, 3, 1 };
24 size_t expValueTable_oid_len = OID_LENGTH(expValueTable_oid);
25
26 value_table_reg =
27 netsnmp_create_handler_registration("expValueTable",
28 expValueTable_handler,
29 expValueTable_oid,
30 expValueTable_oid_len,
31 HANDLER_CAN_RONLY);
32
33 value_table_info = SNMP_MALLOC_TYPEDEF(netsnmp_table_registration_info);
34 netsnmp_table_helper_add_indexes(value_table_info,
35 ASN_OCTET_STR, /* expExpressionOwner */
36 ASN_OCTET_STR, /* expExpressionName */
37 /* expValueInstance */
38 ASN_PRIV_IMPLIED_OBJECT_ID,
39 0);
40
41 value_table_info->min_column = COLUMN_EXPVALUECOUNTER32VAL;
42 value_table_info->max_column = COLUMN_EXPVALUECOUNTER64VAL;
43
44 netsnmp_register_table(value_table_reg, value_table_info);
45 DEBUGMSGTL(("disman:expr:init", "Expression Value Table\n"));
46 }
47
48 void
49 shutdown_expValueTable(void)
50 {
51 netsnmp_tdata_unregister(value_table_reg);
52 netsnmp_table_registration_info_free(value_table_info);
53 }
54
55 netsnmp_variable_list *
56 expValueTable_getEntry(netsnmp_variable_list * indexes,
57 int mode, unsigned int colnum)
58 {
59 struct expExpression *exp;
60 netsnmp_variable_list *res, *vp, *vp2;
61 oid nullInstance[] = {0, 0, 0};
62 int plen;
63 size_t len;
64 unsigned int type = colnum-1; /* column object subIDs and type
65 enumerations are off by one. */
66
67 if (!indexes || !indexes->next_variable ||
68 !indexes->next_variable->next_variable ) {
69 /* XXX - Shouldn't happen! */
70 return NULL;
71 }
72
73 DEBUGMSGTL(( "disman:expr:val", "get (%d) entry (%s, %s, ", mode,
74 indexes->val.string, indexes->next_variable->val.string));
75 DEBUGMSGOID(("disman:expr:val",
76 indexes->next_variable->next_variable->val.objid,
77 indexes->next_variable->next_variable->val_len/sizeof(oid)));
78 DEBUGMSG(( "disman:expr:val", ")\n"));
79
80 /*
81 * Locate the expression that we've been asked to evaluate
82 */
83 if (!indexes->val_len || !indexes->next_variable->val_len ) {
84 /*
85 * Incomplete expression specification
86 */
87 if (mode == MODE_GETNEXT || mode == MODE_GETBULK) {
88 exp = expExpression_getFirstEntry();
89 DEBUGMSGTL(( "disman:expr:val", "first entry (%p)\n", exp ));
90 } else {
91 DEBUGMSGTL(( "disman:expr:val", "incomplete request\n" ));
92 return NULL; /* No match */
93 }
94 } else {
95 exp = expExpression_getEntry( (char*)indexes->val.string,
96 (char*)indexes->next_variable->val.string);
97 DEBUGMSGTL(( "disman:expr:val", "using entry (%p)\n", exp ));
98 }
99
100 /*
101 * We know what type of value was requested,
102 * so ignore any non-matching expressions.
103 */
104 while (exp && exp->expValueType != type) {
105 if (mode != MODE_GETNEXT && mode != MODE_GETBULK) {
106 DEBUGMSGTL(( "disman:expr:val", "wrong type (%d != %ld)\n",
107 type, (exp ? exp->expValueType : 0 )));
108 return NULL; /* Wrong type */
109 }
110 NEXT_EXP:
111 exp = expExpression_getNextEntry( exp->expOwner, exp->expName );
112 DEBUGMSGTL(( "disman:expr:val", "using next entry (%p)\n", exp ));
113 }
114 if (!exp) {
115 DEBUGMSGTL(( "disman:expr:val", "no more entries\n"));
116 return NULL; /* No match (of the required type) */
117 }
118
119
120 /*
121 * Now consider which instance of the chosen expression is needed
122 */
123 vp = indexes->next_variable->next_variable;
124 if ( mode == MODE_GET ) {
125 /*
126 * For a GET request, check that the specified value instance
127 * is valid, and evaluate the expression using this.
128 */
129 if ( !vp || !vp->val_len ) {
130 DEBUGMSGTL(( "disman:expr:val", "no instance index\n"));
131 return NULL; /* No instance provided */
132 }
133 if ( vp->val.objid[0] != 0 ) {
134 DEBUGMSGTL(( "disman:expr:val",
135 "non-zero instance (%" NETSNMP_PRIo "d)\n", vp->val.objid[0]));
136 return NULL; /* Invalid instance */
137 }
138
139 if (exp->expPrefix_len == 0 ) {
140 /*
141 * The only valid instance for a non-wildcarded
142 * expression is .0.0.0
143 */
144 if ( vp->val_len != 3*sizeof(oid) ||
145 vp->val.objid[1] != 0 ||
146 vp->val.objid[2] != 0 ) {
147 DEBUGMSGTL(( "disman:expr:val", "invalid scalar instance\n"));
148 return NULL;
149 }
150 res = expValue_evaluateExpression( exp, NULL, 0 );
151 DEBUGMSGTL(( "disman:expr:val", "scalar get returned (%p)\n", res));
152 } else {
153 /*
154 * Otherwise, skip the leading '.0' and use
155 * the remaining instance subidentifiers.
156 */
157 res = expValue_evaluateExpression( exp, vp->val.objid+1,
158 vp->val_len/sizeof(oid)-1);
159 DEBUGMSGTL(( "disman:expr:val", "w/card get returned (%p)\n", res));
160 }
161 } else {
162 /*
163 * For a GETNEXT request, identify the appropriate next
164 * value instance, and evaluate the expression using
165 * that, updating the index list appropriately.
166 */
167 if ( vp->val_len > 0 && vp->val.objid[0] != 0 ) {
168 DEBUGMSGTL(( "disman:expr:val",
169 "non-zero next instance (%" NETSNMP_PRIo "d)\n", vp->val.objid[0]));
170 return NULL; /* All valid instances start with .0 */
171 }
172 plen = exp->expPrefix_len;
173 if (plen == 0 ) {
174 /*
175 * The only valid instances for GETNEXT on a
176 * non-wildcarded expression are .0 and .0.0
177 * Anything else is too late.
178 */
179 if ((vp->val_len > 2*sizeof(oid)) ||
180 (vp->val_len == 2*sizeof(oid) &&
181 vp->val.objid[1] != 0)) {
182 DEBUGMSGTL(( "disman:expr:val", "invalid scalar next instance\n"));
183 return NULL; /* Invalid instance */
184 }
185
186 /*
187 * Make sure the index varbind list refers to the
188 * (only) valid instance of this expression,
189 * and evaluate it.
190 */
191 snmp_set_var_typed_value( indexes, ASN_OCTET_STR,
192 (u_char*)exp->expOwner, strlen(exp->expOwner));
193 snmp_set_var_typed_value( indexes->next_variable, ASN_OCTET_STR,
194 (u_char*)exp->expName, strlen(exp->expName));
195 snmp_set_var_typed_value( vp, ASN_PRIV_IMPLIED_OBJECT_ID,
196 (u_char*)nullInstance, 3*sizeof(oid));
197 res = expValue_evaluateExpression( exp, NULL, 0 );
198 DEBUGMSGTL(( "disman:expr:val", "scalar next returned (%p)\n", res));
199 } else {
200 /*
201 * Now comes the interesting case - finding the
202 * appropriate instance of a wildcarded expression.
203 */
204 if ( vp->val_len == 0 ) {
205 if ( !exp->pvars ) {
206 DEBUGMSGTL(( "disman:expr:val", "no instances\n"));
207 goto NEXT_EXP;
208 }
209 DEBUGMSGTL(( "disman:expr:val", "using first instance\n"));
210 vp2 = exp->pvars;
211 } else {
212 /*
213 * Search the list of instances for the (first) greater one
214 * XXX - This comparison relies on the OID of the prefix
215 * object being the same length as the wildcarded
216 * parameter objects. It ain't necessarily so.
217 */
218 for ( vp2 = exp->pvars; vp2; vp2 = vp2->next_variable ) {
219 if ( snmp_oid_compare( vp2->name + plen,
220 vp2->name_length - plen,
221 vp->name,
222 vp->name_length) < 0 ) {
223 DEBUGMSGTL(( "disman:expr:val", "next instance "));
224 DEBUGMSGOID(("disman:expr:val", vp2->name, vp2->name_length ));
225 DEBUGMSG(( "disman:expr:val", "\n"));
226 break;
227 }
228 }
229 if (!vp2) {
230 DEBUGMSGTL(( "disman:expr:val", "no next instance\n"));
231 goto NEXT_EXP;
232 }
233 }
234 snmp_set_var_typed_value( indexes, ASN_OCTET_STR,
235 (u_char*)exp->expOwner, strlen(exp->expOwner));
236 snmp_set_var_typed_value( indexes->next_variable, ASN_OCTET_STR,
237 (u_char*)exp->expName, strlen(exp->expName));
238 if (vp2) {
239 len = vp2->name_length - exp->expPrefix_len;
240 snmp_set_var_typed_value( vp, ASN_PRIV_IMPLIED_OBJECT_ID,
241 (u_char*)(vp2->name+exp->expPrefix_len), len);
242 } else {
243 len = 1;
244 }
245 res = expValue_evaluateExpression( exp, vp->val.objid+1, len-1);
246 DEBUGMSGTL(( "disman:expr:val", "w/card next returned (%p)\n", res));
247 }
248 }
249 return res;
250 }
251
252 /** handles requests for the expValueTable table */
253 int
254 expValueTable_handler(netsnmp_mib_handler *handler,
255 netsnmp_handler_registration *reginfo,
256 netsnmp_agent_request_info *reqinfo,
257 netsnmp_request_info *requests)
258 {
259
260 netsnmp_request_info *request;
261 netsnmp_table_request_info *tinfo;
262 netsnmp_variable_list *value;
263 oid expValueOID[] = { 1, 3, 6, 1, 2, 1, 90, 1, 3, 1, 1, 99 };
264 size_t expValueOID_len = OID_LENGTH(expValueOID);
265 oid name_buf[ MAX_OID_LEN ];
266 size_t name_buf_len = MAX_OID_LEN;
267
268 DEBUGMSGTL(("disman:expr:mib", "Expression Value Table handler (%d)\n",
269 reqinfo->mode));
270 switch (reqinfo->mode) {
271 case MODE_GET:
272 case MODE_GETNEXT:
273 for (request = requests; request; request = request->next) {
274 tinfo = netsnmp_extract_table_info(request);
275 value = expValueTable_getEntry(tinfo->indexes,
276 reqinfo->mode,
277 tinfo->colnum);
278 if (!value || !value->val.integer) {
279 netsnmp_set_request_error(reqinfo, request,
280 (reqinfo->mode == MODE_GET) ?
281 SNMP_NOSUCHINSTANCE :
282 SNMP_ENDOFMIBVIEW);
283 continue;
284 }
285 if ( reqinfo->mode == MODE_GETNEXT ) {
286 /*
287 * Need to update the request varbind OID
288 * to match the instance just evaluated.
289 * (XXX - Is this the appropriate mechanism?)
290 */
291 build_oid_noalloc( name_buf, MAX_OID_LEN, &name_buf_len,
292 expValueOID, expValueOID_len, tinfo->indexes );
293 name_buf[ expValueOID_len -1 ] = tinfo->colnum;
294 snmp_set_var_objid(request->requestvb, name_buf, name_buf_len);
295 }
296
297 switch (tinfo->colnum) {
298 case COLUMN_EXPVALUECOUNTER32VAL:
299 snmp_set_var_typed_integer(request->requestvb, ASN_COUNTER,
300 *value->val.integer);
301 break;
302 case COLUMN_EXPVALUEUNSIGNED32VAL:
303 snmp_set_var_typed_integer(request->requestvb, ASN_UNSIGNED,
304 *value->val.integer);
305 break;
306 case COLUMN_EXPVALUETIMETICKSVAL:
307 snmp_set_var_typed_integer(request->requestvb, ASN_TIMETICKS,
308 *value->val.integer);
309 break;
310 case COLUMN_EXPVALUEINTEGER32VAL:
311 snmp_set_var_typed_integer(request->requestvb, ASN_INTEGER,
312 *value->val.integer);
313 break;
314 case COLUMN_EXPVALUEIPADDRESSVAL:
315 snmp_set_var_typed_integer(request->requestvb, ASN_IPADDRESS,
316 *value->val.integer);
317 break;
318 case COLUMN_EXPVALUEOCTETSTRINGVAL:
319 snmp_set_var_typed_value( request->requestvb, ASN_OCTET_STR,
320 value->val.string, value->val_len);
321 break;
322 case COLUMN_EXPVALUEOIDVAL:
323 snmp_set_var_typed_value( request->requestvb, ASN_OBJECT_ID,
324 (u_char *)value->val.objid, value->val_len);
325 break;
326 case COLUMN_EXPVALUECOUNTER64VAL:
327 snmp_set_var_typed_value( request->requestvb, ASN_COUNTER64,
328 (u_char *)value->val.counter64, value->val_len);
329 break;
330 }
331 /* remember to free the variable list, or we will leak here.. */
332 snmp_free_varbind(value);
333 }
334 break;
335
336 }
337 DEBUGMSGTL(("disman:expr:mib", "Expression Value handler - done \n"));
338 return SNMP_ERR_NOERROR;
339 }