~ubuntu-branches/ubuntu/trusty/net-snmp/trusty

« back to all changes in this revision

Viewing changes to agent/mibgroup/host/hr_device.c

  • Committer: Bazaar Package Importer
  • Author(s): Martin Pitt
  • Date: 2004-09-13 12:06:21 UTC
  • Revision ID: james.westby@ubuntu.com-20040913120621-g952ntonlleihcvm
Tags: upstream-5.1.1
ImportĀ upstreamĀ versionĀ 5.1.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 *  Host Resources MIB - Device group implementation - hr_device.c
 
3
 *
 
4
 */
 
5
/* Portions of this file are subject to the following copyright(s).  See
 
6
 * the Net-SNMP's COPYING file for more details and other copyrights
 
7
 * that may apply:
 
8
 */
 
9
/*
 
10
 * Portions of this file are copyrighted by:
 
11
 * Copyright ļæ½ 2003 Sun Microsystems, Inc. All rights reserved.
 
12
 * Use is subject to license terms specified in the COPYING file
 
13
 * distributed with the Net-SNMP package.
 
14
 */
 
15
 
 
16
#include <net-snmp/net-snmp-config.h>
 
17
#if HAVE_STRING_H
 
18
#include <string.h>
 
19
#else
 
20
#include <strings.h>
 
21
#endif
 
22
 
 
23
#include "host_res.h"
 
24
#include "hr_device.h"
 
25
 
 
26
        /*********************
 
27
         *
 
28
         *  Kernel & interface information,
 
29
         *   and internal forward declarations
 
30
         *
 
31
         *********************/
 
32
 
 
33
int             Get_Next_Device(void);
 
34
 
 
35
PFV             init_device[HRDEV_TYPE_MAX];
 
36
PFIV            next_device[HRDEV_TYPE_MAX];
 
37
PFV             save_device[HRDEV_TYPE_MAX];
 
38
int             dev_idx_inc[HRDEV_TYPE_MAX];
 
39
 
 
40
PFS             device_descr[HRDEV_TYPE_MAX];
 
41
PFO             device_prodid[HRDEV_TYPE_MAX];
 
42
PFI             device_status[HRDEV_TYPE_MAX];
 
43
PFI             device_errors[HRDEV_TYPE_MAX];
 
44
 
 
45
int             current_type;
 
46
 
 
47
void            Init_Device(void);
 
48
int             header_hrdevice(struct variable *, oid *, size_t *, int,
 
49
                                size_t *, WriteMethod **);
 
50
 
 
51
 
 
52
        /*********************
 
53
         *
 
54
         *  Initialisation & common implementation functions
 
55
         *
 
56
         *********************/
 
57
 
 
58
#define HRDEV_INDEX             1
 
59
#define HRDEV_TYPE              2
 
60
#define HRDEV_DESCR             3
 
61
#define HRDEV_ID                4
 
62
#define HRDEV_STATUS            5
 
63
#define HRDEV_ERRORS            6
 
64
 
 
65
struct variable4 hrdevice_variables[] = {
 
66
    {HRDEV_INDEX, ASN_INTEGER, RONLY, var_hrdevice, 2, {1, 1}},
 
67
    {HRDEV_TYPE, ASN_OBJECT_ID, RONLY, var_hrdevice, 2, {1, 2}},
 
68
    {HRDEV_DESCR, ASN_OCTET_STR, RONLY, var_hrdevice, 2, {1, 3}},
 
69
    {HRDEV_ID, ASN_OBJECT_ID, RONLY, var_hrdevice, 2, {1, 4}},
 
70
    {HRDEV_STATUS, ASN_INTEGER, RONLY, var_hrdevice, 2, {1, 5}},
 
71
    {HRDEV_ERRORS, ASN_COUNTER, RONLY, var_hrdevice, 2, {1, 6}}
 
72
};
 
73
oid             hrdevice_variables_oid[] = { 1, 3, 6, 1, 2, 1, 25, 3, 2 };
 
74
 
 
75
 
 
76
void
 
77
init_hr_device(void)
 
78
{
 
79
    int             i;
 
80
 
 
81
    /*
 
82
     * Initially assume no devices
 
83
     *    Insert pointers to initialisation/get_next routines
 
84
     *    for particular device types as they are implemented
 
85
     *      (set up in the appropriate 'init_*()' routine )
 
86
     */
 
87
 
 
88
    for (i = 0; i < HRDEV_TYPE_MAX; ++i) {
 
89
        init_device[i] = NULL;
 
90
        next_device[i] = NULL;
 
91
        save_device[i] = NULL;
 
92
        dev_idx_inc[i] = 0;     /* Assume random indices */
 
93
 
 
94
        device_descr[i] = NULL;
 
95
        device_prodid[i] = NULL;
 
96
        device_status[i] = NULL;
 
97
        device_errors[i] = NULL;
 
98
    }
 
99
 
 
100
    REGISTER_MIB("host/hr_device", hrdevice_variables, variable4,
 
101
                 hrdevice_variables_oid);
 
102
}
 
