~ubuntu-branches/ubuntu/gutsy/net-snmp/gutsy-security

« back to all changes in this revision

Viewing changes to agent/mibgroup/mibII/sysORTable.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
 *  Template MIB group implementation - sysORTable.c
 
3
 *
 
4
 */
 
5
#include <net-snmp/net-snmp-config.h>
 
6
#if HAVE_STDLIB_H
 
7
#include <stdlib.h>
 
8
#endif
 
9
#include <sys/types.h>
 
10
#if TIME_WITH_SYS_TIME
 
11
# ifdef WIN32
 
12
#  include <sys/timeb.h>
 
13
# else
 
14
#  include <sys/time.h>
 
15
# endif
 
16
# include <time.h>
 
17
#else
 
18
# if HAVE_SYS_TIME_H
 
19
#  include <sys/time.h>
 
20
# else
 
21
#  include <time.h>
 
22
# endif
 
23
#endif
 
24
#if HAVE_STRING_H
 
25
#include <string.h>
 
26
#else
 
27
#include <strings.h>
 
28
#endif
 
29
 
 
30
#if HAVE_WINSOCK_H
 
31
#include <winsock.h>
 
32
#endif
 
33
#if HAVE_NETINET_IN_H
 
34
#include <netinet/in.h>
 
35
#endif
 
36
 
 
37
#if HAVE_DMALLOC_H
 
38
#include <dmalloc.h>
 
39
#endif
 
40
 
 
41
#include <net-snmp/net-snmp-includes.h>
 
42
#include <net-snmp/agent/net-snmp-agent-includes.h>
 
43
#include <net-snmp/agent/agent_callbacks.h>
 
44
 
 
45
#include "struct.h"
 
46
#include "util_funcs.h"
 
47
#include "sysORTable.h"
 
48
#include "snmpd.h"
 
49
 
 
50
#ifdef USING_AGENTX_SUBAGENT_MODULE
 
51
#include "agentx/subagent.h"
 
52
#include "agentx/client.h"
 
53
#endif
 
54
 
 
55
 
 
56
struct timeval  sysOR_lastchange;
 
57
static struct sysORTable *table = NULL;
 
58
static int      numEntries = 0;
 
59
 
 
60
/*
 
61
 * define the structure we're going to ask the agent to register our
 
62
 * information at 
 
63
 */
 
64
struct variable1 sysORTable_variables[] = {
 
65
    {SYSORTABLEID, ASN_OBJECT_ID, RONLY, var_sysORTable, 1, {2}},
 
66
    {SYSORTABLEDESCR, ASN_OCTET_STR, RONLY, var_sysORTable, 1, {3}},
 
67
    {SYSORTABLEUPTIME, ASN_TIMETICKS, RONLY, var_sysORTable, 1, {4}}
 
68
};
 
69
 
 
70
/*
 
71
 * Define the OID pointer to the top of the mib tree that we're
 
72
 * registering underneath 
 
73
 */
 
74
oid             sysORTable_variables_oid[] = { SNMP_OID_MIB2, 1, 9, 1 };
 
75
#ifdef USING_MIBII_SYSTEM_MIB_MODULE
 
76
extern oid      system_module_oid[];
 
77
extern int      system_module_oid_len;
 
78
extern int      system_module_count;
 
79
#endif
 
80
 
 
81
void
 
82
init_sysORTable(void)
 
83
{
 
84
    /*
 
85
     * register ourselves with the agent to handle our mib tree 
 
86
     */
 
87
 
 
88
#ifdef USING_AGENTX_SUBAGENT_MODULE
 
89
    if (netsnmp_ds_get_boolean(NETSNMP_DS_APPLICATION_ID, NETSNMP_DS_AGENT_ROLE) == MASTER_AGENT)
 
90
        (void) register_mib_priority("mibII/sysORTable",
 
91
                                     (struct variable *)
 
92
                                     sysORTable_variables,
 
93
                                     sizeof(struct variable1),
 
94
                                     sizeof(sysORTable_variables) /
 
95
                                     sizeof(struct variable1),
 
96
                                     sysORTable_variables_oid,
 
97
                                     sizeof(sysORTable_variables_oid) /
 
98
                                     sizeof(oid), 1);
 
99
    else
 
100
#endif
 
101
        REGISTER_MIB("mibII/sysORTable", sysORTable_variables, variable1,
 
102
                     sysORTable_variables_oid);
 
103
 
 
104
#ifdef USING_MIBII_SYSTEM_MIB_MODULE
 
105
    if (++system_module_count == 3)
 
106
        REGISTER_SYSOR_TABLE(system_module_oid, system_module_oid_len,
 
107
                             "The MIB module for SNMPv2 entities");
 
108
#endif
 
109
 
 
110
    gettimeofday(&sysOR_lastchange, NULL);
 
111
}
 
