pike.c

Go to the documentation of this file.
00001 /*
00002  * $Id: pike.c 4949 2008-09-18 12:02:39Z henningw $
00003  *
00004  * PIKE module
00005  *
00006  * Copyright (C) 2001-2003 FhG Fokus
00007  *
00008  * This file is part of Kamailio, a free SIP server.
00009  *
00010  * Kamailio is free software; you can redistribute it and/or modify
00011  * it under the terms of the GNU General Public License as published by
00012  * the Free Software Foundation; either version 2 of the License, or
00013  * (at your option) any later version
00014  *
00015  * Kamailio is distributed in the hope that it will be useful,
00016  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00017  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00018  * GNU General Public License for more details.
00019  *
00020  * You should have received a copy of the GNU General Public License 
00021  * along with this program; if not, write to the Free Software 
00022  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00023  *
00024  * History:
00025  * --------
00026  *  2003-03-11  updated to the new module exports interface (andrei)
00027  *  2003-03-11  converted to the new locking interface: locking.h --
00028  *               major changes (andrei)
00029  *  2003-03-16  flags export parameter added (janakj)
00030  *  2008-04-17  new parameter to control the module's log regarding the
00031  *               blocking/unblocking of IPs (bogdan)
00032  */
00033 
00034 
00035 #include <stdio.h>
00036 #include <string.h>
00037 #include <stdlib.h>
00038 #include <unistd.h>
00039 #include <fcntl.h>
00040 
00041 #include "../../sr_module.h"
00042 #include "../../error.h"
00043 #include "../../dprint.h"
00044 #include "../../ut.h"
00045 #include "../../mem/shm_mem.h"
00046 #include "../../timer.h"
00047 #include "../../locking.h"
00048 #include "ip_tree.h"
00049 #include "timer.h"
00050 #include "pike_mi.h"
00051 #include "pike_funcs.h"
00052 
00053 MODULE_VERSION
00054 
00055 
00056 
00057 static int pike_init(void);
00058 static int pike_exit(void);
00059 
00060 
00061 
00062 /* parameters */
00063 static int time_unit = 2;
00064 static int max_reqs  = 30;
00065 int timeout   = 120;
00066 int pike_log_level = L_WARN;
00067 
00068 /* global variables */
00069 gen_lock_t*             timer_lock=0;
00070 struct list_link*       timer = 0;
00071 
00072 
00073 static cmd_export_t cmds[]={
00074    {"pike_check_req", (cmd_function)pike_check_req,  0,  0, 0, REQUEST_ROUTE},
00075    {0,0,0,0,0,0}
00076 };
00077 
00078 static param_export_t params[]={
00079    {"sampling_time_unit",    INT_PARAM,  &time_unit},
00080    {"reqs_density_per_unit", INT_PARAM,  &max_reqs},
00081    {"remove_latency",        INT_PARAM,  &timeout},
00082    {"pike_log_level",        INT_PARAM, &pike_log_level},
00083    {0,0,0}
00084 };
00085 
00086 
00087 static mi_export_t mi_cmds [] = {
00088    {MI_PIKE_LIST, mi_pike_list,   MI_NO_INPUT_FLAG,  0,  0 },
00089    {0,0,0,0,0}
00090 };
00091 
00092 
00093 struct module_exports exports= {
00094    "pike",
00095    DEFAULT_DLFLAGS, /* dlopen flags */
00096    cmds,
00097    params,
00098    0,           /* exported statistics */
00099    mi_cmds,     /* exported MI functions */
00100    0,           /* exported pseudo-variables */
00101    0,           /* extra processes */
00102    pike_init,   /* module initialization function */
00103    0,
00104    (destroy_function) pike_exit,   /* module exit function */
00105    0  /* per-child init function */
00106 };
00107 
00108 
00109 
00110 
00111 static int pike_init(void)
00112 {
00113    /* alloc the timer lock */
00114    timer_lock=lock_alloc();
00115    if (timer_lock==0) {
00116       LM_ERR(" alloc locks failed!\n");
00117       goto error1;
00118    }
00119    /* init the lock */
00120    if (lock_init(timer_lock)==0){
00121       LM_ERR(" init lock failed\n");
00122       goto error1;
00123    }
00124 
00125    /* init the IP tree */
00126    if ( init_ip_tree(max_reqs)!=0 ) {
00127       LM_ERR(" ip_tree creation failed!\n");
00128       goto error2;
00129    }
00130 
00131    /* init timer list */
00132    timer = (struct list_link*)shm_malloc(sizeof(struct list_link));
00133    if (timer==0) {
00134       LM_ERR(" cannot alloc shm mem for timer!\n");
00135       goto error3;
00136    }
00137    timer->next = timer->prev = timer;
00138 
00139    /* registering timing functions  */
00140    register_timer( clean_routine , 0, 1 );
00141    register_timer( swap_routine , 0, time_unit );
00142 
00143    return 0;
00144 error3:
00145    destroy_ip_tree();
00146 error2:
00147    lock_destroy(timer_lock);
00148 error1:
00149    if (timer_lock) lock_dealloc(timer_lock);
00150    timer_lock = 0;
00151    return -1;
00152 }
00153 
00154 
00155 
00156 static int pike_exit(void)
00157 {
00158    /* destroy semaphore */
00159    if (timer_lock) {
00160       lock_destroy(timer_lock);
00161       lock_dealloc(timer_lock);
00162    }
00163 
00164    /* empty the timer list head */
00165    if (timer) {
00166       shm_free(timer);
00167       timer = 0;
00168    }
00169 
00170    /* destroy the IP tree */
00171    destroy_ip_tree();
00172 
00173    return 0;
00174 }
00175 
00176 

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