103
 
 
104
 
 
105
/*
 
106
 * header_hrdevice(...
 
107
 * Arguments:
 
108
 * vp     IN      - pointer to variable entry that points here
 
109
 * name    IN/OUT  - IN/name requested, OUT/name found
 
110
 * length  IN/OUT  - length of IN/OUT oid's 
 
111
 * exact   IN      - TRUE if an exact match was requested
 
112
 * var_len OUT     - length of variable or 0 if function returned
 
113
 * write_method
 
114
 * 
 
115
 */
 
116
 
 
117
int
 
118
header_hrdevice(struct variable *vp,
 
119
                oid * name,
 
120
                size_t * length,
 
121
                int exact, size_t * var_len, WriteMethod ** write_method)
 
122
{
 
123
#define HRDEV_ENTRY_NAME_LENGTH 11
 
124
    oid             newname[MAX_OID_LEN];
 
125
    int             dev_idx, LowIndex = -1, LowType = -1;
 
126
    int             result;
 
127
 
 
128
    DEBUGMSGTL(("host/hr_device", "var_hrdevice: "));
 
129
    DEBUGMSGOID(("host/hr_device", name, *length));
 
130
    DEBUGMSG(("host/hr_device", " %d\n", exact));
 
131
 
 
132
    memcpy((char *) newname, (char *) vp->name,
 
133
           (int) vp->namelen * sizeof(oid));
 
134
 
 
135
 
 
136
    /*
 
137
     *  Find the "next" device entry.
 
138
     *  If we're in the middle of the table, then there's
 
139
     *     no point in examining earlier types of devices,
 
140
     *     so set the starting type to that of the variable
 
141
     *     being queried.
 
142
     *  If we've moved from one column of the table to another,
 
143
     *     then we need to start at the beginning again.
 
144
     *     (i.e. the 'compare' fails to match)
 
145
     *  Similarly if we're at the start of the table
 
146
     *     (i.e. *length is too short to be a full instance)
 
147
     */
 
148
 
 
149
    if ((snmp_oid_compare(vp->name, vp->namelen, name, vp->namelen) == 0)
 
150
        && (*length > HRDEV_ENTRY_NAME_LENGTH))
 
151
        current_type = (name[HRDEV_ENTRY_NAME_LENGTH] >> HRDEV_TYPE_SHIFT);
 
152
    else
 
153
        current_type = 0;
 
154
 
 
155
    Init_Device();
 
156
    for (;;) {
 
157
        dev_idx = Get_Next_Device();
 
158
        DEBUGMSG(("host/hr_device", "(index %d ....", dev_idx));
 
159
        if (dev_idx == -1)
 
160
            break;
 
161
        if (LowType != -1 && LowType < (dev_idx >> HRDEV_TYPE_SHIFT))
 
162
            break;
 
163
        newname[HRDEV_ENTRY_NAME_LENGTH] = dev_idx;
 
164
        DEBUGMSGOID(("host/hr_device", newname, *length));
 
165
        DEBUGMSG(("host/hr_device", "\n"));
 
166
        result = snmp_oid_compare(name, *length, newname, vp->namelen + 1);
 
167
        if (exact && (result == 0)) {
 
168
            if (save_device[current_type] != NULL)
 
169
                (*save_device[current_type]) ();
 
170
            LowIndex = dev_idx;
 
171
            break;
 
172
        }
 
173
        if ((!exact && (result < 0)) &&
 
174
            (LowIndex == -1 || dev_idx < LowIndex)) {
 
175
            if (save_device[current_type] != NULL)
 
176
                (*save_device[current_type]) ();
 
177
            LowIndex = dev_idx;
 
178
            LowType = (dev_idx >> HRDEV_TYPE_SHIFT);
 
179
            if (dev_idx_inc[LowType])   /* Increasing indices => now done */
 
180
                break;
 
181
        }
 
182
 
 
183
    }
 
184
 
 
185
    if (LowIndex == -1) {
 
186
        DEBUGMSGTL(("host/hr_device", "... index out of range\n"));
 
187
        return (MATCH_FAILED);
 
188
    }
 
189
 
 
190
    newname[HRDEV_ENTRY_NAME_LENGTH] = LowIndex;
 
191
    memcpy((char *) name, (char *) newname,
 
192
           ((int) vp->namelen + 1) * sizeof(oid));
 
193
    *length = vp->namelen + 1;
 
194
    *write_method = 0;
 
195
    *var_len = sizeof(long);    /* default to 'long' results */
 
196
 
 
197
    DEBUGMSGTL(("host/hr_device", "... get device stats "));
 
198
    DEBUGMSGOID(("host/hr_device", name, *length));
 
199
    DEBUGMSG(("host/hr_device", "\n"));
 
200
 
 
201
    return LowIndex;
 
202
}
 
203
 
 
204
 
 
205
oid             device_type_id[] = { 1, 3, 6, 1, 2, 1, 25, 3, 1, 99 };  /* hrDeviceType99 */
 
