2 * @file parameter_impl.cpp
3 * @brief parameter module implementation class.
5 * L7VSD: Linux Virtual Server for Layer7 Load Balancing
6 * Copyright (C) 2008 NTT COMWARE Corporation.
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
23 **********************************************************************/
28 #include "parameter_impl.h"
29 #include "logger_wrapper.h"
31 #include <boost/lexical_cast.hpp>
33 #ifndef INIT_PARAMETER_FILE
34 #define INIT_PARAMETER_FILE "/etc/l7vs/sslproxy/sslproxy.logger_init.cf"
35 #endif //INIT_PARAMETER_FILE
37 #define LINE_LENGTH 4096
39 LOG_CATEGORY_TAG parameter_cat = LOG_CAT_SSLPROXY_PARAMETER;
41 void loadLoggerConf(){
42 l7vs::Logger::getInstance().loadConf();
46 * ParameterImpl class Constructor
48 l7vs::ParameterImpl::ParameterImpl()
53 * Initialize ParameterImpl class
55 bool l7vs::ParameterImpl::init()
63 //add member to component table
64 compTable[PARAM_COMP_ALL] = component("all", NULL);
65 compTable[PARAM_COMP_NOCAT] = component("nocategory", NULL);
66 compTable[PARAM_COMP_LOGGER] = component("logger", loadLoggerConf);
67 compTable[PARAM_COMP_SSLPROXY] = component("sslproxy", NULL);
70 retbool = rereadFile( PARAM_COMP_ALL, INIT_PARAMETER_FILE);
71 /*-------- DEBUG LOG for sslproxy --------*/
72 if (LOG_LV_DEBUG == logger_get_log_level(parameter_cat)) {
73 LOGGER_PUT_LOG_DEBUG(parameter_cat, 4,
74 "function : bool l7vs::ParameterImpl::init() : "
79 /*------ DEBUG LOG END for sslproxy ------*/
85 * checks that an argument is a numeric.
86 * @param[in] const std::string
87 * @return is numeric = true / not numeric = false
89 bool l7vs::ParameterImpl::isNumeric(const std::string& val)
92 boost::lexical_cast<int>(val);
94 catch (boost::bad_lexical_cast& ex) {
101 * delete blank before and behind a sentence
102 * @param[in] const std::string
103 * @return std::string
105 std::string l7vs::ParameterImpl::removebrank( const std::string& val )
107 std::string str = val;
108 //remove brank(head of the sentence and end of the sentence)
109 std::string::iterator its;
111 while( its != str.end() ){
112 if( ( ' ' == *its ) || ( '\t' == *its ) ){
116 if( "" == str )return str;
117 //remove brank(head of the sentence and end of the sentence)
119 while( its != str.begin() ){
120 if( ( ' ' == *its ) || ( '\t' == *its ) ){
129 * Read Parameter from file
130 * @param[in] const std::string
131 * @return success = true / fail = false
133 bool l7vs::ParameterImpl::readParameterFile( const std::string& val )
136 char readbuf[LINE_LENGTH];
138 LOGGER_PUT_LOG_INFO( parameter_cat,1, "read parameter file : %s", val.c_str() );
140 std::vector<std::string> paramlines;
145 std::ifstream ifs( val.c_str() );
150 memset( readbuf, 0, LINE_LENGTH );
151 ifs.getline( readbuf, ( LINE_LENGTH - 1 ) );
152 std::string str = readbuf;
153 str = removebrank( str );
155 if( '#' == str[0] )continue;
157 if( !str.empty() )paramlines.push_back( str );
159 std::string sectionname = compTable[PARAM_COMP_NOCAT].section;
160 for( std::vector<std::string>::iterator it = paramlines.begin();
161 it != paramlines.end(); it++ ){
163 std::string str = *it;
165 std::string tmpstr = str.substr( str.rfind("]")+1 );
166 tmpstr = removebrank( tmpstr.substr( 0, tmpstr.find( "#" ) ) );
167 if( !tmpstr.empty() )continue;
168 //section string validity check
169 //invalid "#","[","]","\" character
170 tmpstr = removebrank( str.substr( str.find( "[" )+1, str.rfind( "]", str.length() )-1 ) );
171 if( std::string::npos != tmpstr.find( "[" ) )tmpstr = compTable[PARAM_COMP_NOCAT].section;
172 if( std::string::npos != tmpstr.find( "]" ) )tmpstr = compTable[PARAM_COMP_NOCAT].section;
173 if( std::string::npos != tmpstr.find( "\\" ) )tmpstr = compTable[PARAM_COMP_NOCAT].section;
174 if( std::string::npos != tmpstr.find( "#" ) )tmpstr = compTable[PARAM_COMP_NOCAT].section;
175 if( std::string::npos != tmpstr.find( "=" ) )tmpstr = compTable[PARAM_COMP_NOCAT].section;
176 sectionname = tmpstr;
178 preparse.insert( std::pair<std::string,std::string>( sectionname,str ) );
186 if( preparse.empty() )retval = false;
191 * read parameter and parse
192 * @param[in] comp PARAMETER_COMPONENT_TAG
193 * @param[in] filename config file name
194 * @return success = true / fail = false
196 bool l7vs::ParameterImpl::rereadFile(PARAMETER_COMPONENT_TAG comp, const std::string& filename)
200 LOGGER_PUT_LOG_INFO( parameter_cat,2, "read parameter : COMPONENT = %s", compTable[comp].section.c_str() );
202 if( !readParameterFile( filename ) )return false;
207 /*-------- DEBUG LOG for sslproxy --------*/
208 if (LOG_LV_DEBUG == logger_get_log_level(parameter_cat)) {
209 LOGGER_PUT_LOG_DEBUG(parameter_cat, 5,
210 "function : bool l7vs::ParameterImpl::rereadFile("
211 "PARAMETER_COMPONENT_TAG comp, "
212 "const std::string& filename) : "
215 compTable[comp].section.c_str(),
218 /*------ DEBUG LOG END for sslproxy ------*/
220 std::multimap<std::string,std::string>::iterator it;
221 std::multimap<std::string,std::string>::iterator ite;
222 if( PARAM_COMP_ALL == comp ){
223 it = preparse.begin();
225 it = preparse.find( compTable[comp].section );
227 ite = preparse.end();
229 for( std::multimap<std::string,std::string>::iterator its = it; its !=ite; its++ ){
230 //be made an invalid line if there is no = in a setting line.
231 if( std::string::npos == its->second.find( "=" ) )continue;
232 std::string key = removebrank( its->second.substr( 0, its->second.find( "=" ) ) );
233 //Key string validity check
234 if( key.empty() )continue;
235 //invalid "#","[","]","\" character
236 if( std::string::npos != key.find( "[" ))continue;
237 if( std::string::npos != key.find( "]" ))continue;
238 if( std::string::npos != key.find( "\\" ))continue;
239 if( std::string::npos != key.find( "#" ) )continue;
241 key = its->first + "." + key;
243 std::string value = removebrank( its->second.substr( its->second.find( "=" )+1, std::string::npos ) );
244 //Value string validity check
245 if( '"' == value[0] ) {
246 if( std::string::npos == value.find( "\"", 1 ) )continue;
247 std::string tmpstr = removebrank( value.substr( value.find( "\"", 1 )+1, value.find( "#", value.find( "\"", 1 )+1 ) ) );
248 if( !tmpstr.empty() )continue;
249 tmpstr = removebrank( value.substr( 1, value.find( "\"", 1 )-1 ) );
250 stringMap.insert( std::pair<std::string, std::string>( key, tmpstr ) );
251 /*-------- DEBUG LOG for sslproxy --------*/
252 if (LOG_LV_DEBUG == logger_get_log_level(parameter_cat)) {
253 LOGGER_PUT_LOG_DEBUG(parameter_cat, 6,
254 "function : bool l7vs::ParameterImpl::rereadFile() : "
261 /*------ DEBUG LOG END for sslproxy ------*/
263 value = value.substr( 0, value.find_first_of( "#", 0 ) );
264 if( std::string::npos != value.find( "\\" ))continue;
265 if( std::string::npos != value.find( "=" ))continue;
266 if( value.empty() )continue;
267 if( isNumeric( value ) ){
269 intMap.insert( std::pair<std::string, int>( key, boost::lexical_cast<int>( value ) ) );
270 /*-------- DEBUG LOG for sslproxy --------*/
271 if (LOG_LV_DEBUG == logger_get_log_level(parameter_cat)) {
272 LOGGER_PUT_LOG_DEBUG(parameter_cat, 7,
273 "function : bool l7vs::ParameterImpl::rereadFile() : "
280 /*------ DEBUG LOG END for sslproxy ------*/
282 catch (boost::bad_lexical_cast& ex) {
283 stringMap.insert( std::pair<std::string, std::string>( key, value ) );
284 /*-------- DEBUG LOG for sslproxy --------*/
285 if (LOG_LV_DEBUG == logger_get_log_level(parameter_cat)) {
286 LOGGER_PUT_LOG_DEBUG(parameter_cat, 8,
287 "function : bool l7vs::ParameterImpl::rereadFile() : "
294 /*------ DEBUG LOG END for sslproxy ------*/
297 stringMap.insert( std::pair<std::string, std::string>( key, value ) );
298 /*-------- DEBUG LOG for sslproxy --------*/
299 if (LOG_LV_DEBUG == logger_get_log_level(parameter_cat)) {
300 LOGGER_PUT_LOG_DEBUG(parameter_cat, 9,
301 "function : bool l7vs::ParameterImpl::rereadFile() : "
308 /*------ DEBUG LOG END for sslproxy ------*/
313 std::map<PARAMETER_COMPONENT_TAG, component>::iterator itr = compTable.find( comp );
314 if( itr != compTable.end() ){
316 if( NULL != itr->second.function )itr->second.function();
318 catch( const std::logic_error& err ){
319 LOGGER_PUT_LOG_ERROR( parameter_cat,1, "set parameter function failure at category: %s", itr->second.section.c_str() );
323 if( PARAM_COMP_ALL == comp ){
324 for( itr = compTable.begin(); itr != compTable.end(); ++itr ){
326 if( NULL != itr->second.function )itr->second.function();
328 catch( const std::logic_error& err ){
329 LOGGER_PUT_LOG_ERROR( parameter_cat,2, "set parameter function failure at category: %s", itr->second.section.c_str() );
339 * Existence of a integer parameter is checked
340 * @param[in] comp PARAMETER_COMPONENT_TAG
341 * @param[in] key key string
342 * @return exist = true / not exist = false
344 bool l7vs::ParameterImpl::isIntExist(const PARAMETER_COMPONENT_TAG comp, const std::string& key)
347 std::string comp_key = compTable[comp].section + "." + key;
348 if( intMap.end() != intMap.find( comp_key ) ){
355 * Existence of a string parameter is checked
356 * @param[in] comp PARAMETER_COMPONENT_TAG
357 * @param[in] key key string
358 * @return exist = true / not exist = false
360 bool l7vs::ParameterImpl::isStringExist(const PARAMETER_COMPONENT_TAG comp, const std::string& key)
363 std::string comp_key = compTable[comp].section + "." + key;
364 if( stringMap.end() != stringMap.find( comp_key ) ){
371 * get a integer parameter
372 * @param[in] comp PARAMETER_COMPONENT_TAG
373 * @param[in] key key string
374 * @return integer data
376 int l7vs::ParameterImpl::getIntValue(const PARAMETER_COMPONENT_TAG comp, const std::string& key)
378 /*-------- DEBUG LOG for sslproxy --------*/
379 if (LOG_LV_DEBUG == logger_get_log_level(parameter_cat) &&
380 comp != PARAM_COMP_LOGGER) {
381 LOGGER_PUT_LOG_DEBUG(parameter_cat, 10,
382 "in_function : int l7vs::ParameterImpl::getIntValue("
383 "const PARAMETER_COMPONENT_TAG comp, "
384 "const std::string& key) : "
387 compTable[comp].section.c_str(),
390 /*------ DEBUG LOG END for sslproxy ------*/
393 std::string comp_key = compTable[comp].section + "." + key;
394 std::multimap<std::string, int>::iterator it = intMap.begin();
395 std::multimap<std::string, int>::iterator ite = intMap.end();
397 for( std::multimap<std::string, int>::iterator its = it; its !=ite; its++ ) {
398 /*-------- DEBUG LOG for sslproxy --------*/
399 if (LOG_LV_DEBUG == logger_get_log_level(parameter_cat) &&
400 comp != PARAM_COMP_LOGGER) {
401 LOGGER_PUT_LOG_DEBUG(parameter_cat, 11,
402 "function : int l7vs::ParameterImpl::getIntValue() : "
403 "Search intMap [%s][%d]",
407 /*------ DEBUG LOG END for sslproxy ------*/
408 if (its->first == comp_key) {
409 /*-------- DEBUG LOG for sslproxy --------*/
410 if (LOG_LV_DEBUG == logger_get_log_level(parameter_cat) &&
411 comp != PARAM_COMP_LOGGER) {
412 LOGGER_PUT_LOG_DEBUG(parameter_cat, 12,
413 "function : int l7vs::ParameterImpl::getIntValue() : "
416 /*------ DEBUG LOG END for sslproxy ------*/
417 retint = its->second;
421 /*-------- DEBUG LOG for sslproxy --------*/
422 if (LOG_LV_DEBUG == logger_get_log_level(parameter_cat) &&
423 comp != PARAM_COMP_LOGGER) {
424 LOGGER_PUT_LOG_DEBUG(parameter_cat, 13,
425 "out_function : int l7vs::ParameterImpl::getIntValue("
426 "const PARAMETER_COMPONENT_TAG comp, "
427 "const std::string& key) : "
431 /*------ DEBUG LOG END for sslproxy ------*/
436 * get a string parameter
437 * @param[in] comp PARAMETER_COMPONENT_TAG
438 * @param[in] key key string
439 * @return string data
441 std::string l7vs::ParameterImpl::getStringValue(const PARAMETER_COMPONENT_TAG comp, const std::string& key)
443 /*-------- DEBUG LOG for sslproxy --------*/
444 if (LOG_LV_DEBUG == logger_get_log_level(parameter_cat) &&
445 comp != PARAM_COMP_LOGGER) {
446 LOGGER_PUT_LOG_DEBUG(parameter_cat, 14,
447 "in_function : std::string l7vs::ParameterImpl::getStringValue("
448 "const PARAMETER_COMPONENT_TAG comp, "
449 "const std::string& key) : "
452 compTable[comp].section.c_str(),
455 /*------ DEBUG LOG END for sslproxy ------*/
457 std::string retstr = "";
458 std::string comp_key = compTable[comp].section + "." + key;
459 std::multimap<std::string, std::string>::iterator it = stringMap.begin();
460 std::multimap<std::string, std::string>::iterator ite = stringMap.end();
462 for( std::multimap<std::string, std::string>::iterator its = it; its !=ite; its++ ) {
463 /*-------- DEBUG LOG for sslproxy --------*/
464 if (LOG_LV_DEBUG == logger_get_log_level(parameter_cat) &&
465 comp != PARAM_COMP_LOGGER) {
466 LOGGER_PUT_LOG_DEBUG(parameter_cat, 15,
467 "function : std::string l7vs::ParameterImpl::getStringValue() : "
468 "Search stringMap [%s][%s]",
470 its->second.c_str());
472 /*------ DEBUG LOG END for sslproxy ------*/
473 if (its->first == comp_key) {
474 /*-------- DEBUG LOG for sslproxy --------*/
475 if (LOG_LV_DEBUG == logger_get_log_level(parameter_cat) &&
476 comp != PARAM_COMP_LOGGER) {
477 LOGGER_PUT_LOG_DEBUG(parameter_cat, 16,
478 "function : std::string l7vs::ParameterImpl::getStringValue() : "
481 /*------ DEBUG LOG END for sslproxy ------*/
482 retstr = its->second;
486 /*-------- DEBUG LOG for sslproxy --------*/
487 if (LOG_LV_DEBUG == logger_get_log_level(parameter_cat) &&
488 comp != PARAM_COMP_LOGGER) {
489 LOGGER_PUT_LOG_DEBUG(parameter_cat, 17,
490 "out_function : std::string l7vs::ParameterImpl::getStringValue("
491 "const PARAMETER_COMPONENT_TAG comp, "
492 "const std::string& key) : "
496 /*------ DEBUG LOG END for sslproxy ------*/
502 * @param[in] comp PARAMETER_COMPONENT_TAG
503 * @param[in] key key string
504 * @param[out] retMap map data
506 void l7vs::ParameterImpl::getStringMapValue(const PARAMETER_COMPONENT_TAG comp, const std::string& key, std::multimap<std::string, std::string>& retMap)
508 /*-------- DEBUG LOG for sslproxy --------*/
509 if (LOG_LV_DEBUG == logger_get_log_level(parameter_cat) &&
510 comp != PARAM_COMP_LOGGER) {
511 LOGGER_PUT_LOG_DEBUG(parameter_cat, 18,
512 "in_function : void l7vs::ParameterImpl::getStringMapValue("
513 "const PARAMETER_COMPONENT_TAG comp, "
514 "const std::string& key, "
515 "std::multimap<std::string, std::string>& retMap) : "
518 compTable[comp].section.c_str(),
521 /*------ DEBUG LOG END for sslproxy ------*/
523 std::string comp_key = compTable[comp].section + "." + key;
524 std::multimap<std::string, std::string>::iterator it = stringMap.begin();
525 std::multimap<std::string, std::string>::iterator ite = stringMap.end();
527 for( std::multimap<std::string, std::string>::iterator its = it; its !=ite; its++ ) {
528 /*-------- DEBUG LOG for sslproxy --------*/
529 if (LOG_LV_DEBUG == logger_get_log_level(parameter_cat) &&
530 comp != PARAM_COMP_LOGGER) {
531 LOGGER_PUT_LOG_DEBUG(parameter_cat, 19,
532 "function : void l7vs::ParameterImpl::getStringMapValue() : "
533 "Search stringMap [%s][%s]",
535 its->second.c_str());
537 /*------ DEBUG LOG END for sslproxy ------*/
538 if (its->first == comp_key) {
539 /*-------- DEBUG LOG for sslproxy --------*/
540 if (LOG_LV_DEBUG == logger_get_log_level(parameter_cat) &&
541 comp != PARAM_COMP_LOGGER) {
542 LOGGER_PUT_LOG_DEBUG(parameter_cat, 20,
543 "function : void l7vs::ParameterImpl::getStringMapValue() : "
544 "Key matched. Insert map.");
546 /*------ DEBUG LOG END for sslproxy ------*/
547 retMap.insert( std::pair<std::string, std::string>( key, its->second ) );
551 /*-------- DEBUG LOG for sslproxy --------*/
552 if (LOG_LV_DEBUG == logger_get_log_level(parameter_cat) &&
553 comp != PARAM_COMP_LOGGER) {
554 LOGGER_PUT_LOG_DEBUG(parameter_cat, 21,
555 "out_function : void l7vs::ParameterImpl::getStringMapValue("
556 "const PARAMETER_COMPONENT_TAG comp, "
557 "const std::string& key, "
558 "std::multimap<std::string, std::string>& retMap)");
560 /*------ DEBUG LOG END for sslproxy ------*/
564 * set-parameter function pointer relates component-tag
565 * @param[in] comp section TAG
566 * @param[in] p_func function pointer
568 void l7vs::ParameterImpl::registerFunctionPointer( const PARAMETER_COMPONENT_TAG comp, void (*p_func)() )
570 compTable[comp].function = p_func;