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 bool SendOpenPDU(int fd)
79 const char * description = "Stg SMUX Plugin";
83 memset(&msg, 0, sizeof(msg));
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))
90 "SendOpenPDU() - failed to convert string to OBJECT_IDENTIFIER\n");
93 OCTET_STRING_fromString(&msg.choice.simple.description, description);
94 OCTET_STRING_fromString(&msg.choice.simple.password, "");
97 error = der_encode_to_buffer(&asn_DEF_OpenPDU, &msg, buffer, sizeof(buffer));
99 if (error.encoded == -1)
101 printfd(__FILE__, "Could not encode OpenPDU (at %s)\n",
102 error.failed_type ? error.failed_type->name : "unknown");
107 write(fd, buffer, error.encoded);
108 printfd(__FILE__, "OpenPDU encoded successfully to %d bytes\n",
114 int SendClosePDU(int fd)
118 memset(&msg, 0, sizeof(msg));
120 asn_long2INTEGER(&msg, ClosePDU_goingDown);
123 asn_enc_rval_t error;
124 error = der_encode_to_buffer(&asn_DEF_ClosePDU, &msg, buffer, sizeof(buffer));
126 if (error.encoded == -1)
128 printfd(__FILE__, "Could not encode ClosePDU (at %s)\n",
129 error.failed_type ? error.failed_type->name : "unknown");
134 write(fd, buffer, error.encoded);
135 printfd(__FILE__, "ClosePDU encoded successfully\n");
140 int SendRReqPDU(int fd)
142 int oid[] = {1, 3, 6, 1, 4, 1, 38313, 1};
143 asn_enc_rval_t error;
146 memset(&msg, 0, sizeof(msg));
149 asn_long2INTEGER(&msg.operation, RReqPDU__operation_readOnly);
150 OBJECT_IDENTIFIER_set_arcs(&msg.subtree,
156 error = der_encode_to_buffer(&asn_DEF_RReqPDU, &msg, buffer, sizeof(buffer));
158 if (error.encoded == -1)
160 printfd(__FILE__, "Could not encode RReqPDU (at %s)\n",
161 error.failed_type ? error.failed_type->name : "unknown");
166 write(fd, buffer, error.encoded);
167 printfd(__FILE__, "RReqPDU encoded successfully to %d bytes\n",
173 SMUX_PDUs_t * RecvSMUXPDUs(int fd)
176 SMUX_PDUs_t * pdus = NULL;
178 memset(buffer, 0, sizeof(buffer));
180 size_t length = read(fd, buffer, sizeof(buffer));
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)
187 printfd(__FILE__, "Failed to decode PDUs at byte %ld\n",
188 (long)error.consumed);
194 std::string OI2String(OBJECT_IDENTIFIER_t * oi)
199 int count = OBJECT_IDENTIFIER_get_arcs(oi, arcs, sizeof(arcs[0]), 1024);
204 for (int i = 0; i < count; ++i)
208 strprintf(&arc, "%d", arcs[i]);
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",