📄 ip.c
字号:
/* * IP MIB group implementation - ip.c * */#include <config.h>#if defined(IFNET_NEEDS_KERNEL) && !defined(_KERNEL)#define _KERNEL 1#define _I_DEFINED_KERNEL#endif#include <sys/types.h>#if HAVE_SYS_PARAM_H#include <sys/param.h>#endif#include <sys/socket.h>#if HAVE_STRING_H#include <string.h>#endif#if HAVE_STDLIB_H#include <stdlib.h>#endif#if HAVE_NETINET_IN_H#include <netinet/in.h>#endif#if HAVE_SYS_SYSCTL_H#ifdef _I_DEFINED_KERNEL#undef _KERNEL#endif#include <sys/sysctl.h>#ifdef _I_DEFINED_KERNEL#define _KERNEL 1#endif#endif#if HAVE_SYS_SYSMP_H#include <sys/sysmp.h>#endif#if HAVE_SYS_TCPIPSTATS_H#include <sys/tcpipstats.h>#endif#include <net/if.h>#if HAVE_NET_IF_VAR_H#include <net/if_var.h>#endif#ifdef _I_DEFINED_KERNEL#undef _KERNEL#endif#if HAVE_NETINET_IN_SYSTM_H#include <netinet/in_systm.h>#endif#if HAVE_SYS_HASHING_H#include <sys/hashing.h>#endif#if HAVE_NETINET_IN_VAR_H#include <netinet/in_var.h>#endif#include <netinet/ip.h>#if HAVE_NETINET_IP_VAR_H#include <netinet/ip_var.h>#endif#if HAVE_INET_MIB2_H#include <inet/mib2.h>#endif#if HAVE_SYS_STREAM_H#include <sys/stream.h>#endif#include <net/route.h>#if HAVE_SYSLOG_H#include <syslog.h>#endif#if HAVE_DMALLOC_H#include <dmalloc.h>#endif#include "mibincl.h"#ifdef solaris2#include "kernel_sunos5.h"#else#include "kernel.h"#endif#include "system.h"#include "asn1.h"#include "snmp_debug.h"#include "auto_nlist.h"#ifdef MIB_IPCOUNTER_SYMBOL#include <sys/mib.h>#include <netinet/mib_kern.h>#endif /* MIB_IPCOUNTER_SYMBOL *//* #include "../common_header.h" */#include "ip.h"#include "interfaces.h"#include "sysORTable.h" /********************* * * Kernel & interface information, * and internal forward declarations * *********************/#ifdef linuxstatic void linux_read_ip_stat (struct ip_mib *);#endif /********************* * * 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 variable4 ip_variables[] = { {IPFORWARDING, ASN_INTEGER, RONLY, var_ip, 1, {1 }}, {IPDEFAULTTTL, ASN_INTEGER, RONLY, var_ip, 1, {2 }},#ifndef sunV3 {IPINRECEIVES, ASN_COUNTER, RONLY, var_ip, 1, {3 }},#endif {IPINHDRERRORS, ASN_COUNTER, RONLY, var_ip, 1, {4 }},#ifndef sunV3 {IPINADDRERRORS, ASN_COUNTER, RONLY, var_ip, 1, {5 }}, {IPFORWDATAGRAMS, ASN_COUNTER, RONLY, var_ip, 1, {6 }},#endif {IPINUNKNOWNPROTOS, ASN_COUNTER, RONLY, var_ip, 1, {7 }},#ifndef sunV3 {IPINDISCARDS, ASN_COUNTER, RONLY, var_ip, 1, {8 }}, {IPINDELIVERS, ASN_COUNTER, RONLY, var_ip, 1, {9 }},#endif {IPOUTREQUESTS, ASN_COUNTER, RONLY, var_ip, 1, {10 }}, {IPOUTDISCARDS, ASN_COUNTER, RONLY, var_ip, 1, {11 }}, {IPOUTNOROUTES, ASN_COUNTER, RONLY, var_ip, 1, {12 }}, {IPREASMTIMEOUT, ASN_INTEGER, RONLY, var_ip, 1, {13 }},#ifndef sunV3 {IPREASMREQDS, ASN_COUNTER, RONLY, var_ip, 1, {14 }}, {IPREASMOKS, ASN_COUNTER, RONLY, var_ip, 1, {15 }}, {IPREASMFAILS, ASN_COUNTER, RONLY, var_ip, 1, {16 }},#endif {IPFRAGOKS, ASN_COUNTER, RONLY, var_ip, 1, {17 }}, {IPFRAGFAILS, ASN_COUNTER, RONLY, var_ip, 1, {18 }}, {IPFRAGCREATES, ASN_COUNTER, RONLY, var_ip, 1, {19 }}, {IPADADDR, ASN_IPADDRESS, RONLY, var_ipAddrEntry, 3, {20, 1, 1}}, {IPADIFINDEX, ASN_INTEGER, RONLY, var_ipAddrEntry, 3, {20, 1, 2}},#ifndef sunV3 {IPADNETMASK, ASN_IPADDRESS, RONLY, var_ipAddrEntry, 3, {20, 1, 3}},#endif {IPADBCASTADDR, ASN_INTEGER, RONLY, var_ipAddrEntry, 3, {20, 1, 4}}, {IPADREASMMAX, ASN_INTEGER, RONLY, var_ipAddrEntry, 3, {20, 1, 5}}, {IPROUTEDEST, ASN_IPADDRESS, RONLY, var_ipRouteEntry, 3, {21, 1, 1}}, {IPROUTEIFINDEX, ASN_INTEGER, RONLY, var_ipRouteEntry, 3, {21, 1, 2}}, {IPROUTEMETRIC1, ASN_INTEGER, RONLY, var_ipRouteEntry, 3, {21, 1, 3}}, {IPROUTEMETRIC2, ASN_INTEGER, RONLY, var_ipRouteEntry, 3, {21, 1, 4}}, {IPROUTEMETRIC3, ASN_INTEGER, RONLY, var_ipRouteEntry, 3, {21, 1, 5}}, {IPROUTEMETRIC4, ASN_INTEGER, RONLY, var_ipRouteEntry, 3, {21, 1, 6}}, {IPROUTENEXTHOP, ASN_IPADDRESS, RONLY, var_ipRouteEntry, 3, {21, 1, 7}}, {IPROUTETYPE, ASN_INTEGER, RONLY, var_ipRouteEntry, 3, {21, 1, 8}}, {IPROUTEPROTO, ASN_INTEGER, RONLY, var_ipRouteEntry, 3, {21, 1, 9}}, {IPROUTEAGE, ASN_INTEGER, RONLY, var_ipRouteEntry, 3, {21, 1, 10}}, {IPROUTEMASK, ASN_IPADDRESS, RONLY, var_ipRouteEntry, 3, {21, 1, 11}}, {IPROUTEMETRIC5, ASN_INTEGER, RONLY, var_ipRouteEntry, 3, {21, 1, 12}}, {IPROUTEINFO, ASN_OBJECT_ID, RONLY, var_ipRouteEntry, 3, {21, 1, 13}}, {IPMEDIAIFINDEX, ASN_INTEGER, RONLY, var_atEntry, 3, {22, 1, 1}}, {IPMEDIAPHYSADDRESS, ASN_OCTET_STR, RONLY, var_atEntry, 3, {22, 1, 2}}, {IPMEDIANETADDRESS, ASN_IPADDRESS, RONLY, var_atEntry, 3, {22, 1, 3}}, {IPMEDIATYPE, ASN_INTEGER, RONLY, var_atEntry, 3, {22, 1, 4}}, {IPROUTEDISCARDS, ASN_COUNTER, RONLY, var_ip, 1, {23 }}};/* 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_variables_oid[] = { SNMP_OID_MIB2,4 };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 */void init_ip(void){ /* register ourselves with the agent to handle our mib tree */ REGISTER_MIB("mibII/ip", ip_variables, variable4, ip_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 */#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}/* header_ip(... Arguments: vp IN - pointer to variable entry that points here name IN/OUT - IN/name requested, OUT/name found length IN/OUT - length of IN/OUT oid's exact IN - TRUE if an exact match was requested var_len OUT - length of variable or 0 if function returned write_method */static intheader_ip(struct variable *vp, oid *name, size_t *length, int exact, size_t *var_len, WriteMethod **write_method){#define IP_NAME_LENGTH 8 oid newname[MAX_OID_LEN]; int result; DEBUGMSGTL(("mibII/ip", "var_ip: ")); DEBUGMSGOID(("mibII/ip", name, *length)); DEBUGMSG(("mibII/ip"," %d\n", exact)); memcpy( (char *)newname,(char *)vp->name, (int)vp->namelen * sizeof(oid)); newname[IP_NAME_LENGTH] = 0; result = snmp_oid_compare(name, *length, newname, (int)vp->namelen + 1); if ((exact && (result != 0)) || (!exact && (result >= 0))) return(MATCH_FAILED); memcpy( (char *)name,(char *)newname, ((int)vp->namelen + 1) * sizeof(oid)); *length = vp->namelen + 1; *write_method = 0; *var_len = sizeof(long); /* default to 'long' results */ return(MATCH_SUCCEEDED);} /********************* * * System specific implementation functions * *********************/#if !defined(CAN_USE_SYSCTL) || !defined(IPCTL_STATS)#ifndef solaris2#ifndef linux#ifndef HAVE_SYS_TCPIPSTATS_Hu_char *var_ip(struct variable *vp, oid *name, size_t *length, int exact, size_t *var_len, WriteMethod **write_method){ static struct ipstat ipstat;#ifdef MIB_IPCOUNTER_SYMBOL static counter MIB_ipcounter[MIB_ipMAXCTR+1];#endif int i; if (header_ip(vp, name, length, exact, var_len, write_method) == MATCH_FAILED ) return NULL; /* * Get the IP statistics from the kernel... */#ifdef IPSTAT_SYMBOL auto_nlist(IPSTAT_SYMBOL, (char *)&ipstat, sizeof (ipstat));#endif#ifdef MIB_IPCOUNTER_SYMBOL auto_nlist(MIB_IPCOUNTER_SYMBOL, (char *)&MIB_ipcounter, (MIB_ipMAXCTR+1)*sizeof (counter));#endif long_return = 0; switch (vp->magic){ case IPFORWARDING:#ifndef sparc auto_nlist(IP_FORWARDING_SYMBOL,(char *) &i, sizeof(i)); if (i) { long_return = 1; /* GATEWAY */ } else { long_return = 2; /* HOST */ }#else /* sparc */#if NO_DUMMY_VALUES return NULL;#endif#endif /* sparc */ return (u_char *) &long_return; case IPDEFAULTTTL: /* * Allow for a kernel w/o TCP. */ if (!auto_nlist(TCP_TTL_SYMBOL, (char *) &long_return, sizeof(long_return)))#if NO_DUMMY_VALUES return NULL;#else long_return = 60; /* XXX */#endif#ifdef hpux long_return = (u_char)long_return;#endif return (u_char *) &long_return; case IPINRECEIVES: long_return = ipstat.ips_total; return (u_char *) &long_return; case IPINHDRERRORS: long_return = ipstat.ips_badsum + ipstat.ips_tooshort + ipstat.ips_toosmall + ipstat.ips_badhlen + ipstat.ips_badlen; return (u_char *) &long_return; case IPINADDRERRORS: long_return = ipstat.ips_cantforward; return (u_char *) &long_return; case IPFORWDATAGRAMS: long_return = ipstat.ips_forward; return (u_char *) &long_return; case IPINUNKNOWNPROTOS:#ifdef MIB_IPCOUNTER_SYMBOL long_return = MIB_ipcounter[7];#else#if NO_DUMMY_VALUES return NULL;#endif#endif return (u_char *) &long_return; case IPINDISCARDS:#ifdef MIB_IPCOUNTER_SYMBOL long_return = MIB_ipcounter[8];#else#if NO_DUMMY_VALUES return NULL;#endif#endif return (u_char *) &long_return; case IPINDELIVERS: long_return = ipstat.ips_total - (ipstat.ips_badsum + ipstat.ips_tooshort + ipstat.ips_toosmall + ipstat.ips_badhlen + ipstat.ips_badlen); return (u_char *) &long_return; case IPOUTREQUESTS:#ifdef MIB_IPCOUNTER_SYMBOL long_return = MIB_ipcounter[10];#else#if NO_DUMMY_VALUES return NULL;#endif#endif return (u_char *) &long_return; case IPOUTDISCARDS:#ifdef MIB_IPCOUNTER_SYMBOL long_return = MIB_ipcounter[11];#else#if NO_DUMMY_VALUES return NULL;#endif#endif return (u_char *) &long_return; case IPOUTNOROUTES: long_return = ipstat.ips_cantforward; return (u_char *) &long_return; case IPREASMTIMEOUT: long_return = IPFRAGTTL; return (u_char *) &long_return; case IPREASMREQDS: long_return = ipstat.ips_fragments; return (u_char *) &long_return; case IPREASMOKS:#ifdef MIB_IPCOUNTER_SYMBOL long_return = MIB_ipcounter[15];#else#if NO_DUMMY_VALUES return NULL;#endif long_return = ipstat.ips_fragments; /* XXX */ /* * NB: This is the count of fragments received, rather than * "the number of IP datagrams successfully reassembled" */#endif return (u_char *) &long_return; case IPREASMFAILS: long_return = ipstat.ips_fragdropped + ipstat.ips_fragtimeout; return (u_char *) &long_return; case IPFRAGOKS:#ifdef MIB_IPCOUNTER_SYMBOL long_return = MIB_ipcounter[17];#else#if NO_DUMMY_VALUES return NULL;#endif#endif return (u_char *) &long_return; case IPFRAGFAILS:#ifdef MIB_IPCOUNTER_SYMBOL long_return = MIB_ipcounter[18];#else#if NO_DUMMY_VALUES return NULL;#endif#endif return (u_char *) &long_return; case IPFRAGCREATES:#ifdef MIB_IPCOUNTER_SYMBOL long_return = MIB_ipcounter[19];#else#if NO_DUMMY_VALUES return NULL;#endif#endif return (u_char *) &long_return; case IPROUTEDISCARDS:#if NO_DUMMY_VALUES return NULL;#endif return (u_char *) &long_return; default: DEBUGMSGTL(("snmpd", "unknown sub-id %d in var_ip\n", vp->magic)); } return NULL;}#else /* HAVE_SYS_TCPIPSTATS_H */u_char *var_ip(struct variable *vp, oid *name, size_t *length, int exact, size_t *var_len, WriteMethod **write_method){ static struct kna tcpipstats; int i; if (header_ip(vp, name, length, exact, var_len, write_method) == MATCH_FAILED ) return NULL; /* * Get the IP statistics from the kernel... */ if (sysmp (MP_SAGET, MPSA_TCPIPSTATS, &tcpipstats, sizeof tcpipstats) == -1) { snmp_log_perror ("sysmp(MP_SAGET)(MPSA_TCPIPSTATS)"); }#define ipstat tcpipstats.ipstat
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -