コード例 #1
0
SNMPRemoteAgent::SNMPRemoteAgent(std::string name, std::string community, unsigned int port, SNMPSession * pSession) {
	_name = name;
	_port = port;
	_community = community;
	_bInit = false;
	_mySession = false;
	_pSession = pSession;
	_hEntity = SNMPAPI_FAILURE;
	_hContext = SNMPAPI_FAILURE;
	if(_pSession == NULL) {
		try {
			_pSession = new SNMPSession();
		} catch(SNMPException * pe) {
			SNMP_RETHROW_ERROR("Could not create WinSNMP session", pe, SNMPAPI_OTHER_ERROR, SNMP_ERROR);
		}
		_mySession = true;
	}
	HSNMP_SESSION hSession = _pSession->getHandle();
	if(hSession == SNMPAPI_FAILURE) {
		if(_mySession && _pSession != NULL) {
			delete _pSession;
		}
		_pSession = NULL;		
		SNMP_THROW_ERROR("Invalid session", SnmpGetLastError(NULL), SNMP_ERROR);
	}
	_hEntity = SnmpStrToEntity(hSession, _name.c_str());
	if (_hEntity == SNMPAPI_FAILURE) {
		LPHOSTENT lpHostent = gethostbyname(_name.c_str());
		IN_ADDR host;
		if(!lpHostent) {
			if(_mySession && _pSession != NULL) {
				delete _pSession;
			}
			_pSession = NULL;
			SNMP_THROW_ERROR("Could not get host by name", SNMPAPI_OTHER_ERROR, SNMP_ERROR);
		}
		memmove (&host, lpHostent->h_addr, sizeof(IN_ADDR));
		_hEntity = SnmpStrToEntity(hSession, inet_ntoa(host));
		if (_hEntity == SNMPAPI_FAILURE) {
			if(_mySession && _pSession != NULL) {
				delete _pSession;
			}
			_pSession = NULL;
			SNMP_THROW_ERROR("Could not convert remote agent address to entity", SnmpGetLastError(hSession), SNMP_ERROR);
		}
	}
	if(port != 0) {
		if(SnmpSetPort(_hEntity, port) != SNMPAPI_SUCCESS) {
			SnmpFreeEntity(_hEntity);
			_hEntity = SNMPAPI_FAILURE;
			if(_mySession && _pSession != NULL) {
				delete _pSession;
			}
			_pSession = NULL;
			SNMP_THROW_ERROR("Could not set remote agent port", SnmpGetLastError(hSession), SNMP_ERROR);
		}
	}
	smiOCTETS dCtx;
	dCtx.len = (smiUINT32)strlen(community.c_str());
	dCtx.ptr = (smiLPBYTE)community.c_str();
	_hContext = SnmpStrToContext(hSession, &dCtx);
	if (_hContext == SNMPAPI_FAILURE)  {
		SnmpFreeEntity(_hEntity);
		_hEntity = SNMPAPI_FAILURE;
		if(_mySession && _pSession != NULL) {
			delete _pSession;
		}
		_pSession = NULL;
		SNMP_THROW_ERROR("Could not convert community name to context", SnmpGetLastError(hSession), SNMP_ERROR);
	}
	_pSession->registerRemoteAgent(this);
	_bInit = true;
}
コード例 #2
0
int _tmain(int argc, _TCHAR* argv[])
{

    unsigned long majorVal = 1;
    unsigned long *major = &majorVal;
    unsigned long minorVal = 1;
    unsigned long *minor = &minorVal;
    unsigned long levelVal = 1;
    unsigned long *level = &levelVal;
    unsigned long translateModeVal = 1;
    unsigned long *translateMode = &translateModeVal;
    unsigned long retransmitModeVal = 1;
    unsigned long *retransmitMode = &retransmitModeVal;
    SnmpStartup(major, minor, level, translateMode, retransmitMode);
    HWND hsession;
    HSNMP_ENTITY entDest;
    char destIP[] = "192.168.1.1";
    HSNMP_ENTITY entSrc;
    char srcIP[] = "192.168.1.2";
    HSNMP_CONTEXT hContext;
    char Ctx[]="public";
    SNMPAPI_STATUS status = 0;
    LPCSTR string = "entityName";
    UINT msg;
    LPHSNMP_PDU PDU = 0;
    LPHSNMP_VBL varbind_lst;
    LPHSNMP_CONTEXT context;
    smiLPINT PDU_typ, r_id, error_stat, error_index;
    smiOCTETS dContext;
    smiOID dName;
    smiVALUE dValue;
    SNMP_worker worker;

    HSNMP_SESSION session = SnmpCreateSession(NULL, 0, &SNcallback, (void*)&worker);

    entDest = SnmpStrToEntity(session, destIP);
    entSrc  = SnmpStrToEntity(session, srcIP);
    dContext.ptr=(smiLPBYTE)Ctx;
    dContext.len=strlen(Ctx);
    hContext= SnmpStrToContext(session,&dContext);
    printf("entity return: %i\n", entDest);
    status = SnmpListen(entDest, status);
    printf("listen return: %i\n", status);
    while (status = SnmpRecvMsg(session, &entSrc, &entDest, context, PDU) ) {
        printf("recv return: %i\n", status);
        printf("PDU: %s\n", PDU);
    };
    SnmpGetPduData(PDU, PDU_typ, r_id, error_stat, error_index, varbind_lst);
    printf("%lu, %lu\n", *major, *minor);
    printf("%p, %p\n", (void *)major, (void *)minor);
    /*
    char *ip = "192.168.1.174";
    char *community = "public";
    LPSNMP_MGR_SESSION ptr;
    ptr = SnmpMgrOpen(ip, community, 10000, 3);
    SnmpRecvMsg(
    SnmpMgrClose(ptr);
    */

    SnmpCleanup();
    return 0;
}