mib.c
来自「eCos操作系统源码」· C语言 代码 · 共 2,472 行 · 第 1/5 页
C
2,472 行
fprint_description(stdout, objid, objidlen);}voidfprint_description(FILE *f, oid *objid, size_t objidlen) /* number of subidentifiers */{ struct tree *tp = get_tree(objid, objidlen, tree_head); struct tree *subtree = tree_head; fprintf(f, "%s OBJECT-TYPE\n", tp->label); print_tree_node(f, tp); fprintf(f, "::= {"); while (objidlen > 1) { for(; subtree; subtree = subtree->next_peer){ if (*objid == subtree->subid){ if (strncmp( subtree->label, ANON, ANON_LEN)) fprintf(f, " %s(%lu)", subtree->label, subtree->subid); else fprintf(f, " %lu", subtree->subid); break; } } if (subtree == 0) break; objid++; objidlen--; subtree = subtree->child_list; if (subtree == 0) break; } fprintf(f, " %lu }\n", *objid);}voidprint_tree_node(FILE *f, struct tree *tp){ const char *cp; char str[MAXTOKEN]; int i, prevmod; if (tp) { module_name(tp->modid, str); fprintf(f, " -- FROM\t%s", str); for (i = 1, prevmod = tp->modid; i < tp->number_modules; i++) { if (prevmod != tp->module_list[i]) { module_name(tp->module_list[i], str); fprintf(f, ", %s", str); } prevmod = tp->module_list[i]; } fprintf(f, "\n"); if (tp->tc_index != -1) { fprintf(f, " -- TEXTUAL CONVENTION %s\n", get_tc_descriptor(tp->tc_index)); } switch (tp->type) { case TYPE_OBJID: cp = "OBJECT IDENTIFIER"; break; case TYPE_OCTETSTR: cp = "OCTET STRING"; break; case TYPE_INTEGER: cp = "INTEGER"; break; case TYPE_NETADDR: cp = "NetworkAddress"; break; case TYPE_IPADDR: cp = "IpAddress"; break; case TYPE_COUNTER: cp = "Counter"; break; case TYPE_GAUGE: cp = "Gauge"; break; case TYPE_TIMETICKS: cp = "TimeTicks"; break; case TYPE_OPAQUE: cp = "Opaque"; break; case TYPE_NULL: cp = "NULL"; break; case TYPE_COUNTER64: cp = "Counter64"; break; case TYPE_BITSTRING: cp = "BIT STRING"; break; case TYPE_NSAPADDRESS: cp = "NsapAddress"; break; case TYPE_UINTEGER: cp = "UInteger32"; break; case 0: cp = NULL; break; default: sprintf(str,"type_%d", tp->type); cp = str; }#if SNMP_TESTING_CODE if (!cp && (tp->ranges || tp->enums)) { /* ranges without type ? */ sprintf(str,"?0 with %s %s ?", tp->ranges ? "Range" : "", tp->enums ? "Enum" : ""); cp = str; }#endif /* SNMP_TESTING_CODE */ if (cp) fprintf(f, " SYNTAX\t%s", cp); if (tp->ranges) { struct range_list *rp = tp->ranges; int first = 1; fprintf(f, " ("); while (rp) { if (first) first = 0; else fprintf(f, " | "); if (rp->low == rp->high) fprintf(f, "%d", rp->low); else fprintf(f, "%d..%d", rp->low, rp->high); rp = rp->next; } fprintf(f, ") "); } if (tp->enums) { struct enum_list *ep = tp->enums; int first = 1; fprintf(f," { "); while (ep) { if (first) first = 0; else fprintf(f, ", "); fprintf(f, "%s(%d)", ep->label, ep->value); ep = ep->next; } fprintf(f," } "); } if (cp) fprintf(f, "\n"); if (tp->hint) fprintf(f, " DISPLAY-HINT\t\"%s\"\n", tp->hint); if (tp->units) fprintf(f, " UNITS\t\"%s\"\n", tp->units); switch (tp->access) { case MIB_ACCESS_READONLY: cp = "read-only"; break; case MIB_ACCESS_READWRITE: cp = "read-write"; break; case MIB_ACCESS_WRITEONLY: cp = "write-only"; break; case MIB_ACCESS_NOACCESS: cp = "not-accessible"; break; case MIB_ACCESS_NOTIFY: cp = "accessible-for-notify"; break; case MIB_ACCESS_CREATE: cp = "read-create"; break; case 0: cp = NULL; break; default: sprintf(str,"access_%d", tp->access); cp = str; } if (cp) fprintf(f, " MAX-ACCESS\t%s\n", cp); switch (tp->status) { case MIB_STATUS_MANDATORY: cp = "mandatory"; break; case MIB_STATUS_OPTIONAL: cp = "optional"; break; case MIB_STATUS_OBSOLETE: cp = "obsolete"; break; case MIB_STATUS_DEPRECATED: cp = "deprecated"; break; case MIB_STATUS_CURRENT: cp = "current"; break; case 0: cp = NULL; break; default: sprintf(str,"status_%d", tp->status); cp = str; }#if SNMP_TESTING_CODE if (!cp && (tp->indexes)) { /* index without status ? */ sprintf(str,"?0 with %s ?", tp->indexes ? "Index" : ""); cp = str; }#endif /* SNMP_TESTING_CODE */ if (cp) fprintf(f, " STATUS\t%s\n", cp); if (tp->indexes) { struct index_list *ip = tp->indexes; int first=1; fprintf(f, " INDEXES\t"); fprintf(f," { "); while (ip) { if (first) first = 0; else fprintf(f, ", "); if (ip->isimplied) fprintf(f, "IMPLIED "); fprintf(f, "%s", ip->ilabel); ip = ip->next; } fprintf(f," }\n"); } if (tp->description) fprintf(f, " DESCRIPTION\t\"%s\"\n", tp->description); } else fprintf(f, "No description\n");}intget_module_node(const char *fname, const char *module, oid *objid, size_t *objidlen){ int modid, rc = 0; struct tree *tp; char *name, *cp; if ( !strcmp(module, "ANY") ) modid = -1; else { read_module(module); modid = which_module( module ); if (modid == -1) return 0; } /* Isolate the first component of the name ... */ name = strdup(fname); cp = strchr( name, '.' ); if ( cp != NULL ) { *cp = '\0'; cp++; } /* ... and locate it in the tree. */ tp = find_tree_node(name, modid); if (tp){ size_t maxlen = *objidlen; /* Set the first element of the object ID */ if (node_to_oid(tp, objid, objidlen)) { rc = 1; /* If the name requested was more than one element, tag on the rest of the components */ if (cp != NULL) rc = _add_strings_to_oid(tp, cp, objid, objidlen, maxlen); } } free(name); return (rc);}/* * Populate object identifier from a node in the MIB hierarchy. * Build up the object ID, working backwards, * starting from the end of the objid buffer. * When the top of the MIB tree is reached, adjust the buffer. * * The buffer length is set to the number of subidentifiers * for the object identifier associated with the MIB node. * Returns the number of subidentifiers copied. * * If 0 is returned, the objid buffer is too small, * and the buffer contents are indeterminate. * The buffer length can be used to create a larger buffer. */intnode_to_oid(struct tree *tp, oid *objid, size_t *objidlen){ int numids, lenids; oid *op; if (!tp || !objid || !objidlen) return 0; lenids = (int)*objidlen; op = objid + lenids; /* points after the last element */ for(numids = 0; tp; tp = tp->parent, numids++) { if (numids >= lenids) continue; --op; *op = tp->subid; } *objidlen = (size_t)numids; if (numids > lenids) { return 0; } if (numids < lenids) memmove(objid, op, numids * sizeof(oid)); return (numids);}static int_add_strings_to_oid(struct tree *tp, char *cp, oid *objid, size_t *objidlen, size_t maxlen){ int subid; struct tree *tp2 = NULL; char *cp2 = NULL; char doingquote = 0; while ( cp != NULL ) { cp2 = strchr( cp, '.' ); /* Isolate the next entry */ if ( cp2 != NULL ) { *cp2 = '\0'; cp2++; } if ( *cp == '"' || *cp == '\'') { /* Is it the beggining of a quoted string */ doingquote = *cp++; /* insert length if requested */ if (doingquote == '"') { if (*objidlen >= maxlen) return 0; objid[ *objidlen ] = (strchr(cp,doingquote) - cp); (*objidlen)++; } while(*cp != doingquote) { if (*objidlen >= maxlen) return 0; objid[ *objidlen ] = *cp++; (*objidlen)++; } tp = NULL; /* must be pure numeric from here, right? */ cp = cp2; continue; } /* Is it numeric ? */ if ( isdigit( *cp ) ) subid=(strtol(cp,0,0)); else subid = -1; /* Search for the appropriate child */ if ( tp != NULL ) tp2 = tp->child_list; while ( tp2 != NULL ) { if (( (int)tp2->subid == subid ) || ( !strcasecmp( tp2->label, cp ))) { if (*objidlen >= maxlen) return 0; objid[ *objidlen ] = tp2->subid; (*objidlen)++; tp = tp2; break; } tp2 = tp2->next_peer; } if ( tp2 == NULL ) { if ( subid == -1 ) { return 0; } /* pure numeric from now on */ if (*objidlen >= maxlen) return 0; objid[ *objidlen ] = subid; (*objidlen)++; tp = NULL; } cp = cp2; } return 1;}/* * see comments on find_best_tree_node for usage after first time. */intget_wild_node(const char *name, oid *objid, size_t *objidlen){ struct tree *tp = find_best_tree_node(name, tree_head, NULL); if (!tp) return 0; return get_node(tp->label, objid, objidlen);}intget_node(const char *name, oid *objid, size_t *objidlen){ char *cp; int res; if (( cp=strchr(name, ':')) == NULL ) res = get_module_node( name, "ANY", objid, objidlen ); else { char *module; /* * requested name is of the form * "module:subidentifier" */ module = (char *)malloc((size_t)(cp-name+1)); memcpy(module,name,(size_t)(cp-name)); module[cp-name] = 0; cp++; /* cp now point to the subidentifier */ if (*cp == ':') cp++; /* 'cp' and 'name' *do* go that way round! */ res = get_module_node( cp, module, objid, objidlen ); free(module); } if (res == 0) { SET_SNMP_ERROR(SNMPERR_UNKNOWN_OBJID); } return res;}#ifdef testingmain(int argc, char* argv[]){ oid objid[MAX_OID_LEN]; int objidlen = MAX_OID_LEN; int count; struct variable_list variable; init_mib(); if (argc < 2) print_subtree(stdout, tree_head, 0); variable.type = ASN_INTEGER; variable.val.integer = 3; variable.val_len = 4; for (argc--; argc; argc--, argv++) { objidlen = MAX_OID_LEN; printf("read_objid(%s) = %d\n", argv[1], read_objid(argv[1], objid, &objidlen)); for(count = 0; count < objidlen; count++) printf("%d.", objid[count]); printf("\n"); print_variable(objid, objidlen, &variable); }}#endif /* testing *//* initialize: no peers included in the report. */void clear_tree_flags(register struct tree *tp){ for (; tp; tp = tp->next_peer) { tp->reported = 0; if (tp->child_list) clear_tree_flags(tp->child_list); /*RECURSE*/ }}/* * Update: 1998-07-17 <jhy@gsu.edu> * Added print_oid_report* functions. */static int print_subtree_oid_report_labeledoid = 0;static int print_subtree_oid_report_oid = 0;static int print_subtree_oid_report_symbolic = 0;static int print_subtree_oid_report_suffix = 0;/* These methods recurse. */static void print_parent_labeledoid(FILE *, struct tree *);static void print_parent_oid(FILE *, struct tree *);static void print_parent_label(FILE *, struct tree *);static void print_subtree_oid_report(FILE *, struct tree *, int);voidprint_oid_report (FILE *fp){ struct tree *tp; clear_tree_flags(tree_head); for (tp = tree_head ; tp ; tp=tp->next_peer) print_subtree_oid_report (fp, tp, 0);}voidprint_oid_report_enable_labeledoid (void){ print_subtree_oid_report_labeledoid = 1;}voidprint_oid_report_enable_oid (void){ print_subtree_oid_report_oid = 1;}voidprint_oid_report_enable_suffix (void){ print_subtree_oid_report_suffix = 1;}voidprint_oid_report_enable_symbolic (void){ print_subtree_oid_report_symbolic = 1;}/* * helper methods for print_subtree_oid_report() * each one traverses back up the node tree * until there is no parent. Then, the label combination * is output, such that the parent is displayed first. * * Warning: these methods are all recursive. */static voidprint_parent_labeledoid(FILE *f, struct tree *tp){ if(tp) { if(tp->parent) { print_parent_labeledoid(f, tp->parent); /*RECURSE*/ } fprintf(f, ".%s(%lu)", tp->label, tp->subid); }}static voidprint_parent_oid(FILE *f, struct tree *tp){ if(tp) { if(tp->parent) { print_parent_oid(f, tp->parent); /*RECURSE*/ }
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?