1 #include <unistd.h> // write
3 #include <cstring> // memset
6 #include "stg/common.h"
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"
17 bool String2OI(const std::string & str, OBJECT_IDENTIFIER_t * oi)
19 size_t left = 0, pos = 0, arcPos = 0;
21 pos = str.find_first_of('.', left);
25 pos = str.find_first_of('.', left);
27 while (pos != std::string::npos)
30 if (str2x(str.substr(left, left - pos), arc))
36 pos = str.find_first_of('.', left);
38 if (left < str.length())
41 if (str2x(str.substr(left, left - pos), arc))
47 OBJECT_IDENTIFIER_set_arcs(oi, arcs, sizeof(arcs[0]), static_cast<unsigned int>(arcPos));
51 bool SendOpenPDU(int fd)
53 const char * description = "Stg SMUX Plugin";
57 memset(&msg, 0, sizeof(msg));
59 msg.present = OpenPDU_PR_simple;
60 asn_long2INTEGER(&msg.choice.simple.version, SimpleOpen__version_version_1);
61 if (!String2OI(PEN_PREFIX, &msg.choice.simple.identity))
64 "SendOpenPDU() - failed to convert string to OBJECT_IDENTIFIER\n");
67 OCTET_STRING_fromString(&msg.choice.simple.description, description);
68 OCTET_STRING_fromString(&msg.choice.simple.password, "");
71 error = der_encode_to_buffer(&asn_DEF_OpenPDU, &msg, buffer, sizeof(buffer));
73 ASN_STRUCT_FREE_CONTENTS_ONLY(asn_DEF_OpenPDU, &msg);
75 if (error.encoded == -1)
77 printfd(__FILE__, "Could not encode OpenPDU (at %s)\n",
78 error.failed_type ? error.failed_type->name : "unknown");
83 if (write(fd, buffer, error.encoded) < 0)
85 printfd(__FILE__, "Failed to send OpenPDU: %s\n", strerror(errno));
92 bool SendClosePDU(int fd)
96 memset(&msg, 0, sizeof(msg));
98 asn_long2INTEGER(&msg, ClosePDU_goingDown);
101 asn_enc_rval_t error;
102 error = der_encode_to_buffer(&asn_DEF_ClosePDU, &msg, buffer, sizeof(buffer));
104 ASN_STRUCT_FREE_CONTENTS_ONLY(asn_DEF_ClosePDU, &msg);
106 if (error.encoded == -1)
108 printfd(__FILE__, "Could not encode ClosePDU (at %s)\n",
109 error.failed_type ? error.failed_type->name : "unknown");
114 if (write(fd, buffer, error.encoded) < 0)
116 printfd(__FILE__, "Failed to send ClosePDU: %s\n", strerror(errno));
123 bool SendRReqPDU(int fd)
125 int oid[] = {1, 3, 6, 1, 4, 1, 38313, 1};
126 asn_enc_rval_t error;
129 memset(&msg, 0, sizeof(msg));
132 asn_long2INTEGER(&msg.operation, RReqPDU__operation_readOnly);
133 OBJECT_IDENTIFIER_set_arcs(&msg.subtree,
139 error = der_encode_to_buffer(&asn_DEF_RReqPDU, &msg, buffer, sizeof(buffer));
141 ASN_STRUCT_FREE_CONTENTS_ONLY(asn_DEF_RReqPDU, &msg);
143 if (error.encoded == -1)
145 printfd(__FILE__, "Could not encode RReqPDU (at %s)\n",
146 error.failed_type ? error.failed_type->name : "unknown");
151 if (write(fd, buffer, error.encoded) < 0)
153 printfd(__FILE__, "Failed to send RReqPDU: %s\n", strerror(errno));
160 SMUX_PDUs_t * RecvSMUXPDUs(int fd)
163 SMUX_PDUs_t * pdus = NULL;
165 memset(buffer, 0, sizeof(buffer));
167 size_t length = read(fd, buffer, sizeof(buffer));
170 asn_dec_rval_t error;
171 error = ber_decode(0, &asn_DEF_SMUX_PDUs, (void **)&pdus, buffer, length);
173 if(error.code != RC_OK)
175 printfd(__FILE__, "Failed to decode PDUs at byte %ld\n",
176 (long)error.consumed);
182 bool SendGetResponsePDU(int fd, GetResponse_PDU_t * getResponse)
184 asn_enc_rval_t error;
187 error = der_encode_to_buffer(&asn_DEF_GetResponse_PDU, getResponse, buffer,
190 if (error.encoded == -1)
192 printfd(__FILE__, "Could not encode GetResponsePDU (at %s)\n",
193 error.failed_type ? error.failed_type->name : "unknown");
198 if (write(fd, buffer, error.encoded) < 0)
200 printfd(__FILE__, "Failed to send GetResponsePDU: %s\n", strerror(errno));
207 bool SendGetResponseErrorPDU(int fd,
208 const PDU_t * getRequest,
212 asn_enc_rval_t error;
213 GetResponse_PDU_t msg;
215 memset(&msg, 0, sizeof(msg));
218 asn_INTEGER2long(&getRequest->request_id, &id);
219 asn_long2INTEGER(&msg.request_id, id);
220 asn_long2INTEGER(&msg.error_status, errorStatus);
221 asn_long2INTEGER(&msg.error_index, errorIndex);
224 error = der_encode_to_buffer(&asn_DEF_GetResponse_PDU, &msg, buffer,
227 ASN_STRUCT_FREE_CONTENTS_ONLY(asn_DEF_GetResponse_PDU, &msg);
229 if (error.encoded == -1)
231 printfd(__FILE__, "Could not encode GetResponsePDU for error (at %s)\n",
232 error.failed_type ? error.failed_type->name : "unknown");
237 if (write(fd, buffer, error.encoded) < 0)
239 printfd(__FILE__, "Failed to send GetResponseErrorPDU: %s\n", strerror(errno));