📄 m2iflib.c
字号:
/* m2IfLib.c - MIB-II interface-group API for SNMP agents *//* Copyright 1984 - 2002 Wind River Systems, Inc. */#include "copyright_wrs.h"/*modification history--------------------01w,25jun02,ann making a temporary fix to the counter update code01v,10may02,kbw making man page edits01u,07may02,kbw making man page edits01t,25apr02,vvv added semaphore protection in m2IfTableUpdate (SPR #74394)01s,05dec01,vvv fixed stack table search for high index values (SPR #71316) 01r,15oct01,rae merge from truestack01q,14jun01,vvv fixed compilation warnings01p,30mar01,rae allow m2 stuff to be excluded (SPR# 65428)01o,23jan01,spm fixed modification history after RFC 2233 merge01n,13nov00,niq merged version 01p of tor2_0.open_stack-f1 branch (base 01l): added RFC 2233 support01m,03nov00,zhu fix SPR#30082: ifIndex should remain constant01l,24mar99,ead Finally fixed the ifLastChange problem! Created the m2SetIfLastChange() function which is called from ifioctl() in if.c. (SPR #23290)01k,16mar99,ead additional fixes to SPR #23290, not indexing in pm2IfTable also zeroed each instance of ifLastChange in m2IfInit so the correct zero value is set during a reboot01j,16mar99,spm recovered orphaned code from tor1_0_1.sens1_1 (SPR #25770)01i,11mar99,ead ifLastChange was not getting updated properly (SPR #23290)01h,06oct98,ann corrected the flag check in m2IfTblEntryGet() for ifOperStatus (SPR #22275)01g,26aug97,spm removed compiler warnings (SPR #7866)01f,03apr96,rjc set the m2InterfaceSem semaphore to NULL in m2IfDelete01e,25jan95,jdi doc cleanup.01d,11nov94,rhp edited man pages some more01c,11nov94,rhp edited man pages01b,22feb94,elh added extern to quiet compiler.01a,08dec93,jag written*//*DESCRIPTIONThis library provides MIB-II services for the interface group. Itprovides routines to initialize the group, access the group scalarvariables, read the table interfaces and change the state of the interfaces.For a broader description of MIB-II services, see the manual entry for m2Lib.To use this feature, include the following component:INCLUDE_MIB2_IFUSING THIS LIBRARYThis library can be initialized and deleted by calling m2IfInit() andm2IfDelete() respectively, if only the interface group's services areneeded. If full MIB-II support is used, this group and all other groupscan be initialized and deleted by calling m2Init() and m2Delete().The interface group supports the Simple Network Management Protocol (SNMP)concept of traps, as specified by RFC 1215. The traps supported by thisgroup are "link up" and "link down." This library enables an applicationto register a hook routine and an argument. This hook routine can becalled by the library when a "link up" or "link down" condition isdetected. The hook routine must have the following prototype:.CSvoid TrapGenerator (int trapType, /@ M2_LINK_DOWN_TRAP or M2_LINK_UP_TRAP @/ int interfaceIndex, void * myPrivateArg);.CEThe trap routine and argument can be specified at initialization time asinput parameters to the routine m2IfInit() or to the routine m2Init().The interface-group global variables can be accessed as follows:.CSM2_INTERFACE ifVars;if (m2IfGroupInfoGet (&ifVars) == OK) /@ values in ifVars are valid @/.CEAn interface table entry can be retrieved as follows:.CSM2_INTERFACETBL interfaceEntry;/@ Specify zero as the index to get the first entry in the table @/interfaceEntry.ifIndex = 2; /@ Get interface with index 2 @/if (m2IfTblEntryGet (M2_EXACT_VALUE, &interfaceEntry) == OK) /@ values in interfaceEntry are valid @/.CEAn interface entry operational state can be changed as follows:.CSM2_INTERFACETBL ifEntryToSet;ifEntryToSet.ifIndex = 2; /@ Select interface with index 2 @/ /@ MIB-II value to set the interface @/ /@ to the down state. @/ifEntryToSet.ifAdminStatus = M2_ifAdminStatus_down;if (m2IfTblEntrySet (&ifEntryToSet) == OK) /@ Interface is now in the down state @/.CEINCLUDE FILES: m2Lib.h SEE ALSO: m2Lib, m2SysLib, m2IpLib, m2IcmpLib, m2UdpLib, m2TcpLib*//* includes */#include "vxWorks.h"#include "stdio.h"#include "stdlib.h"#include "m2IfLib.h"#include "private/m2LibP.h"#include "ioctl.h"#include "semLib.h"#include "string.h"#include "tickLib.h"#include "sysLib.h"#include "errnoLib.h"#include "limits.h"#include "float.h"#include "memPartLib.h"#include "ipProto.h"#include "private/muxLibP.h"#ifdef VIRTUAL_STACK#include "netinet/vsLib.h"#endif /* VIRTUAL_STACK *//* defines */#define END_OBJ_MIB 1 /* Used in ifnetToEndFieldsGet() */#define END_OBJ_FLAGS 2 /* Used in ifnetToEndFieldsGet() *//* imports */IMPORT FUNCPTR _m2SetIfLastChange; /* def'd in if.c for scalability */IMPORT FUNCPTR _m2IfTableUpdate;/* globals */static const UI64 U64_MAX = { UINT_MAX, UINT_MAX };#ifndef VIRTUAL_STACKAVL_TREE pM2IfRoot = NULL;AVL_TREE * pM2IfRootPtr = &pM2IfRoot;M2_IFINDEX * pm2IfTable; /* Network I/F table (Allocated Dynamically) */int m2IfCount; /* Number of network interfaces */int ifsInList; /* Actual number of interfaces in our list */LOCAL FUNCPTR pM2TrapRtn; /* Pointer to trap routine supplied by the user */LOCAL void * pM2TrapRtnArg; /* Pointer to trap routine argument supplied by */ /* the user *//* last time a row was added/removed from the ifTable/ifXTable */LOCAL ULONG ifTableLastChange;/* last time a row was added/removed from the ifStackTable or when the * ifStackStatus was last changed */LOCAL ULONG ifStackLastChange;/* Semaphore used to protect the Interface group */SEM_ID m2InterfaceSem;/* * Global variable used to keep the group startup time in hundreds of a second. * The functionality here is the same as the functionality provided in the * module m2SysLib.c. It is duplicated to allow for both modules to exist * independently. */LOCAL unsigned long startCentiSecs; /* Hundred of Seconds at start */#endif /* VIRTUAL_STACK *//* * The zero object id is used throught out the MIB-II library to fill OID * requests when an object ID is not provided by a group variable. */LOCAL M2_OBJECTID ifZeroObjectId = { 2, {0,0} };extern int ifioctl ();LOCAL void m2IfIncr32Bit (M2_DATA *, ULONG *, ULONG);LOCAL void m2IfIncr64Bit (M2_DATA *, UI64 *, ULONG);LOCAL void m2IfDefaultValsGet (M2_DATA *, M2_IFINDEX *);LOCAL void m2IfCommonValsGet (M2_DATA *, M2_IFINDEX *);LOCAL STATUS rcvEtherAddrGet (struct ifnet *, M2_IFINDEX *);LOCAL STATUS rcvEtherAddrAdd (M2_IFINDEX *, unsigned char *);LOCAL unsigned long centiSecsGet (void);LOCAL BOOL stackEntryIsTop(int);LOCAL BOOL stackEntryIsBottom(int);LOCAL void * ifnetToEndFieldsGet (struct ifnet *, int);/***************************************************************************** m2IfAlloc - allocate the structure for the interface table** This routine is called by the driver during initialization of the interface.* The memory for the interface table is allocated here. We also set the* default update routines in the M2_ID struct. These fields can later be* overloaded using the installed routines in the M2_ID. Once this function* returns, it is the driver's responsibility to set the pMib2Tbl pointer in* the END object to the new M2_ID.** When this call returns, the calling routine must set the END_MIB_2233 * bit of the flags field in the END object.** RETURNS: Pointer to the M2_ID structure that was allocated.*/M2_ID * m2IfAlloc ( ULONG ifType, /* If type of the interface */ UCHAR * pEnetAddr, /* Physical address of interface */ ULONG addrLen, /* Address length */ ULONG mtuSize, /* MTU of interface */ ULONG speed, /* Speed of the interface */ char * pName, /* Name of the device */ int unit /* Unit number of the device */ ) { M2_ID * pM2Id; M2_INTERFACETBL * pM2IfTbl; M2_2233TBL * pM2233Tbl; /* Allocate memory for the interface table */ pM2Id = KHEAP_ALLOC(sizeof(M2_ID)); if (!pM2Id) return NULL; memset(pM2Id, 0, sizeof(M2_ID)); /* Enter the values provided by the driver */ pM2IfTbl = &pM2Id->m2Data.mibIfTbl; pM2233Tbl = &pM2Id->m2Data.mibXIfTbl; pM2IfTbl->ifType = ifType; pM2IfTbl->ifMtu = mtuSize; pM2IfTbl->ifSpeed = speed; memcpy (&pM2IfTbl->ifPhysAddress.phyAddress[0], pEnetAddr, addrLen); pM2IfTbl->ifPhysAddress.addrLength = addrLen; sprintf (&pM2233Tbl->ifName[0], "%s%d", pName, unit); /* Set the default update routines */ pM2Id->m2PktCountRtn = m2IfGenericPacketCount; pM2Id->m2CtrUpdateRtn = m2IfCounterUpdate; pM2Id->m2VarUpdateRtn = m2IfVariableUpdate; return (pM2Id); }/***************************************************************************** m2IfFree - free an interface data structure** This routine frees the given M2_ID. Note if the driver is not an RFC 2233* driver then the M2_ID is NULL and this function simply returns.** RETURNS: OK if successful, ERROR otherwise*/STATUS m2IfFree ( M2_ID * pId /* pointer to the driver's M2_ID object */ ) { if (pId == NULL) { return ERROR; } else { KHEAP_FREE((char *)pId); return OK; } }/***************************************************************************** m2IfIncr32Bit - increment a 32 bit interface counter** This function is used to increment an interface counter. The counter* to update is pointed to by pStat and amount specifies how much to increment* the counter. If the counter would roll-over if incremented by the amount,* the the ifCounterDiscontinuityTime is set to the system uptime.** RETURNS: N/A*/LOCAL void m2IfIncr32Bit ( M2_DATA * pMib2Data, /* pointer to interface stat struct */ ULONG * pStat, /* pointer to the counter to update */ ULONG amount /* amount to update the counter */ ) {#ifdef notdef if (*pStat > UINT_MAX) { *pStat = UINT_MAX; } if ((UINT_MAX - *pStat) < amount) { *pStat = UINT_MAX; pMib2Data->mibXIfTbl.ifCounterDiscontinuityTime = centiSecsGet(); } else { *pStat += amount; }#endif *pStat += amount; }/***************************************************************************** m2IfIncr64Bit - increment a 64 bit interface counter** This function is used to increment an interface counter. The counter* to update is pointed to by pStat and amount specifies how much to increment* the counter. If the counter would roll-over if incremented by the amount,* the the ifCounterDiscontinuityTime is set to the system uptime.** RETURNS: N/A*/LOCAL void m2IfIncr64Bit ( M2_DATA * pMib2Data, /* pointer to interface stat struct */ UI64 * pStat, /* pointer to the counter to update */ ULONG amount /* amount to update the counter */ ) { UI64 u64Diff; UI64 u64Amount; if (UI64_COMP(pStat, &U64_MAX) >= 0) { UI64_COPY(pStat, &U64_MAX); } UI64_ZERO(&u64Diff); UI64_ZERO(&u64Amount); u64Amount.low = amount; UI64_SUB64(&u64Diff, &U64_MAX, pStat); if (UI64_COMP(&u64Diff, &u64Amount) < 0) { UI64_COPY(pStat, &U64_MAX); pMib2Data->mibXIfTbl.ifCounterDiscontinuityTime = centiSecsGet(); } else { UI64_ADD32(pStat, amount); } }/***************************************************************************** m2IfGenericPacketCount - increment the interface packet counters ** This function updates the basic interface counters for a packet. It knows * nothing of the underlying media. Thus, so only the 'ifInOctets', * 'ifHCInOctets', 'ifOutOctets', 'ifHCOutOctets', and * 'ifCounterDiscontinuityTime' variables are incremented. The <ctrl> * argument specifies whether the packet is being sent or just received * (M2_PACKET_IN or M2_PACKET_OUT).** RETURNS: ERROR if the M2_ID is NULL, OK if the counters were updated.*/STATUS m2IfGenericPacketCount ( M2_ID * pId, /* The pointer to the device M2_ID object */ UINT ctrl, /* Update In or Out counters */ UCHAR * pPkt, /* The incoming/outgoing packet */ ULONG pktLen /* Length of the packet */ ) { M2_DATA * pMib2Data; if (pId == NULL) return ERROR; pMib2Data = &(pId->m2Data); switch (ctrl) { case M2_PACKET_IN: m2IfIncr32Bit(pMib2Data, &(pMib2Data->mibIfTbl.ifInOctets), pktLen); break; case M2_PACKET_OUT: m2IfIncr32Bit(pMib2Data, &(pMib2Data->mibIfTbl.ifOutOctets), pktLen); break; default: return ERROR; }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -