From fdee6fdb88e79929c952fea956fa57e5780804cf Mon Sep 17 00:00:00 2001 From: Maxim Mamontov Date: Tue, 26 Jul 2011 18:15:32 +0300 Subject: [PATCH 1/1] Added a wrapper for correct oid comparsion --- .../stargazer/plugins/other/smux/Makefile | 1 + .../stargazer/plugins/other/smux/handlers.cpp | 4 +- .../stargazer/plugins/other/smux/sensors.h | 4 +- .../stargazer/plugins/other/smux/smux.cpp | 30 ++--- projects/stargazer/plugins/other/smux/smux.h | 1 + .../stargazer/plugins/other/smux/types.cpp | 121 ++++++++++++++++++ projects/stargazer/plugins/other/smux/types.h | 34 +++++ .../stargazer/plugins/other/smux/utils.cpp | 42 +++--- 8 files changed, 197 insertions(+), 40 deletions(-) create mode 100644 projects/stargazer/plugins/other/smux/types.cpp create mode 100644 projects/stargazer/plugins/other/smux/types.h diff --git a/projects/stargazer/plugins/other/smux/Makefile b/projects/stargazer/plugins/other/smux/Makefile index e94fdcda..e9298273 100644 --- a/projects/stargazer/plugins/other/smux/Makefile +++ b/projects/stargazer/plugins/other/smux/Makefile @@ -8,6 +8,7 @@ SRCS = smux.cpp \ sensors.cpp \ handlers.cpp \ utils.cpp \ + types.cpp \ asn1/DisplayString.c \ asn1/PhysAddress.c \ asn1/IfEntry.c \ diff --git a/projects/stargazer/plugins/other/smux/handlers.cpp b/projects/stargazer/plugins/other/smux/handlers.cpp index bc8b60eb..cad9739f 100644 --- a/projects/stargazer/plugins/other/smux/handlers.cpp +++ b/projects/stargazer/plugins/other/smux/handlers.cpp @@ -77,7 +77,7 @@ for (int i = 0; i < vbl->list.count; ++i) { VarBind_t * vb = getRequest->variable_bindings.list.array[i]; Sensors::iterator it; - it = sensors.find(OI2String(&vb->name)); + it = sensors.find(OID(&vb->name)); if (it == sensors.end()) { SendGetResponseErrorPDU(sock, getRequest, @@ -117,7 +117,7 @@ for (int i = 0; i < vbl->list.count; ++i) { VarBind_t * vb = getRequest->variable_bindings.list.array[i]; Sensors::iterator it; - it = sensors.upper_bound(OI2String(&vb->name)); + it = sensors.upper_bound(OID(&vb->name)); if (it == sensors.end()) { SendGetResponseErrorPDU(sock, getRequest, diff --git a/projects/stargazer/plugins/other/smux/sensors.h b/projects/stargazer/plugins/other/smux/sensors.h index be53762b..dbfb807b 100644 --- a/projects/stargazer/plugins/other/smux/sensors.h +++ b/projects/stargazer/plugins/other/smux/sensors.h @@ -1,7 +1,6 @@ #ifndef __SENSORS_H__ #define __SENSORS_H__ -#include #include #include "stg/users.h" @@ -11,13 +10,14 @@ #include "asn1/ObjectSyntax.h" #include "value2os.h" +#include "types.h" class Sensor { public: virtual bool GetValue(ObjectSyntax_t * objectSyntax) const = 0; }; -typedef std::map Sensors; +typedef std::map Sensors; class TableSensor { public: diff --git a/projects/stargazer/plugins/other/smux/smux.cpp b/projects/stargazer/plugins/other/smux/smux.cpp index 4629aeab..f6c1fade 100644 --- a/projects/stargazer/plugins/other/smux/smux.cpp +++ b/projects/stargazer/plugins/other/smux/smux.cpp @@ -16,11 +16,11 @@ #include "smux.h" #include "utils.h" -PLUGIN_CREATOR sac; +PLUGIN_CREATOR smc; PLUGIN * GetPlugin() { -return sac.GetPlugin(); +return smc.GetPlugin(); } SMUX_SETTINGS::SMUX_SETTINGS() @@ -33,7 +33,7 @@ int SMUX_SETTINGS::ParseSettings(const MODULE_SETTINGS & s) PARAM_VALUE pv; std::vector::const_iterator pvi; int p; -/////////////////////////// + pv.param = "Port"; pvi = std::find(s.moduleParams.begin(), s.moduleParams.end(), pv); if (pvi == s.moduleParams.end()) @@ -116,19 +116,19 @@ if (PrepareNet()) return -1; // Users -sensors[".1.3.6.1.4.1.38313.1.1.1"] = new TotalUsersSensor(*users); -sensors[".1.3.6.1.4.1.38313.1.1.2"] = new ConnectedUsersSensor(*users); -sensors[".1.3.6.1.4.1.38313.1.1.3"] = new AuthorizedUsersSensor(*users); -sensors[".1.3.6.1.4.1.38313.1.1.4"] = new AlwaysOnlineUsersSensor(*users); -sensors[".1.3.6.1.4.1.38313.1.1.5"] = new NoCashUsersSensor(*users); -sensors[".1.3.6.1.4.1.38313.1.1.7"] = new DisabledDetailStatsUsersSensor(*users); -sensors[".1.3.6.1.4.1.38313.1.1.8"] = new DisabledUsersSensor(*users); -sensors[".1.3.6.1.4.1.38313.1.1.9"] = new PassiveUsersSensor(*users); -sensors[".1.3.6.1.4.1.38313.1.1.10"] = new CreditUsersSensor(*users); -sensors[".1.3.6.1.4.1.38313.1.1.11"] = new FreeMbUsersSensor(*users); -sensors[".1.3.6.1.4.1.38313.1.1.12"] = new TariffChangeUsersSensor(*users); +sensors[OID(".1.3.6.1.4.1.38313.1.1.1")] = new TotalUsersSensor(*users); +sensors[OID(".1.3.6.1.4.1.38313.1.1.2")] = new ConnectedUsersSensor(*users); +sensors[OID(".1.3.6.1.4.1.38313.1.1.3")] = new AuthorizedUsersSensor(*users); +sensors[OID(".1.3.6.1.4.1.38313.1.1.4")] = new AlwaysOnlineUsersSensor(*users); +sensors[OID(".1.3.6.1.4.1.38313.1.1.5")] = new NoCashUsersSensor(*users); +sensors[OID(".1.3.6.1.4.1.38313.1.1.7")] = new DisabledDetailStatsUsersSensor(*users); +sensors[OID(".1.3.6.1.4.1.38313.1.1.8")] = new DisabledUsersSensor(*users); +sensors[OID(".1.3.6.1.4.1.38313.1.1.9")] = new PassiveUsersSensor(*users); +sensors[OID(".1.3.6.1.4.1.38313.1.1.10")] = new CreditUsersSensor(*users); +sensors[OID(".1.3.6.1.4.1.38313.1.1.11")] = new FreeMbUsersSensor(*users); +sensors[OID(".1.3.6.1.4.1.38313.1.1.12")] = new TariffChangeUsersSensor(*users); // Tariffs -sensors[".1.3.6.1.4.1.38313.1.2.1"] = new TotalTariffsSensor(*tariffs); +sensors[OID(".1.3.6.1.4.1.38313.1.2.1")] = new TotalTariffsSensor(*tariffs); if (!running) { diff --git a/projects/stargazer/plugins/other/smux/smux.h b/projects/stargazer/plugins/other/smux/smux.h index aa872700..aaf5514c 100644 --- a/projects/stargazer/plugins/other/smux/smux.h +++ b/projects/stargazer/plugins/other/smux/smux.h @@ -16,6 +16,7 @@ #include "stg/tariffs.h" #include "sensors.h" +#include "types.h" extern "C" PLUGIN * GetPlugin(); diff --git a/projects/stargazer/plugins/other/smux/types.cpp b/projects/stargazer/plugins/other/smux/types.cpp new file mode 100644 index 00000000..ac1d0253 --- /dev/null +++ b/projects/stargazer/plugins/other/smux/types.cpp @@ -0,0 +1,121 @@ +#include +#include +#include + +#include "types.h" + +bool StringToArcs(const char * str, size_t length, std::vector & arcs) +{ +unsigned a[1024]; +if (length == 0) + return false; +const char * left = str; +if (*left == '.') + ++left; +size_t arcPos = 0; +while ((left - str) < length) + { + char * pos = NULL; + unsigned arc = strtoul(left, &pos, 10); + if (pos == left) + return false; + a[arcPos++] = arc; + if (arcPos >= 1024) + return false; + left = pos + 1; + } + +std::vector newArcs(a, a + arcPos); +arcs.swap(newArcs); +return true; +} + +OID::OID(const std::string & str) +{ +if (!StringToArcs(str.c_str(), str.length(), arcs)) + throw std::runtime_error("Invalid oid"); +} + +OID::OID(const char * str, size_t length) +{ +if (!StringToArcs(str, length, arcs)) + throw std::runtime_error("Invalid oid"); +} + +OID::OID(const std::vector & a) + : arcs(a) +{ +} + +OID::OID(const unsigned * a, size_t length) +{ +std::vector newArcs(a, a + length); +arcs.swap(newArcs); +} + +OID::OID(OBJECT_IDENTIFIER_t * oid) +{ +unsigned a[1024]; +int count = OBJECT_IDENTIFIER_get_arcs(oid, a, sizeof(a[0]), 1024); + +if (count > 1024) + throw std::runtime_error("OID is too long"); + +std::vector newArcs(a, a + count); +arcs.swap(newArcs); +} + +OID::OID(const OID & rvalue) + : arcs(rvalue.arcs) +{ +} + +OID::~OID() +{ +} + +std::string OID::ToString() const +{ +std::stringstream stream; +for (size_t i = 0; i < arcs.size(); ++i) + stream << "." << arcs[i]; +return stream.str(); +} + +void OID::ToOID(OBJECT_IDENTIFIER_t * oid) const +{ +OBJECT_IDENTIFIER_set_arcs(oid, &arcs.front(), sizeof(unsigned), arcs.size()); +} + +OID & OID::operator=(const OID & rvalue) +{ +arcs = rvalue.arcs; +return *this; +} + +bool OID::operator==(const OID & rvalue) const +{ +if (arcs.size() != rvalue.arcs.size()) + return false; +for (size_t i = 0; i < arcs.size(); ++i) + if (arcs[i] != rvalue.arcs[i]) + return false; +return true; +} + +bool OID::operator<(const OID & rvalue) const +{ +for (size_t i = 0; i < std::min(arcs.size(), rvalue.arcs.size()); ++i) + if (arcs[i] > rvalue.arcs[i]) + return false; +if (rvalue.arcs.size() < arcs.size()) + return false; +return true; +} + +std::ostream & OID::operator<<(std::ostream & stream) const +{ +for (size_t i = 0; i < arcs.size(); ++i) + stream << "." << arcs[i]; +return stream; +} diff --git a/projects/stargazer/plugins/other/smux/types.h b/projects/stargazer/plugins/other/smux/types.h new file mode 100644 index 00000000..26d6e24f --- /dev/null +++ b/projects/stargazer/plugins/other/smux/types.h @@ -0,0 +1,34 @@ +#ifndef __TYPES_H__ +#define __TYPES_H__ + +#include +#include +#include + +#include "asn1/OBJECT_IDENTIFIER.h" + +class OID { + public: + OID(const std::string & str); + OID(const char * str, size_t length); + OID(const std::vector & arcs); + OID(const unsigned * arcs, size_t length); + OID(OBJECT_IDENTIFIER_t * oid); + OID(const OID & rvalue); + ~OID(); + + std::string ToString() const; + const std::vector & ToVector() const { return arcs; } + void ToOID(OBJECT_IDENTIFIER_t * oid) const; + + OID & operator=(const OID & rvalue); + bool operator==(const OID & rvalue) const; + bool operator<(const OID & rvalue) const; + + std::ostream & operator<<(std::ostream & stream) const; + + private: + std::vector arcs; +}; + +#endif diff --git a/projects/stargazer/plugins/other/smux/utils.cpp b/projects/stargazer/plugins/other/smux/utils.cpp index 4bfc0c10..328cea88 100644 --- a/projects/stargazer/plugins/other/smux/utils.cpp +++ b/projects/stargazer/plugins/other/smux/utils.cpp @@ -74,6 +74,27 @@ OBJECT_IDENTIFIER_set_arcs(oi, arcs, sizeof(arcs[0]), arcPos); return true; } +std::string OI2String(OBJECT_IDENTIFIER_t * oi) +{ +std::string res; + +int arcs[1024]; +int count = OBJECT_IDENTIFIER_get_arcs(oi, arcs, sizeof(arcs[0]), 1024); + +if (count > 1024) + return ""; + +for (int i = 0; i < count; ++i) + { + res += "."; + std::string arc; + strprintf(&arc, "%d", arcs[i]); + res += arc; + } + +return res; +} + bool SendOpenPDU(int fd) { const char * description = "Stg SMUX Plugin"; @@ -191,27 +212,6 @@ if(error.code != RC_OK) return pdus; } -std::string OI2String(OBJECT_IDENTIFIER_t * oi) -{ -std::string res; - -int arcs[1024]; -int count = OBJECT_IDENTIFIER_get_arcs(oi, arcs, sizeof(arcs[0]), 1024); - -if (count > 1024) - return ""; - -for (int i = 0; i < count; ++i) - { - res += "."; - std::string arc; - strprintf(&arc, "%d", arcs[i]); - res += arc; - } - -return res; -} - int SendGetResponsePDU(int fd, GetResponse_PDU_t * getResponse) { asn_enc_rval_t error; -- 2.44.2