osp_mod.c

Go to the documentation of this file.
00001 /*
00002  * openser osp module. 
00003  *
00004  * This module enables openser to communicate with an Open Settlement 
00005  * Protocol (OSP) server.  The Open Settlement Protocol is an ETSI 
00006  * defined standard for Inter-Domain VoIP pricing, authorization
00007  * and usage exchange.  The technical specifications for OSP 
00008  * (ETSI TS 101 321 V4.1.1) are available at www.etsi.org.
00009  *
00010  * Uli Abend was the original contributor to this module.
00011  * 
00012  * Copyright (C) 2001-2005 Fhg Fokus
00013  *
00014  * This file is part of Kamailio, a free SIP server.
00015  *
00016  * Kamailio is free software; you can redistribute it and/or modify
00017  * it under the terms of the GNU General Public License as published by
00018  * the Free Software Foundation; either version 2 of the License, or
00019  * (at your option) any later version
00020  *
00021  * Kamailio is distributed in the hope that it will be useful,
00022  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00023  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00024  * GNU General Public License for more details.
00025  *
00026  * You should have received a copy of the GNU General Public License
00027  * along with this program; if not, write to the Free Software
00028  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00029  *
00030  * History:
00031  * ---------
00032  *  2006-03-13  RR functions are loaded via API function (bogdan)
00033  */
00034 
00035 #include <osp/osp.h>
00036 #include "../rr/api.h"
00037 #include "../auth/api.h"
00038 #include "osp_mod.h"
00039 #include "orig_transaction.h"
00040 #include "term_transaction.h"
00041 #include "usage.h"
00042 #include "tm.h"
00043 #include "provider.h"
00044 
00045 MODULE_VERSION
00046 
00047 extern unsigned int _osp_sp_number;
00048 extern char* _osp_sp_uris[];
00049 extern unsigned long _osp_sp_weights[];
00050 extern char* _osp_device_ip;
00051 extern char* _osp_device_port;
00052 extern unsigned char* _osp_private_key;
00053 extern unsigned char* _osp_local_certificate;
00054 extern unsigned char* _osp_ca_certificate;
00055 extern int _osp_crypto_hw;
00056 extern int _osp_validate_callid;
00057 extern int _osp_token_format;
00058 extern int _osp_ssl_lifetime;
00059 extern int _osp_persistence;
00060 extern int _osp_retry_delay;
00061 extern int _osp_retry_limit;
00062 extern int _osp_timeout;
00063 extern int _osp_max_dests;
00064 extern int _osp_use_rpid;
00065 extern int _osp_redir_uri;
00066 extern char _osp_PRIVATE_KEY[];
00067 extern char _osp_LOCAL_CERTIFICATE[];
00068 extern char _osp_CA_CERTIFICATE[];
00069 extern char* _osp_snid_avp;
00070 extern int_str _osp_snid_avpname;
00071 extern unsigned short _osp_snid_avptype;
00072 extern OSPTPROVHANDLE _osp_provider;
00073 
00074 struct rr_binds osp_rr;
00075 auth_api_t osp_auth;
00076 int osp_index[OSP_DEF_SPS];
00077 
00078 static int ospInitMod(void);
00079 static void ospDestMod(void);
00080 static int ospInitChild(int);
00081 static int  ospVerifyParameters(void);
00082 static void ospDumpParameters(void);
00083 
00084 static cmd_export_t cmds[]={
00085     {"checkospheader",          (cmd_function)ospCheckHeader,      0, 0, 0, REQUEST_ROUTE|FAILURE_ROUTE}, 
00086     {"validateospheader",       (cmd_function)ospValidateHeader,   0, 0, 0, REQUEST_ROUTE|FAILURE_ROUTE}, 
00087     {"requestosprouting",       (cmd_function)ospRequestRouting,   0, 0, 0, REQUEST_ROUTE|FAILURE_ROUTE}, 
00088     {"checkosproute",           (cmd_function)ospCheckRoute,       0, 0, 0, REQUEST_ROUTE|FAILURE_ROUTE}, 
00089     {"prepareosproute",         (cmd_function)ospPrepareRoute,     0, 0, 0, BRANCH_ROUTE}, 
00090     {"prepareallosproutes",     (cmd_function)ospPrepareAllRoutes, 0, 0, 0, REQUEST_ROUTE|FAILURE_ROUTE}, 
00091     {"checkcallingtranslation", (cmd_function)ospCheckTranslation, 0, 0, 0, BRANCH_ROUTE}, 
00092     {"reportospusage",          (cmd_function)ospReportUsage,      1, 0, 0, REQUEST_ROUTE}, 
00093     {0, 0, 0, 0, 0, 0}
00094 };
00095 
00096 static param_export_t params[]={
00097     {"sp1_uri",                        STR_PARAM, &_osp_sp_uris[0]},
00098     {"sp2_uri",                        STR_PARAM, &_osp_sp_uris[1]},
00099     {"sp3_uri",                        STR_PARAM, &_osp_sp_uris[2]},
00100     {"sp4_uri",                        STR_PARAM, &_osp_sp_uris[3]},
00101     {"sp5_uri",                        STR_PARAM, &_osp_sp_uris[4]},
00102     {"sp6_uri",                        STR_PARAM, &_osp_sp_uris[5]},
00103     {"sp7_uri",                        STR_PARAM, &_osp_sp_uris[6]},
00104     {"sp8_uri",                        STR_PARAM, &_osp_sp_uris[7]},
00105     {"sp9_uri",                        STR_PARAM, &_osp_sp_uris[8]},
00106     {"sp10_uri",                       STR_PARAM, &_osp_sp_uris[9]},
00107     {"sp11_uri",                       STR_PARAM, &_osp_sp_uris[10]},
00108     {"sp12_uri",                       STR_PARAM, &_osp_sp_uris[11]},
00109     {"sp13_uri",                       STR_PARAM, &_osp_sp_uris[12]},
00110     {"sp14_uri",                       STR_PARAM, &_osp_sp_uris[13]},
00111     {"sp15_uri",                       STR_PARAM, &_osp_sp_uris[14]},
00112     {"sp16_uri",                       STR_PARAM, &_osp_sp_uris[15]},
00113     {"sp1_weight",                     INT_PARAM, &(_osp_sp_weights[0])},
00114     {"sp2_weight",                     INT_PARAM, &(_osp_sp_weights[1])},
00115     {"sp3_weight",                     INT_PARAM, &(_osp_sp_weights[2])},
00116     {"sp4_weight",                     INT_PARAM, &(_osp_sp_weights[3])},
00117     {"sp5_weight",                     INT_PARAM, &(_osp_sp_weights[4])},
00118     {"sp6_weight",                     INT_PARAM, &(_osp_sp_weights[5])},
00119     {"sp7_weight",                     INT_PARAM, &(_osp_sp_weights[6])},
00120     {"sp8_weight",                     INT_PARAM, &(_osp_sp_weights[7])},
00121     {"sp9_weight",                     INT_PARAM, &(_osp_sp_weights[8])},
00122     {"sp10_weight",                    INT_PARAM, &(_osp_sp_weights[9])},
00123     {"sp11_weight",                    INT_PARAM, &(_osp_sp_weights[10])},
00124     {"sp12_weight",                    INT_PARAM, &(_osp_sp_weights[11])},
00125     {"sp13_weight",                    INT_PARAM, &(_osp_sp_weights[12])},
00126     {"sp14_weight",                    INT_PARAM, &(_osp_sp_weights[13])},
00127     {"sp15_weight",                    INT_PARAM, &(_osp_sp_weights[14])},
00128     {"sp16_weight",                    INT_PARAM, &(_osp_sp_weights[15])},
00129     {"device_ip",                      STR_PARAM, &_osp_device_ip},
00130     {"device_port",                    STR_PARAM, &_osp_device_port},
00131     {"private_key",                    STR_PARAM, &_osp_private_key},
00132     {"local_certificate",              STR_PARAM, &_osp_local_certificate},
00133     {"ca_certificates",                STR_PARAM, &_osp_ca_certificate},
00134     {"enable_crypto_hardware_support", INT_PARAM, &_osp_crypto_hw},
00135     {"validate_callid",                INT_PARAM, &(_osp_validate_callid)},
00136     {"token_format",                   INT_PARAM, &_osp_token_format},
00137     {"ssl_lifetime",                   INT_PARAM, &_osp_ssl_lifetime},
00138     {"persistence",                    INT_PARAM, &_osp_persistence},
00139     {"retry_delay",                    INT_PARAM, &_osp_retry_delay},
00140     {"retry_limit",                    INT_PARAM, &_osp_retry_limit},
00141     {"timeout",                        INT_PARAM, &_osp_timeout},
00142     {"max_destinations",               INT_PARAM, &_osp_max_dests},
00143     {"use_rpid_for_calling_number",    INT_PARAM, &_osp_use_rpid},
00144     {"redirection_uri_format",         INT_PARAM, &_osp_redir_uri},
00145     {"source_networkid_avp",           STR_PARAM, &_osp_snid_avp},
00146     {0,0,0} 
00147 };
00148 
00149 struct module_exports exports = {
00150     "osp",
00151     DEFAULT_DLFLAGS,    /* dlopen flags */
00152     cmds,
00153     params,
00154     0,                  /* exported statistics */
00155     0,                  /* exported MI functions */
00156     0,                  /* exported pseudo-variables */
00157     0,                  /* extra processes */
00158     ospInitMod,         /* module initialization function */
00159     0,                  /* response function*/
00160     ospDestMod,         /* destroy function */
00161     ospInitChild,       /* per-child init function */
00162 };
00163 
00164 /*
00165  * Initialize OSP module
00166  * return 0 success, -1 failure
00167  */
00168 static int ospInitMod(void)
00169 {
00170     bind_auth_t bind_auth;
00171 
00172     if (ospVerifyParameters() != 0) {
00173         /* At least one parameter incorrect -> error */
00174         return -1;   
00175     }
00176 
00177     /* Load the RR API */
00178     if (load_rr_api(&osp_rr) != 0) {
00179         LM_WARN("failed to load the RR API. Check if you load the rr module\n");
00180         LM_WARN("add_rr_param is required for reporting duration for OSP transactions\n");
00181         memset(&osp_rr, 0, sizeof(osp_rr));
00182     }
00183 
00184     /* Load the AUTH API */
00185     bind_auth = (bind_auth_t)find_export("bind_auth", 0, 0);
00186     if ((bind_auth == NULL) || (bind_auth(&osp_auth) != 0)) {
00187         LM_WARN("failed to load the AUTH API. Check if you load the auth module.\n");
00188         LM_WARN("rpid_avp & rpid_avp_type is required for calling number translation\n");
00189         memset(&osp_auth, 0, sizeof(osp_auth));
00190     }
00191 
00192     if (ospInitTm() < 0) {
00193         return -1;
00194     }
00195 
00196     /* everything is fine, initialization done */
00197     return 0;
00198 }
00199 
00200 /*
00201  * Destrroy OSP module
00202  */
00203 static void ospDestMod(void)
00204 {
00205 }
00206 
00207 /*
00208  * Initializeild process of OSP module
00209  * param rank
00210  * return 0 success, -1 failure
00211  */
00212 static int ospInitChild(
00213     int rank)
00214 {
00215     int code = -1;
00216 
00217     code = ospSetupProvider();
00218 
00219     LM_DBG("provider '%d' (%d)\n", _osp_provider, code);
00220 
00221     return 0;
00222 }
00223 
00224 /*
00225  * Verify parameters for OSP module
00226  * return 0 success, -1 failure
00227  */
00228 static int ospVerifyParameters(void)
00229 {
00230     int i;
00231     pv_spec_t avp_spec;
00232     str avp_str;
00233     int result = 0;
00234 
00235     /* Default location for the cert files is in the compile time variable CFG_DIR */
00236     if (_osp_private_key == NULL) {
00237         sprintf(_osp_PRIVATE_KEY, "%spkey.pem", CFG_DIR);
00238         _osp_private_key = (unsigned char*)_osp_PRIVATE_KEY;
00239     } 
00240 
00241     if (_osp_local_certificate == NULL) {
00242         sprintf(_osp_LOCAL_CERTIFICATE, "%slocalcert.pem", CFG_DIR);
00243         _osp_local_certificate = (unsigned char*)_osp_LOCAL_CERTIFICATE;
00244     }
00245 
00246     if (_osp_ca_certificate == NULL) {
00247         sprintf(_osp_CA_CERTIFICATE, "%scacert_0.pem", CFG_DIR);
00248         _osp_ca_certificate = (unsigned char*)_osp_CA_CERTIFICATE;
00249     }
00250 
00251     if (_osp_device_ip == NULL) {
00252         _osp_device_ip = "";
00253     }
00254 
00255     if (_osp_device_port == NULL) {
00256         _osp_device_port = "";
00257     }
00258 
00259     if (_osp_max_dests > OSP_DEF_DESTS || _osp_max_dests < 1) {
00260         _osp_max_dests = OSP_DEF_DESTS;    
00261         LM_WARN("max_destinations is out of range, reset to %d\n", OSP_DEF_DESTS); 
00262     }
00263 
00264     if (_osp_token_format < 0 || _osp_token_format > 2) {
00265         _osp_token_format = OSP_DEF_TOKEN;
00266         LM_WARN("token_format is out of range, reset to %d\n", OSP_DEF_TOKEN);
00267     }
00268 
00269     _osp_sp_number = 0;
00270     for (i = 0; i < OSP_DEF_SPS; i++) {
00271         if (_osp_sp_uris[i] != NULL) {
00272             if (_osp_sp_number != i) {
00273                 _osp_sp_uris[_osp_sp_number] = _osp_sp_uris[i];
00274                 _osp_sp_weights[_osp_sp_number] = _osp_sp_weights[i];
00275                 _osp_sp_uris[i] = NULL;
00276                 _osp_sp_weights[i] = OSP_DEF_WEIGHT;
00277             }
00278             osp_index[_osp_sp_number] = i + 1;
00279             _osp_sp_number++;
00280         }
00281     }
00282 
00283     if (_osp_sp_number == 0) {
00284         LM_ERR("at least one service point uri must be configured\n");
00285         result = -1;
00286     }
00287 
00288     if (_osp_snid_avp && *_osp_snid_avp) {
00289         avp_str.s = _osp_snid_avp;
00290         avp_str.len = strlen(_osp_snid_avp);
00291         if (pv_parse_spec(&avp_str, &avp_spec) == NULL ||
00292             avp_spec.type != PVT_AVP ||
00293             pv_get_avp_name(0, &(avp_spec.pvp), &_osp_snid_avpname, &_osp_snid_avptype) != 0)
00294         {
00295             LM_WARN("'%s' invalid AVP definition\n", _osp_snid_avp);
00296             _osp_snid_avpname.n = 0;
00297             _osp_snid_avptype = 0;
00298         }
00299     } else {
00300         _osp_snid_avpname.n = 0;
00301         _osp_snid_avptype = 0;
00302     }
00303 
00304     ospDumpParameters();
00305 
00306     return result;
00307 }
00308 
00309 /*
00310  * Dump OSP module configuration
00311  */
00312 static void ospDumpParameters(void) 
00313 {
00314     int i;
00315 
00316     LM_INFO("module configuration: ");
00317     LM_INFO("    number of service points '%d'", _osp_sp_number);
00318     for (i = 0; i < _osp_sp_number; i++) {
00319         LM_INFO("    sp%d_uri '%s' sp%d_weight '%ld' ", 
00320             osp_index[i], _osp_sp_uris[i], osp_index[i], _osp_sp_weights[i]);
00321     }
00322     LM_INFO("    device_ip '%s' device_port '%s' ", _osp_device_ip, _osp_device_port);
00323     LM_INFO("    private_key '%s' ", _osp_private_key);
00324     LM_INFO("    local_certificate '%s' ", _osp_local_certificate);
00325     LM_INFO("    ca_certificates '%s' ", _osp_ca_certificate);
00326     LM_INFO("    enable_crypto_hardware_support '%d' ", _osp_crypto_hw);
00327     LM_INFO("    token_format '%d' ", _osp_token_format);
00328     LM_INFO("    ssl_lifetime '%d' ", _osp_ssl_lifetime);
00329     LM_INFO("    persistence '%d' ", _osp_persistence);
00330     LM_INFO("    retry_delay '%d' ", _osp_retry_delay);
00331     LM_INFO("    retry_limit '%d' ", _osp_retry_limit);
00332     LM_INFO("    timeout '%d' ", _osp_timeout);
00333     LM_INFO("    validate_call_id '%d' ", _osp_validate_callid);
00334     LM_INFO("    use_rpid_for_calling_number '%d' ", _osp_use_rpid);
00335     LM_INFO("    redirection_uri_format '%d' ", _osp_redir_uri);
00336     LM_INFO("    max_destinations '%d'\n", _osp_max_dests);
00337     if (_osp_snid_avpname.n == 0) {
00338         LM_INFO("    source network ID disabled\n");
00339     } else if (_osp_snid_avptype & AVP_NAME_STR) {
00340         LM_INFO("    source network ID AVP name '%.*s'\n", _osp_snid_avpname.s.len, _osp_snid_avpname.s.s);
00341     } else {
00342         LM_INFO("    source network ID AVP ID '%d'\n", _osp_snid_avpname.n);
00343     }
00344 }
00345 

Generated on Thu May 24 00:00:28 2012 for Kamailio - The Open Source SIP Server by  doxygen 1.5.6