📄 snmpdlib.c
字号:
semTake (SNMP_CoarseLock, WAIT_FOREVER); if ((pLeaf = SNMP_View_Create (pTreeOid, treeOidLen)) == NULL ) { snmpdLog (SNMP_WARN, "View create failed\n"); errno = S_snmpdLib_VIEW_CREATE_FAILURE; status = ERROR; goto lockRelease; } if (SNMP_View_Install (index, pLeaf)) { SNMP_View_Delete(pLeaf); (void) snmpdLog (SNMP_WARN, "View install failed\n"); errno = S_snmpdLib_VIEW_INSTALL_FAILURE; status = ERROR; goto lockRelease; } /* We assume volatile storage (STO_VOL) and table entry is active * from start */ SNMP_View_Set_StorageType (pLeaf, STO_VOL); SNMP_View_Set_Status (pLeaf, RS_ACTIVE) ; SNMP_View_Set_Type (pLeaf, viewType) ; if (SNMP_View_Set_Mask (pLeaf, (OCTET_T *) pMask, maskLen)) { SNMP_View_Delete(pLeaf); (void) snmpdLog (SNMP_WARN, "View set mask failed\n") ; errno = S_snmpdLib_VIEW_MASK_FAILURE; status = ERROR; goto lockRelease; } status = OK; /* fall thru to lockRelease */ lockRelease: semGive (SNMP_CoarseLock); return (status); } /********************************************************************************* snmpdViewTblRemove - remove view table and free allocated resources ** This routine removes all the entries in the view table and frees all* allocated resources. It is used at agent-shutdown time.** RETURNS: N/A** NOMANUAL*/LOCAL void snmpdViewTblRemove (void) { VIEWLEAF_T * pLeaf; /* ptr to leaf of view tree */ while ( (pLeaf = SNMP_View_Next (NULL)) != NULL) { if (SNMP_View_Deinstall (pLeaf->index, pLeaf->subtree.component_list, pLeaf->subtree.num_components )) { snmpdLog (SNMP_WARN, "View deinstall failed\n"); errno = S_snmpdLib_VIEW_DEINSTALL_FAILURE; continue; } SNMP_View_Delete (pLeaf); } }/********************************************************************************* snmpdViewEntryRemove - remove an entry from the view table** This routine removes an entry from the view table and deallocates* the associated resources. The entry should have been previously* created with snmpdViewEntrySet().** RETURNS: N/A** ERRNO: S_snmpdLib_VIEW_LOOKUP_FAILURE, S_snmpdLib_VIEW_DEINSTALL_FAILURE** SEE ALSO: snmpdViewEntrySet()*/void snmpdViewEntryRemove ( OIDC_T * pTreeOid, /* oid of view subtree to remove */ int treeOidLen, /* length of view subtree oid */ UINT_16_T index /* index of entry */ ) { VIEWLEAF_T * pLeaf; /* ptr to leaf of view tree */ semTake (SNMP_CoarseLock, WAIT_FOREVER); if ((pLeaf = SNMP_View_Lookup (index, pTreeOid, treeOidLen)) == NULL) { snmpdLog (SNMP_WARN, "Unable to lookup view entry\n"); errno = S_snmpdLib_VIEW_LOOKUP_FAILURE; goto lockRelease; } if (SNMP_View_Deinstall (index, pTreeOid, treeOidLen)) { snmpdLog (SNMP_WARN, "View deinstall failed\n"); errno = S_snmpdLib_VIEW_DEINSTALL_FAILURE; goto lockRelease; } SNMP_View_Delete (pLeaf); /* fall through to lockRelease */lockRelease: semGive (SNMP_CoarseLock); }#endif/*sar*//********************************************************************************* snmpdTreeAdd - dynamically add a subtree to the SNMP agent MIB tree** This routine adds the specified MIB subtree, located in memory at* the address <pTreeAddr>, to the agent's MIB data tree at the node* with the object identifier specified by <pTreeOidStr>. This subtree* is normally generated with the `-start' option to `mibcomp'.** RETURNS: OK on success, otherwise ERROR.** ERRNO: S_snmpdLib_MIB_ADDITION_FAILURE** SEE ALSO:* .I WindNet SNMPv1v2c VxWorks Component Release Supplement*/STATUS snmpdTreeAdd ( char * pTreeOidStr, MIBNODE_T * pTreeAddr ) { int status = 0; MIBNODE_T * pReplacedNode; OBJ_ID_T treeOid; OIDC_T oidArray [MAX_OID_LEN]; treeOid.num_components = snmpOidStrToArray (pTreeOidStr, oidArray); if (treeOid.num_components > 0) { treeOid.component_list = oidArray; semTake (SNMP_CoarseLock, WAIT_FOREVER); status = Add_Node_From_Root (NULL, &treeOid, pTreeAddr, &pReplacedNode); semGive (SNMP_CoarseLock); if ( status == 0 ) { snmpdLog (SNMP_INFO, "Mib tree added successfully\n"); } else { snmpdLog (SNMP_WARN, "Unable to add mib tree\n"); errno = S_snmpdLib_MIB_ADDITION_FAILURE; } } return ((status == 0) ? OK : ERROR); }/******************************************************************************* snmpdTreeRemove - dynamically remove part of the SNMP agent MIB tree** This routine deletes part of the SNMP agent MIB tree at* runtime. Once the specified subtree is deleted, any further requests* to access objects of that subtree fail.** RETURNS: N/A** ERRNO: S_snmpdLib_NODE_NOT_FOUND** SEE ALSO:* .I WindNet SNMPv1v2c VxWorks Component Release Supplement*/void snmpdTreeRemove ( char * pTreeOidStr /* char string specifying oid of tree to remove */ ) { OBJ_ID_T treeOid; OIDC_T oidArray [MAX_OID_LEN]; treeOid.num_components = snmpOidStrToArray (pTreeOidStr, oidArray); if (treeOid.num_components > 0) { treeOid.component_list = oidArray; semTake (SNMP_CoarseLock, WAIT_FOREVER); if (Remove_Node_From_Root (NULL, &treeOid) == NULL) { errno = S_snmpdLib_NODE_NOT_FOUND; } semGive (SNMP_CoarseLock); } }/******************************************************************************** snmpdTrapSend - general interface to trap facilities** This routine sends a trap of type <trapType> and specific* <trapSpecific> to all the specified destinations, given an array of* destinations in <ppDestAddrTbl> of len <numDestn>.** The <version> parameter is either SNMP_VERSION_1 or SNMP_VERSION_2,* depending on whether a v1-style or v2-style trap is to be generated.* The community used is specified by <pTrapCmnty>. <pLocalAddr>* indicates a local address to use for a sending endpoint. <pMyOid>* and <myOidlen> specify the system object identifier (<sysObjId>) to* use.** <numVarBinds> indicates the number of variable bindings that are to* be encoded in the packet. <trapVarBindsRtn> is the routine to use* for doing the variable bindings. <pCookie> is passed to this* routine as shown below:** .CS* (*trapVarBindsRtn) (pPkt, pCookie)* .CE** RETURNS: N/A** ERRNO: S_snmpdLib_INVALID_SNMP_VERSION, S_snmpdLib_TRAP_CREATE_FAILURE,* S_snmpdLib_TRAP_BIND_FAILURE, S_snmpdLib_TRAP_ENCODE_FAILURE*/void snmpdTrapSend ( void * pSnmpEndpoint, /* snmp agent transport endpoint */ int numDestn, /* number of destinations */ void ** ppDestAddrTbl, /* array of ptrs to destinations */ void * pLocalAddr, /* local address */ int version, /* SNMP version */ char * pTrapCmnty, /* trap community string */ OIDC_T * pMyOid, /* agent object identifier */ int myOidLen, /* length of agent object identifier */ u_long * pIpAddr, /* ip address of sender */ int trapType, /* trap type */ int trapSpecific, /* trap specific code */ int numVarBinds, /* number of varbinds in packet */ FUNCPTR trapVarBindsRtn, /* routine to bind varbinds */ void * pCookie /* argument to binding routine */ ) { EBUFFER_T buf; SNMP_PKT_T * pPkt; int ix; static int requestId; /* transaction identifier for pkt */ EBufferInitialize (&buf); ++ requestId; if (version == SNMP_VERSION_1) { pPkt = SNMP_Create_Trap (SNMP_VERSION_1, strlen (pTrapCmnty), pTrapCmnty, myOidLen, (OIDC_T*) pMyOid, (OCTET_T *) pIpAddr, trapType, trapSpecific, snmpCentiSecsGet (), numVarBinds); } else if (version == SNMP_VERSION_2) { pPkt = SNMP_Create_Request2 (TRAP2_PDU, version, strlen (pTrapCmnty), pTrapCmnty, requestId, numVarBinds, 0, 0); } else { /* some invalid version */ snmpdLog (SNMP_WARN, "Invalid SNMP version for trap creation\n"); errno = S_snmpdLib_INVALID_SNMP_VERSION; return; } if ( pPkt == NULL ) { snmpdLog (SNMP_WARN, "Failed to create trap pkt\n"); errno = S_snmpdLib_TRAP_CREATE_FAILURE; return; } if ( (trapVarBindsRtn != NULL) && (*trapVarBindsRtn) (pPkt, pCookie)) { snmpdLog (SNMP_WARN, "Failed to bind vars in trap pkt\n"); errno = S_snmpdLib_TRAP_BIND_FAILURE; return; } if (SNMP_Encode_Packet (pPkt, &buf) == -1) { snmpdLog (SNMP_WARN, "Failed to encode trap pkt\n"); errno = S_snmpdLib_TRAP_ENCODE_FAILURE; SNMP_Free (pPkt); return; } SNMP_Free (pPkt); for ( ix = 0; ix < numDestn; ++ix, ++ppDestAddrTbl ) { snmpIoWrite (pSnmpEndpoint, (char *) buf.start_bp, EBufferUsed (&buf), *ppDestAddrTbl, pLocalAddr); } EBufferClean (&buf); ++ snmp_stats.snmpOutPkts; ++ snmp_stats.snmpOutTraps; }/******************************************************************************** snmpCentiSecsGet - get hundreds of a second** The number of hundreds of a second that have passed since this routine was* first called.** Note: This routine assumes that the sysClkRate will never get above* 2^32 / 100. If it does then the remainder portion of the equations will* rollover. This _shouldn't_ be a problem.** RETURNS: Hundreds of a second since the group was initialized.** NOMANUAL*/LOCAL ulong_t snmpCentiSecsGet (void) { static ulong_t numRollovers; static ulong_t rolloverCentiSecsExtra; ulong_t currCentiSecs = 0; ulong_t clkRate = sysClkRateGet (); ulong_t currTicks = tickGet (); static ulong_t prevTicks; if ((snmpStartCentiSecs == 0) && (numRollovers == 0)) { numRollovers = 0; rolloverCentiSecsExtra = 0; prevTicks = 0; snmpStartCentiSecs = ((currTicks / clkRate) * 100) + (((currTicks % clkRate) * 100) / clkRate); } /* Check if the ticks have rolled over. This can happen early if the sysClkRate is greater than 100. */ if (prevTicks > currTicks) { numRollovers++; /* The additional term covers the fact that 0xFFFFFFFF is 1 less than the actuall rollover amount. */ rolloverCentiSecsExtra += (((ulong_t)0xFFFFFFFF / clkRate) * 100) + ((((ulong_t)0xFFFFFFFF % clkRate) * 100 + (clkRate / 2)) / clkRate) + (numRollovers * 100) / clkRate; } /* To clarify... This is: whole second + (remainder + rounding). */ currCentiSecs = ((currTicks / clkRate) * 100) + (((currTicks % clkRate) * 100 + (clkRate / 2)) / clkRate); /* Save the current ticks for next time this routine is called. */ prevTicks = currTicks; return (rolloverCentiSecsExtra + currCentiSecs - snmpStartCentiSecs); }#if (INSTALL_ENVOY_SNMP_VERSION_3)/******************************************************************************** snmpSecsGet - get second based clock** A second based clock, the start time doesn't matter ** RETURNS: A second based count** NOMANUAL*/ulong_t snmpSecsGet (void) { return(tickGet() / sysClkRateGet()); }#endif /* INSTALL_ENVOY_SNMP_VERSION_3 *//****************************************************************************** generateTrap - generate the MIB-2 trap** This routine is passed to the m2IfInit routine inorder to generate* a trap. It is a wrapper (with the correct paramters) to the* snmpIoTrapSend routine** RETURNS: N/A** NOMANUAL*/void generateTrap ( int ifTrapType, /* LINK_UP_TRAP or LINK_DOWN_TRAP */ int interfaceIndex, /* Index for the interface */ void * pM2TrapRtnArg ) { snmpIoTrapSend (ifTrapType, interfaceIndex); }/******************************************************************************** snmpNextIndex - Computes the next feasible OID for m2 routines** Function to compute the next feasible OID for some given * table index type.** <compc> - component count of input oid sequence
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -