📄 winextdll.c
字号:
/* * winExtDLL Net-SNMP extension * (c) 2006 Alex Burger * * Created 9/9/06 * * Purpose: To load Windows SNMP Service extension DLLs provided with Windows * (such as hostmib.dll). This allows Net-SNMP to be a replacement * for the Windows SNMP service. * * Notes: This extension requires the PSDK including the Snmp.h header file. * Including Snmp.h will conflict with existing Net-SNMP defines for * ASN_OCTETSTRING etc. To resolve this, create a copy of Snmp.h in * the PSDK include/ folder called Snmp-winExtDLL.h and change all * occurances of ASN_ to MS_ASN_ * * This extension requires that the Windows SNMP Service is installed * but set to disabled. This is required so that the extension DLLs * are available for loading, and also because this extension and the * existing Windows extensions use the Windows SNMP API from snmpapi.dll. * * This extension is NOT for dynamically loading Net-SNMP extensions. */#include <windows.h>#include <cstdio>#include <Snmp-winExtDLL.h> // Modified Windows SDK snmp.h. See Notes above#include <mgmtapi.h>#include <string.h>/* * include important headers */#include <net-snmp/net-snmp-config.h>#if HAVE_STDLIB_H#include <stdlib.h>#endif#if HAVE_STRING_H#include <string.h>#else#include <strings.h>#endif/* * needed by util_funcs.h */#if TIME_WITH_SYS_TIME# ifdef WIN32# include <sys/timeb.h># else# include <sys/time.h># endif# include <time.h>#else# if HAVE_SYS_TIME_H# include <sys/time.h># else# include <time.h># endif#endif#if HAVE_WINSOCK_H#include <winsock.h>#endif#if HAVE_NETINET_IN_H#include <netinet/in.h>#endif#include <net-snmp/net-snmp-includes.h>#include <net-snmp/agent/net-snmp-agent-includes.h>#include "util_funcs.h"#include "winExtDLL.h"#define SZBUF_MAX 1024#define SZBUF_DLLNAME_MAX 254#define MAX_WINEXT_DLLS 100#define MAX_KEY_LENGTH 255#define MAX_VALUE_NAME 16383#define MAX_WINEXT_TRAP_EVENTS 100#define DEBUGMSGWINOID(x) do {if (_DBG_IF_) {__DBGMSGWINOID(x);} }while(0)#define __DBGMSGWINOID(x) debugmsg_win_oid xvoid debugmsg_win_oid(const char *token, const AsnObjectIdentifier * theoid);/* Structure to hold name, pointers to functions and MIB tree supported by * each Windows SNMP Extension DLL */typedef struct { char dll_name[SZBUF_DLLNAME_MAX]; DWORD (WINAPI *xSnmpExtensionInit)(DWORD, HANDLE*, AsnObjectIdentifier*); DWORD (WINAPI *xSnmpExtensionInitEx)(AsnObjectIdentifier*); DWORD (WINAPI *xSnmpExtensionQuery)(BYTE, SnmpVarBindList* ,AsnInteger32* ,AsnInteger32*); DWORD (WINAPI *xSnmpExtensionQueryEx)(DWORD, DWORD, SnmpVarBindList*, AsnOctetString*, AsnInteger32*, AsnInteger32*); BOOL (WINAPI *xSnmpExtensionTrap)( AsnObjectIdentifier *, AsnInteger *, AsnInteger *, AsnTimeticks *, SnmpVarBindList * ); HANDLE *subagentTrapEvent; netsnmp_handler_registration *my_handler; oid name[MAX_OID_LEN]; // pSupportedView in Net-SNMP format size_t name_length; AsnObjectIdentifier pSupportedView;} winExtensionAgents;winExtensionAgents winExtensionAgent[MAX_WINEXT_DLLS];winExtensionAgents winExtensionAgent_temp; /* For sorting */int winExtensionAgent_index = 0;char *extDLLs[MAX_WINEXT_DLLS];int extDLLs_index = 0;HANDLE *subagentTrapEvents[MAX_WINEXT_TRAP_EVENTS];int subagentTrapEvents_index = 0;void winExtDLL_free_config_winExtDLL(void);void read_ExtensionAgents_list();void read_ExtensionAgents_list2(const TCHAR *);void subagentTrapCheck();void send_trap( AsnObjectIdentifier *, AsnInteger *, AsnInteger *, AsnTimeticks *, SnmpVarBindList *);void init_winExtDLL(void){ // Windows SNMP DWORD dwUptimeReference = 0; HANDLE subagentTrapEvent; AsnObjectIdentifier pSupportedView; BOOL result; HANDLE hThread; DWORD IDThread; char dll_name[SZBUF_DLLNAME_MAX]; DWORD (WINAPI *xSnmpExtensionInit)(DWORD, HANDLE*, AsnObjectIdentifier*); DWORD (WINAPI *xSnmpExtensionInitEx)(AsnObjectIdentifier*); DWORD (WINAPI *xSnmpExtensionQuery)(BYTE, SnmpVarBindList* ,AsnInteger32* ,AsnInteger32*); DWORD (WINAPI *xSnmpExtensionQueryEx)(DWORD, DWORD, SnmpVarBindList*, AsnOctetString*, AsnInteger32*, AsnInteger32*); BOOL (WINAPI *xSnmpExtensionTrap)( AsnObjectIdentifier *, AsnInteger *, AsnInteger *, AsnTimeticks *, SnmpVarBindList * ); // Net-SNMP oid name[MAX_OID_LEN]; size_t length = 0; int i; int DLLnum = 0; int winExtensionAgent_num = 0; int iter, indx; netsnmp_handler_registration *my_handler; HANDLE hInst = NULL; DEBUGMSGTL(("winExtDLL", "init_winExtDLL called\n")); read_ExtensionAgents_list(); DEBUGMSGTL(("winExtDLL", "winExtDLL enabled.\n")); DEBUGMSGTL(("winExtDLL", "Size of winExtensionAgent: %d\n",sizeof(winExtensionAgent) / sizeof(winExtensionAgents))); for(i=0; i <= sizeof(winExtensionAgent) / sizeof(winExtensionAgents); i++) { winExtensionAgent[0].xSnmpExtensionInit = NULL; winExtensionAgent[0].xSnmpExtensionInitEx = NULL; } /* Load all the DLLs */ for (DLLnum = 0; DLLnum <= extDLLs_index; DLLnum++) { if (! (extDLLs[DLLnum])) continue; DEBUGMSGTL(("winExtDLL", "-----------------------------------------\n")); DEBUGMSGTL(("winExtDLL", "DLL to load: %s, DLL number: %d, winExtensionAgent_num: %d\n", extDLLs[DLLnum], DLLnum, winExtensionAgent_num)); DEBUGMSGTL(("winExtDLL", "Size of DLL to load: %d\n", strlen(extDLLs[DLLnum]))); hInst = LoadLibrary(extDLLs[DLLnum]); if (hInst == NULL) { DEBUGMSGTL(("winExtDLL","Could not load Windows extension DLL %s.\n", extDLLs[DLLnum])); snmp_log(LOG_ERR, "Could not load Windows extension DLL: %s.\n", extDLLs[DLLnum]); continue; } else { DEBUGMSGTL(("winExtDLL","DLL loaded.\n")); } // Create local copy of DLL name and functions strncpy(dll_name, extDLLs[DLLnum], SZBUF_DLLNAME_MAX-1); xSnmpExtensionInit = (DWORD (WINAPI *)(DWORD, HANDLE*, AsnObjectIdentifier*)) GetProcAddress ((HMODULE) hInst, "SnmpExtensionInit"); xSnmpExtensionInitEx = (DWORD (WINAPI *)(AsnObjectIdentifier*)) GetProcAddress ((HMODULE) hInst, "SnmpExtensionInitEx"); xSnmpExtensionQuery = (DWORD (WINAPI *)(BYTE, SnmpVarBindList* ,AsnInteger32* ,AsnInteger32*)) GetProcAddress ((HMODULE) hInst, "SnmpExtensionQuery"); xSnmpExtensionQueryEx = (DWORD (WINAPI *)(DWORD, DWORD, SnmpVarBindList*, AsnOctetString*, AsnInteger32*, AsnInteger32*)) GetProcAddress ((HMODULE) hInst, "SnmpExtensionQueryEx"); xSnmpExtensionTrap = (BOOL (WINAPI *)(AsnObjectIdentifier *, AsnInteger *, AsnInteger *, AsnTimeticks *, SnmpVarBindList * )) GetProcAddress ((HMODULE) hInst, "SnmpExtensionTrap"); if (xSnmpExtensionQuery) DEBUGMSGTL(("winExtDLL", "xSnmpExtensionQuery found\n")); if (xSnmpExtensionQueryEx) DEBUGMSGTL(("winExtDLL", "xSnmpExtensionQueryEx found\n")); if (xSnmpExtensionQuery) DEBUGMSGTL(("winExtDLL", "xSnmpExtensionTrap found\n")); // Store DLL name and functions in winExtensionAgent array strncpy(winExtensionAgent[winExtensionAgent_num].dll_name, dll_name, SZBUF_DLLNAME_MAX-1); winExtensionAgent[winExtensionAgent_num].xSnmpExtensionInit = xSnmpExtensionInit; winExtensionAgent[winExtensionAgent_num].xSnmpExtensionInitEx = xSnmpExtensionInitEx; winExtensionAgent[winExtensionAgent_num].xSnmpExtensionQuery = xSnmpExtensionQuery; winExtensionAgent[winExtensionAgent_num].xSnmpExtensionQueryEx = xSnmpExtensionQueryEx; winExtensionAgent[winExtensionAgent_num].xSnmpExtensionTrap = xSnmpExtensionTrap; // Init and get first supported view from Windows SNMP extension DLL result = xSnmpExtensionInit(dwUptimeReference, &subagentTrapEvent, &pSupportedView); DEBUGMSGTL(("winExtDLL", "Supported view: ")); DEBUGMSGWINOID(("winExtDLL", &pSupportedView)); DEBUGMSG(("winExtDLL", "\n")); // Store the subagent's trap handler, even if it's NULL winExtensionAgent[winExtensionAgent_num].subagentTrapEvent = subagentTrapEvent; // Store the subagent's trap handler in a global array for use by waitformultipleobjects() if (subagentTrapEvent) { DEBUGMSGTL(("winExtDLL", "Trap handler defined. Storing...\n")); subagentTrapEvents[subagentTrapEvents_index] = subagentTrapEvent; subagentTrapEvents_index++; } // Convert OID from Windows 'supported view' to Net-SNMP for (i = 0; i < (pSupportedView.idLength > MAX_OID_LEN?MAX_OID_LEN:pSupportedView.idLength); i++) { name[i] = (oid)pSupportedView.ids[i]; } length = i; // Store supported view in Net-SNMP format memcpy(winExtensionAgent[winExtensionAgent_num].name, name, sizeof(name)); winExtensionAgent[winExtensionAgent_num].name_length = length; DEBUGMSGTL(("winExtDLL", "Windows OID converted to Net-SNMP: ")); DEBUGMSGOID(("winExtDLL", name, length)); DEBUGMSG(("winExtDLL", "\n")); // Store supported view in Windows format SnmpUtilOidCpy(&winExtensionAgent[winExtensionAgent_num].pSupportedView,&pSupportedView); // Create handler registration winExtensionAgent[winExtensionAgent_num].my_handler = netsnmp_create_handler_registration("winExtDLL", var_winExtDLL, name, length, HANDLER_CAN_RWRITE); if (!winExtensionAgent[winExtensionAgent_num].my_handler) { snmp_log(LOG_ERR, "malloc failed registering handler for winExtDLL"); DEBUGMSGTL(("winExtDLL", "malloc failed registering handler for winExtDLL")); return (-1); } else { DEBUGMSGTL(("winExtDLL", "handler registered\n")); } // Register handler with Net-SNMP netsnmp_register_handler(winExtensionAgent[winExtensionAgent_num].my_handler); // Check for additional supported views and register them with the same handler if (winExtensionAgent[winExtensionAgent_num].xSnmpExtensionInitEx) { DEBUGMSGTL(("winExtDLL", "xSnmpExtensionInitEx found\n")); winExtensionAgent_num++; while (1) { // Loop looking for more supported views // Store DLL name and functions in winExtensionAgent array strncpy(winExtensionAgent[winExtensionAgent_num].dll_name, dll_name, SZBUF_DLLNAME_MAX-1); winExtensionAgent[winExtensionAgent_num].xSnmpExtensionInit = xSnmpExtensionInit; winExtensionAgent[winExtensionAgent_num].xSnmpExtensionInitEx = xSnmpExtensionInitEx; winExtensionAgent[winExtensionAgent_num].xSnmpExtensionQuery = xSnmpExtensionQuery; winExtensionAgent[winExtensionAgent_num].xSnmpExtensionQueryEx = xSnmpExtensionQueryEx; winExtensionAgent[winExtensionAgent_num].xSnmpExtensionTrap = xSnmpExtensionTrap; winExtensionAgent[winExtensionAgent_num].subagentTrapEvent = NULL; // Get extra supported view result = xSnmpExtensionInitEx(&pSupportedView); if (! (result)) { winExtensionAgent_num--; break; } DEBUGMSGTL(("winExtDLL", "result of xSnmpExtensionInitEx: %d\n",result)); DEBUGMSGTL(("winExtDLL", "Supported view: ")); DEBUGMSGWINOID(("winExtDLL", &pSupportedView)); DEBUGMSG(("winExtDLL", "\n")); // Convert OID from Windows 'supported view' to Net-SNMP for (i = 0; i < (pSupportedView.idLength > MAX_OID_LEN?MAX_OID_LEN:pSupportedView.idLength); i++) { name[i] = (oid)pSupportedView.ids[i]; } length = i; // Store supported view in Net-SNMP format memcpy(winExtensionAgent[winExtensionAgent_num].name, name, sizeof(name)); winExtensionAgent[winExtensionAgent_num].name_length = length; DEBUGMSGTL(("winExtDLL", "Windows OID converted to Net-SNMP: ")); DEBUGMSGOID(("winExtDLL", name, length)); DEBUGMSG(("winExtDLL", "\n")); // Store supported view in Windows format SnmpUtilOidCpy(&winExtensionAgent[winExtensionAgent_num].pSupportedView,&pSupportedView); // Create handler registration
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -