1
#include <net-snmp/net-snmp-config.h>
2
#include <net-snmp/net-snmp-includes.h>
3
#include <net-snmp/agent/net-snmp-agent-includes.h>
4
#include <net-snmp/agent/scalar.h>
12
#include <net-snmp/agent/cache_handler.h>
13
#include "agent/nsCache.h"
14
#include "util_funcs.h"
19
* OIDs for the cacheging control scalar objects
21
* Note that these we're registering the full object rather
22
* than the (sole) valid instance in each case, in order
23
* to handle requests for invalid instances properly.
25
oid nsCacheTimeout_oid[] = { 1, 3, 6, 1, 4, 1, 8072, 1, 5, 1};
26
oid nsCacheEnabled_oid[] = { 1, 3, 6, 1, 4, 1, 8072, 1, 5, 2};
29
* ... and for the cache table.
32
#define NSCACHE_TIMEOUT 2
33
#define NSCACHE_STATUS 3
35
#define NSCACHE_STATUS_ENABLED 1
36
#define NSCACHE_STATUS_DISABLED 2
37
#define NSCACHE_STATUS_EMPTY 3
38
#define NSCACHE_STATUS_ACTIVE 4
39
#define NSCACHE_STATUS_EXPIRED 5
41
oid nsCacheTable_oid[] = { 1, 3, 6, 1, 4, 1, 8072, 1, 5, 3};
47
netsnmp_table_registration_info *table_info;
48
netsnmp_iterator_info *iinfo;
51
* Register the scalar objects...
53
DEBUGMSGTL(("nsCacheScalars", "Initializing\n"));
54
netsnmp_register_scalar(
55
netsnmp_create_handler_registration(
56
"nsCacheTimeout", handle_nsCacheTimeout,
57
nsCacheTimeout_oid, OID_LENGTH(nsCacheTimeout_oid),
60
netsnmp_register_scalar(
61
netsnmp_create_handler_registration(
62
"nsCacheEnabled", handle_nsCacheEnabled,
63
nsCacheEnabled_oid, OID_LENGTH(nsCacheEnabled_oid),
69
* We need to define the column structure and indexing....
72
table_info = SNMP_MALLOC_TYPEDEF(netsnmp_table_registration_info);
76
netsnmp_table_helper_add_indexes(table_info, ASN_PRIV_IMPLIED_OBJECT_ID, 0);
77
table_info->min_column = NSCACHE_TIMEOUT;
78
table_info->max_column = NSCACHE_STATUS;
82
* .... and the iteration information ....
84
iinfo = SNMP_MALLOC_TYPEDEF(netsnmp_iterator_info);
88
iinfo->get_first_data_point = get_first_cache_entry;
89
iinfo->get_next_data_point = get_next_cache_entry;
90
iinfo->table_reginfo = table_info;
94
* .... and register the table with the agent.
96
netsnmp_register_table_iterator(
97
netsnmp_create_handler_registration(
98
"tzCacheTable", handle_nsCacheTable,
99
nsCacheTable_oid, OID_LENGTH(nsCacheTable_oid),
106
* nsCache scalar handling
110
handle_nsCacheTimeout(netsnmp_mib_handler *handler,
111
netsnmp_handler_registration *reginfo,
112
netsnmp_agent_request_info *reqinfo,
113
netsnmp_request_info *requests)
115
extern long cache_default_timeout;
116
netsnmp_request_info *request=NULL;
118
switch (reqinfo->mode) {
121
for (request = requests; request; request=request->next) {
122
snmp_set_var_typed_value(request->requestvb, ASN_INTEGER,
123
(u_char*)&cache_default_timeout,
124
sizeof(cache_default_timeout));
129
case MODE_SET_RESERVE1:
130
for (request = requests; request; request=request->next) {
131
if ( request->status != 0 ) {
132
return SNMP_ERR_NOERROR; /* Already got an error */
134
if ( request->requestvb->type != ASN_INTEGER ) {
135
netsnmp_set_request_error(reqinfo, request, SNMP_ERR_WRONGTYPE);
136
return SNMP_ERR_WRONGTYPE;
138
if ( *request->requestvb->val.integer < 0 ) {
139
netsnmp_set_request_error(reqinfo, request, SNMP_ERR_WRONGVALUE);
140
return SNMP_ERR_WRONGVALUE;
145
case MODE_SET_COMMIT:
146
cache_default_timeout = *requests->requestvb->val.integer;
150
return SNMP_ERR_NOERROR;
155
handle_nsCacheEnabled(netsnmp_mib_handler *handler,
156
netsnmp_handler_registration *reginfo,
157
netsnmp_agent_request_info *reqinfo,
158
netsnmp_request_info *requests)
160
extern long caching_enabled;
162
netsnmp_request_info *request=NULL;
164
switch (reqinfo->mode) {
167
enabled = (caching_enabled
168
? NSCACHE_STATUS_ENABLED /* Actually True/False */
169
: NSCACHE_STATUS_DISABLED );
170
for (request = requests; request; request=request->next) {
171
snmp_set_var_typed_value(request->requestvb, ASN_INTEGER,
172
(u_char*)&enabled, sizeof(enabled));
177
case MODE_SET_RESERVE1:
178
for (request = requests; request; request=request->next) {
179
if ( request->status != 0 ) {
180
return SNMP_ERR_NOERROR; /* Already got an error */
182
if ( request->requestvb->type != ASN_INTEGER ) {
183
netsnmp_set_request_error(reqinfo, request, SNMP_ERR_WRONGTYPE);
184
return SNMP_ERR_WRONGTYPE;
186
if ((*request->requestvb->val.integer != NSCACHE_STATUS_ENABLED) &&
187
(*request->requestvb->val.integer != NSCACHE_STATUS_DISABLED)) {
188
netsnmp_set_request_error(reqinfo, request, SNMP_ERR_WRONGVALUE);
189
return SNMP_ERR_WRONGVALUE;
194
case MODE_SET_COMMIT:
195
enabled = *requests->requestvb->val.integer;
196
if (enabled == NSCACHE_STATUS_DISABLED)
198
caching_enabled = enabled;
202
return SNMP_ERR_NOERROR;
207
* nsCacheTable handling
210
netsnmp_variable_list *
211
get_first_cache_entry(void **loop_context, void **data_context,
212
netsnmp_variable_list *index,
213
netsnmp_iterator_info *data)
215
extern netsnmp_cache *cache_head;
220
snmp_set_var_value(index, (u_char*)cache_head->rootoid,
221
sizeof(oid) * cache_head->rootoid_len);
222
*loop_context = (void*)cache_head;
223
*data_context = (void*)cache_head;
227
netsnmp_variable_list *
228
get_next_cache_entry(void **loop_context, void **data_context,
229
netsnmp_variable_list *index,
230
netsnmp_iterator_info *data)
232
netsnmp_cache *cache = (netsnmp_cache *)*loop_context;
238
snmp_set_var_value(index, (u_char*)cache->rootoid,
239
sizeof(oid) * cache->rootoid_len);
240
*loop_context = (void*)cache;
241
*data_context = (void*)cache;
247
handle_nsCacheTable(netsnmp_mib_handler *handler,
248
netsnmp_handler_registration *reginfo,
249
netsnmp_agent_request_info *reqinfo,
250
netsnmp_request_info *requests)
253
netsnmp_request_info *request = NULL;
254
netsnmp_table_request_info *table_info = NULL;
255
netsnmp_cache *cache_entry = NULL;
257
switch (reqinfo->mode) {
260
for (request=requests; request; request=request->next) {
261
if (requests->processed != 0)
264
cache_entry = (netsnmp_cache*)netsnmp_extract_iterator_context(request);
265
table_info = netsnmp_extract_table_info(request);
267
switch (table_info->colnum) {
268
case NSCACHE_TIMEOUT:
270
netsnmp_set_request_error(reqinfo, request, SNMP_NOSUCHINSTANCE);
273
snmp_set_var_typed_value(request->requestvb, ASN_INTEGER,
274
(u_char*)&cache_entry->timeout,
275
sizeof(cache_entry->timeout));
280
netsnmp_set_request_error(reqinfo, request, SNMP_NOSUCHINSTANCE);
283
status = (cache_entry->enabled ?
284
(cache_entry->timestamp ?
285
(!atime_ready(cache_entry->timestamp,
286
1000*cache_entry->timeout) ?
287
NSCACHE_STATUS_ACTIVE:
288
NSCACHE_STATUS_EXPIRED) :
289
NSCACHE_STATUS_EMPTY) :
290
NSCACHE_STATUS_DISABLED);
291
snmp_set_var_typed_value(request->requestvb, ASN_INTEGER,
292
(u_char*)&status, sizeof(status));
296
netsnmp_set_request_error(reqinfo, request, SNMP_NOSUCHOBJECT);
303
case MODE_SET_RESERVE1:
304
for (request=requests; request; request=request->next) {
305
if (requests->processed != 0)
307
if ( request->status != 0 ) {
308
return SNMP_ERR_NOERROR; /* Already got an error */
310
cache_entry = (netsnmp_cache*)netsnmp_extract_iterator_context(request);
311
table_info = netsnmp_extract_table_info(request);
313
switch (table_info->colnum) {
314
case NSCACHE_TIMEOUT:
316
netsnmp_set_request_error(reqinfo, request, SNMP_ERR_NOCREATION);
317
return SNMP_ERR_NOCREATION;
319
if ( request->requestvb->type != ASN_INTEGER ) {
320
netsnmp_set_request_error(reqinfo, request, SNMP_ERR_WRONGTYPE);
321
return SNMP_ERR_WRONGTYPE;
323
if (*request->requestvb->val.integer < 0 ) {
324
netsnmp_set_request_error(reqinfo, request, SNMP_ERR_WRONGVALUE);
325
return SNMP_ERR_WRONGVALUE;
331
netsnmp_set_request_error(reqinfo, request, SNMP_ERR_NOCREATION);
332
return SNMP_ERR_NOCREATION;
334
if ( request->requestvb->type != ASN_INTEGER ) {
335
netsnmp_set_request_error(reqinfo, request, SNMP_ERR_WRONGTYPE);
336
return SNMP_ERR_WRONGTYPE;
338
status = *request->requestvb->val.integer;
339
if (!((status == NSCACHE_STATUS_ENABLED ) ||
340
(status == NSCACHE_STATUS_DISABLED ) ||
341
(status == NSCACHE_STATUS_EMPTY ))) {
342
netsnmp_set_request_error(reqinfo, request, SNMP_ERR_WRONGVALUE);
343
return SNMP_ERR_WRONGVALUE;
348
netsnmp_set_request_error(reqinfo, request, SNMP_ERR_NOCREATION);
349
return SNMP_ERR_NOCREATION; /* XXX - is this right ? */
356
case MODE_SET_COMMIT:
357
for (request=requests; request; request=request->next) {
358
if (requests->processed != 0)
360
if ( request->status != 0 ) {
361
return SNMP_ERR_NOERROR; /* Already got an error */
363
cache_entry = (netsnmp_cache*)netsnmp_extract_iterator_context(request);
365
netsnmp_set_request_error(reqinfo, request, SNMP_ERR_COMMITFAILED);
366
return SNMP_ERR_COMMITFAILED; /* Shouldn't happen! */
368
table_info = netsnmp_extract_table_info(request);
370
switch (table_info->colnum) {
371
case NSCACHE_TIMEOUT:
372
cache_entry->timeout = *request->requestvb->val.integer;
376
switch (*request->requestvb->val.integer) {
377
case NSCACHE_STATUS_ENABLED:
378
cache_entry->enabled = 1;
380
case NSCACHE_STATUS_DISABLED:
381
cache_entry->enabled = 0;
383
case NSCACHE_STATUS_EMPTY:
384
cache_entry->free_cache(cache_entry, cache_entry->magic);
385
free(cache_entry->timestamp);
386
cache_entry->timestamp = NULL;
395
return SNMP_ERR_NOERROR;