⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 mib.c

📁 snmp up 2
💻 C
📖 第 1 页 / 共 5 页
字号:
/* * mib.c * * $Id: mib.c,v 5.33 2003/01/14 01:30:59 rstory Exp $ * * Update: 1998-07-17 <jhy@gsu.edu> * Added print_oid_report* functions. * *//**********************************************************************	Copyright 1988, 1989, 1991, 1992 by Carnegie Mellon University                      All Rights ReservedPermission to use, copy, modify, and distribute this software and itsdocumentation for any purpose and without fee is hereby granted,provided that the above copyright notice appear in all copies and thatboth that copyright notice and this permission notice appear insupporting documentation, and that the name of CMU not beused in advertising or publicity pertaining to distribution of thesoftware without specific, written prior permission.CMU DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDINGALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALLCMU BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES ORANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THISSOFTWARE.******************************************************************/#include <net-snmp/net-snmp-config.h>#include <stdio.h>#include <ctype.h>#include <sys/types.h>#if HAVE_NETINET_IN_H#include <netinet/in.h>#endif#if TIME_WITH_SYS_TIME# ifdef WIN32#  include <sys/timeb.h># else#  include <sys/time.h># endif# include <time.h>#else# if HAVE_SYS_TIME_H#  include <sys/time.h># else#  include <time.h># endif#endif#if HAVE_STRING_H#include <string.h>#else#include <strings.h>#endif#if HAVE_STDLIB_H#include <stdlib.h>#endif#if HAVE_SYS_SELECT_H#include <sys/select.h>#endif#if HAVE_WINSOCK_H#include <winsock.h>#endif#if HAVE_DMALLOC_H#include <dmalloc.h>#endif#include <net-snmp/types.h>#include <net-snmp/output_api.h>#include <net-snmp/config_api.h>#include <net-snmp/utilities.h>#include <net-snmp/library/asn1.h>#include <net-snmp/library/snmp_api.h>#include <net-snmp/library/mib.h>#include <net-snmp/library/parse.h>#include <net-snmp/library/int64.h>#include <net-snmp/library/snmp_client.h>/** @defgroup mib_utilities mib parsing and datatype manipulation routines. *  @ingroup library * *  @{ */static char    *uptimeString(u_long, char *);static struct tree *_get_realloc_symbol(const oid * objid, size_t objidlen,                                        struct tree *subtree,                                        u_char ** buf, size_t * buf_len,                                        size_t * out_len,                                        int allow_realloc,                                        int *buf_overflow,                                        struct index_list *in_dices,                                        size_t * end_of_known);static void     print_tree_node(FILE *, struct tree *, int);static void     handle_mibdirs_conf(const char *token, char *line);static void     handle_mibs_conf(const char *token, char *line);static void     handle_mibfile_conf(const char *token, char *line);/* * helper functions for get_module_node  */static int      node_to_oid(struct tree *, oid *, size_t *);static int      _add_strings_to_oid(struct tree *, char *,                                    oid *, size_t *, size_t);extern struct tree *tree_head;static struct tree *tree_top;struct tree    *Mib;            /* Backwards compatibility */oid             RFC1213_MIB[] = { 1, 3, 6, 1, 2, 1 };static char     Standard_Prefix[] = ".1.3.6.1.2.1";/* * Set default here as some uses of read_objid require valid pointer.  */static char    *Prefix = &Standard_Prefix[0];typedef struct _PrefixList {    const char     *str;    int             len;}              *PrefixListPtr, PrefixList;/* * Here are the prefix strings. * Note that the first one finds the value of Prefix or Standard_Prefix. * Any of these MAY start with period; all will NOT end with period. * Period is added where needed.  See use of Prefix in this module. */PrefixList      mib_prefixes[] = {    {&Standard_Prefix[0]},      /* placeholder for Prefix data */    {".iso.org.dod.internet.mgmt.mib-2"},    {".iso.org.dod.internet.experimental"},    {".iso.org.dod.internet.private"},    {".iso.org.dod.internet.snmpParties"},    {".iso.org.dod.internet.snmpSecrets"},    {NULL, 0}                   /* end of list */};/** * @internal * Converts timeticks to hours, minutes, seconds string. * * @param timeticks    The timeticks to convert. * @param buf          Buffer to write to, has to be at  *                     least 64 Bytes large. *        * @return The buffer. */static char    *uptimeString(u_long timeticks, char *buf){    int             centisecs, seconds, minutes, hours, days;    if (netsnmp_ds_get_boolean(NETSNMP_DS_LIBRARY_ID, NETSNMP_DS_LIB_NUMERIC_TIMETICKS)) {        sprintf(buf, "%lu", timeticks);        return buf;    }    centisecs = timeticks % 100;    timeticks /= 100;    days = timeticks / (60 * 60 * 24);    timeticks %= (60 * 60 * 24);    hours = timeticks / (60 * 60);    timeticks %= (60 * 60);    minutes = timeticks / 60;    seconds = timeticks % 60;    if (netsnmp_ds_get_boolean(NETSNMP_DS_LIBRARY_ID, NETSNMP_DS_LIB_QUICK_PRINT))        sprintf(buf, "%d:%d:%02d:%02d.%02d",                days, hours, minutes, seconds, centisecs);    else {        if (days == 0) {            sprintf(buf, "%d:%02d:%02d.%02d",                    hours, minutes, seconds, centisecs);        } else if (days == 1) {            sprintf(buf, "%d day, %d:%02d:%02d.%02d",                    days, hours, minutes, seconds, centisecs);        } else {            sprintf(buf, "%d days, %d:%02d:%02d.%02d",                    days, hours, minutes, seconds, centisecs);        }    }    return buf;}/** * @internal * Prints the character pointed to if in human-readable ASCII range, * otherwise prints a dot. * * @param buf Buffer to print the character to. * @param ch  Character to print. */static voidsprint_char(char *buf, const u_char ch){    if (isprint(ch)) {        sprintf(buf, "%c", (int) ch);    } else {        sprintf(buf, ".");    }}/** * Prints a hexadecimal string into a buffer. * * The characters pointed by *cp are encoded as hexadecimal string. * * If allow_realloc is true the buffer will be (re)allocated to fit in the  * needed size. (Note: *buf may change due to this.) *  * @param buf      address of the buffer to print to. * @param buf_len  address to an integer containing the size of buf. * @param out_len  incremented by the number of characters printed. * @param allow_realloc if not zero reallocate the buffer to fit the  *                      needed size. * @param cp       the array of characters to encode. * @param len      the array length of cp. *  * @return 1 on success, or 0 on failure (out of memory, or buffer to *         small when not allowed to realloc.) */intsprint_realloc_hexstring(u_char ** buf, size_t * buf_len, size_t * out_len,                         int allow_realloc, const u_char * cp, size_t len){    const u_char   *tp;    size_t          lenleft;    for (; len >= 16; len -= 16) {        while ((*out_len + 50) >= *buf_len) {            if (!(allow_realloc && snmp_realloc(buf, buf_len))) {                return 0;            }        }        sprintf((char *) (*buf + *out_len),                "%02X %02X %02X %02X %02X %02X %02X %02X ", cp[0], cp[1],                cp[2], cp[3], cp[4], cp[5], cp[6], cp[7]);        *out_len += strlen((char *) (*buf + *out_len));        cp += 8;        sprintf((char *) (*buf + *out_len),                "%02X %02X %02X %02X %02X %02X %02X %02X", cp[0], cp[1],                cp[2], cp[3], cp[4], cp[5], cp[6], cp[7]);        *out_len += strlen((char *) (*buf + *out_len));        cp += 8;        if (netsnmp_ds_get_boolean(NETSNMP_DS_LIBRARY_ID, NETSNMP_DS_LIB_PRINT_HEX_TEXT)) {            while ((*out_len + 21) >= *buf_len) {                if (!(allow_realloc && snmp_realloc(buf, buf_len))) {                    return 0;                }            }            sprintf((char *) (*buf + *out_len), "  [");            *out_len += strlen((char *) (*buf + *out_len));            for (tp = cp - 16; tp < cp; tp++) {                sprint_char((char *) (*buf + *out_len), *tp);                (*out_len)++;            }            sprintf((char *) (*buf + *out_len), "]");            *out_len += strlen((char *) (*buf + *out_len));        }        if (len > 16) {            while ((*out_len + 2) >= *buf_len) {                if (!(allow_realloc && snmp_realloc(buf, buf_len))) {                    return 0;                }            }            *(*buf + (*out_len)++) = '\n';            *(*buf + *out_len) = 0;        }    }    lenleft = len;    for (; len > 0; len--) {        while ((*out_len + 4) >= *buf_len) {            if (!(allow_realloc && snmp_realloc(buf, buf_len))) {                return 0;            }        }        sprintf((char *) (*buf + *out_len), "%02X ", *cp++);        *out_len += strlen((char *) (*buf + *out_len));    }    if ((lenleft > 0)        && netsnmp_ds_get_boolean(NETSNMP_DS_LIBRARY_ID, NETSNMP_DS_LIB_PRINT_HEX_TEXT)) {        while ((*out_len + 5 + lenleft) >= *buf_len) {            if (!(allow_realloc && snmp_realloc(buf, buf_len))) {                return 0;            }        }        sprintf((char *) (*buf + *out_len), "  [");        *out_len += strlen((char *) (*buf + *out_len));        for (tp = cp - lenleft; tp < cp; tp++) {            sprint_char((char *) (*buf + *out_len), *tp);            (*out_len)++;        }        sprintf((char *) (*buf + *out_len), "]");        *out_len += strlen((char *) (*buf + *out_len));    }    return 1;}/** * Prints an ascii string into a buffer. * * The characters pointed by *cp are encoded as an ascii string. *  * If allow_realloc is true the buffer will be (re)allocated to fit in the  * needed size. (Note: *buf may change due to this.) *  * @param buf      address of the buffer to print to. * @param buf_len  address to an integer containing the size of buf. * @param out_len  incremented by the number of characters printed. * @param allow_realloc if not zero reallocate the buffer to fit the  *                      needed size. * @param cp       the array of characters to encode. * @param len      the array length of cp. *  * @return 1 on success, or 0 on failure (out of memory, or buffer to *         small when not allowed to realloc.) */intsprint_realloc_asciistring(u_char ** buf, size_t * buf_len,                           size_t * out_len, int allow_realloc,                           const u_char * cp, size_t len){    int             i;    for (i = 0; i < (int) len; i++) {        if (isprint(*cp)) {            if (*cp == '\\' || *cp == '"') {                if ((*out_len >= *buf_len) &&                    !(allow_realloc && snmp_realloc(buf, buf_len))) {                    return 0;                }                *(*buf + (*out_len)++) = '\\';            }            if ((*out_len >= *buf_len) &&                !(allow_realloc && snmp_realloc(buf, buf_len))) {                return 0;            }            *(*buf + (*out_len)++) = *cp++;        } else {            if ((*out_len >= *buf_len) &&

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -