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]), arcPos);
51 std::string OI2String(OBJECT_IDENTIFIER_t * oi)
56 int count = OBJECT_IDENTIFIER_get_arcs(oi, arcs, sizeof(arcs[0]), 1024);
61 for (int i = 0; i < count; ++i)
65 strprintf(&arc, "%d", arcs[i]);
72 bool SendOpenPDU(int fd)
74 const char * description = "Stg SMUX Plugin";
78 memset(&msg, 0, sizeof(msg));
80 msg.present = OpenPDU_PR_simple;
81 asn_long2INTEGER(&msg.choice.simple.version, SimpleOpen__version_version_1);
82 if (!String2OI(PEN_PREFIX, &msg.choice.simple.identity))
85 "SendOpenPDU() - failed to convert string to OBJECT_IDENTIFIER\n");
88 OCTET_STRING_fromString(&msg.choice.simple.description, description);
89 OCTET_STRING_fromString(&msg.choice.simple.password, "");
92 error = der_encode_to_buffer(&asn_DEF_OpenPDU, &msg, buffer, sizeof(buffer));
94 ASN_STRUCT_FREE_CONTENTS_ONLY(asn_DEF_OpenPDU, &msg);
96 if (error.encoded == -1)
98 printfd(__FILE__, "Could not encode OpenPDU (at %s)\n",
99 error.failed_type ? error.failed_type->name : "unknown");
104 write(fd, buffer, error.encoded);
109 int SendClosePDU(int fd)
113 memset(&msg, 0, sizeof(msg));
115 asn_long2INTEGER(&msg, ClosePDU_goingDown);
118 asn_enc_rval_t error;
119 error = der_encode_to_buffer(&asn_DEF_ClosePDU, &msg, buffer, sizeof(buffer));
121 ASN_STRUCT_FREE_CONTENTS_ONLY(asn_DEF_ClosePDU, &msg);
123 if (error.encoded == -1)
125 printfd(__FILE__, "Could not encode ClosePDU (at %s)\n",
126 error.failed_type ? error.failed_type->name : "unknown");
131 write(fd, buffer, error.encoded);
136 int SendRReqPDU(int fd)
138 int oid[] = {1, 3, 6, 1, 4, 1, 38313, 1};
139 asn_enc_rval_t error;
142 memset(&msg, 0, sizeof(msg));
145 asn_long2INTEGER(&msg.operation, RReqPDU__operation_readOnly);
146 OBJECT_IDENTIFIER_set_arcs(&msg.subtree,
152 error = der_encode_to_buffer(&asn_DEF_RReqPDU, &msg, buffer, sizeof(buffer));
154 ASN_STRUCT_FREE_CONTENTS_ONLY(asn_DEF_RReqPDU, &msg);
156 if (error.encoded == -1)
158 printfd(__FILE__, "Could not encode RReqPDU (at %s)\n",
159 error.failed_type ? error.failed_type->name : "unknown");
164 write(fd, buffer, error.encoded);
169 SMUX_PDUs_t * RecvSMUXPDUs(int fd)
172 SMUX_PDUs_t * pdus = NULL;
174 memset(buffer, 0, sizeof(buffer));
176 size_t length = read(fd, buffer, sizeof(buffer));
179 asn_dec_rval_t error;
180 error = ber_decode(0, &asn_DEF_SMUX_PDUs, (void **)&pdus, buffer, length);
182 if(error.code != RC_OK)
184 printfd(__FILE__, "Failed to decode PDUs at byte %ld\n",
185 (long)error.consumed);
191 int SendGetResponsePDU(int fd, GetResponse_PDU_t * getResponse)
193 asn_enc_rval_t error;
196 error = der_encode_to_buffer(&asn_DEF_GetResponse_PDU, getResponse, buffer,
199 if (error.encoded == -1)
201 printfd(__FILE__, "Could not encode GetResponsePDU (at %s)\n",
202 error.failed_type ? error.failed_type->name : "unknown");
207 write(fd, buffer, error.encoded);
212 int SendGetResponseErrorPDU(int fd,
213 const PDU_t * getRequest,
217 asn_enc_rval_t error;
218 GetResponse_PDU_t msg;
220 memset(&msg, 0, sizeof(msg));
223 asn_INTEGER2long(&getRequest->request_id, &id);
224 asn_long2INTEGER(&msg.request_id, id);
225 asn_long2INTEGER(&msg.error_status, errorStatus);
226 asn_long2INTEGER(&msg.error_index, errorIndex);
229 error = der_encode_to_buffer(&asn_DEF_GetResponse_PDU, &msg, buffer,
232 ASN_STRUCT_FREE_CONTENTS_ONLY(asn_DEF_GetResponse_PDU, &msg);
234 if (error.encoded == -1)
236 printfd(__FILE__, "Could not encode GetResponsePDU for error (at %s)\n",
237 error.failed_type ? error.failed_type->name : "unknown");
242 write(fd, buffer, error.encoded);