📄 readmib.c
字号:
/* $Header: /usr/cvsroot/target/src/wrn/wm/demo/snmptalk/readmib.c,v 1.3 2003/01/15 14:04:55 josh Exp $ *//* * Copyright (C) 1999-2005 Wind River Systems, Inc. * All rights reserved. Provided under license only. * Distribution or other use of this software is only * permitted pursuant to the terms of a license agreement * from Wind River Systems (and is otherwise prohibited). * Refer to that license agreement for terms of use. *//**************************************************************************** * Copyright 1993-1997 Epilogue Technology Corporation. * Copyright 1998 Integrated Systems, Inc. * All rights reserved. ****************************************************************************//* reads in a mib as written by the -readtree output mode of Emmissary. *//* * $Log: readmib.c,v $ * Revision 1.3 2003/01/15 14:04:55 josh * directory structure shifting * * Revision 1.2 2001/11/08 16:47:29 tneale * Updated for newset file layout * * Revision 1.1.1.1 2001/11/05 17:49:12 tneale * Tornado shuffle * * Revision 7.11 2001/01/19 22:24:48 paul * Update copyright. * * Revision 7.10 2000/03/17 00:14:34 meister * Update copyright message * * Revision 7.9 1998/10/14 13:04:31 sar * Removed use of errno after STRTOL as our portable string routine * can't use it due to portability concerns. * * Revision 7.8 1998/06/03 21:50:29 sar * Moved nvutils and nvviews to snark/lib so we can have one copy shared * amongst the demos * Moved the common string functions from stdf to common/lib and created * a series of macros for this code to use * Moved strdup to snark/lib and renamed it to etc_strdup to avoid * needing one from the system libraries * * Revision 7.7 1998/02/25 15:21:58 sra * Finish moving types.h, bug.h, and bugdef.h to common/h/. * * Revision 7.6 1998/02/25 04:58:01 sra * Update copyrights. * * Revision 7.5 1997/03/20 06:53:54 sra * DFARS-safe copyright text. Zap! * * Revision 7.4 1997/02/25 10:58:16 sra * Update copyright notice, dust under the bed. * * Revision 7.3 1997/01/08 23:26:53 sar * Updated include files to use envoy/h as appropriate after * move from envoy/utils to snark/snmptalk * * Revision 7.2 1997/01/08 01:58:11 sar * Removed no_pp stuff and updated copyrights * * Revision 7.1 1996/10/26 17:22:47 sra * Cast to suppress compiler warning. * * Revision 7.0 1996/03/18 20:19:20 sar * Updated rev to 7.0 and copyright to 96 * * Revision 6.2 1996/02/27 21:31:57 sar * add an ifdef on sunvarargs for the declaration of error and error2 * to keep the dec in sync with the definition. * * Revision 6.1 1995/10/20 22:39:59 sar * removed cases for VT_BITSTRING, VT_NSAPADDRESS and VT_UINTEGER32 * * Revision 6.0 1995/05/31 21:50:21 sra * Release 6.0. * * Revision 5.0 1994/05/16 15:57:40 sar * Updated revision to 5.0 and copyright to include 1994 * * Revision 4.1 1994/03/22 07:37:01 sra * Silently ignore "$indx" lines, since we don't yet know what to do with * them. * * Revision 4.0 1993/06/24 17:14:56 sar * Updated rev to 4.0 and copyright to 93 * * Revision 1.9 1993/06/13 02:48:16 sar * moved inclusion of stdf.h to be after asn1.h so that NO_PP will be defined * if desired. * * Revision 1.8 1993/06/09 20:01:20 dab * made it work with SunOS varargs as well as stdarg.h * * Revision 1.7 1993/05/25 18:27:42 sar * added new types (bitstring, counter64, nsapaddress and uint32) * * Revision 1.6 1993/05/05 16:12:25 sar * Added some NO_PP defs to keep compilers happy. * * Revision 1.5 1993/03/02 18:22:01 dab * Don't bitch about comments * * Revision 1.4 1993/03/01 18:36:19 dab * Fixed up some warnings from gcc. * * Revision 1.3 1993/02/19 22:16:28 dab * Added RCS stuff to the top. * *//* [clearcase]modification history-------------------01a,19apr05,job update copyright notices*/#include <wrn/wm/common/types.h>#include <wrn/wm/common/install.h>#include <wrn/wm/common/config.h>#include <snmptalk.h>#include <stdio.h>#ifdef SUN_VARARGS# include <varargs.h>#else# include <stdarg.h>#endif#include <ctype.h>#include <errno.h>#include <wrn/wm/snmp/engine/asn1conf.h>#include <wrn/wm/snmp/engine/asn1.h>#include <wrn/wm/snmp/engine/buffer.h>#include <wrn/wm/snmp/engine/mib.h>#include <wrn/wm/snmp/engine/snmpdefs.h>#include <wrn/wm/snmp/engine/snmp.h>#include "readmib.h"#include <wrn/wm/common/glue.h>/* constants*/#define MAX_LINE_LEN 256 /* maximum line length that getline can * read from the input file */#define NODE_ALLOC_COUNT 10 /* number of nametree nodes that new_node * allocates at a time from the free store *//* globals*/nametree* mibt = 0;/* locals*/static char* infilename;static int lineno = 0;static FILE* fin;extern nametree* mibt;/* function prototypes */int read_mib ( char* );void init_oid_tree ( void );char* getline ( void );int parse_node ( char**, char**, char** );int parse_enum ( char**, char**, char** );int check_oid ( char* );nametree* add_node_to_oid_tree ( char*, char*, char* );nametree* add_enum ( char*, char*, char* );nametree* find_node ( char* );nametree* new_node ( void );int convert_type ( char* );void* MemAlloc ( size_t );void MemFree ( void* );char* StrDup ( char* );#ifdef SUN_VARARGSvoid error ( va_alist );void error2 ( va_alist );#elsevoid error ( char* format, ... );void error2 ( char* format, ... );#endif#if 0static char* resolve_type ( int type );#endif/* read_mib reads a disk file containing a "flat" representation of an SNMP MIB and builds a tree representation of the MIB in memory. read_mib places a pointer to the tree it constructs in the global mibt. This tree is in the "nametree" format output by the epilogue MIB compiler -nametree option. The input file is assumed to have been generated by the Epilogue MIB compiler using the -readtree option. The file may contain two types of lines: lines beginning with $obj and lines beginning with $enum. A line beginning with $obj adds a new node to the MIB tree. The format of a $obj line is as follows: $obj <object-identifier> <descriptor> <type> where <object-identifier> is a sequence of non-negative integers separated by dots, and identifies the OBJECT IDENTIFIER name of the node. Each component of the OBJECT IDENTIFIER except the final one is assumed to refer to a node added to the MIB tree by a previous $obj line. The final component of the OBJECT IDENTIFIER must not correspond to a node already added to the MIB tree by a previous $obj line. <descriptor> is the descriptor associated with the MIB tree node. <type> is the "epilogue" name corresponding to the SYNTAX associated with the node. If the node is a non-leaf node, <type> is zero. Otherwise, type is one of the strings "VT_NUMBER", "VT_COUNTER", "VT_STRING", "VT_OBJECT", "VT_IPADDRESS", "VT_TIMETICKS", "VT_GAUGE", "VT_COUNTER64" (without the enclosing quotes). A line beginning with $enum associates an enumerated value with a node in the MIB tree. The format of a $enum line is as follows: $enum <object-identifier> <identifier>(<value>) where <object-identifier> is a sequence of non-negative integers separated by dots, and identifies the OBJECT IDENTIFIER name of the node with which an enumerated value is being associated. A node with the specified name is assumed to have been added to the MIB tree by a prior $obj line. The node should have the type VT_NUMBER. <identifier> is the identifier associated with the enumerated value. <value> is the INTEGER value of the enumerated value. NOTE: read_mib assumes that mibt is 0 on entry. If mibt is not 0, the memory it references is not deallocated, and is lost.*/int read_mib( char* filename ){ char* s; char* keywd; char* oid; char* name; char* type; char* value; infilename = filename; fin = fopen( infilename, "r" ); if (fin == NULL) { error2( "unable to open input file \"%s\" (%d)", infilename, errno ); return 0; } init_oid_tree(); while ((s = getline()) != 0) { keywd = strtok( s, " \t" ); if (keywd != 0) { if (STRCMP( keywd, "$obj" ) == 0) { /* oid tree node */ if (parse_node( &oid, &name, &type )) { if (check_oid( oid )) add_node_to_oid_tree( oid, name, type ); else error( "invalid OBJECT IDENTIFIER \"%s\"", oid ); } else error( "bad $obj" ); } else if (STRCMP( keywd, "$enum" ) == 0) { /* named number */ if (parse_enum( &oid, &name, &value )) { if (check_oid( oid )) add_enum( oid, name, value ); else error( "invalid OBJECT IDENTIFIER \"%s\"", oid ); } else error( "bad $enum" ); } else if (STRCMP( keywd, "$indx" ) == 0) ; /* ignore $indx for now */ else if (keywd[0] == ';') ; /* ignore comments */ else error( "expected $obj or $enum, not \"%s\"", keywd ); } } fclose( fin ); /* return ((mibt == 0) || (mibt->children == 0)) ? 0 : 1; */ return (mibt == 0) ? 0 : 1;}/* init_oid_tree initializes the MIB tree referenced by the global mibt. This tree is in the "nametree" format output by the epilogue MIB compiler -nametree option. init_oid_tree allocates a single root node named "mib_root" and places a pointer to that node in mibt. NOTE: init_oid_tree assumes mibt is 0 on entry. If mibt is not 0, the memory it references is not deallocated, and is lost.*/void init_oid_tree(){ mibt = 0;}/* getline reads the next line of input from the file referenced by the global FILE* fin and returns a pointer to a null- terminated string containing the line. (getline removes the newline character from the input line.) NOTE that the pointer returned by getline references a STATIC location -- each call to getline overwrites the previous line of input. The preprocessor macro MAX_LINE_LEN indicates the maximum line length that getline can handle. If getline encounters an input line longer than MAX_LINE_LEN characters, it discards the line, writes an error message to stderr, and returns a pointer to the next line of input.*/char* getline(){ static char buffer[MAX_LINE_LEN]; char* s; size_t len; int lineTooLong; len = 1; do { lineno++; lineTooLong = 0; do { s = fgets( buffer, MAX_LINE_LEN, fin ); if (s == 0) break; len = STRLEN( buffer ); if (!lineTooLong && (buffer[len-1] != '\n')) { error( "line too long" ); lineTooLong = 1; } } while (buffer[len-1] != '\n'); } while (lineTooLong && (s != 0)); if (s != 0) buffer[len-1] = '\0'; else if (!feof( fin )) error2( "reading from file \"%s\" (%d)\n", infilename, ferror( fin ) ); return s;}/* parse_node is called to continue parsing a line beginning with a $obj directive. It invokes strtok to parse the object identifier, name, and type of the MIB node from the input line held internally by strtok. parse_node returns 1 if it successfully parsed an object identifier, name, and type; otherwise it returns 0. if parse_node returns 1, the character string pointers referenced by the char** parameters oid, name, and type have been set to point to null-terminated strings containing the text of the parsed object identifier, name, and type, respectively. NOTE that parse_node does no checking of the object identifier, name, and type values it returns. A return value of 1 means only three non-empty strings were parsed. NOTE ALSO that the character string pointers returned by parse_node may reference STATIC memory, depending on the type of argument passed as the first argument to strtok prior to invoking parse_node.*/int parse_node( char** oid, char** name, char** type ){ int ok = 0; *oid = *name = *type = 0; *oid = strtok( 0, " \t" ); if (*oid != 0) { *name = strtok( 0, " \t" ); if (*name != 0) { *type = strtok( 0, " \t" ); ok = 1; } } return ok;}/* check_oid verifies that its argument is a syntactically valid oid consisting of a sequence of non-negative integers separated by dots. If the string represents a valid oid with at least one componenet, check_oid returns the number of components in the oid; otherwise it returns 0.*/int check_oid( char* oid ){ register char* s = oid; char* startOfSubID; size_t len; int comps = 0; if (!isdigit(*s)) return 0; /* oid must start with a digit */ while (1) { startOfSubID = s; /* remember start of subidentifier */ while (isdigit(*s)) s++; /* accept digits */ len = (size_t) (s - startOfSubID); if ((len < 1) || (len > 10) || ((len == 10) && (memcmp( startOfSubID, "4294967295", 10 ) > 0))) return 0; /* subidentifier too big */ comps++; if (*s == '.') s++; /* skip over dot and loop for another subidentifier */ else break; } return (*s == '\0') ? comps : 0;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -