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

📄 m2iflib.c

📁 vxworks源码源码解读是学习vxworks的最佳途径
💻 C
📖 第 1 页 / 共 2 页
字号:
    semGive (m2InterfaceSem);    return (OK);    }/******************************************************************************** m2IfTblEntrySet - set the state of a MIB-II interface entry to UP or DOWN** This routine selects the interface specified in the input parameter* <pIfTblEntry> and sets the interface to the requested state.  It is* the responsibility of the calling routine to set the interface* index, and to make sure that the state specified in the* `ifAdminStatus' field of the structure at <pIfTblEntry> is a valid* MIB-II state, up(1) or down(2).** RETURNS: OK, or ERROR if the input parameter is not specified, an interface* is no longer valid, the interface index is incorrect, or the ioctl() command* to the interface fails.** ERRNO:*   S_m2Lib_INVALID_PARAMETER*   S_m2Lib_ENTRY_NOT_FOUND*   S_m2Lib_IF_CNFG_CHANGED** SEE ALSO:* m2IfInit(), m2IfGroupInfoGet(), m2IfTblEntryGet(), m2IfDelete()*/STATUS m2IfTblEntrySet    (    M2_INTERFACETBL * pIfTblEntry    /* pointer to requested entry to change */    )    {    struct ifreq ifReqChange;        /* Driver IOCTL command structure */     /*     * If the the table the SNMP Mgr has is invalid, it must be read again     * before a set operation can be honored.     */     if (pIfTblEntry == NULL || ifAttachChange > 0)	{	if (pIfTblEntry == NULL)	    errnoSet (S_m2Lib_INVALID_PARAMETER);	else	    errnoSet (S_m2Lib_IF_CNFG_CHANGED);        return (ERROR);	}     semTake (m2InterfaceSem, WAIT_FOREVER);     pIfTblEntry->ifIndex--;     /* Map MIB-II index to our format */     /* Validate the bounds of the requested index  */    if (pIfTblEntry->ifIndex < 0 || pIfTblEntry->ifIndex >= m2IfCount)        {	errnoSet (S_m2Lib_ENTRY_NOT_FOUND);        semGive (m2InterfaceSem);        return (ERROR);        }     /*     * The only supported state changes for the interface are UP and DOWN.  The     * TEST sate is not supported by the drivers.     */    if (pIfTblEntry->ifAdminStatus == M2_ifAdminStatus_up)        {		/* 	 * If the driver is in the UP state, there is no need to send the	 * IOCTL command, otherwise format the IOCTL request. 	 */         if (pm2IfTable [pIfTblEntry->ifIndex].pNetIfDrv->ac_if.if_flags &            IFF_UP)            {            semGive (m2InterfaceSem);            return (OK);            }         sprintf (ifReqChange.ifr_name,"%s%d",                 pm2IfTable [pIfTblEntry->ifIndex].pNetIfDrv->ac_if.if_name,                 pm2IfTable [pIfTblEntry->ifIndex].pNetIfDrv->ac_if.if_unit);         ifReqChange.ifr_flags =          pm2IfTable [pIfTblEntry->ifIndex].pNetIfDrv->ac_if.if_flags | IFF_UP;        }    else        {	/* 	 * If the driver is in the DOWN state, there is no need to send the	 * IOCTL command, otherwise format the IOCTL request. 	 */         if (pIfTblEntry->ifAdminStatus != M2_ifAdminStatus_down)            {            semGive (m2InterfaceSem);            return (ERROR);            }         if ((pm2IfTable [pIfTblEntry->ifIndex].pNetIfDrv->ac_if.if_flags &             IFF_UP) == 0)            {            semGive (m2InterfaceSem);            return (OK);            }         sprintf (ifReqChange.ifr_name,"%s%d",                 pm2IfTable [pIfTblEntry->ifIndex].pNetIfDrv->ac_if.if_name,                 pm2IfTable [pIfTblEntry->ifIndex].pNetIfDrv->ac_if.if_unit);         ifReqChange.ifr_flags =         pm2IfTable [pIfTblEntry->ifIndex].pNetIfDrv->ac_if.if_flags & ~IFF_UP;        }         /*         * The network semaphore is not taken for this operation because the         * called routine takes the network semphore.  Issue the IOCTL to the	 * driver routine, and verify that the request is honored.         */         if (ifioctl (0, SIOCSIFFLAGS, (char *)&ifReqChange) != 0)            {            semGive (m2InterfaceSem);            return (ERROR);            }     semGive (m2InterfaceSem);    return (OK);    }/********************************************************************************* m2NetIfTableRead - reads the list of network drivers into an internal table** This routine reads the BSD list of network drivers into its internal table.* This table is used to handle MIB-II request for the interface group as well* as the IP group.  The global variables <pm2IfTable> and <m2IfCount> are* set by this routine.  If the user has registered a routine to generate a * trap, the routine will be called.** NOMANUAL** RETURNS: OK, ERROR if there are no network drivers in the list, or if memory* can not be allocated.** ERRNO:* S_m2Lib_IF_TBL_IS_EMPTY** SEE ALSO: N/A*/int m2NetIfTableRead (void)    {    struct ifnet * pIfNet;      /* Pointer to system Network I/F table */    NET_IF_TBL   * pTmpIfTable; /* Temp Pointer Network I/F table */    NET_IF_TBL   * pOldIfTable; /* Pointer to old MIB-II Network I/F table */    int            oldIfCount;  /* Old MIB-II Network I/F count */    int            netLock;     /* Used to secure the Network Code Access */    int            ifIndex;     /* Used for index counter */    int            trapValue;	/* Input parameter for a trap call */    M2_NETDRVCNFG  ifDrvCnfg;   /* Driver Interface configuration IOCTL */     /* Save the old table, and initialize new table pointers. */     pOldIfTable = pm2IfTable;    oldIfCount  = m2IfCount;     pm2IfTable = NULL;    m2IfCount = 0;     netLock = splnet ();        /* Get exclusive access to Network Code */     /* Count the number of network interfaces in the system */     for (pIfNet = ifnet; pIfNet != NULL; m2IfCount++)        {        pIfNet = pIfNet->if_next;        }     if (m2IfCount == 0)        {        splx (netLock);         /* Give up exclusive access to Network Code */	if (pOldIfTable != NULL)	    free (pOldIfTable); /* Free Interface table is invalid anyways */	errnoSet (S_m2Lib_IF_TBL_IS_EMPTY);        return (ERROR);         /* The Network I/F list is empty */        }     /* Allocate the Interface Table */     if ((pm2IfTable = (NET_IF_TBL *) malloc ((sizeof(NET_IF_TBL)* m2IfCount)))         == NULL)        {        splx (netLock);         /* Give up exclusive access to Network Code */	if (pOldIfTable != NULL)	    free (pOldIfTable); /* Free Interface table is invalid anyways */        return (ERROR);        }     /* Read Network Interface Table */    /*     * Traverse the BSD driver interface table, and collect all the  necessary     * driver information in the MIB-II interface table.  This provides a one     * one mapping.     */     for (pIfNet = ifnet, pTmpIfTable = pm2IfTable, ifIndex = 1;         pIfNet != NULL; pTmpIfTable++, ifIndex++)        {        pTmpIfTable->netIfIndex       = ifIndex;        pTmpIfTable->pNetIfDrv        = (struct arpcom *) pIfNet;        pTmpIfTable->netIfAdminStatus = ((pIfNet->if_flags & IFF_UP) != 0 ) ?				  M2_ifAdminStatus_up : M2_ifAdminStatus_down;         /* Set Interface OID to default */	bcopy (((char *) ifZeroObjectId.idArray), 	       ((char *) pTmpIfTable->netIfOid.idArray),	       ifZeroObjectId.idLength * sizeof (long));         pTmpIfTable->netIfOid.idLength = ifZeroObjectId.idLength;         /*         * Read the Type, Speed and OID of each Network Interface.  This is done	 * by sending an IOCTL to the network driver.  Drivers which do not 	 * support MIB-II fail this IOCTL command, and default values are 	 * assinged. Otherwise the parameters returned by the driver are used.         */         bzero ((char *) &ifDrvCnfg, sizeof(ifDrvCnfg));         if ((pIfNet->if_ioctl != NULL) && ((*pIfNet->if_ioctl)                             (pIfNet, SIOCGMIB2CNFG, (caddr_t)&ifDrvCnfg)) == 0)            {            /* Read Driver configuration */             pTmpIfTable->netIfType  = ifDrvCnfg.ifType;             if (ifDrvCnfg.ifSpecific.idLength > 0)                {		bcopy (((char *) ifDrvCnfg.ifSpecific.idArray), 		       ((char *) pTmpIfTable->netIfOid.idArray),		       ifDrvCnfg.ifSpecific.idLength * sizeof (long));                 pTmpIfTable->netIfOid.idLength = ifDrvCnfg.ifSpecific.idLength;                }            }        else            {            /* Make an educated guess at the driver configuration */             if ((strncmp(pIfNet->if_name, "bp", 2) == 0) ||                (strncmp(pIfNet->if_name, "sm", 2) == 0))                {                /* VxWorks backplane drivers */                 pTmpIfTable->netIfType  = M2_ifType_other;                pTmpIfTable->netIfSpeed = 0;                }            else            if (strncmp(pIfNet->if_name, "lo", 2) == 0)                {                /* IP Loop back interface. */                 pTmpIfTable->netIfType  = M2_ifType_softwareLoopback;                pTmpIfTable->netIfSpeed = 0;                }            else            if (strncmp(pIfNet->if_name, "sl", 2) == 0)                {                /* Slip Network Interface */                 pTmpIfTable->netIfType  = M2_ifType_slip;                pTmpIfTable->netIfSpeed = 19200;        /* Baud Rate ??? */                }            else            if (strncmp(pIfNet->if_name, "ppp", 3) == 0)                {                /* PPP Network Interface */                 pTmpIfTable->netIfType  = M2_ifType_ppp;                pTmpIfTable->netIfSpeed = 19200;        /* Baud Rate ??? */                }            else               {		/* Use Ethernet parameters as defaults */                 pTmpIfTable->netIfType  = M2_ifType_ethernet_csmacd;                pTmpIfTable->netIfSpeed = 10000000;                }            }         pIfNet = pIfNet->if_next;       /* Go to the Next interface */        }     ifAttachChange = 0;         /* Reset Attach flag set in module if.c */     splx (netLock);             /* Give up exclusive access to Network Code */     /*      * If a routine to generate traps was NOT specified at the time the     * interfaces group was initialize we are done.     */    if (pM2TrapRtn == NULL)        {        if (pOldIfTable != NULL)            {	    free ((char *) pOldIfTable);            }        return (OK);        }     /* Go through Inteface Table an generate traps if necessary */     if (pOldIfTable == NULL || oldIfCount != m2IfCount)        {        /* Interface table has changed.  Generate traps */         for (ifIndex = 0, pTmpIfTable = pm2IfTable; ifIndex < m2IfCount;                                                pTmpIfTable++, ifIndex++)            {            trapValue = ( (pTmpIfTable->pNetIfDrv->ac_if.if_flags & IFF_UP)                            == 0) ? M2_LINK_DOWN_TRAP : M2_LINK_UP_TRAP;            (*pM2TrapRtn)(trapValue, ifIndex, pM2TrapRtnArg);            }        }     /* Free old interface table */    if (pOldIfTable != NULL)        {	free ((char *) pOldIfTable);        }    return (OK);    }/********************************************************************************* m2IfDelete - delete all resources used to access the interface group** This routine frees all the resources allocated at the time the group was* initialized.  The interface group should not be accessed after this routine * has been called.** RETURNS: OK, always.** SEE ALSO:* m2IfInit(), m2IfGroupInfoGet(), m2IfTblEntryGet(), m2IfTblEntrySet()*/STATUS m2IfDelete (void)    {    /* Release the interface semaphore */    if (m2InterfaceSem != NULL)        {        semDelete (m2InterfaceSem);        m2InterfaceSem = NULL;        }    /* Free Network I/F Table Struct */     if (pm2IfTable != NULL)        {        free (pm2IfTable);        pm2IfTable = NULL;        }    /* Clear trap routine variables */    pM2TrapRtn    = NULL;    pM2TrapRtnArg = NULL;    return (OK);    }/*************************************************************************** * * m2SetIfLastChange - set the ifLastChange MIB variable for the interface * * This routine sets the ifLastChange and ifAdminStatus for the interface * specified by the device and unit arguments. * * NOMANUAL * * RETURNS: OK, or ERROR if the interface isn't found in pm2IfTable * * SEE ALSO: * m2IfInit(), m2IfGroupInfoGet(), m2IfTblEntryGet(), m2IfTblEntrySet() */STATUS m2SetIfLastChange    (    char *	device,    short	unit,    short	flags    )    {    int			ifIndex;    int			trapValue;    NET_IF_TBL *	pTmpIfTable;    /* find the interface in pm2IfTable */    for (ifIndex = 0, pTmpIfTable = pm2IfTable; ifIndex < m2IfCount;	 pTmpIfTable++, ifIndex++)        {        if ((strcmp(pTmpIfTable->pNetIfDrv->ac_if.if_name, device) == 0) &&	    (pTmpIfTable->pNetIfDrv->ac_if.if_unit == unit))	    {	    /* update the ifLastChange variable */	    pTmpIfTable->netIfLastChange = centiSecsGet();	    /* update the ifAdminStatus variable */	    pTmpIfTable->netIfAdminStatus =	        ((flags & IFF_UP) != 0) ?		M2_ifAdminStatus_up : M2_ifAdminStatus_down;	    /* if needed, send out necessary traps */            if (pM2TrapRtn != NULL)                {                trapValue = (pTmpIfTable->netIfAdminStatus !=		             M2_ifAdminStatus_up) ?                            M2_LINK_DOWN_TRAP : M2_LINK_UP_TRAP;                (*pM2TrapRtn)(trapValue, pTmpIfTable->netIfIndex,		              pM2TrapRtnArg);                }            return (OK);	    }        }    return (ERROR);	/* interface not found */    }

⌨️ 快捷键说明

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