206
int             device_type_len =
 
207
    sizeof(device_type_id) / sizeof(device_type_id[0]);
 
208
 
 
209
 
 
210
        /*********************
 
211
         *
 
212
         *  System specific implementation functions
 
213
         *
 
214
         *********************/
 
215
 
 
216
 
 
217
u_char         *
 
218
var_hrdevice(struct variable *vp,
 
219
             oid * name,
 
220
             size_t * length,
 
221
             int exact, size_t * var_len, WriteMethod ** write_method)
 
222
{
 
223
    int             dev_idx, type;
 
224
    oid            *oid_p;
 
225
    static char     string[1024];
 
226
 
 
227
    dev_idx =
 
228
        header_hrdevice(vp, name, length, exact, var_len, write_method);
 
229
    if (dev_idx == MATCH_FAILED)
 
230
        return NULL;
 
231
 
 
232
    type = (dev_idx >> HRDEV_TYPE_SHIFT);
 
233
 
 
234
    switch (vp->magic) {
 
235
    case HRDEV_INDEX:
 
236
        long_return = dev_idx;
 
237
        return (u_char *) & long_return;
 
238
    case HRDEV_TYPE:
 
239
        device_type_id[device_type_len - 1] = type;
 
240
        *var_len = sizeof(device_type_id);
 
241
        return (u_char *) device_type_id;
 
242
    case HRDEV_DESCR:
 
243
        if (device_descr[type] != NULL) {
 
244
            strncpy(string, ((*device_descr[type]) (dev_idx)),
 
245
                    sizeof(string)-1);
 
246
            string[ sizeof(string)-1] = 0;
 
247
        } else
 
248
#if NO_DUMMY_VALUES
 
249
            return NULL;
 
250
#else
 
251
            sprintf(string, "a black box of some sort");
 
252
#endif
 
253
        *var_len = strlen(string);
 
254
        return (u_char *) string;
 
255
    case HRDEV_ID:
 
256
        if (device_prodid[type] != NULL)
 
257
            oid_p = ((*device_prodid[type]) (dev_idx, var_len));
 
258
        else {
 
259
            oid_p = nullOid;
 
260
            *var_len = nullOidLen;
 
261
        }
 
262
        return (u_char *) oid_p;
 
263
    case HRDEV_STATUS:
 
264
        if (device_status[type] != NULL)
 
265
            long_return = ((*device_status[type]) (dev_idx));
 
266
        else
 
267
#if NO_DUMMY_VALUES
 
268
            return NULL;
 
269
#else
 
270
            long_return = 2;    /* Assume running */
 
271
#endif
 
272
        return (u_char *) & long_return;
 
273
    case HRDEV_ERRORS:
 
274
        if (device_errors[type] != NULL)
 
275
            long_return = (*device_errors[type]) (dev_idx);
 
276
        else
 
277
#if NO_DUMMY_VALUES
 
278
            return NULL;
 
279
#else
 
280
            long_return = 0;    /* Assume OK */
 
281
#endif
 
282
        return (u_char *) & long_return;
 
283
    default:
 
284
        DEBUGMSGTL(("snmpd", "unknown sub-id %d in var_hrdevice\n",
 
285
                    vp->magic));
 
286
    }
 
287
    return NULL;
 
288
}
 
289
 
 
290
 
 
291
        /*********************
 
292
         *
 
293
         *  Internal implementation functions
 
294
         *
 
295
         *********************/
 
296
 
 
297
 
 
298
void
 
299
Init_Device(void)
 
300
{
 
301
    /*
 
302
     *  Find the first non-NULL initialisation function
 
303
     *    and call it
 
304
     */
 
305
    while (current_type < HRDEV_TYPE_MAX &&
 
306
           init_device[current_type] == NULL)
 
307
        if (++current_type >= HRDEV_TYPE_MAX)
 
308
            return;
 
309
    /* Check current_type, if >= MAX first time into loop, would fail below */
 
310
    if (current_type < HRDEV_TYPE_MAX)
 
311
        (*init_device[current_type]) ();
 
312
}
 
313
 
 
314
int
 
315
Get_Next_Device(void)
 
316
{
 
317
    int             result = -1;
 
318
 
 
319
    /*
 
320
     *  Call the 'next device' function for the current
 
321
     *    type of device
 
322
     *
 
323
     *  TODO:  save the necessary information about that device
 
324
     */
 
325
    if (current_type < HRDEV_TYPE_MAX && next_device[current_type] != NULL)
 
326
        result = (*next_device[current_type]) ();
 
327
 
 
328
    /*
 
329
     *  No more devices of the current type.
 
330
     *  Try the next type (if any)
 
331
     */
 
332
    if (result == -1) {
 
333
        if (++current_type >= HRDEV_TYPE_MAX) {
 
334
            current_type = 0;
 
335
            return -1;
 
336
        }
 
337
        Init_Device();
 
338
        return Get_Next_Device();
 
339
    }
 
340
    return result;
 
341
}