00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020 #include <string.h>
00021 #include "log.h"
00022 #include "hsms_config.h"
00023
00024 #define HSMS_CONFIG_LOG_LEVEL 5
00025
00026 using namespace freesecs;
00027
00028 char *hsms_config_t::str_err[] = {"No errors", "Invalid value", "Invalid token"};
00029
00030 hsms_config_t::hsms_config_t(const char *_name)
00031 :name(_name)
00032 ,type(UNDEFINED)
00033 ,port(0)
00034 ,t6(0)
00035 ,t8(0)
00036 ,linktest(0)
00037 ,session_id(0)
00038 ,parse_flags(0)
00039 {
00040 }
00041
00042 hsms_config_t::hsms_config_t(const hsms_config_t& other)
00043 {
00044 this->operator=(other);
00045 }
00046
00047 hsms_config_t&
00048 hsms_config_t::operator = (const hsms_config_t& other)
00049 {
00050 name = other.name;
00051 type = other.type;
00052 port = other.port;
00053 t6 = other.t6;
00054 t8 = other.t8;
00055 linktest = other.linktest;
00056 session_id = other.session_id;
00057 parse_flags = other.parse_flags;
00058
00059 return *this;
00060 }
00061
00062 hsms_config_t::~hsms_config_t()
00063 {
00064 }
00065
00066 int
00067 hsms_config_t::set_attr(const char *name, const char *value)
00068 {
00069 int res = 0, i;
00070 if(0 == strcmp(name, "port"))
00071 {
00072 port = atoi(value);
00073 if(port)
00074 {
00075 parse_flags |= PORT;
00076 }
00077 else
00078 {
00079 res = INVALID_VALUE;
00080 }
00081 }
00082 else if(0 == strcmp(name, "t6"))
00083 {
00084 t6 = atoi(value);
00085 if(t6)
00086 {
00087 parse_flags |= T6;
00088 }
00089 else
00090 {
00091 res = INVALID_VALUE;
00092 }
00093 }
00094 else if(0 == strcmp(name, "t8"))
00095 {
00096 t8 = atoi(value);
00097 if(t8)
00098 {
00099 parse_flags |= T8;
00100 }
00101 else
00102 {
00103 res = INVALID_VALUE;
00104 }
00105 }
00106 else if(0 == strcmp(name, "linktest"))
00107 {
00108 i = atoi(value);
00109 if(0 <= i)
00110 {
00111 linktest = i;
00112 parse_flags |= LINKTEST;
00113 }
00114 else
00115 {
00116 res = INVALID_VALUE;
00117 }
00118 }
00119 else if(0 == strcmp(name, "session_id"))
00120 {
00121 session_id = atoi(value);
00122 if(0 < session_id)
00123 {
00124 parse_flags |= SESSION_ID;
00125 }
00126 else
00127 {
00128 res = INVALID_VALUE;
00129 }
00130 }
00131
00132
00133 else if(0 == strcmp(name, "cnx")){}
00134 else if(0 == strcmp(name, "atimeouts")){}
00135 else if(0 == strcmp(name, "ptimeouts")){}
00136 else
00137 {
00138 res = INVALID_TOKEN;
00139 }
00140 return res;
00141 }
00142
00143 void
00144 hsms_config_t::complete()
00145 {
00146 parse_flags |= COMPLETED;
00147 }
00148 bool
00149 hsms_config_t::is_valid()const
00150 {
00151 return (parse_flags & PORT) > 0
00152 && (parse_flags & T6) > 0
00153 && (parse_flags & T8) > 0
00154 && (parse_flags & SESSION_ID) > 0
00155 && (parse_flags & UNEXPECTED) == 0
00156 && (parse_flags & COMPLETED) > 0;
00157 }
00158
00159 hsms_passive_config_t::hsms_passive_config_t(const char *name)
00160 :hsms_config_t(name)
00161 {
00162 type = PASSIVE;
00163 }
00164
00165 hsms_passive_config_t::hsms_passive_config_t(const hsms_passive_config_t &other)
00166 :hsms_config_t(other)
00167 {
00168 this->operator=(other);
00169 }
00170
00171 hsms_passive_config_t &
00172 hsms_passive_config_t::operator = (const hsms_passive_config_t & other)
00173 {
00174 this->hsms_config_t::operator=(other);
00175 t7 = other.t7;
00176
00177 return *this;
00178
00179 }
00180
00181 bool
00182 hsms_passive_config_t::is_valid()const
00183 {
00184 return hsms_config_t::is_valid()
00185 && (parse_flags & hsms_config_t::T7) > 0;
00186 }
00187
00188 int
00189 hsms_passive_config_t::set_attr(const char *name, const char *value)
00190 {
00191 int res = 0;
00192 if(0 == (res = hsms_config_t::set_attr(name, value)))
00193 {
00194 }
00195 else if(0 == strcmp(name, "t7"))
00196 {
00197 res = hsms_config_t::NO_ERROR;
00198 t7 = atoi(value);
00199 if(t7)
00200 {
00201 parse_flags |= hsms_config_t::T7;
00202 }
00203 else
00204 {
00205 res = INVALID_VALUE;
00206 }
00207 }
00208 else
00209 {
00210 res = INVALID_TOKEN;
00211 parse_flags |= hsms_config_t::UNEXPECTED;
00212 }
00213 return res;
00214 }
00215
00216 hsms_active_config_t::hsms_active_config_t(const char *name)
00217 :hsms_config_t(name)
00218 {
00219 type = ACTIVE;
00220 }
00221
00222 hsms_active_config_t::hsms_active_config_t(const hsms_active_config_t& other)
00223 :hsms_config_t(other)
00224 {
00225 this->operator=(other);
00226 }
00227
00228 hsms_active_config_t &
00229 hsms_active_config_t::operator = (const hsms_active_config_t& other)
00230 {
00231 this->hsms_config_t::operator=(other);
00232 t5 = other.t5;
00233 ip = other.ip;
00234
00235 return *this;
00236 }
00237
00238 bool
00239 hsms_active_config_t::is_valid()const
00240 {
00241 return hsms_config_t::is_valid()
00242 && (parse_flags & hsms_config_t::T5) > 0
00243 && (parse_flags & hsms_config_t::IP) > 0;
00244 }
00245
00246 int
00247 hsms_active_config_t::set_attr(const char *name, const char *value)
00248 {
00249 int res = 0;
00250 if(0 == (res = hsms_config_t::set_attr(name, value)))
00251 {
00252 }
00253 else if(0 == strcmp(name, "t5"))
00254 {
00255 res = hsms_config_t::NO_ERROR;
00256 t5 = atoi(value);
00257 if(t5)
00258 {
00259 parse_flags |= hsms_config_t::T5;
00260 }
00261 else
00262 {
00263 res = INVALID_VALUE;
00264 }
00265 }
00266 else if(0 == strcmp(name, "ip"))
00267 {
00268 res = hsms_config_t::NO_ERROR;
00269 ip = value;
00270 if(0 < ip.length())
00271 {
00272 parse_flags |= hsms_config_t::IP;
00273 }
00274 else
00275 {
00276 res = INVALID_VALUE;
00277 }
00278 }
00279 else
00280 {
00281 res = INVALID_TOKEN;
00282 parse_flags |= hsms_config_t::UNEXPECTED;
00283 }
00284 return res;
00285 }
00286
00287 hsms_config_parser_t::hsms_config_parser_t()
00288 :curr_cnx(NULL), process(false)
00289 {
00290 if(!(p = XML_ParserCreate(NULL)))
00291 {
00292 TRACE_ERROR("Couldn't allocate memory for parser");
00293 throw;
00294 }
00295
00296 XML_SetUserData(p, (void*)this);
00297
00298 XML_SetElementHandler(p, start_handler, end_handler);
00299 XML_SetCharacterDataHandler(p, char_handler);
00300 }
00301
00302 hsms_config_parser_t::~hsms_config_parser_t()
00303 {
00304 XML_ParserFree(p);
00305 }
00306
00307 int
00308 hsms_config_parser_t::parse(const char *s, int len, int is_final)
00309 {
00310 return XML_Parse(p, s, len, is_final);
00311 }
00312
00313 static char item_name[128];
00314
00315 void
00316 hsms_config_parser_t::start_handler(void *data, const char *el, const char **attr)
00317 {
00318 hsms_config_parser_t *hsms_p = (hsms_config_parser_t*)data;
00319 if(0 == strcmp(el, "hsms"))
00320 {
00321 hsms_p->process = true;
00322 }
00323 if(!hsms_p->process)
00324 {
00325 return;
00326 }
00327 if(0 == strcmp(el, "cnx"))
00328 {
00329 if(NULL == attr
00330 || NULL == attr[0]
00331 || NULL == attr[1]
00332 || NULL == attr[2]
00333 || NULL == attr[3])
00334 {
00335 TRACE_ERROR("!!!!CNX ATTRIBUTE ERROR!!!!!\n");
00336 }
00337 else
00338 {
00339 if(0 == strcmp(attr[3], "passive"))
00340 {
00341
00342
00343
00344
00345 hsms_p->curr_cnx = new hsms_passive_config_t(attr[1]);
00346 }
00347 else if(0 == strcmp(attr[3], "active"))
00348 {
00349
00350
00351
00352
00353 hsms_p->curr_cnx = new hsms_active_config_t(attr[1]);
00354 }
00355 else
00356 {
00357 TRACE_ERROR("!!!!UNKNOWN CNX TYPE: %s!!!!!\n", attr[3]);
00358 hsms_p->curr_cnx = NULL;
00359 }
00360 }
00361 }
00362 memset(item_name, 0, sizeof(item_name));
00363 strncpy(item_name, el, sizeof(item_name)-1);
00364 }
00365
00366 void
00367 hsms_config_parser_t::end_handler(void *data, const char *el)
00368 {
00369 hsms_config_parser_t *hsms_p = (hsms_config_parser_t*)data;
00370
00371 if(0 == strcmp(el, "hsms"))
00372 {
00373 hsms_p->process = false;
00374 }
00375
00376 else if( 0 == strcmp(el, "cnx")
00377 && NULL != hsms_p->curr_cnx
00378 && true == hsms_p->process )
00379 {
00380 hsms_p->curr_cnx->complete();
00381 TRACE_DEBUG(HSMS_CONFIG_LOG_LEVEL, "%s: cnx %s valid: %d\n",
00382 __func__,
00383 hsms_p->curr_cnx->name.c_str(),
00384 hsms_p->curr_cnx->is_valid());
00385
00386 if(hsms_config_t::PASSIVE == hsms_p->curr_cnx->type)
00387 {
00388 hsms_p->passive_configs.push_back(*((hsms_passive_config_t*)hsms_p->curr_cnx));
00389 }
00390 else if(hsms_config_t::ACTIVE == hsms_p->curr_cnx->type)
00391 {
00392 hsms_p->active_configs.push_back(*((hsms_active_config_t*)hsms_p->curr_cnx));
00393 }
00394 delete hsms_p->curr_cnx;
00395 hsms_p->curr_cnx = NULL;
00396 }
00397 memset(item_name, 0, sizeof(item_name));
00398 }
00399
00400 void
00401 hsms_config_parser_t::char_handler(void *data, const char *txt, int txtlen)
00402 {
00403 static const int buflen = 128;
00404 static char buf[buflen];
00405 int copy_len, line_number, error;
00406 std::string err_str;
00407
00408 hsms_config_parser_t *hsms_p = (hsms_config_parser_t*)data;
00409 if(NULL == hsms_p->curr_cnx
00410 || false == hsms_p->process)
00411 {
00412 return;
00413 }
00414
00415 if(0 == strlen(item_name) || 0 == txtlen)
00416 {
00417 return;
00418 }
00419
00420 copy_len = buflen-1 < txtlen ? buflen-1 : txtlen;
00421 memset(buf, 0, sizeof(buf));
00422 strncpy(buf, txt, copy_len);
00423
00424 if(hsms_config_t::NO_ERROR != (error = hsms_p->curr_cnx->set_attr(item_name, buf)))
00425 {
00426 line_number = XML_GetCurrentLineNumber(hsms_p->p);
00427
00428 sprintf(buf, "Config error at line %d: %s", line_number, hsms_config_t::str_err[error]);
00429 err_str = buf;
00430 }
00431 }
00432