| 1 |
/* |
| 2 |
* table_container.c |
| 3 |
* $Id$ |
| 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 |
* Portions of this file are copyrighted by: |
| 10 |
* Copyright (c) 2016 VMware, Inc. All rights reserved. |
| 11 |
* Use is subject to license terms specified in the COPYING file |
| 12 |
* distributed with the Net-SNMP package. |
| 13 |
*/ |
| 14 |
|
| 15 |
#include <net-snmp/net-snmp-config.h> |
| 16 |
#include <net-snmp/net-snmp-features.h> |
| 17 |
|
| 18 |
#include <net-snmp/net-snmp-includes.h> |
| 19 |
#include <net-snmp/agent/net-snmp-agent-includes.h> |
| 20 |
|
| 21 |
#include <net-snmp/agent/table_container.h> |
| 22 |
|
| 23 |
#ifdef HAVE_STRING_H |
| 24 |
#include <string.h> |
| 25 |
#else |
| 26 |
#include <strings.h> |
| 27 |
#endif |
| 28 |
|
| 29 |
#include <net-snmp/agent/table.h> |
| 30 |
#include <net-snmp/library/container.h> |
| 31 |
#include <net-snmp/library/snmp_assert.h> |
| 32 |
|
| 33 |
netsnmp_feature_provide(table_container); |
| 34 |
netsnmp_feature_child_of(table_container, table_container_all); |
| 35 |
netsnmp_feature_child_of(table_container_replace_row, table_container_all); |
| 36 |
netsnmp_feature_child_of(table_container_extract, table_container_all); |
| 37 |
netsnmp_feature_child_of(table_container_management, table_container_all); |
| 38 |
netsnmp_feature_child_of(table_container_row_remove, table_container_all); |
| 39 |
netsnmp_feature_child_of(table_container_row_insert, table_container_all); |
| 40 |
netsnmp_feature_child_of(table_container_all, mib_helpers); |
| 41 |
|
| 42 |
#ifndef NETSNMP_FEATURE_REMOVE_TABLE_CONTAINER |
| 43 |
|
| 44 |
/* |
| 45 |
* snmp.h:#define SNMP_MSG_INTERNAL_SET_BEGIN -1 |
| 46 |
* snmp.h:#define SNMP_MSG_INTERNAL_SET_RESERVE1 0 |
| 47 |
* snmp.h:#define SNMP_MSG_INTERNAL_SET_RESERVE2 1 |
| 48 |
* snmp.h:#define SNMP_MSG_INTERNAL_SET_ACTION 2 |
| 49 |
* snmp.h:#define SNMP_MSG_INTERNAL_SET_COMMIT 3 |
| 50 |
* snmp.h:#define SNMP_MSG_INTERNAL_SET_FREE 4 |
| 51 |
* snmp.h:#define SNMP_MSG_INTERNAL_SET_UNDO 5 |
| 52 |
*/ |
| 53 |
|
| 54 |
/* |
| 55 |
* PRIVATE structure for holding important info for each table. |
| 56 |
*/ |
| 57 |
typedef struct container_table_data_s { |
| 58 |
|
| 59 |
/** Number of handlers whose myvoid pointer points to this structure. */ |
| 60 |
int refcnt; |
| 61 |
|
| 62 |
/** registration info for the table */ |
| 63 |
netsnmp_table_registration_info *tblreg_info; |
| 64 |
|
| 65 |
/** container for the table rows */ |
| 66 |
netsnmp_container *table; |
| 67 |
|
| 68 |
/* |
| 69 |
* mutex_type lock; |
| 70 |
*/ |
| 71 |
|
| 72 |
/* what type of key do we want? */ |
| 73 |
char key_type; |
| 74 |
|
| 75 |
} container_table_data; |
| 76 |
|
| 77 |
/** @defgroup table_container table_container |
| 78 |
* Helps you implement a table when data can be found via a netsnmp_container. |
| 79 |
* @ingroup table |
| 80 |
* |
| 81 |
* The table_container handler is used (automatically) in conjuntion |
| 82 |
* with the @link table table@endlink handler. |
| 83 |
* |
| 84 |
* This handler will use the index information provided by |
| 85 |
* the @link table @endlink handler to find the row needed to process |
| 86 |
* the request. |
| 87 |
* |
| 88 |
* The container must use one of 3 key types. It is the sub-handler's |
| 89 |
* responsibility to ensure that the container and key type match (unless |
| 90 |
* neither is specified, in which case a default will be used.) |
| 91 |
* |
| 92 |
* The current key types are: |
| 93 |
* |
| 94 |
* TABLE_CONTAINER_KEY_NETSNMP_INDEX |
| 95 |
* The container should do comparisons based on a key that may be cast |
| 96 |
* to a netsnmp index (netsnmp_index *). This index contains only the |
| 97 |
* index portion of the OID, not the entire OID. |
| 98 |
* |
| 99 |
* TABLE_CONTAINER_KEY_VARBIND_INDEX |
| 100 |
* The container should do comparisons based on a key that may be cast |
| 101 |
* to a netsnmp variable list (netsnmp_variable_list *). This variable |
| 102 |
* list will contain one varbind for each index component. |
| 103 |
* |
| 104 |
* TABLE_CONTAINER_KEY_VARBIND_RAW (NOTE: unimplemented) |
| 105 |
* While not yet implemented, future plans include passing the request |
| 106 |
* varbind with the full OID to a container. |
| 107 |
* |
| 108 |
* If a key type is not specified at registration time, the default key type |
| 109 |
* of TABLE_CONTAINER_KEY_NETSNMP_INDEX will be used. If a container is |
| 110 |
* provided, or the handler name is aliased to a container type, the container |
| 111 |
* must use a netsnmp index. |
| 112 |
* |
| 113 |
* If no container is provided, a lookup will be made based on the |
| 114 |
* sub-handler's name, or if that isn't found, "table_container". The |
| 115 |
* table_container key type will be netsnmp_index. |
| 116 |
* |
| 117 |
* The container must, at a minimum, implement find and find_next. If a NULL |
| 118 |
* key is passed to the container, it must return the first item, if any. |
| 119 |
* All containers provided by net-snmp fulfil this requirement. |
| 120 |
* |
| 121 |
* This handler will only register to process 'data lookup' modes. In |
| 122 |
* traditional net-snmp modes, that is any GET-like mode (GET, GET-NEXT, |
| 123 |
* GET-BULK) or the first phase of a SET (RESERVE1). In the new baby-steps |
| 124 |
* mode, DATA_LOOKUP is it's own mode, and is a pre-cursor to other modes. |
| 125 |
* |
| 126 |
* When called, the handler will call the appropriate container method |
| 127 |
* with the appropriate key type. If a row was not found, the result depends |
| 128 |
* on the mode. |
| 129 |
* |
| 130 |
* GET Processing |
| 131 |
* An exact match must be found. If one is not, the error NOSUCHINSTANCE |
| 132 |
* is set. |
| 133 |
* |
| 134 |
* GET-NEXT / GET-BULK |
| 135 |
* If no row is found, the column number will be increased (using any |
| 136 |
* valid_columns structure that may have been provided), and the first row |
| 137 |
* will be retrieved. If no first row is found, the processed flag will be |
| 138 |
* set, so that the sub-handler can skip any processing related to the |
| 139 |
* request. The agent will notice this unsatisfied request, and attempt to |
| 140 |
* pass it to the next appropriate handler. |
| 141 |
* |
| 142 |
* SET |
| 143 |
* If the hander did not register with the HANDLER_CAN_NOT_CREATE flag |
| 144 |
* set in the registration modes, it is assumed that this is a row |
| 145 |
* creation request and a NULL row is added to the request's data list. |
| 146 |
* The sub-handler is responsbile for dealing with any row creation |
| 147 |
* contraints and inserting any newly created rows into the container |
| 148 |
* and the request's data list. |
| 149 |
* |
| 150 |
* If a row is found, it will be inserted into |
| 151 |
* the request's data list. The sub-handler may retrieve it by calling |
| 152 |
* netsnmp_container_table_extract_context(request); * |
| 153 |
* NOTE NOTE NOTE: |
| 154 |
* |
| 155 |
* This helper and it's API are still being tested and are subject to change. |
| 156 |
* |
| 157 |
* @{ |
| 158 |
*/ |
| 159 |
|
| 160 |
static int |
| 161 |
_container_table_handler(netsnmp_mib_handler *handler, |
| 162 |
netsnmp_handler_registration *reginfo, |
| 163 |
netsnmp_agent_request_info *agtreq_info, |
| 164 |
netsnmp_request_info *requests); |
| 165 |
|
| 166 |
static void * |
| 167 |
_find_next_row(netsnmp_container *c, |
| 168 |
netsnmp_table_request_info *tblreq, |
| 169 |
void * key); |
| 170 |
|
| 171 |
/********************************************************************** |
| 172 |
********************************************************************** |
| 173 |
* * |
| 174 |
* * |
| 175 |
* PUBLIC Registration functions * |
| 176 |
* * |
| 177 |
* * |
| 178 |
********************************************************************** |
| 179 |
**********************************************************************/ |
| 180 |
|
| 181 |
/* ================================== |
| 182 |
* |
| 183 |
* Container Table API: Table maintenance |
| 184 |
* |
| 185 |
* ================================== */ |
| 186 |
|
| 187 |
#ifndef NETSNMP_FEATURE_REMOVE_TABLE_CONTAINER_MANAGEMENT |
| 188 |
container_table_data * |
| 189 |
netsnmp_tcontainer_create_table( const char *name, |
| 190 |
netsnmp_container *container, long flags ) |
| 191 |
{ |
| 192 |
container_table_data *table; |
| 193 |
|
| 194 |
table = SNMP_MALLOC_TYPEDEF(container_table_data); |
| 195 |
if (!table) |
| 196 |
return NULL; |
| 197 |
if (container) |
| 198 |
table->table = container; |
| 199 |
else { |
| 200 |
table->table = netsnmp_container_find("table_container"); |
| 201 |
if (!table->table) { |
| 202 |
SNMP_FREE(table); |
| 203 |
return NULL; |
| 204 |
} |
| 205 |
} |
| 206 |
|
| 207 |
if (flags) |
| 208 |
table->key_type = (char)(flags & 0x03); /* Use lowest two bits */ |
| 209 |
else |
| 210 |
table->key_type = TABLE_CONTAINER_KEY_NETSNMP_INDEX; |
| 211 |
|
| 212 |
if (!table->table->compare) |
| 213 |
table->table->compare = netsnmp_compare_netsnmp_index; |
| 214 |
if (!table->table->ncompare) |
| 215 |
table->table->ncompare = netsnmp_ncompare_netsnmp_index; |
| 216 |
|
| 217 |
return table; |
| 218 |
} |
| 219 |
|
| 220 |
void |
| 221 |
netsnmp_tcontainer_delete_table( container_table_data *table ) |
| 222 |
{ |
| 223 |
if (!table) |
| 224 |
return; |
| 225 |
|
| 226 |
if (table->table) |
| 227 |
CONTAINER_FREE(table->table); |
| 228 |
|
| 229 |
SNMP_FREE(table); |
| 230 |
return; |
| 231 |
} |
| 232 |
#endif /* NETSNMP_FEATURE_REMOVE_TABLE_CONTAINER_MANAGEMENT */ |
| 233 |
|
| 234 |
/* |
| 235 |
* The various standalone row operation routines |
| 236 |
* (create/clone/copy/delete) |
| 237 |
* will be specific to a particular table, |
| 238 |
* so can't be implemented here. |
| 239 |
*/ |
| 240 |
|
| 241 |
int |
| 242 |
netsnmp_tcontainer_add_row( container_table_data *table, netsnmp_index *row ) |
| 243 |
{ |
| 244 |
if (!table || !table->table || !row) |
| 245 |
return -1; |
| 246 |
CONTAINER_INSERT( table->table, row ); |
| 247 |
return 0; |
| 248 |
} |
| 249 |
|
| 250 |
netsnmp_index * |
| 251 |
netsnmp_tcontainer_remove_row( container_table_data *table, netsnmp_index *row ) |
| 252 |
{ |
| 253 |
if (!table || !table->table || !row) |
| 254 |
return NULL; |
| 255 |
CONTAINER_REMOVE( table->table, row ); |
| 256 |
return NULL; |
| 257 |
} |
| 258 |
|
| 259 |
#ifndef NETSNMP_FEATURE_REMOVE_TABLE_CONTAINER_REPLACE_ROW |
| 260 |
int |
| 261 |
netsnmp_tcontainer_replace_row( container_table_data *table, |
| 262 |
netsnmp_index *old_row, netsnmp_index *new_row ) |
| 263 |
{ |
| 264 |
if (!table || !table->table || !old_row || !new_row) |
| 265 |
return -1; |
| 266 |
netsnmp_tcontainer_remove_row( table, old_row ); |
| 267 |
netsnmp_tcontainer_add_row( table, new_row ); |
| 268 |
return 0; |
| 269 |
} |
| 270 |
#endif /* NETSNMP_FEATURE_REMOVE_TABLE_CONTAINER_REPLACE_ROW */ |
| 271 |
|
| 272 |
/* netsnmp_tcontainer_remove_delete_row() will be table-specific too */ |
| 273 |
|
| 274 |
|
| 275 |
/* ================================== |
| 276 |
* |
| 277 |
* Container Table API: MIB maintenance |
| 278 |
* |
| 279 |
* ================================== */ |
| 280 |
|
| 281 |
static void * |
| 282 |
netsnmp_container_table_data_clone(void *p) |
| 283 |
{ |
| 284 |
container_table_data *tad = p; |
| 285 |
|
| 286 |
++tad->refcnt; |
| 287 |
return tad; |
| 288 |
} |
| 289 |
|
| 290 |
static void |
| 291 |
netsnmp_container_table_data_free(void *p) |
| 292 |
{ |
| 293 |
container_table_data *tad = p; |
| 294 |
|
| 295 |
if (--tad->refcnt == 0) |
| 296 |
free(tad); |
| 297 |
} |
| 298 |
|
| 299 |
/** returns a netsnmp_mib_handler object for the table_container helper */ |
| 300 |
netsnmp_mib_handler * |
| 301 |
netsnmp_container_table_handler_get(netsnmp_table_registration_info *tabreg, |
| 302 |
netsnmp_container *container, char key_type) |
| 303 |
{ |
| 304 |
container_table_data *tad; |
| 305 |
netsnmp_mib_handler *handler; |
| 306 |
|
| 307 |
if (NULL == tabreg) { |
| 308 |
snmp_log(LOG_ERR, "bad param in netsnmp_container_table_register\n"); |
| 309 |
return NULL; |
| 310 |
} |
| 311 |
|
| 312 |
tad = SNMP_MALLOC_TYPEDEF(container_table_data); |
| 313 |
handler = netsnmp_create_handler("table_container", |
| 314 |
_container_table_handler); |
| 315 |
if((NULL == tad) || (NULL == handler)) { |
| 316 |
if(tad) free(tad); /* SNMP_FREE wasted on locals */ |
| 317 |
if(handler) free(handler); /* SNMP_FREE wasted on locals */ |
| 318 |
snmp_log(LOG_ERR, |
| 319 |
"malloc failure in netsnmp_container_table_register\n"); |
| 320 |
return NULL; |
| 321 |
} |
| 322 |
|
| 323 |
tad->refcnt = 1; |
| 324 |
tad->tblreg_info = tabreg; /* we need it too, but it really is not ours */ |
| 325 |
if(key_type) |
| 326 |
tad->key_type = key_type; |
| 327 |
else |
| 328 |
tad->key_type = TABLE_CONTAINER_KEY_NETSNMP_INDEX; |
| 329 |
|
| 330 |
if(NULL == container) |
| 331 |
container = netsnmp_container_find("table_container"); |
| 332 |
tad->table = container; |
| 333 |
|
| 334 |
if (NULL==container->compare) |
| 335 |
container->compare = netsnmp_compare_netsnmp_index; |
| 336 |
if (NULL==container->ncompare) |
| 337 |
container->ncompare = netsnmp_ncompare_netsnmp_index; |
| 338 |
|
| 339 |
handler->myvoid = (void*)tad; |
| 340 |
handler->data_clone = netsnmp_container_table_data_clone; |
| 341 |
handler->data_free = netsnmp_container_table_data_free; |
| 342 |
handler->flags |= MIB_HANDLER_AUTO_NEXT; |
| 343 |
|
| 344 |
return handler; |
| 345 |
} |
| 346 |
|
| 347 |
int |
| 348 |
netsnmp_container_table_register(netsnmp_handler_registration *reginfo, |
| 349 |
netsnmp_table_registration_info *tabreg, |
| 350 |
netsnmp_container *container, char key_type ) |
| 351 |
{ |
| 352 |
netsnmp_mib_handler *handler; |
| 353 |
|
| 354 |
if ((NULL == reginfo) || (NULL == reginfo->handler) || (NULL == tabreg)) { |
| 355 |
snmp_log(LOG_ERR, "bad param in netsnmp_container_table_register\n"); |
| 356 |
netsnmp_handler_registration_free(reginfo); |
| 357 |
return SNMPERR_GENERR; |
| 358 |
} |
| 359 |
|
| 360 |
if (NULL==container) |
| 361 |
container = netsnmp_container_find(reginfo->handlerName); |
| 362 |
|
| 363 |
handler = netsnmp_container_table_handler_get(tabreg, container, key_type); |
| 364 |
if (!handler || |
| 365 |
(netsnmp_inject_handler(reginfo, handler) != SNMPERR_SUCCESS)) { |
| 366 |
snmp_log(LOG_ERR, "could not create container table handler\n"); |
| 367 |
netsnmp_handler_free(handler); |
| 368 |
netsnmp_handler_registration_free(reginfo); |
| 369 |
return MIB_REGISTRATION_FAILED; |
| 370 |
} |
| 371 |
|
| 372 |
return netsnmp_register_table(reginfo, tabreg); |
| 373 |
} |
| 374 |
|
| 375 |
int |
| 376 |
netsnmp_container_table_unregister(netsnmp_handler_registration *reginfo) |
| 377 |
{ |
| 378 |
container_table_data *tad; |
| 379 |
|
| 380 |
if (!reginfo) |
| 381 |
return MIB_UNREGISTRATION_FAILED; |
| 382 |
tad = (container_table_data *) |
| 383 |
netsnmp_find_handler_data_by_name(reginfo, "table_container"); |
| 384 |
if (tad) { |
| 385 |
CONTAINER_FREE( tad->table ); |
| 386 |
tad->table = NULL; |
| 387 |
/* |
| 388 |
* Note: don't free the memory tad points at here - that is done |
| 389 |
* by netsnmp_container_table_data_free(). |
| 390 |
*/ |
| 391 |
} |
| 392 |
return netsnmp_unregister_table( reginfo ); |
| 393 |
} |
| 394 |
|
| 395 |
/** retrieve the container used by the table_container helper */ |
| 396 |
#ifndef NETSNMP_FEATURE_REMOVE_TABLE_CONTAINER_EXTRACT |
| 397 |
netsnmp_container* |
| 398 |
netsnmp_container_table_container_extract(netsnmp_request_info *request) |
| 399 |
{ |
| 400 |
return (netsnmp_container *) |
| 401 |
netsnmp_request_get_list_data(request, TABLE_CONTAINER_CONTAINER); |
| 402 |
} |
| 403 |
#endif /* NETSNMP_FEATURE_REMOVE_TABLE_CONTAINER_EXTRACT */ |
| 404 |
|
| 405 |
#ifndef NETSNMP_USE_INLINE |
| 406 |
/** find the context data used by the table_container helper */ |
| 407 |
void * |
| 408 |
netsnmp_container_table_row_extract(netsnmp_request_info *request) |
| 409 |
{ |
| 410 |
/* |
| 411 |
* NOTE: this function must match in table_container.c and table_container.h. |
| 412 |
* if you change one, change them both! |
| 413 |
*/ |
| 414 |
return netsnmp_request_get_list_data(request, TABLE_CONTAINER_ROW); |
| 415 |
} |
| 416 |
/** find the context data used by the table_container helper */ |
| 417 |
void * |
| 418 |
netsnmp_container_table_extract_context(netsnmp_request_info *request) |
| 419 |
{ |
| 420 |
/* |
| 421 |
* NOTE: this function must match in table_container.c and table_container.h. |
| 422 |
* if you change one, change them both! |
| 423 |
*/ |
| 424 |
return netsnmp_request_get_list_data(request, TABLE_CONTAINER_ROW); |
| 425 |
} |
| 426 |
#endif /* inline */ |
| 427 |
|
| 428 |
#ifndef NETSNMP_FEATURE_REMOVE_TABLE_CONTAINER_ROW_INSERT |
| 429 |
/** inserts a newly created table_container entry into a request list */ |
| 430 |
void |
| 431 |
netsnmp_container_table_row_insert(netsnmp_request_info *request, |
| 432 |
netsnmp_index *row) |
| 433 |
{ |
| 434 |
netsnmp_request_info *req; |
| 435 |
netsnmp_table_request_info *table_info = NULL; |
| 436 |
netsnmp_variable_list *this_index = NULL; |
| 437 |
netsnmp_variable_list *that_index = NULL; |
| 438 |
oid base_oid[] = {0, 0}; /* Make sure index OIDs are legal! */ |
| 439 |
oid this_oid[MAX_OID_LEN]; |
| 440 |
oid that_oid[MAX_OID_LEN]; |
| 441 |
size_t this_oid_len, that_oid_len; |
| 442 |
|
| 443 |
if (!request) |
| 444 |
return; |
| 445 |
|
| 446 |
/* |
| 447 |
* We'll add the new row information to any request |
| 448 |
* structure with the same index values as the request |
| 449 |
* passed in (which includes that one!). |
| 450 |
* |
| 451 |
* So construct an OID based on these index values. |
| 452 |
*/ |
| 453 |
|
| 454 |
table_info = netsnmp_extract_table_info(request); |
| 455 |
this_index = table_info->indexes; |
| 456 |
build_oid_noalloc(this_oid, MAX_OID_LEN, &this_oid_len, |
| 457 |
base_oid, 2, this_index); |
| 458 |
|
| 459 |
/* |
| 460 |
* We need to look through the whole of the request list |
| 461 |
* (as received by the current handler), as there's no |
| 462 |
* guarantee that this routine will be called by the first |
| 463 |
* varbind that refers to this row. |
| 464 |
* In particular, a RowStatus controlled row creation |
| 465 |
* may easily occur later in the variable list. |
| 466 |
* |
| 467 |
* So first, we rewind to the head of the list.... |
| 468 |
*/ |
| 469 |
for (req=request; req->prev; req=req->prev) |
| 470 |
; |
| 471 |
|
| 472 |
/* |
| 473 |
* ... and then start looking for matching indexes |
| 474 |
* (by constructing OIDs from these index values) |
| 475 |
*/ |
| 476 |
for (; req; req=req->next) { |
| 477 |
if (req->processed) |
| 478 |
continue; |
| 479 |
|
| 480 |
table_info = netsnmp_extract_table_info(req); |
| 481 |
that_index = table_info->indexes; |
| 482 |
build_oid_noalloc(that_oid, MAX_OID_LEN, &that_oid_len, |
| 483 |
base_oid, 2, that_index); |
| 484 |
|
| 485 |
/* |
| 486 |
* This request has the same index values, |
| 487 |
* so add the newly-created row information. |
| 488 |
*/ |
| 489 |
if (snmp_oid_compare(this_oid, this_oid_len, |
| 490 |
that_oid, that_oid_len) == 0) { |
| 491 |
netsnmp_request_add_list_data(req, |
| 492 |
netsnmp_create_data_list(TABLE_CONTAINER_ROW, row, NULL)); |
| 493 |
} |
| 494 |
} |
| 495 |
} |
| 496 |
#endif /* NETSNMP_FEATURE_REMOVE_TABLE_CONTAINER_ROW_INSERT */ |
| 497 |
|
| 498 |
#ifndef NETSNMP_FEATURE_REMOVE_TABLE_CONTAINER_ROW_REMOVE |
| 499 |
/** removes a table_container entry from a request list */ |
| 500 |
void |
| 501 |
netsnmp_container_table_row_remove(netsnmp_request_info *request, |
| 502 |
netsnmp_index *row) |
| 503 |
{ |
| 504 |
netsnmp_request_info *req; |
| 505 |
netsnmp_table_request_info *table_info = NULL; |
| 506 |
netsnmp_variable_list *this_index = NULL; |
| 507 |
netsnmp_variable_list *that_index = NULL; |
| 508 |
oid base_oid[] = {0, 0}; /* Make sure index OIDs are legal! */ |
| 509 |
oid this_oid[MAX_OID_LEN]; |
| 510 |
oid that_oid[MAX_OID_LEN]; |
| 511 |
size_t this_oid_len, that_oid_len; |
| 512 |
|
| 513 |
if (!request) |
| 514 |
return; |
| 515 |
|
| 516 |
/* |
| 517 |
* We'll add the new row information to any request |
| 518 |
* structure with the same index values as the request |
| 519 |
* passed in (which includes that one!). |
| 520 |
* |
| 521 |
* So construct an OID based on these index values. |
| 522 |
*/ |
| 523 |
|
| 524 |
table_info = netsnmp_extract_table_info(request); |
| 525 |
this_index = table_info->indexes; |
| 526 |
build_oid_noalloc(this_oid, MAX_OID_LEN, &this_oid_len, |
| 527 |
base_oid, 2, this_index); |
| 528 |
|
| 529 |
/* |
| 530 |
* We need to look through the whole of the request list |
| 531 |
* (as received by the current handler), as there's no |
| 532 |
* guarantee that this routine will be called by the first |
| 533 |
* varbind that refers to this row. |
| 534 |
* In particular, a RowStatus controlled row creation |
| 535 |
* may easily occur later in the variable list. |
| 536 |
* |
| 537 |
* So first, we rewind to the head of the list.... |
| 538 |
*/ |
| 539 |
for (req=request; req->prev; req=req->prev) |
| 540 |
; |
| 541 |
|
| 542 |
/* |
| 543 |
* ... and then start looking for matching indexes |
| 544 |
* (by constructing OIDs from these index values) |
| 545 |
*/ |
| 546 |
for (; req; req=req->next) { |
| 547 |
if (req->processed) |
| 548 |
continue; |
| 549 |
|
| 550 |
table_info = netsnmp_extract_table_info(req); |
| 551 |
that_index = table_info->indexes; |
| 552 |
build_oid_noalloc(that_oid, MAX_OID_LEN, &that_oid_len, |
| 553 |
base_oid, 2, that_index); |
| 554 |
|
| 555 |
/* |
| 556 |
* This request has the same index values, |
| 557 |
* so add the newly-created row information. |
| 558 |
*/ |
| 559 |
if (snmp_oid_compare(this_oid, this_oid_len, |
| 560 |
that_oid, that_oid_len) == 0) { |
| 561 |
netsnmp_request_remove_list_data(req, TABLE_CONTAINER_ROW); |
| 562 |
} |
| 563 |
} |
| 564 |
} |
| 565 |
#endif /* NETSNMP_FEATURE_REMOVE_TABLE_CONTAINER_ROW_REMOVE */ |
| 566 |
|
| 567 |
/** @cond */ |
| 568 |
/********************************************************************** |
| 569 |
********************************************************************** |
| 570 |
* * |
| 571 |
* * |
| 572 |
* DATA LOOKUP functions * |
| 573 |
* * |
| 574 |
* * |
| 575 |
********************************************************************** |
| 576 |
**********************************************************************/ |
| 577 |
NETSNMP_STATIC_INLINE void |
| 578 |
_set_key( container_table_data * tad, netsnmp_request_info *request, |
| 579 |
netsnmp_table_request_info *tblreq_info, |
| 580 |
void **key, netsnmp_index *index ) |
| 581 |
{ |
| 582 |
if (TABLE_CONTAINER_KEY_NETSNMP_INDEX == tad->key_type) { |
| 583 |
index->oids = tblreq_info->index_oid; |
| 584 |
index->len = tblreq_info->index_oid_len; |
| 585 |
*key = index; |
| 586 |
} |
| 587 |
else if (TABLE_CONTAINER_KEY_VARBIND_INDEX == tad->key_type) { |
| 588 |
*key = tblreq_info->indexes; |
| 589 |
} |
| 590 |
#if 0 |
| 591 |
else if (TABLE_CONTAINER_KEY_VARBIND_RAW == tad->key_type) { |
| 592 |
*key = request->requestvb; |
| 593 |
} |
| 594 |
#endif |
| 595 |
else |
| 596 |
*key = NULL; |
| 597 |
} |
| 598 |
|
| 599 |
|
| 600 |
NETSNMP_STATIC_INLINE void |
| 601 |
_data_lookup(netsnmp_handler_registration *reginfo, |
| 602 |
netsnmp_agent_request_info *agtreq_info, |
| 603 |
netsnmp_request_info *request, container_table_data * tad) |
| 604 |
{ |
| 605 |
netsnmp_index *row = NULL; |
| 606 |
netsnmp_table_request_info *tblreq_info; |
| 607 |
netsnmp_variable_list *var; |
| 608 |
netsnmp_index index; |
| 609 |
void *key; |
| 610 |
|
| 611 |
var = request->requestvb; |
| 612 |
|
| 613 |
DEBUGIF("table_container") { |
| 614 |
DEBUGMSGTL(("table_container", " data_lookup oid:")); |
| 615 |
DEBUGMSGOID(("table_container", var->name, var->name_length)); |
| 616 |
DEBUGMSG(("table_container", "\n")); |
| 617 |
} |
| 618 |
|
| 619 |
/* |
| 620 |
* Get pointer to the table information for this request. This |
| 621 |
* information was saved by table_helper_handler. |
| 622 |
*/ |
| 623 |
tblreq_info = netsnmp_extract_table_info(request); |
| 624 |
/** the table_helper_handler should enforce column boundaries. */ |
| 625 |
netsnmp_assert((NULL != tblreq_info) && |
| 626 |
(tblreq_info->colnum <= tad->tblreg_info->max_column)); |
| 627 |
|
| 628 |
if ((agtreq_info->mode == MODE_GETNEXT) || |
| 629 |
(agtreq_info->mode == MODE_GETBULK)) { |
| 630 |
/* |
| 631 |
* find the row. This will automatically move to the next |
| 632 |
* column, if necessary. |
| 633 |
*/ |
| 634 |
_set_key( tad, request, tblreq_info, &key, &index ); |
| 635 |
row = (netsnmp_index*)_find_next_row(tad->table, tblreq_info, key); |
| 636 |
if (row) { |
| 637 |
/* |
| 638 |
* update indexes in tblreq_info (index & varbind), |
| 639 |
* then update request varbind oid |
| 640 |
*/ |
| 641 |
if(TABLE_CONTAINER_KEY_NETSNMP_INDEX == tad->key_type) { |
| 642 |
tblreq_info->index_oid_len = row->len; |
| 643 |
memcpy(tblreq_info->index_oid, row->oids, |
| 644 |
row->len * sizeof(oid)); |
| 645 |
netsnmp_update_variable_list_from_index(tblreq_info); |
| 646 |
} |
| 647 |
else if (TABLE_CONTAINER_KEY_VARBIND_INDEX == tad->key_type) { |
| 648 |
/** xxx-rks: shouldn't tblreq_info->indexes be updated |
| 649 |
before we call this?? */ |
| 650 |
netsnmp_update_indexes_from_variable_list(tblreq_info); |
| 651 |
} |
| 652 |
|
| 653 |
if (TABLE_CONTAINER_KEY_VARBIND_RAW != tad->key_type) { |
| 654 |
netsnmp_table_build_oid_from_index(reginfo, request, |
| 655 |
tblreq_info); |
| 656 |
} |
| 657 |
} |
| 658 |
else { |
| 659 |
/* |
| 660 |
* no results found. Flag the request so lower handlers will |
| 661 |
* ignore it, but it is not an error - getnext will move |
| 662 |
* on to another handler to process this request. |
| 663 |
*/ |
| 664 |
netsnmp_set_request_error(agtreq_info, request, SNMP_ENDOFMIBVIEW); |
| 665 |
DEBUGMSGTL(("table_container", "no row found\n")); |
| 666 |
} |
| 667 |
} /** GETNEXT/GETBULK */ |
| 668 |
else { |
| 669 |
|
| 670 |
_set_key( tad, request, tblreq_info, &key, &index ); |
| 671 |
row = (netsnmp_index*)CONTAINER_FIND(tad->table, key); |
| 672 |
if (NULL == row) { |
| 673 |
/* |
| 674 |
* not results found. For a get, that is an error |
| 675 |
*/ |
| 676 |
DEBUGMSGTL(("table_container", "no row found\n")); |
| 677 |
#ifndef NETSNMP_NO_WRITE_SUPPORT |
| 678 |
if((agtreq_info->mode != MODE_SET_RESERVE1) || /* get */ |
| 679 |
(reginfo->modes & HANDLER_CAN_NOT_CREATE)) { /* no create */ |
| 680 |
#endif /* NETSNMP_NO_WRITE_SUPPORT */ |
| 681 |
netsnmp_set_request_error(agtreq_info, request, |
| 682 |
SNMP_NOSUCHINSTANCE); |
| 683 |
#ifndef NETSNMP_NO_WRITE_SUPPORT |
| 684 |
} |
| 685 |
#endif /* NETSNMP_NO_WRITE_SUPPORT */ |
| 686 |
} |
| 687 |
} /** GET/SET */ |
| 688 |
|
| 689 |
/* |
| 690 |
* save the data and table in the request. |
| 691 |
*/ |
| 692 |
if (SNMP_ENDOFMIBVIEW != request->requestvb->type) { |
| 693 |
if (NULL != row) |
| 694 |
netsnmp_request_add_list_data(request, |
| 695 |
netsnmp_create_data_list |
| 696 |
(TABLE_CONTAINER_ROW, |
| 697 |
row, NULL)); |
| 698 |
netsnmp_request_add_list_data(request, |
| 699 |
netsnmp_create_data_list |
| 700 |
(TABLE_CONTAINER_CONTAINER, |
| 701 |
tad->table, NULL)); |
| 702 |
} |
| 703 |
} |
| 704 |
|
| 705 |
/********************************************************************** |
| 706 |
********************************************************************** |
| 707 |
* * |
| 708 |
* * |
| 709 |
* netsnmp_table_container_helper_handler() * |
| 710 |
* * |
| 711 |
* * |
| 712 |
********************************************************************** |
| 713 |
**********************************************************************/ |
| 714 |
static int |
| 715 |
_container_table_handler(netsnmp_mib_handler *handler, |
| 716 |
netsnmp_handler_registration *reginfo, |
| 717 |
netsnmp_agent_request_info *agtreq_info, |
| 718 |
netsnmp_request_info *requests) |
| 719 |
{ |
| 720 |
int rc = SNMP_ERR_NOERROR; |
| 721 |
int oldmode, need_processing = 0; |
| 722 |
container_table_data *tad; |
| 723 |
|
| 724 |
/** sanity checks */ |
| 725 |
netsnmp_assert((NULL != handler) && (NULL != handler->myvoid)); |
| 726 |
netsnmp_assert((NULL != reginfo) && (NULL != agtreq_info)); |
| 727 |
|
| 728 |
DEBUGMSGTL(("table_container", "Mode %s, Got request:\n", |
| 729 |
se_find_label_in_slist("agent_mode",agtreq_info->mode))); |
| 730 |
|
| 731 |
/* |
| 732 |
* First off, get our pointer from the handler. This |
| 733 |
* lets us get to the table registration information we |
| 734 |
* saved in get_table_container_handler(), as well as the |
| 735 |
* container where the actual table data is stored. |
| 736 |
*/ |
| 737 |
tad = (container_table_data *)handler->myvoid; |
| 738 |
|
| 739 |
/* |
| 740 |
* only do data lookup for first pass |
| 741 |
* |
| 742 |
* xxx-rks: this should really be handled up one level. we should |
| 743 |
* be able to say what modes we want to be called for during table |
| 744 |
* registration. |
| 745 |
*/ |
| 746 |
oldmode = agtreq_info->mode; |
| 747 |
if(MODE_IS_GET(oldmode) |
| 748 |
#ifndef NETSNMP_NO_WRITE_SUPPORT |
| 749 |
|| (MODE_SET_RESERVE1 == oldmode) |
| 750 |
#endif /* NETSNMP_NO_WRITE_SUPPORT */ |
| 751 |
) { |
| 752 |
netsnmp_request_info *curr_request; |
| 753 |
/* |
| 754 |
* Loop through each of the requests, and |
| 755 |
* try to find the appropriate row from the container. |
| 756 |
*/ |
| 757 |
for (curr_request = requests; curr_request; curr_request = curr_request->next) { |
| 758 |
/* |
| 759 |
* skip anything that doesn't need processing. |
| 760 |
*/ |
| 761 |
if (curr_request->processed != 0) { |
| 762 |
DEBUGMSGTL(("table_container", "already processed\n")); |
| 763 |
continue; |
| 764 |
} |
| 765 |
|
| 766 |
/* |
| 767 |
* find data for this request |
| 768 |
*/ |
| 769 |
_data_lookup(reginfo, agtreq_info, curr_request, tad); |
| 770 |
|
| 771 |
if(curr_request->processed) |
| 772 |
continue; |
| 773 |
|
| 774 |
++need_processing; |
| 775 |
} /** for ( ... requests ... ) */ |
| 776 |
} |
| 777 |
|
| 778 |
/* |
| 779 |
* send GET instead of GETNEXT to sub-handlers |
| 780 |
* xxx-rks: again, this should be handled further up. |
| 781 |
*/ |
| 782 |
if ((oldmode == MODE_GETNEXT) && (handler->next)) { |
| 783 |
/* |
| 784 |
* tell agent handlder not to auto call next handler |
| 785 |
*/ |
| 786 |
handler->flags |= MIB_HANDLER_AUTO_NEXT_OVERRIDE_ONCE; |
| 787 |
|
| 788 |
/* |
| 789 |
* if we found rows to process, pretend to be a get request |
| 790 |
* and call handler below us. |
| 791 |
*/ |
| 792 |
if(need_processing > 0) { |
| 793 |
agtreq_info->mode = MODE_GET; |
| 794 |
rc = netsnmp_call_next_handler(handler, reginfo, agtreq_info, |
| 795 |
requests); |
| 796 |
if (rc != SNMP_ERR_NOERROR) { |
| 797 |
DEBUGMSGTL(("table_container", |
| 798 |
"next handler returned %d\n", rc)); |
| 799 |
} |
| 800 |
|
| 801 |
agtreq_info->mode = oldmode; /* restore saved mode */ |
| 802 |
} |
| 803 |
} |
| 804 |
|
| 805 |
return rc; |
| 806 |
} |
| 807 |
/** @endcond */ |
| 808 |
|
| 809 |
|
| 810 |
/* ================================== |
| 811 |
* |
| 812 |
* Container Table API: Row operations |
| 813 |
* |
| 814 |
* ================================== */ |
| 815 |
|
| 816 |
static void * |
| 817 |
_find_next_row(netsnmp_container *c, |
| 818 |
netsnmp_table_request_info *tblreq, |
| 819 |
void * key) |
| 820 |
{ |
| 821 |
void *row = NULL; |
| 822 |
|
| 823 |
if (!c || !tblreq || !tblreq->reg_info ) { |
| 824 |
snmp_log(LOG_ERR,"_find_next_row param error\n"); |
| 825 |
return NULL; |
| 826 |
} |
| 827 |
|
| 828 |
/* |
| 829 |
* table helper should have made sure we aren't below our minimum column |
| 830 |
*/ |
| 831 |
netsnmp_assert(tblreq->colnum >= tblreq->reg_info->min_column); |
| 832 |
|
| 833 |
/* |
| 834 |
* if no indexes then use first row. |
| 835 |
*/ |
| 836 |
if(tblreq->number_indexes == 0) { |
| 837 |
row = CONTAINER_FIRST(c); |
| 838 |
} else { |
| 839 |
|
| 840 |
if(NULL == key) { |
| 841 |
netsnmp_index index; |
| 842 |
index.oids = tblreq->index_oid; |
| 843 |
index.len = tblreq->index_oid_len; |
| 844 |
row = CONTAINER_NEXT(c, &index); |
| 845 |
} |
| 846 |
else |
| 847 |
row = CONTAINER_NEXT(c, key); |
| 848 |
|
| 849 |
/* |
| 850 |
* we don't have a row, but we might be at the end of a |
| 851 |
* column, so try the next column. |
| 852 |
*/ |
| 853 |
if (NULL == row) { |
| 854 |
/* |
| 855 |
* don't set tblreq next_col unless we know there is one, |
| 856 |
* so we don't mess up table handler sparse table processing. |
| 857 |
*/ |
| 858 |
oid next_col = netsnmp_table_next_column(tblreq); |
| 859 |
if (0 != next_col) { |
| 860 |
tblreq->colnum = next_col; |
| 861 |
row = CONTAINER_FIRST(c); |
| 862 |
} |
| 863 |
} |
| 864 |
} |
| 865 |
|
| 866 |
return row; |
| 867 |
} |
| 868 |
|
| 869 |
/** |
| 870 |
* deprecated, backwards compatability only |
| 871 |
* |
| 872 |
* expected impact to remove: none |
| 873 |
* - used between helpers, shouldn't have been used by end users |
| 874 |
* |
| 875 |
* replacement: none |
| 876 |
* - never should have been a public method in the first place |
| 877 |
*/ |
| 878 |
netsnmp_index * |
| 879 |
netsnmp_table_index_find_next_row(netsnmp_container *c, |
| 880 |
netsnmp_table_request_info *tblreq) |
| 881 |
{ |
| 882 |
return (netsnmp_index*)_find_next_row(c, tblreq, NULL ); |
| 883 |
} |
| 884 |
|
| 885 |
/* ================================== |
| 886 |
* |
| 887 |
* Container Table API: Index operations |
| 888 |
* |
| 889 |
* ================================== */ |
| 890 |
|
| 891 |
#else /* NETSNMP_FEATURE_REMOVE_TABLE_CONTAINER */ |
| 892 |
netsnmp_feature_unused(table_container); |
| 893 |
#endif /* NETSNMP_FEATURE_REMOVE_TABLE_CONTAINER */ |
| 894 |
/** @} */ |
| 895 |
|
| 896 |
|