112
 
 
113
        /*********************
 
114
         *
 
115
         *  System specific implementation functions
 
116
         *
 
117
         *********************/
 
118
 
 
119
u_char         *
 
120
var_sysORTable(struct variable *vp,
 
121
               oid * name,
 
122
               size_t * length,
 
123
               int exact, size_t * var_len, WriteMethod ** write_method)
 
124
{
 
125
    unsigned long   i = 0;
 
126
    static unsigned long ret;
 
127
    struct sysORTable *ptr = table;
 
128
 
 
129
    if (header_simple_table
 
130
        (vp, name, length, exact, var_len, write_method, numEntries))
 
131
        return NULL;
 
132
 
 
133
    for (i = 1; ptr != NULL && i < name[*length - 1]; ptr = ptr->next, i++) {
 
134
        DEBUGMSGTL(("mibII/sysORTable", "sysORTable -- %lu != %lu\n",
 
135
                    i, name[*length - 1]));
 
136
    }
 
137
    if (ptr == NULL) {
 
138
        DEBUGMSGTL(("mibII/sysORTable", "sysORTable -- no match: %lu\n",
 
139
                    i));
 
140
        return NULL;
 
141
    }
 
142
    DEBUGMSGTL(("mibII/sysORTable", "sysORTable -- match: %lu\n", i));
 
143
 
 
144
    switch (vp->magic) {
 
145
    case SYSORTABLEID:
 
146
        *var_len = ptr->OR_oidlen * sizeof(ptr->OR_oid[0]);
 
147
        return (u_char *) ptr->OR_oid;
 
148
 
 
149
    case SYSORTABLEDESCR:
 
150
        *var_len = strlen(ptr->OR_descr);
 
151
        return (u_char *) ptr->OR_descr;
 
152
 
 
153
    case SYSORTABLEUPTIME:
 
154
        ret = netsnmp_timeval_uptime(&ptr->OR_uptime);
 
155
        return (u_char *) & ret;
 
156
 
 
157
    default:
 
158
        DEBUGMSGTL(("snmpd", "unknown sub-id %d in var_sysORTable\n",
 
159
                    vp->magic));
 
160
    }
 
161
    return NULL;
 
162
}
 
163
 
 
164
 
 
165
int
 
166
register_sysORTable_sess(oid * oidin,
 
167
                         size_t oidlen,
 
168
                         const char *descr, netsnmp_session * ss)
 
169
{
 
170
    struct sysORTable *ptr, **nptr;
 
171
    struct register_sysOR_parameters reg_sysOR_parms;
 
172
 
 
173
    DEBUGMSGTL(("mibII/sysORTable", "sysORTable registering: "));
 
174
    DEBUGMSGOID(("mibII/sysORTable", oidin, oidlen));
 
175
    DEBUGMSG(("mibII/sysORTable", "\n"));
 
176
 
 
177
    ptr = (struct sysORTable *) malloc(sizeof(struct sysORTable));
 
178
    if (ptr == NULL) {
 
179
        return SYS_ORTABLE_REGISTRATION_FAILED;
 
180
    }
 
181
    ptr->OR_descr = (char *) strdup(descr);
 
182
    if (ptr->OR_descr == NULL) {
 
183
        free(ptr);
 
184
        return SYS_ORTABLE_REGISTRATION_FAILED;
 
185
    }
 
186
    ptr->OR_oidlen = oidlen;
 
187
    ptr->OR_oid = (oid *) malloc(sizeof(oid) * oidlen);
 
188
    if (ptr->OR_oid == NULL) {
 
189
        free(ptr->OR_descr);
 
190
        free(ptr);
 
191
        return SYS_ORTABLE_REGISTRATION_FAILED;
 
192
    }
 
193
    memcpy(ptr->OR_oid, oidin, sizeof(oid) * oidlen);
 
194
    gettimeofday(&(ptr->OR_uptime), NULL);
 
195
    gettimeofday(&(sysOR_lastchange), NULL);
 
196
    ptr->OR_sess = ss;
 
197
    ptr->next = NULL;
 
198
    numEntries++;
 
199
 
 
200
    /* add this entry to the end of the chained list */
 
201
    nptr = &table;
 
202
    while (*nptr != NULL)
 
203
        nptr = &((*nptr)->next);
 
204
    *nptr = ptr;
 
205
 
 
206
    reg_sysOR_parms.name = oidin;
 
207
    reg_sysOR_parms.namelen = oidlen;
 
208
    reg_sysOR_parms.descr = descr;
 
209
    snmp_call_callbacks(SNMP_CALLBACK_APPLICATION,
 
210
                        SNMPD_CALLBACK_REG_SYSOR, &reg_sysOR_parms);
 
211
 
 
212
    return SYS_ORTABLE_REGISTERED_OK;
 
213
}
 
