]> git.stg.codes - stg.git/blob - projects/stargazer/plugins/other/smux/utils.cpp
SMUX plugin code separated to SNMP helper functions and core logic
[stg.git] / projects / stargazer / plugins / other / smux / utils.cpp
1 #include <sys/select.h>
2 #include <unistd.h> // write
3
4 #include <cstring> // memset
5 #include <cerrno>
6
7 #include "stg/common.h"
8
9 #include "asn1/OpenPDU.h"
10 #include "asn1/ClosePDU.h"
11 #include "asn1/RReqPDU.h"
12 #include "asn1/ber_decoder.h"
13 #include "asn1/der_encoder.h"
14
15 #include "pen.h"
16 #include "utils.h"
17
18 bool WaitPackets(int sd)
19 {
20 fd_set rfds;
21 FD_ZERO(&rfds);
22 FD_SET(sd, &rfds);
23
24 struct timeval tv;
25 tv.tv_sec = 0;
26 tv.tv_usec = 500000;
27
28 int res = select(sd + 1, &rfds, NULL, NULL, &tv);
29 if (res == -1) // Error
30     {
31     if (errno != EINTR)
32         printfd(__FILE__, "Error on select: '%s'\n", strerror(errno));
33     return false;
34     }
35
36 if (res == 0) // Timeout
37     return false;
38
39 return true;
40 }
41
42 bool String2OI(const std::string & str, OBJECT_IDENTIFIER_t * oi)
43 {
44 size_t left = 0, pos = 0, arcPos = 0;
45 int arcs[1024];
46 pos = str.find_first_of('.', left);
47 if (pos == 0)
48     {
49     left = 1;
50     pos = str.find_first_of('.', left);
51     }
52 while (pos != std::string::npos)
53     {
54     int arc = 0;
55     if (str2x(str.substr(left, left - pos), arc))
56         {
57         return false;
58         }
59     arcs[arcPos++] = arc;
60     left = pos + 1;
61     pos = str.find_first_of('.', left);
62     }
63 if (left < str.length())
64     {
65     int arc = 0;
66     if (str2x(str.substr(left, left - pos), arc))
67         {
68         return false;
69         }
70     arcs[arcPos++] = arc;
71     }
72 printfd(__FILE__, "String2OI() - arcPos: %d\n", arcPos);
73 OBJECT_IDENTIFIER_set_arcs(oi, arcs, sizeof(arcs[0]), arcPos);
74 return true;
75 }
76
77 bool SendOpenPDU(int fd)
78 {
79 const char * description = "Stg SMUX Plugin";
80 asn_enc_rval_t error;
81 OpenPDU_t msg;
82
83 memset(&msg, 0, sizeof(msg));
84
85 msg.present = OpenPDU_PR_simple;
86 asn_long2INTEGER(&msg.choice.simple.version, SimpleOpen__version_version_1);
87 if (!String2OI(PEN_PREFIX, &msg.choice.simple.identity))
88     {
89     printfd(__FILE__,
90             "SendOpenPDU() - failed to convert string to OBJECT_IDENTIFIER\n");
91     return false;
92     }
93 OCTET_STRING_fromString(&msg.choice.simple.description, description);
94 OCTET_STRING_fromString(&msg.choice.simple.password, "");
95
96 char buffer[1024];
97 error = der_encode_to_buffer(&asn_DEF_OpenPDU, &msg, buffer, sizeof(buffer));
98
99 if (error.encoded == -1)
100     {
101     printfd(__FILE__, "Could not encode OpenPDU (at %s)\n",
102             error.failed_type ? error.failed_type->name : "unknown");
103     return false;
104     }
105 else
106     {
107     write(fd, buffer, error.encoded);
108     printfd(__FILE__, "OpenPDU encoded successfully to %d bytes\n",
109             error.encoded);
110     }
111 return true;
112 }
113
114 int SendClosePDU(int fd)
115 {
116 ClosePDU_t msg;
117
118 memset(&msg, 0, sizeof(msg));
119
120 asn_long2INTEGER(&msg, ClosePDU_goingDown);
121
122 char buffer[1024];
123 asn_enc_rval_t error;
124 error = der_encode_to_buffer(&asn_DEF_ClosePDU, &msg, buffer, sizeof(buffer));
125
126 if (error.encoded == -1)
127     {
128     printfd(__FILE__, "Could not encode ClosePDU (at %s)\n",
129             error.failed_type ? error.failed_type->name : "unknown");
130     return -1;
131     }
132 else
133     {
134     write(fd, buffer, error.encoded);
135     printfd(__FILE__, "ClosePDU encoded successfully\n");
136     }
137 return 0;
138 }
139
140 int SendRReqPDU(int fd)
141 {
142 int oid[] = {1, 3, 6, 1, 4, 1, 38313, 1};
143 asn_enc_rval_t error;
144 RReqPDU_t msg;
145
146 memset(&msg, 0, sizeof(msg));
147
148 msg.priority = 0;
149 asn_long2INTEGER(&msg.operation, RReqPDU__operation_readOnly);
150 OBJECT_IDENTIFIER_set_arcs(&msg.subtree,
151                            oid,
152                            sizeof(oid[0]),
153                            8);
154
155 char buffer[1024];
156 error = der_encode_to_buffer(&asn_DEF_RReqPDU, &msg, buffer, sizeof(buffer));
157
158 if (error.encoded == -1)
159     {
160     printfd(__FILE__, "Could not encode RReqPDU (at %s)\n",
161             error.failed_type ? error.failed_type->name : "unknown");
162     return -1;
163     }
164 else
165     {
166     write(fd, buffer, error.encoded);
167     printfd(__FILE__, "RReqPDU encoded successfully to %d bytes\n",
168             error.encoded);
169     }
170 return 0;
171 }
172
173 SMUX_PDUs_t * RecvSMUXPDUs(int fd)
174 {
175 char buffer[1024];
176 SMUX_PDUs_t * pdus = NULL;
177
178 memset(buffer, 0, sizeof(buffer));
179
180 size_t length = read(fd, buffer, sizeof(buffer));
181 if (length < 1)
182     return NULL;
183 asn_dec_rval_t error;
184 error = ber_decode(0, &asn_DEF_SMUX_PDUs, (void **)&pdus, buffer, length);
185 if(error.code != RC_OK)
186     {
187     printfd(__FILE__, "Failed to decode PDUs at byte %ld\n",
188             (long)error.consumed);
189     return NULL;
190     }
191 return pdus;
192 }
193
194 std::string OI2String(OBJECT_IDENTIFIER_t * oi)
195 {
196 std::string res;
197
198 int arcs[1024];
199 int count = OBJECT_IDENTIFIER_get_arcs(oi, arcs, sizeof(arcs[0]), 1024);
200
201 if (count > 1024)
202     return "";
203
204 for (int i = 0; i < count; ++i)
205     {
206     res += ".";
207     std::string arc;
208     strprintf(&arc, "%d", arcs[i]);
209     res += arc;
210     }
211
212 return res;
213 }
214
215 int SendGetResponsePDU(int fd, GetResponse_PDU_t * getResponse)
216 {
217 asn_enc_rval_t error;
218
219 char buffer[1024];
220 error = der_encode_to_buffer(&asn_DEF_GetResponse_PDU, getResponse, buffer,
221                              sizeof(buffer));
222
223 if (error.encoded == -1)
224     {
225     printfd(__FILE__, "Could not encode GetResponsePDU (at %s)\n",
226             error.failed_type ? error.failed_type->name : "unknown");
227     return -1;
228     }
229 else
230     {
231     write(fd, buffer, error.encoded);
232     printfd(__FILE__, "GetResponsePDU encoded successfully to %d bytes\n",
233             error.encoded);
234     }
235 return 0;
236 }
237
238 int SendGetResponseErrorPDU(int fd,
239                             const PDU_t * getRequest,
240                             int errorStatus,
241                             int errorIndex)
242 {
243 asn_enc_rval_t error;
244 GetResponse_PDU_t msg;
245
246 memset(&msg, 0, sizeof(msg));
247
248 msg.request_id = getRequest->request_id;
249 asn_long2INTEGER(&msg.error_status, errorStatus);
250 asn_long2INTEGER(&msg.error_index, errorIndex);
251
252 char buffer[1024];
253 error = der_encode_to_buffer(&asn_DEF_GetResponse_PDU, &msg, buffer,
254                              sizeof(buffer));
255
256 if (error.encoded == -1)
257     {
258     printfd(__FILE__, "Could not encode GetResponsePDU for error (at %s)\n",
259             error.failed_type ? error.failed_type->name : "unknown");
260     return -1;
261     }
262 else
263     {
264     write(fd, buffer, error.encoded);
265     printfd(__FILE__,
266             "GetResponsePDU for error encoded successfully to %d bytes\n",
267             error.encoded);
268     }
269 return 0;
270 }