]> git.stg.codes - stg.git/blob - projects/stargazer/plugins/other/smux/handlers.cpp
Handlers moved to separate file and added to compilation
[stg.git] / projects / stargazer / plugins / other / smux / handlers.cpp
1 #include "asn1/OpenPDU.h"
2 #include "asn1/ClosePDU.h"
3 #include "asn1/RReqPDU.h"
4 #include "asn1/GetRequest-PDU.h"
5 #include "asn1/GetResponse-PDU.h"
6 #include "asn1/VarBindList.h"
7 #include "asn1/VarBind.h"
8 #include "asn1/OBJECT_IDENTIFIER.h"
9 #include "asn1/ber_decoder.h"
10 #include "asn1/der_encoder.h"
11
12 #include "stg/common.h"
13
14 #include "smux.h"
15
16 std::string OI2String(OBJECT_IDENTIFIER_t * oi);
17 int SendGetResponsePDU(int fd, GetResponse_PDU_t * getResponse);
18 int SendGetResponseErrorPDU(int fd,
19                             const PDU_t * getRequest,
20                             int errorStatus,
21                             int errorIndex);
22
23 bool SMUX::CloseHandler(const SMUX_PDUs_t * pdus)
24 {
25 printfd(__FILE__, "SMUX::CloseHandler()\n");
26 asn_fprint(stderr, &asn_DEF_SMUX_PDUs, pdus);
27 return false;
28 }
29
30 bool SMUX::RegisterResponseHandler(const SMUX_PDUs_t * pdus)
31 {
32 printfd(__FILE__, "SMUX::RegisterResponseHandler()\n");
33 asn_fprint(stderr, &asn_DEF_SMUX_PDUs, pdus);
34 return false;
35 }
36
37 bool SMUX::PDUsRequestHandler(const SMUX_PDUs_t * pdus)
38 {
39 printfd(__FILE__, "SMUX::PDUsRequestHandler()\n");
40 asn_fprint(stderr, &asn_DEF_SMUX_PDUs, pdus);
41 PDUsHandlers::iterator it;
42 it = pdusHandlers.find(pdus->choice.pdus.present);
43 if (it != pdusHandlers.end())
44     {
45     return (this->*(it->second))(&pdus->choice.pdus);
46     }
47 else
48     {
49     switch (pdus->present)
50         {
51         case PDUs_PR_NOTHING:
52             printfd(__FILE__, "SMUX::PDUsRequestHandler() - nothing\n");
53             break;
54         case PDUs_PR_get_response:
55             printfd(__FILE__, "SMUX::PDUsRequestHandler() - get response\n");
56             break;
57         case PDUs_PR_trap:
58             printfd(__FILE__, "SMUX::PDUsRequestHandler() - trap\n");
59             break;
60         default:
61             printfd(__FILE__, "SMUX::PDUsRequestHandler() - undefined\n");
62         }
63     }
64 return false;
65 }
66
67 bool SMUX::CommitOrRollbackHandler(const SMUX_PDUs_t * pdus)
68 {
69 printfd(__FILE__, "SMUX::CommitOrRollbackHandler()\n");
70 asn_fprint(stderr, &asn_DEF_SMUX_PDUs, pdus);
71 return false;
72 }
73
74 bool SMUX::GetRequestHandler(const PDUs_t * pdus)
75 {
76 printfd(__FILE__, "SMUX::GetRequestHandler()\n");
77 asn_fprint(stderr, &asn_DEF_PDUs, pdus);
78 const GetRequest_PDU_t * getRequest = &pdus->choice.get_request;
79 GetResponse_PDU_t msg;
80 VarBindList_t * varBindList = &msg.variable_bindings;
81 memset(&msg, 0, sizeof(msg));
82
83 msg.request_id = getRequest->request_id;
84 asn_long2INTEGER(&msg.error_status, 0);
85 asn_long2INTEGER(&msg.error_index, 0);
86
87 const VarBindList_t * vbl = &getRequest->variable_bindings; 
88 for (int i = 0; i < vbl->list.count; ++i)
89     {
90     VarBind_t * vb = getRequest->variable_bindings.list.array[i];
91     Sensors::iterator it;
92     it = sensors.find(OI2String(&vb->name));
93     if (it == sensors.end())
94         {
95         SendGetResponseErrorPDU(sock, getRequest,
96                                 PDU__error_status_noSuchName, i);
97         return true;
98         }
99
100     VarBind_t newVb;
101     memset(&newVb, 0, sizeof(newVb));
102
103     newVb.name = vb->name;
104     it->second->GetValue(&newVb.value);
105
106     ASN_SEQUENCE_ADD(varBindList, &newVb);
107     }
108
109 SendGetResponsePDU(sock, &msg);
110 asn_fprint(stderr, &asn_DEF_PDU, &msg);
111 return false;
112 }
113
114 bool SMUX::GetNextRequestHandler(const PDUs_t * pdus)
115 {
116 printfd(__FILE__, "SMUX::GetNextRequestHandler()\n");
117 asn_fprint(stderr, &asn_DEF_PDUs, pdus);
118 const GetRequest_PDU_t * getRequest = &pdus->choice.get_request;
119 GetResponse_PDU_t msg;
120 VarBindList_t * varBindList = &msg.variable_bindings;
121 memset(&msg, 0, sizeof(msg));
122
123 msg.request_id = getRequest->request_id;
124 asn_long2INTEGER(&msg.error_status, 0);
125 asn_long2INTEGER(&msg.error_index, 0);
126
127 const VarBindList_t * vbl = &getRequest->variable_bindings; 
128 for (int i = 0; i < vbl->list.count; ++i)
129     {
130     VarBind_t * vb = getRequest->variable_bindings.list.array[i];
131     Sensors::iterator it;
132     it = sensors.upper_bound(OI2String(&vb->name));
133     if (it == sensors.end())
134         {
135         SendGetResponseErrorPDU(sock, getRequest,
136                                 PDU__error_status_noSuchName, i);
137         return true;
138         }
139
140     VarBind_t newVb;
141     memset(&newVb, 0, sizeof(newVb));
142
143     newVb.name = vb->name;
144     it->second->GetValue(&newVb.value);
145
146     ASN_SEQUENCE_ADD(varBindList, &newVb);
147     }
148
149 SendGetResponsePDU(sock, &msg);
150 asn_fprint(stderr, &asn_DEF_PDU, &msg);
151 return false;
152 }
153
154 bool SMUX::SetRequestHandler(const PDUs_t * pdus)
155 {
156 printfd(__FILE__, "SMUX::SetRequestHandler()\n");
157 asn_fprint(stderr, &asn_DEF_PDUs, pdus);
158 SendGetResponseErrorPDU(sock, &pdus->choice.set_request,
159                         PDU__error_status_readOnly, 0);
160 return false;
161 }
162
163 std::string OI2String(OBJECT_IDENTIFIER_t * oi)
164 {
165 std::string res;
166
167 int arcs[1024];
168 int count = OBJECT_IDENTIFIER_get_arcs(oi, arcs, sizeof(arcs[0]), 1024);
169
170 if (count > 1024)
171     return "";
172
173 for (int i = 0; i < count; ++i)
174     {
175     res += ".";
176     std::string arc;
177     strprintf(&arc, "%d", arcs[i]);
178     res += arc;
179     }
180
181 return res;
182 }
183
184 int SendGetResponsePDU(int fd, GetResponse_PDU_t * getResponse)
185 {
186 asn_enc_rval_t error;
187
188 char buffer[1024];
189 error = der_encode_to_buffer(&asn_DEF_GetResponse_PDU, getResponse, buffer,
190                              sizeof(buffer));
191
192 if (error.encoded == -1)
193     {
194     printfd(__FILE__, "Could not encode GetResponsePDU (at %s)\n",
195             error.failed_type ? error.failed_type->name : "unknown");
196     return -1;
197     }
198 else
199     {
200     write(fd, buffer, error.encoded);
201     printfd(__FILE__, "GetResponsePDU encoded successfully to %d bytes\n",
202             error.encoded);
203     }
204 return 0;
205 }
206
207 int SendGetResponseErrorPDU(int fd,
208                             const PDU_t * getRequest,
209                             int errorStatus,
210                             int errorIndex)
211 {
212 asn_enc_rval_t error;
213 GetResponse_PDU_t msg;
214
215 memset(&msg, 0, sizeof(msg));
216
217 msg.request_id = getRequest->request_id;
218 asn_long2INTEGER(&msg.error_status, errorStatus);
219 asn_long2INTEGER(&msg.error_index, errorIndex);
220
221 char buffer[1024];
222 error = der_encode_to_buffer(&asn_DEF_GetResponse_PDU, &msg, buffer,
223                              sizeof(buffer));
224
225 if (error.encoded == -1)
226     {
227     printfd(__FILE__, "Could not encode GetResponsePDU for error (at %s)\n",
228             error.failed_type ? error.failed_type->name : "unknown");
229     return -1;
230     }
231 else
232     {
233     write(fd, buffer, error.encoded);
234     printfd(__FILE__,
235             "GetResponsePDU for error encoded successfully to %d bytes\n",
236             error.encoded);
237     }
238 return 0;
239 }