214
 
 
215
int
 
216
register_sysORTable(oid * oidin, size_t oidlen, const char *descr)
 
217
{
 
218
    return register_sysORTable_sess(oidin, oidlen, descr, NULL);
 
219
}
 
220
 
 
221
 
 
222
 
 
223
int
 
224
unregister_sysORTable_sess(oid * oidin,
 
225
                           size_t oidlen, netsnmp_session * ss)
 
226
{
 
227
    struct sysORTable *ptr, *prev = NULL, *next;
 
228
    int             found = SYS_ORTABLE_NO_SUCH_REGISTRATION;
 
229
    struct register_sysOR_parameters reg_sysOR_parms;
 
230
 
 
231
    DEBUGMSGTL(("mibII/sysORTable", "sysORTable unregistering: "));
 
232
    DEBUGMSGOID(("mibII/sysORTable", oidin, oidlen));
 
233
    DEBUGMSG(("mibII/sysORTable", "\n"));
 
234
 
 
235
    for (ptr = table; ptr; ptr = next)
 
236
    {
 
237
        next = ptr->next;
 
238
        if (ptr->OR_sess == ss &&
 
239
          (snmp_oid_compare(oidin, oidlen, ptr->OR_oid, ptr->OR_oidlen) == 0))
 
240
        {
 
241
            if (prev == NULL)
 
242
                table = ptr->next;
 
243
            else
 
244
                prev->next = ptr->next;
 
245
 
 
246
            free(ptr->OR_oid);
 
247
            free(ptr->OR_descr);
 
248
            free(ptr);
 
249
            numEntries--;
 
250
            gettimeofday(&(sysOR_lastchange), NULL);
 
251
            found = SYS_ORTABLE_UNREGISTERED_OK;
 
252
            break;
 
253
        } else
 
254
            prev = ptr;
 
255
    }
 
256
 
 
257
    reg_sysOR_parms.name = oidin;
 
258
    reg_sysOR_parms.namelen = oidlen;
 
259
    snmp_call_callbacks(SNMP_CALLBACK_APPLICATION,
 
260
                        SNMPD_CALLBACK_UNREG_SYSOR, &reg_sysOR_parms);
 
261
 
 
262
    return found;
 
263
}
 
264
 
 
265
 
 
266
int
 
267
unregister_sysORTable(oid * oidin, size_t oidlen)
 
268
{
 
269
    return unregister_sysORTable_sess(oidin, oidlen, NULL);
 
270
}
 
271
 
 
272
void
 
273
unregister_sysORTable_by_session(netsnmp_session * ss)
 
274
{
 
275
    struct sysORTable *ptr, *prev = NULL, *next;
 
276
 
 
277
    for (ptr = table; ptr; ptr = next)
 
278
    {
 
279
        next = ptr->next;
 
280
        if (((ss->flags & SNMP_FLAGS_SUBSESSION) && ptr->OR_sess == ss) ||
 
281
            (!(ss->flags & SNMP_FLAGS_SUBSESSION) && ptr->OR_sess &&
 
282
             ptr->OR_sess->subsession == ss)) {
 
283
            if (prev == NULL)
 
284
                table = next;
 
285
            else
 
286
                prev->next = next;
 
287
            free(ptr->OR_oid);
 
288
            free(ptr->OR_descr);
 
289
            free(ptr);
 
290
            numEntries--;
 
291
            gettimeofday(&(sysOR_lastchange), NULL);
 
292
        } else
 
293
            prev = ptr;
 
294
    }
 
295
}