Debian Codegraph @

agent/mibgroup/disman/expr/expObject.c

raw
1 /*
2 * DisMan Expression MIB:
3 * Core implementation of expression object handling
4 */
5
6 #include <net-snmp/net-snmp-config.h>
7 #include <net-snmp/net-snmp-includes.h>
8 #include <net-snmp/agent/net-snmp-agent-includes.h>
9 #include "disman/expr/expObject.h"
10 #include "disman/expr/expExpression.h"
11
12 netsnmp_tdata *expObject_table_data;
13
14 /*
15 * Initializes the container for the expression object table,
16 * regardless of which module is initialised first.
17 */
18 void
19 init_expObject_table_data(void)
20 {
21 DEBUGMSGTL(("disman:expr:init", "init expObject container\n"));
22 if (!expObject_table_data) {
23 expObject_table_data = netsnmp_tdata_create_table("expObjectTable", 0);
24 DEBUGMSGTL(("disman:expr:init", "create expObject container (%p)\n",
25 expObject_table_data));
26 }
27 }
28
29 /* Initialize the expObject module */
30 void
31 init_expObject(void)
32 {
33 init_expObject_table_data();
34 }
35
36
37 /*
38 * Create a new row in the object table
39 */
40 struct expObject *
41 expObject_createEntry(const char *expOwner, const char *expName, long expIndex, int fixed)
42 {
43 netsnmp_tdata_row *row;
44
45 row = expObject_createRow(expOwner, expName, expIndex, fixed);
46 return row ? (struct expObject *)row->data : NULL;
47 }
48
49 netsnmp_tdata_row *
50 expObject_createRow( const char *expOwner, const char *expName, long expIndex, int fixed)
51 {
52 struct expObject *entry;
53 netsnmp_tdata_row *row;
54 size_t expOwner_len = (expOwner) ? strlen(expOwner) : 0;
55 size_t expName_len = (expName) ? strlen(expName) : 0;
56
57 /*
58 * Create the expObject entry, and the
59 * (table-independent) row wrapper structure...
60 */
61 entry = SNMP_MALLOC_TYPEDEF(struct expObject);
62 if (!entry)
63 return NULL;
64
65 row = netsnmp_tdata_create_row();
66 if (!row) {
67 SNMP_FREE(entry);
68 return NULL;
69 }
70 row->data = entry;
71
72 /*
73 * ... initialize this row with the indexes supplied
74 * and the default values for the row...
75 */
76 if (expOwner)
77 memcpy(entry->expOwner, expOwner, expOwner_len);
78 netsnmp_tdata_row_add_index(row, ASN_OCTET_STR,
79 entry->expOwner, expOwner_len);
80 if (expName)
81 memcpy(entry->expName, expName, expName_len);
82 netsnmp_tdata_row_add_index(row, ASN_OCTET_STR,
83 entry->expName, expName_len);
84 entry->expObjectIndex = expIndex;
85 netsnmp_tdata_row_add_index(row, ASN_INTEGER,
86 &entry->expObjectIndex, sizeof(long));
87
88 entry->expObjectSampleType = 1; /* absoluteValue */
89 entry->expObjDiscontinuityType = 1; /* timeTicks */
90 if (fixed)
91 entry->flags |= EXP_OBJ_FLAG_FIXED;
92
93 /*
94 * ... and insert the row into the table container.
95 */
96 netsnmp_tdata_add_row(expObject_table_data, row);
97 return row;
98 }
99
100 /*
101 * Remove a row from the expression object table
102 */
103 void
104 expObject_removeEntry(netsnmp_tdata_row * row)
105 {
106 struct expObject *entry;
107
108 if (!row)
109 return; /* Nothing to remove */
110 entry = (struct expObject *)
111 netsnmp_tdata_remove_and_delete_row(expObject_table_data, row);
112 if (entry) {
113 if (entry->vars ) snmp_free_varbind( entry->vars );
114 if (entry->old_vars ) snmp_free_varbind( entry->old_vars );
115 if (entry->dvars ) snmp_free_varbind( entry->dvars );
116 if (entry->old_dvars ) snmp_free_varbind( entry->old_dvars );
117 if (entry->cvars ) snmp_free_varbind( entry->cvars );
118 SNMP_FREE(entry);
119 }
120 }
121
122
123 netsnmp_tdata_row *
124 expObject_getFirst( const char *expOwner, const char *expName )
125 {
126 netsnmp_tdata_row *row;
127 struct expObject *entry;
128 netsnmp_variable_list owner_var;
129 netsnmp_variable_list name_var;
130
131 if (!expOwner || !expName)
132 return NULL;
133
134 /*
135 * Find the first object entry that could potentially
136 * refer to the specified expression...
137 */
138 memset(&owner_var, 0, sizeof(netsnmp_variable_list));
139 memset(&name_var, 0, sizeof(netsnmp_variable_list));
140 snmp_set_var_typed_value( &owner_var, ASN_OCTET_STR,
141 (const u_char*)expOwner, strlen(expOwner));
142 snmp_set_var_typed_value( &name_var, ASN_OCTET_STR,
143 (const u_char*)expName, strlen(expName));
144 owner_var.next_variable = &name_var;
145 row = netsnmp_tdata_row_next_byidx( expObject_table_data, &owner_var );
146
147 /*
148 * ... and check that it does!
149 */
150 if (!row || !row->data)
151 return NULL;
152 entry = (struct expObject *)row->data;
153
154 if ((strcmp( entry->expOwner, expOwner ) != 0) ||
155 (strcmp( entry->expName, expName ) != 0))
156 return NULL;
157
158 return row;
159 }
160
161 netsnmp_tdata_row *
162 expObject_getNext( netsnmp_tdata_row *thisRow )
163 {
164 struct expObject *thisEntry;
165 struct expObject *nextEntry;
166 netsnmp_tdata_row *nextRow;
167
168 if (!thisRow || !thisRow->data)
169 return NULL;
170 thisEntry = (struct expObject *)thisRow->data;
171
172 /*
173 * Retrieve the next row, and check whether this
174 * refers to the same expression too.
175 */
176 nextRow = netsnmp_tdata_row_next( expObject_table_data, thisRow );
177
178 if (!nextRow || !nextRow->data)
179 return NULL;
180 nextEntry = (struct expObject *)nextRow->data;
181
182 if ((strcmp( nextEntry->expOwner, thisEntry->expOwner ) != 0) ||
183 (strcmp( nextEntry->expName, thisEntry->expName ) != 0))
184 return NULL;
185
186 return nextRow;
187 }
188
189
190 netsnmp_variable_list *
191 _expObject_buildList( oid *root, size_t root_len, size_t prefix_len,
192 netsnmp_variable_list *template_list )
193 {
194 netsnmp_variable_list *query_list = NULL;
195 netsnmp_variable_list *vp1, *vp2 = NULL;
196 oid name[ MAX_OID_LEN ];
197 int i;
198
199 if ( prefix_len ) {
200 /*
201 * For wildcarded objects, build a list of all relevant
202 * instances, using the template_list to provide the
203 * necessary instance suffixes.
204 */
205 memset( name, 0, sizeof(name));
206 memcpy( name, root, root_len*sizeof(oid));
207 for ( vp1 = template_list; vp1; vp1=vp1->next_variable ) {
208 /*
209 * Append a new varbind to the list for this object ...
210 */
211 if ( !query_list ) {
212 query_list = (netsnmp_variable_list*)
213 SNMP_MALLOC_TYPEDEF( netsnmp_variable_list );
214 vp2 = query_list;
215 } else {
216 vp2->next_variable = (netsnmp_variable_list*)
217 SNMP_MALLOC_TYPEDEF( netsnmp_variable_list );
218 vp2 = vp2->next_variable;
219 }
220 /*
221 * ... and set the OID using the template suffix
222 */
223 for ( i=0; i <= vp1->name_length - prefix_len; i++)
224 name[ root_len+i ] = vp1->name[ prefix_len+i ];
225 snmp_set_var_objid( vp2, name, root_len+i );
226 }
227 } else {
228 /*
229 * Otherwise, just use the (single) OID provided.
230 */
231 query_list = (netsnmp_variable_list*)
232 SNMP_MALLOC_TYPEDEF( netsnmp_variable_list );
233 snmp_set_var_objid( query_list, root, root_len );
234 }
235 return query_list;
236 }
237
238
239 void
240 expObject_getData( struct expExpression *expr, struct expObject *obj )
241 {
242 netsnmp_variable_list *var;
243
244 /*
245 * Retrieve and store the basic object value(s)
246 * (keeping the previous values if necessary)
247 */
248 if (obj->flags & EXP_OBJ_FLAG_PREFIX ) {
249 /*
250 * If this is the expExpressionPrefix object, then
251 * we already have the necessary list of values.
252 * There's no need to retrieve it again.
253 * This also takes care of releasing the prefix list
254 * once the results are no longer needed.
255 */
256 var = expr->pvars;
257 } else {
258 if (!(obj->flags & EXP_OBJ_FLAG_OWILD ))
259 /*
260 * Set up the request 'list' for an
261 * exact (non-wildcarded) object.
262 */
263 var = _expObject_buildList( obj->expObjectID,
264 obj->expObjectID_len, 0, NULL );
265 else {
266 if ( !expr->expPrefix_len ) {
267 /*
268 * You can't really have wildcarded objects unless
269 * the expression as a whole is wildcarded too.
270 */
271 return;
272 }
273 /*
274 * Set up the request list for a wildcarded object
275 */
276 var = _expObject_buildList( obj->expObjectID,
277 obj->expObjectID_len,
278 expr->expPrefix_len,
279 expr->pvars );
280 }
281 netsnmp_query_get( var, expr->session );
282 }
283
284 if ( obj->expObjectSampleType != EXPSAMPLETYPE_ABSOLUTE ) {
285 /*
286 * For Delta (and Changed) samples, we need
287 * to store the previous value as well.
288 */
289 if ( obj->old_vars )
290 snmp_free_varbind( obj->old_vars );
291 obj->old_vars = obj->vars;
292 } else
293 snmp_free_varbind( obj->vars );
294
295 obj->vars = var;
296
297
298 /*
299 * For Delta samples, there may be a discontinuity marker
300 * (or set of wildcarded markers) to be sampled as well.
301 * This necessarily requires storing the previous marker(s).
302 */
303 if (( obj->expObjectSampleType != EXPSAMPLETYPE_ABSOLUTE ) &&
304 ( obj->flags & EXP_OBJ_FLAG_DDISC )) {
305
306 if ( obj->flags & EXP_OBJ_FLAG_DWILD )
307 var = _expObject_buildList( obj->expObjDeltaD,
308 obj->expObjDeltaD_len,
309 expr->expPrefix_len,
310 expr->pvars );
311 else
312 var = _expObject_buildList( obj->expObjDeltaD,
313 obj->expObjDeltaD_len, 0, NULL );
314 netsnmp_query_get( var, expr->session );
315 if ( obj->old_dvars )
316 snmp_free_varbind( obj->old_dvars );
317 obj->old_dvars = obj->dvars;
318 obj->dvars = var;
319 }
320
321 /*
322 * If there's an expObjectConditional value specified
323 * (or set of wildcarded values) then add these to the
324 * ever-growing collection of retrieved values.
325 */
326 if ( obj->expObjCond_len ) {
327 if ( obj->flags & EXP_OBJ_FLAG_CWILD )
328 var = _expObject_buildList( obj->expObjCond,
329 obj->expObjCond_len,
330 expr->expPrefix_len,
331 expr->pvars );
332 else
333 var = _expObject_buildList( obj->expObjCond,
334 obj->expObjCond_len, 0, NULL );
335 /*
336 * XXX - Check when to use GetNext
337 *
338 * (The MIB description seems bogus?)
339 */
340 netsnmp_query_get( var, expr->session );
341 if ( obj->cvars )
342 snmp_free_varbind( obj->cvars );
343 obj->cvars = var;
344 }
345 }