📄 m2tcplib.c
字号:
/* m2TcpLib.c - MIB-II TCP-group API for SNMP agents *//* Copyright 1984 - 2001 Wind River Systems, Inc. */#include "copyright_wrs.h"/*modification history--------------------01f,15oct01,rae merge from truestack ver 01j, base 01e (VIRTUAL_STACK)01e,08mar97,vin added changes to accomodate changes in pcb structure.01d,25jan95,jdi doc cleanup.01c,11nov94,rhp edit man pages01b,10nov94,rhp fixed typo in m2TcpInit man page01a,08dec93,jag written*//*DESCRIPTIONThis library provides MIB-II services for the TCP group. It provides routinesto initialize the group, access the group global variables, read the tableof TCP connections, and change the state of a TCP connection. For a broaderdescription of MIB-II services, see the manual entry for m2Lib.To use this feature, include the following component:INCLUDE_MIB2_TCPUSING THIS LIBRARYThis library can be initialized and deleted by calling m2TcpInit() andm2TcpDelete() respectively, if only the TCP group's services are needed.If full MIB-II support is used, this group and all other groups can beinitialized and deleted by calling m2Init() and m2Delete().The group global variables are accessed by callingm2TcpGroupInfoGet() as follows:.CS M2_TCP tcpVars; if (m2TcpGroupInfoGet (&tcpVars) == OK) /@ values in tcpVars are valid @/.CEThe TCP table of connections can be accessed in lexicographical order. Thefirst entry in the table can be accessed by setting the table index tozero. Every other entry thereafter can be accessed by passing tom2TcpConnTblEntryGet() the index retrieved in the previous invocationincremented to the next lexicographical value by givingM2_NEXT_VALUE as the search parameter. For example:.CSM2_TCPCONNTBL tcpEntry; /@ Specify a zero index to get the first entry in the table @/ tcpEntry.tcpConnLocalAddress = 0; /@ Local IP address in host byte order @/ tcpEntry.tcpConnLocalPort = 0; /@ Local TCP port @/ tcpEntry.tcpConnRemAddress = 0; /@ remote IP address @/ tcpEntry.tcpConnRemPort = 0; /@ remote TCP port in host byte order @/ /@ get the first entry in the table @/ if ((m2TcpConnTblEntryGet (M2_NEXT_VALUE, &tcpEntry) == OK) /@ values in tcpEntry in the first entry are valid @/ /@ process first entry in the table @/ /@ * For the next call, increment the index returned in the previous call. * The increment is to the next possible lexicographic entry; for * example, if the returned index was 147.11.46.8.2000.147.11.46.158.1000 * the index passed in the next invocation should be * 147.11.46.8.2000.147.11.46.158.1001. If an entry in the table * matches the specified index, then that entry is returned. * Otherwise the closest entry following it, in lexicographic order, * is returned. @/ /@ get the second entry in the table @/ if ((m2TcpConnTblEntryGet (M2_NEXT_VALUE, &tcpEntry) == OK) /@ values in tcpEntry in the second entry are valid @/.CEThe TCP table of connections allows only for a connection to be deleted asspecified in the MIB-II. For example: .CS M2_TCPCONNTBL tcpEntry; /@ Fill in the index for the connection to be deleted in the table @/ /@ Local IP address in host byte order, and local port number @/ tcpEntry.tcpConnLocalAddress = 0x930b2e08; tcpEntry.tcpConnLocalPort = 3000; /@ Remote IP address in host byte order, and remote port number @/ tcpEntry.tcpConnRemAddress = 0x930b2e9e; tcpEntry.tcpConnRemPort = 3000; tcpEntry.tcpConnState = 12; /@ MIB-II state value for delete @/ /@ set the entry in the table @/ if ((m2TcpConnTblEntrySet (&tcpEntry) == OK) /@ tcpEntry deleted successfuly @/.CEINCLUDE FILES: m2Lib.h SEE ALSO:m2Lib, m2IfLib, m2IpLib, m2IcmpLib, m2UdpLib, m2SysLib*//* includes */#include "vxWorks.h"#include "m2Lib.h"#include <socket.h>#include <net/route.h>#include <netinet/in.h>#include <netinet/tcp.h>#include <netinet/tcp_timer.h>#include <netinet/tcp_fsm.h>#include <netinet/tcp_var.h>#include <netinet/tcp_timer.h>#include <netinet/in_pcb.h>#include <net/protosw.h>#include <private/iosLibP.h>#ifdef VIRTUAL_STACK#include "netinet/vsLib.h"#endif/* defines *//* MIB-II TCP state definitions */#define M2TCP_CLOSED 1#define M2TCP_LISTEN 2#define M2TCP_SYNSENT 3#define M2TCP_SYNRECEIVED 4#define M2TCP_ESTABLISHED 5#define M2TCP_FINWAIT1 6#define M2TCP_FINWAIT2 7#define M2TCP_CLOSEWAIT 8#define M2TCP_LASTACK 9#define M2TCP_CLOSING 10#define M2TCP_TIMEWAIT 11#define M2TCP_DELETETCB 12/* globals *//* * This table maps the BSD TCP states to the MIB-II TCP specified states. The * table is indexed using the BSD TCP states. */LOCAL long m2TcpStates [TCP_NSTATES] = { M2TCP_CLOSED, /* TCPS_CLOSED -> M2TCP_CLOSED */ M2TCP_LISTEN, /* TCPS_LISTEN -> M2TCP_LISTEN */ M2TCP_SYNSENT, /* TCPS_SYN_SENT -> M2TCP_SYNSENT */ M2TCP_SYNRECEIVED, /* TCPS_SYN_RECEIVED -> M2TCP_SYNRECEIVED */ M2TCP_ESTABLISHED, /* TCPS_ESTABLISHED -> M2TCP_ESTABLISHED */ M2TCP_CLOSEWAIT, /* TCPS_CLOSE_WAIT -> M2TCP_CLOSEWAIT */ M2TCP_FINWAIT1, /* TCPS_FIN_WAIT_1 -> M2TCP_FINWAIT1 */ M2TCP_CLOSING, /* TCPS_CLOSING -> M2TCP_CLOSING */ M2TCP_LASTACK, /* TCPS_LAST_ACK -> M2TCP_LASTACK */ M2TCP_FINWAIT2, /* TCPS_FIN_WAIT_2 -> M2TCP_FINWAIT2 */ M2TCP_TIMEWAIT, /* TCPS_TIME_WAIT -> M2TCP_TIMEWAIT */ };/********************************************************************************* m2TcpInit - initialize MIB-II TCP-group access** This routine allocates the resources needed to allow access to the TCP * MIB-II variables. This routine must be called before any TCP variables* can be accessed.** RETURNS: OK, always.** SEE ALSO: * m2TcpGroupInfoGet(), m2TcpConnEntryGet(), m2TcpConnEntrySet(), m2TcpDelete()*/STATUS m2TcpInit (void) { return (OK); }/******************************************************************************** m2TcpGroupInfoGet - get MIB-II TCP-group scalar variables** This routine fills in the TCP structure pointed to by <pTcpInfo> with the* values of MIB-II TCP-group scalar variables.** RETURNS: OK, or ERROR if <pTcpInfo> is not a valid pointer.** ERRNO:* S_m2Lib_INVALID_PARAMETER** SEE ALSO: * m2TcpInit(), m2TcpConnEntryGet(), m2TcpConnEntrySet(), m2TcpDelete()*/STATUS m2TcpGroupInfoGet ( M2_TCPINFO * pTcpInfo /* pointer to the TCP group structure */ ) { int netLock; /* Use to secure the Network Code Access */ struct inpcb * pInpCb; /* Ptr to an internet control block */ struct tcpcb * pTcpCb; /* Ptr to a TCP connection control block */ /* Validate Pointer to TCP structure */ if (pTcpInfo == NULL) { errnoSet (S_m2Lib_INVALID_PARAMETER); return (ERROR); } netLock = splnet (); /* Get exclusive access to Network Code */ /* Traverse the list of TCP control block and count the # of connections */#ifdef VIRTUAL_STACK /* * To avoid introducing a conflict with the "tcpcb" structure tag, * virtual stacks do not alias the head of the pcb list. */ for (pInpCb = tcb.lh_first, pTcpInfo->tcpCurrEstab = 0;#else for (pInpCb = tcpcb.lh_first, pTcpInfo->tcpCurrEstab = 0;#endif /* VIRTUAL_STACK */ pInpCb != NULL; pInpCb = pInpCb->inp_list.le_next) { /* Get TCP Connection control structure */ pTcpCb = (struct tcpcb *) pInpCb->inp_ppcb; if ((pTcpCb->t_state == TCPS_ESTABLISHED) || (pTcpCb->t_state == TCPS_CLOSE_WAIT)) pTcpInfo->tcpCurrEstab++; } splx (netLock); /* Give up exclusive access to Network Code */ pTcpInfo->tcpRtoAlgorithm = M2_tcpRtoAlgorithm_vanj; pTcpInfo->tcpRtoMin = (TCPTV_MIN * 1000) / PR_SLOWHZ; pTcpInfo->tcpRtoMax = (TCPTV_REXMTMAX * 1000) / PR_SLOWHZ; /* The Maximum number of TCP connections is determined Dynamically == -1 */ pTcpInfo->tcpMaxConn = -1; pTcpInfo->tcpActiveOpens = tcpstat.tcps_connattempt; pTcpInfo->tcpPassiveOpens = tcpstat.tcps_accepts; pTcpInfo->tcpAttemptFails = tcpstat.tcps_conndrops; pTcpInfo->tcpEstabResets = tcpstat.tcps_drops; pTcpInfo->tcpInSegs = tcpstat.tcps_rcvtotal; pTcpInfo->tcpOutSegs = tcpstat.tcps_sndtotal - tcpstat.tcps_sndrexmitpack - tcpstat.tcps_persisttimeo; pTcpInfo->tcpRetransSegs = tcpstat.tcps_sndrexmitpack; pTcpInfo->tcpInErrs = tcpstat.tcps_rcvbadsum + tcpstat.tcps_rcvbadoff + tcpstat.tcps_rcvshort;#ifdef VIRTUAL_STACK /* * (Former) tcpOutRsts global renamed for virtual stacks to prevent * name conflict with existing structure element. */ pTcpInfo->tcpOutRsts = tcpOutResets;#else pTcpInfo->tcpOutRsts = tcpOutRsts;#endif return (OK); }/******************************************************************************** tcpConnCmp - compare two TCP connections in lexicographical order** This routine compares two TCP connection control blocks. It compares the* value 1 with value 2. It returns equal, value 1 greater than * value 2, or value 1 less than value 2. The comparison is done based on the* lexicographical order specified by MIB-II.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -