static void incrTypeAndNodeUsageCounter(SmiModule *smiModule, SmiNode *smiNode, int flags) { SmiType *smiType; char *extModule; /* * First check whether the node is external. If yes, increment the * external node counter and we are done. */ extModule = smiGetNodeModule(smiNode)->name; if (extModule && strcmp(extModule, smiModule->name) != 0) { if (flags & INCR_NODE) { extNodeList = incrUsageCounter(extNodeList, extModule, smiNode->name, 1); extModuleList = incrUsageCounter(extModuleList, extModule, NULL, 1); } return; } /* * Next, check whether the type of the node is external. If yes, * increment the external type counter and we are done. Do not * count base types (that is types that have no parent type). */ smiType = smiGetNodeType(smiNode); if (! smiType) { return; } if (smiType->name && smiGetParentType(smiType)) { char *extModule = smiGetTypeModule(smiType)->name; if (extModule /* && *extModule */ && strcmp(extModule, smiModule->name) != 0) { if (flags & INCR_TYPE) { extTypeList = incrUsageCounter(extTypeList, extModule, smiType->name, 1); extModuleList = incrUsageCounter(extModuleList, extModule, NULL, 1); } } } /* * Finally, count the type name (whether external or not does not * matter here nor does it matter whether it is a base type or * not). */ if (! smiType->name && smiGetParentType(smiType)) { smiType = smiGetParentType(smiType); } typeList = incrUsageCounter(typeList, smiGetTypeModule(smiType)->name, smiType->name, 1); }
static void fprintTypedef(FILE *f, int indent, SmiType *smiType) { SmiModule *parentModule; SmiType *parentType; fprintSegment(f, indent, "<typedef", 0); if (smiType->name) { fprint(f, " name=\"%s\"", smiType->name); } fprint(f, " basetype=\"%s\"", getStringBasetype(smiType->basetype)); if (smiType->name && smiType->status != SMI_STATUS_UNKNOWN) { fprint(f, " status=\"%s\"", getStringStatus(smiType->status)); } fprint(f, ">\n"); parentType = smiGetParentType(smiType); parentModule = smiGetTypeModule(parentType); if (parentType && parentType->name && parentModule && strlen(parentModule->name)) { fprintSegment(f, indent + INDENT, "<parent ", 0); fprintf(f, "module=\"%s\" name=\"%s\"/>\n", parentModule->name, parentType->name); } fprintRanges(f, indent + INDENT, smiType); fprintNamedNumbers(f, indent + INDENT, smiType); fprintValue(f, indent + INDENT, &smiType->value, smiType); fprintFormat(f, indent + INDENT, smiType->format); fprintUnits(f, indent + INDENT, smiType->units); fprintDescription(f, indent + INDENT, smiType->description); fprintReference(f, indent + INDENT, smiType->reference); fprintSegment(f, indent, "</typedef>\n", 0); }
static char *getTypeString(SmiBasetype basetype, SmiType *smiType) { int i; char *typeModule, *typeName; typeName = smiType ? smiType->name : NULL; typeModule = smiType ? smiGetTypeModule(smiType)->name : NULL; if ((!typeModule) && (typeName) && (basetype != SMI_BASETYPE_ENUM) && (basetype != SMI_BASETYPE_BITS)) { for(i=0; convertType[i]; i += 2) { if (!strcmp(typeName, convertType[i])) { return convertType[i+1]; } } } if ((!typeModule) || (!strlen(typeModule)) || (!typeName)) { if (basetype == SMI_BASETYPE_ENUM) { return "Enumeration"; } if (basetype == SMI_BASETYPE_BITS) { return "Bits"; } } if (!typeName) { return getStringBasetype(basetype); } /* TODO: fully qualified if unambigous */ return typeName; }
static void incrRowStatusCounter(SmiNode *rowNode) { SmiNode *smiNode; SmiType *smiType; SmiModule *smiModule; for (smiNode = smiGetFirstChildNode(rowNode); smiNode; smiNode = smiGetNextChildNode(smiNode)) { smiType = smiGetNodeType(smiNode); if (smiType && smiType->name) { smiModule = smiGetTypeModule(smiType); if (smiModule && smiModule->name && strcmp(smiType->name, "RowStatus") == 0 && strcmp(smiModule->name, "SNMPv2-TC") == 0) { break; } } } if (smiNode) { #if 0 fprintf(stderr, "** %s\t%s\t%s\n", rowNode->name, smiNode->name, smiType->name); /* xxx count rows indexed by ifIndex, InterfaceIndex, InterfaceIndexOrZero, ... */ #endif } }
static int ber_len_val_octs(SmiType *smiType, len_type flags) { int len = 0; SmiModule *smiModule; smiModule = smiGetTypeModule(smiType); if (smiModule && smiModule->name && smiType->name) { int i; for (i = 0; specialTypes[i].module; i++) { if (strcmp(specialTypes[i].module, smiModule->name) == 0 && (strcmp(specialTypes[i].name, smiType->name) == 0)) { break; } } if (specialTypes[i].module) { switch (flags) { case len_max: return specialTypes[i].max; break; case len_mean: return specialTypes[i].mean; break; case len_min: return specialTypes[i].min; break; } } } switch (flags) { case len_max: len = smiGetMaxSize(smiType); break; case len_mean: len = (smiGetMaxSize(smiType) + smiGetMinSize(smiType)) / 2; break; case len_min: len = smiGetMinSize(smiType); break; } return len; }
static void createImportList(SmiModule *smiModule) { SmiNode *smiNode; SmiType *smiType; SmiModule *smiTypeModule; SmiNodekind kind = SMI_NODEKIND_SCALAR | SMI_NODEKIND_COLUMN; SmiImport *smiImport; for (smiNode = smiGetFirstNode(smiModule, kind); smiNode; smiNode = smiGetNextNode(smiNode, kind)) { smiType = smiGetNodeType(smiNode); if (smiType && (smiType->decl == SMI_DECL_IMPLICIT_TYPE)) { smiType = smiGetParentType(smiType); } if (smiType) { smiTypeModule = smiGetTypeModule(smiType); if (smiTypeModule && strcmp(smiTypeModule->name, smiModule->name)) { if (strlen(smiTypeModule->name)) { addImport(smiTypeModule->name, smiType->name); } } if (smiType->basetype == SMI_BASETYPE_INTEGER32) { addImport("SNMPv2-SMI", "Integer32"); } } } for (smiImport = smiGetFirstImport(smiModule); smiImport; smiImport = smiGetNextImport(smiImport)) { if (islower((int) smiImport->name[0]) || (smiImport->module && !strcmp(smiImport->module, "SNMPv2-SMI")) || (smiImport->module && !strcmp(smiImport->module, "SNMPv2-TC"))) { addImport(smiImport->module, smiImport->name); } } }
static void fprintNode(FILE *f, int indent, SmiNode *smiNode, SmiNode *lastSmiNode) { SmiModule *smiModule; SmiType *smiType; char *tag = NULL; if (smiNode->nodekind == SMI_NODEKIND_NODE) { tag = "node"; } else if (smiNode->nodekind == SMI_NODEKIND_CAPABILITIES) { tag = "node"; } else if (smiNode->nodekind == SMI_NODEKIND_TABLE) { tag = "table"; } else if (smiNode->nodekind == SMI_NODEKIND_ROW) { indent += INDENT; tag = "row"; } else if (smiNode->nodekind == SMI_NODEKIND_COLUMN) { indent += 2 * INDENT; tag = "column"; } else if (smiNode->nodekind == SMI_NODEKIND_SCALAR) { tag = "scalar"; } if (lastSmiNode && lastSmiNode->nodekind == SMI_NODEKIND_COLUMN && smiNode->nodekind != SMI_NODEKIND_COLUMN) { fprintNodeEndTag(f, indent + INDENT, "row"); fprintNodeEndTag(f, indent, "table"); } smiType = smiGetNodeType(smiNode); fprintNodeStartTag(f, indent, tag, smiNode); if (smiType && (smiType->basetype != SMI_BASETYPE_UNKNOWN)) { fprintSegment(f, indent + INDENT, "<syntax>\n", 0); smiModule = smiGetTypeModule(smiType); if (smiType->name && smiModule) { fprintSegment(f, indent + 2 *INDENT, "", 0); fprint(f, "<type "); fprintf(f, "module=\"%s\" name=\"%s\"/>\n", smiModule->name, smiType->name); } else { fprintTypedef(f, indent + 2 * INDENT, smiType); } fprintSegment(f, indent + INDENT, "</syntax>\n", 0); } if ((smiNode->nodekind != SMI_NODEKIND_TABLE) && (smiNode->nodekind != SMI_NODEKIND_ROW) && (smiNode->nodekind != SMI_NODEKIND_CAPABILITIES) && (smiNode->nodekind != SMI_NODEKIND_NODE)) { fprintAccess(f, indent + INDENT, smiNode->access); } if (smiType) { fprintValue(f, indent + INDENT, &smiNode->value, smiType); } fprintFormat(f, indent + INDENT, smiNode->format); fprintUnits(f, indent + INDENT, smiNode->units); if (smiNode->nodekind == SMI_NODEKIND_ROW) { fprintIndex(f, indent + INDENT, smiNode); } fprintDescription(f, indent + INDENT, smiNode->description); fprintReference(f, indent + INDENT, smiNode->reference); if (smiNode->nodekind != SMI_NODEKIND_ROW && smiNode->nodekind != SMI_NODEKIND_TABLE) { fprintNodeEndTag(f, indent, tag); } }
static void addMetrics(Metrics *metrics, SmiModule *smiModule) { SmiNode *smiNode; SmiType *smiType; size_t len; for (smiNode = smiGetFirstNode(smiModule, SMI_NODEKIND_ANY); smiNode; smiNode = smiGetNextNode(smiNode, SMI_NODEKIND_ANY)) { len = smiNode->description ? strlen(smiNode->description) : 0; switch (smiNode->nodekind) { case SMI_NODEKIND_TABLE: incrStatusCounter(&metrics->statusTables, smiNode->status); incrStatusCounter(&metrics->statusAll, smiNode->status); incrLengthCounter(&metrics->lengthTables, smiNode->description, smiNode->reference, smiNode->units, smiNode->format); incrLengthCounter(&metrics->lengthAll, smiNode->description, smiNode->reference, smiNode->units, smiNode->format); break; case SMI_NODEKIND_ROW: incrIndexCounter(&metrics->indexTables, smiNode->indexkind); incrLengthCounter(&metrics->lengthRows, smiNode->description, smiNode->reference, smiNode->units, smiNode->format); incrLengthCounter(&metrics->lengthAll, smiNode->description, smiNode->reference, smiNode->units, smiNode->format); incrRowStatusCounter(smiNode); { int cnt = 0; foreachIndexDo(NULL, smiNode, count, &cnt); incrIndexLenCounter(&metrics->indexLenTables, cnt); foreachIndexDo(NULL, smiNode, yadayada, smiModule); } { int cmplx = 0; foreachIndexDo(NULL, smiNode, complexity, &cmplx); incrIndexComplexityCounter(smiModule, smiNode, cmplx); incrIndexComplexityMetric(&metrics->indexComplexity, cmplx); } // count the childs ... { SmiModule *smiModule = smiGetModule("SNMPv2-TC"); SmiNode *childNode; SmiType *rowStatus = smiGetType(smiModule, "RowStatus"); SmiType *storageType = smiGetType(smiModule, "StorageType"); // include index elements not in table int n = 0; for (childNode = smiGetFirstChildNode(smiNode); childNode; childNode = smiGetNextChildNode(childNode)) { n++; if (rowStatus == smiGetNodeType(childNode)) { fprintf(stderr, "**** GEEEEEE - ROWSTATUS\n"); } if (storageType == smiGetNodeType(childNode)) { fprintf(stderr, "**** GEEEEEE - STORAGETYPE\n"); } } incrTableLenCounter(&metrics->tableLength, n); } break; case SMI_NODEKIND_COLUMN: incrBasetypeCounter(&metrics->basetypesColumns, smiNode); incrBasetypeCounter(&metrics->basetypesAll, smiNode); incrStatusCounter(&metrics->statusColumns, smiNode->status); incrStatusCounter(&metrics->statusAll, smiNode->status); incrAccessCounter(&metrics->accessColumns, smiNode->access); incrAccessCounter(&metrics->accessAll, smiNode->access); incrLengthCounter(&metrics->lengthColumns, smiNode->description, smiNode->reference, smiNode->units, smiNode->format); incrLengthCounter(&metrics->lengthAll, smiNode->description, smiNode->reference, smiNode->units, smiNode->format); incrTypeAndNodeUsageCounter(smiModule, smiNode, INCR_TYPE); break; case SMI_NODEKIND_SCALAR: incrBasetypeCounter(&metrics->basetypesScalars, smiNode); incrBasetypeCounter(&metrics->basetypesAll, smiNode); incrStatusCounter(&metrics->statusScalars, smiNode->status); incrStatusCounter(&metrics->statusAll, smiNode->status); incrAccessCounter(&metrics->accessScalars, smiNode->access); incrAccessCounter(&metrics->accessAll, smiNode->access); incrLengthCounter(&metrics->lengthScalars, smiNode->description, smiNode->reference, smiNode->units, smiNode->format); incrLengthCounter(&metrics->lengthAll, smiNode->description, smiNode->reference, smiNode->units, smiNode->format); incrTypeAndNodeUsageCounter(smiModule, smiNode, INCR_TYPE); break; case SMI_NODEKIND_NOTIFICATION: incrStatusCounter(&metrics->statusNotifications, smiNode->status); incrStatusCounter(&metrics->statusAll, smiNode->status); incrLengthCounter(&metrics->lengthNotifications, smiNode->description, smiNode->reference, smiNode->units, smiNode->format); incrLengthCounter(&metrics->lengthAll, smiNode->description, smiNode->reference, smiNode->units, smiNode->format); break; case SMI_NODEKIND_GROUP: incrStatusCounter(&metrics->statusGroups, smiNode->status); incrStatusCounter(&metrics->statusAll, smiNode->status); break; case SMI_NODEKIND_COMPLIANCE: incrStatusCounter(&metrics->statusCompliances, smiNode->status); incrStatusCounter(&metrics->statusAll, smiNode->status); break; } } for (smiType = smiGetFirstType(smiModule); smiType; smiType = smiGetNextType(smiType)) { /* * Ignore all types with empty descriptions coming from the * "SNMPv2-SMI" module since they are not really defined * types but part of the language itself. */ if (! smiType->description) { SmiModule *m = smiGetTypeModule(smiType); if (m && strcmp(m->name, "SNMPv2-SMI") == 0) { continue; } } incrStatusCounter(&metrics->statusTypes, smiType->status); incrStatusCounter(&metrics->statusAll, smiType->status); incrLengthCounter(&metrics->lengthTypes, smiType->description, smiType->reference, smiType->units, smiType->format); incrLengthCounter(&metrics->lengthAll, smiType->description, smiType->reference, smiType->units, smiType->format); } }
static void append_index(SmiSubid *oid, unsigned int *oidlen, SmiNode *indexNode, len_type flags) { SmiInteger32 int32 = 0; SmiUnsigned32 uint32 = 0; SmiType *indexType; SmiModule *indexModule; int i, len = 0; if (! indexNode) return; indexType = smiGetNodeType(indexNode); if (! indexType) return; indexModule = smiGetTypeModule(indexType); switch (indexType->basetype) { case SMI_BASETYPE_OBJECTIDENTIFIER: switch (flags) { case len_max: len = 128 - *oidlen; if (indexNode->implied) len--; break; case len_mean: len = 16; break; case len_min: len = 2; break; } if (! indexNode->implied && *oidlen < 128) { oid[(*oidlen)++] = len; } for (i = 0; i < len && *oidlen < 128; i++) { switch (flags) { case len_max: if (i == 0) { oid[(*oidlen)++] = 2; } else { oid[(*oidlen)++] = 4294967295UL; } break; case len_mean: oid[(*oidlen)++] = i + 1; break; case len_min: oid[(*oidlen)++] = 0; break; } } break; case SMI_BASETYPE_OCTETSTRING: case SMI_BASETYPE_BITS: switch (flags) { case len_max: len = smiGetMaxSize(indexType); break; case len_mean: len = (smiGetMaxSize(indexType) + smiGetMinSize(indexType) / 2); break; case len_min: len = smiGetMinSize(indexType); break; } if (indexModule && indexModule->name && indexType->name) { int i; for (i = 0; specialTypes[i].module; i++) { if (strcmp(specialTypes[i].module, indexModule->name) == 0 && (strcmp(specialTypes[i].name, indexType->name) == 0)) { break; } } if (specialTypes[i].module) { switch (flags) { case len_max: len = specialTypes[i].max; break; case len_mean: len = specialTypes[i].mean; break; case len_min: len = specialTypes[i].min; break; } } } if (! indexNode->implied && *oidlen < 128) { oid[(*oidlen)++] = len; } for (i = 0; i < len && *oidlen < 128; i++) { switch (flags) { case len_max: oid[(*oidlen)++] = 255; break; case len_mean: if (i == 0) { oid[(*oidlen)++] = 1; } else { oid[(*oidlen)++] = (i%2) ? 85 : 170; } break; case len_min: oid[(*oidlen)++] = 0; break; } } break; case SMI_BASETYPE_ENUM: switch (flags) { case len_max: int32 = getAbsMaxEnum(indexType); break; case len_mean: int32 = (getAbsMaxEnum(indexType) - getAbsMinEnum(indexType)) / 2; break; case len_min: int32 = getAbsMinEnum(indexType); break; } if (*oidlen < 128) { oid[(*oidlen)++] = int32; } break; case SMI_BASETYPE_INTEGER32: switch (flags) { case len_max: int32 = getAbsMaxInteger32(indexType); break; case len_mean: int32 = (getAbsMaxInteger32(indexType) + getAbsMinInteger32(indexType)) / 2; break; case len_min: int32 = getAbsMinInteger32(indexType); break; } if (*oidlen < 128) { oid[(*oidlen)++] = int32; } break; case SMI_BASETYPE_UNSIGNED32: switch (flags) { case len_max: uint32 = getMaxUnsigned32(indexType); break; case len_mean: uint32 = (getMaxUnsigned32(indexType) + getMinUnsigned32(indexType)) / 2; break; case len_min: uint32 = getMinUnsigned32(indexType); break; } if (*oidlen < 128) { oid[(*oidlen)++] = uint32; } break; case SMI_BASETYPE_UNKNOWN: case SMI_BASETYPE_INTEGER64: case SMI_BASETYPE_UNSIGNED64: case SMI_BASETYPE_FLOAT32: case SMI_BASETYPE_FLOAT64: case SMI_BASETYPE_FLOAT128: case SMI_BASETYPE_POINTER: /* should never really get here */ break; } }
static void dumpSizeOfCreatePDU(FILE *f, SmiModule *smiModule, SmiNode *smiNode, int ignoreDefaultColumns) { SmiNode *child; SmiType *childType; SmiModule *childTypeModule; int worst = 0; int best = 0; int avg = 0; int b, w, a, n = 0; int isRowStatus; for (child = smiGetFirstChildNode(smiNode); child; child = smiGetNextChildNode(child)) { if (child->access == SMI_ACCESS_READ_WRITE) { /* Ensure RowStatus columns are present even if they * have a default value. */ childType = smiGetNodeType(child); childTypeModule = childType ? smiGetTypeModule(childType) : NULL; isRowStatus = (childType && childType->name && childTypeModule && childTypeModule->name) ? (strcmp(childType->name, "RowStatus") == 0 && strcmp(childTypeModule->name, "SNMPv2-TC") == 0) : 0; /* xxx at least one PDU must be present xxx */ if (ignoreDefaultColumns && child->value.basetype != SMI_BASETYPE_UNKNOWN && !isRowStatus) { continue; } b = ber_len_varbind(child, len_min); a = ber_len_varbind(child, len_mean); w = ber_len_varbind(child, len_max); #if 0 fprintf(f, " %-32s\t[%d..%d] | %d\n", child->name, b, w, a); #endif best += b, worst += w, avg += a, n++; } } /* varbind list sequence length and tag */ best += ber_len_length(best) + 1; avg += ber_len_length(avg) + 1; worst += ber_len_length(worst) + 1; /* request-id as defined in RFC 3416 */ best += ber_len_int32(0); avg += ber_len_int32(1073741824); worst += ber_len_int32(-214783648); /* error-status as defined in RFC 3416 */ best += ber_len_int32(0); avg += ber_len_int32(0); worst += ber_len_int32(18); /* error-index as defined in RFC 3416 */ best += ber_len_int32(0); avg += ber_len_int32(0); worst += ber_len_int32(n-1); /* PDU sequence length and tag */ best += ber_len_length(best) + 1; avg += ber_len_length(avg) + 1; worst += ber_len_length(worst) + 1; fprintf(f, "%-23s %-23s \t%d\t[%d..%d]\n", smiModule->name, smiNode->name, avg, best, worst); }