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

📄 table_dataset.c

📁 开发snmp的开发包有两个开放的SNMP开发库
💻 C
📖 第 1 页 / 共 3 页
字号:
     * loop through indexes and add types      */    for (index = tp->indexes; index; index = index->next) {        if (!snmp_parse_oid(index->ilabel, name, &name_length) ||            (NULL ==             (indexnode = get_tree(name, name_length, get_tree_head())))) {            config_pwarn("can't instatiate table since "                         "I don't know anything about one index");            snmp_log(LOG_WARNING, "  index %s not found in tree\n",                     index->ilabel);            return;             /* xxx mem leak */        }                    type = mib_to_asn_type(indexnode->type);        if (type == (u_char) - 1) {            config_pwarn("unknown index type");            return;             /* xxx mem leak */        }        /*         * if implied, mark it as such. also mark fixed length         * octet strings as implied (ie no length prefix) as well.         * */        if ((TYPE_OCTETSTR == indexnode->type) &&  /* octet str */            (NULL != indexnode->ranges) &&         /* & has range */            (NULL == indexnode->ranges->next) &&   /*   but only one */            (indexnode->ranges->high ==            /*   & high==low */             indexnode->ranges->low)) {            type |= ASN_PRIVATE;            fixed_len = indexnode->ranges->high;        }        else if (index->isimplied)            type |= ASN_PRIVATE;                DEBUGMSGTL(("table_set_add_table",                    "adding default index of type %d\n", type));        netsnmp_table_dataset_add_index(table_set, type);        /*         * hack alert: for fixed lenght strings, save the         * lenght for use during oid parsing.         */        if (fixed_len) {            /*             * find last (just added) index             */            netsnmp_variable_list *var =  table_set->table->indexes_template;            while (NULL != var->next_variable)                var = var->next_variable;            var->val_len = fixed_len;        }    }}/** @internal */voidnetsnmp_config_parse_table_set(const char *token, char *line){    oid             table_name[MAX_OID_LEN];    size_t          table_name_length = MAX_OID_LEN;    struct tree    *tp;    netsnmp_table_data_set *table_set;    data_set_tables *tables;    unsigned int    mincol = 0xffffff, maxcol = 0;    char           *pos;    /*     * instatiate a fake table based on MIB information      */    DEBUGMSGTL(("9:table_set_add_table", "processing '%s'\n", line));    if (NULL != (pos = strchr(line,' '))) {        config_pwarn("ignoring extra tokens on line");        snmp_log(LOG_WARNING,"  ignoring '%s'\n", pos);        *pos = '\0';    }    /*     * check for duplicate table     */    tables = (data_set_tables *) netsnmp_get_list_data(auto_tables, line);    if (NULL != tables) {        config_pwarn("duplicate table definition");        return;    }    /*     * parse oid and find tree structure     */    if (!snmp_parse_oid(line, table_name, &table_name_length)) {        config_pwarn            ("can't instatiate table since I can't parse the table name");        return;    }    if(NULL == (tp = get_tree(table_name, table_name_length,                              get_tree_head()))) {        config_pwarn("can't instatiate table since "                     "I can't find mib information about it");        return;    }    if (NULL == (tp = tp->child_list) || NULL == tp->child_list) {        config_pwarn("can't instatiate table since it doesn't appear to be "                     "a proper table (no children)");        return;    }    table_set = netsnmp_create_table_data_set(line);    /*     * check for augments indexes     */    if (NULL != tp->augments) {        oid             name[MAX_OID_LEN];        size_t          name_length = MAX_OID_LEN;        struct tree    *tp2;            if (!snmp_parse_oid(tp->augments, name, &name_length)) {            config_pwarn("I can't parse the augment tabel name");            snmp_log(LOG_WARNING, "  can't parse %s\n", tp->augments);            return;        }        if(NULL == (tp2 = get_tree(name, name_length, get_tree_head()))) {            config_pwarn("can't instatiate table since "                         "I can't find mib information about augment table");            snmp_log(LOG_WARNING, "  table %s not found in tree\n",                     tp->augments);            return;        }        _table_set_add_indexes(table_set, tp2);    }    _table_set_add_indexes(table_set, tp);        /*     * loop through children and add each column info      */    for (tp = tp->child_list; tp; tp = tp->next_peer) {        int             canwrite = 0;        u_char          type;        type = mib_to_asn_type(tp->type);        if (type == (u_char) - 1) {            config_pwarn("unknown column type");            return;             /* xxx mem leak */        }        DEBUGMSGTL(("table_set_add_table",                    "adding column %s(%d) of type %d (access %d)\n",                    tp->label, tp->subid, type, tp->access));        switch (tp->access) {        case MIB_ACCESS_CREATE:            table_set->allow_creation = 1;        case MIB_ACCESS_READWRITE:        case MIB_ACCESS_WRITEONLY:            canwrite = 1;        case MIB_ACCESS_READONLY:            DEBUGMSGTL(("table_set_add_table",                        "adding column %d of type %d\n", tp->subid, type));            netsnmp_table_set_add_default_row(table_set, tp->subid, type,                                              canwrite, NULL, 0);            mincol = SNMP_MIN(mincol, tp->subid);            maxcol = SNMP_MAX(maxcol, tp->subid);            break;        case MIB_ACCESS_NOACCESS:        case MIB_ACCESS_NOTIFY:            break;        default:            config_pwarn("unknown column access type");            break;        }    }    /*     * register the table      */    netsnmp_register_table_data_set(netsnmp_create_handler_registration                                    (line, NULL, table_name,                                     table_name_length,                                     HANDLER_CAN_RWRITE), table_set, NULL);    netsnmp_register_auto_data_table(table_set, NULL);}#endif /* NETSNMP_DISABLE_MIB_LOADING *//** @internal */voidnetsnmp_config_parse_add_row(const char *token, char *line){    char            buf[SNMP_MAXBUF_MEDIUM];    char            tname[SNMP_MAXBUF_MEDIUM];    size_t          buf_size;    int             rc;    data_set_tables *tables;    netsnmp_variable_list *vb;  /* containing only types */    netsnmp_table_row *row;    netsnmp_table_data_set_storage *dr;    line = copy_nword(line, tname, SNMP_MAXBUF_MEDIUM);    tables = (data_set_tables *) netsnmp_get_list_data(auto_tables, tname);    if (!tables) {        config_pwarn("Unknown table trying to add a row");        return;    }    /*     * do the indexes first      */    row = netsnmp_create_table_data_row();    for (vb = tables->table_set->table->indexes_template; vb;         vb = vb->next_variable) {        if (!line) {            config_pwarn("missing an index value");            return;        }        DEBUGMSGTL(("table_set_add_row", "adding index of type %d\n",                    vb->type));        buf_size = SNMP_MAXBUF_MEDIUM;        line = read_config_read_memory(vb->type, line, buf, &buf_size);        netsnmp_table_row_add_index(row, vb->type, buf, buf_size);    }    /*     * then do the data      */    for (dr = tables->table_set->default_row; dr; dr = dr->next) {        if (!line) {            config_pwarn("missing a data value. "                         "All columns must be specified.");            snmp_log(LOG_WARNING,"  can't find value for column %d\n",                     dr->column - 1);            return;        }        buf_size = SNMP_MAXBUF_MEDIUM;        line = read_config_read_memory(dr->type, line, buf, &buf_size);        DEBUGMSGTL(("table_set_add_row",                    "adding data at column %d of type %d\n", dr->column,                    dr->type));        netsnmp_set_row_column(row, dr->column, dr->type, buf, buf_size);        if (dr->writable)            netsnmp_mark_row_column_writable(row, dr->column, 1);       /* make writable */    }    rc = netsnmp_table_data_add_row(tables->table_set->table, row);    if (SNMPERR_SUCCESS != rc) {        config_pwarn("error adding table row");    }    if (NULL != line) {        config_pwarn("extra data value. Too many columns specified.");        snmp_log(LOG_WARNING,"  extra data '%s'\n", line);    }}/* ================================== * * Data Set API: Row operations * * ================================== *//** returns the first row in the table */netsnmp_table_row *netsnmp_table_data_set_get_first_row(netsnmp_table_data_set *table){    return netsnmp_table_data_get_first_row(table->table);}/** returns the next row in the table */netsnmp_table_row *netsnmp_table_data_set_get_next_row(netsnmp_table_data_set *table,                                    netsnmp_table_row      *row){    return netsnmp_table_data_get_next_row(table->table, row);}intnetsnmp_table_set_num_rows(netsnmp_table_data_set *table){    if (!table)        return 0;    return netsnmp_table_data_num_rows(table->table);}/* ================================== * * Data Set API: Column operations * * ================================== *//** Finds a column within a given storage set, given the pointer to   the start of the storage set list.*/netsnmp_table_data_set_storage *netsnmp_table_data_set_find_column(netsnmp_table_data_set_storage *start,                                   unsigned int column){    while (start && start->column != column)        start = start->next;    return start;}/** * marks a given column in a row as writable or not. */intnetsnmp_mark_row_column_writable(netsnmp_table_row *row, int column,                                 int writable){    netsnmp_table_data_set_storage *data;    if (!row)        return SNMPERR_GENERR;    data = (netsnmp_table_data_set_storage *) row->data;    data = netsnmp_table_data_set_find_column(data, column);    if (!data) {        /*         * create it          */        data = SNMP_MALLOC_TYPEDEF(netsnmp_table_data_set_storage);        if (!data) {            snmp_log(LOG_CRIT, "no memory in netsnmp_set_row_column");            return SNMPERR_MALLOC;        }        data->column = column;        data->writable = writable;        data->next = row->data;        row->data = data;    } else {        data->writable = writable;    }    return SNMPERR_SUCCESS;}/** * sets a given column in a row with data given a type, value, and * length.  Data is memdup'ed by the function. */intnetsnmp_set_row_column(netsnmp_table_row *row, unsigned int column,                       int type, const char *value, size_t value_len){    netsnmp_table_data_set_storage *data;    if (!row)        return SNMPERR_GENERR;    data = (netsnmp_table_data_set_storage *) row->data;    data = netsnmp_table_data_set_find_column(data, column);    if (!data) {        /*         * create it          */        data = SNMP_MALLOC_TYPEDEF(netsnmp_table_data_set_storage);        if (!data) {            snmp_log(LOG_CRIT, "no memory in netsnmp_set_row_column");            return SNMPERR_MALLOC;        }        data->column = column;        data->type = type;        data->next = row->data;        row->data = data;    }    if (value) {        if (data->type != type)            return SNMPERR_GENERR;        SNMP_FREE(data->data.voidp);        if (value_len) {            if (memdup(&data->data.string, value, (value_len)) !=                SNMPERR_SUCCESS) {                snmp_log(LOG_CRIT, "no memory in netsnmp_set_row_column");                return SNMPERR_MALLOC;            }        } else {            data->data.string = malloc(1);        }        data->data_len = value_len;    }    return SNMPERR_SUCCESS;}/* ================================== * * Data Set API: Index operations * * ================================== *//** adds an index to the table.  Call this repeatly for each index. */NETSNMP_INLINE voidnetsnmp_table_dataset_add_index(netsnmp_table_data_set *table, u_char type){    if (!table)        return;    netsnmp_table_data_add_index(table->table, type);}/** adds multiple indexes to a table_dataset helper object. *  To end the list, use a 0 after the list of ASN index types. */void#if HAVE_STDARG_Hnetsnmp_table_set_add_indexes(netsnmp_table_data_set *tset,                              ...)#elsenetsnmp_table_set_add_indexes(va_alist)     va_dcl#endif{    va_list         debugargs;    int             type;#if HAVE_STDARG_H    va_start(debugargs, tset);#else    netsnmp_table_data_set *tset;    va_start(debugargs);    tset = va_arg(debugargs, netsnmp_table_data_set *);#endif    while ((type = va_arg(debugargs, int)) != 0) {        netsnmp_table_dataset_add_index(tset, (u_char)type);    }    va_end(debugargs);}/** @}  */

⌨️ 快捷键说明

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