ip.c

来自「包含一下内容: * An extensible agent * An SNM」· C语言 代码 · 共 559 行 · 第 1/2 页

C
559
字号
/* *  IP MIB group implementation - ip.c * */#include <config.h>#if defined(IFNET_NEEDS_KERNEL) && !defined(_KERNEL)#define _KERNEL 1#define _I_DEFINED_KERNEL#endif#if HAVE_SYS_PARAM_H#include <sys/param.h>#endif#if HAVE_SYS_SOCKET_H#include <sys/socket.h>#endif#if HAVE_STRING_H#include <string.h>#else#include <strings.h>#endif#if HAVE_WINSOCK_H#include <winsock.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#if HAVE_NET_IF_H#include <net/if.h>#endif#if HAVE_NET_IF_VAR_H#include <net/if_var.h>#endif#ifdef _I_DEFINED_KERNEL#undef _KERNEL#endif#if HAVE_NETINET_IN_H#include <netinet/in.h>#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#if HAVE_NETINET_IP_H#include <netinet/ip.h>#endif#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#if HAVE_NET_ROUTE_H#include <net/route.h>#endif#if HAVE_SYSLOG_H#include <syslog.h>#endif#include "tools.h"#ifdef solaris2#include "kernel_sunos5.h"#else#include "kernel.h"#endif#ifdef linux#include "kernel_linux.h"#endif#include "mibincl.h"#include "util_funcs.h"#include "system.h"#include "auto_nlist.h"#ifdef MIB_IPCOUNTER_SYMBOL#include <sys/mib.h>#include <netinet/mib_kern.h>#endif /* MIB_IPCOUNTER_SYMBOL */#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#endifmarker_t ip_stats_cache_marker = NULL;	/*********************	 *	 *  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 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}},#ifdef USING_MIBII_AT_MODULE    {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}},#endif    {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}	/*********************	 *	 *  System specific implementation functions	 *	 *********************/#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#ifdef HAVE_SYS_TCPIPSTATS_H#define IP_STAT_STRUCTURE	struct kna#define	USES_TRADITIONAL_IPSTAT#endif#ifdef WIN32#include <iphlpapi.h>#define IP_STAT_STRUCTURE MIB_IPSTATS#endif#if !defined(IP_STAT_STRUCTURE)#define IP_STAT_STRUCTURE	struct ipstat#define	USES_TRADITIONAL_IPSTAT#endiflong read_ip_stat (IP_STAT_STRUCTURE *, int);u_char *var_ip(struct variable *vp,       oid *name,       size_t *length,       int exact,       size_t *var_len,       WriteMethod **write_method){    static IP_STAT_STRUCTURE ipstat;    static long ret_value;    if (header_generic(vp, name, length, exact, var_len, write_method) == MATCH_FAILED )	return NULL;    ret_value = read_ip_stat (&ipstat, vp->magic);    if ( ret_value < 0 )	return NULL;#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    switch (vp->magic){

⌨️ 快捷键说明

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