📄 ip.c
字号:
/* * IP MIB group implementation - ip.c * */#include <net-snmp/net-snmp-config.h>#include "mibII_common.h"#if HAVE_SYS_HASHING_H#include <sys/hashing.h>#endif#if HAVE_NETINET_IN_VAR_H#include <netinet/in_var.h>#endif#if HAVE_SYSLOG_H#include <syslog.h>#endif#include <net-snmp/net-snmp-includes.h>#include <net-snmp/agent/net-snmp-agent-includes.h>#include <net-snmp/agent/auto_nlist.h>#include "util_funcs.h"#include "ip.h"#include "ipAddr.h"#include "interfaces.h"#include "sysORTable.h"#ifndef MIB_STATS_CACHE_TIMEOUT#define MIB_STATS_CACHE_TIMEOUT 5#endif#ifndef IP_STATS_CACHE_TIMEOUT#define IP_STATS_CACHE_TIMEOUT MIB_STATS_CACHE_TIMEOUT#endif#if defined(HAVE_LIBPERFSTAT_H) && (defined(aix4) || defined(aix5)) && !defined(FIRST_PROTOCOL)#include <libperfstat.h>#ifdef FIRST_PROTOCOLperfstat_protocol_t ps_proto;perfstat_id_t ps_name;#define _USE_PERFSTAT_PROTOCOL 1#endif#endif /********************* * * Kernel & interface information, * and internal forward declarations * *********************/ /********************* * * Initialisation & common implementation functions * *********************/extern void init_routes(void);/* * define the structure we're going to ask the agent to register our * information at */struct variable1 ipaddr_variables[] = { {IPADADDR, ASN_IPADDRESS, RONLY, var_ipAddrEntry, 1, {1}}, {IPADIFINDEX, ASN_INTEGER, RONLY, var_ipAddrEntry, 1, {2}},#ifndef sunV3 {IPADNETMASK, ASN_IPADDRESS, RONLY, var_ipAddrEntry, 1, {3}},#endif {IPADBCASTADDR, ASN_INTEGER, RONLY, var_ipAddrEntry, 1, {4}}, {IPADREASMMAX, ASN_INTEGER, RONLY, var_ipAddrEntry, 1, {5}}};struct variable1 iproute_variables[] = { {IPROUTEDEST, ASN_IPADDRESS, RWRITE, var_ipRouteEntry, 1, {1}}, {IPROUTEIFINDEX, ASN_INTEGER, RWRITE, var_ipRouteEntry, 1, {2}}, {IPROUTEMETRIC1, ASN_INTEGER, RWRITE, var_ipRouteEntry, 1, {3}}, {IPROUTEMETRIC2, ASN_INTEGER, RWRITE, var_ipRouteEntry, 1, {4}}, {IPROUTEMETRIC3, ASN_INTEGER, RWRITE, var_ipRouteEntry, 1, {5}}, {IPROUTEMETRIC4, ASN_INTEGER, RWRITE, var_ipRouteEntry, 1, {6}}, {IPROUTENEXTHOP, ASN_IPADDRESS, RWRITE, var_ipRouteEntry, 1, {7}}, {IPROUTETYPE, ASN_INTEGER, RWRITE, var_ipRouteEntry, 1, {8}}, {IPROUTEPROTO, ASN_INTEGER, RONLY, var_ipRouteEntry, 1, {9}}, {IPROUTEAGE, ASN_INTEGER, RWRITE, var_ipRouteEntry, 1, {10}}, {IPROUTEMASK, ASN_IPADDRESS, RWRITE, var_ipRouteEntry, 1, {11}}, {IPROUTEMETRIC5, ASN_INTEGER, RWRITE, var_ipRouteEntry, 1, {12}}, {IPROUTEINFO, ASN_OBJECT_ID, RONLY, var_ipRouteEntry, 1, {13}}};struct variable1 ipmedia_variables[] = {#ifdef USING_MIBII_AT_MODULE#if defined (WIN32) || defined (cygwin) {IPMEDIAIFINDEX, ASN_INTEGER, RWRITE, var_atEntry, 1, {1}}, {IPMEDIAPHYSADDRESS, ASN_OCTET_STR, RWRITE, var_atEntry, 1, {2}}, {IPMEDIANETADDRESS, ASN_IPADDRESS, RWRITE, var_atEntry, 1, {3}}, {IPMEDIATYPE, ASN_INTEGER, RWRITE, var_atEntry, 1, {4}}#else {IPMEDIAIFINDEX, ASN_INTEGER, RONLY, var_atEntry, 1, {1}}, {IPMEDIAPHYSADDRESS, ASN_OCTET_STR, RONLY, var_atEntry, 1, {2}}, {IPMEDIANETADDRESS, ASN_IPADDRESS, RONLY, var_atEntry, 1, {3}}, {IPMEDIATYPE, ASN_INTEGER, RONLY, var_atEntry, 1, {4}}#endif#endif};/* * Define the OID pointer to the top of the mib tree that we're * registering underneath, and the OID of the MIB module */oid ip_oid[] = { SNMP_OID_MIB2, 4 };oid ipaddr_variables_oid[] = { SNMP_OID_MIB2, 4, 20, 1 };oid iproute_variables_oid[] = { SNMP_OID_MIB2, 4, 21, 1 };oid ipmedia_variables_oid[] = { SNMP_OID_MIB2, 4, 22, 1 };oid ip_module_oid[] = { SNMP_OID_MIB2, 4 };oid ip_module_oid_len = sizeof(ip_module_oid) / sizeof(oid);int ip_module_count = 0; /* Need to liaise with icmp.c */voidinit_ip(void){ netsnmp_handler_registration *reginfo; /* * register ourselves with the agent as a group of scalars... */ DEBUGMSGTL(("mibII/ip", "Initialising IP group\n")); reginfo = netsnmp_create_handler_registration("ip", ip_handler, ip_oid, OID_LENGTH(ip_oid), HANDLER_CAN_RONLY); netsnmp_register_scalar_group(reginfo, IPFORWARDING, IPROUTEDISCARDS); /* * .... with a local cache * (except for HP-UX 11, which extracts objects individually) */#ifndef hpux11 netsnmp_inject_handler( reginfo, netsnmp_get_cache_handler(IP_STATS_CACHE_TIMEOUT, ip_load, ip_free, ip_oid, OID_LENGTH(ip_oid)));#endif /* * register (using the old-style API) to handle the IP tables */ REGISTER_MIB("mibII/ipaddr", ipaddr_variables, variable1, ipaddr_variables_oid); REGISTER_MIB("mibII/iproute", iproute_variables, variable1, iproute_variables_oid); REGISTER_MIB("mibII/ipmedia", ipmedia_variables, variable1, ipmedia_variables_oid); if (++ip_module_count == 2) REGISTER_SYSOR_ENTRY(ip_module_oid, "The MIB module for managing IP and ICMP implementations"); /* * for speed optimization, we call this now to do the lookup */#ifndef _USE_PERFSTAT_PROTOCOL#ifdef IPSTAT_SYMBOL auto_nlist(IPSTAT_SYMBOL, 0, 0);#endif#ifdef IP_FORWARDING_SYMBOL auto_nlist(IP_FORWARDING_SYMBOL, 0, 0);#endif#ifdef TCP_TTL_SYMBOL auto_nlist(TCP_TTL_SYMBOL, 0, 0);#endif#ifdef MIB_IPCOUNTER_SYMBOL auto_nlist(MIB_IPCOUNTER_SYMBOL, 0, 0);#endif#ifdef solaris2 init_kernel_sunos5();#endif#endif} /********************* * * System specific data formats * *********************/#ifdef hpux11#define IP_STAT_STRUCTURE int#endif#ifdef linux#define IP_STAT_STRUCTURE struct ip_mib#define USES_SNMP_DESIGNED_IPSTAT#endif#ifdef solaris2#define IP_STAT_STRUCTURE mib2_ip_t#define USES_SNMP_DESIGNED_IPSTAT#endif#if defined (WIN32) || defined (cygwin)#include <iphlpapi.h>#define IP_STAT_STRUCTURE MIB_IPSTATSlong ipForwarding;long oldipForwarding;long ipTTL, oldipTTL;#endif /* WIN32 cygwin */#ifdef HAVE_SYS_TCPIPSTATS_H#define IP_STAT_STRUCTURE struct kna#define USES_TRADITIONAL_IPSTAT#endif#if !defined(IP_STAT_STRUCTURE)#define IP_STAT_STRUCTURE struct ipstat#define USES_TRADITIONAL_IPSTAT#endifIP_STAT_STRUCTURE ipstat; /********************* * * System independent handler * (mostly) * *********************/intip_handler(netsnmp_mib_handler *handler, netsnmp_handler_registration *reginfo, netsnmp_agent_request_info *reqinfo, netsnmp_request_info *requests){ netsnmp_request_info *request; netsnmp_variable_list *requestvb; long ret_value; oid subid; int type = ASN_COUNTER; /* * The cached data should already have been loaded by the * cache handler, higher up the handler chain. * But just to be safe, check this and load it manually if necessary */#ifdef _USE_PERFSTAT_PROTOCOL ip_load(NULL, NULL);#elif !defined(hpux11) if (!netsnmp_cache_is_valid(reqinfo, reginfo->handlerName)) { netsnmp_assert(!"cache == valid"); /* always false */ ip_load( NULL, NULL ); /* XXX - check for failure */ }#endif /* * * */ DEBUGMSGTL(("mibII/ip", "Handler - mode %s\n", se_find_label_in_slist("agent_mode", reqinfo->mode))); switch (reqinfo->mode) { case MODE_GET: for (request=requests; request; request=request->next) { requestvb = request->requestvb; subid = requestvb->name[OID_LENGTH(ip_oid)]; /* XXX */ DEBUGMSGTL(( "mibII/ip", "oid: ")); DEBUGMSGOID(("mibII/ip", requestvb->name, requestvb->name_length)); DEBUGMSG(( "mibII/ip", "\n")); switch (subid) {#ifdef USES_SNMP_DESIGNED_IPSTAT case IPFORWARDING: ret_value = ipstat.ipForwarding; type = ASN_INTEGER; break; case IPDEFAULTTTL: ret_value = ipstat.ipDefaultTTL; type = ASN_INTEGER; break; case IPINRECEIVES: ret_value = ipstat.ipInReceives; break; case IPINHDRERRORS: ret_value = ipstat.ipInHdrErrors; break; case IPINADDRERRORS: ret_value = ipstat.ipInAddrErrors; break; case IPFORWDATAGRAMS: ret_value = ipstat.ipForwDatagrams; break; case IPINUNKNOWNPROTOS: ret_value = ipstat.ipInUnknownProtos; break; case IPINDISCARDS: ret_value = ipstat.ipInDiscards; break; case IPINDELIVERS: ret_value = ipstat.ipInDelivers; break; case IPOUTREQUESTS: ret_value = ipstat.ipOutRequests; break; case IPOUTDISCARDS: ret_value = ipstat.ipOutDiscards; break; case IPOUTNOROUTES: ret_value = ipstat.ipOutNoRoutes; break; case IPREASMTIMEOUT: ret_value = ipstat.ipReasmTimeout; type = ASN_INTEGER; break; case IPREASMREQDS: ret_value = ipstat.ipReasmReqds; break; case IPREASMOKS: ret_value = ipstat.ipReasmOKs; break; case IPREASMFAILS: ret_value = ipstat.ipReasmFails; break; case IPFRAGOKS: ret_value = ipstat.ipFragOKs; break; case IPFRAGFAILS: ret_value = ipstat.ipFragFails; break; case IPFRAGCREATES: ret_value = ipstat.ipFragCreates; break; case IPROUTEDISCARDS: ret_value = ipstat.ipRoutingDiscards; break;#elif defined(USES_TRADITIONAL_IPSTAT) && !defined(_USE_PERFSTAT_PROTOCOL)#ifdef HAVE_SYS_TCPIPSTATS_H /* * This actually reads statistics for *all* the groups together, * so we need to isolate the IP-specific bits. */#define ipstat ipstat.ipstat#endif case IPFORWARDING: case IPDEFAULTTTL: /* * Query these two individually */ ret_value = ip_load(NULL, (void *)subid); if (ret_value == -1 ) { netsnmp_set_request_error(reqinfo, request, SNMP_NOSUCHOBJECT); continue; } type = ASN_INTEGER; break; case IPINRECEIVES: ret_value = ipstat.ips_total; break; case IPINHDRERRORS: ret_value = ipstat.ips_badsum + ipstat.ips_tooshort + ipstat.ips_toosmall + ipstat.ips_badhlen + ipstat.ips_badlen; break; case IPINADDRERRORS: ret_value = ipstat.ips_cantforward; break; case IPFORWDATAGRAMS: ret_value = ipstat.ips_forward; break; case IPINUNKNOWNPROTOS:#if STRUCT_IPSTAT_HAS_IPS_NOPROTO ret_value = ipstat.ips_noproto; break;#else netsnmp_set_request_error(reqinfo, request, SNMP_NOSUCHOBJECT); continue;#endif case IPINDISCARDS:#if STRUCT_IPSTAT_HAS_IPS_FRAGDROPPED ret_value = ipstat.ips_fragdropped; /* ?? */ break;#else netsnmp_set_request_error(reqinfo, request, SNMP_NOSUCHOBJECT); continue;#endif case IPINDELIVERS:#if STRUCT_IPSTAT_HAS_IPS_DELIVERED ret_value = ipstat.ips_delivered; break;#else netsnmp_set_request_error(reqinfo, request, SNMP_NOSUCHOBJECT); continue;#endif case IPOUTREQUESTS:#if STRUCT_IPSTAT_HAS_IPS_LOCALOUT ret_value = ipstat.ips_localout; break;#else netsnmp_set_request_error(reqinfo, request, SNMP_NOSUCHOBJECT); continue;#endif case IPOUTDISCARDS:#if STRUCT_IPSTAT_HAS_IPS_ODROPPED ret_value = ipstat.ips_odropped; break;#else netsnmp_set_request_error(reqinfo, request, SNMP_NOSUCHOBJECT); continue;#endif case IPOUTNOROUTES: /* * XXX: how to calculate this (counts dropped routes, not packets)? * ipstat.ips_cantforward isn't right, as it counts packets. * ipstat.ips_noroute is also incorrect. */ netsnmp_set_request_error(reqinfo, request, SNMP_NOSUCHOBJECT); continue; case IPREASMTIMEOUT: ret_value = IPFRAGTTL; type = ASN_INTEGER; break; case IPREASMREQDS: ret_value = ipstat.ips_fragments; break; case IPREASMOKS:#if STRUCT_IPSTAT_HAS_IPS_REASSEMBLED ret_value = ipstat.ips_reassembled; break;#else netsnmp_set_request_error(reqinfo, request, SNMP_NOSUCHOBJECT); continue;#endif case IPREASMFAILS: ret_value = ipstat.ips_fragdropped + ipstat.ips_fragtimeout; break; case IPFRAGOKS:#if STRUCT_IPSTAT_HAS_IPS_FRAGMENTED ret_value = ipstat.ips_fragments; break;#else /* XXX */ ret_value = ipstat.ips_fragments - (ipstat.ips_fragdropped + ipstat.ips_fragtimeout); break;#endif case IPFRAGFAILS:#if STRUCT_IPSTAT_HAS_IPS_CANTFRAG ret_value = ipstat.ips_cantfrag; break;#else netsnmp_set_request_error(reqinfo, request, SNMP_NOSUCHOBJECT); continue;#endif case IPFRAGCREATES:#if STRUCT_IPSTAT_HAS_IPS_OFRAGMENTS ret_value = ipstat.ips_ofragments; break;#else netsnmp_set_request_error(reqinfo, request, SNMP_NOSUCHOBJECT); continue;#endif case IPROUTEDISCARDS:#if STRUCT_IPSTAT_HAS_IPS_NOROUTE ret_value = ipstat.ips_noroute; break;#else netsnmp_set_request_error(reqinfo, request, SNMP_NOSUCHOBJECT); continue;#endif#ifdef HAVE_SYS_TCPIPSTATS_H#undef ipstat#endif#elif defined(hpux11) case IPFORWARDING: case IPDEFAULTTTL:
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -