📄 mibutils.c
字号:
buf[buflen-1] = '\0'; len = STRLEN(buf); buflen -= len; buf += len; if (verbose && (buflen > 1)) { sprintf(tmpbuf, "(%ld)", val); strncpy(buf, tmpbuf, buflen); buf[buflen-1] = '\0'; } return; } } } if (buflen > 1) { sprintf(tmpbuf, "%ld", val); strncpy(buf, tmpbuf, buflen); buf[buflen-1] = '\0'; } else buf[0] = '\0';}/* Provide name to OID translation. Fills in the passed OID buffer * and returns the number of components needed for the OID, 0 if not * found. */int string2oid(char *name, /* name to search for */ OIDC_T *oid, /* OID buffer to fill */ int oidlen) /* length of OID buffer */{ int count; int depth; int ret; struct nametree *nt; char *cp; count = 0; while (*name) { if (ifisnumber(name)) { if (count < oidlen) oid[count] = atol(name); while (isdigit(*name)) name++; if (*name == '.') name++; count++; } else { if (count == 0) nt = mibt; else { nt = oid_find(oid + count, count, mibt, &depth); if (nt == 0) { printf("lost already"); return 0; } } cp = name; while (!((*cp == '.') || (*cp == '\0'))) cp++; if (*cp == '.') *cp++ = '\0'; ret = mib_bfsearch(nt, name, oid, count < oidlen ? oidlen - count : 0); if (ret == 0) return 0; count += ret; name = cp; } } return count;}/* Scans a string OID to see if the first element is all digits. */static int ifisnumber(char *str){ while (isdigit(*str)) str++; if ((*str == '.') || (*str == '\0')) return 1; return 0;}/* Does a breadth-first search of the MIB tree looking for NAME, starting at * the passed starting point and filling in the OID buffer as it goes along. * Returns the OID length or 0 if none found. */static int mib_bfsearch(struct nametree *start, /* starting point for search */ char *name, /* name to search for */ OIDC_T *oid, /* OID buffer to fill */ int oidlen) /* length of OID buffer */{ struct bft { struct bft *link; struct nametree *nt; } *current, *next, *tmp; struct nametree *nt; int level; int ret; ret = 0; current = 0; /* this is just to keep the compiler happy * that current really is initialized. */ /* start off at the top of the tree */ next = (struct bft *)malloc(sizeof(struct bft)); if (next == 0) return 0; next->link = 0; next->nt = start; /* each time through the loop we check the possibilities at this level * and make a list of the next level we're going to need to check. */ for (level = 1; next; level++) { /* iterate over all the remembered children for this level */ current = next; next = 0; while (current) { /* check each arc for a match and remember any children * seen */ for (nt = current->nt; nt; nt = nt->sibling) { /* Is this the one? */ if (STRICMP(nt->name, name) == 0) { ret = level; for (; nt; nt = nt->parent) if (level < oidlen) oid[--level] = nt->number; goto done; } /* if this isn't a leaf node and it has children, remember * them */ if ((nt->type == 0) & (nt->children != 0)) { if ((tmp = (struct bft *)malloc(sizeof(struct bft))) == 0) goto done; tmp->nt = nt->children; tmp->link = next; next = tmp; } } /* free this block */ tmp = current; current = tmp->link; free((char *)tmp); } } /* free up the remainder of the allocated buffers and return */ done: while (current) { tmp = current; current = current->link; free((char *)tmp); } while (next) { tmp = next; next = next->link; free((char *)tmp); } return ret;}/* * This routine is used to find a leaf integer's enumeration value in the * MIB. StartingOid should contain the OID of a leaf integer. The search * for an enumeration's name (using the Name argument) begins at the first * immediate child of the node indicated by StartingOid. All enumerations * for a leaf integer will be stored as children of that leaf. The routine * returns the value of the enumeration, if it can find it. Otherwise, it * returns -1. * Restrictions: the enumeration value stored in the MIB can't be -1 as this * value is used to indicate that Name could not be found as an immediate * child of the MIB node indicated by StartingOid. */int FindLeafValue(char* Name, OIDC_T* StartingOid, int StartingOidLen ){ struct nametree *nt; int depth; nt = oid_find( StartingOid, StartingOidLen, mibt, &depth ); if ( nt != 0 ) { if ( nt->children != 0 ) { nt = nt->children; while ( nt ) { if (STRICMP(nt->name, Name) == 0) { return (int) (nt->number); } nt = nt->sibling; } return -1; /* ran out of siblings to check... */ } else { return -1; /* no children of the starting node... */ } } else { return -1; /* couldn't even find the starting point... */ } } /* close FindLeafValue(... *//******************************************************************************* strToOctet - Convert a string into an octet string.** DESCRIPTION* If the string begins with a '#' then the string is to be converted.* Use one the following formats.* Decmial: # 192 1 192 172 0 6* Hex: # 0xC0 0x01 0xC0 0xAC 0x0 0x06* Octal: # 0300 01 0300 0254 00 06* Mix: # 0xC0 1 0300 0254 0x0 06* * The above formats translate into the following octet string with a length* of 6 octets.* 'C001C0AC0006'H** Else if the first characters is not a '#' then it is a simple copy.** RETURNS* The number of octets** NOMANUAL*/unsigned long strToOctet (unsigned char * str, unsigned char * oct){ int i = 0; int j; char subId[32]; char * p = str; char hexFormat = 1; if ( p == NULL ) return 0; if ( *p == '#' || (*p == '0' && (*(p+1) == 'x' || *(p+1) == 'X'))) { if (*p == '#') p++; else { /* we have string of octets */ hexFormat = 1; p += 2; } while( *p != '\0' ) { /* move past whitespaces and/or a comma and * other delimiting characters */ while( *p == ' ' || *p == '\t' || *p == ',' || *p == ':' || *p == '.') p++; j = 0; /* if not a seperator then copy to subId array. */ while ( *p != ' ' && *p != '\t' && *p != ',' && *p != ':' && *p != '.' && *p != '\0') subId[j++] = *p++; subId[j] = '\0'; if (hexFormat == 1) oct[i++] = (unsigned char)strtoul (subId, (char **)NULL, 16); else /* let the format determine the base of the number. * 123=decmial, 0300=octal, 0xAC=hex */ oct [i++] = (unsigned char)strtoul (subId, (char **)NULL, 0); } } else { strcpy(oct, str); i = strlen(oct); } return i;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -