QtNetSNMP::SNMPSession *QtNetSNMP::QSNMPCore::createSession(SNMPVersion version, const QString& community, const QString& agent) throw(QSNMPException) { SNMPSession session; SNMPSession *openedSession; std::string stdCommunity = community.toStdString(); std::string stdAgent = agent.toStdString(); if(version != SNMPv1 && version != SNMPv2) throw QSNMPException("QSNMPCore :: Create Session :: Version not supported"); snmp_sess_init(&session); session.remote_port = _port; session.retries = _retries; session.timeout = _timeout; session.version = version; session.community = reinterpret_cast<u_char *>(const_cast<char *>(stdCommunity.c_str())); session.community_len = stdCommunity.length(); session.peername = const_cast<char *>(stdAgent.c_str()); SOCK_STARTUP; if(!(openedSession = snmp_open(&session))) { SOCK_CLEANUP; throw QSNMPException("QSNMPCore :: Create Session :: Open Session"); } return openedSession; }
QtNetSNMP::QMIBTree *QtNetSNMP::QSNMPManager::getMIBFile(const QString& fileName) throw(QSNMPException) { if(!QFile(fileName).exists()) throw QSNMPException("QSNMPManager :: Get MIB File :: File not found"); return _core -> getMIBTree(read_mib(fileName.toStdString().c_str())); }
QtNetSNMP::SNMPPDU *QtNetSNMP::QSNMPCore::sendPDU(SNMPSession *session, SNMPPDU *pdu) throw(QSNMPException) { SNMPPDU *response; int status; status = snmp_synch_response(session, pdu, &response); if(status == STAT_SUCCESS) if(response -> errstat == SNMP_ERR_NOERROR) return response; else throw QSNMPException("QSNMPCore :: Send PDU :: Responde PDU has errors"); else if(status == STAT_TIMEOUT) throw QSNMPException("QSNMPCore :: Send PDU :: Timeout. No response from agent"); else throw QSNMPException("QSNMPCore :: Send PDU :: SNMP Session error"); }
QtNetSNMP::QMIBTree *QtNetSNMP::QSNMPManager::getMIBModule(const QString& module) throw(QSNMPException) { if(module.isEmpty()) return _core -> getMIBTree(read_all_mibs()); else if(getModulesInstalled().indexOf(module) == -1) throw QSNMPException("QSNMPManager :: Get MIB Module :: Module not found"); return _core -> getMIBTree(read_module(module.toStdString().c_str())); }
void QtNetSNMP::QSNMPOID::setTextOID(const QString& textOID) throw(QSNMPException) { QRegExp regExp("\\d+(\\.\\d+)*"); if(!regExp.exactMatch(textOID)) throw QSNMPException("QSNMPOID :: Set Text OID :: Textual representation of OID is wrong"); if(_numOID) delete _numOID; _numOID = new QVector<oid>(); foreach(QString str, textOID.split(".")) _numOID -> append(str.toInt()); }
QtNetSNMP::SNMPPDU *QtNetSNMP::QSNMPCore::createPDU(SNMPPDUType type, QVector<QSNMPObject *>& objs, unsigned short nrepeaters, unsigned short mrepetitions) throw(QSNMPException) { SNMPPDU *pdu; if(type != SNMPPDUGet && type != SNMPPDUGetNext && type != SNMPPDUGetBulk && type != SNMPPDUSet) throw QSNMPException("QSNMPCore :: Create PDU :: Unknown PDU type"); if(objs.empty()) throw QSNMPException("QSNMPCore :: Create PDU :: SNMP objects vector is empty"); pdu = snmp_pdu_create(type); foreach (QSNMPObject *object, objs) { if(type == SNMPPDUSet) { if(object -> data() -> type() == SNMPDataUnknown) throw QSNMPException("QSNMPCore :: Create PDU :: Unknown SNMP data type"); char dataType; switch(object -> data() -> type()) { case SNMPDataInteger: dataType = 'i'; break; case SNMPDataUnsigned: dataType = 'u'; break; case SNMPDataBits: dataType = 'b'; break; case SNMPDataCounter: dataType = 'c'; break; case SNMPDataTimeTicks: dataType = 't'; break; case SNMPDataCounter64: dataType = 'C'; break; case SNMPDataBitString: dataType = 'b'; break; case SNMPDataOctetString: dataType = 's'; break; case SNMPDataIPAddress: dataType = 'a'; break; case SNMPDataObjectId: dataType = 'o'; break; default: dataType = '='; } snmp_add_var(pdu, object -> objID() -> numOID() -> data(), static_cast<size_t>(object -> objID() -> numOID() -> size()), dataType, static_cast<const char *>(object -> data() -> value())); } else snmp_add_null_var(pdu, object -> objID() -> numOID() -> data(), static_cast<size_t>(object -> objID() -> numOID() -> size())); } if(type == SNMPPDUGetBulk) { pdu -> errstat = nrepeaters; pdu -> errindex = mrepetitions; } return pdu; }