?? mgmtapi.c
字號:
/*++
Copyright (c) 1997 Microsoft Corporation
Module Name:
mgmtapi.c
Abstract:
SNMP Management API (wrapped around WinSNMP API).
Environment:
User Mode - Win32
Revision History:
05-Feb-1997 DonRyan
Rewrote functions to be wrappers around WinSNMP.
--*/
///////////////////////////////////////////////////////////////////////////////
// //
// Include Files //
// //
///////////////////////////////////////////////////////////////////////////////
#include <nt.h>
#include <ntrtl.h>
#include <nturtl.h>
#include <windows.h>
#include <wsipx.h>
#include <winsnmp.h>
#include <mgmtapi.h>
#include <oidconv.h>
#include <snmputil.h>
///////////////////////////////////////////////////////////////////////////////
// //
// Private Definitions //
// //
///////////////////////////////////////////////////////////////////////////////
typedef struct _SNMP_MGR_SESSION {
SOCKET UnusedSocket; // WARNING: Previous versions of the
struct sockaddr UnusedDestAddr; // MGMTAPI.H header file exposed the
LPSTR UnusedCommunity; // SNMP_MGR_SESSION structure which
INT UnusedTimeout; // unfortunately encouraged people to
INT UnusedNumRetries; // muck with it. Since this structure
AsnInteger UnusedRequestId; // has now changed we must protect it.
CRITICAL_SECTION SessionLock; // multiple threads may share session
HSNMP_SESSION hSnmpSession; // handle to winsnmp session
HSNMP_ENTITY hAgentEntity; // handle to agent entity
HSNMP_ENTITY hManagerEntity; // handle to manager entity
HSNMP_CONTEXT hViewContext; // handle to view context
HSNMP_PDU hPdu; // handle to snmp pdu
HSNMP_VBL hVbl; // handle to snmp pdu
HWND hWnd; // handle to window
smiINT32 nPduType; // current pdu type
smiINT32 nRequestId; // current request id
smiINT32 nErrorIndex; // error index from pdu
smiINT32 nErrorStatus; // error status from pdu
smiINT32 nLastError; // last system error
SnmpVarBindList * pVarBindList; // pointer to varbind list
} SNMP_MGR_SESSION, *PSNMP_MGR_SESSION;
typedef struct _TRAP_LIST_ENTRY {
LIST_ENTRY Link; // linked-list link
AsnObjectIdentifier EnterpriseOID; // generating enterprise
AsnNetworkAddress AgentAddress; // generating agent addr
AsnNetworkAddress SourceAddress; // generating network addr
AsnInteger nGenericTrap; // generic trap type
AsnInteger nSpecificTrap; // enterprise specific type
AsnOctetString Community; // generating community
AsnTimeticks TimeStamp; // time stamp
SnmpVarBindList VarBindList; // variable bindings
} TRAP_LIST_ENTRY, * PTRAP_LIST_ENTRY;
#define IPADDRLEN 4
#define IPXADDRLEN 10
#define MAXENTITYSTRLEN 128
#define MINVARBINDLEN 2
#define SYSUPTIMEINDEX 0
#define SNMPTRAPOIDINDEX 1
#define DEFAULT_ADDRESS_IP "127.0.0.1"
#define DEFAULT_ADDRESS_IPX "00000000.000000000000"
#define NOTIFICATION_CLASS "MGMTAPI Notification Class"
#define WM_WSNMP_INCOMING (WM_USER + 1)
#define WM_WSNMP_DONE (WM_USER + 2)
#define WSNMP_FAILED(s) ((s) == SNMPAPI_FAILURE)
#define WSNMP_SUCCEEDED(s) ((s) != SNMPAPI_FAILURE)
#define WSNMP_ASSERT(s) { if (!(s)); }
///////////////////////////////////////////////////////////////////////////////
// //
// Global Variables //
// //
///////////////////////////////////////////////////////////////////////////////
HINSTANCE g_hDll; // module handle
HANDLE g_hTrapEvent = NULL; // trap event handle
HANDLE g_hTrapThread = NULL; // trap thread handle
HANDLE g_hTrapRegisterdEvent = NULL; // event to sync. SnmpMgrTrapListen
BOOL g_fIsSnmpStarted = FALSE; // indicates winsnmp inited
BOOL g_fIsSnmpListening = FALSE; // indicates trap thread on
BOOL g_fIsTrapRegistered = FALSE; // indicates trap registered
DWORD g_dwRequestId = 1; // unique pdu request id
LIST_ENTRY g_IncomingTraps; // incoming trap queue
CRITICAL_SECTION g_GlobalLock; // process resource lock
SNMP_MGR_SESSION g_TrapSMS; // process trap session
///////////////////////////////////////////////////////////////////////////////
// //
// Private Procedures //
// //
///////////////////////////////////////////////////////////////////////////////
DWORD
GetRequestId(
)
/*++
Routine Description:
Retrieve next global request id.
Arguments:
None.
Return Values:
Returns request id.
--*/
{
DWORD dwRequestId;
// obtain exclusive access to request id
EnterCriticalSection(&g_GlobalLock);
// obtain copy of request id
dwRequestId = g_dwRequestId++;
// obtain exclusive access to request id
LeaveCriticalSection(&g_GlobalLock);
return dwRequestId;
}
BOOL
TransferVb(
PSNMP_MGR_SESSION pSMS,
SnmpVarBind * pVarBind
)
/*++
Routine Description:
Transfer VarBind structure to WinSNMP structure.
Arguments:
pSMS - pointer to mgmtapi session structure.
pVarBind - pointer to varbind to transfer.
Return Values:
Returns true if successful.
--*/
{
BOOL fOk = FALSE;
SNMPAPI_STATUS status;
smiVALUE tmpValue;
smiOID tmpOID;
// validate session ptr
WSNMP_ASSERT(pSMS != NULL);
// validate pointers
if ((pVarBind != NULL) &&
(pVarBind->name.ids != NULL) &&
(pVarBind->name.idLength != 0)) {
// re-init
fOk = TRUE;
// transfer oid information
tmpOID.len = pVarBind->name.idLength;
tmpOID.ptr = pVarBind->name.ids;
// only initialize value if set
if (pSMS->nPduType == SNMP_PDU_SET) {
// syntax values are equivalent
tmpValue.syntax = (smiINT32)(BYTE)pVarBind->value.asnType;
// determine type
switch (pVarBind->value.asnType) {
case ASN_INTEGER32:
// transfer signed int
tmpValue.value.sNumber = pVarBind->value.asnValue.number;
break;
case ASN_UNSIGNED32:
case ASN_COUNTER32:
case ASN_GAUGE32:
case ASN_TIMETICKS:
// transfer unsigned int
tmpValue.value.uNumber = pVarBind->value.asnValue.unsigned32;
break;
case ASN_COUNTER64:
// transfer 64-bit counter
tmpValue.value.hNumber.lopart =
pVarBind->value.asnValue.counter64.LowPart;
tmpValue.value.hNumber.hipart =
pVarBind->value.asnValue.counter64.HighPart;
break;
case ASN_OPAQUE:
case ASN_IPADDRESS:
case ASN_OCTETSTRING:
case ASN_BITS:
// transfer octet string
tmpValue.value.string.len =
pVarBind->value.asnValue.string.length;
tmpValue.value.string.ptr =
pVarBind->value.asnValue.string.stream;
break;
case ASN_OBJECTIDENTIFIER:
// transfer object id
tmpValue.value.oid.len =
pVarBind->value.asnValue.object.idLength;
tmpValue.value.oid.ptr =
pVarBind->value.asnValue.object.ids;
break;
case ASN_NULL:
case SNMP_EXCEPTION_NOSUCHOBJECT:
case SNMP_EXCEPTION_NOSUCHINSTANCE:
case SNMP_EXCEPTION_ENDOFMIBVIEW:
// initialize empty byte
tmpValue.value.empty = 0;
break;
default:
// failure
fOk = FALSE;
break;
}
}
if (fOk) {
// register varbind
status = SnmpSetVb(
pSMS->hVbl,
0, // index
&tmpOID,
(pSMS->nPduType == SNMP_PDU_SET)
? &tmpValue
: NULL
);
// validate return code
if (WSNMP_FAILED(status)) {
SNMPDBG((
SNMP_LOG_ERROR,
"MGMTAPI: SnmpSetVb returned %d.\n",
SnmpGetLastError(pSMS->hSnmpSession)
));
// failure
fOk = FALSE;
}
}
}
return fOk;
}
BOOL
AllocateVbl(
PSNMP_MGR_SESSION pSMS
)
/*++
Routine Description:
Transfer VarBindList structure to WinSNMP structure.
Arguments:
pSMS - pointer to mgmtapi session structure.
Return Values:
Returns true if successful.
--*/
{
BOOL fOk = FALSE;
SNMPAPI_STATUS status;
SnmpVarBind * pVarBind;
DWORD cVarBind;
// validate session ptr
WSNMP_ASSERT(pSMS != NULL);
// validate parameters
WSNMP_ASSERT(pSMS->pVarBindList != NULL);
WSNMP_ASSERT(pSMS->pVarBindList->len != 0);
WSNMP_ASSERT(pSMS->pVarBindList->list != NULL);
// allocate resources for variable bindings list
pSMS->hVbl = SnmpCreateVbl(pSMS->hSnmpSession, NULL, NULL);
// validate varbind handle
if (WSNMP_SUCCEEDED(pSMS->hVbl)) {
// re-init
fOk = TRUE;
// initialize varbind pointer
pVarBind = pSMS->pVarBindList->list;
// initialize varbind count
cVarBind = pSMS->pVarBindList->len;
// process each varbind
while (fOk && cVarBind--) {
// transfer variable binding
fOk = TransferVb(pSMS, pVarBind++);
}
if (!fOk) {
// release varbind list handle
status = SnmpFreeVbl(pSMS->hVbl);
// validate return code
if (WSNMP_FAILED(status)) {
SNMPDBG((
SNMP_LOG_ERROR,
"MGMTAPI: SnmpFreeVbl returned %d.\n",
SnmpGetLastError(pSMS->hSnmpSession)
));
}
// re-initialize
pSMS->hVbl = (HSNMP_VBL)NULL;
}
} else {
SNMPDBG((
SNMP_LOG_ERROR,
"MGMTAPI: SnmpCreateVbl returned %d.\n",
SnmpGetLastError(pSMS->hSnmpSession)
));
}
return fOk;
}
BOOL
FreeVbl(
PSNMP_MGR_SESSION pSMS
)
/*++
Routine Description:
Cleanup VarBind resources from WinSNMP structure.
Arguments:
pSMS - pointer to mgmtapi session structure.
Return Values:
Returns true if successful.
--*/
{
BOOL fOk = TRUE;
SNMPAPI_STATUS status;
// validate session ptr
WSNMP_ASSERT(pSMS != NULL);
// validate handle
if (pSMS->hVbl != (HSNMP_VBL)NULL) {
// actually release vbl handle
status = SnmpFreeVbl(pSMS->hVbl);
// validate return code
if (WSNMP_FAILED(status)) {
SNMPDBG((
SNMP_LOG_ERROR,
"MGMTAPI: SnmpFreeVbl returned %d.\n",
SnmpGetLastError(pSMS->hSnmpSession)
));
// failure
fOk = FALSE;
}
// re-initialize handle
pSMS->hVbl = (HSNMP_VBL)NULL;
}
return fOk;
}
BOOL
AllocatePdu(
PSNMP_MGR_SESSION pSMS
)
/*++
Routine Description:
Initialize session structure for sending request.
Arguments:
pSMS - pointer to mgmtapi session structure.
Return Values:
Returns true if successful.
--*/
{
BOOL fOk = FALSE;
// validate session ptr
WSNMP_ASSERT(pSMS != NULL);
// transfer varbinds
if (AllocateVbl(pSMS)) {
// grab next shared request id
pSMS->nRequestId = GetRequestId();
// create request pdu
pSMS->hPdu = SnmpCreatePdu(
pSMS->hSnmpSession,
pSMS->nPduType,
pSMS->nRequestId,
0, // errorStatus
0, // errorIndex
pSMS->hVbl
);
// validate return status
if (WSNMP_SUCCEEDED(pSMS->hPdu)) {
// success
fOk = TRUE;
} else {
SNMPDBG((
SNMP_LOG_ERROR,
"MGMTAPI: SnmpCreatePdu returned %d.\n",
SnmpGetLastError(pSMS->hSnmpSession)
));
// free resources
FreeVbl(pSMS);
}
}
return fOk;
}
BOOL
FreePdu(
PSNMP_MGR_SESSION pSMS
)
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -