📄 snmpelpt.cpp
字号:
WriteTrace(0x14,"ReopenLog: Log file %s has been cleared; reopening log\n",
lpszLogName);
CloseEventLog(hLogHandle); // first, close old handle
hLogHandle = OpenEventLog( (LPTSTR) NULL, lpszLogName);
if (hLogHandle == NULL)
{ // did log file open?
lastError = GetLastError(); // save error code
WriteTrace(0x14,"ReopenLog: Error in EventLogOpen for %s = %lu \n",
lpszLogName, lastError);
WriteLog(SNMPELEA_ERROR_OPEN_EVENT_LOG, lpszLogName, lastError); // log the error message
return(FALSE); // failed -- forget this one
}
WriteTrace(0x00,"ReopenLog: New handle for %s is %08X\n",
lpszLogName, hLogHandle);
*(phEventLogs+dwOffset) = hLogHandle; // save new handle now
WriteTrace(0x00,"ReopenLog: Reissuing NotifyChangeEventLog for log\n");
if (!NotifyChangeEventLog(*(phEventLogs+dwOffset),
*(phWaitEventPtr+dwOffset)))
{
lastError = GetLastError();
WriteTrace(0x14,"ReopenLog: NotifyChangeEventLog failed with code %lu\n",
lastError);
WriteLog(SNMPELEA_ERROR_LOG_NOTIFY, lastError); // log error message
return(FALSE);
}
WriteTrace(0x00,"ReopenLog: ChangeNotify was successful\n");
return(TRUE);
}
VOID
DisplayLogRecord(
IN PEVENTLOGRECORD pEventBuffer,
IN DWORD dwSize,
IN DWORD dwNeeded
)
/*++
Routine Description:
This routine is called to display the event log record after reading it.
Arguments:
pEventBuffer - This is a pointer to an EVENTLOGRECORD structure
containing the current event log record.
dwSize - Contains the size in bytes of the amount of data
just read into the buffer specified on the
ReadEventLog.
dwNeeded - Contains the size in bytes of the amount of storage
required to read the next log record if GetLastError()
returns ERROR_INSUFFICIENT_BUFFER.
Return Value:
None
--*/
{
PCHAR pcString; // temporary string pointer
UINT j; // temporary loop counter
if (nTraceLevel) // if not maximum tracing
{
return; // just get out
}
WriteTrace(0x00,"DisplayLogRecord: Values from ReadEventLog follow:\n");
WriteTrace(0x00,"DisplayLogRecord: EventSize = %lu EventNeeded = %lu\n",
dwSize, dwNeeded);
WriteTrace(0x00,"DisplayLogRecord: Event Log Buffer contents follow:\n");
WriteTrace(0x00,"DisplayLogRecord: Length = %lu Record Number = %lu\n",
pEventBuffer->Length, pEventBuffer->RecordNumber);
WriteTrace(0x00,"DisplayLogRecord: Time generated = %08X Time written = %08X\n",
pEventBuffer->TimeGenerated, pEventBuffer->TimeWritten);
WriteTrace(0x00,"DisplayLogRecord: Event ID = %lu (%08X) Event Type = %04X\n",
pEventBuffer->EventID, pEventBuffer->EventID, pEventBuffer->EventType);
WriteTrace(0x00,"DisplayLogRecord: Num Strings = %lu EventCategory = %04X\n",
pEventBuffer->NumStrings, pEventBuffer->EventCategory);
WriteTrace(0x00,"DisplayLogRecord: String Offset = %lu Data Length = %lu\n",
pEventBuffer->StringOffset, pEventBuffer->DataLength);
WriteTrace(0x00,"DisplayLogRecord: Data Offset = %lu\n",
pEventBuffer->DataOffset);
pcString = (PCHAR) pEventBuffer + EVENTRECSIZE;
WriteTrace(0x00,"DisplayLogRecord: EventBuffer address is %08X\n", pEventBuffer);
WriteTrace(0x00,"DisplayLogRecord: EVENTRECSIZE is %lu\n",EVENTRECSIZE);
WriteTrace(0x00,"DisplayLogRecord: String pointer is assigned address %08X\n",
pcString);
WriteTrace(0x00,"DisplayLogRecord: SourceName[] = %s\n", pcString);
pcString += strlen(pcString) + 1;
WriteTrace(0x00,"DisplayLogRecord: Computername[] = %s\n", pcString);
pcString = (PCHAR) pEventBuffer + pEventBuffer->StringOffset;
WriteTrace(0x00,"DisplayLogRecord: String pointer is assigned address %08X\n",
pcString);
for (j = 0; j < pEventBuffer->NumStrings; j++)
{
WriteTrace(0x00,"DisplayLogRecord: String #%lu ->%s\n", j, pcString);
pcString += strlen(pcString) + 1;
}
}
BOOL
AddBufferToQueue(
IN PVarBindQueue lpVarBindEntry // pointer to varbind entry structure
)
/*++
Routine Description:
This routine will add a varbind entry to the queue of traps to send.
Arguments:
lpVarBindEntry - This is a pointer to a varbind entry.
Return Value:
TRUE - The varbind entry was successfully added to the queue.
FALSE - The varbind entry could not be added to the queue.
Notes:
--*/
{
PVarBindQueue pBuffer; // temporary pointer
HANDLE hWaitList[2]; // wait event array
LONG lastError; // for GetLastError()
DWORD status; // for wait
WriteTrace(0x0a,"AddBufferToQueue: Entering AddBufferToQueue function\n");
if (fThresholdEnabled && fThreshold)
{
WriteTrace(0x0a,"AddBufferToQueue: Performance threshold flag is on. No data will be processed.\n");
WriteTrace(0x0a,"AddBufferToQueue: Exiting AddBufferToQueue function with FALSE\n");
return(FALSE);
}
WriteTrace(0x00,"AddBufferToQueue: Current buffer pointer is %08X\n", lpVarBindQueue);
WriteTrace(0x00,"AddBufferToQueue: Adding buffer address %08X to queue\n", lpVarBindEntry);
hWaitList[0] = hMutex; // mutex handle
hWaitList[1] = hStopAll; // DLL termination event handle
WriteTrace(0x00,"AddBufferToQueue: Handle to Mutex object is %08X\n", hMutex);
WriteTrace(0x0a,"AddBufferToQueue: Waiting for Mutex object to become available\n");
while (TRUE)
{
status = WaitForMultipleObjects(
2, // only two objects to wait on
(CONST PHANDLE) &hWaitList, // address of array of event handles
FALSE, // only one event is required
1000); // only wait one second
lastError = GetLastError(); // save any error conditions
WriteTrace(0x0a,"AddBufferToQueue: WaitForMulitpleObjects returned a value of %lu\n", status);
switch (status)
{
case WAIT_FAILED:
WriteTrace(0x14,"AddBufferToQueue: Error waiting for mutex event array is %lu\n",
lastError); // trace error message
WriteLog(SNMPELEA_ERROR_WAIT_ARRAY, lastError); // log error message
WriteTrace(0x0a,"AddBufferToQueue: Exiting AddBufferToQueue routine with FALSE\n");
return(FALSE); // get out now
case WAIT_TIMEOUT:
WriteTrace(0x0a,"AddBufferToQueue: Mutex object not available yet. Wait will continue.\n");
continue; // retry the wait
case WAIT_ABANDONED:
WriteTrace(0x14,"AddBufferToQueue: Mutex object has been abandoned.\n");
WriteLog(SNMPELEA_MUTEX_ABANDONED);
WriteTrace(0x0a,"AddBufferToQueue: Exiting AddBufferToQueue routine with FALSE\n");
return(FALSE); // get out now
case 1:
WriteTrace(0x0a,"AddBufferToQueue: DLL shutdown detected. Wait abandoned.\n");
WriteTrace(0x0a,"AddBufferToQueue: Exiting AddBufferToQueue routine with FALSE\n");
return(FALSE);
case 0:
WriteTrace(0x0a,"AddBufferToQueue: Mutex object acquired.\n");
break;
default:
WriteTrace(0x14,"AddBufferToQueue: Undefined error encountered in WaitForMultipleObjects. Wait abandoned.\n");
WriteLog(SNMPELEA_ERROR_WAIT_UNKNOWN);
WriteTrace(0x0a,"AddBufferToQueue: Exiting AddBufferToQueue routine with FALSE\n");
return(FALSE); // get out now
} // end switch for processing WaitForMultipleObjects
if (dwTrapQueueSize > MAX_QUEUE_SIZE)
{
WriteTrace(0x14,"AddBufferToQueue: queue too big -- posting notification event %08X\n",
hEventNotify);
if ( !SetEvent(hEventNotify) )
{
lastError = GetLastError(); // get error return codes
WriteTrace(0x14,"AddBufferToQueue: Unable to post event %08X; reason is %lu\n",
hEventNotify, lastError);
WriteLog(SNMPELEA_CANT_POST_NOTIFY_EVENT, HandleToUlong(hEventNotify), lastError);
}
else
{
if (!ReleaseMutex(hMutex))
{
lastError = GetLastError(); // get error information
WriteTrace(0x14,"AddBufferToQueue: Unable to release mutex object for reason code %lu\n",
lastError);
WriteLog(SNMPELEA_RELEASE_MUTEX_ERROR, lastError);
}
else
{
Sleep(1000); //try and let the other thread get the mutex
continue; //and try and get the mutex again...
}
}
}
break; // if we get here, then we've got the Mutex object
} // end while true for acquiring Mutex object
if (lpVarBindQueue == (PVarBindQueue) NULL)
{
dwTrapQueueSize = 1;
WriteTrace(0x0a,"AddBufferToQueue: Current queue is empty. Adding %08X as first queue entry\n",
lpVarBindEntry);
lpVarBindQueue = lpVarBindEntry; // indicate first in queue
WriteTrace(0x0a,"AddBufferToQueue: Releasing mutex object %08X\n", hMutex);
if (!ReleaseMutex(hMutex))
{
lastError = GetLastError(); // get error information
WriteTrace(0x14,"AddBufferToQueue: Unable to release mutex object for reason code %lu\n",
lastError);
WriteLog(SNMPELEA_RELEASE_MUTEX_ERROR, lastError);
}
WriteTrace(0x0a,"AddBufferToQueue: Exiting AddBufferToQueue function with TRUE\n");
return(TRUE); // show added to queue
}
WriteTrace(0x0a,"AddBufferToQueue: Queue is not empty. Scanning for end of queue.\n");
pBuffer = lpVarBindQueue; // starting point
while (pBuffer->lpNextQueueEntry != (PVarBindQueue) NULL)
{
WriteTrace(0x00,"AddBufferToQueue: This buffer address is %08X, next buffer pointer is %08X\n",
pBuffer, pBuffer->lpNextQueueEntry);
pBuffer = pBuffer->lpNextQueueEntry; // point to next buffer
}
WriteTrace(0x0a,"AddBufferToQueue: Adding buffer address %08X as next buffer pointer in %08X\n",
lpVarBindEntry, pBuffer);
pBuffer->lpNextQueueEntry = lpVarBindEntry; // add to end of chain
dwTrapQueueSize++;
WriteTrace(0x0a,"AddBufferToQueue: Releasing mutex object %08X\n", hMutex);
if (!ReleaseMutex(hMutex))
{
lastError = GetLastError(); // get error information
WriteTrace(0x14,"AddBufferToQueue: Unable to release mutex object for reason code %lu\n",
lastError);
WriteLog(SNMPELEA_RELEASE_MUTEX_ERROR, lastError);
}
WriteTrace(0x0a,"AddBufferToQueue: Exiting AddBufferToQueue function with TRUE\n");
return(TRUE); // show added to queue
}
HINSTANCE
AddSourceHandle(
IN LPTSTR lpszModuleName
)
{
PSourceHandleList pNewModule;
pNewModule = (PSourceHandleList) SNMP_malloc(sizeof(SourceHandleList));
if (pNewModule == NULL)
{
WriteTrace(0x14,"AddSourceHandle: Unable to acquire storage for source/handle entry.\n");
WriteLog(SNMPELEA_COUNT_TABLE_ALLOC_ERROR);
return NULL;
}
pNewModule->handle = NULL;
_tcscpy(pNewModule->sourcename, lpszModuleName);
// load the module as a data file; we look only for messages
pNewModule->handle = LoadLibraryEx(lpszModuleName, NULL, LOAD_LIBRARY_AS_DATAFILE);
// loading the module failed
if (pNewModule->handle == (HINSTANCE) NULL )
{
DWORD dwError = GetLastError();
WriteTrace(
0x14,
"AddSourceHandle: Unable to load message module %s; LoadLibraryEx returned %lu\n",
lpszModuleName,
dwError);
WriteLog(
SNMPELEA_CANT_LOAD_MSG_DLL,
lpszModuleName,
dwError);
WriteTrace(0x0a,"AddSourceHandle: Exiting AddSourceHandle with NULL.\n");
SNMP_free(pNewModule);
return NULL;
}
pNewModule->Next = lpSourceHandleList; // set forward pointer
lpSourceHandleList = pNewModule; //add item to list.
return pNewModule->handle;
}
HINSTANCE
FindSourceHandle(
IN LPTSTR lpszSource
)
{
PSourceHandleList lpSource;
if (lpSourceHandleList == (PSourceHandleList) NULL)
{
return ((HINSTANCE) NULL);
}
lpSource = lpSourceHandleList;
while (lpSource != (PSourceHandleList) NULL)
{
if (_tcscmp(lpszSource, lpSource->sourcename) == 0)
{
return (lpSource->handle);
}
lpSource = lpSource->Next;
}
return ((HINSTANCE) NULL);
}
VOID
ScanParameters(
IN OUT LPTSTR *lpStringArray, // pointer to array of insertion strings
IN UINT nNumStr, // number of insertion strings
IN OUT PUINT nStringsSize, // address of size of all insertion strings
IN LPTSTR lpszSrc, // pointer to source name for event
IN LPTSTR lpszLog, // pointer to the registry name for this source
IN HMODULE hPrimModule // handle to secondary message module DLL
)
/*++
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -