summaryrefslogtreecommitdiff
path: root/agent/mibgroup/disman/expr/expObject.c
blob: c27437ba344552ecde7d266c0317a27f1ac02b8c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
/*
 * DisMan Expression MIB:
 *    Core implementation of expression object handling
 */

#include <net-snmp/net-snmp-config.h>
#include <net-snmp/net-snmp-includes.h>
#include <net-snmp/agent/net-snmp-agent-includes.h>
#include "disman/expr/expObject.h"
#include "disman/expr/expExpression.h"

netsnmp_tdata *expObject_table_data;

    /*
     * Initializes the container for the expression object table,
     * regardless of which module is initialised first.
     */
void
init_expObject_table_data(void)
{
    DEBUGMSGTL(("disman:expr:init", "init expObject container\n"));
    if (!expObject_table_data) {
         expObject_table_data = netsnmp_tdata_create_table("expObjectTable", 0);
         DEBUGMSGTL(("disman:expr:init", "create expObject container (%p)\n",
                                          expObject_table_data));
    }
}

/* Initialize the expObject module */
void
init_expObject(void)
{
    init_expObject_table_data();
}


/*
 * Create a new row in the object table 
 */
struct expObject *
expObject_createEntry(const char *expOwner, const char *expName, long expIndex, int fixed)
{
    netsnmp_tdata_row *row;

    row = expObject_createRow(expOwner, expName, expIndex, fixed);
    return row ? (struct expObject *)row->data : NULL;
}

netsnmp_tdata_row *
expObject_createRow( const char *expOwner, const char *expName, long expIndex, int fixed)
{
    struct expObject  *entry;
    netsnmp_tdata_row *row;
    size_t expOwner_len = (expOwner) ? strlen(expOwner) : 0;
    size_t expName_len  = (expName)  ? strlen(expName)  : 0;

    /*
     * Create the expObject entry, and the
     * (table-independent) row wrapper structure...
     */
    entry = SNMP_MALLOC_TYPEDEF(struct expObject);
    if (!entry)
        return NULL;

    row = netsnmp_tdata_create_row();
    if (!row) {
        SNMP_FREE(entry);
        return NULL;
    }
    row->data = entry;

    /*
     * ... initialize this row with the indexes supplied
     *     and the default values for the row...
     */
    if (expOwner)
        memcpy(entry->expOwner, expOwner, expOwner_len);
    netsnmp_tdata_row_add_index(row, ASN_OCTET_STR,
                                entry->expOwner, expOwner_len);
    if (expName)
        memcpy(entry->expName,  expName,  expName_len);
    netsnmp_tdata_row_add_index(row, ASN_OCTET_STR,
                                entry->expName, expName_len);
    entry->expObjectIndex = expIndex;
    netsnmp_tdata_row_add_index(row, ASN_INTEGER,
                               &entry->expObjectIndex, sizeof(long));

    entry->expObjectSampleType     = 1;  /* absoluteValue */
    entry->expObjDiscontinuityType = 1;  /* timeTicks     */
    if (fixed)
        entry->flags |= EXP_OBJ_FLAG_FIXED;

    /*
     * ... and insert the row into the table container.
     */
    netsnmp_tdata_add_row(expObject_table_data, row);
    return row;
}

/*
 * Remove a row from the expression object table 
 */
void
expObject_removeEntry(netsnmp_tdata_row * row)
{
    struct expObject *entry;

    if (!row)
        return;                 /* Nothing to remove */
    entry = (struct expObject *)
        netsnmp_tdata_remove_and_delete_row(expObject_table_data, row);
    if (entry) {
        if (entry->vars      ) snmp_free_varbind( entry->vars      );
        if (entry->old_vars  ) snmp_free_varbind( entry->old_vars  );
        if (entry->dvars     ) snmp_free_varbind( entry->dvars     );
        if (entry->old_dvars ) snmp_free_varbind( entry->old_dvars );
        if (entry->cvars     ) snmp_free_varbind( entry->cvars     );
        SNMP_FREE(entry);
    }
}


netsnmp_tdata_row *
expObject_getFirst( const char *expOwner, const char *expName )
{
    netsnmp_tdata_row *row;
    struct expObject  *entry;
    netsnmp_variable_list owner_var;
    netsnmp_variable_list  name_var;

    if (!expOwner || !expName)
        return NULL;

    /*
     * Find the first object entry that could potentially
     *   refer to the specified expression...
     */
    memset(&owner_var, 0, sizeof(netsnmp_variable_list));
    memset(&name_var,  0, sizeof(netsnmp_variable_list));
    snmp_set_var_typed_value( &owner_var, ASN_OCTET_STR,
                       (const u_char*)expOwner, strlen(expOwner));
    snmp_set_var_typed_value( &name_var,  ASN_OCTET_STR,
                       (const u_char*)expName,  strlen(expName));
    owner_var.next_variable = &name_var;
    row = netsnmp_tdata_row_next_byidx( expObject_table_data, &owner_var );

    /*
     * ... and check that it does!
     */
    if (!row || !row->data)
        return NULL;
    entry = (struct expObject *)row->data;

    if ((strcmp( entry->expOwner, expOwner ) != 0) ||
        (strcmp( entry->expName,  expName  ) != 0))
        return NULL;

    return row;
}

netsnmp_tdata_row *
expObject_getNext( netsnmp_tdata_row *thisRow )
{
    struct expObject  *thisEntry;
    struct expObject  *nextEntry;
    netsnmp_tdata_row *nextRow;

    if (!thisRow || !thisRow->data)
        return NULL;
    thisEntry = (struct expObject *)thisRow->data;

    /*
     * Retrieve the next row, and check whether this
     *   refers to the same expression too.
     */
    nextRow = netsnmp_tdata_row_next( expObject_table_data, thisRow );

    if (!nextRow || !nextRow->data)
        return NULL;
    nextEntry = (struct expObject *)nextRow->data;

    if ((strcmp( nextEntry->expOwner, thisEntry->expOwner ) != 0) ||
        (strcmp( nextEntry->expName,  thisEntry->expName  ) != 0))
        return NULL;

    return nextRow;
}


netsnmp_variable_list *
_expObject_buildList( oid *root, size_t root_len, size_t prefix_len,
                      netsnmp_variable_list *template_list )
{
    netsnmp_variable_list *query_list = NULL;
    netsnmp_variable_list *vp1,  *vp2 = NULL;
    oid    name[ MAX_OID_LEN ];
    int i;

    if ( prefix_len ) {
        /*
         * For wildcarded objects, build a list of all relevant
         *   instances, using the template_list to provide the
         *   necessary instance suffixes.
         */
        memset( name, 0, sizeof(name));
        memcpy( name, root, root_len*sizeof(oid));
        for ( vp1 = template_list; vp1; vp1=vp1->next_variable ) {
            /*
             * Append a new varbind to the list for this object ...
             */
            if ( !query_list ) {
                query_list = (netsnmp_variable_list*)
                                 SNMP_MALLOC_TYPEDEF( netsnmp_variable_list );
                vp2 = query_list;
            } else {
                vp2->next_variable = (netsnmp_variable_list*)
                                 SNMP_MALLOC_TYPEDEF( netsnmp_variable_list );
                vp2 = vp2->next_variable;
            }
            /*
             * ... and set the OID using the template suffix
             */
            for ( i=0; i <= vp1->name_length - prefix_len; i++)
                name[ root_len+i ] = vp1->name[ prefix_len+i ];
            snmp_set_var_objid( vp2, name, root_len+i );
        }
    } else {
        /*
         * Otherwise, just use the (single) OID provided.
         */
        query_list = (netsnmp_variable_list*)
                                 SNMP_MALLOC_TYPEDEF( netsnmp_variable_list );
        snmp_set_var_objid( query_list, root, root_len );
    }
    return query_list;
}


void
expObject_getData( struct expExpression  *expr, struct expObject  *obj )
{
    netsnmp_variable_list *var;

    /*
     * Retrieve and store the basic object value(s)
     *   (keeping the previous values if necessary)
     */
    if (obj->flags & EXP_OBJ_FLAG_PREFIX ) {
        /*
         * If this is the expExpressionPrefix object, then
         *   we already have the necessary list of values.
         *   There's no need to retrieve it again.
         * This also takes care of releasing the prefix list
         *   once the results are no longer needed.
         */
        var = expr->pvars;
    } else {
        if (!(obj->flags & EXP_OBJ_FLAG_OWILD ))
            /*
             * Set up the request 'list' for an
             *   exact (non-wildcarded) object.
             */
            var = _expObject_buildList( obj->expObjectID,
                                        obj->expObjectID_len, 0, NULL );
        else {
            if ( !expr->expPrefix_len ) {
                /*
                 * You can't really have wildcarded objects unless
                 *   the expression as a whole is wildcarded too.
                 */
                return;
            }
            /*
             * Set up the request list for a wildcarded object
             */
            var = _expObject_buildList( obj->expObjectID,
                                        obj->expObjectID_len,
                                       expr->expPrefix_len,
                                       expr->pvars );
        }
        netsnmp_query_get( var, expr->session );
    }
    
    if ( obj->expObjectSampleType != EXPSAMPLETYPE_ABSOLUTE ) {
        /*
         * For Delta (and Changed) samples, we need
         *   to store the previous value as well.
         */
        if ( obj->old_vars )
            snmp_free_varbind( obj->old_vars );
        obj->old_vars = obj->vars;
    } else
        snmp_free_varbind( obj->vars );

    obj->vars = var;


    /*
     * For Delta samples, there may be a discontinuity marker
     *   (or set of wildcarded markers) to be sampled as well.
     *   This necessarily requires storing the previous marker(s).
     */
    if (( obj->expObjectSampleType != EXPSAMPLETYPE_ABSOLUTE ) &&
        ( obj->flags & EXP_OBJ_FLAG_DDISC )) {

        if ( obj->flags & EXP_OBJ_FLAG_DWILD )
            var = _expObject_buildList( obj->expObjDeltaD,
                                        obj->expObjDeltaD_len,
                                       expr->expPrefix_len,
                                       expr->pvars );
        else
            var = _expObject_buildList( obj->expObjDeltaD,
                                        obj->expObjDeltaD_len, 0, NULL );
        netsnmp_query_get( var, expr->session );
        if ( obj->old_dvars )
            snmp_free_varbind( obj->old_dvars );
        obj->old_dvars = obj->dvars;
        obj->dvars     = var;
    }

    /*
     * If there's an expObjectConditional value specified
     *   (or set of wildcarded values) then add these to the
     *   ever-growing collection of retrieved values.
     */
    if ( obj->expObjCond_len ) {
        if ( obj->flags & EXP_OBJ_FLAG_CWILD )
            var = _expObject_buildList( obj->expObjCond,
                                        obj->expObjCond_len,
                                       expr->expPrefix_len,
                                       expr->pvars );
        else
            var = _expObject_buildList( obj->expObjCond,
                                        obj->expObjCond_len, 0, NULL );
        /*
         * XXX - Check when to use GetNext
         *
         *    (The MIB description seems bogus?)
         */
        netsnmp_query_get( var, expr->session );
        if ( obj->cvars )
            snmp_free_varbind( obj->cvars );
        obj->cvars = var;
    }
}