1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815
|
/*
* tnmSnmpInst.c --
*
* Utilities to organize the tree of SNMP nodes maintained by snmp
* session to keep track of session bindings and agent instances.
*
* Copyright (c) 1994-1996 Technical University of Braunschweig.
*
* See the file "license.terms" for information on usage and redistribution
* of this file, and for a DISCLAIMER OF ALL WARRANTIES.
*/
#include "tnmSnmp.h"
#include "tnmMib.h"
/*
* The root of the tree containing all MIB instances.
*/
static TnmSnmpNode *instTree = NULL;
/*
* Forward declarations for procedures defined later in this file:
*/
static void
DumpTree _ANSI_ARGS_((TnmSnmpNode *instPtr));
static void
FreeNode _ANSI_ARGS_((TnmSnmpNode *inst));
static TnmSnmpNode*
AddNode _ANSI_ARGS_((char *id, int offset, int syntax,
int access, char *tclVarName));
static void
RemoveNode _ANSI_ARGS_((TnmSnmpNode *root, char *varname));
static TnmSnmpNode*
FindNode _ANSI_ARGS_((TnmSnmpNode *root, Tnm_Oid *oid, int len));
static TnmSnmpNode*
FindNextNode _ANSI_ARGS_((TnmSnmpNode *root, Tnm_Oid *oid, int len));
static char*
DeleteNodeProc _ANSI_ARGS_((ClientData clientData, Tcl_Interp *interp,
char *name1, char *name2, int flags));
/*
*----------------------------------------------------------------------
*
* DumpTree --
*
* This procedure dump the whole instance tree structure. It is
* only used to debug the instance tree processing code.
*
* Results:
* None.
*
* Side effects:
* None.
*
*----------------------------------------------------------------------
*/
static void
DumpTree(instPtr)
TnmSnmpNode *instPtr;
{
if (instPtr) {
fprintf(stderr, "** %s (%s)\n",
instPtr->label ? instPtr->label : "(none)",
TnmGetTableValue(tnmSnmpMibAccessTable, instPtr->access));
if (instPtr->childPtr) {
DumpTree(instPtr->childPtr);
}
if (instPtr->nextPtr) {
DumpTree(instPtr->nextPtr);
}
}
}
/*
*----------------------------------------------------------------------
*
* FreeNode --
*
* This procedure frees an instance node and all associated
* resources.
*
* Results:
* None.
*
* Side effects:
* None.
*
*----------------------------------------------------------------------
*/
static void
FreeNode(instPtr)
TnmSnmpNode *instPtr;
{
if (instPtr->label) {
ckfree(instPtr->label);
}
if (instPtr->tclVarName) {
ckfree(instPtr->tclVarName);
}
while (instPtr->bindings) {
SNMP_Binding *bindPtr = instPtr->bindings;
instPtr->bindings = instPtr->bindings->nextPtr;
if (bindPtr->command) {
ckfree(bindPtr->command);
}
ckfree((char *) bindPtr);
}
ckfree((char *) instPtr);
}
/*
*----------------------------------------------------------------------
*
* AddNode --
*
* This procedure adds a new instance node to the tree
* of instances.
*
* Results:
* A pointer to the new node or NULL if there was an error.
*
* Side effects:
* None.
*
*----------------------------------------------------------------------
*/
static TnmSnmpNode*
AddNode(soid, offset, syntax, access, tclVarName)
char *soid;
int offset;
int syntax;
int access;
char *tclVarName;
{
Tnm_Oid *oid;
int i, oidlen;
TnmSnmpNode *p, *q = NULL;
if (instTree == NULL) {
instTree = (TnmSnmpNode *) ckalloc(sizeof(TnmSnmpNode));
memset((char *) instTree, 0, sizeof(TnmSnmpNode));
instTree->label = "1";
instTree->subid = 1;
}
oid = Tnm_StrToOid(soid, &oidlen);
if (oid[0] != 1 || oidlen < 1) {
return NULL;
}
if (oidlen == 1 && oid[0] == 1) {
return instTree;
}
for (p = instTree, i = 1; i < oidlen; p = q, i++) {
for (q = p->childPtr; q; q = q->nextPtr) {
if (q->subid == oid[i]) break;
}
if (! q) {
/*
* Create new intermediate nodes.
*/
TnmSnmpNode *n;
char *s = Tnm_OidToStr(oid, i+1);
n = (TnmSnmpNode *) ckalloc(sizeof(TnmSnmpNode));
memset((char *) n, 0, sizeof(TnmSnmpNode));
n->label = ckstrdup(s);
n->subid = oid[i];
n->offset = offset;
if (! p->childPtr) { /* first node */
p->childPtr = n;
} else if (p->childPtr->subid > oid[i]) { /* insert head */
n->nextPtr = p->childPtr;
p->childPtr = n;
} else { /* somewhere else */
for (q = p->childPtr; q->nextPtr && q->nextPtr->subid < oid[i];
q = q->nextPtr) ;
if (q->nextPtr && q->nextPtr->subid == oid[i]) {
continue;
}
n->nextPtr = q->nextPtr;
q->nextPtr = n;
}
q = n;
}
}
if (q) {
if (q->label) ckfree(q->label);
if (q->tclVarName && q->tclVarName != tclVarName) {
ckfree(q->tclVarName);
}
q->label = soid;
q->offset = offset;
q->syntax = syntax;
q->access = access;
q->tclVarName = tclVarName;
}
return q;
}
/*
*----------------------------------------------------------------------
*
* FindNextNode --
*
* This procedure locates the lexikographic next instance
* node in the instance tree.
*
* Results:
* A pointer to the node or NULL if there is no next node.
*
* Side effects:
* None.
*
*----------------------------------------------------------------------
*/
static TnmSnmpNode*
FindNextNode(root, oid, len)
TnmSnmpNode *root;
Tnm_Oid *oid;
int len;
{
TnmSnmpNode *p, *inst;
static int force = 0;
/*
* Reset the force flag if we start a new search from the root of
* the instance tree. The flag will be set whenever we decide that
* the next instance we find will be a good candidate.
*/
if (root == instTree) {
force = 0;
}
/*
* Skip over all subidentifier that are lower than the
* subidentifier of interest.
*/
p = root;
if (len) {
while (p && p->subid < oid[0]) p = p->nextPtr;
}
/*
* Loop over all neighbours at this tree level. Descend if possible.
*/
while (p) {
if (p->childPtr) {
if (len > 0 && p->subid == oid[0]) {
/* descend - positive match */
inst = FindNextNode(p->childPtr, oid + 1, len - 1);
} else if (p->syntax) {
/* no match - use this node */
return p;
} else {
/* descend - force next node */
force = 1;
inst = FindNextNode(p->childPtr, NULL, 0);
}
if (inst) return inst;
} else {
if (len == 0 && p->syntax) {
/* found - node has larger oid */
return p;
} else if (((len && p->subid != oid[0]) || force) && p->syntax) {
/* no match - forced to use this node */
return p;
} else {
/* force next node */
force = 1;
}
}
p = p->nextPtr;
}
return NULL;
}
/*
*----------------------------------------------------------------------
*
* FindNode --
*
* This procedure locates an instance node in the instance
* node tree.
*
* Results:
* A pointer to the node or NULL if there is no next node.
*
* Side effects:
* None.
*
*----------------------------------------------------------------------
*/
static TnmSnmpNode*
FindNode(root, oid, len)
TnmSnmpNode *root;
Tnm_Oid *oid;
int len;
{
TnmSnmpNode *p, *q = NULL;
int i;
if (oid[0] != 1) return NULL;
for (p = root, i = 1; p && i < len; p = q, i++) {
for (q = p->childPtr; q; q = q->nextPtr) {
if (q->subid == oid[i]) break;
}
if (!q) {
return NULL;
}
}
return q;
}
/*
*----------------------------------------------------------------------
*
* RemoveNode --
*
* This procedure removes all nodes from the tree that are
* associated with a given Tcl variable.
*
* Results:
* None.
*
* Side effects:
* None.
*
*----------------------------------------------------------------------
*/
static void
RemoveNode(root, varName)
TnmSnmpNode *root;
char *varName;
{
TnmSnmpNode *p, *q;
if (!root) return;
for (p = root; p; p = p->nextPtr) {
if (p->childPtr) {
q = p->childPtr;
RemoveNode(q, varName);
if (q->tclVarName && (strcmp(q->tclVarName, varName) == 0)) {
p->childPtr = q->nextPtr;
FreeNode(q);
}
}
if (p->nextPtr) {
q = p->nextPtr;
if (q->tclVarName && (strcmp(q->tclVarName, varName) == 0)) {
p->nextPtr = q->nextPtr;
FreeNode(q);
}
}
}
}
/*
*----------------------------------------------------------------------
*
* DeleteNodeProc --
*
* This procedure is a variable trace callback which is called
* by the Tcl interpreter whenever a MIB variable is removed.
* We have to run through the whole tree to discard these
* variables.
*
* Results:
* Always NULL.
*
* Side effects:
* None.
*
*----------------------------------------------------------------------
*/
static char*
DeleteNodeProc(clientData, interp, name1, name2, flags)
ClientData clientData;
Tcl_Interp *interp;
char *name1;
char *name2;
int flags;
{
int len = strlen(name1);
char *varName;
if (name2) {
len += strlen(name2);
}
varName = ckalloc(len + 3);
strcpy(varName, name1);
if (name2) {
strcat(varName,"(");
strcat(varName, name2);
strcat(varName,")");
}
RemoveNode(instTree, varName);
ckfree(varName);
return NULL;
}
/*
*----------------------------------------------------------------------
*
* Tnm_SnmpCreateNode --
*
* This procedure creates a new node in the instance tree
* and a Tcl array variable that will be used to access and
* modify the instance from within Tcl.
*
* Results:
* A standard Tcl result.
*
* Side effects:
* None.
*
*----------------------------------------------------------------------
*/
int
Tnm_SnmpCreateNode(interp, label, tclVarName, defval)
Tcl_Interp *interp;
char *label;
char *tclVarName;
char *defval;
{
char *soid = NULL;
Tnm_MibNode *nodePtr = Tnm_MibFindNode(label, NULL, 0);
int access, offset = 0, syntax = 0;
char *varName = NULL;
if (!nodePtr || nodePtr->childPtr) {
Tcl_AppendResult(interp, "unknown object type \"", label, "\"",
(char *) NULL);
return TCL_ERROR;
}
soid = ckstrdup(Tnm_MibGetOid(label, 0));
if (! Tnm_IsOid(soid)) {
Tcl_AppendResult(interp, "illegal instance identifier \"",
soid, "\"", (char *) NULL);
return TCL_ERROR;
}
/*
* Calculate the instance identifier. Return an error if we
* have no real instance. Otherwise save a pointer to the
* instance identifier so that we can access it later.
*/
{
int oidLen;
Tnm_Oid *oid;
Tnm_MibNode *basePtr = NULL;
char *freeme = NULL;
for (oid = Tnm_StrToOid(soid, &oidLen); oidLen; oidLen--) {
freeme = Tnm_OidToStr(oid, oidLen);
basePtr = Tnm_MibFindNode(freeme, NULL, 1);
if (basePtr) break;
}
if (! basePtr || strlen(soid) <= strlen(freeme)) {
Tcl_AppendResult(interp, "instance identifier missing in \"",
label, "\"", (char *) NULL);
return TCL_ERROR;
}
if (freeme) {
offset = strlen(freeme)+1;
}
}
syntax = Tnm_MibGetBaseSyntax(label, 0);
/*
* Check whether the instance is accessible.
*/
access = nodePtr->access;
if (access == TNM_MIB_NOACCESS) {
Tcl_AppendResult(interp, "object \"", label, "\" is not accessible",
(char *) NULL);
goto errorExit;
}
/*
* Check if the instance identifier is "0" for scalars. We have
* a scalar if the parent node is not of ASN.1 syntax SEQUENCE.
*
* XXX We should also check if the instance identifier is valid
* for table entries.
*/
if (! nodePtr->parentPtr) {
Tcl_AppendResult(interp, "instance \"", label, "\" not allowed",
(char *) NULL);
goto errorExit;
}
if (nodePtr->parentPtr && nodePtr->parentPtr->syntax != ASN1_SEQUENCE) {
if (strcmp(soid + offset, "0") != 0) {
Tcl_AppendResult(interp, "illegal instance identifier \"",
soid + offset, "\" for instance \"", label, "\"",
(char *) NULL);
goto errorExit;
}
}
/*
* Now create the Tcl variable and the instance tree node.
* Do not use tclVarName directly because it might be a string
* which is not writable and Tcl likes to modify this string
* internally.
*/
varName = ckstrdup(tclVarName);
if (defval) {
if (Tcl_SetVar(interp, varName, defval,
TCL_GLOBAL_ONLY | TCL_LEAVE_ERR_MSG) == NULL) {
goto errorExit;
}
}
AddNode(soid, offset, syntax, access, varName);
Tcl_TraceVar(interp, varName, TCL_TRACE_UNSETS | TCL_GLOBAL_ONLY,
DeleteNodeProc, (ClientData) NULL);
Tcl_ResetResult(interp);
return TCL_OK;
errorExit:
if (soid) ckfree(soid);
return TCL_ERROR;
}
/*
*----------------------------------------------------------------------
*
* Tnm_SnmpFindNode --
*
* This procedure locates the instance node for the given oid
* in the instance node tree.
*
* Results:
* A pointer to the node or NULL if there is no next node.
*
* Side effects:
* None.
*
*----------------------------------------------------------------------
*/
TnmSnmpNode*
Tnm_SnmpFindNode(session, oid, oidlen)
SNMP_Session *session;
Tnm_Oid *oid;
int oidlen;
{
return FindNode(instTree, oid, oidlen);
}
/*
*----------------------------------------------------------------------
*
* Tnm_SnmpFindNextNode --
*
* This procedure locates the next instance for the given oid
* in the instance tree.
*
* Results:
* A pointer to the node or NULL if there is no next node.
*
* Side effects:
* None.
*
*----------------------------------------------------------------------
*/
TnmSnmpNode*
Tnm_SnmpFindNextNode(session, oid, oidlen)
SNMP_Session *session;
Tnm_Oid *oid;
int oidlen;
{
return FindNextNode(instTree, oid, oidlen);
}
/*
*----------------------------------------------------------------------
*
* Tnm_SnmpSetNodeBinding --
*
* This procedure creates and modifies SNMP event bindings
* for a specific MIB node.
*
* Results:
* A standard Tcl result.
*
* Side effects:
* None.
*
*----------------------------------------------------------------------
*/
int
Tnm_SnmpSetNodeBinding(session, oid, oidlen, event, command)
SNMP_Session *session;
Tnm_Oid *oid;
int oidlen;
int event;
char *command;
{
TnmSnmpNode *node = NULL;
SNMP_Binding *bindPtr = NULL;
/*
* Create an anonymous node if there is no instance known yet.
*/
node = FindNode(instTree, oid, oidlen);
if (!node) {
node = AddNode(ckstrdup(Tnm_OidToStr(oid, oidlen)), 0, 0, 0, NULL);
if (! node) {
return TCL_ERROR;
}
}
/*
* Check if we already have a binding for this event type.
*/
for (bindPtr = node->bindings; bindPtr; bindPtr = bindPtr->nextPtr) {
if (bindPtr->event == event) break;
}
/*
* Create a new binding if necessary. Overwrite already
* existing bindings.
*/
if (command) {
if (!bindPtr) {
bindPtr = (SNMP_Binding *) ckalloc(sizeof(SNMP_Binding));
memset((char *) bindPtr, 0, sizeof(SNMP_Binding));
bindPtr->event = event;
bindPtr->nextPtr = node->bindings;
node->bindings = bindPtr;
}
if (bindPtr->command) {
ckfree(bindPtr->command);
bindPtr->command = NULL;
}
if (*command != '\0') {
bindPtr->command = ckstrdup(command);
}
}
return TCL_OK;
}
/*
*----------------------------------------------------------------------
*
* Tnm_SnmpGetNodeBinding --
*
* This procedure retrieves the current binding for the given
* MIB node and event type.
*
* Results:
* A pointer to the command bound to this event or NULL if
* there is either no node of if there is no binding for
* the node.
*
* Side effects:
* None.
*
*----------------------------------------------------------------------
*/
char*
Tnm_SnmpGetNodeBinding(session, oid, oidlen, event)
SNMP_Session *session;
Tnm_Oid *oid;
int oidlen;
int event;
{
TnmSnmpNode *node = NULL;
SNMP_Binding *bindPtr = NULL;
node = FindNode(instTree, oid, oidlen);
if (! node) {
return NULL;
}
for (bindPtr = node->bindings; bindPtr; bindPtr = bindPtr->nextPtr) {
if (bindPtr->event == event) break;
}
if (! bindPtr) {
return NULL;
}
return bindPtr->command;
}
/*
*----------------------------------------------------------------------
*
* Tnm_SnmpEvalNodeBinding --
*
* This procedure evaluates a binding for a given node. We
* start at the given node and follow the path to the top of
* the instance tree. We evaluate all bindings during our walk
* up the tree. A break return code can be used to stop this
* process.
*
* Results:
* A standard Tcl result.
*
* Side effects:
* None.
*
*----------------------------------------------------------------------
*/
int
Tnm_SnmpEvalNodeBinding(session, pdu, inst, event, value, oldValue)
SNMP_Session *session;
SNMP_PDU *pdu;
TnmSnmpNode *inst;
int event;
char *value;
char *oldValue;
{
int oidlen;
Tnm_Oid *oid;
int code = TCL_OK;
char *instOid;
oid = Tnm_StrToOid(inst->label, &oidlen);
instOid = inst->label+inst->offset;
for (; oidlen > 0; oidlen--) {
SNMP_Binding *bindPtr;
inst = FindNode(instTree, oid, oidlen);
if (!inst) continue;
for (bindPtr = inst->bindings; bindPtr; bindPtr = bindPtr->nextPtr) {
if (bindPtr->event == event) break;
}
if (bindPtr && bindPtr->command) {
/*
* Evaluate the binding and check if the instance is still
* there after we complete the binding. It may have been
* deleted in the callback. Also make sure that we have an
* error status of TNM_SNMP_NOERROR and error index 0 during the
* callback.
*/
int error_status = pdu->error_status;
int error_index = pdu->error_index;
pdu->error_status = TNM_SNMP_NOERROR;
pdu->error_index = 0;
code = Tnm_SnmpEvalCallback(session->agentInterp, session,
pdu, bindPtr->command,
inst->label, instOid,
value, oldValue);
pdu->error_status = error_status;
pdu->error_index = error_index;
if (code == TCL_OK && !FindNode(instTree, oid, oidlen)) {
code = TCL_ERROR;
}
if (code == TCL_BREAK || code == TCL_ERROR) break;
}
}
return code;
}
|