]> git.stg.codes - stg.git/blob - projects/stargazer/plugins/other/smux/handlers.cpp
Prevent memory leak in smux plugin
[stg.git] / projects / stargazer / plugins / other / smux / handlers.cpp
1 #include <cassert>
2
3 #include "stg/GetRequest-PDU.h"
4 #include "stg/GetResponse-PDU.h"
5 #include "stg/VarBindList.h"
6 #include "stg/VarBind.h"
7
8 #include "stg/common.h"
9
10 #include "utils.h"
11 #include "smux.h"
12
13 bool SMUX::CloseHandler(const SMUX_PDUs_t * pdus)
14 {
15 printfd(__FILE__, "SMUX::CloseHandler()\n");
16 asn_fprint(stderr, &asn_DEF_SMUX_PDUs, pdus);
17 return false;
18 }
19
20 bool SMUX::RegisterResponseHandler(const SMUX_PDUs_t * pdus)
21 {
22 printfd(__FILE__, "SMUX::RegisterResponseHandler()\n");
23 asn_fprint(stderr, &asn_DEF_SMUX_PDUs, pdus);
24 return false;
25 }
26
27 bool SMUX::PDUsRequestHandler(const SMUX_PDUs_t * pdus)
28 {
29 printfd(__FILE__, "SMUX::PDUsRequestHandler()\n");
30 asn_fprint(stderr, &asn_DEF_SMUX_PDUs, pdus);
31 PDUsHandlers::iterator it;
32 it = pdusHandlers.find(pdus->choice.pdus.present);
33 if (it != pdusHandlers.end())
34     {
35     return (this->*(it->second))(&pdus->choice.pdus);
36     }
37 else
38     {
39     switch (pdus->present)
40         {
41         case PDUs_PR_NOTHING:
42             printfd(__FILE__, "SMUX::PDUsRequestHandler() - nothing\n");
43             break;
44         case PDUs_PR_get_response:
45             printfd(__FILE__, "SMUX::PDUsRequestHandler() - get response\n");
46             break;
47         case PDUs_PR_trap:
48             printfd(__FILE__, "SMUX::PDUsRequestHandler() - trap\n");
49             break;
50         default:
51             printfd(__FILE__, "SMUX::PDUsRequestHandler() - undefined\n");
52         }
53     }
54 return false;
55 }
56
57 bool SMUX::CommitOrRollbackHandler(const SMUX_PDUs_t * pdus)
58 {
59 printfd(__FILE__, "SMUX::CommitOrRollbackHandler()\n");
60 asn_fprint(stderr, &asn_DEF_SMUX_PDUs, pdus);
61 return false;
62 }
63
64 bool SMUX::GetRequestHandler(const PDUs_t * pdus)
65 {
66 printfd(__FILE__, "SMUX::GetRequestHandler()\n");
67 asn_fprint(stderr, &asn_DEF_PDUs, pdus);
68 const GetRequest_PDU_t * getRequest = &pdus->choice.get_request;
69 GetResponse_PDU_t * msg = static_cast<GetResponse_PDU_t *>(calloc(1, sizeof(GetResponse_PDU_t)));
70 assert(msg && "Enought mempry to allocate GetResponse_PDU_t");
71 VarBindList_t * varBindList = &msg->variable_bindings;
72
73 long id = 0;
74 asn_INTEGER2long(&getRequest->request_id, &id);
75 asn_long2INTEGER(&msg->request_id, id);
76 asn_long2INTEGER(&msg->error_status, 0);
77 asn_long2INTEGER(&msg->error_index, 0);
78
79 const VarBindList_t * vbl = &getRequest->variable_bindings; 
80 for (int i = 0; i < vbl->list.count; ++i)
81     {
82     VarBind_t * vb = getRequest->variable_bindings.list.array[i];
83     Sensors::iterator it;
84     it = sensors.find(OID(&vb->name));
85     if (it == sensors.end())
86         {
87         SendGetResponseErrorPDU(sock, getRequest,
88                                 PDU__error_status_noSuchName, i);
89         return true;
90         }
91
92     VarBind_t * newVb = static_cast<VarBind_t *>(calloc(1, sizeof(VarBind_t)));
93     assert(newVb && "Enought mempry to allocate VarBind_t");
94
95     it->first.ToOID(&newVb->name);
96     it->second->GetValue(&newVb->value);
97
98     ASN_SEQUENCE_ADD(varBindList, newVb);
99     }
100
101 SendGetResponsePDU(sock, msg);
102 asn_fprint(stderr, &asn_DEF_GetResponse_PDU, msg);
103 ASN_STRUCT_FREE(asn_DEF_GetResponse_PDU, msg);
104 return false;
105 }
106
107 bool SMUX::GetNextRequestHandler(const PDUs_t * pdus)
108 {
109 printfd(__FILE__, "SMUX::GetNextRequestHandler()\n");
110 asn_fprint(stderr, &asn_DEF_PDUs, pdus);
111 const GetRequest_PDU_t * getRequest = &pdus->choice.get_request;
112 GetResponse_PDU_t * msg = static_cast<GetResponse_PDU_t *>(calloc(1, sizeof(GetResponse_PDU_t)));
113 assert(msg && "Enought mempry to allocate GetResponse_PDU_t");
114 VarBindList_t * varBindList = &msg->variable_bindings;
115
116 long id = 0;
117 asn_INTEGER2long(&getRequest->request_id, &id);
118 asn_long2INTEGER(&msg->request_id, id);
119 asn_long2INTEGER(&msg->error_status, 0);
120 asn_long2INTEGER(&msg->error_index, 0);
121
122 const VarBindList_t * vbl = &getRequest->variable_bindings; 
123 for (int i = 0; i < vbl->list.count; ++i)
124     {
125     VarBind_t * vb = getRequest->variable_bindings.list.array[i];
126     Sensors::iterator it;
127     it = sensors.upper_bound(OID(&vb->name));
128     if (it == sensors.end())
129         {
130         printfd(__FILE__, "SMUX::GetNextRequestHandler() - '%s' not found\n", OID(&vb->name).ToString().c_str());
131         SendGetResponseErrorPDU(sock, getRequest,
132                                 PDU__error_status_noSuchName, i);
133         return true;
134         }
135
136     VarBind_t * newVb = static_cast<VarBind_t *>(calloc(1, sizeof(VarBind_t)));
137     assert(newVb && "Enought mempry to allocate VarBind_t");
138
139     it->first.ToOID(&newVb->name);
140     it->second->GetValue(&newVb->value);
141
142     ASN_SEQUENCE_ADD(varBindList, newVb);
143     }
144
145 SendGetResponsePDU(sock, msg);
146 asn_fprint(stderr, &asn_DEF_PDU, msg);
147 ASN_STRUCT_FREE(asn_DEF_GetResponse_PDU, msg);
148 return false;
149 }
150
151 bool SMUX::SetRequestHandler(const PDUs_t * pdus)
152 {
153 printfd(__FILE__, "SMUX::SetRequestHandler()\n");
154 asn_fprint(stderr, &asn_DEF_PDUs, pdus);
155 SendGetResponseErrorPDU(sock, &pdus->choice.set_request,
156                         PDU__error_status_readOnly, 0);
157 return false;
158 }