| 1 |
#include <net-snmp/net-snmp-config.h> |
| 2 |
#include <net-snmp/net-snmp-features.h> |
| 3 |
|
| 4 |
#ifdef HAVE_STDLIB_H |
| 5 |
#include <stdlib.h> |
| 6 |
#endif |
| 7 |
#ifdef HAVE_STRING_H |
| 8 |
#include <string.h> |
| 9 |
#else |
| 10 |
#include <strings.h> |
| 11 |
#endif |
| 12 |
|
| 13 |
netsnmp_feature_require(ulong_instance); |
| 14 |
netsnmp_feature_require(register_read_only_table_data); |
| 15 |
netsnmp_feature_require(table_build_result); |
| 16 |
netsnmp_feature_require(table_dataset); |
| 17 |
|
| 18 |
#include <net-snmp/net-snmp-includes.h> |
| 19 |
#include <net-snmp/agent/net-snmp-agent-includes.h> |
| 20 |
|
| 21 |
#include "testhandler.h" |
| 22 |
|
| 23 |
#include <net-snmp/agent/table.h> |
| 24 |
#include <net-snmp/agent/instance.h> |
| 25 |
#include <net-snmp/agent/table_data.h> |
| 26 |
#include <net-snmp/agent/table_dataset.h> |
| 27 |
|
| 28 |
static oid my_test_oid[4] = { 1, 2, 3, 4 }; |
| 29 |
static oid my_table_oid[4] = { 1, 2, 3, 5 }; |
| 30 |
static oid my_instance_oid[5] = { 1, 2, 3, 6, 1 }; |
| 31 |
static oid my_data_table_oid[4] = { 1, 2, 3, 7 }; |
| 32 |
static oid my_data_ulong_instance[4] = { 1, 2, 3, 9 }; |
| 33 |
|
| 34 |
static netsnmp_handler_registration *ro_scalar_h, *rw_scalar_h, *table_h; |
| 35 |
static netsnmp_handler_registration *my_test; |
| 36 |
static netsnmp_table_registration_info *table_info1; |
| 37 |
static netsnmp_table_registration_info *table_info2; |
| 38 |
static netsnmp_table_data *table; |
| 39 |
|
| 40 |
static u_long my_ulong = 0; |
| 41 |
|
| 42 |
static Netsnmp_Node_Handler my_test_handler; |
| 43 |
static Netsnmp_Node_Handler my_test_table_handler; |
| 44 |
static Netsnmp_Node_Handler my_data_table_handler; |
| 45 |
static Netsnmp_Node_Handler my_test_instance_handler; |
| 46 |
|
| 47 |
void |
| 48 |
init_testhandler(void) |
| 49 |
{ |
| 50 |
/* |
| 51 |
* we're registering at .1.2.3.4 |
| 52 |
*/ |
| 53 |
u_long ind1; |
| 54 |
netsnmp_table_row *row; |
| 55 |
|
| 56 |
DEBUGMSGTL(("testhandler", "initializing\n")); |
| 57 |
|
| 58 |
/* |
| 59 |
* basic handler test |
| 60 |
*/ |
| 61 |
ro_scalar_h = netsnmp_create_handler_registration("myTest", my_test_handler, |
| 62 |
my_test_oid, 4, HANDLER_CAN_RONLY); |
| 63 |
netsnmp_register_handler(ro_scalar_h); |
| 64 |
|
| 65 |
/* |
| 66 |
* instance handler test |
| 67 |
*/ |
| 68 |
rw_scalar_h = netsnmp_create_handler_registration("myInstance", |
| 69 |
my_test_instance_handler, my_instance_oid, 5, HANDLER_CAN_RWRITE); |
| 70 |
netsnmp_register_instance(rw_scalar_h); |
| 71 |
|
| 72 |
netsnmp_register_ulong_instance("myulong", |
| 73 |
my_data_ulong_instance, 4, |
| 74 |
&my_ulong, NULL); |
| 75 |
|
| 76 |
/* |
| 77 |
* table helper test |
| 78 |
*/ |
| 79 |
|
| 80 |
my_test = netsnmp_create_handler_registration("myTable", |
| 81 |
my_test_table_handler, |
| 82 |
my_table_oid, 4, |
| 83 |
HANDLER_CAN_RONLY); |
| 84 |
if (!my_test) |
| 85 |
return; |
| 86 |
|
| 87 |
table_info1 = SNMP_MALLOC_TYPEDEF(netsnmp_table_registration_info); |
| 88 |
if (!table_info1) |
| 89 |
return; |
| 90 |
|
| 91 |
netsnmp_table_helper_add_indexes(table_info1, ASN_INTEGER, ASN_INTEGER, |
| 92 |
0); |
| 93 |
table_info1->min_column = 3; |
| 94 |
table_info1->max_column = 3; |
| 95 |
netsnmp_register_table(my_test, table_info1); |
| 96 |
|
| 97 |
/* |
| 98 |
* data table helper test |
| 99 |
*/ |
| 100 |
/* |
| 101 |
* we'll construct a simple table here with two indexes: an |
| 102 |
* integer and a string (why not). It'll contain only one |
| 103 |
* column so the data pointer is merely the data in that |
| 104 |
* column. |
| 105 |
*/ |
| 106 |
|
| 107 |
table = netsnmp_create_table_data("data_table_test"); |
| 108 |
|
| 109 |
netsnmp_table_data_add_index(table, ASN_INTEGER); |
| 110 |
netsnmp_table_data_add_index(table, ASN_OCTET_STR); |
| 111 |
|
| 112 |
/* |
| 113 |
* 1 partridge in a pear tree |
| 114 |
*/ |
| 115 |
row = netsnmp_create_table_data_row(); |
| 116 |
ind1 = 1; |
| 117 |
netsnmp_table_row_add_index(row, ASN_INTEGER, &ind1, sizeof(ind1)); |
| 118 |
netsnmp_table_row_add_index(row, ASN_OCTET_STR, "partridge", |
| 119 |
strlen("partridge")); |
| 120 |
row->data = NETSNMP_REMOVE_CONST(void *, "pear tree"); |
| 121 |
netsnmp_table_data_add_row(table, row); |
| 122 |
|
| 123 |
/* |
| 124 |
* 2 turtle doves |
| 125 |
*/ |
| 126 |
row = netsnmp_create_table_data_row(); |
| 127 |
ind1 = 2; |
| 128 |
netsnmp_table_row_add_index(row, ASN_INTEGER, &ind1, sizeof(ind1)); |
| 129 |
netsnmp_table_row_add_index(row, ASN_OCTET_STR, "turtle", |
| 130 |
strlen("turtle")); |
| 131 |
row->data = NETSNMP_REMOVE_CONST(void *, "doves"); |
| 132 |
netsnmp_table_data_add_row(table, row); |
| 133 |
|
| 134 |
/* |
| 135 |
* we're going to register it as a normal table too, so we get the |
| 136 |
* automatically parsed column and index information |
| 137 |
*/ |
| 138 |
table_info2 = SNMP_MALLOC_TYPEDEF(netsnmp_table_registration_info); |
| 139 |
if (table_info2 == NULL) |
| 140 |
return; |
| 141 |
|
| 142 |
netsnmp_table_helper_add_indexes(table_info2, ASN_INTEGER, |
| 143 |
ASN_OCTET_STR, 0); |
| 144 |
table_info2->min_column = 3; |
| 145 |
table_info2->max_column = 3; |
| 146 |
|
| 147 |
table_h = netsnmp_create_handler_registration("12days", |
| 148 |
my_data_table_handler, my_data_table_oid, 4, HANDLER_CAN_RONLY); |
| 149 |
netsnmp_register_read_only_table_data(table_h, table, table_info2); |
| 150 |
|
| 151 |
} |
| 152 |
|
| 153 |
void |
| 154 |
shutdown_testhandler(void) |
| 155 |
{ |
| 156 |
netsnmp_unregister_handler(my_test); |
| 157 |
netsnmp_table_registration_info_free(table_info1); |
| 158 |
netsnmp_unregister_handler(table_h); |
| 159 |
netsnmp_table_registration_info_free(table_info2); |
| 160 |
netsnmp_unregister_handler(rw_scalar_h); |
| 161 |
netsnmp_unregister_handler(ro_scalar_h); |
| 162 |
} |
| 163 |
|
| 164 |
int |
| 165 |
my_test_handler(netsnmp_mib_handler *handler, |
| 166 |
netsnmp_handler_registration *reginfo, |
| 167 |
netsnmp_agent_request_info *reqinfo, |
| 168 |
netsnmp_request_info *requests) |
| 169 |
{ |
| 170 |
|
| 171 |
oid myoid1[] = { 1, 2, 3, 4, 5, 6 }; |
| 172 |
static u_long accesses = 0; |
| 173 |
|
| 174 |
DEBUGMSGTL(("testhandler", "Got request:\n")); |
| 175 |
/* |
| 176 |
* loop through requests |
| 177 |
*/ |
| 178 |
while (requests) { |
| 179 |
netsnmp_variable_list *var = requests->requestvb; |
| 180 |
|
| 181 |
DEBUGMSGTL(("testhandler", " oid:")); |
| 182 |
DEBUGMSGOID(("testhandler", var->name, var->name_length)); |
| 183 |
DEBUGMSG(("testhandler", "\n")); |
| 184 |
|
| 185 |
switch (reqinfo->mode) { |
| 186 |
case MODE_GET: |
| 187 |
if (netsnmp_oid_equals(var->name, var->name_length, myoid1, 6) |
| 188 |
== 0) { |
| 189 |
snmp_set_var_typed_value(var, ASN_INTEGER, |
| 190 |
(u_char *) & accesses, |
| 191 |
sizeof(accesses)); |
| 192 |
return SNMP_ERR_NOERROR; |
| 193 |
} |
| 194 |
break; |
| 195 |
|
| 196 |
case MODE_GETNEXT: |
| 197 |
if (snmp_oid_compare(var->name, var->name_length, myoid1, 6) |
| 198 |
< 0) { |
| 199 |
snmp_set_var_objid(var, myoid1, 6); |
| 200 |
snmp_set_var_typed_value(var, ASN_INTEGER, |
| 201 |
(u_char *) & accesses, |
| 202 |
sizeof(accesses)); |
| 203 |
return SNMP_ERR_NOERROR; |
| 204 |
} |
| 205 |
break; |
| 206 |
|
| 207 |
default: |
| 208 |
netsnmp_set_request_error(reqinfo, requests, SNMP_ERR_GENERR); |
| 209 |
break; |
| 210 |
} |
| 211 |
|
| 212 |
requests = requests->next; |
| 213 |
} |
| 214 |
return SNMP_ERR_NOERROR; |
| 215 |
} |
| 216 |
|
| 217 |
/* |
| 218 |
* functionally this is a simply a multiplication table for 12x12 |
| 219 |
*/ |
| 220 |
|
| 221 |
#define MAX_COLONE 12 |
| 222 |
#define MAX_COLTWO 12 |
| 223 |
#define RESULT_COLUMN 3 |
| 224 |
int |
| 225 |
my_test_table_handler(netsnmp_mib_handler *handler, |
| 226 |
netsnmp_handler_registration *reginfo, |
| 227 |
netsnmp_agent_request_info *reqinfo, |
| 228 |
netsnmp_request_info *requests) |
| 229 |
{ |
| 230 |
|
| 231 |
netsnmp_table_registration_info |
| 232 |
*handler_reg_info = |
| 233 |
(netsnmp_table_registration_info *) handler->prev->myvoid; |
| 234 |
|
| 235 |
netsnmp_table_request_info *table_info; |
| 236 |
u_long result; |
| 237 |
int x, y; |
| 238 |
|
| 239 |
while (requests) { |
| 240 |
netsnmp_variable_list *var = requests->requestvb; |
| 241 |
|
| 242 |
if (requests->processed != 0) |
| 243 |
continue; |
| 244 |
|
| 245 |
DEBUGMSGTL(("testhandler_table", "Got request:\n")); |
| 246 |
DEBUGMSGTL(("testhandler_table", " oid:")); |
| 247 |
DEBUGMSGOID(("testhandler_table", var->name, var->name_length)); |
| 248 |
DEBUGMSG(("testhandler_table", "\n")); |
| 249 |
|
| 250 |
table_info = netsnmp_extract_table_info(requests); |
| 251 |
if (table_info == NULL) { |
| 252 |
requests = requests->next; |
| 253 |
continue; |
| 254 |
} |
| 255 |
|
| 256 |
switch (reqinfo->mode) { |
| 257 |
case MODE_GETNEXT: |
| 258 |
/* |
| 259 |
* beyond our search range? |
| 260 |
*/ |
| 261 |
if (table_info->colnum > RESULT_COLUMN) |
| 262 |
break; |
| 263 |
|
| 264 |
/* |
| 265 |
* below our minimum column? |
| 266 |
*/ |
| 267 |
if (table_info->colnum < RESULT_COLUMN || |
| 268 |
/* |
| 269 |
* or no index specified |
| 270 |
*/ |
| 271 |
table_info->indexes->val.integer == NULL) { |
| 272 |
table_info->colnum = RESULT_COLUMN; |
| 273 |
x = 0; |
| 274 |
y = 0; |
| 275 |
} else { |
| 276 |
x = *(table_info->indexes->val.integer); |
| 277 |
y = *(table_info->indexes->next_variable->val.integer); |
| 278 |
} |
| 279 |
|
| 280 |
if (table_info->number_indexes == |
| 281 |
handler_reg_info->number_indexes) { |
| 282 |
y++; /* GETNEXT is basically just y+1 for this table */ |
| 283 |
if (y > MAX_COLTWO) { /* (with wrapping) */ |
| 284 |
y = 0; |
| 285 |
x++; |
| 286 |
} |
| 287 |
} |
| 288 |
if (x <= MAX_COLONE) { |
| 289 |
result = x * y; |
| 290 |
|
| 291 |
*(table_info->indexes->val.integer) = x; |
| 292 |
*(table_info->indexes->next_variable->val.integer) = y; |
| 293 |
netsnmp_table_build_result(reginfo, requests, |
| 294 |
table_info, ASN_INTEGER, |
| 295 |
(u_char *) & result, |
| 296 |
sizeof(result)); |
| 297 |
} |
| 298 |
|
| 299 |
break; |
| 300 |
|
| 301 |
case MODE_GET: |
| 302 |
if (var->type == ASN_NULL) { /* valid request if ASN_NULL */ |
| 303 |
/* |
| 304 |
* is it the right column? |
| 305 |
*/ |
| 306 |
if (table_info->colnum == RESULT_COLUMN && |
| 307 |
/* |
| 308 |
* and within the max boundries? |
| 309 |
*/ |
| 310 |
*(table_info->indexes->val.integer) <= MAX_COLONE && |
| 311 |
*(table_info->indexes->next_variable->val.integer) |
| 312 |
<= MAX_COLTWO) { |
| 313 |
|
| 314 |
/* |
| 315 |
* then, the result is column1 * column2 |
| 316 |
*/ |
| 317 |
result = *(table_info->indexes->val.integer) * |
| 318 |
*(table_info->indexes->next_variable->val.integer); |
| 319 |
snmp_set_var_typed_value(var, ASN_INTEGER, |
| 320 |
(u_char *) & result, |
| 321 |
sizeof(result)); |
| 322 |
} |
| 323 |
} |
| 324 |
break; |
| 325 |
|
| 326 |
} |
| 327 |
|
| 328 |
requests = requests->next; |
| 329 |
} |
| 330 |
|
| 331 |
return SNMP_ERR_NOERROR; |
| 332 |
} |
| 333 |
|
| 334 |
#define TESTHANDLER_SET_NAME "my_test" |
| 335 |
int |
| 336 |
my_test_instance_handler(netsnmp_mib_handler *handler, |
| 337 |
netsnmp_handler_registration *reginfo, |
| 338 |
netsnmp_agent_request_info *reqinfo, |
| 339 |
netsnmp_request_info *requests) |
| 340 |
{ |
| 341 |
|
| 342 |
static u_long accesses = 0; |
| 343 |
u_long *accesses_cache = NULL; |
| 344 |
|
| 345 |
DEBUGMSGTL(("testhandler", "Got instance request:\n")); |
| 346 |
|
| 347 |
switch (reqinfo->mode) { |
| 348 |
case MODE_GET: |
| 349 |
snmp_set_var_typed_value(requests->requestvb, ASN_UNSIGNED, |
| 350 |
(u_char *) & accesses, sizeof(accesses)); |
| 351 |
break; |
| 352 |
|
| 353 |
#ifndef NETSNMP_NO_WRITE_SUPPORT |
| 354 |
case MODE_SET_RESERVE1: |
| 355 |
if (requests->requestvb->type != ASN_UNSIGNED) |
| 356 |
netsnmp_set_request_error(reqinfo, requests, |
| 357 |
SNMP_ERR_WRONGTYPE); |
| 358 |
break; |
| 359 |
|
| 360 |
case MODE_SET_RESERVE2: |
| 361 |
/* |
| 362 |
* store old info for undo later |
| 363 |
*/ |
| 364 |
accesses_cache = netsnmp_memdup(&accesses, sizeof(accesses)); |
| 365 |
if (accesses_cache == NULL) { |
| 366 |
netsnmp_set_request_error(reqinfo, requests, |
| 367 |
SNMP_ERR_RESOURCEUNAVAILABLE); |
| 368 |
return SNMP_ERR_NOERROR; |
| 369 |
} |
| 370 |
netsnmp_request_add_list_data(requests, |
| 371 |
netsnmp_create_data_list |
| 372 |
(TESTHANDLER_SET_NAME, |
| 373 |
accesses_cache, free)); |
| 374 |
break; |
| 375 |
|
| 376 |
case MODE_SET_ACTION: |
| 377 |
/* |
| 378 |
* update current |
| 379 |
*/ |
| 380 |
accesses = *(requests->requestvb->val.integer); |
| 381 |
DEBUGMSGTL(("testhandler", "updated accesses -> %lu\n", accesses)); |
| 382 |
break; |
| 383 |
|
| 384 |
case MODE_SET_UNDO: |
| 385 |
accesses = |
| 386 |
*((u_long *) netsnmp_request_get_list_data(requests, |
| 387 |
TESTHANDLER_SET_NAME)); |
| 388 |
break; |
| 389 |
|
| 390 |
case MODE_SET_COMMIT: |
| 391 |
case MODE_SET_FREE: |
| 392 |
/* |
| 393 |
* nothing to do |
| 394 |
*/ |
| 395 |
break; |
| 396 |
#endif /* NETSNMP_NO_WRITE_SUPPORT */ |
| 397 |
} |
| 398 |
|
| 399 |
return SNMP_ERR_NOERROR; |
| 400 |
} |
| 401 |
|
| 402 |
int |
| 403 |
my_data_table_handler(netsnmp_mib_handler *handler, |
| 404 |
netsnmp_handler_registration *reginfo, |
| 405 |
netsnmp_agent_request_info *reqinfo, |
| 406 |
netsnmp_request_info *requests) |
| 407 |
{ |
| 408 |
|
| 409 |
char *column3; |
| 410 |
netsnmp_table_request_info *table_info; |
| 411 |
netsnmp_table_row *row; |
| 412 |
|
| 413 |
while (requests) { |
| 414 |
if (requests->processed) { |
| 415 |
requests = requests->next; |
| 416 |
continue; |
| 417 |
} |
| 418 |
|
| 419 |
/* |
| 420 |
* extract our stored data and table info |
| 421 |
*/ |
| 422 |
row = netsnmp_extract_table_row(requests); |
| 423 |
table_info = netsnmp_extract_table_info(requests); |
| 424 |
if (!table_info || !row || !row->data) |
| 425 |
continue; |
| 426 |
column3 = (char *) row->data; |
| 427 |
|
| 428 |
/* |
| 429 |
* there's only one column, we don't need to check if it's right |
| 430 |
*/ |
| 431 |
netsnmp_table_data_build_result(reginfo, reqinfo, requests, row, |
| 432 |
table_info->colnum, |
| 433 |
ASN_OCTET_STR, (u_char*)column3, |
| 434 |
strlen(column3)); |
| 435 |
requests = requests->next; |
| 436 |
} |
| 437 |
return SNMP_ERR_NOERROR; |
| 438 |
} |