4 #include "stg/common.h"
6 #pragma GCC diagnostic push
7 #pragma GCC diagnostic ignored "-Wold-style-cast"
8 #include "stg/OpenPDU.h"
9 #include "stg/ClosePDU.h"
10 #include "stg/RReqPDU.h"
11 #include "stg/ber_decoder.h"
12 #include "stg/der_encoder.h"
13 #pragma GCC diagnostic pop
15 #include <cstring> // memset
18 #include <unistd.h> // write
20 bool String2OI(const std::string & str, OBJECT_IDENTIFIER_t * oi)
22 size_t left = 0, pos = 0, arcPos = 0;
24 pos = str.find_first_of('.', left);
28 pos = str.find_first_of('.', left);
30 while (pos != std::string::npos)
33 if (str2x(str.substr(left, left - pos), arc))
39 pos = str.find_first_of('.', left);
41 if (left < str.length())
44 if (str2x(str.substr(left, left - pos), arc))
50 OBJECT_IDENTIFIER_set_arcs(oi, arcs, sizeof(arcs[0]), static_cast<unsigned int>(arcPos));
54 bool SendOpenPDU(int fd)
56 const char * description = "Stg SMUX Plugin";
60 memset(&msg, 0, sizeof(msg));
62 msg.present = OpenPDU_PR_simple;
63 asn_long2INTEGER(&msg.choice.simple.version, SimpleOpen__version_version_1);
64 if (!String2OI(PEN_PREFIX, &msg.choice.simple.identity))
67 "SendOpenPDU() - failed to convert string to OBJECT_IDENTIFIER\n");
70 OCTET_STRING_fromString(&msg.choice.simple.description, description);
71 OCTET_STRING_fromString(&msg.choice.simple.password, "");
74 error = der_encode_to_buffer(&asn_DEF_OpenPDU, &msg, buffer, sizeof(buffer));
76 ASN_STRUCT_FREE_CONTENTS_ONLY(asn_DEF_OpenPDU, &msg);
78 if (error.encoded == -1)
80 printfd(__FILE__, "Could not encode OpenPDU (at %s)\n",
81 error.failed_type ? error.failed_type->name : "unknown");
86 if (write(fd, buffer, error.encoded) < 0)
88 printfd(__FILE__, "Failed to send OpenPDU: %s\n", strerror(errno));
95 bool SendClosePDU(int fd)
99 memset(&msg, 0, sizeof(msg));
101 asn_long2INTEGER(&msg, ClosePDU_goingDown);
104 asn_enc_rval_t error;
105 error = der_encode_to_buffer(&asn_DEF_ClosePDU, &msg, buffer, sizeof(buffer));
107 ASN_STRUCT_FREE_CONTENTS_ONLY(asn_DEF_ClosePDU, &msg);
109 if (error.encoded == -1)
111 printfd(__FILE__, "Could not encode ClosePDU (at %s)\n",
112 error.failed_type ? error.failed_type->name : "unknown");
117 if (write(fd, buffer, error.encoded) < 0)
119 printfd(__FILE__, "Failed to send ClosePDU: %s\n", strerror(errno));
126 bool SendRReqPDU(int fd)
128 int oid[] = {1, 3, 6, 1, 4, 1, 38313, 1};
129 asn_enc_rval_t error;
132 memset(&msg, 0, sizeof(msg));
135 asn_long2INTEGER(&msg.operation, RReqPDU__operation_readOnly);
136 OBJECT_IDENTIFIER_set_arcs(&msg.subtree,
142 error = der_encode_to_buffer(&asn_DEF_RReqPDU, &msg, buffer, sizeof(buffer));
144 ASN_STRUCT_FREE_CONTENTS_ONLY(asn_DEF_RReqPDU, &msg);
146 if (error.encoded == -1)
148 printfd(__FILE__, "Could not encode RReqPDU (at %s)\n",
149 error.failed_type ? error.failed_type->name : "unknown");
154 if (write(fd, buffer, error.encoded) < 0)
156 printfd(__FILE__, "Failed to send RReqPDU: %s\n", strerror(errno));
163 SMUX_PDUs_t * RecvSMUXPDUs(int fd)
166 SMUX_PDUs_t * pdus = NULL;
168 memset(buffer, 0, sizeof(buffer));
170 size_t length = read(fd, buffer, sizeof(buffer));
173 asn_dec_rval_t error;
175 error = ber_decode(0, &asn_DEF_SMUX_PDUs, &p, buffer, length);
177 if(error.code != RC_OK)
179 printfd(__FILE__, "Failed to decode PDUs at byte %ld\n",
180 static_cast<long>(error.consumed));
186 bool SendGetResponsePDU(int fd, GetResponse_PDU_t * getResponse)
188 asn_enc_rval_t error;
191 error = der_encode_to_buffer(&asn_DEF_GetResponse_PDU, getResponse, buffer,
194 if (error.encoded == -1)
196 printfd(__FILE__, "Could not encode GetResponsePDU (at %s)\n",
197 error.failed_type ? error.failed_type->name : "unknown");
202 if (write(fd, buffer, error.encoded) < 0)
204 printfd(__FILE__, "Failed to send GetResponsePDU: %s\n", strerror(errno));
211 bool SendGetResponseErrorPDU(int fd,
212 const PDU_t * getRequest,
216 asn_enc_rval_t error;
217 GetResponse_PDU_t msg;
219 memset(&msg, 0, sizeof(msg));
222 asn_INTEGER2long(&getRequest->request_id, &id);
223 asn_long2INTEGER(&msg.request_id, id);
224 asn_long2INTEGER(&msg.error_status, errorStatus);
225 asn_long2INTEGER(&msg.error_index, errorIndex);
228 error = der_encode_to_buffer(&asn_DEF_GetResponse_PDU, &msg, buffer,
231 ASN_STRUCT_FREE_CONTENTS_ONLY(asn_DEF_GetResponse_PDU, &msg);
233 if (error.encoded == -1)
235 printfd(__FILE__, "Could not encode GetResponsePDU for error (at %s)\n",
236 error.failed_type ? error.failed_type->name : "unknown");
241 if (write(fd, buffer, error.encoded) < 0)
243 printfd(__FILE__, "Failed to send GetResponseErrorPDU: %s\n", strerror(errno));