1 #include <sys/select.h>
2 #include <unistd.h> // write
4 #include <cstring> // memset
7 #include "stg/common.h"
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"
18 bool WaitPackets(int sd)
28 int res = select(sd + 1, &rfds, NULL, NULL, &tv);
29 if (res == -1) // Error
32 printfd(__FILE__, "Error on select: '%s'\n", strerror(errno));
36 if (res == 0) // Timeout
42 bool String2OI(const std::string & str, OBJECT_IDENTIFIER_t * oi)
44 size_t left = 0, pos = 0, arcPos = 0;
46 pos = str.find_first_of('.', left);
50 pos = str.find_first_of('.', left);
52 while (pos != std::string::npos)
55 if (str2x(str.substr(left, left - pos), arc))
61 pos = str.find_first_of('.', left);
63 if (left < str.length())
66 if (str2x(str.substr(left, left - pos), arc))
72 printfd(__FILE__, "String2OI() - arcPos: %d\n", arcPos);
73 OBJECT_IDENTIFIER_set_arcs(oi, arcs, sizeof(arcs[0]), arcPos);
77 std::string OI2String(OBJECT_IDENTIFIER_t * oi)
82 int count = OBJECT_IDENTIFIER_get_arcs(oi, arcs, sizeof(arcs[0]), 1024);
87 for (int i = 0; i < count; ++i)
91 strprintf(&arc, "%d", arcs[i]);
98 bool SendOpenPDU(int fd)
100 const char * description = "Stg SMUX Plugin";
101 asn_enc_rval_t error;
104 memset(&msg, 0, sizeof(msg));
106 msg.present = OpenPDU_PR_simple;
107 asn_long2INTEGER(&msg.choice.simple.version, SimpleOpen__version_version_1);
108 if (!String2OI(PEN_PREFIX, &msg.choice.simple.identity))
111 "SendOpenPDU() - failed to convert string to OBJECT_IDENTIFIER\n");
114 OCTET_STRING_fromString(&msg.choice.simple.description, description);
115 OCTET_STRING_fromString(&msg.choice.simple.password, "");
118 error = der_encode_to_buffer(&asn_DEF_OpenPDU, &msg, buffer, sizeof(buffer));
120 if (error.encoded == -1)
122 printfd(__FILE__, "Could not encode OpenPDU (at %s)\n",
123 error.failed_type ? error.failed_type->name : "unknown");
128 write(fd, buffer, error.encoded);
129 printfd(__FILE__, "OpenPDU encoded successfully to %d bytes\n",
135 int SendClosePDU(int fd)
139 memset(&msg, 0, sizeof(msg));
141 asn_long2INTEGER(&msg, ClosePDU_goingDown);
144 asn_enc_rval_t error;
145 error = der_encode_to_buffer(&asn_DEF_ClosePDU, &msg, buffer, sizeof(buffer));
147 if (error.encoded == -1)
149 printfd(__FILE__, "Could not encode ClosePDU (at %s)\n",
150 error.failed_type ? error.failed_type->name : "unknown");
155 write(fd, buffer, error.encoded);
156 printfd(__FILE__, "ClosePDU encoded successfully\n");
161 int SendRReqPDU(int fd)
163 int oid[] = {1, 3, 6, 1, 4, 1, 38313, 1};
164 asn_enc_rval_t error;
167 memset(&msg, 0, sizeof(msg));
170 asn_long2INTEGER(&msg.operation, RReqPDU__operation_readOnly);
171 OBJECT_IDENTIFIER_set_arcs(&msg.subtree,
177 error = der_encode_to_buffer(&asn_DEF_RReqPDU, &msg, buffer, sizeof(buffer));
179 if (error.encoded == -1)
181 printfd(__FILE__, "Could not encode RReqPDU (at %s)\n",
182 error.failed_type ? error.failed_type->name : "unknown");
187 write(fd, buffer, error.encoded);
188 printfd(__FILE__, "RReqPDU encoded successfully to %d bytes\n",
194 SMUX_PDUs_t * RecvSMUXPDUs(int fd)
197 SMUX_PDUs_t * pdus = NULL;
199 memset(buffer, 0, sizeof(buffer));
201 size_t length = read(fd, buffer, sizeof(buffer));
204 asn_dec_rval_t error;
205 error = ber_decode(0, &asn_DEF_SMUX_PDUs, (void **)&pdus, buffer, length);
206 if(error.code != RC_OK)
208 printfd(__FILE__, "Failed to decode PDUs at byte %ld\n",
209 (long)error.consumed);
215 int SendGetResponsePDU(int fd, GetResponse_PDU_t * getResponse)
217 asn_enc_rval_t error;
220 error = der_encode_to_buffer(&asn_DEF_GetResponse_PDU, getResponse, buffer,
223 if (error.encoded == -1)
225 printfd(__FILE__, "Could not encode GetResponsePDU (at %s)\n",
226 error.failed_type ? error.failed_type->name : "unknown");
231 write(fd, buffer, error.encoded);
232 printfd(__FILE__, "GetResponsePDU encoded successfully to %d bytes\n",
238 int SendGetResponseErrorPDU(int fd,
239 const PDU_t * getRequest,
243 asn_enc_rval_t error;
244 GetResponse_PDU_t msg;
246 memset(&msg, 0, sizeof(msg));
248 msg.request_id = getRequest->request_id;
249 asn_long2INTEGER(&msg.error_status, errorStatus);
250 asn_long2INTEGER(&msg.error_index, errorIndex);
253 error = der_encode_to_buffer(&asn_DEF_GetResponse_PDU, &msg, buffer,
256 if (error.encoded == -1)
258 printfd(__FILE__, "Could not encode GetResponsePDU for error (at %s)\n",
259 error.failed_type ? error.failed_type->name : "unknown");
264 write(fd, buffer, error.encoded);
266 "GetResponsePDU for error encoded successfully to %d bytes\n",