X-Git-Url: https://git.stg.codes/ssmd.git/blobdiff_plain/ae26516a8828d54ab7c0e0bca92031a54a15ed62..6b235fa5a6fd3b514febe5b1328f97df14fb10bc:/src/switch.cpp diff --git a/src/switch.cpp b/src/switch.cpp index 43c83da..db8a1be 100644 --- a/src/switch.cpp +++ b/src/switch.cpp @@ -26,7 +26,8 @@ Switch::Switch(const Settings & settings, _writeCommunity(writeCommunity), _uplinkPort(uplinkPort), _nextUpACL(1), - _nextDownACL(1) + _nextDownACL(1), + _aclsCreated(false) { } @@ -39,12 +40,38 @@ Switch::Switch(const Switch & rvalue) _uplinkPort(rvalue._uplinkPort), _nextUpACL(rvalue._nextUpACL), _nextDownACL(rvalue._nextDownACL), - _acls(rvalue._acls) + _acls(rvalue._acls), + _aclsCreated(rvalue._aclsCreated) { } Switch::~Switch() { + if (_aclsCreated) { + IpAddress addr(_ip.c_str()); + if (!addr.valid()) { + logger << "Switch::~Switch() - ivalid switch ip: '" << _ip << "'" << std::endl; + return; + } + + CTarget target(addr, _readCommunity.c_str(), _writeCommunity.c_str()); + if (!target.valid()) { + logger << "Switch::~Switch() - failed to create target for the switch '" << _ip << "'" << std::endl; + return; + } + + target.set_version(version2c); + + if (!checkProfiles(target)) { + logger << "Switch::~Switch() - no upload and download profiles defined for the switch '" << _ip << "'" << std::endl; + return; + } + + if (!dropACLs(target)) { + logger << "Switch::~Switch() - failed to drop ACLs for the switch '" << _ip << "'" << std::endl; + return; + } + } } Switch & Switch::operator=(const Switch & rvalue) @@ -56,6 +83,7 @@ Switch & Switch::operator=(const Switch & rvalue) _nextUpACL = rvalue._nextUpACL; _nextDownACL = rvalue._nextDownACL; _acls = rvalue._acls; + _aclsCreated = rvalue._aclsCreated; return *this; } @@ -106,6 +134,7 @@ void Switch::sync() if (!createACLs(target)) { logger << "Switch::sync() - failed to create ACLs for the switch '" << _ip << "'" << std::endl; + return; } if (_settings.isDebug()) { @@ -174,23 +203,27 @@ bool Switch::dropACLsByTable(const CTarget & target, unsigned profileId, const S dropACLOidPrefix += "."; dropACLOidPrefix += boost::lexical_cast(profileId); SNMPList aclsList(table.getList()); - Pdu pdu; - SNMPList::const_iterator it; - for (it = aclsList.begin(); it != aclsList.end(); ++it) { - int id; - if (it->get_value(id) != SNMP_CLASS_SUCCESS) { + SNMPList::const_iterator it(aclsList.begin()); + size_t chunks = aclsList.size() / _settings.maxACLPerPDU() + 1; + for (size_t i = 0; i < chunks && it != aclsList.end(); ++i) { + Pdu pdu; + for (size_t j = 0; j < _settings.maxACLPerPDU() && it != aclsList.end(); ++j, ++it) { + int id; + if (int c = it->get_value(id) != SNMP_CLASS_SUCCESS) { + logger << "Switch::dropACLsByTable() - failed to get ACL id for the switch '" << _ip << "'. Error message: '" << Snmp::error_msg(c) << "'" << std::endl; + return false; + } + std::string dropACLOid(dropACLOidPrefix); + dropACLOid += "."; + dropACLOid += boost::lexical_cast(id); + Vb vb(Oid(dropACLOid.c_str())); + vb.set_value(int(6)); + pdu += vb; + } + if (int c = _snmp.set(pdu, target) != SNMP_CLASS_SUCCESS) { + logger << "Switch::dropACLsByTable() - failed to invoke Snmp::set for the switch '" << _ip << "'. Error message: '" << Snmp::error_msg(c) << "'" << std::endl; return false; } - std::string dropACLOid(dropACLOidPrefix); - dropACLOid += "."; - dropACLOid += boost::lexical_cast(id); - Vb vb(Oid(dropACLOid.c_str())); - vb.set_value(int(6)); - pdu += vb; - } - if (_snmp.set(pdu, target) != SNMP_CLASS_SUCCESS) { - logger << "Switch::dropACLsByTable() - failed to invoke Snmp::set for the switch '" << _ip << "'" << std::endl; - return false; } return true; } @@ -201,10 +234,11 @@ bool Switch::createACLs(const CTarget & target) for (it = _acls.begin(); it != _acls.end(); ++it) { Pdu pdu; it->appendPdu(pdu); - if (_snmp.set(pdu, target) != SNMP_CLASS_SUCCESS) { - logger << "Switch::createACLs() - failed to invoke Snmp::set for the switch '" << _ip << "'" << std::endl; + if (int c = _snmp.set(pdu, target) != SNMP_CLASS_SUCCESS) { + logger << "Switch::createACLs() - failed to invoke Snmp::set for the switch '" << _ip << "'. Error message: '" << Snmp::error_msg(c) << "'" << std::endl; return false; } + _aclsCreated = true; } return true; }