📄 tcp.c
字号:
/* * TCP MIB group implementation - tcp.c * */#include <net-snmp/net-snmp-config.h>#include "mibII_common.h"#if HAVE_STDLIB_H#include <stdlib.h>#endif#if HAVE_UNISTD_H#include <unistd.h>#endif#if HAVE_SYS_PROTOSW_H#include <sys/protosw.h>#endif#if HAVE_ARPA_INET_H#include <arpa/inet.h>#endif#if defined(osf4) || defined(osf5) || defined(aix4) || defined(hpux10)/* * these are undefed to remove a stupid warning on osf compilers * because they get redefined with a slightly different notation of the * same value. -- Wes */#undef TCP_NODELAY#undef TCP_MAXSEG#endif#if HAVE_NETINET_TCP_H#include <netinet/tcp.h>#endif#if HAVE_NETINET_TCPIP_H#include <netinet/tcpip.h>#endif#if HAVE_NETINET_TCP_TIMER_H#include <netinet/tcp_timer.h>#endif#if HAVE_NETINET_TCP_VAR_H#include <netinet/tcp_var.h>#endif#if HAVE_NETINET_TCP_FSM_H#include <netinet/tcp_fsm.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 "tcp.h"#include "tcpTable.h"#include "sysORTable.h"#ifndef MIB_STATS_CACHE_TIMEOUT#define MIB_STATS_CACHE_TIMEOUT 5#endif#ifndef TCP_STATS_CACHE_TIMEOUT#define TCP_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_FIRST_PROTOCOL 1#endif#endif /********************* * * Kernel & interface information, * and internal forward declarations * *********************/ /* * FreeBSD4 *does* need an explicit variable 'hz' * since this appears in a system header file. * But only define it under FreeBSD, since it * breaks other systems (notable AIX) */#ifdef freebsd4int hz = 1000;#endifextern int TCP_Count_Connections( void ); /********************* * * Initialisation & common implementation functions * *********************//* * Define the OID pointer to the top of the mib tree that we're * registering underneath, and the OID for the MIB module */oid tcp_oid[] = { SNMP_OID_MIB2, 6 };oid tcp_module_oid[] = { SNMP_OID_MIB2, 49 };voidinit_tcp(void){ netsnmp_handler_registration *reginfo; /* * register ourselves with the agent as a group of scalars... */ DEBUGMSGTL(("mibII/tcpScalar", "Initialising TCP scalar group\n")); reginfo = netsnmp_create_handler_registration("tcp", tcp_handler, tcp_oid, OID_LENGTH(tcp_oid), HANDLER_CAN_RONLY); netsnmp_register_scalar_group(reginfo, TCPRTOALGORITHM, TCPOUTRSTS); /* * .... with a local cache * (except for HP-UX 11, which extracts objects individually) */#ifndef hpux11 netsnmp_inject_handler( reginfo, netsnmp_get_cache_handler(TCP_STATS_CACHE_TIMEOUT, tcp_load, tcp_free, tcp_oid, OID_LENGTH(tcp_oid)));#endif REGISTER_SYSOR_ENTRY(tcp_module_oid, "The MIB module for managing TCP implementations");#if !defined(_USE_FIRST_PROTOCOL)#ifdef TCPSTAT_SYMBOL auto_nlist(TCPSTAT_SYMBOL, 0, 0);#endif#ifdef TCP_SYMBOL auto_nlist(TCP_SYMBOL, 0, 0);#endif#ifdef freebsd4 hz = sysconf(_SC_CLK_TCK); /* get ticks/s from system */#endif#ifdef solaris2 init_kernel_sunos5();#endif#endif} /********************* * * System specific implementation functions * *********************/#ifdef hpux11#define TCP_STAT_STRUCTURE int#endif#ifdef linux#define TCP_STAT_STRUCTURE struct tcp_mib#define USES_SNMP_DESIGNED_TCPSTAT#undef TCPSTAT_SYMBOL#endif#ifdef solaris2#define TCP_STAT_STRUCTURE mib2_tcp_t#define USES_SNMP_DESIGNED_TCPSTAT#endif#if defined (WIN32) || defined (cygwin)#include <iphlpapi.h>#define TCP_STAT_STRUCTURE MIB_TCPSTATS#endif#ifdef HAVE_SYS_TCPIPSTATS_H#define TCP_STAT_STRUCTURE struct kna#define USES_TRADITIONAL_TCPSTAT#endif#if !defined(TCP_STAT_STRUCTURE)#define TCP_STAT_STRUCTURE struct tcpstat#define USES_TRADITIONAL_TCPSTAT#endifTCP_STAT_STRUCTURE tcpstat; /********************* * * System independent handler (mostly) * *********************/inttcp_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 = -1; 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 */#if defined(_USE_FIRST_PROTOCOL) tcp_load(NULL, NULL);#elif !defined(hpux11) if (!netsnmp_cache_is_valid(reqinfo, reginfo->handlerName)) { netsnmp_assert(!"cache == valid"); /* always false */ tcp_load( NULL, NULL ); /* XXX - check for failure */ }#endif /* * * */ DEBUGMSGTL(("mibII/tcpScalar", "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(tcp_oid)]; /* XXX */ DEBUGMSGTL(( "mibII/tcpScalar", "oid: ")); DEBUGMSGOID(("mibII/tcpScalar", requestvb->name, requestvb->name_length)); DEBUGMSG(( "mibII/tcpScalar", "\n")); switch (subid) {#ifdef USES_SNMP_DESIGNED_TCPSTAT case TCPRTOALGORITHM: ret_value = tcpstat.tcpRtoAlgorithm; type = ASN_INTEGER; break; case TCPRTOMIN: ret_value = tcpstat.tcpRtoMin; type = ASN_INTEGER; break; case TCPRTOMAX: ret_value = tcpstat.tcpRtoMax; type = ASN_INTEGER; break; case TCPMAXCONN: ret_value = tcpstat.tcpMaxConn; type = ASN_INTEGER; break; case TCPACTIVEOPENS: ret_value = tcpstat.tcpActiveOpens; break; case TCPPASSIVEOPENS: ret_value = tcpstat.tcpPassiveOpens; break; case TCPATTEMPTFAILS: ret_value = tcpstat.tcpAttemptFails; break; case TCPESTABRESETS: ret_value = tcpstat.tcpEstabResets; break; case TCPCURRESTAB: ret_value = tcpstat.tcpCurrEstab; type = ASN_GAUGE; break; case TCPINSEGS: ret_value = tcpstat.tcpInSegs; break; case TCPOUTSEGS: ret_value = tcpstat.tcpOutSegs; break; case TCPRETRANSSEGS: ret_value = tcpstat.tcpRetransSegs; break; case TCPINERRS:#ifdef solaris2 ret_value = tcp_load(NULL, (void *)TCPINERRS); if (ret_value == -1) { netsnmp_set_request_error(reqinfo, request, SNMP_NOSUCHOBJECT); continue; } break;#elif defined(linux) if (tcpstat.tcpInErrsValid) { ret_value = tcpstat.tcpInErrs; break; } else { netsnmp_set_request_error(reqinfo, request, SNMP_NOSUCHOBJECT); continue; }#else /* linux */ netsnmp_set_request_error(reqinfo, request, SNMP_NOSUCHOBJECT); continue;#endif /* solaris2 */ case TCPOUTRSTS:#ifdef linux if (tcpstat.tcpOutRstsValid) { ret_value = tcpstat.tcpOutRsts; break; }#endif /* linux */ netsnmp_set_request_error(reqinfo, request, SNMP_NOSUCHOBJECT); continue;#elif defined(USES_TRADITIONAL_TCPSTAT) && !defined(_USE_FIRST_PROTOCOL)#ifdef HAVE_SYS_TCPIPSTATS_H /* * This actually reads statistics for *all* the groups together, * so we need to isolate the TCP-specific bits. */#define tcpstat tcpstat.tcpstat#endif case TCPRTOALGORITHM: /* Assume Van Jacobsen's algorithm */ ret_value = 4; type = ASN_INTEGER; break; case TCPRTOMIN:#ifdef TCPTV_NEEDS_HZ ret_value = TCPTV_MIN;#else ret_value = TCPTV_MIN / PR_SLOWHZ * 1000;#endif type = ASN_INTEGER; break; case TCPRTOMAX:#ifdef TCPTV_NEEDS_HZ ret_value = TCPTV_REXMTMAX;#else ret_value = TCPTV_REXMTMAX / PR_SLOWHZ * 1000;#endif type = ASN_INTEGER; break; case TCPMAXCONN: ret_value = -1; /* Dynamic maximum */ type = ASN_INTEGER; break; case TCPACTIVEOPENS: ret_value = tcpstat.tcps_connattempt; break; case TCPPASSIVEOPENS: ret_value = tcpstat.tcps_accepts; break; /* * NB: tcps_drops is actually the sum of the two MIB * counters tcpAttemptFails and tcpEstabResets. */ case TCPATTEMPTFAILS: ret_value = tcpstat.tcps_conndrops; break; case TCPESTABRESETS: ret_value = tcpstat.tcps_drops; break; case TCPCURRESTAB:#ifdef USING_MIBII_TCPTABLE_MODULE ret_value = TCP_Count_Connections();#else ret_value = 0;#endif type = ASN_GAUGE; break; case TCPINSEGS: ret_value = tcpstat.tcps_rcvtotal; break; case TCPOUTSEGS: /* * RFC 1213 defines this as the number of segments sent * "excluding those containing only retransmitted octets" */ ret_value = tcpstat.tcps_sndtotal - tcpstat.tcps_sndrexmitpack; break; case TCPRETRANSSEGS: ret_value = tcpstat.tcps_sndrexmitpack; break; case TCPINERRS: ret_value = tcpstat.tcps_rcvbadsum + tcpstat.tcps_rcvbadoff#ifdef STRUCT_TCPSTAT_HAS_TCPS_RCVMEMDROP + tcpstat.tcps_rcvmemdrop#endif + tcpstat.tcps_rcvshort; break; case TCPOUTRSTS: ret_value = tcpstat.tcps_sndctrl - tcpstat.tcps_closed; break;#ifdef HAVE_SYS_TCPIPSTATS_H#undef tcpstat#endif#elif defined(hpux11) case TCPRTOALGORITHM: case TCPRTOMIN: case TCPRTOMAX:
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -