Esempio n. 1
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);
    }
}
Esempio n. 2
0
SNMPAPI_STATUS SNMPSession::processNotification(WPARAM wParam, LPARAM lParam) {
	if(wParam == 0) {
		HSNMP_PDU hPdu;
		HSNMP_CONTEXT hContext;
		HSNMP_ENTITY hDstEntity;
		HSNMP_ENTITY hSrcEntity;
		SNMPAPI_STATUS ret = SnmpRecvMsg(_hSession, &hSrcEntity, &hDstEntity, &hContext, &hPdu);
		SNMPRemoteAgent * pRa = getRemoteAgent(hContext);		
		int requestId = (DWORD)lParam;
		SNMPRequest * pRequest = NULL;
		if(pRa) {
			pRequest = pRa->getRequest(requestId);
		}
		if(ret == SNMPAPI_FAILURE) {
			if(pRequest) {
				pRequest->setResult(SNMP_ERROR_GENERR);
			}
			SnmpFreeVbl(hPdu);
			return SNMPAPI_SUCCESS;
		}	
		smiINT32 lReqId, lType, lErr, lIdx;
		HSNMP_VBL hVbl;
		ret = SnmpGetPduData(hPdu, &lType, &lReqId, &lErr, &lIdx, &hVbl);
		if(ret == SNMPAPI_FAILURE) {
			if(pRequest) {
				pRequest->setResult(SNMP_ERROR_GENERR);
			}
			SnmpFreeVbl(hPdu);
			return SNMPAPI_SUCCESS;
		}
#ifdef SMNP_TRACER
		if(SNMPTRACEAENABLED()) {
			std::stringstream trace;
			trace << "rx " << SNMPManager::pduToStr(hDstEntity, hSrcEntity, hContext, lType, lReqId, lErr, hVbl, false);
			SNMPTRACE(trace.str());
		}
#endif
		if(lType != SNMP_PDU_RESPONSE) {
			pRequest = NULL;
		}
		if(pRequest == NULL && lType != SNMP_PDU_TRAP && lType != SNMP_PDU_INFORM) {
			SnmpFreeVbl(hVbl);
			SnmpFreeVbl(hPdu);
			return SNMPAPI_SUCCESS;
		}

		smiOID dRetName;
		smiVALUE dRetValue;
		int count = SnmpCountVbl(hVbl);
		if(count) {
			char szName[MAXOBJIDSTRSIZE+1];
			szName[0] = szName[MAXOBJIDSTRSIZE] = '\0';
			for(int i=0; i<count; i++) {
				if(SnmpGetVb(hVbl, i+1, &dRetName, &dRetValue) == SNMPAPI_FAILURE) {
					SnmpFreeVbl(hVbl);
					SnmpFreePdu(hPdu);
					if(pRequest) {
						pRequest->setResult(SNMP_ERROR_GENERR);
					}
					return SNMPAPI_SUCCESS;
				}
				if(SnmpOidToStr(&dRetName, sizeof(szName), szName) == SNMPAPI_FAILURE) {
					szName[0] = '\0';
				}
				SNMPObject obj;
				obj.setValue(&dRetValue);
				obj.setOID(szName);
				if(pRequest) {
					pRequest->addToResult(obj);
				} else {
					processTrap(obj);
				}
				SnmpFreeDescriptor(SNMP_SYNTAX_OID, (smiLPOPAQUE)&dRetName);
				if(dRetValue.syntax == SNMP_SYNTAX_OID) {
					SnmpFreeDescriptor(dRetValue.syntax, (smiLPOPAQUE)&dRetValue.value.oid);
				} else if(	dRetValue.syntax == SNMP_SYNTAX_OPAQUE ||
							dRetValue.syntax == SNMP_SYNTAX_OCTETS ||
							dRetValue.syntax == SNMP_SYNTAX_BITS ||
							dRetValue.syntax == SNMP_SYNTAX_IPADDR ||
							dRetValue.syntax == SNMP_SYNTAX_NSAPADDR) {
					SnmpFreeDescriptor(dRetValue.syntax, (smiLPOPAQUE)&dRetValue.value.string);
				}
			}
		}
		SnmpFreeVbl(hVbl);
		SnmpFreePdu(hPdu);
		if(pRequest) {
			pRequest->setResult(lErr, false);
		}
		return SNMPAPI_SUCCESS;
	}
	return SNMPAPI_SUCCESS;
}
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;
}