Main Page   Class Hierarchy   Compound List   File List   Compound Members  

C_TransOCTcap32.cpp

00001 /*
00002  *  This program is free software; you can redistribute it and/or modify
00003  *  it under the terms of the GNU General Public License as published by
00004  *  the Free Software Foundation; either version 2 of the License, or
00005  *  (at your option) any later version.
00006  *
00007  *  This program is distributed in the hope that it will be useful,
00008  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00009  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00010  *  GNU General Public License for more details.
00011  *
00012  *  You should have received a copy of the GNU General Public License
00013  *  along with this program; if not, write to the Free Software
00014  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00015  *
00016  * (c)Copyright 2006 Hewlett-Packard Development Company, LP.
00017  *
00018  */
00019 
00020 #include "C_TransOCTcap32.hpp"
00021 #include "Utils.hpp"
00022 
00023 #include "create_tcap_itu_stack.hpp"
00024 #include "create_tcap_ansi_stack.hpp"
00025 
00026 #include "C_ProtocolExternalFrame.hpp"
00027 
00028 #include "iostream_t.hpp"
00029 #include "string_t.hpp"
00030 #include <regex.h>
00031 // #include <ttlCommon.h>
00032 
00033 
00034 #define LOG_ERROR(m) { \
00035  char L_err [100] ; \
00036  snprintf(L_err, 100, (m)) ; \
00037 (*m_log_error)(L_err); \
00038 }
00039 
00040 #define LOG_ERROR_P1(m,P1) { \
00041  char L_err [100] ; \
00042  snprintf(L_err, 100, (m), (P1)) ; \
00043 (*m_log_error)(L_err); \
00044 }
00045 
00046 #define LOG_ERROR_P2(m,P1,P2) { \
00047  char L_err [100] ; \
00048  snprintf(L_err, 100, (m), (P1),(P2)) ; \
00049 (*m_log_error)(L_err); \
00050 }
00051 
00052 
00053 #define LOG_ALL(m) { \
00054  char L_msg [100] ; \
00055  snprintf(L_msg, 100, (m)) ; \
00056 (*m_log_info)(L_msg); \
00057 }
00058 
00059 #define LOG_ALL_P1(m,P1) { \
00060  char L_msg [100] ; \
00061  snprintf(L_msg, 100, (m), (P1)) ; \
00062 (*m_log_info)(L_msg); \
00063 }
00064 
00065 #define LOG_ALL_P2(m,P1,P2) { \
00066  char L_msg [100] ; \
00067  snprintf(L_msg, 100, (m), (P1),(P2)) ; \
00068 (*m_log_info)(L_msg); \
00069 }
00070 
00071 
00072 
00073 C_TransOCTcap32::C_TransOCTcap32() {
00074   m_cnx_id = -1 ; 
00075   m_stack = NULL ;
00076 
00077   // m_cnx_info_map.clear () ;
00078 
00079   m_library_name = NULL ;
00080   m_flavour = E_FLAVOUR_UNKNOWN ;
00081 }
00082 
00083 C_TransOCTcap32::~C_TransOCTcap32() {
00084 
00085 
00086   // T_CnxInfoList::iterator L_it ;
00087   // T_pCnxInfo              L_cnx_info ;
00088 
00089   // T_CnxInfoMap::iterator  L_it_map ; 
00090 
00091   m_cnx_id = -1 ; 
00092   DELETE_VAR(m_stack);
00093   FREE_TABLE(m_library_name);
00094   m_flavour = E_FLAVOUR_UNKNOWN ;
00095 
00096 
00097 //    if (! m_cnx_info_map.empty()) {
00098 //      for(L_it_map = m_cnx_info_map.begin();
00099 //      L_it_map != m_cnx_info_map.end();
00100 //      L_it_map++) {
00101 //        L_cnx_info = L_it_map->second ;
00102 //        delete_CnxInfo(&L_cnx_info);
00103 //      }
00104 //      m_cnx_info_map.erase(m_cnx_info_map.begin(), 
00105 //                       m_cnx_info_map.end());
00106 //    }
00107 
00108 }
00109 
00110 
00111 int C_TransOCTcap32::config (T_pConfigValueList P_config_param_list) {
00112   return(m_stack->config(P_config_param_list));
00113 }
00114 
00115 
00116 int C_TransOCTcap32::init (char* P_buf,
00117                            T_logFunction P_logError, 
00118                            T_logFunction P_logInfo) {
00119 
00120   int L_ret = 0 ;
00121 
00122   //  TTL_M_INIT ((char*)"SEAGULL");
00123 
00124   m_log_error = P_logError ;
00125   m_log_info = P_logInfo ;
00126   L_ret = analyze_init_string(P_buf) ;
00127   if (L_ret == 0) {
00128     L_ret = m_stack->init(m_log_error, m_log_info) ;
00129   }
00130   return (L_ret);
00131 }
00132 
00133 int C_TransOCTcap32::close () {
00134 
00135   // map access
00136 
00137 //    T_pCnxInfo              L_cnx_info ;
00138 //    int                     L_ret  = 0 ;
00139 //    T_CnxInfoMap::iterator  L_it_map   ; 
00140 
00141 //    if (! m_cnx_info_map.empty()) {
00142 //      for(L_it_map = m_cnx_info_map.begin();
00143 //      L_it_map != m_cnx_info_map.end();
00144 //      L_it_map++) {
00145 //        L_cnx_info = L_it_map->second ;
00146 //        L_ret = m_stack->close(L_cnx_info->m_cnx_id) ;
00147 //        if (L_ret == -1) {
00148 //      break ;
00149 //        }
00150 //      }
00151 //    }
00152 
00153   // return (L_ret);
00154 
00155 
00156   return (m_stack->close(m_cnx_id));
00157 
00158 }
00159 
00160 int C_TransOCTcap32::open (int              P_channel_id,
00161                            char*            P_buf, 
00162                            T_pOpenStatus    P_status,
00163                            C_ProtocolFrame *P_protocol) {
00164 
00165   int L_ret = 0 ;
00166   C_ProtocolExternalFrame* L_protocol = NULL ; 
00167   
00168   T_pCnxInfo L_cnxInfo = NULL ;
00169 
00170   if (L_ret != -1) {
00171     L_protocol 
00172       = dynamic_cast<C_ProtocolExternalFrame*> (P_protocol);
00173     if (L_protocol == NULL) {
00174       LOG_ERROR("protocol type not compatible (external expected)");
00175       L_ret = -1 ;
00176     }
00177   }
00178   
00179   if (L_ret != -1) {
00180     L_cnxInfo = create_CnxInfo() ;
00181     L_ret = (analyze_open_string(P_buf, L_cnxInfo) == true) ? 
00182       m_stack->open(P_channel_id, L_cnxInfo->m_class_value,
00183                     L_cnxInfo->m_inst_id,L_cnxInfo->m_app_id,L_cnxInfo->m_ossn,
00184                     L_protocol) : -1 ;
00185 
00186     if (L_ret != -1) {    
00187       m_cnx_id = L_ret ;
00188 
00189       LOG_ALL_P2("Stack name [%s] and ossn [%d]", 
00190               L_cnxInfo->m_class_value, 
00191               L_cnxInfo->m_ossn);
00192 
00193       LOG_ALL_P2("instance id [%d] and application id [%d]", 
00194               L_cnxInfo->m_inst_id, 
00195               L_cnxInfo->m_app_id);
00196 
00197       LOG_ALL_P1("Stack opened with connection id [%d]" , m_cnx_id);
00198 
00199       L_cnxInfo->m_cnx_id = L_ret ;
00200       // m_cnx_info_map.insert(T_CnxInfoMap::value_type(L_ret, L_cnxInfo));
00201       delete_CnxInfo(&L_cnxInfo);
00202       (*P_status) =  E_OPEN_OK ;
00203     } else {
00204       delete_CnxInfo(&L_cnxInfo);
00205       (*P_status) =  E_OPEN_FAILED ;
00206     }
00207   }
00208 
00209   return (L_ret);
00210 }
00211 
00212 int C_TransOCTcap32::close (int P_id) {
00213 
00214   // map access
00215 
00216 //    T_pCnxInfo              L_cnx_info ;
00217 //    int                     L_ret  = 0 ;
00218 //    T_CnxInfoMap::iterator  L_it_map   ; 
00219 
00220 
00221 //    L_it_map = m_cnx_info_map.find(T_CnxInfoMap::key_type(P_id));
00222 //    if (L_it_map == m_cnx_info_map.end()) {
00223 //      LOG_ERROR_P1("Unable to find cnxId [%d]", P_id);
00224 //      L_ret = -1 ;
00225 //    }
00226 
00227 //    if (L_ret != -1) {
00228 //      L_cnx_info = L_it_map->second ;
00229 //      L_ret = m_stack->close(L_cnx_info->m_cnx_id) ;
00230 //    }
00231 
00232 
00233   // return (L_ret);
00234 
00235 
00236   return (m_stack->close(P_id));
00237 }
00238 
00239 int C_TransOCTcap32::pre_select (int                   P_n, 
00240                                  fd_set               *P_readfds,  
00241                                  fd_set               *P_writefds,
00242                                  fd_set               *P_exceptfds, 
00243                                  struct timeval       *P_timeout, 
00244                                  int                  *P_cnx, 
00245                                  size_t                P_nb) {
00246 
00247   // map access
00248 
00249   return (m_stack->pre_select (P_n, P_readfds,
00250                                P_writefds, P_exceptfds, P_timeout)) ;
00251 }
00252 
00253 
00254 int C_TransOCTcap32::post_select (int                  P_n, 
00255                                   fd_set              *P_readfds,  
00256                                   fd_set              *P_writefds,
00257                                   fd_set              *P_exceptfds, 
00258                                   T_pC_TransportEvent  P_eventTable,
00259                                   size_t              *P_nb) {
00260 
00261   // map access
00262   
00263   return (m_stack->post_select (P_n, P_readfds, P_writefds,
00264                                 P_exceptfds, P_eventTable, P_nb));
00265 }
00266 
00267 bool C_TransOCTcap32::get_message(int P_id, T_pReceiveMsgContext P_ctxt) {
00268   return (m_stack->get_message(P_id, P_ctxt));
00269 }
00270 
00271 int C_TransOCTcap32::send_message(int P_id, C_MessageFrame *P_msg) {
00272   return (m_stack->send_message(P_id, P_msg));
00273 }
00274 
00275 T_SelectDef C_TransOCTcap32::select_definition () {
00276   return (NULL);
00277 }
00278 
00279 int C_TransOCTcap32::analyze_init_string (char *P_buf) {
00280 
00281   int        L_ret = 0 ;
00282   char      *L_value ;
00283   char      *L_path_value = NULL ;
00284   char      *L_lib_name_value = NULL ;
00285 
00286   L_value = find_value(P_buf, (char*)"flavour");
00287   if (L_value == NULL) {
00288     LOG_ERROR("Flavour not defined in your config file");
00289     L_ret = -1 ;
00290   } else {
00291     m_flavour = analyze_flavour (L_value);
00292     if (m_flavour != C_TransOCTcap32::E_FLAVOUR_UNKNOWN) {
00293       L_path_value = find_value(P_buf, (char*)"path");
00294       L_lib_name_value = find_value(P_buf, (char*)"library");
00295       L_ret = find_library (L_value, L_path_value, L_lib_name_value);
00296 
00297       if (L_ret == 0) {
00298         if (m_flavour == C_TransOCTcap32::E_FLAVOUR_ITU) {
00299           m_stack = create_tcap_itu_stack(m_library_name);
00300           //      NEW_VAR(m_stack, C_TcapStackITU(m_library_name));
00301         } else {
00302           m_stack = create_tcap_ansi_stack(m_library_name);
00303           //      NEW_VAR(m_stack, C_TcapStackANSI(m_library_name));
00304         }
00305       }
00306 
00307       FREE_TABLE(L_path_value);
00308       FREE_TABLE(L_lib_name_value);
00309 
00310     } else {
00311       LOG_ERROR("Flavour unknown");
00312       L_ret = -1 ;
00313     }
00314     FREE_TABLE(L_value);
00315   }
00316 
00317   return (L_ret);
00318 }
00319 
00320 
00321 bool C_TransOCTcap32::analyze_open_string (char *P_buf, 
00322                                            T_pCnxInfo P_cnx_info) {
00323 
00324   char *L_ossn_value = NULL ;
00325   char *L_appl_value = NULL ;
00326   char *L_inst_value = NULL ;
00327   char *L_end_ptr           ;
00328   bool  L_ret  = true       ;
00329 
00330 
00331   P_cnx_info->m_class_value = find_value(P_buf, (char *)"class");
00332   if (P_cnx_info->m_class_value == NULL) {
00333     LOG_ERROR("No class defined");
00334     L_ret = false ;
00335   } 
00336 
00337   L_ossn_value = find_value(P_buf, (char *)"ossn");
00338   if (L_ossn_value == NULL) {
00339     LOG_ERROR("No ossn defined");
00340     L_ret = false ;
00341   } else {
00342     L_end_ptr = NULL ;
00343     P_cnx_info->m_ossn = strtol(L_ossn_value, &L_end_ptr, 10);
00344     if (L_end_ptr[0] != '\0') {
00345       LOG_ERROR("ossn bad format");
00346 
00347       L_ret = false ;
00348     }
00349   }
00350 
00351   L_appl_value = find_value(P_buf, (char *)"application");
00352   if (L_appl_value == NULL) {
00353     LOG_ERROR("No application id defined");
00354     L_ret = false ;
00355   } else {
00356     L_end_ptr = NULL ;
00357     P_cnx_info->m_app_id = strtol(L_appl_value, &L_end_ptr, 10);
00358     if (L_end_ptr[0] != '\0') {
00359       LOG_ERROR("application id bad format");
00360       L_ret = false ;
00361     }
00362   }
00363 
00364   L_inst_value = find_value(P_buf, (char *)"instance");
00365   if (L_inst_value == NULL) {
00366     LOG_ERROR("No instance id defined");
00367     L_ret = false ;
00368   } else {
00369     L_end_ptr = NULL ;
00370     P_cnx_info->m_inst_id = strtol(L_inst_value, &L_end_ptr, 10);
00371     if (L_end_ptr[0] != '\0') {
00372       LOG_ERROR("instance id bad format");
00373       L_ret = false ;
00374     }
00375   }
00376   return (L_ret) ;
00377 }
00378 
00379 char* C_TransOCTcap32::find_value (char *P_buf, char *P_field) {
00380 
00381   char *L_value = NULL ;
00382 
00383   regex_t    L_reg_expr ;
00384   int        L_status ;
00385   char       L_buffer[100];
00386   regmatch_t L_pmatch[3] ;
00387   size_t     L_size = 0 ;
00388 
00389   string_t   L_string = "" ;
00390   
00391   L_string  = "([[:blank:]]*" ;
00392   L_string += P_field ;
00393   L_string += "[[:blank:]]*=[[:blank:]]*)([^[:blank:];]+)";
00394 
00395   L_status = regcomp (&L_reg_expr, 
00396                       L_string.c_str(),
00397                       REG_EXTENDED) ;
00398 
00399   if (L_status != 0) {
00400     regerror(L_status, &L_reg_expr, L_buffer, 100);
00401     regfree (&L_reg_expr) ;
00402 
00403   } else {
00404   
00405     L_status = regexec (&L_reg_expr, P_buf, 3, L_pmatch, 0) ;
00406     regfree (&L_reg_expr) ;
00407 
00408     if (L_status == 0) {
00409       
00410       L_size = L_pmatch[2].rm_eo - L_pmatch[2].rm_so ;
00411       
00412       ALLOC_TABLE(L_value, char*, sizeof(char), L_size+1);
00413       memcpy(L_value, &(P_buf[L_pmatch[2].rm_so]), L_size);
00414       L_value[L_size]='\0' ;
00415       
00416     } 
00417   }
00418 
00419 
00420   return (L_value);
00421 }
00422 
00423 C_TransOCTcap32::T_FlavourKey C_TransOCTcap32::analyze_flavour(char *P_name) {
00424 
00425   static const char* L_flavour_name_table[] = {"ITU", "WBB", "WAA",
00426                                                "ANSI", "AAA", "ABB"};
00427   static const int   L_nb_supported = 6 ;
00428   C_TransOCTcap32::T_FlavourKey L_ret = C_TransOCTcap32::E_FLAVOUR_UNKNOWN ;
00429 
00430   int L_flavour = 0 ;
00431   for (L_flavour = 0; L_flavour < L_nb_supported; L_flavour++) {
00432     if (strcmp(P_name, L_flavour_name_table[L_flavour]) == 0) {
00433       L_ret = (L_flavour < 3) ? C_TransOCTcap32::E_FLAVOUR_ITU : C_TransOCTcap32::E_FLAVOUR_ANSI ;
00434       break ;
00435     }
00436   }
00437   return (L_ret);
00438 }
00439 
00440 int C_TransOCTcap32::find_library(char *P_name, char *P_path, char *P_lib) {
00441   
00442   int L_ret = 0 ;
00443 
00444   if ((P_path != NULL) && (P_lib != NULL)) {
00445     m_library_name = verify_path(P_path, P_lib) ;
00446     L_ret = (m_library_name != NULL) ? 0 : -1 ;
00447   } else {
00448     L_ret = determine_lib(P_name);
00449     if (L_ret == -1) {
00450       LOG_ERROR("library not found");
00451     }
00452   }
00453   return (L_ret);
00454 }
00455 
00456 char *C_TransOCTcap32::verify_path(char * P_libPath, char * P_libName) {
00457 
00458   char             *L_Path = NULL ;
00459   FILE             *L_fp ;
00460 
00461   ALLOC_TABLE(L_Path, char*, sizeof(char), strlen(P_libName)+strlen(P_libPath)+1);
00462   sprintf(L_Path,"%s/%s",P_libPath,P_libName);
00463 
00464   if((L_fp = (fopen(L_Path,"rb"))) == NULL) {
00465     FREE_TABLE(L_Path);
00466   } else {
00467     fclose(L_fp);
00468   }
00469   return (L_Path);
00470 }
00471 
00472 int C_TransOCTcap32::determine_lib(char *P_name) {
00473 
00474   char              L_LibName_Aux[20];
00475   char              L_LibName_sl[30];
00476   char              L_LibName_so[30];
00477   int               L_result = -1;
00478   char             *L_FlavourName = P_name ;
00479   int               L_i = 0 ;
00480 
00481   static const char* L_Lib_Path[] = {"/opt/OC/lib/hpux64", 
00482                                      "/opt/OC/lib/hpux32",
00483                                      "/opt/OC/lib/"};
00484   static const int   L_nb_path  = 3 ;
00485 
00486   L_LibName_Aux[0] = '\0';
00487 
00488   strcpy(L_LibName_Aux,"libSS7util");
00489   sprintf(L_LibName_Aux,"%s%s",L_LibName_Aux, L_FlavourName);
00490 
00491   strcpy(L_LibName_sl,L_LibName_Aux);
00492   sprintf(L_LibName_sl,"%s%s",L_LibName_sl, ".sl");
00493 
00494   strcpy(L_LibName_so,L_LibName_Aux);
00495   sprintf(L_LibName_so,"%s%s",L_LibName_so, ".so");
00496 
00497   for (L_i = 0; L_i < L_nb_path; L_i++) {
00498     // build LibName with .so
00499     m_library_name = verify_path((char *)L_Lib_Path[L_i],L_LibName_so) ;
00500     if (m_library_name != NULL) {
00501       L_result = 0 ;
00502       break ;
00503     } else {
00504       // build LibName with .sl
00505       m_library_name = verify_path((char *)L_Lib_Path[L_i],L_LibName_sl) ;
00506       if(m_library_name != NULL) { 
00507         L_result = 0 ;
00508         break ;
00509       } 
00510     }
00511   }
00512   return (L_result);
00513 }
00514 
00515 T_pTransport create_ctransoctcap_instance () {
00516   C_TransOCTcap32 *L_inst ;
00517   NEW_VAR(L_inst, C_TransOCTcap32());
00518   return (L_inst);
00519 }
00520 
00521 void delete_ctransoctcap_instance (T_ppTransport P_inst) {
00522   if (P_inst != NULL) {
00523     C_TransOCTcap32* L_inst = dynamic_cast<C_TransOCTcap32*>(*P_inst) ;
00524     DELETE_VAR(L_inst);
00525     *P_inst = NULL ;
00526   }
00527 }
00528 
00529 
00530 
00531 
00532 // end of file

Generated on Wed Mar 7 14:44:59 2007 for Seagull by doxygen1.2.14 written by Dimitri van Heesch, © 1997-2002