Пример #1
1
SNMPSession::SNMPSession() {
	_hSession = SnmpCreateSession (0, 0, snmpCallBackFunction, this);
	if(SNMPAPI_FAILURE == _hSession) {
#ifdef SMNP_TRACER
		SNMP_THROW_ERROR("Could not create WinSNMP session", SnmpGetLastError(NULL), SNMP_ERROR);
#endif
	}
	SNMPManager::getManager()->registerSession(this);
}
Пример #2
0
int SNMPObject::compareOID(SNMPObject * pOther) const
{
	if(!_bInit || !pOther->_bInit) {
		SNMP_THROW_ERROR("SNMP object OID(s) not initialized", SNMPAPI_OTHER_ERROR, SNMP_ERROR);
	}
	smiINT res = 0;
	if(SnmpOidCompare(&_oid, &(pOther->_oid), 0, &res) != SNMPAPI_SUCCESS) {
		SNMP_THROW_ERROR("Could not compare OIDs", SnmpGetLastError(NULL), SNMP_ERROR);
	}
	return (int)res;
}
Пример #3
0
void SNMPRemoteAgent::sendRequest(SNMPRequest * pRequest) {
    if(!_bInit) {
        pRequest->setResult(SNMP_ERROR_GENERR);
#ifdef SMNP_TRACER
        SNMP_THROW_ERROR("Not initialized", SNMPAPI_OTHER_ERROR, SNMP_ERROR);
#endif
    }
    HSNMP_PDU hPDU = pRequest->_hPdu;
    int id = pRequest->_requestId;
    addToRequestMap(id, pRequest);
#ifdef SMNP_TRACER
    if(SNMPTRACEAENABLED()) {
        std::stringstream trace;
        smiINT32 lReqId, lType, lErr, lIdx;
        HSNMP_VBL hVbl;
        SnmpGetPduData(hPDU, &lType, &lReqId, &lErr, &lIdx, &hVbl);
        trace << "tx " << SNMPManager::pduToStr(_hEntity, 0, _hContext, lType, lReqId, lErr, hVbl, true);
        SnmpFreeVbl(hVbl);
        SNMPTRACE(trace.str());
    }
#endif
    if(SnmpSendMsg(_pSession->getHandle(), 0, _hEntity, _hContext, hPDU) == SNMPAPI_FAILURE) {
        pRequest->setResult(SNMP_ERROR_GENERR);
        //SNMP_THROW_ERROR("Could not send PDU", SnmpGetLastError(_pSession->getHandle()), SNMP_ERROR);
    }
}
Пример #4
0
int SNMPObject::compareOID(std::string oid) const
{
	if(!_bInit) {
		SNMP_THROW_ERROR("SNMP object OID not initialized", SNMPAPI_OTHER_ERROR, SNMP_ERROR);
		return 0;
	}
	smiOID otherOid;
	if(SnmpStrToOid(oid.c_str(), &otherOid) == SNMPAPI_FAILURE) {
		SNMP_THROW_ERROR("Could not convert OID from string representation", SnmpGetLastError(NULL), SNMP_ERROR);
	}
	smiINT res = 0;
	if(SnmpOidCompare(&_oid, &otherOid, 0, &res) != SNMPAPI_SUCCESS) {
		SnmpFreeDescriptor(SNMP_SYNTAX_OCTETS, (smiLPOPAQUE)&otherOid);
		SNMP_THROW_ERROR("Could not compare OIDs", SnmpGetLastError(NULL), SNMP_ERROR);
	}
	SnmpFreeDescriptor(SNMP_SYNTAX_OCTETS, (smiLPOPAQUE)&otherOid);
	return (int)res;
}
Пример #5
0
std::string	SNMPObject::getOID() const {
	if(!_bInit) {
		return "";
	}
	char szName[MAXOBJIDSTRSIZE+1];
	szName[0] = szName[MAXOBJIDSTRSIZE] = '\0';
	if(SnmpOidToStr (&_oid, sizeof(szName), szName) == SNMPAPI_FAILURE) {
		SNMP_THROW_ERROR("Failed to convert OID to string", SnmpGetLastError(NULL), SNMP_ERROR);
	}
	return std::string(szName);
}
Пример #6
0
void SNMPObject::setOID(std::string oid) {	
	if(_bInit) {
		SnmpFreeDescriptor(SNMP_SYNTAX_OCTETS, (smiLPOPAQUE)&_oid);
		_bInit = false;
	}
	if(oid != "") {
		if(SnmpStrToOid(oid.c_str(), &_oid) == SNMPAPI_FAILURE) {
			SNMP_THROW_ERROR("Could not convert OID from string representation", SnmpGetLastError(NULL), SNMP_ERROR);
		}
		_bInit = true;
	}
}
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;
}