]> git.stg.codes - stg.git/blobdiff - projects/stargazer/plugins/other/smux/handlers.cpp
Prevent memory leak in smux plugin
[stg.git] / projects / stargazer / plugins / other / smux / handlers.cpp
index f0f3a8c1d097996927ea6eb9408f64dcb179797f..5f25a82f90dd30ae0762ddeeb84c9009e4e39a23 100644 (file)
@@ -1,16 +1,13 @@
-#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 <cassert>
+
+#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"
 
 bool SMUX::CloseHandler(const SMUX_PDUs_t * pdus)
@@ -27,9 +24,9 @@ asn_fprint(stderr, &asn_DEF_SMUX_PDUs, pdus);
 return false;
 }
 
-bool SMUX::PDUsHandler(const SMUX_PDUs_t * pdus)
+bool SMUX::PDUsRequestHandler(const SMUX_PDUs_t * pdus)
 {
-printfd(__FILE__, "SMUX::PDUsHandler()\n");
+printfd(__FILE__, "SMUX::PDUsRequestHandler()\n");
 asn_fprint(stderr, &asn_DEF_SMUX_PDUs, pdus);
 PDUsHandlers::iterator it;
 it = pdusHandlers.find(pdus->choice.pdus.present);
@@ -42,16 +39,16 @@ else
     switch (pdus->present)
         {
         case PDUs_PR_NOTHING:
-            printfd(__FILE__, "SMUX::PDUsHandler() - nothing\n");
+            printfd(__FILE__, "SMUX::PDUsRequestHandler() - nothing\n");
             break;
         case PDUs_PR_get_response:
-            printfd(__FILE__, "SMUX::PDUsHandler() - get response\n");
+            printfd(__FILE__, "SMUX::PDUsRequestHandler() - get response\n");
             break;
         case PDUs_PR_trap:
-            printfd(__FILE__, "SMUX::PDUsHandler() - trap\n");
+            printfd(__FILE__, "SMUX::PDUsRequestHandler() - trap\n");
             break;
         default:
-            printfd(__FILE__, "SMUX::PDUsHandler() - undefined\n");
+            printfd(__FILE__, "SMUX::PDUsRequestHandler() - undefined\n");
         }
     }
 return false;
@@ -69,20 +66,22 @@ bool SMUX::GetRequestHandler(const PDUs_t * pdus)
 printfd(__FILE__, "SMUX::GetRequestHandler()\n");
 asn_fprint(stderr, &asn_DEF_PDUs, pdus);
 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<GetResponse_PDU_t *>(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,
@@ -90,17 +89,18 @@ for (int i = 0; i < vbl->list.count; ++i)
         return true;
         }
 
-    VarBind_t newVb;
-    memset(&newVb, 0, sizeof(newVb));
+    VarBind_t * newVb = static_cast<VarBind_t *>(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);
+SendGetResponsePDU(sock, msg);
+asn_fprint(stderr, &asn_DEF_GetResponse_PDU, msg);
+ASN_STRUCT_FREE(asn_DEF_GetResponse_PDU, msg);
 return false;
 }
 
@@ -109,38 +109,42 @@ bool SMUX::GetNextRequestHandler(const PDUs_t * pdus)
 printfd(__FILE__, "SMUX::GetNextRequestHandler()\n");
 asn_fprint(stderr, &asn_DEF_PDUs, pdus);
 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<GetResponse_PDU_t *>(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())
         {
+        printfd(__FILE__, "SMUX::GetNextRequestHandler() - '%s' not found\n", OID(&vb->name).ToString().c_str());
         SendGetResponseErrorPDU(sock, getRequest,
                                 PDU__error_status_noSuchName, i);
         return true;
         }
 
-    VarBind_t newVb;
-    memset(&newVb, 0, sizeof(newVb));
+    VarBind_t * newVb = static_cast<VarBind_t *>(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);
+SendGetResponsePDU(sock, msg);
+asn_fprint(stderr, &asn_DEF_PDU, msg);
+ASN_STRUCT_FREE(asn_DEF_GetResponse_PDU, msg);
 return false;
 }