X-Git-Url: https://git.stg.codes/stg.git/blobdiff_plain/07802098b4ebcddaa55994f7a87fe0b0e8514d04..fb3a3aae0c569547b43b5df6fa9b3df5ea3e033f:/projects/stargazer/plugins/other/smux/handlers.cpp diff --git a/projects/stargazer/plugins/other/smux/handlers.cpp b/projects/stargazer/plugins/other/smux/handlers.cpp index 5a8c917d..ac179da9 100644 --- a/projects/stargazer/plugins/other/smux/handlers.cpp +++ b/projects/stargazer/plugins/other/smux/handlers.cpp @@ -1,49 +1,55 @@ -#include "asn1/OpenPDU.h" -#include "asn1/ClosePDU.h" -#include "asn1/RReqPDU.h" -#include "asn1/GetRequest-PDU.h" -#include "asn1/GetResponse-PDU.h" -#include "asn1/VarBindList.h" -#include "asn1/VarBind.h" -#include "asn1/OBJECT_IDENTIFIER.h" -#include "asn1/ber_decoder.h" -#include "asn1/der_encoder.h" +#include + +#include "stg/GetRequest-PDU.h" +#include "stg/GetResponse-PDU.h" +#include "stg/VarBindList.h" +#include "stg/VarBind.h" #include "stg/common.h" +#include "utils.h" #include "smux.h" -std::string OI2String(OBJECT_IDENTIFIER_t * oi); -int SendGetResponsePDU(int fd, GetResponse_PDU_t * getResponse); -int SendGetResponseErrorPDU(int fd, - const PDU_t * getRequest, - int errorStatus, - int errorIndex); - +#ifdef SMUX_DEBUG bool SMUX::CloseHandler(const SMUX_PDUs_t * pdus) { printfd(__FILE__, "SMUX::CloseHandler()\n"); asn_fprint(stderr, &asn_DEF_SMUX_PDUs, pdus); -return false; +return true; +} +#else +bool SMUX::CloseHandler(const SMUX_PDUs_t *) +{ +return true; } +#endif +#ifdef SMUX_DEBUG bool SMUX::RegisterResponseHandler(const SMUX_PDUs_t * pdus) { printfd(__FILE__, "SMUX::RegisterResponseHandler()\n"); asn_fprint(stderr, &asn_DEF_SMUX_PDUs, pdus); -return false; +return true; +} +#else +bool SMUX::RegisterResponseHandler(const SMUX_PDUs_t *) +{ +return true; } +#endif bool SMUX::PDUsRequestHandler(const SMUX_PDUs_t * pdus) { +#ifdef SMUX_DEBUG printfd(__FILE__, "SMUX::PDUsRequestHandler()\n"); asn_fprint(stderr, &asn_DEF_SMUX_PDUs, pdus); -PDUsHandlers::iterator it; -it = pdusHandlers.find(pdus->choice.pdus.present); +#endif +PDUsHandlers::iterator it(pdusHandlers.find(pdus->choice.pdus.present)); if (it != pdusHandlers.end()) { return (this->*(it->second))(&pdus->choice.pdus); } +#ifdef SMUX_DEBUG else { switch (pdus->present) @@ -61,179 +67,125 @@ else printfd(__FILE__, "SMUX::PDUsRequestHandler() - undefined\n"); } } -return false; +#endif +return true; } +#ifdef SMUX_DEBUG bool SMUX::CommitOrRollbackHandler(const SMUX_PDUs_t * pdus) { printfd(__FILE__, "SMUX::CommitOrRollbackHandler()\n"); asn_fprint(stderr, &asn_DEF_SMUX_PDUs, pdus); -return false; +return true; } +#else +bool SMUX::CommitOrRollbackHandler(const SMUX_PDUs_t *) +{ +return true; +} +#endif bool SMUX::GetRequestHandler(const PDUs_t * pdus) { +#ifdef SMUX_DEBUG printfd(__FILE__, "SMUX::GetRequestHandler()\n"); asn_fprint(stderr, &asn_DEF_PDUs, pdus); +#endif const GetRequest_PDU_t * getRequest = &pdus->choice.get_request; -GetResponse_PDU_t msg; -VarBindList_t * varBindList = &msg.variable_bindings; -memset(&msg, 0, sizeof(msg)); +GetResponse_PDU_t * msg = static_cast(calloc(1, sizeof(GetResponse_PDU_t))); +assert(msg && "Enought mempry to allocate GetResponse_PDU_t"); +VarBindList_t * varBindList = &msg->variable_bindings; -msg.request_id = getRequest->request_id; -asn_long2INTEGER(&msg.error_status, 0); -asn_long2INTEGER(&msg.error_index, 0); +long id = 0; +asn_INTEGER2long(&getRequest->request_id, &id); +asn_long2INTEGER(&msg->request_id, id); +asn_long2INTEGER(&msg->error_status, 0); +asn_long2INTEGER(&msg->error_index, 0); const VarBindList_t * vbl = &getRequest->variable_bindings; 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, - PDU__error_status_noSuchName, i); - return true; + return SendGetResponseErrorPDU(sock, getRequest, + PDU__error_status_noSuchName, i); } - VarBind_t newVb; - memset(&newVb, 0, sizeof(newVb)); + VarBind_t * newVb = static_cast(calloc(1, sizeof(VarBind_t))); + assert(newVb && "Enought mempry to allocate VarBind_t"); - newVb.name = vb->name; - it->second->GetValue(&newVb.value); + it->first.ToOID(&newVb->name); + it->second->GetValue(&newVb->value); - ASN_SEQUENCE_ADD(varBindList, &newVb); + ASN_SEQUENCE_ADD(varBindList, newVb); } -SendGetResponsePDU(sock, &msg); -asn_fprint(stderr, &asn_DEF_PDU, &msg); -return false; +bool res = SendGetResponsePDU(sock, msg); +#ifdef SMUX_DEBUG +asn_fprint(stderr, &asn_DEF_GetResponse_PDU, msg); +#endif +ASN_STRUCT_FREE(asn_DEF_GetResponse_PDU, msg); +return res; } bool SMUX::GetNextRequestHandler(const PDUs_t * pdus) { +#ifdef SMUX_DEBUG printfd(__FILE__, "SMUX::GetNextRequestHandler()\n"); asn_fprint(stderr, &asn_DEF_PDUs, pdus); +#endif const GetRequest_PDU_t * getRequest = &pdus->choice.get_request; -GetResponse_PDU_t msg; -VarBindList_t * varBindList = &msg.variable_bindings; -memset(&msg, 0, sizeof(msg)); +GetResponse_PDU_t * msg = static_cast(calloc(1, sizeof(GetResponse_PDU_t))); +assert(msg && "Enought mempry to allocate GetResponse_PDU_t"); +VarBindList_t * varBindList = &msg->variable_bindings; -msg.request_id = getRequest->request_id; -asn_long2INTEGER(&msg.error_status, 0); -asn_long2INTEGER(&msg.error_index, 0); +long id = 0; +asn_INTEGER2long(&getRequest->request_id, &id); +asn_long2INTEGER(&msg->request_id, id); +asn_long2INTEGER(&msg->error_status, 0); +asn_long2INTEGER(&msg->error_index, 0); const VarBindList_t * vbl = &getRequest->variable_bindings; 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, - PDU__error_status_noSuchName, i); - return true; +#ifdef SMUX_DEBUG + printfd(__FILE__, "SMUX::GetNextRequestHandler() - '%s' not found\n", OID(&vb->name).ToString().c_str()); +#endif + return SendGetResponseErrorPDU(sock, getRequest, + PDU__error_status_noSuchName, i); } - VarBind_t newVb; - memset(&newVb, 0, sizeof(newVb)); + VarBind_t * newVb = static_cast(calloc(1, sizeof(VarBind_t))); + assert(newVb && "Enought mempry to allocate VarBind_t"); - newVb.name = vb->name; - it->second->GetValue(&newVb.value); + it->first.ToOID(&newVb->name); + it->second->GetValue(&newVb->value); - ASN_SEQUENCE_ADD(varBindList, &newVb); + ASN_SEQUENCE_ADD(varBindList, newVb); } -SendGetResponsePDU(sock, &msg); -asn_fprint(stderr, &asn_DEF_PDU, &msg); -return false; +bool res = SendGetResponsePDU(sock, msg); +#ifdef SMUX_DEBUG +asn_fprint(stderr, &asn_DEF_PDU, msg); +#endif +ASN_STRUCT_FREE(asn_DEF_GetResponse_PDU, msg); +return res; } bool SMUX::SetRequestHandler(const PDUs_t * pdus) { +#ifdef SMUX_DEBUG printfd(__FILE__, "SMUX::SetRequestHandler()\n"); asn_fprint(stderr, &asn_DEF_PDUs, pdus); -SendGetResponseErrorPDU(sock, &pdus->choice.set_request, - PDU__error_status_readOnly, 0); -return false; -} - -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; - -char buffer[1024]; -error = der_encode_to_buffer(&asn_DEF_GetResponse_PDU, getResponse, buffer, - sizeof(buffer)); - -if (error.encoded == -1) - { - printfd(__FILE__, "Could not encode GetResponsePDU (at %s)\n", - error.failed_type ? error.failed_type->name : "unknown"); - return -1; - } -else - { - write(fd, buffer, error.encoded); - printfd(__FILE__, "GetResponsePDU encoded successfully to %d bytes\n", - error.encoded); - } -return 0; -} - -int SendGetResponseErrorPDU(int fd, - const PDU_t * getRequest, - int errorStatus, - int errorIndex) -{ -asn_enc_rval_t error; -GetResponse_PDU_t msg; - -memset(&msg, 0, sizeof(msg)); - -msg.request_id = getRequest->request_id; -asn_long2INTEGER(&msg.error_status, errorStatus); -asn_long2INTEGER(&msg.error_index, errorIndex); - -char buffer[1024]; -error = der_encode_to_buffer(&asn_DEF_GetResponse_PDU, &msg, buffer, - sizeof(buffer)); - -if (error.encoded == -1) - { - printfd(__FILE__, "Could not encode GetResponsePDU for error (at %s)\n", - error.failed_type ? error.failed_type->name : "unknown"); - return -1; - } -else - { - write(fd, buffer, error.encoded); - printfd(__FILE__, - "GetResponsePDU for error encoded successfully to %d bytes\n", - error.encoded); - } -return 0; +#endif +return SendGetResponseErrorPDU(sock, &pdus->choice.set_request, + PDU__error_status_readOnly, 0); }