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, 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 uint32_t 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,
141 error = der_encode_to_buffer(&asn_DEF_RReqPDU, &msg, buffer, sizeof(buffer));
143 ASN_STRUCT_FREE_CONTENTS_ONLY(asn_DEF_RReqPDU, &msg);
145 if (error.encoded == -1)
147 printfd(__FILE__, "Could not encode RReqPDU (at %s)\n",
148 error.failed_type ? error.failed_type->name : "unknown");
153 if (write(fd, buffer, error.encoded) < 0)
155 printfd(__FILE__, "Failed to send RReqPDU: %s\n", strerror(errno));
162 SMUX_PDUs_t * RecvSMUXPDUs(int fd)
165 SMUX_PDUs_t * pdus = NULL;
167 memset(buffer, 0, sizeof(buffer));
169 size_t length = read(fd, buffer, sizeof(buffer));
172 asn_dec_rval_t error;
174 error = ber_decode(0, &asn_DEF_SMUX_PDUs, &p, buffer, length);
176 if(error.code != RC_OK)
178 printfd(__FILE__, "Failed to decode PDUs at byte %ld\n",
179 static_cast<long>(error.consumed));
185 bool SendGetResponsePDU(int fd, GetResponse_PDU_t * getResponse)
187 asn_enc_rval_t error;
190 error = der_encode_to_buffer(&asn_DEF_GetResponse_PDU, getResponse, buffer,
193 if (error.encoded == -1)
195 printfd(__FILE__, "Could not encode GetResponsePDU (at %s)\n",
196 error.failed_type ? error.failed_type->name : "unknown");
201 if (write(fd, buffer, error.encoded) < 0)
203 printfd(__FILE__, "Failed to send GetResponsePDU: %s\n", strerror(errno));
210 bool SendGetResponseErrorPDU(int fd,
211 const PDU_t * getRequest,
215 asn_enc_rval_t error;
216 GetResponse_PDU_t msg;
218 memset(&msg, 0, sizeof(msg));
221 asn_INTEGER2long(&getRequest->request_id, &id);
222 asn_long2INTEGER(&msg.request_id, id);
223 asn_long2INTEGER(&msg.error_status, errorStatus);
224 asn_long2INTEGER(&msg.error_index, errorIndex);
227 error = der_encode_to_buffer(&asn_DEF_GetResponse_PDU, &msg, buffer,
230 ASN_STRUCT_FREE_CONTENTS_ONLY(asn_DEF_GetResponse_PDU, &msg);
232 if (error.encoded == -1)
234 printfd(__FILE__, "Could not encode GetResponsePDU for error (at %s)\n",
235 error.failed_type ? error.failed_type->name : "unknown");
240 if (write(fd, buffer, error.encoded) < 0)
242 printfd(__FILE__, "Failed to send GetResponseErrorPDU: %s\n", strerror(errno));