📄 table_tdata.c
字号:
#include <net-snmp/net-snmp-config.h>#if HAVE_STRING_H#include <string.h>#else#include <strings.h>#endif#include <net-snmp/net-snmp-includes.h>#include <net-snmp/agent/net-snmp-agent-includes.h>#include <net-snmp/agent/table.h>#include <net-snmp/agent/table_tdata.h>#include <net-snmp/agent/table_container.h>#include <net-snmp/agent/read_only.h>#if HAVE_DMALLOC_H#include <dmalloc.h>#endif/** @defgroup tdata tdata * Implement a table with datamatted storage. * @ingroup table * * This helper helps you implement a table where all the rows are * expected to be stored within the agent itself and not in some * external storage location. It can be used to store a list of * rows, where a row consists of the indexes to the table and a * generic data pointer. You can then implement a subhandler which * is passed the exact row definition and data it must return data * for or accept data for. Complex GETNEXT handling is greatly * simplified in this case. * * @{ *//* ================================== * * TData API: Table maintenance * * ================================== *//* * generates the index portion of an table oid from a varlist. */void_netsnmp_tdata_generate_index_oid(netsnmp_tdata_row *row){ build_oid(&row->oid_index.oids, &row->oid_index.len, NULL, 0, row->indexes);}/** creates and returns a 'tdata' table data structure */netsnmp_tdata *netsnmp_tdata_create_table(const char *name, long flags){ netsnmp_tdata *table = SNMP_MALLOC_TYPEDEF(netsnmp_tdata); if ( !table ) return NULL; if (name) table->name = strdup(name); table->container = netsnmp_container_find( "table_container" ); return table;}/** creates and returns a 'tdata' table data structure */voidnetsnmp_tdata_delete_table(netsnmp_tdata *table){ if (!table) return; if (table->name) free(table->name); if (table->container) CONTAINER_FREE(table->container); SNMP_FREE(table); return;}/** creates and returns a pointer to new row data structure */netsnmp_tdata_row *netsnmp_tdata_create_row(void){ netsnmp_tdata_row *row = SNMP_MALLOC_TYPEDEF(netsnmp_tdata_row); return row;}/** clones a 'tdata' row. DOES NOT CLONE THE TABLE-SPECIFIC ENTRY DATA. */netsnmp_tdata_row *netsnmp_tdata_clone_row(netsnmp_tdata_row *row){ netsnmp_tdata_row *newrow = NULL; if (!row) return NULL; memdup((u_char **) & newrow, (u_char *) row, sizeof(netsnmp_tdata_row)); if (!newrow) return NULL; if (row->indexes) { newrow->indexes = snmp_clone_varbind(newrow->indexes); if (!newrow->indexes) { SNMP_FREE(newrow); return NULL; } } if (row->oid_index.oids) { memdup((u_char **) & newrow->oid_index.oids, (u_char *) row->oid_index.oids, row->oid_index.len * sizeof(oid)); if (!newrow->oid_index.oids) { if (newrow->indexes) snmp_free_varbind(newrow->indexes); SNMP_FREE(newrow); return NULL; } } return newrow;}/** copy the contents of a 'tdata' row. DOES NOT COPY THE TABLE-SPECIFIC ENTRY DATA. */intnetsnmp_tdata_copy_row(netsnmp_tdata_row *dst_row, netsnmp_tdata_row *src_row){ if ( !src_row || !dst_row ) return -1; memcpy((u_char *) dst_row, (u_char *) src_row, sizeof(netsnmp_tdata_row)); if (src_row->indexes) { dst_row->indexes = snmp_clone_varbind(src_row->indexes); if (!dst_row->indexes) return -1; } if (src_row->oid_index.oids) { memdup((u_char **) &dst_row->oid_index.oids, (u_char *) src_row->oid_index.oids, src_row->oid_index.len * sizeof(oid)); if (!dst_row->oid_index.oids) return -1; } return 0;}/** deletes the memory used by the specified row * returns the table-specific entry data * (that it doesn't know how to delete) */void *netsnmp_tdata_delete_row(netsnmp_tdata_row *row){ void *data; if (!row) return NULL; /* * free the memory we can */ if (row->indexes) snmp_free_varbind(row->indexes); SNMP_FREE(row->oid_index.oids); data = row->data; free(row); /* * return the void * pointer */ return data;}/** * Adds a row to the given table (stored in proper lexographical order). * * returns SNMPERR_SUCCESS on successful addition. * or SNMPERR_GENERR on failure (E.G., indexes already existed) */intnetsnmp_tdata_add_row(netsnmp_tdata *table, netsnmp_tdata_row *row){ if (!row || !table) return SNMPERR_GENERR; if (row->indexes) _netsnmp_tdata_generate_index_oid(row); if (!row->oid_index.oids) { snmp_log(LOG_ERR, "illegal data attempted to be added to table %s (no index)\n", table->name); return SNMPERR_GENERR; } /* * The individual index values probably won't be needed, * so this memory can be released. * Note that this is purely internal to the helper. * The calling application can set this flag as * a hint to the helper that these values aren't * required, but it's up to the helper as to * whether it takes any notice or not! */ if (table->flags & TDATA_FLAG_NO_STORE_INDEXES) { snmp_free_varbind(row->indexes); row->indexes = NULL; } /* * add this row to the stored table */ CONTAINER_INSERT( table->container, row ); DEBUGMSGTL(("tdata_add_row", "added row (%x)\n", row)); return SNMPERR_SUCCESS;}/** swaps out origrow with newrow. This does *not* delete/free anything! */voidnetsnmp_tdata_replace_row(netsnmp_tdata *table, netsnmp_tdata_row *origrow, netsnmp_tdata_row *newrow){ netsnmp_tdata_remove_row(table, origrow); netsnmp_tdata_add_row(table, newrow);}/** * removes a row from the given table and returns it (no free's called) * * returns the row pointer itself on successful removing. * or NULL on failure (bad arguments) */netsnmp_tdata_row *netsnmp_tdata_remove_row(netsnmp_tdata *table, netsnmp_tdata_row *row){ if (!row || !table) return NULL; CONTAINER_REMOVE( table->container, row ); return row;}/** * removes and frees a row of the given table and * returns the table-specific entry data * * returns the void * pointer on successful deletion. * or NULL on failure (bad arguments) */void *netsnmp_tdata_remove_and_delete_row(netsnmp_tdata *table, netsnmp_tdata_row *row){ if (!row || !table) return NULL; /* * remove it from the list */ netsnmp_tdata_remove_row(table, row); return netsnmp_tdata_delete_row(row);}/* ================================== * * TData API: MIB maintenance * * ================================== */Netsnmp_Node_Handler _netsnmp_tdata_helper_handler;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -