⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 m2iflib.c

📁 vxworks源码源码解读是学习vxworks的最佳途径
💻 C
📖 第 1 页 / 共 2 页
字号:
/* m2IfLib.c - MIB-II interface-group API for SNMP agents *//* Copyright 1984 - 1999 Wind River Systems, Inc. */#include "copyright_wrs.h"/*modification history--------------------01l,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.USING 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:.CS    M2_INTERFACE   ifVars;    if (m2IfGroupInfoGet (&ifVars) == OK)	/@ values in ifVars are valid @/.CEAn interface table entry can be retrieved as follows:.CS    M2_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:.CS    M2_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 "m2Lib.h"#include "private/m2LibP.h"#include "ioctl.h"#include "semLib.h"#include "string.h"#include "tickLib.h"#include "sysLib.h"#include "errnoLib.h"/* globals */NET_IF_TBL  * pm2IfTable;     /* Network I/F table (Allocated Dynamically) */int           m2IfCount;      /* Number of network interfaces */LOCAL FUNCPTR pM2TrapRtn;     /* Pointer to trap routine supplied by the user */LOCAL void *  pM2TrapRtnArg;  /* Pointer to trap routine argument supplied by */			      /* the user *//* Semaphore used to protect the Interface group */SEM_ID m2InterfaceSem;/* * 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} };/*  * 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 */extern int ifioctl ();/******************************************************************************** centiSecsGet - get hundreds of a second** The number of hundreds of a second that have passed since this routine was* first called.** RETURNS: Hundreds of a second since the group was initialized.** SEE ALSO: N/A*/LOCAL unsigned long centiSecsGet (void)    {    unsigned long currCentiSecs;    unsigned long clkRate = sysClkRateGet ();    if (startCentiSecs == 0)	startCentiSecs = (tickGet () * 100) / clkRate;    currCentiSecs = (tickGet () * 100) / clkRate;    return (currCentiSecs - startCentiSecs);    }/******************************************************************************** m2IfInit - initialize MIB-II interface-group routines** This routine allocates the resources needed to allow access to the * MIB-II interface-group variables.  This routine must be called before any * interface variables can be accessed.  The input parameter <pTrapRtn> is an* optional pointer to a user-supplied SNMP trap generator.  The input parameter* <pTrapArg> is an optional argument to the trap generator.  Only one trap* generator is supported.** RETURNS: OK, always.** ERRNO:* S_m2Lib_CANT_CREATE_IF_SEM** SEE ALSO: * m2IfGroupInfoGet(), m2IfTblEntryGet(), m2IfTblEntrySet(), m2IfDelete()*/STATUS m2IfInit     (    FUNCPTR 	pTrapRtn, 	/* pointer to user trap generator */    void * 	pTrapArg	/* pointer to user trap generator argument */    )    {    int i;      /* temporary index in the pm2IfTable */    /* Create the Interface semaphore */     if (m2InterfaceSem == NULL)        {        m2InterfaceSem = semMCreate (SEM_Q_PRIORITY | SEM_INVERSION_SAFE |                                   SEM_DELETE_SAFE);        if (m2InterfaceSem == NULL)            {	    errnoSet (S_m2Lib_CANT_CREATE_IF_SEM);            return (ERROR);            }        }    /* Take the interface semaphore, and initialize group global variables */     semTake (m2InterfaceSem, WAIT_FOREVER);    pM2TrapRtn    = pTrapRtn;    pM2TrapRtnArg = pTrapArg;     if ((pm2IfTable == NULL) && (m2NetIfTableRead () == ERROR))        {	pM2TrapRtn    = NULL;	pM2TrapRtnArg = NULL;        semGive (m2InterfaceSem);        return (ERROR);        }    /* (Re)Initialize all the ifLastChange instances */    for (i = 0; i < m2IfCount; i++)        {        (&pm2IfTable[i])->netIfLastChange = 0;        }    semGive (m2InterfaceSem);    (void) centiSecsGet ();     /* Initialize group time reference */    return (OK);    }/******************************************************************************** m2IfGroupInfoGet -  get the MIB-II interface-group scalar variables** This routine fills out the interface-group structure at <pIfInfo> with* the values of MIB-II interface-group global variables.** RETURNS: OK, or ERROR if <pIfInfo> is not a valid pointer.** ERRNO:* S_m2Lib_INVALID_PARAMETER** SEE ALSO:* m2IfInit(), m2IfTblEntryGet(), m2IfTblEntrySet(), m2IfDelete()*/STATUS m2IfGroupInfoGet    (    M2_INTERFACE * pIfInfo	/* pointer to interface group structure */    )    {    semTake (m2InterfaceSem, WAIT_FOREVER);     /* Read the interface table, if a configuration change has occurred */    if (ifAttachChange > 0)        if (m2NetIfTableRead () == ERROR)       /* Read the Network If table */            {            semGive (m2InterfaceSem);            return (ERROR);            }     /* Validate the input parameter pointer */    if (pIfInfo == NULL)        {        semGive (m2InterfaceSem);	errnoSet (S_m2Lib_INVALID_PARAMETER);        return (ERROR);        }    /* Number of network interfaces in the system independent of their state */     pIfInfo->ifNumber = m2IfCount;       semGive (m2InterfaceSem);    return (OK);    }/******************************************************************************** m2IfTblEntryGet -  get a MIB-II interface-group table entry** This routine maps the MIB-II interface index to the system's internal* interface index.  The <search> parameter is set to either* M2_EXACT_VALUE or M2_NEXT_VALUE; for a discussion of its use, see* the manual entry for m2Lib.  If the status of the interface has changed* since it was last read, the user trap routine is called.** RETURNS: * OK, or ERROR if the input parameter is not specified, or a match is not found.** ERRNO:*  S_m2Lib_INVALID_PARAMETER*  S_m2Lib_ENTRY_NOT_FOUND** SEE ALSO:* m2Lib, m2IfInit(), m2IfGroupInfoGet(), m2IfTblEntrySet(), m2IfDelete()*/STATUS m2IfTblEntryGet    (    int               search,       /* M2_EXACT_VALUE or M2_NEXT_VALUE */    M2_INTERFACETBL * pIfReqEntry   /* pointer to requested interface entry */    )    {    M2_NETDRVCNTRS    m2DrvCounters;   /* Network Driver IOCTL struct */    NET_IF_TBL      * pNetIf;          /* Temporay Pointer to I/F table */     /* Validate pointer to requeste structure */     if (pIfReqEntry == NULL)	{	errnoSet (S_m2Lib_INVALID_PARAMETER);        return (ERROR);	}   /* Initialize Driver IOCTL structure */     bzero ((char *) &m2DrvCounters, sizeof (M2_NETDRVCNTRS));     semTake (m2InterfaceSem, WAIT_FOREVER);     /* Read the interace table, if a configuration change has occurred */    if (ifAttachChange > 0)        if (m2NetIfTableRead () == ERROR)       /* Read the Network If table */            {            semGive (m2InterfaceSem);            return (ERROR);            }    /*     * For an M2_EXACT_VALUE search map the MIB-II interface index to our      * format.  For a M2_NEXT_VALUE search the MIB-II interface index maps     * directly to      * our format.     */    if (search == M2_EXACT_VALUE)        pIfReqEntry->ifIndex--;    /* Validate the bounds of the requested index  */     if (pIfReqEntry->ifIndex >= m2IfCount || pIfReqEntry->ifIndex < 0)        {        semGive (m2InterfaceSem);	errnoSet (S_m2Lib_ENTRY_NOT_FOUND);        return (ERROR);        }    /* Set pointer to the requested interface index */     pNetIf = &pm2IfTable [pIfReqEntry->ifIndex];     /* Fill interface entry request */    pIfReqEntry->ifIndex = pNetIf->netIfIndex;     sprintf (pIfReqEntry->ifDescr, "%s%d", pNetIf->pNetIfDrv->ac_if.if_name,             pNetIf->pNetIfDrv->ac_if.if_unit);     bcopy (((char *)pNetIf->netIfOid.idArray), 	   ((char *)pIfReqEntry->ifSpecific.idArray),	   ((int) pNetIf->netIfOid.idLength * sizeof (long)));    pIfReqEntry->ifSpecific.idLength = pNetIf->netIfOid.idLength;    pIfReqEntry->ifMtu   = pNetIf->pNetIfDrv->ac_if.if_mtu;     /* If the device does not have a hardware address set it to zero */     if (pNetIf->pNetIfDrv->ac_if.if_flags &                                (IFF_LOOPBACK | IFF_POINTOPOINT | IFF_NOARP))        {        pIfReqEntry->ifPhysAddress.addrLength = 0;        }    else        {        bcopy (((char *) pNetIf->pNetIfDrv->ac_enaddr),               ((char *) pIfReqEntry->ifPhysAddress.phyAddress), ETHERADDRLEN);        pIfReqEntry->ifPhysAddress.addrLength = ETHERADDRLEN;        }    /* Get the ifAdminStatus, ifLastChange, and ifOperStatus  */    pIfReqEntry->ifAdminStatus =                        ((pNetIf->pNetIfDrv->ac_if.if_flags & IFF_UP) != 0) ?                          M2_ifAdminStatus_up : M2_ifAdminStatus_down;    pNetIf->netIfAdminStatus  = pIfReqEntry->ifAdminStatus;    pIfReqEntry->ifLastChange = pNetIf->netIfLastChange;    pIfReqEntry->ifOperStatus =            ((pNetIf->pNetIfDrv->ac_if.if_flags & IFF_UP) != 0) ?                                      M2_ifOperStatus_up : M2_ifOperStatus_down;     pIfReqEntry->ifInUcastPkts  = pNetIf->pNetIfDrv->ac_if.if_ipackets -				  pNetIf->pNetIfDrv->ac_if.if_imcasts;    pIfReqEntry->ifInNUcastPkts = pNetIf->pNetIfDrv->ac_if.if_imcasts;    pIfReqEntry->ifInErrors     = pNetIf->pNetIfDrv->ac_if.if_ierrors;    pIfReqEntry->ifOutUcastPkts = pNetIf->pNetIfDrv->ac_if.if_opackets -				  pNetIf->pNetIfDrv->ac_if.if_omcasts;    pIfReqEntry->ifOutNUcastPkts= pNetIf->pNetIfDrv->ac_if.if_omcasts;    pIfReqEntry->ifOutErrors    = pNetIf->pNetIfDrv->ac_if.if_oerrors;    pIfReqEntry->ifOutQLen      = pNetIf->pNetIfDrv->ac_if.if_snd.ifq_len;     /*     * The MIB-II interface entry variables below can only be tracked by the      * hardware driver.  Drivers that support these variables respond to the     * IOCTL successfuly. If these variables are not supported by the driver     * default values are used.     */     if ( (pNetIf->pNetIfDrv->ac_if.if_ioctl != NULL) &&         ( (*pNetIf->pNetIfDrv->ac_if.if_ioctl) (& (pNetIf->pNetIfDrv->ac_if),                                                 SIOCGMIB2CNTRS,                                                  (caddr_t)&m2DrvCounters))         == 0)        {	/* Fill parameters with driver counters */        pNetIf->netIfSpeed             = m2DrvCounters.ifSpeed;        pIfReqEntry->ifInOctets        = m2DrvCounters.ifInOctets;        pIfReqEntry->ifInDiscards      = m2DrvCounters.ifInDiscards;        pIfReqEntry->ifInUnknownProtos = m2DrvCounters.ifInUnknownProtos;        pIfReqEntry->ifOutOctets       = m2DrvCounters.ifOutOctets;        pIfReqEntry->ifOutDiscards     = m2DrvCounters.ifOutDiscards;        }    else        {	/* Fill parameters with defaults */        pIfReqEntry->ifInOctets        = -1;        pIfReqEntry->ifInDiscards      =  0;        pIfReqEntry->ifInUnknownProtos =  0;        pIfReqEntry->ifOutOctets       = -1;        pIfReqEntry->ifOutDiscards     =  0;        }     pIfReqEntry->ifType  = pNetIf->netIfType;    pIfReqEntry->ifSpeed = pNetIf->netIfSpeed; 

⌨️ 快捷键说明

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