📄 interface.c
字号:
/* * Interface MIB architecture support * * $Id: interface.c 15768 2007-01-22 16:18:29Z rstory $ */#include <net-snmp/net-snmp-config.h>#include <net-snmp/net-snmp-includes.h>#include "mibII/mibII_common.h"#include "if-mib/ifTable/ifTable_constants.h"#include "if-mib/data_access/interface.h"#include <net-snmp/agent/net-snmp-agent-includes.h>#include <net-snmp/library/snmp_enum.h>#include <net-snmp/data_access/interface.h>/**---------------------------------------------------------------------*//* * local static vars */static netsnmp_conf_if_list *conf_list = NULL;static int need_wrap_check = -1;static int _access_interface_init = 0;/* * local static prototypes */static int _access_interface_entry_compare_name(const void *lhs, const void *rhs);#ifndef NETSNMP_ACCESS_INTERFACE_NOARCHstatic void _access_interface_entry_release(netsnmp_interface_entry * entry, void *unused);#endifstatic void _access_interface_entry_save_name(const char *name, oid index);static void _parse_interface_config(const char *token, char *cptr);static void _free_interface_config(void);/**---------------------------------------------------------------------*//* * external per-architecture functions prototypes * * These shouldn't be called by the general public, so they aren't in * the header file. */#ifndef NETSNMP_ACCESS_INTERFACE_NOARCHextern void netsnmp_arch_interface_init(void);extern intnetsnmp_arch_interface_container_load(netsnmp_container* container, u_int load_flags);extern intnetsnmp_arch_set_admin_status(netsnmp_interface_entry * entry, int ifAdminStatus);extern int netsnmp_arch_interface_index_find(const char*name);#endif/** * initialization */voidinit_interface(void){ snmpd_register_config_handler("interface", _parse_interface_config, _free_interface_config, "name type speed");}voidnetsnmp_access_interface_init(void){ netsnmp_assert(0 == _access_interface_init); /* who is calling twice? */ if (1 == _access_interface_init) return; _access_interface_init = 1;#ifndef NETSNMP_ACCESS_INTERFACE_NOARCH { netsnmp_container * ifcontainer; netsnmp_arch_interface_init(); /* * load once to set up ifIndexes */ ifcontainer = netsnmp_access_interface_container_load(NULL, 0); if(NULL != ifcontainer) netsnmp_access_interface_container_free(ifcontainer, 0); }#endif}/**---------------------------------------------------------------------*//* * container functions *//** * initialize interface container */netsnmp_container *netsnmp_access_interface_container_init(u_int flags){ netsnmp_container *container1; DEBUGMSGTL(("access:interface:container", "init\n")); /* * create the containers. one indexed by ifIndex, the other * indexed by ifName. */ container1 = netsnmp_container_find("access_interface:table_container"); if (NULL == container1) return NULL; container1->container_name = strdup("interface container"); if (flags & NETSNMP_ACCESS_INTERFACE_INIT_ADDL_IDX_BY_NAME) { netsnmp_container *container2 = netsnmp_container_find("access_interface_by_name:access_interface:table_container"); if (NULL == container2) return NULL; container2->container_name = strdup("interface name container"); container2->compare = _access_interface_entry_compare_name; netsnmp_container_add_index(container1, container2); } return container1;}/** * load interface information in specified container * * @param container empty container, or NULL to have one created for you * @param load_flags flags to modify behaviour. Examples: * NETSNMP_ACCESS_INTERFACE_INIT_ADDL_IDX_BY_NAME * * @retval NULL error * @retval !NULL pointer to container */#ifndef NETSNMP_ACCESS_INTERFACE_NOARCHnetsnmp_container*netsnmp_access_interface_container_load(netsnmp_container* container, u_int load_flags){ int rc; DEBUGMSGTL(("access:interface:container", "load\n")); netsnmp_assert(1 == _access_interface_init); if (NULL == container) container = netsnmp_access_interface_container_init(load_flags); if (NULL == container) { snmp_log(LOG_ERR, "no container specified/found for access_interface\n"); return NULL; } rc = netsnmp_arch_interface_container_load(container, load_flags); if (0 != rc) { netsnmp_access_interface_container_free(container, NETSNMP_ACCESS_INTERFACE_FREE_NOFLAGS); container = NULL; } return container;}voidnetsnmp_access_interface_container_free(netsnmp_container *container, u_int free_flags){ DEBUGMSGTL(("access:interface:container", "free\n")); if (NULL == container) { snmp_log(LOG_ERR, "invalid container for netsnmp_access_interface_free\n"); return; } if(! (free_flags & NETSNMP_ACCESS_INTERFACE_FREE_DONT_CLEAR)) { /* * free all items. */ CONTAINER_CLEAR(container, (netsnmp_container_obj_func*)_access_interface_entry_release, NULL); } CONTAINER_FREE(container);}/** * @retval 0 interface not found */oidnetsnmp_access_interface_index_find(const char *name){ DEBUGMSGTL(("access:interface:find", "index\n")); netsnmp_assert(1 == _access_interface_init); return netsnmp_arch_interface_index_find(name);}#endif/**---------------------------------------------------------------------*//* * ifentry functions *//** */netsnmp_interface_entry *netsnmp_access_interface_entry_get_by_index(netsnmp_container *container, oid index){ netsnmp_index tmp; DEBUGMSGTL(("access:interface:entry", "by_index\n")); netsnmp_assert(1 == _access_interface_init); if (NULL == container) { snmp_log(LOG_ERR, "invalid container for netsnmp_access_interface_entry_get_by_index\n"); return NULL; } tmp.len = 1; tmp.oids = &index; return (netsnmp_interface_entry *) CONTAINER_FIND(container, &tmp);}/** */netsnmp_interface_entry *netsnmp_access_interface_entry_get_by_name(netsnmp_container *container, const char *name){ netsnmp_interface_entry tmp; DEBUGMSGTL(("access:interface:entry", "by_name\n")); netsnmp_assert(1 == _access_interface_init); if (NULL == container) { snmp_log(LOG_ERR, "invalid container for netsnmp_access_interface_entry_get_by_name\n"); return NULL; } if (NULL == container->next) { snmp_log(LOG_ERR, "secondary index missing for netsnmp_access_interface_entry_get_by_name\n"); return NULL; } tmp.name = (char *)name; return CONTAINER_FIND(container->next, &tmp);}/** * @retval NULL index not found */const char *netsnmp_access_interface_name_find(oid index){ DEBUGMSGTL(("access:interface:find", "name\n")); netsnmp_assert(1 == _access_interface_init); return se_find_label_in_slist("interfaces", index);}/** */netsnmp_interface_entry *netsnmp_access_interface_entry_create(const char *name, oid if_index){ netsnmp_interface_entry *entry = SNMP_MALLOC_TYPEDEF(netsnmp_interface_entry); DEBUGMSGTL(("access:interface:entry", "create\n")); netsnmp_assert(1 == _access_interface_init); if(NULL == entry) return NULL; if(NULL != name) entry->name = strdup(name); /* * get if index, and save name for reverse lookup */#ifndef NETSNMP_ACCESS_INTERFACE_NOARCH if (0 == if_index) entry->index = netsnmp_access_interface_index_find(name); else#endif entry->index = if_index; _access_interface_entry_save_name(name, entry->index); entry->descr = strdup(name); /* * make some assumptions */ entry->connector_present = 1; entry->oid_index.len = 1; entry->oid_index.oids = (oid *) & entry->index; return entry;}/** */voidnetsnmp_access_interface_entry_free(netsnmp_interface_entry * entry){ DEBUGMSGTL(("access:interface:entry", "free\n")); if (NULL == entry) return; /* * SNMP_FREE not needed, for any of these, * since the whole entry is about to be freed */ if (NULL != entry->old_stats) free(entry->old_stats); if (NULL != entry->name) free(entry->name); if (NULL != entry->descr) free(entry->descr); if (NULL != entry->paddr) free(entry->paddr); free(entry);}/* * Blech - backwards compatible mibII/interfaces style interface * functions, so we don't have to update older modules to use * the new code to get correct ifIndex values. */#if defined( USING_IF_MIB_IFTABLE_IFTABLE_DATA_ACCESS_MODULE ) && \ ! defined( NETSNMP_NO_BACKWARDS_COMPATABILITY )static netsnmp_iterator *it = NULL;static netsnmp_container *c = NULL;static netsnmp_interface_entry *e = NULL;/** * */voidInterface_Scan_Init(void){ /* * ifTable container shouldn't change, so we shouldn' have to * re-fetch it every time. */ if (NULL != c) netsnmp_access_interface_container_free(c, 0); c = netsnmp_access_interface_container_load(NULL, 0); if (NULL != c) { if (NULL != it) ITERATOR_RELEASE(it); it = CONTAINER_ITERATOR(c); } if (NULL != it) e = ITERATOR_FIRST(it);}intInterface_Scan_Next(short *index, char *name, netsnmp_interface_entry **entry, void *dc){
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -