sst.c

Go to the documentation of this file.
00001 /*
00002  * $Id$
00003  *
00004  * SIP Session Timer (sst) module - support for tracking dialogs and
00005  * SIP Session Timers.
00006  *
00007  * Copyright (C) 2006 SOMA Networks, INC.
00008  * Written by: Ron Winacott (karwin)
00009  *
00010  * This file is part of SIP-router, a free SIP server.
00011  *
00012  * SIP-router is free software; you can redistribute it and/or modify it
00013  * under the terms of the GNU General Public License as published by
00014  * the Free Software Foundation; either version 2 of the License, or
00015  * (at your option) any later version
00016  *
00017  * SIP-router is distributed in the hope that it will be useful, but
00018  * WITHOUT ANY WARRANTY; without even the implied warranty of
00019  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00020  * General Public License for more details.
00021  *
00022  * You should have received a copy of the GNU General Public License
00023  * along with this program; if not, write to the Free Software
00024  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
00025  * USA
00026  *
00027  * History:
00028  * --------
00029  * 2006-05-11 initial version (karwin)
00030  * 2006-10-10 RFC compilent changes. Added the other flags (karwin)
00031  */
00032 
00046 #include <stdio.h>
00047 #include <string.h>
00048 #include <stdlib.h>
00049 
00050 #include "../../modules/sl/sl.h"
00051 #include "sst_handlers.h" /* also includes sr_module.h needed by
00052                              handlers */
00053 #ifdef STATISTICS
00054 #include "../../lib/kcore/kstats_wrapper.h"
00055 #endif
00056 
00057 MODULE_VERSION
00058 
00059 static int mod_init(void);
00060 
00061 
00063 sl_api_t slb;
00064 
00065 /*
00066  * statistic variables 
00067  */
00068 int sst_enable_stats = 1;
00069 stat_var *expired_sst = 0;
00070 
00077 pv_spec_t timeout_avp; 
00078 static char* timeout_spec = 0; 
00086 unsigned int sst_minSE = 90; 
00087 
00092 unsigned int sst_reject = 1;
00093 
00095 static int sst_flag = -1;
00096 
00097 
00098 /*
00099  * Binding to the dialog module
00100  */
00101 struct dlg_binds dialog_st;
00102 struct dlg_binds *dlg_binds = &dialog_st;
00103 
00104 /*
00105  * Script commands we export.
00106  */
00107 static cmd_export_t cmds[]={
00108         {"sstCheckMin", (cmd_function)sst_check_min, 1, 0, 0, REQUEST_ROUTE | ONREPLY_ROUTE },
00109         {0,0,0,0,0,0}
00110 };
00111 
00112 /*
00113  * Script parameters
00114  */
00115 static param_export_t mod_params[]={
00116         { "enable_stats", INT_PARAM, &sst_enable_stats                  },
00117         { "min_se", INT_PARAM, &sst_minSE                               },
00118         { "timeout_avp", STR_PARAM, &timeout_spec                       },
00119         { "reject_to_small",            INT_PARAM, &sst_reject          },
00120         { "sst_flag",                           INT_PARAM, &sst_flag    },
00121         { 0,0,0 }
00122 };
00123 
00124 #ifdef STATISTICS
00125 /*
00126  * Export the statistics we have
00127  */
00128 static stat_export_t mod_stats[] = {
00129         {"expired_sst", 0,  &expired_sst},
00130         {0,0,0}
00131 };
00132 #endif /* STATISTICS */
00133 
00134 struct module_exports exports= {
00135         "sst",        /* module's name */
00136         DEFAULT_DLFLAGS, /* dlopen flags */
00137         cmds,         /* exported functions */
00138         mod_params,   /* param exports */
00139         0,            /* exported statistics (they are registered from mod_init) */
00140         0,            /* exported MI functions */
00141         0,            /* exported pseudo-variables */
00142         0,            /* extra processes */
00143         mod_init,     /* module initialization function */
00144         0,            /* reply processing function */
00145         0,            /* Destroy function */
00146         0             /* per-child init function */
00147 };
00148 
00159 static int mod_init(void) 
00160 {
00161         str s;
00162         /* if statistics are disabled, prevent their registration to core. */
00163         if (sst_enable_stats==0) {
00164                 exports.stats = 0;
00165         }
00166 
00167 #ifdef STATISTICS
00168         /* register statistics */
00169         if (sst_enable_stats!=0)
00170         {
00171                 if (register_module_stats( exports.name, mod_stats)!=0 ) {
00172                         LM_ERR("failed to register core statistics\n");
00173                         return -1;
00174                 }
00175         }
00176 #endif
00177 
00178 
00179         if (sst_flag == -1) {
00180                 LM_ERR("no sst flag set!!\n");
00181                 return -1;
00182         } 
00183         else if (sst_flag > MAX_FLAG) {
00184                 LM_ERR("invalid sst flag %d!!\n", sst_flag);
00185                 return -1;
00186         }
00187 
00188         if (timeout_spec != NULL) {
00189                 LM_DBG("Dialog AVP is %s", timeout_spec);
00190                 s.s = timeout_spec; s.len = strlen(s.s);
00191                 if (pv_parse_spec(&s, &timeout_avp)==0 
00192                 && (timeout_avp.type != PVT_AVP)){
00193                         LM_ERR("malformed or non AVP timeout AVP definition in '%s'\n",
00194                                         timeout_spec);
00195                         return -1;
00196                 }
00197         }
00198 
00199         /* bind the SL API */
00200         if (sl_load_api(&slb)!=0) {
00201                 LM_ERR("cannot bind to SL API\n");
00202                 return -1;
00203         }
00204 
00205         /* Init the handlers */
00206         sst_handler_init((timeout_spec?&timeout_avp:0), sst_minSE, 
00207                         sst_flag, sst_reject);
00208 
00209         /* Register the main (static) dialog call back. */
00210         if (load_dlg_api(&dialog_st) != 0) {
00211                 LM_ERR("failed to load dialog hooks");
00212                 return(-1);
00213         }
00214 
00215         /* Load dialog hooks */
00216         dialog_st.register_dlgcb(NULL, DLGCB_CREATED, sst_dialog_created_CB, NULL, NULL);
00217 
00218         return 0;
00219 }