📄 mgmtapi.c
字号:
SNMP_LOG_ERROR,
"MGMTAPI: Ignoring invalid pdu type %d.\n",
nPduType
));
// continue
fDone = FALSE;
}
} else {
SNMPDBG((
SNMP_LOG_ERROR,
"MGMTAPI: SnmpGetPduData returned %d.\n",
SnmpGetLastError(pSMS->hSnmpSession)
));
// retrieve last error status from winsnmp
pSMS->nLastError = SnmpGetLastError(pSMS->hSnmpSession);
}
// release temporary entity
SnmpFreeEntity(hAgentEntity);
// release temporary entity
SnmpFreeEntity(hManagerEntity);
// release temporary context
SnmpFreeContext(hViewContext);
} else {
SNMPDBG((
SNMP_LOG_ERROR,
"MGMTAPI: SnmpRecvMsg returned %d.\n",
SnmpGetLastError(pSMS->hSnmpSession)
));
// retrieve last error status from winsnmp
pSMS->nLastError = SnmpGetLastError(pSMS->hSnmpSession);
}
// release pdu
FreePdu(pSMS);
return fDone;
}
LRESULT
CALLBACK
NotificationWndProc(
HWND hWnd,
UINT uMsg,
WPARAM wParam,
LPARAM lParam
)
/*++
Routine Description:
Callback that processes WinSNMP notifications.
Arguments:
hWnd - window handle.
uMsg - message identifier.
wParam - first message parameter.
lParam - second message parameter.
Return Values:
The return value is the result of the message processing and
depends on the message sent.
--*/
{
// check for winsnmp notification
if (uMsg == WM_WSNMP_INCOMING) {
PSNMP_MGR_SESSION pSMS;
// retrieve mgmtapi session pointer from window
pSMS = (PSNMP_MGR_SESSION)GetWindowLongPtr(hWnd, 0);
// validate session ptr
WSNMP_ASSERT(pSMS != NULL);
// process notification message
if (NotificationCallback(pSMS)) {
// post message to break out of message pump
PostMessage(pSMS->hWnd, WM_WSNMP_DONE, (WPARAM)0, (LPARAM)0);
}
return (LRESULT)0;
} else {
// forward all other messages to windows
return DefWindowProc(hWnd, uMsg, wParam, lParam);
}
}
BOOL
RegisterNotificationClass(
)
/*++
Routine Description:
Register notification class for sessions.
Arguments:
None.
Return Values:
Returns true if successful.
--*/
{
BOOL fOk;
WNDCLASS wc;
// initialize notification window class
wc.lpfnWndProc = (WNDPROC)NotificationWndProc;
wc.lpszClassName = NOTIFICATION_CLASS;
wc.lpszMenuName = NULL;
wc.hInstance = g_hDll;
wc.hIcon = NULL;
wc.hCursor = NULL;
wc.hbrBackground = NULL;
wc.cbWndExtra = sizeof(PSNMP_MGR_SESSION);
wc.cbClsExtra = 0;
wc.style = 0;
// register class
fOk = RegisterClass(&wc);
if (!fOk) {
SNMPDBG((
SNMP_LOG_ERROR,
"MGMTAPI: RegisterClass returned %d.\n",
GetLastError()
));
}
return fOk;
}
BOOL
UnregisterNotificationClass(
)
/*++
Routine Description:
Unregister notification class.
Arguments:
None.
Return Values:
Returns true if successful.
--*/
{
BOOL fOk;
// unergister notification window class
fOk = UnregisterClass(NOTIFICATION_CLASS, g_hDll);
if (!fOk) {
SNMPDBG((
SNMP_LOG_ERROR,
"MGMTAPI: UnregisterClass returned %d.\n",
GetLastError()
));
}
return fOk;
}
BOOL
StartSnmpIfNecessary(
)
/*++
Routine Description:
Initialize WinSNMP DLL if necessary.
Arguments:
None.
Return Values:
Returns true if successful.
--*/
{
BOOL fOk = TRUE;
// serialize access to startup code
EnterCriticalSection(&g_GlobalLock);
// see if already started
if (g_fIsSnmpStarted != TRUE) {
SNMPAPI_STATUS status;
// initialize start params
smiUINT32 nMajorVersion = 0;
smiUINT32 nMinorVersion = 0;
smiUINT32 nLevel = 0;
smiUINT32 nTranslateMode = 0;
smiUINT32 nRetransmitMode = 0;
// start winsnmp
status = SnmpStartup(
&nMajorVersion,
&nMinorVersion,
&nLevel,
&nTranslateMode,
&nRetransmitMode
);
// validate return code
if (WSNMP_SUCCEEDED(status)) {
SNMPDBG((
SNMP_LOG_TRACE,
"MGMTAPI: SnmpStartup succeeded:\n"
"MGMTAPI:\tnMajorVersion = %d\n"
"MGMTAPI:\tnMinorVersion = %d\n"
"MGMTAPI:\tnLevel = %d\n"
"MGMTAPI:\tnTranslateMode = %d\n"
"MGMTAPI:\tnRetransmitMode = %d\n",
nMajorVersion,
nMinorVersion,
nLevel,
nTranslateMode,
nRetransmitMode
));
// allocate global trap available event
g_hTrapEvent = CreateEvent(NULL, FALSE, FALSE, NULL);
// allocate global event to sync. SnmpMgrTrapListen
g_hTrapRegisterdEvent = CreateEvent(NULL, FALSE, FALSE, NULL);
// make sure translate mode is snmp v1
SnmpSetTranslateMode(SNMPAPI_UNTRANSLATED_V1);
// make sure retransmit mode is on
SnmpSetRetransmitMode(SNMPAPI_ON);
// register notification class
RegisterNotificationClass();
// save new status
g_fIsSnmpStarted = TRUE;
// success
fOk = TRUE;
} else {
SNMPDBG((
SNMP_LOG_ERROR,
"MGMTAPI: SnmpStartup returned %d.\n",
SnmpGetLastError((HSNMP_SESSION)NULL)
));
// failure
fOk = FALSE;
}
}
// serialize access to startup code
LeaveCriticalSection(&g_GlobalLock);
return fOk;
}
BOOL
CleanupIfNecessary(
)
/*++
Routine Description:
Cleanup WinSNMP DLL if necessary.
Arguments:
None.
Return Values:
Returns true if successful.
--*/
{
BOOL fOk = TRUE;
// serialize access to startup code
EnterCriticalSection(&g_GlobalLock);
// see if already started
if (g_fIsSnmpStarted == TRUE) {
SNMPAPI_STATUS status;
// shutdown winsnmp
status = SnmpCleanup();
// validate return code
if (WSNMP_FAILED(status)) {
SNMPDBG((
SNMP_LOG_ERROR,
"MGMTAPI: SnmpCleanup returned %d.\n",
SnmpGetLastError((HSNMP_SESSION)NULL)
));
// failure
fOk = FALSE;
}
// unregister notification class
UnregisterNotificationClass();
// save new status
g_fIsSnmpStarted = FALSE;
}
// check trap handle
if (g_hTrapEvent != NULL) {
// close trap handle
CloseHandle(g_hTrapEvent);
// re-initialize
g_hTrapEvent = NULL;
}
// serialize access to startup code
LeaveCriticalSection(&g_GlobalLock);
return fOk;
}
BOOL
CreateNotificationWindow(
PSNMP_MGR_SESSION pSMS
)
/*++
Routine Description:
Create notification window for session.
Arguments:
pSMS - pointer to MGMTAPI session structure.
Return Values:
Returns true if successful.
--*/
{
BOOL fOk;
// validate session ptr
WSNMP_ASSERT(pSMS != NULL);
// create notification window
pSMS->hWnd = CreateWindow(
NOTIFICATION_CLASS,
NULL, // pointer to window name
0, // window style
0, // horizontal position of window
0, // vertical position of window
0, // window width
0, // window height
NULL, // handle to parent or owner window
NULL, // handle to menu or child-window identifier
g_hDll, // handle to application instance
NULL // pointer to window-creation data
);
// validate window handle
if (pSMS->hWnd != NULL) {
// store pointer to session in window
SetWindowLongPtr(pSMS->hWnd, 0, (LONG_PTR)pSMS);
// success
fOk = TRUE;
} else {
SNMPDBG((
SNMP_LOG_ERROR,
"MGMTAPI: CreateWindow returned %d.\n",
GetLastError()
));
// failure
fOk = FALSE;
}
return fOk;
}
BOOL
DestroyNotificationWindow(
HWND hWnd
)
/*++
Routine Description:
Destroy notification window for session.
Arguments:
hWnd - window handle for session.
Return Values:
Returns true if successful.
--*/
{
BOOL fOk;
// destroy notification window
fOk = DestroyWindow(hWnd);
if (!fOk) {
SNMPDBG((
SNMP_LOG_ERROR,
"MGMTAPI: DestroyWindow returned %d.\n",
GetLastError()
));
}
return fOk;
}
BOOL
CloseSession(
PSNMP_MGR_SESSION pSMS
)
/*++
Routine Description:
Close WinSNMP session associated with MGMTAPI session.
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);
// check if window opened
if (pSMS->hWnd != (HWND)NULL) {
// destroy notification window
fOk = DestroyNotificationWindow(pSMS->hWnd);
}
// check if agent entity allocated
if (pSMS->hAgentEntity != (HSNMP_ENTITY)NULL) {
// close the entity handle
status = SnmpFreeEntity(pSMS->hAgentEntity);
// validate status
if (WSNMP_FAILED(status)) {
SNMPDBG((
SNMP_LOG_ERROR,
"MGMTAPI: SnmpFreeEntity returned %d.\n",
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -