| 1 |
/* |
| 2 |
* table_array.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_array.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_child_of(table_array_all, mib_helpers); |
| 34 |
|
| 35 |
netsnmp_feature_child_of(table_array_register,table_array_all); |
| 36 |
netsnmp_feature_child_of(table_array_find_table_array_handler,table_array_all); |
| 37 |
netsnmp_feature_child_of(table_array_extract_array_context,table_array_all); |
| 38 |
netsnmp_feature_child_of(table_array_check_row_status,table_array_all); |
| 39 |
|
| 40 |
#ifndef NETSNMP_FEATURE_REMOVE_TABLE_CONTAINER |
| 41 |
|
| 42 |
/* |
| 43 |
* snmp.h:#define SNMP_MSG_INTERNAL_SET_BEGIN -1 |
| 44 |
* snmp.h:#define SNMP_MSG_INTERNAL_SET_RESERVE1 0 |
| 45 |
* snmp.h:#define SNMP_MSG_INTERNAL_SET_RESERVE2 1 |
| 46 |
* snmp.h:#define SNMP_MSG_INTERNAL_SET_ACTION 2 |
| 47 |
* snmp.h:#define SNMP_MSG_INTERNAL_SET_COMMIT 3 |
| 48 |
* snmp.h:#define SNMP_MSG_INTERNAL_SET_FREE 4 |
| 49 |
* snmp.h:#define SNMP_MSG_INTERNAL_SET_UNDO 5 |
| 50 |
*/ |
| 51 |
|
| 52 |
static const char *mode_name[] = { |
| 53 |
"Reserve 1", |
| 54 |
"Reserve 2", |
| 55 |
"Action", |
| 56 |
"Commit", |
| 57 |
"Free", |
| 58 |
"Undo" |
| 59 |
}; |
| 60 |
|
| 61 |
/* |
| 62 |
* PRIVATE structure for holding important info for each table. |
| 63 |
*/ |
| 64 |
typedef struct table_container_data_s { |
| 65 |
|
| 66 |
/** registration info for the table */ |
| 67 |
netsnmp_table_registration_info *tblreg_info; |
| 68 |
|
| 69 |
/** container for the table rows */ |
| 70 |
netsnmp_container *table; |
| 71 |
|
| 72 |
/* |
| 73 |
* mutex_type lock; |
| 74 |
*/ |
| 75 |
|
| 76 |
/** do we want to group rows with the same index |
| 77 |
* together when calling callbacks? */ |
| 78 |
int group_rows; |
| 79 |
|
| 80 |
/** callbacks for this table */ |
| 81 |
netsnmp_table_array_callbacks *cb; |
| 82 |
|
| 83 |
} table_container_data; |
| 84 |
|
| 85 |
/** @defgroup table_array table_array |
| 86 |
* Helps you implement a table when data can be stored locally. The data is stored in a sorted array, using a binary search for lookups. |
| 87 |
* @ingroup table |
| 88 |
* |
| 89 |
* The table_array handler is used (automatically) in conjuntion |
| 90 |
* with the @link table table@endlink handler. It is primarily |
| 91 |
* intended to be used with the mib2c configuration file |
| 92 |
* mib2c.array-user.conf. |
| 93 |
* |
| 94 |
* The code generated by mib2c is useful when you have control of |
| 95 |
* the data for each row. If you cannot control when rows are added |
| 96 |
* and deleted (or at least be notified of changes to row data), |
| 97 |
* then this handler is probably not for you. |
| 98 |
* |
| 99 |
* This handler makes use of callbacks (function pointers) to |
| 100 |
* handle various tasks. Code is generated for each callback, |
| 101 |
* but will need to be reviewed and flushed out by the user. |
| 102 |
* |
| 103 |
* NOTE NOTE NOTE: Once place where mib2c is somewhat lacking |
| 104 |
* is with regards to tables with external indices. If your |
| 105 |
* table makes use of one or more external indices, please |
| 106 |
* review the generated code very carefully for comments |
| 107 |
* regarding external indices. |
| 108 |
* |
| 109 |
* NOTE NOTE NOTE: This helper, the API and callbacks are still |
| 110 |
* being tested and may change. |
| 111 |
* |
| 112 |
* The generated code will define a structure for storage of table |
| 113 |
* related data. This structure must be used, as it contains the index |
| 114 |
* OID for the row, which is used for keeping the array sorted. You can |
| 115 |
* add addition fields or data to the structure for your own use. |
| 116 |
* |
| 117 |
* The generated code will also have code to handle SNMP-SET processing. |
| 118 |
* If your table does not support any SET operations, simply comment |
| 119 |
* out the \#define \<PREFIX\>_SET_HANDLING (where \<PREFIX\> is your |
| 120 |
* table name) in the header file. |
| 121 |
* |
| 122 |
* SET processing modifies the row in-place. The duplicate_row |
| 123 |
* callback will be called to save a copy of the original row. |
| 124 |
* In the event of a failure before the commite phase, the |
| 125 |
* row_copy callback will be called to restore the original row |
| 126 |
* from the copy. |
| 127 |
* |
| 128 |
* Code will be generated to handle row creation. This code may be |
| 129 |
* disabled by commenting out the \#define \<PREFIX\>_ROW_CREATION |
| 130 |
* in the header file. |
| 131 |
* |
| 132 |
* If your table contains a RowStatus object, by default the |
| 133 |
* code will not allow object in an active row to be modified. |
| 134 |
* To allow active rows to be modified, remove the comment block |
| 135 |
* around the \#define \<PREFIX\>_CAN_MODIFY_ACTIVE_ROW in the header |
| 136 |
* file. |
| 137 |
* |
| 138 |
* Code will be generated to maintain a secondary index for all |
| 139 |
* rows, stored in a binary tree. This is very useful for finding |
| 140 |
* rows by a key other than the OID index. By default, the functions |
| 141 |
* for maintaining this tree will be based on a character string. |
| 142 |
* NOTE: this will likely be made into a more generic mechanism, |
| 143 |
* using new callback methods, in the near future. |
| 144 |
* |
| 145 |
* The generated code contains many TODO comments. Make sure you |
| 146 |
* check each one to see if it applies to your code. Examples include |
| 147 |
* checking indices for syntax (ranges, etc), initializing default |
| 148 |
* values in newly created rows, checking for row activation and |
| 149 |
* deactivation requirements, etc. |
| 150 |
* |
| 151 |
* @{ |
| 152 |
*/ |
| 153 |
|
| 154 |
/********************************************************************** |
| 155 |
********************************************************************** |
| 156 |
* * |
| 157 |
* * |
| 158 |
* PUBLIC Registration functions * |
| 159 |
* * |
| 160 |
* * |
| 161 |
********************************************************************** |
| 162 |
**********************************************************************/ |
| 163 |
/** register specified callbacks for the specified table/oid. If the |
| 164 |
group_rows parameter is set, the row related callbacks will be |
| 165 |
called once for each unique row index. Otherwise, each callback |
| 166 |
will be called only once, for all objects. |
| 167 |
*/ |
| 168 |
int |
| 169 |
netsnmp_table_container_register(netsnmp_handler_registration *reginfo, |
| 170 |
netsnmp_table_registration_info *tabreg, |
| 171 |
netsnmp_table_array_callbacks *cb, |
| 172 |
netsnmp_container *container, |
| 173 |
int group_rows) |
| 174 |
{ |
| 175 |
table_container_data *tad = SNMP_MALLOC_TYPEDEF(table_container_data); |
| 176 |
if (!tad) |
| 177 |
return SNMPERR_GENERR; |
| 178 |
tad->tblreg_info = tabreg; /* we need it too, but it really is not ours */ |
| 179 |
|
| 180 |
if (!cb) { |
| 181 |
snmp_log(LOG_ERR, "table_array registration with no callbacks\n" ); |
| 182 |
free(tad); /* SNMP_FREE is overkill for local var */ |
| 183 |
return SNMPERR_GENERR; |
| 184 |
} |
| 185 |
/* |
| 186 |
* check for required callbacks |
| 187 |
*/ |
| 188 |
if ((cb->can_set && |
| 189 |
((NULL==cb->duplicate_row) || (NULL==cb->delete_row) || |
| 190 |
(NULL==cb->row_copy)) )) { |
| 191 |
snmp_log(LOG_ERR, "table_array registration with incomplete " |
| 192 |
"callback structure.\n"); |
| 193 |
free(tad); /* SNMP_FREE is overkill for local var */ |
| 194 |
return SNMPERR_GENERR; |
| 195 |
} |
| 196 |
|
| 197 |
if (NULL==container) { |
| 198 |
tad->table = netsnmp_container_find("table_array"); |
| 199 |
snmp_log(LOG_ERR, "table_array couldn't allocate container\n" ); |
| 200 |
free(tad); /* SNMP_FREE is overkill for local var */ |
| 201 |
return SNMPERR_GENERR; |
| 202 |
} else |
| 203 |
tad->table = container; |
| 204 |
if (NULL==tad->table->compare) |
| 205 |
tad->table->compare = netsnmp_compare_netsnmp_index; |
| 206 |
if (NULL==tad->table->ncompare) |
| 207 |
tad->table->ncompare = netsnmp_ncompare_netsnmp_index; |
| 208 |
|
| 209 |
tad->cb = cb; |
| 210 |
|
| 211 |
reginfo->handler->myvoid = tad; |
| 212 |
|
| 213 |
return netsnmp_register_table(reginfo, tabreg); |
| 214 |
} |
| 215 |
|
| 216 |
#ifndef NETSNMP_FEATURE_REMOVE_TABLE_ARRAY_REGISTER |
| 217 |
int |
| 218 |
netsnmp_table_array_register(netsnmp_handler_registration *reginfo, |
| 219 |
netsnmp_table_registration_info *tabreg, |
| 220 |
netsnmp_table_array_callbacks *cb, |
| 221 |
netsnmp_container *container, |
| 222 |
int group_rows) |
| 223 |
{ |
| 224 |
netsnmp_mib_handler *handler = |
| 225 |
netsnmp_create_handler(reginfo->handlerName, |
| 226 |
netsnmp_table_array_helper_handler); |
| 227 |
if (!handler || |
| 228 |
(netsnmp_inject_handler(reginfo, handler) != SNMPERR_SUCCESS)) { |
| 229 |
snmp_log(LOG_ERR, "could not create table array handler\n"); |
| 230 |
netsnmp_handler_free(handler); |
| 231 |
netsnmp_handler_registration_free(reginfo); |
| 232 |
return SNMP_ERR_GENERR; |
| 233 |
} |
| 234 |
|
| 235 |
return netsnmp_table_container_register(reginfo, tabreg, cb, |
| 236 |
container, group_rows); |
| 237 |
} |
| 238 |
#endif /* NETSNMP_FEATURE_REMOVE_TABLE_ARRAY_REGISTER */ |
| 239 |
|
| 240 |
/** find the handler for the table_array helper. */ |
| 241 |
#ifndef NETSNMP_FEATURE_REMOVE_TABLE_ARRAY_FIND_TABLE_ARRAY_HANDLER |
| 242 |
netsnmp_mib_handler * |
| 243 |
netsnmp_find_table_array_handler(netsnmp_handler_registration *reginfo) |
| 244 |
{ |
| 245 |
netsnmp_mib_handler *mh; |
| 246 |
if (!reginfo) |
| 247 |
return NULL; |
| 248 |
mh = reginfo->handler; |
| 249 |
while (mh) { |
| 250 |
if (mh->access_method == netsnmp_table_array_helper_handler) |
| 251 |
break; |
| 252 |
mh = mh->next; |
| 253 |
} |
| 254 |
|
| 255 |
return mh; |
| 256 |
} |
| 257 |
#endif /* NETSNMP_FEATURE_REMOVE_TABLE_ARRAY_FIND_TABLE_ARRAY_HANDLER */ |
| 258 |
|
| 259 |
/** find the context data used by the table_array helper */ |
| 260 |
#ifndef NETSNMP_FEATURE_REMOVE_TABLE_ARRAY_EXTRACT_ARRAY_CONTEXT |
| 261 |
netsnmp_container * |
| 262 |
netsnmp_extract_array_context(netsnmp_request_info *request) |
| 263 |
{ |
| 264 |
return (netsnmp_container*)netsnmp_request_get_list_data(request, TABLE_ARRAY_NAME); |
| 265 |
} |
| 266 |
#endif /* NETSNMP_FEATURE_REMOVE_TABLE_ARRAY_EXTRACT_ARRAY_CONTEXT */ |
| 267 |
|
| 268 |
/** this function is called to validate RowStatus transitions. */ |
| 269 |
#ifndef NETSNMP_FEATURE_REMOVE_TABLE_ARRAY_CHECK_ROW_STATUS |
| 270 |
int |
| 271 |
netsnmp_table_array_check_row_status(netsnmp_table_array_callbacks *cb, |
| 272 |
netsnmp_request_group *ag, |
| 273 |
long *rs_new, long *rs_old) |
| 274 |
{ |
| 275 |
netsnmp_index *row_ctx; |
| 276 |
netsnmp_index *undo_ctx; |
| 277 |
if (!ag || !cb) |
| 278 |
return SNMPERR_GENERR; |
| 279 |
row_ctx = ag->existing_row; |
| 280 |
undo_ctx = ag->undo_info; |
| 281 |
|
| 282 |
/* |
| 283 |
* xxx-rks: revisit row delete scenario |
| 284 |
*/ |
| 285 |
if (row_ctx) { |
| 286 |
/* |
| 287 |
* either a new row, or change to old row |
| 288 |
*/ |
| 289 |
/* |
| 290 |
* is it set to active? |
| 291 |
*/ |
| 292 |
if (RS_IS_GOING_ACTIVE(*rs_new)) { |
| 293 |
/* |
| 294 |
* is it ready to be active? |
| 295 |
*/ |
| 296 |
if ((NULL==cb->can_activate) || |
| 297 |
cb->can_activate(undo_ctx, row_ctx, ag)) |
| 298 |
*rs_new = RS_ACTIVE; |
| 299 |
else |
| 300 |
return SNMP_ERR_INCONSISTENTVALUE; |
| 301 |
} else { |
| 302 |
/* |
| 303 |
* not going active |
| 304 |
*/ |
| 305 |
if (undo_ctx) { |
| 306 |
/* |
| 307 |
* change |
| 308 |
*/ |
| 309 |
if (RS_IS_ACTIVE(*rs_old)) { |
| 310 |
/* |
| 311 |
* check pre-reqs for deactivation |
| 312 |
*/ |
| 313 |
if (cb->can_deactivate && |
| 314 |
!cb->can_deactivate(undo_ctx, row_ctx, ag)) { |
| 315 |
return SNMP_ERR_INCONSISTENTVALUE; |
| 316 |
} |
| 317 |
} |
| 318 |
} else { |
| 319 |
/* |
| 320 |
* new row |
| 321 |
*/ |
| 322 |
} |
| 323 |
|
| 324 |
if (*rs_new != RS_DESTROY) { |
| 325 |
if ((NULL==cb->can_activate) || |
| 326 |
cb->can_activate(undo_ctx, row_ctx, ag)) |
| 327 |
*rs_new = RS_NOTINSERVICE; |
| 328 |
else |
| 329 |
*rs_new = RS_NOTREADY; |
| 330 |
} else { |
| 331 |
if (cb->can_delete && !cb->can_delete(undo_ctx, row_ctx, ag)) { |
| 332 |
return SNMP_ERR_INCONSISTENTVALUE; |
| 333 |
} |
| 334 |
ag->row_deleted = 1; |
| 335 |
} |
| 336 |
} |
| 337 |
} else { |
| 338 |
/* |
| 339 |
* check pre-reqs for delete row |
| 340 |
*/ |
| 341 |
if (cb->can_delete && !cb->can_delete(undo_ctx, row_ctx, ag)) { |
| 342 |
return SNMP_ERR_INCONSISTENTVALUE; |
| 343 |
} |
| 344 |
} |
| 345 |
|
| 346 |
return SNMP_ERR_NOERROR; |
| 347 |
} |
| 348 |
#endif /* NETSNMP_FEATURE_REMOVE_TABLE_ARRAY_CHECK_ROW_STATUS */ |
| 349 |
|
| 350 |
/** @} */ |
| 351 |
|
| 352 |
/** @cond */ |
| 353 |
/********************************************************************** |
| 354 |
********************************************************************** |
| 355 |
********************************************************************** |
| 356 |
********************************************************************** |
| 357 |
* * |
| 358 |
* * |
| 359 |
* * |
| 360 |
* * |
| 361 |
* EVERYTHING BELOW THIS IS PRIVATE IMPLEMENTATION DETAILS. * |
| 362 |
* * |
| 363 |
* * |
| 364 |
* * |
| 365 |
* * |
| 366 |
********************************************************************** |
| 367 |
********************************************************************** |
| 368 |
********************************************************************** |
| 369 |
**********************************************************************/ |
| 370 |
|
| 371 |
/********************************************************************** |
| 372 |
********************************************************************** |
| 373 |
* * |
| 374 |
* * |
| 375 |
* Structures, Utility/convenience functions * |
| 376 |
* * |
| 377 |
* * |
| 378 |
********************************************************************** |
| 379 |
**********************************************************************/ |
| 380 |
/* |
| 381 |
* context info for SET requests |
| 382 |
*/ |
| 383 |
#ifndef NETSNMP_NO_WRITE_SUPPORT |
| 384 |
typedef struct set_context_s { |
| 385 |
netsnmp_agent_request_info *agtreq_info; |
| 386 |
table_container_data *tad; |
| 387 |
int status; |
| 388 |
} set_context; |
| 389 |
#endif /* NETSNMP_NO_WRITE_SUPPORT */ |
| 390 |
|
| 391 |
void |
| 392 |
build_new_oid(netsnmp_handler_registration *reginfo, |
| 393 |
netsnmp_table_request_info *tblreq_info, |
| 394 |
netsnmp_index *row, netsnmp_request_info *current) |
| 395 |
{ |
| 396 |
oid coloid[MAX_OID_LEN]; |
| 397 |
|
| 398 |
if (!tblreq_info || !reginfo || !row || !current) |
| 399 |
return; |
| 400 |
|
| 401 |
memcpy(coloid, reginfo->rootoid, reginfo->rootoid_len * sizeof(oid)); |
| 402 |
|
| 403 |
/** table.entry */ |
| 404 |
coloid[reginfo->rootoid_len] = 1; |
| 405 |
|
| 406 |
/** table.entry.column */ |
| 407 |
coloid[reginfo->rootoid_len + 1] = tblreq_info->colnum; |
| 408 |
|
| 409 |
/** table.entry.column.index */ |
| 410 |
memcpy(&coloid[reginfo->rootoid_len + 2], row->oids, |
| 411 |
row->len * sizeof(oid)); |
| 412 |
|
| 413 |
snmp_set_var_objid(current->requestvb, coloid, |
| 414 |
reginfo->rootoid_len + 2 + row->len); |
| 415 |
} |
| 416 |
|
| 417 |
/********************************************************************** |
| 418 |
********************************************************************** |
| 419 |
* * |
| 420 |
* * |
| 421 |
* GET procession functions * |
| 422 |
* * |
| 423 |
* * |
| 424 |
********************************************************************** |
| 425 |
**********************************************************************/ |
| 426 |
int |
| 427 |
process_get_requests(netsnmp_handler_registration *reginfo, |
| 428 |
netsnmp_agent_request_info *agtreq_info, |
| 429 |
netsnmp_request_info *requests, |
| 430 |
table_container_data * tad) |
| 431 |
{ |
| 432 |
int rc = SNMP_ERR_NOERROR; |
| 433 |
netsnmp_request_info *current; |
| 434 |
netsnmp_index *row = NULL; |
| 435 |
netsnmp_table_request_info *tblreq_info; |
| 436 |
netsnmp_variable_list *var; |
| 437 |
|
| 438 |
/* |
| 439 |
* Loop through each of the requests, and |
| 440 |
* try to find the appropriate row from the container. |
| 441 |
*/ |
| 442 |
for (current = requests; current; current = current->next) { |
| 443 |
|
| 444 |
var = current->requestvb; |
| 445 |
DEBUGMSGTL(("table_array:get", |
| 446 |
" process_get_request oid:")); |
| 447 |
DEBUGMSGOID(("table_array:get", var->name, |
| 448 |
var->name_length)); |
| 449 |
DEBUGMSG(("table_array:get", "\n")); |
| 450 |
|
| 451 |
/* |
| 452 |
* skip anything that doesn't need processing. |
| 453 |
*/ |
| 454 |
if (current->processed != 0) { |
| 455 |
DEBUGMSGTL(("table_array:get", "already processed\n")); |
| 456 |
continue; |
| 457 |
} |
| 458 |
|
| 459 |
/* |
| 460 |
* Get pointer to the table information for this request. This |
| 461 |
* information was saved by table_helper_handler. When |
| 462 |
* debugging, we double check a few assumptions. For example, |
| 463 |
* the table_helper_handler should enforce column boundaries. |
| 464 |
*/ |
| 465 |
tblreq_info = netsnmp_extract_table_info(current); |
| 466 |
netsnmp_assert(tblreq_info->colnum <= tad->tblreg_info->max_column); |
| 467 |
|
| 468 |
if ((agtreq_info->mode == MODE_GETNEXT) || |
| 469 |
(agtreq_info->mode == MODE_GETBULK)) { |
| 470 |
/* |
| 471 |
* find the row |
| 472 |
*/ |
| 473 |
row = netsnmp_table_index_find_next_row(tad->table, tblreq_info); |
| 474 |
if (!row) { |
| 475 |
/* |
| 476 |
* no results found. |
| 477 |
* |
| 478 |
* xxx-rks: how do we skip this entry for the next handler, |
| 479 |
* but still allow it a chance to hit another handler? |
| 480 |
*/ |
| 481 |
DEBUGMSGTL(("table_array:get", "no row found\n")); |
| 482 |
netsnmp_set_request_error(agtreq_info, current, |
| 483 |
SNMP_ENDOFMIBVIEW); |
| 484 |
continue; |
| 485 |
} |
| 486 |
|
| 487 |
/* |
| 488 |
* * if data was found, make sure it has the column we want |
| 489 |
*/ |
| 490 |
/* xxx-rks: add suport for sparse tables */ |
| 491 |
|
| 492 |
/* |
| 493 |
* build new oid |
| 494 |
*/ |
| 495 |
build_new_oid(reginfo, tblreq_info, row, current); |
| 496 |
|
| 497 |
} /** GETNEXT/GETBULK */ |
| 498 |
else { |
| 499 |
netsnmp_index index; |
| 500 |
index.oids = tblreq_info->index_oid; |
| 501 |
index.len = tblreq_info->index_oid_len; |
| 502 |
|
| 503 |
row = (netsnmp_index*)CONTAINER_FIND(tad->table, &index); |
| 504 |
if (!row) { |
| 505 |
DEBUGMSGTL(("table_array:get", "no row found\n")); |
| 506 |
netsnmp_set_request_error(agtreq_info, current, |
| 507 |
SNMP_NOSUCHINSTANCE); |
| 508 |
continue; |
| 509 |
} |
| 510 |
} /** GET */ |
| 511 |
|
| 512 |
/* |
| 513 |
* get the data |
| 514 |
*/ |
| 515 |
rc = tad->cb->get_value(current, row, tblreq_info); |
| 516 |
|
| 517 |
} /** for ( ... requests ... ) */ |
| 518 |
|
| 519 |
return rc; |
| 520 |
} |
| 521 |
|
| 522 |
/********************************************************************** |
| 523 |
********************************************************************** |
| 524 |
* * |
| 525 |
* * |
| 526 |
* SET procession functions * |
| 527 |
* * |
| 528 |
* * |
| 529 |
********************************************************************** |
| 530 |
**********************************************************************/ |
| 531 |
|
| 532 |
void |
| 533 |
group_requests(netsnmp_agent_request_info *agtreq_info, |
| 534 |
netsnmp_request_info *requests, |
| 535 |
netsnmp_container *request_group, table_container_data * tad) |
| 536 |
{ |
| 537 |
netsnmp_table_request_info *tblreq_info; |
| 538 |
netsnmp_index *row, *tmp, index; |
| 539 |
netsnmp_request_info *current; |
| 540 |
netsnmp_request_group *g; |
| 541 |
netsnmp_request_group_item *i; |
| 542 |
|
| 543 |
for (current = requests; current; current = current->next) { |
| 544 |
/* |
| 545 |
* skip anything that doesn't need processing. |
| 546 |
*/ |
| 547 |
if (current->processed != 0) { |
| 548 |
DEBUGMSGTL(("table_array:group", |
| 549 |
"already processed\n")); |
| 550 |
continue; |
| 551 |
} |
| 552 |
|
| 553 |
/* |
| 554 |
* 3.2.1 Setup and paranoia |
| 555 |
* * |
| 556 |
* * Get pointer to the table information for this request. This |
| 557 |
* * information was saved by table_helper_handler. When |
| 558 |
* * debugging, we double check a few assumptions. For example, |
| 559 |
* * the table_helper_handler should enforce column boundaries. |
| 560 |
*/ |
| 561 |
row = NULL; |
| 562 |
tblreq_info = netsnmp_extract_table_info(current); |
| 563 |
netsnmp_assert(tblreq_info->colnum <= tad->tblreg_info->max_column); |
| 564 |
|
| 565 |
/* |
| 566 |
* search for index |
| 567 |
*/ |
| 568 |
index.oids = tblreq_info->index_oid; |
| 569 |
index.len = tblreq_info->index_oid_len; |
| 570 |
tmp = (netsnmp_index*)CONTAINER_FIND(request_group, &index); |
| 571 |
if (tmp) { |
| 572 |
DEBUGMSGTL(("table_array:group", |
| 573 |
" existing group:")); |
| 574 |
DEBUGMSGOID(("table_array:group", index.oids, |
| 575 |
index.len)); |
| 576 |
DEBUGMSG(("table_array:group", "\n")); |
| 577 |
g = (netsnmp_request_group *) tmp; |
| 578 |
i = SNMP_MALLOC_TYPEDEF(netsnmp_request_group_item); |
| 579 |
if (i == NULL) |
| 580 |
return; |
| 581 |
i->ri = current; |
| 582 |
i->tri = tblreq_info; |
| 583 |
i->next = g->list; |
| 584 |
g->list = i; |
| 585 |
|
| 586 |
/** xxx-rks: store map of colnum to request */ |
| 587 |
continue; |
| 588 |
} |
| 589 |
|
| 590 |
DEBUGMSGTL(("table_array:group", " new group")); |
| 591 |
DEBUGMSGOID(("table_array:group", index.oids, |
| 592 |
index.len)); |
| 593 |
DEBUGMSG(("table_array:group", "\n")); |
| 594 |
g = SNMP_MALLOC_TYPEDEF(netsnmp_request_group); |
| 595 |
i = SNMP_MALLOC_TYPEDEF(netsnmp_request_group_item); |
| 596 |
if (i == NULL || g == NULL) { |
| 597 |
SNMP_FREE(i); |
| 598 |
SNMP_FREE(g); |
| 599 |
return; |
| 600 |
} |
| 601 |
g->list = i; |
| 602 |
g->table = tad->table; |
| 603 |
i->ri = current; |
| 604 |
i->tri = tblreq_info; |
| 605 |
/** xxx-rks: store map of colnum to request */ |
| 606 |
|
| 607 |
/* |
| 608 |
* search for row. all changes are made to the original row, |
| 609 |
* later, we'll make a copy in undo_info before we start processing. |
| 610 |
*/ |
| 611 |
row = g->existing_row = (netsnmp_index*)CONTAINER_FIND(tad->table, &index); |
| 612 |
if (!g->existing_row) { |
| 613 |
if (!tad->cb->create_row) { |
| 614 |
#ifndef NETSNMP_NO_WRITE_SUPPORT |
| 615 |
if(MODE_IS_SET(agtreq_info->mode)) |
| 616 |
netsnmp_set_request_error(agtreq_info, current, |
| 617 |
SNMP_ERR_NOTWRITABLE); |
| 618 |
else |
| 619 |
#endif /* NETSNMP_NO_WRITE_SUPPORT */ |
| 620 |
netsnmp_set_request_error(agtreq_info, current, |
| 621 |
SNMP_NOSUCHINSTANCE); |
| 622 |
free(g); |
| 623 |
free(i); |
| 624 |
continue; |
| 625 |
} |
| 626 |
/** use undo_info temporarily */ |
| 627 |
row = g->existing_row = tad->cb->create_row(&index); |
| 628 |
if (!row) { |
| 629 |
/* xxx-rks : parameter to create_row to allow |
| 630 |
* for better error reporting. */ |
| 631 |
netsnmp_set_request_error(agtreq_info, current, |
| 632 |
SNMP_ERR_GENERR); |
| 633 |
free(g); |
| 634 |
free(i); |
| 635 |
continue; |
| 636 |
} |
| 637 |
g->row_created = 1; |
| 638 |
} |
| 639 |
|
| 640 |
g->index.oids = row->oids; |
| 641 |
g->index.len = row->len; |
| 642 |
|
| 643 |
CONTAINER_INSERT(request_group, g); |
| 644 |
|
| 645 |
} /** for( current ... ) */ |
| 646 |
} |
| 647 |
|
| 648 |
#ifndef NETSNMP_NO_WRITE_SUPPORT |
| 649 |
static void |
| 650 |
release_netsnmp_request_group(void *g, void *v) |
| 651 |
{ |
| 652 |
netsnmp_request_group_item *tmp; |
| 653 |
netsnmp_request_group *group = g; |
| 654 |
|
| 655 |
if (!g) |
| 656 |
return; |
| 657 |
while (group->list) { |
| 658 |
tmp = group->list; |
| 659 |
group->list = tmp->next; |
| 660 |
free(tmp); |
| 661 |
} |
| 662 |
|
| 663 |
free(group); |
| 664 |
} |
| 665 |
|
| 666 |
static void |
| 667 |
release_netsnmp_request_groups(void *vp) |
| 668 |
{ |
| 669 |
netsnmp_container *c = vp; |
| 670 |
CONTAINER_FOR_EACH(c, release_netsnmp_request_group, NULL); |
| 671 |
CONTAINER_FREE(c); |
| 672 |
} |
| 673 |
|
| 674 |
static void |
| 675 |
process_set_group(void *o, void *c) |
| 676 |
{ |
| 677 |
/* xxx-rks: should we continue processing after an error?? */ |
| 678 |
set_context *context = (set_context *) c; |
| 679 |
netsnmp_request_group *ag = o; |
| 680 |
int rc = SNMP_ERR_NOERROR; |
| 681 |
|
| 682 |
switch (context->agtreq_info->mode) { |
| 683 |
|
| 684 |
case MODE_SET_RESERVE1:/** -> SET_RESERVE2 || SET_FREE */ |
| 685 |
|
| 686 |
/* |
| 687 |
* if not a new row, save undo info |
| 688 |
*/ |
| 689 |
if (ag->row_created == 0) { |
| 690 |
if (context->tad->cb->duplicate_row) |
| 691 |
ag->undo_info = context->tad->cb->duplicate_row(ag->existing_row); |
| 692 |
else |
| 693 |
ag->undo_info = NULL; |
| 694 |
if (NULL == ag->undo_info) { |
| 695 |
rc = SNMP_ERR_RESOURCEUNAVAILABLE; |
| 696 |
break; |
| 697 |
} |
| 698 |
} |
| 699 |
|
| 700 |
if (context->tad->cb->set_reserve1) |
| 701 |
context->tad->cb->set_reserve1(ag); |
| 702 |
break; |
| 703 |
|
| 704 |
case MODE_SET_RESERVE2:/** -> SET_ACTION || SET_FREE */ |
| 705 |
if (context->tad->cb->set_reserve2) |
| 706 |
context->tad->cb->set_reserve2(ag); |
| 707 |
break; |
| 708 |
|
| 709 |
case MODE_SET_ACTION:/** -> SET_COMMIT || SET_UNDO */ |
| 710 |
if (context->tad->cb->set_action) |
| 711 |
context->tad->cb->set_action(ag); |
| 712 |
break; |
| 713 |
|
| 714 |
case MODE_SET_COMMIT:/** FINAL CHANCE ON SUCCESS */ |
| 715 |
if (ag->row_created == 0) { |
| 716 |
/* |
| 717 |
* this is an existing row, has it been deleted? |
| 718 |
*/ |
| 719 |
if (ag->row_deleted == 1) { |
| 720 |
DEBUGMSGT((TABLE_ARRAY_NAME, "action: deleting row\n")); |
| 721 |
if (CONTAINER_REMOVE(ag->table, ag->existing_row) != 0) { |
| 722 |
rc = SNMP_ERR_COMMITFAILED; |
| 723 |
break; |
| 724 |
} |
| 725 |
} |
| 726 |
} else if (ag->row_deleted == 0) { |
| 727 |
/* |
| 728 |
* new row (that hasn't been deleted) should be inserted |
| 729 |
*/ |
| 730 |
DEBUGMSGT((TABLE_ARRAY_NAME, "action: inserting row\n")); |
| 731 |
if (CONTAINER_INSERT(ag->table, ag->existing_row) != 0) { |
| 732 |
rc = SNMP_ERR_COMMITFAILED; |
| 733 |
break; |
| 734 |
} |
| 735 |
} |
| 736 |
|
| 737 |
if (context->tad->cb->set_commit) |
| 738 |
context->tad->cb->set_commit(ag); |
| 739 |
|
| 740 |
/** no more use for undo_info, so free it */ |
| 741 |
if (ag->undo_info) { |
| 742 |
context->tad->cb->delete_row(ag->undo_info); |
| 743 |
ag->undo_info = NULL; |
| 744 |
} |
| 745 |
|
| 746 |
#if 0 |
| 747 |
/* XXX-rks: finish row cooperative notifications |
| 748 |
* if the table has requested it, send cooperative notifications |
| 749 |
* for row operations. |
| 750 |
*/ |
| 751 |
if (context->tad->notifications) { |
| 752 |
if (ag->undo_info) { |
| 753 |
if (!ag->existing_row) |
| 754 |
netsnmp_monitor_notify(EVENT_ROW_DEL); |
| 755 |
else |
| 756 |
netsnmp_monitor_notify(EVENT_ROW_MOD); |
| 757 |
} |
| 758 |
else |
| 759 |
netsnmp_monitor_notify(EVENT_ROW_ADD); |
| 760 |
} |
| 761 |
#endif |
| 762 |
|
| 763 |
if ((ag->row_created == 0) && (ag->row_deleted == 1)) { |
| 764 |
context->tad->cb->delete_row(ag->existing_row); |
| 765 |
ag->existing_row = NULL; |
| 766 |
} |
| 767 |
break; |
| 768 |
|
| 769 |
case MODE_SET_FREE:/** FINAL CHANCE ON FAILURE */ |
| 770 |
if (context->tad->cb->set_free) |
| 771 |
context->tad->cb->set_free(ag); |
| 772 |
|
| 773 |
/** no more use for undo_info, so free it */ |
| 774 |
if (ag->row_created == 1) { |
| 775 |
if (context->tad->cb->delete_row) |
| 776 |
context->tad->cb->delete_row(ag->existing_row); |
| 777 |
ag->existing_row = NULL; |
| 778 |
} |
| 779 |
else { |
| 780 |
if (context->tad->cb->delete_row) |
| 781 |
context->tad->cb->delete_row(ag->undo_info); |
| 782 |
ag->undo_info = NULL; |
| 783 |
} |
| 784 |
break; |
| 785 |
|
| 786 |
case MODE_SET_UNDO:/** FINAL CHANCE ON FAILURE */ |
| 787 |
/* |
| 788 |
* status already set - don't change it now |
| 789 |
*/ |
| 790 |
if (context->tad->cb->set_undo) |
| 791 |
context->tad->cb->set_undo(ag); |
| 792 |
|
| 793 |
/* |
| 794 |
* no more use for undo_info, so free it |
| 795 |
*/ |
| 796 |
if (ag->row_created == 0) { |
| 797 |
/* |
| 798 |
* restore old values |
| 799 |
*/ |
| 800 |
context->tad->cb->row_copy(ag->existing_row, ag->undo_info); |
| 801 |
context->tad->cb->delete_row(ag->undo_info); |
| 802 |
ag->undo_info = NULL; |
| 803 |
} |
| 804 |
else { |
| 805 |
context->tad->cb->delete_row(ag->existing_row); |
| 806 |
ag->existing_row = NULL; |
| 807 |
} |
| 808 |
break; |
| 809 |
|
| 810 |
default: |
| 811 |
snmp_log(LOG_ERR, "unknown mode processing SET for " |
| 812 |
"netsnmp_table_array_helper_handler\n"); |
| 813 |
rc = SNMP_ERR_GENERR; |
| 814 |
break; |
| 815 |
} |
| 816 |
|
| 817 |
if (rc) |
| 818 |
netsnmp_set_request_error(context->agtreq_info, |
| 819 |
ag->list->ri, rc); |
| 820 |
|
| 821 |
} |
| 822 |
|
| 823 |
int |
| 824 |
process_set_requests(netsnmp_agent_request_info *agtreq_info, |
| 825 |
netsnmp_request_info *requests, |
| 826 |
table_container_data * tad, char *handler_name) |
| 827 |
{ |
| 828 |
set_context context; |
| 829 |
netsnmp_container *request_group; |
| 830 |
|
| 831 |
/* |
| 832 |
* create and save structure for set info |
| 833 |
*/ |
| 834 |
request_group = (netsnmp_container*) netsnmp_agent_get_list_data |
| 835 |
(agtreq_info, handler_name); |
| 836 |
if (request_group == NULL) { |
| 837 |
netsnmp_data_list *tmp; |
| 838 |
request_group = netsnmp_container_find("request_group:" |
| 839 |
"table_container"); |
| 840 |
request_group->compare = netsnmp_compare_netsnmp_index; |
| 841 |
request_group->ncompare = netsnmp_ncompare_netsnmp_index; |
| 842 |
|
| 843 |
DEBUGMSGTL(("table_array", "Grouping requests by oid\n")); |
| 844 |
|
| 845 |
tmp = netsnmp_create_data_list(handler_name, |
| 846 |
request_group, |
| 847 |
release_netsnmp_request_groups); |
| 848 |
netsnmp_agent_add_list_data(agtreq_info, tmp); |
| 849 |
/* |
| 850 |
* group requests. |
| 851 |
*/ |
| 852 |
group_requests(agtreq_info, requests, request_group, tad); |
| 853 |
} |
| 854 |
|
| 855 |
/* |
| 856 |
* process each group one at a time |
| 857 |
*/ |
| 858 |
context.agtreq_info = agtreq_info; |
| 859 |
context.tad = tad; |
| 860 |
context.status = SNMP_ERR_NOERROR; |
| 861 |
CONTAINER_FOR_EACH(request_group, process_set_group, &context); |
| 862 |
|
| 863 |
return context.status; |
| 864 |
} |
| 865 |
#endif /* NETSNMP_NO_WRITE_SUPPORT */ |
| 866 |
|
| 867 |
|
| 868 |
/********************************************************************** |
| 869 |
********************************************************************** |
| 870 |
* * |
| 871 |
* * |
| 872 |
* netsnmp_table_array_helper_handler() * |
| 873 |
* * |
| 874 |
* * |
| 875 |
********************************************************************** |
| 876 |
**********************************************************************/ |
| 877 |
int |
| 878 |
netsnmp_table_array_helper_handler(netsnmp_mib_handler *handler, |
| 879 |
netsnmp_handler_registration *reginfo, |
| 880 |
netsnmp_agent_request_info *agtreq_info, |
| 881 |
netsnmp_request_info *requests) |
| 882 |
{ |
| 883 |
|
| 884 |
/* |
| 885 |
* First off, get our pointer from the handler. This |
| 886 |
* lets us get to the table registration information we |
| 887 |
* saved in get_table_array_handler(), as well as the |
| 888 |
* container where the actual table data is stored. |
| 889 |
*/ |
| 890 |
int rc = SNMP_ERR_NOERROR; |
| 891 |
table_container_data *tad = (table_container_data *)handler->myvoid; |
| 892 |
|
| 893 |
if (agtreq_info->mode < 0 || agtreq_info->mode > 5) { |
| 894 |
DEBUGMSGTL(("table_array", "Mode %d, Got request:\n", |
| 895 |
agtreq_info->mode)); |
| 896 |
} else { |
| 897 |
DEBUGMSGTL(("table_array", "Mode %s, Got request:\n", |
| 898 |
mode_name[agtreq_info->mode])); |
| 899 |
} |
| 900 |
|
| 901 |
#ifndef NETSNMP_NO_WRITE_SUPPORT |
| 902 |
if (MODE_IS_SET(agtreq_info->mode)) { |
| 903 |
/* |
| 904 |
* netsnmp_mutex_lock(&tad->lock); |
| 905 |
*/ |
| 906 |
rc = process_set_requests(agtreq_info, requests, |
| 907 |
tad, handler->handler_name); |
| 908 |
/* |
| 909 |
* netsnmp_mutex_unlock(&tad->lock); |
| 910 |
*/ |
| 911 |
} else |
| 912 |
#endif /* NETSNMP_NO_WRITE_SUPPORT */ |
| 913 |
rc = process_get_requests(reginfo, agtreq_info, requests, tad); |
| 914 |
|
| 915 |
if (rc != SNMP_ERR_NOERROR) { |
| 916 |
DEBUGMSGTL(("table_array", "processing returned rc %d\n", rc)); |
| 917 |
} |
| 918 |
|
| 919 |
/* |
| 920 |
* Now we've done our processing. If there is another handler below us, |
| 921 |
* call them. |
| 922 |
*/ |
| 923 |
if (handler->next) { |
| 924 |
rc = netsnmp_call_next_handler(handler, reginfo, agtreq_info, requests); |
| 925 |
if (rc != SNMP_ERR_NOERROR) { |
| 926 |
DEBUGMSGTL(("table_array", "next handler returned rc %d\n", rc)); |
| 927 |
} |
| 928 |
} |
| 929 |
|
| 930 |
return rc; |
| 931 |
} |
| 932 |
#endif /* NETSNMP_FEATURE_REMOVE_TABLE_CONTAINER */ |
| 933 |
/** @endcond */ |