📄 tcp.c
字号:
/* * TCP MIB group implementation - tcp.c * */#include <config.h>#include <unistd.h>#if TIME_WITH_SYS_TIME# ifdef WIN32# include <sys/timeb.h># else# include <sys/time.h># endif# include <time.h>#else# if HAVE_SYS_TIME_H# include <sys/time.h># else# include <time.h># endif#endif#include <sys/types.h>#if HAVE_SYS_PARAM_H#include <sys/param.h>#endif#include <sys/socket.h>#if HAVE_SYS_PROTOSW_H#include <sys/protosw.h>#endif#if HAVE_SYS_SYSMP_H#include <sys/sysmp.h>#endif#if defined(IFNET_NEEDS_KERNEL) && !defined(_KERNEL)#define _KERNEL 1#define _I_DEFINED_KERNEL#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_SYS_STREAM_H#include <sys/stream.h>#endif#if HAVE_NET_ROUTE_H#include <net/route.h>#endif#if HAVE_NETINET_IN_H#include <netinet/in.h>#endif#if HAVE_NETINET_IN_SYSTM_H#include <netinet/in_systm.h>#endif#include <netinet/ip.h>#ifdef INET6#include <netinet/ip6.h>#endif#if HAVE_SYS_QUEUE_H#include <sys/queue.h>#endif#if HAVE_NETINET_IP_VAR_H#include <netinet/ip_var.h>#endif#ifdef INET6#include <netinet/ip6_var.h>#endif#if HAVE_SYS_SOCKETVAR_H#include <sys/socketvar.h>#endif#if HAVE_NETINET_IN_PCB_H#include <netinet/in_pcb.h>#endif#if HAVE_INET_MIB2_H#include <inet/mib2.h>#endif#if HAVE_STRING_H#include <string.h>#endif#if HAVE_STDLIB_H#include <stdlib.h>#endif#ifdef solaris2#include "kernel_sunos5.h"#else#include "kernel.h"#endif#include "mibincl.h"#include "../../../snmplib/system.h"#if HAVE_SYS_SYSCTL_H#include <sys/sysctl.h>#endif#if HAVE_ARPA_INET_H#include <arpa/inet.h>#endif#if defined(osf4) || 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#include <netinet/tcp.h>#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#if HAVE_SYS_TCPIPSTATS_H#include <sys/tcpipstats.h>#endif#if HAVE_DMALLOC_H#include <dmalloc.h>#endif#include "auto_nlist.h"#ifdef hpux#include <sys/mib.h>#include <netinet/mib_kern.h>#endif /* hpux *//* #include "../common_header.h" */#include "tcp.h"#include "sysORTable.h" /********************* * * Kernel & interface information, * and internal forward declarations * *********************/#ifdef linuxstatic void linux_read_tcp_stat (struct tcp_mib *);#endif#ifdef freebsd4static unsigned int hz;#endif /********************* * * Initialisation & common implementation functions * *********************/struct variable13 tcp_variables[] = { {TCPRTOALGORITHM, ASN_INTEGER, RONLY, var_tcp, 1, {1}}, {TCPRTOMIN, ASN_INTEGER, RONLY, var_tcp, 1, {2}},#ifndef sunV3 {TCPRTOMAX, ASN_INTEGER, RONLY, var_tcp, 1, {3}},#endif {TCPMAXCONN, ASN_INTEGER, RONLY, var_tcp, 1, {4}},#ifndef sunV3 {TCPACTIVEOPENS, ASN_COUNTER, RONLY, var_tcp, 1, {5}}, {TCPPASSIVEOPENS, ASN_COUNTER, RONLY, var_tcp, 1, {6}}, {TCPATTEMPTFAILS, ASN_COUNTER, RONLY, var_tcp, 1, {7}}, {TCPESTABRESETS, ASN_COUNTER, RONLY, var_tcp, 1, {8}},#endif { TCPCURRESTAB, ASN_GAUGE, RONLY, var_tcp, 1, {9}},#ifndef sunV3 {TCPINSEGS, ASN_COUNTER, RONLY, var_tcp, 1, {10}}, {TCPOUTSEGS, ASN_COUNTER, RONLY, var_tcp, 1, {11} }, {TCPRETRANSSEGS, ASN_COUNTER, RONLY, var_tcp, 1, {12}},#endif {TCPCONNSTATE, ASN_INTEGER, RONLY, var_tcpEntry, 3, {13, 1, 1}}, {TCPCONNLOCALADDRESS, ASN_IPADDRESS, RONLY, var_tcpEntry, 3, {13, 1, 2}}, {TCPCONNLOCALPORT, ASN_INTEGER, RONLY, var_tcpEntry, 3, {13, 1, 3}}, {TCPCONNREMADDRESS, ASN_IPADDRESS, RONLY, var_tcpEntry, 3, {13, 1, 4}}, {TCPCONNREMPORT, ASN_INTEGER, RONLY, var_tcpEntry, 3, {13, 1, 5}}, {TCPINERRS, ASN_COUNTER, RONLY, var_tcp, 1, {14}}, {TCPOUTRSTS, ASN_COUNTER, RONLY, var_tcp, 1, {15}}};/* 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_variables_oid[] = { SNMP_OID_MIB2,6 };oid tcp_module_oid[] = { SNMP_OID_MIB2,49 };void init_tcp(void){ /* register ourselves with the agent to handle our mib tree */ REGISTER_MIB("mibII/tcp", tcp_variables, variable13, tcp_variables_oid); REGISTER_SYSOR_ENTRY( tcp_module_oid, "The MIB module for managing TCP implementations");#ifdef TCPSTAT_SYMBOL auto_nlist( TCPSTAT_SYMBOL,0,0 );#endif#ifdef TCP_SYMBOL auto_nlist( TCP_SYMBOL,0,0 );#endif#if freebsd4 hz=sysconf(_SC_CLK_TCK); /* get ticks/s from system */#endif}/* header_tcp(... 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_tcp(struct variable *vp, oid *name, size_t *length, int exact, size_t *var_len, WriteMethod **write_method){#define TCP_NAME_LENGTH 8 oid newname[MAX_OID_LEN]; int result; DEBUGMSGTL(("mibII/tcp", "var_tcp: ")); DEBUGMSGOID(("mibII/tcp", name, *length)); DEBUGMSG(("mibII/tcp"," %d\n", exact)); memcpy( (char *)newname,(char *)vp->name, vp->namelen * sizeof(oid)); newname[TCP_NAME_LENGTH] = 0; result = snmp_oid_compare(name, *length, newname, vp->namelen + 1); if ((exact && (result != 0)) || (!exact && (result >= 0))) return(MATCH_FAILED); memcpy( (char *)name,(char *)newname, (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 * *********************/#ifndef solaris2#ifdef HAVE_SYS_TCPIPSTATS_Hu_char *var_tcp(struct variable *vp, oid *name, size_t *length, int exact, size_t *var_len, WriteMethod **write_method){ static struct kna tcpipstats;#ifdef TCPTV_NEEDS_HZ /* * I don't know of any such system now, but maybe they'll figure * it out some day. */ int hz = 1000;#endif /* * Allow for a kernel w/o TCP */ if (header_tcp(vp, name, length, exact, var_len, write_method) == MATCH_FAILED ) return NULL; /* * Get the TCP statistics from the kernel... */ if (sysmp (MP_SAGET, MPSA_TCPIPSTATS, &tcpipstats, sizeof tcpipstats) == -1) { snmp_log_perror ("sysmp(MP_SAGET)(MPSA_TCPIPSTATS)"); }#define tcpstat tcpipstats.tcpstat switch (vp->magic){ case TCPRTOALGORITHM:#ifndef linux long_return = 4; /* Van Jacobsen's algorithm *//* XXX */#else if (! tcpstat.TcpRtoAlgorithm) { /* 0 is illegal: assume `other' algorithm: */ long_return = 1; return (u_char *) &long_return; } return (u_char *) &tcpstat.TcpRtoAlgorithm;#endif return (u_char *) &long_return; case TCPRTOMIN:#ifndef linux#ifdef TCPTV_NEEDS_HZ long_return = TCPTV_MIN;#else long_return = TCPTV_MIN / PR_SLOWHZ * 1000;#endif return (u_char *) &long_return;#else return (u_char *) &tcpstat.TcpRtoMin;#endif case TCPRTOMAX:#ifndef linux#ifdef TCPTV_NEEDS_HZ long_return = TCPTV_REXMTMAX;#else long_return = TCPTV_REXMTMAX / PR_SLOWHZ * 1000;#endif return (u_char *) &long_return;#else return (u_char *) &tcpstat.TcpRtoMax;#endif case TCPMAXCONN:#ifndef linux#if NO_DUMMY_VALUES return NULL;#endif long_return = -1; return (u_char *) &long_return;#else return (u_char *) &tcpstat.TcpMaxConn;#endif case TCPACTIVEOPENS: long_return = tcpstat.tcps_connattempt; return (u_char *) &long_return; case TCPPASSIVEOPENS: long_return = tcpstat.tcps_accepts; return (u_char *) &long_return; case TCPATTEMPTFAILS:#ifdef hpux long_return = MIB_tcpcounter[7];#else#if NO_DUMMY_VALUES return NULL;#endif long_return = tcpstat.tcps_conndrops; /* XXX */#endif return (u_char *) &long_return; case TCPESTABRESETS:#ifdef hpux long_return = MIB_tcpcounter[8];#else#if NO_DUMMY_VALUES return NULL;#endif long_return = tcpstat.tcps_drops; /* XXX */#endif return (u_char *) &long_return; /* * NB: tcps_drops is actually the sum of the two MIB * counters tcpAttemptFails and tcpEstabResets. */ case TCPCURRESTAB:#ifndef linux long_return = TCP_Count_Connections(); return (u_char *) &long_return;#else return (u_char *) &tcpstat.TcpCurrEstab;#endif case TCPINSEGS: long_return = tcpstat.tcps_rcvtotal; return (u_char *) &long_return; case TCPOUTSEGS: long_return = tcpstat.tcps_sndtotal - tcpstat.tcps_sndrexmitpack; /* * RFC 1213 defines this as the number of segments sent * "excluding those containing only retransmitted octets" */ return (u_char *) &long_return; case TCPRETRANSSEGS: long_return = tcpstat.tcps_sndrexmitpack; return (u_char *) &long_return;#ifndef linux case TCPINERRS: long_return = tcpstat.tcps_rcvbadsum + tcpstat.tcps_rcvbadoff #ifdef STRUCT_TCPSTAT_HAS_TCPS_RCVMEMDROP + tcpstat.tcps_rcvmemdrop#endif + tcpstat.tcps_rcvshort; return (u_char *) &long_return; case TCPOUTRSTS: long_return = tcpstat.tcps_sndctrl - tcpstat.tcps_closed; return (u_char *) &long_return;#endif /* linux */ default: DEBUGMSGTL(("snmpd", "unknown sub-id %d in var_tcp\n", vp->magic)); } return NULL;}#else /* not HAVE_SYS_TCPIPSTATS_H */u_char *var_tcp(struct variable *vp, oid *name, size_t *length, int exact, size_t *var_len, WriteMethod **write_method){ static struct tcpstat tcpstat;#ifdef hpux static counter MIB_tcpcounter[MIB_tcpMAXCTR+1];#endif#ifdef TCPTV_NEEDS_HZ int hz = 1000;#endif /* * Allow for a kernel w/o TCP */#ifndef linux#ifndef TCPSTAT_SYMBOL return NULL;#else if (auto_nlist_value(TCPSTAT_SYMBOL) == -1) return(NULL);#endif#endif if (header_tcp(vp, name, length, exact, var_len, write_method) == MATCH_FAILED ) return NULL; /* * Get the TCP statistics from the kernel... */#if !defined(CAN_USE_SYSCTL) || !defined(TCPCTL_STATS)#ifndef linux auto_nlist(TCPSTAT_SYMBOL, (char *)&tcpstat, sizeof (tcpstat));#ifdef MIB_TCPCOUNTER_SYMBOL auto_nlist(MIB_TCPCOUNTER_SYMBOL, (char *)&MIB_tcpcounter, (MIB_tcpMAXCTR+1)*sizeof (counter));#endif#else /* linux */ linux_read_tcp_stat (&tcpstat);#endif /* linux */#else /* can use sysctl and net.inet.tcp.stats exists */ { int sname[] = { CTL_NET, PF_INET, IPPROTO_TCP, TCPCTL_STATS }; size_t len; len = sizeof tcpstat; if (sysctl(sname, 4, &tcpstat, &len, 0, 0) < 0) return NULL; }#endif switch (vp->magic) { case TCPRTOALGORITHM:#ifndef linux long_return = 4; /* Van Jacobsen's algorithm *//* XXX */#else if (! tcpstat.TcpRtoAlgorithm) { /* 0 is illegal: assume `other' algorithm: */ long_return = 1; return (u_char *) &long_return; } return (u_char *) &tcpstat.TcpRtoAlgorithm;#endif return (u_char *) &long_return; case TCPRTOMIN:#ifndef linux#ifdef TCPTV_NEEDS_HZ long_return = TCPTV_MIN;#else long_return = TCPTV_MIN / PR_SLOWHZ * 1000;#endif return (u_char *) &long_return;#else return (u_char *) &tcpstat.TcpRtoMin;#endif case TCPRTOMAX:#ifndef linux#ifdef TCPTV_NEEDS_HZ long_return = TCPTV_REXMTMAX;#else long_return = TCPTV_REXMTMAX / PR_SLOWHZ * 1000;#endif return (u_char *) &long_return;#else return (u_char *) &tcpstat.TcpRtoMax;#endif case TCPMAXCONN:#ifndef linux#if NO_DUMMY_VALUES return NULL;#endif long_return = -1; return (u_char *) &long_return;#else return (u_char *) &tcpstat.TcpMaxConn;#endif case TCPACTIVEOPENS: long_return = tcpstat.tcps_connattempt; return (u_char *) &long_return; case TCPPASSIVEOPENS: long_return = tcpstat.tcps_accepts; return (u_char *) &long_return; case TCPATTEMPTFAILS:#ifdef hpux long_return = MIB_tcpcounter[7];#else#if NO_DUMMY_VALUES return NULL;#endif long_return = tcpstat.tcps_conndrops; /* XXX */#endif return (u_char *) &long_return; case TCPESTABRESETS:#ifdef hpux long_return = MIB_tcpcounter[8];#else#if NO_DUMMY_VALUES return NULL;#endif long_return = tcpstat.tcps_drops; /* XXX */#endif return (u_char *) &long_return; /* * NB: tcps_drops is actually the sum of the two MIB * counters tcpAttemptFails and tcpEstabResets. */ case TCPCURRESTAB:#ifndef linux long_return = TCP_Count_Connections();#else long_return = tcpstat.TcpCurrEstab;#endif return (u_char *) &long_return; case TCPINSEGS: long_return = tcpstat.tcps_rcvtotal; return (u_char *) &long_return; case TCPOUTSEGS: long_return = tcpstat.tcps_sndtotal - tcpstat.tcps_sndrexmitpack; /* * RFC 1213 defines this as the number of segments sent * "excluding those containing only retransmitted octets" */ return (u_char *) &long_return; case TCPRETRANSSEGS: long_return = tcpstat.tcps_sndrexmitpack; return (u_char *) &long_return;#ifndef linux case TCPINERRS: long_return = tcpstat.tcps_rcvbadsum + tcpstat.tcps_rcvbadoff #ifdef STRUCT_TCPSTAT_HAS_TCPS_RCVMEMDROP + tcpstat.tcps_rcvmemdrop#endif + tcpstat.tcps_rcvshort; return (u_char *) &long_return; case TCPOUTRSTS: long_return = tcpstat.tcps_sndctrl - tcpstat.tcps_closed; return (u_char *) &long_return;#endif /* linux */ default: DEBUGMSGTL(("snmpd", "unknown sub-id %d in var_tcp\n", vp->magic)); }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -