18,356
社区成员
发帖
与我相关
我的任务
分享
// MyAgent.cpp : Defines the entry point for the DLL application.
//
#include "stdafx.h"
#include <snmp.h>
#include <malloc.h>
#define OID_SIZEOF( Oid ) ( sizeof Oid / sizeof(UINT) )
// template MIB entry
struct MIB_ENTRY
{
AsnObjectIdentifier asnOid;
void * pStorageValue;
CHAR * szStorageName;
BYTE chType;
UINT unAccess;
MIB_ENTRY* pMibNext;
};
// this is the our branch starting point (clled prefix)
UINT g_unMyOIDPrefix[] = {1, 3, 6, 1, 4, 1, 15};
// this is the trap OID to send to client
UINT g_TrapOid[] = {1, 3, 6, 1, 4, 1, 15, 0};
AsnObjectIdentifier MIB_OidPrefix = { OID_SIZEOF(g_unMyOIDPrefix), g_unMyOIDPrefix};
DWORD g_dwStartTime = 0;
HANDLE g_hSimulateTrap = NULL;
HANDLE g_hTrapGenThread = NULL;
unsigned long __stdcall TrapGenThread(void *lpVoid);
int GetRequest(SnmpVarBind *pVarBind);
int GetNextRequest(SnmpVarBind *pVarBind);
int SetRequest(SnmpVarBind *pVarBind);
UINT GetStoreVar(MIB_ENTRY* pMIB, AsnAny *pasnValue);
UINT SetStoreVar(MIB_ENTRY* pMIB, AsnAny asnValue);
/////////MIB Table ////////////////////////////////////////////////
UINT g_unAboutOid[] = {0,0,1};
UINT g_unNameOid[] = {0,0,2};
UINT g_unAgeOid[] = {0,0,3};
char *g_szAbout = NULL;
char *g_szName = NULL;
AsnInteger g_asnIntAge = 0;
/// This is the MIB table and its related variable store
// here evry thing is hard-coded to demonstration perpose
// Actualy it should be loaded from the registry or from some file
MIB_ENTRY g_MyMibTable[] = {
{
{OID_SIZEOF(g_unAboutOid),g_unAboutOid},
&g_szAbout,
"About",
ASN_OCTETSTRING,
SNMP_ACCESS_READ_ONLY,
&g_MyMibTable[1]
},
{
{OID_SIZEOF(g_unNameOid),g_unNameOid},
&g_szName,
"Name",
ASN_OCTETSTRING,
SNMP_ACCESS_READ_WRITE,
&g_MyMibTable[2]
},
{
{OID_SIZEOF(g_unAgeOid),g_unAgeOid},
&g_asnIntAge,
"Age",
ASN_INTEGER,
SNMP_ACCESS_READ_WRITE,
NULL
}
};
UINT g_unMyMibCount = (sizeof(g_MyMibTable) / sizeof(MIB_ENTRY));
///////////////////////////////////////////////////////////////////
BOOL APIENTRY DllMain( HANDLE hModule,DWORD ul_reason_for_call, LPVOID lpReserved)
{
switch (ul_reason_for_call)
{
case DLL_PROCESS_ATTACH:
break;
case DLL_THREAD_ATTACH:
break;
case DLL_THREAD_DETACH:
break;
case DLL_PROCESS_DETACH:
TerminateThread(g_hTrapGenThread,0);
CloseHandle(g_hTrapGenThread);
CloseHandle(g_hSimulateTrap);
break;
}
return TRUE;
}
// When exported funtion will be called during DLL loading and initialization
BOOL SNMP_FUNC_TYPE SnmpExtensionInit(DWORD dwUptimeReference,HANDLE *phSubagentTrapEvent, AsnObjectIdentifier *pFirstSupportedRegion)
{
g_hSimulateTrap = CreateEvent(NULL, FALSE, FALSE, NULL); // creaet this event for the trap
*pFirstSupportedRegion = MIB_OidPrefix;
*phSubagentTrapEvent = g_hSimulateTrap; // by assigning it pass it to the SNMP service
// So when ever you set this event service will call
// SnmpExtensionTrap exported function
// on loading the our SNMP DLL create the thread
g_hTrapGenThread = CreateThread(NULL,0,TrapGenThread,NULL,0,NULL);
// hard coded initialization
g_szAbout = (char*)malloc(sizeof(char)*64);
strcpy(g_szAbout,"Author : Ramanan.T");
g_szName = (char*)malloc(sizeof(char)*64);
strcpy(g_szName,"Your Name");
g_asnIntAge = 0;
g_dwStartTime = GetTickCount();
return SNMPAPI_NOERROR;
}
// this export is to query the MIB table and fields
BOOL SNMP_FUNC_TYPE SnmpExtensionQuery(BYTE bPduType, SnmpVarBindList *pVarBindList, AsnInteger32 *pErrorStatus, AsnInteger32 *pErrorIndex)
{
int nRet = 0;
AsnObjectName;
*pErrorStatus = SNMP_ERRORSTATUS_NOERROR;
*pErrorIndex = 0;
for(UINT i=0;i<pVarBindList->len;i++)
{
*pErrorStatus = SNMP_ERRORSTATUS_NOERROR;
// what type of request we are getting?
switch(bPduType)
{
case SNMP_PDU_GET:// // gets the variable value passed variable in pVarBindList
*pErrorStatus = GetRequest(&pVarBindList->list[i]);
if(*pErrorStatus != SNMP_ERRORSTATUS_NOERROR)
*pErrorIndex++;
break;
case SNMP_PDU_GETNEXT: // gets the next variable related to the passed variable in pVarBindList
*pErrorStatus = GetNextRequest(&pVarBindList->list[i]);
if(*pErrorStatus != SNMP_ERRORSTATUS_NOERROR)
*pErrorIndex++;
break;
case SNMP_PDU_SET: // sets a variable
*pErrorStatus = SetRequest(&pVarBindList->list[i]);
if(*pErrorStatus != SNMP_ERRORSTATUS_NOERROR)
*pErrorIndex++;
break;
default:
*pErrorStatus = SNMP_ERRORSTATUS_NOSUCHNAME;
*pErrorIndex++;
};
}
return SNMPAPI_NOERROR;
}
// this function just simulate traps
// Traps just a 2 variables value from MIB
// Trap is kind of event from server to client
/// When ever the event is set service will call this function and gets the parameters filled.
// After filling the parameters service willsend the trap to all the client connected
BOOL SNMP_FUNC_TYPE SnmpExtensionTrap(AsnObjectIdentifier *pEnterpriseOid, AsnInteger32 *pGenericTrapId, AsnInteger32 *pSpecificTrapId, AsnTimeticks *pTimeStamp, SnmpVarBindList *pVarBindList)
{
static int nNoOfTraps = 1; // just ignore this, I introduced this to send meny traps at once
// any way below we are generating one trap with two values
if(nNoOfTraps) // if it is zero don't send traps
{
pEnterpriseOid->idLength = sizeof(g_TrapOid);
pEnterpriseOid->ids = g_TrapOid;
*pGenericTrapId = SNMP_GENERICTRAP_ENTERSPECIFIC;
*pSpecificTrapId = 1; // ToasterControl Up trap.
*pTimeStamp = GetTickCount() - g_dwStartTime;
// Allocate space for the Variable Bindings.
pVarBindList->list = (SnmpVarBind*)SnmpUtilMemAlloc(2*sizeof(SnmpVarBind));
SnmpUtilOidCpy(&pVarBindList->list[0].name,&MIB_OidPrefix);
SnmpUtilOidAppend(&pVarBindList->list[0].name,&g_MyMibTable[1].asnOid);
pVarBindList->list[0].value.asnType = ASN_OCTETSTRING;
pVarBindList->list[0].value.asnValue.string.dynamic = TRUE;
pVarBindList->list[0].value.asnValue.string.length = strlen(*(LPSTR*)g_MyMibTable[1].pStorageValue);
pVarBindList->list[0].value.asnValue.string.stream =(unsigned char*)SnmpUtilMemAlloc(pVarBindList->list[0].value.asnValue.string.length * sizeof(char));
memcpy(pVarBindList->list[0].value.asnValue.string.stream,*(LPSTR*)g_MyMibTable[1].pStorageValue,pVarBindList->list[0].value.asnValue.string.length);
SnmpUtilOidCpy(&pVarBindList->list[1].name,&MIB_OidPrefix);
SnmpUtilOidAppend(&pVarBindList->list[1].name,&g_MyMibTable[2].asnOid);
pVarBindList->list[1].value.asnType = ASN_INTEGER;
pVarBindList->list[1].value.asnValue.number = *((AsnInteger32*)g_MyMibTable[2].pStorageValue);
pVarBindList->len = 2;
nNoOfTraps--;
// Indicate that valid trap data exists in the parameters.
return TRUE;
}
nNoOfTraps = 1;
// Indicate that no more traps are available, and parameters do not refer to any valid data
return FALSE;
}
// When exported funtion will be called during DLL loading and initialization
BOOL SNMP_FUNC_TYPE SnmpExtensionInit(DWORD dwUptimeReference,HANDLE *phSubagentTrapEvent, AsnObjectIdentifier *pFirstSupportedRegion)
{
g_hSimulateTrap = CreateEvent(NULL, FALSE, FALSE, NULL); // creaet this event for the trap
*pFirstSupportedRegion = MIB_OidPrefix;
*phSubagentTrapEvent = g_hSimulateTrap; // by assigning it pass it to the SNMP service
// So when ever you set this event service will call
// SnmpExtensionTrap exported function
// on loading the our SNMP DLL create the thread
g_hTrapGenThread = CreateThread(NULL,0,TrapGenThread,NULL,0,NULL);
// hard coded initialization
g_szAbout = (char*)malloc(sizeof(char)*64);
strcpy(g_szAbout,"Author : Ramanan.T");
g_szName = (char*)malloc(sizeof(char)*64);
strcpy(g_szName,"Your Name");
g_asnIntAge = 0;
g_dwStartTime = GetTickCount();
return SNMPAPI_NOERROR;
}
// this export is to query the MIB table and fields
BOOL SNMP_FUNC_TYPE SnmpExtensionQuery(BYTE bPduType, SnmpVarBindList *pVarBindList, AsnInteger32 *pErrorStatus, AsnInteger32 *pErrorIndex)
{
int nRet = 0;
AsnObjectName;
*pErrorStatus = SNMP_ERRORSTATUS_NOERROR;
*pErrorIndex = 0;
for(UINT i=0;i<pVarBindList->len;i++)
{
*pErrorStatus = SNMP_ERRORSTATUS_NOERROR;
// what type of request we are getting?
switch(bPduType)
{
case SNMP_PDU_GET:// // gets the variable value passed variable in pVarBindList
*pErrorStatus = GetRequest(&pVarBindList->list[i]);
if(*pErrorStatus != SNMP_ERRORSTATUS_NOERROR)
*pErrorIndex++;
break;
case SNMP_PDU_GETNEXT: // gets the next variable related to the passed variable in pVarBindList
*pErrorStatus = GetNextRequest(&pVarBindList->list[i]);
if(*pErrorStatus != SNMP_ERRORSTATUS_NOERROR)
*pErrorIndex++;
break;
case SNMP_PDU_SET: // sets a variable
*pErrorStatus = SetRequest(&pVarBindList->list[i]);
if(*pErrorStatus != SNMP_ERRORSTATUS_NOERROR)
*pErrorIndex++;
break;
default:
*pErrorStatus = SNMP_ERRORSTATUS_NOSUCHNAME;
*pErrorIndex++;
};
}
return SNMPAPI_NOERROR;
}
// this function just simulate traps
// Traps just a 2 variables value from MIB
// Trap is kind of event from server to client
/// When ever the event is set service will call this function and gets the parameters filled.
// After filling the parameters service willsend the trap to all the client connected
BOOL SNMP_FUNC_TYPE SnmpExtensionTrap(AsnObjectIdentifier *pEnterpriseOid, AsnInteger32 *pGenericTrapId, AsnInteger32 *pSpecificTrapId, AsnTimeticks *pTimeStamp, SnmpVarBindList *pVarBindList)
{
static int nNoOfTraps = 1; // just ignore this, I introduced this to send meny traps at once
// any way below we are generating one trap with two values
if(nNoOfTraps) // if it is zero don't send traps
{
pEnterpriseOid->idLength = sizeof(g_TrapOid);
pEnterpriseOid->ids = g_TrapOid;
*pGenericTrapId = SNMP_GENERICTRAP_ENTERSPECIFIC;
*pSpecificTrapId = 1; // ToasterControl Up trap.
*pTimeStamp = GetTickCount() - g_dwStartTime;
// Allocate space for the Variable Bindings.
pVarBindList->list = (SnmpVarBind*)SnmpUtilMemAlloc(2*sizeof(SnmpVarBind));
SnmpUtilOidCpy(&pVarBindList->list[0].name,&MIB_OidPrefix);
SnmpUtilOidAppend(&pVarBindList->list[0].name,&g_MyMibTable[1].asnOid);
pVarBindList->list[0].value.asnType = ASN_OCTETSTRING;
pVarBindList->list[0].value.asnValue.string.dynamic = TRUE;
pVarBindList->list[0].value.asnValue.string.length = strlen(*(LPSTR*)g_MyMibTable[1].pStorageValue);
pVarBindList->list[0].value.asnValue.string.stream =(unsigned char*)SnmpUtilMemAlloc(pVarBindList->list[0].value.asnValue.string.length * sizeof(char));
memcpy(pVarBindList->list[0].value.asnValue.string.stream,*(LPSTR*)g_MyMibTable[1].pStorageValue,pVarBindList->list[0].value.asnValue.string.length);
SnmpUtilOidCpy(&pVarBindList->list[1].name,&MIB_OidPrefix);
SnmpUtilOidAppend(&pVarBindList->list[1].name,&g_MyMibTable[2].asnOid);
pVarBindList->list[1].value.asnType = ASN_INTEGER;
pVarBindList->list[1].value.asnValue.number = *((AsnInteger32*)g_MyMibTable[2].pStorageValue);
pVarBindList->len = 2;
nNoOfTraps--;
// Indicate that valid trap data exists in the parameters.
return TRUE;
}
nNoOfTraps = 1;
// Indicate that no more traps are available, and parameters do not refer to any valid data
return FALSE;
}
// MyAgent.cpp : Defines the entry point for the DLL application.
//
#include "stdafx.h"
#include <snmp.h>
#include <malloc.h>
#define OID_SIZEOF( Oid ) ( sizeof Oid / sizeof(UINT) )
// template MIB entry
struct MIB_ENTRY
{
AsnObjectIdentifier asnOid;
void * pStorageValue;
CHAR * szStorageName;
BYTE chType;
UINT unAccess;
MIB_ENTRY* pMibNext;
};
// this is the our branch starting point (clled prefix)
UINT g_unMyOIDPrefix[] = {1, 3, 6, 1, 4, 1, 15};
// this is the trap OID to send to client
UINT g_TrapOid[] = {1, 3, 6, 1, 4, 1, 15, 0};
AsnObjectIdentifier MIB_OidPrefix = { OID_SIZEOF(g_unMyOIDPrefix), g_unMyOIDPrefix};
DWORD g_dwStartTime = 0;
HANDLE g_hSimulateTrap = NULL;
HANDLE g_hTrapGenThread = NULL;
unsigned long __stdcall TrapGenThread(void *lpVoid);
int GetRequest(SnmpVarBind *pVarBind);
int GetNextRequest(SnmpVarBind *pVarBind);
int SetRequest(SnmpVarBind *pVarBind);
UINT GetStoreVar(MIB_ENTRY* pMIB, AsnAny *pasnValue);
UINT SetStoreVar(MIB_ENTRY* pMIB, AsnAny asnValue);
/////////MIB Table ////////////////////////////////////////////////
UINT g_unAboutOid[] = {0,0,1};
UINT g_unNameOid[] = {0,0,2};
UINT g_unAgeOid[] = {0,0,3};
char *g_szAbout = NULL;
char *g_szName = NULL;
AsnInteger g_asnIntAge = 0;
/// This is the MIB table and its related variable store
// here evry thing is hard-coded to demonstration perpose
// Actualy it should be loaded from the registry or from some file
MIB_ENTRY g_MyMibTable[] = {
{
{OID_SIZEOF(g_unAboutOid),g_unAboutOid},
&g_szAbout,
"About",
ASN_OCTETSTRING,
SNMP_ACCESS_READ_ONLY,
&g_MyMibTable[1]
},
{
{OID_SIZEOF(g_unNameOid),g_unNameOid},
&g_szName,
"Name",
ASN_OCTETSTRING,
SNMP_ACCESS_READ_WRITE,
&g_MyMibTable[2]
},
{
{OID_SIZEOF(g_unAgeOid),g_unAgeOid},
&g_asnIntAge,
"Age",
ASN_INTEGER,
SNMP_ACCESS_READ_WRITE,
NULL
}
};
UINT g_unMyMibCount = (sizeof(g_MyMibTable) / sizeof(MIB_ENTRY));
///////////////////////////////////////////////////////////////////
BOOL APIENTRY DllMain( HANDLE hModule,DWORD ul_reason_for_call, LPVOID lpReserved)
{
switch (ul_reason_for_call)
{
case DLL_PROCESS_ATTACH:
break;
case DLL_THREAD_ATTACH:
break;
case DLL_THREAD_DETACH:
break;
case DLL_PROCESS_DETACH:
TerminateThread(g_hTrapGenThread,0);
CloseHandle(g_hTrapGenThread);
CloseHandle(g_hSimulateTrap);
break;
}
return TRUE;
}